xref: /linux/net/sctp/ulpevent.c (revision c537b994505099b7197e7d3125b942ecbcc51eb6)
1 /* SCTP kernel reference Implementation
2  * (C) Copyright IBM Corp. 2001, 2004
3  * Copyright (c) 1999-2000 Cisco, Inc.
4  * Copyright (c) 1999-2001 Motorola, Inc.
5  * Copyright (c) 2001 Intel Corp.
6  * Copyright (c) 2001 Nokia, Inc.
7  * Copyright (c) 2001 La Monte H.P. Yarroll
8  *
9  * These functions manipulate an sctp event.   The struct ulpevent is used
10  * to carry notifications and data to the ULP (sockets).
11  * The SCTP reference implementation is free software;
12  * you can redistribute it and/or modify it under the terms of
13  * the GNU General Public License as published by
14  * the Free Software Foundation; either version 2, or (at your option)
15  * any later version.
16  *
17  * The SCTP reference implementation is distributed in the hope that it
18  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
19  *                 ************************
20  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21  * See the GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with GNU CC; see the file COPYING.  If not, write to
25  * the Free Software Foundation, 59 Temple Place - Suite 330,
26  * Boston, MA 02111-1307, USA.
27  *
28  * Please send any bug reports or fixes you make to the
29  * email address(es):
30  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
31  *
32  * Or submit a bug report through the following website:
33  *    http://www.sf.net/projects/lksctp
34  *
35  * Written or modified by:
36  *    Jon Grimm             <jgrimm@us.ibm.com>
37  *    La Monte H.P. Yarroll <piggy@acm.org>
38  *    Ardelle Fan	    <ardelle.fan@intel.com>
39  *    Sridhar Samudrala     <sri@us.ibm.com>
40  *
41  * Any bugs reported given to us we will try to fix... any fixes shared will
42  * be incorporated into the next SCTP release.
43  */
44 
45 #include <linux/types.h>
46 #include <linux/skbuff.h>
47 #include <net/sctp/structs.h>
48 #include <net/sctp/sctp.h>
49 #include <net/sctp/sm.h>
50 
51 static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
52 				       struct sctp_association *asoc);
53 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event);
54 static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event);
55 
56 
57 /* Initialize an ULP event from an given skb.  */
58 SCTP_STATIC void sctp_ulpevent_init(struct sctp_ulpevent *event,
59 				    int msg_flags,
60 				    unsigned int len)
61 {
62 	memset(event, 0, sizeof(struct sctp_ulpevent));
63 	event->msg_flags = msg_flags;
64 	event->rmem_len = len;
65 }
66 
67 /* Create a new sctp_ulpevent.  */
68 SCTP_STATIC struct sctp_ulpevent *sctp_ulpevent_new(int size, int msg_flags,
69 						    gfp_t gfp)
70 {
71 	struct sctp_ulpevent *event;
72 	struct sk_buff *skb;
73 
74 	skb = alloc_skb(size, gfp);
75 	if (!skb)
76 		goto fail;
77 
78 	event = sctp_skb2event(skb);
79 	sctp_ulpevent_init(event, msg_flags, skb->truesize);
80 
81 	return event;
82 
83 fail:
84 	return NULL;
85 }
86 
87 /* Is this a MSG_NOTIFICATION?  */
88 int sctp_ulpevent_is_notification(const struct sctp_ulpevent *event)
89 {
90 	return MSG_NOTIFICATION == (event->msg_flags & MSG_NOTIFICATION);
91 }
92 
93 /* Hold the association in case the msg_name needs read out of
94  * the association.
95  */
96 static inline void sctp_ulpevent_set_owner(struct sctp_ulpevent *event,
97 					   const struct sctp_association *asoc)
98 {
99 	struct sk_buff *skb;
100 
101 	/* Cast away the const, as we are just wanting to
102 	 * bump the reference count.
103 	 */
104 	sctp_association_hold((struct sctp_association *)asoc);
105 	skb = sctp_event2skb(event);
106 	event->asoc = (struct sctp_association *)asoc;
107 	atomic_add(event->rmem_len, &event->asoc->rmem_alloc);
108 	sctp_skb_set_owner_r(skb, asoc->base.sk);
109 }
110 
111 /* A simple destructor to give up the reference to the association. */
112 static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event)
113 {
114 	struct sctp_association *asoc = event->asoc;
115 
116 	atomic_sub(event->rmem_len, &asoc->rmem_alloc);
117 	sctp_association_put(asoc);
118 }
119 
120 /* Create and initialize an SCTP_ASSOC_CHANGE event.
121  *
122  * 5.3.1.1 SCTP_ASSOC_CHANGE
123  *
124  * Communication notifications inform the ULP that an SCTP association
125  * has either begun or ended. The identifier for a new association is
126  * provided by this notification.
127  *
128  * Note: There is no field checking here.  If a field is unused it will be
129  * zero'd out.
130  */
131 struct sctp_ulpevent  *sctp_ulpevent_make_assoc_change(
132 	const struct sctp_association *asoc,
133 	__u16 flags, __u16 state, __u16 error, __u16 outbound,
134 	__u16 inbound, gfp_t gfp)
135 {
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 
220 fail:
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  */
232 struct sctp_ulpevent *sctp_ulpevent_make_peer_addr_change(
233 	const struct sctp_association *asoc,
234 	const struct sockaddr_storage *aaddr,
235 	int flags, int state, int error, gfp_t gfp)
236 {
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 
327 fail:
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  */
346 struct sctp_ulpevent *sctp_ulpevent_make_remote_error(
347 	const struct sctp_association *asoc, struct sctp_chunk *chunk,
348 	__u16 flags, gfp_t gfp)
349 {
350 	struct sctp_ulpevent *event;
351 	struct sctp_remote_error *sre;
352 	struct sk_buff *skb;
353 	sctp_errhdr_t *ch;
354 	__be16 cause;
355 	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);
377 	sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
378 
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 
435 fail:
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  */
444 struct sctp_ulpevent *sctp_ulpevent_make_send_failed(
445 	const struct sctp_association *asoc, struct sctp_chunk *chunk,
446 	__u16 flags, __u32 error, gfp_t gfp)
447 {
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);
469 	sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
470 
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 
544 fail:
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  */
553 struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(
554 	const struct sctp_association *asoc,
555 	__u16 flags, gfp_t gfp)
556 {
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 
608 fail:
609 	return NULL;
610 }
611 
612 /* Create and initialize a SCTP_ADAPTATION_INDICATION notification.
613  *
614  * Socket Extensions for SCTP
615  * 5.3.1.6 SCTP_ADAPTATION_INDICATION
616  */
617 struct sctp_ulpevent *sctp_ulpevent_make_adaptation_indication(
618 	const struct sctp_association *asoc, gfp_t gfp)
619 {
620 	struct sctp_ulpevent *event;
621 	struct sctp_adaptation_event *sai;
622 	struct sk_buff *skb;
623 
624 	event = sctp_ulpevent_new(sizeof(struct sctp_adaptation_event),
625 				  MSG_NOTIFICATION, gfp);
626 	if (!event)
627 		goto fail;
628 
629 	skb = sctp_event2skb(event);
630 	sai = (struct sctp_adaptation_event *)
631 		skb_put(skb, sizeof(struct sctp_adaptation_event));
632 
633 	sai->sai_type = SCTP_ADAPTATION_INDICATION;
634 	sai->sai_flags = 0;
635 	sai->sai_length = sizeof(struct sctp_adaptation_event);
636 	sai->sai_adaptation_ind = asoc->peer.adaptation_ind;
637 	sctp_ulpevent_set_owner(event, asoc);
638 	sai->sai_assoc_id = sctp_assoc2id(asoc);
639 
640 	return event;
641 
642 fail:
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  */
653 struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
654 						struct sctp_chunk *chunk,
655 						gfp_t gfp)
656 {
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 
687 	/* 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));
692 
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) {
699 		event->flags |= SCTP_UNORDERED;
700 		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 
706 fail:
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  */
718 struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
719 	const struct sctp_association *asoc, __u32 indication,
720 	gfp_t gfp)
721 {
722 	struct sctp_ulpevent *event;
723 	struct sctp_pdapi_event *pd;
724 	struct sk_buff *skb;
725 
726 	event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),
727 				  MSG_NOTIFICATION, gfp);
728 	if (!event)
729 		goto fail;
730 
731 	skb = sctp_event2skb(event);
732 	pd = (struct sctp_pdapi_event *)
733 		skb_put(skb, sizeof(struct sctp_pdapi_event));
734 
735 	/* pdapi_type
736 	 *   It should be SCTP_PARTIAL_DELIVERY_EVENT
737 	 *
738 	 * pdapi_flags: 16 bits (unsigned integer)
739 	 *   Currently unused.
740 	 */
741 	pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
742 	pd->pdapi_flags = 0;
743 
744 	/* pdapi_length: 32 bits (unsigned integer)
745 	 *
746 	 * This field is the total length of the notification data, including
747 	 * the notification header.  It will generally be sizeof (struct
748 	 * sctp_pdapi_event).
749 	 */
750 	pd->pdapi_length = sizeof(struct sctp_pdapi_event);
751 
752 	/*  pdapi_indication: 32 bits (unsigned integer)
753 	 *
754 	 * This field holds the indication being sent to the application.
755 	 */
756 	pd->pdapi_indication = indication;
757 
758 	/*  pdapi_assoc_id: sizeof (sctp_assoc_t)
759 	 *
760 	 * The association id field, holds the identifier for the association.
761 	 */
762 	sctp_ulpevent_set_owner(event, asoc);
763 	pd->pdapi_assoc_id = sctp_assoc2id(asoc);
764 
765 	return event;
766 fail:
767 	return NULL;
768 }
769 
770 /* Return the notification type, assuming this is a notification
771  * event.
772  */
773 __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
774 {
775 	union sctp_notification *notification;
776 	struct sk_buff *skb;
777 
778 	skb = sctp_event2skb((struct sctp_ulpevent *)event);
779 	notification = (union sctp_notification *) skb->data;
780 	return notification->sn_header.sn_type;
781 }
782 
783 /* Copy out the sndrcvinfo into a msghdr.  */
784 void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
785 				   struct msghdr *msghdr)
786 {
787 	struct sctp_sndrcvinfo sinfo;
788 
789 	if (sctp_ulpevent_is_notification(event))
790 		return;
791 
792 	/* Sockets API Extensions for SCTP
793 	 * Section 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
794 	 *
795 	 * sinfo_stream: 16 bits (unsigned integer)
796 	 *
797 	 * For recvmsg() the SCTP stack places the message's stream number in
798 	 * this value.
799 	*/
800 	sinfo.sinfo_stream = event->stream;
801 	/* sinfo_ssn: 16 bits (unsigned integer)
802 	 *
803 	 * For recvmsg() this value contains the stream sequence number that
804 	 * the remote endpoint placed in the DATA chunk.  For fragmented
805 	 * messages this is the same number for all deliveries of the message
806 	 * (if more than one recvmsg() is needed to read the message).
807 	 */
808 	sinfo.sinfo_ssn = event->ssn;
809 	/* sinfo_ppid: 32 bits (unsigned integer)
810 	 *
811 	 * In recvmsg() this value is
812 	 * the same information that was passed by the upper layer in the peer
813 	 * application.  Please note that byte order issues are NOT accounted
814 	 * for and this information is passed opaquely by the SCTP stack from
815 	 * one end to the other.
816 	 */
817 	sinfo.sinfo_ppid = event->ppid;
818 	/* sinfo_flags: 16 bits (unsigned integer)
819 	 *
820 	 * This field may contain any of the following flags and is composed of
821 	 * a bitwise OR of these values.
822 	 *
823 	 * recvmsg() flags:
824 	 *
825 	 * SCTP_UNORDERED - This flag is present when the message was sent
826 	 *                 non-ordered.
827 	 */
828 	sinfo.sinfo_flags = event->flags;
829 	/* sinfo_tsn: 32 bit (unsigned integer)
830 	 *
831 	 * For the receiving side, this field holds a TSN that was
832 	 * assigned to one of the SCTP Data Chunks.
833 	 */
834 	sinfo.sinfo_tsn = event->tsn;
835 	/* sinfo_cumtsn: 32 bit (unsigned integer)
836 	 *
837 	 * This field will hold the current cumulative TSN as
838 	 * known by the underlying SCTP layer.  Note this field is
839 	 * ignored when sending and only valid for a receive
840 	 * operation when sinfo_flags are set to SCTP_UNORDERED.
841 	 */
842 	sinfo.sinfo_cumtsn = event->cumtsn;
843 	/* sinfo_assoc_id: sizeof (sctp_assoc_t)
844 	 *
845 	 * The association handle field, sinfo_assoc_id, holds the identifier
846 	 * for the association announced in the COMMUNICATION_UP notification.
847 	 * All notifications for a given association have the same identifier.
848 	 * Ignored for one-to-one style sockets.
849 	 */
850 	sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
851 
852 	/* context value that is set via SCTP_CONTEXT socket option. */
853 	sinfo.sinfo_context = event->asoc->default_rcv_context;
854 
855 	/* These fields are not used while receiving. */
856 	sinfo.sinfo_timetolive = 0;
857 
858 	put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
859 		 sizeof(struct sctp_sndrcvinfo), (void *)&sinfo);
860 }
861 
862 /* Do accounting for bytes received and hold a reference to the association
863  * for each skb.
864  */
865 static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
866 				       struct sctp_association *asoc)
867 {
868 	struct sk_buff *skb, *frag;
869 
870 	skb = sctp_event2skb(event);
871 	/* Set the owner and charge rwnd for bytes received.  */
872 	sctp_ulpevent_set_owner(event, asoc);
873 	sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
874 
875 	if (!skb->data_len)
876 		return;
877 
878 	/* Note:  Not clearing the entire event struct as this is just a
879 	 * fragment of the real event.  However, we still need to do rwnd
880 	 * accounting.
881 	 * In general, the skb passed from IP can have only 1 level of
882 	 * fragments. But we allow multiple levels of fragments.
883 	 */
884 	for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
885 		sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
886 	}
887 }
888 
889 /* Do accounting for bytes just read by user and release the references to
890  * the association.
891  */
892 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
893 {
894 	struct sk_buff *skb, *frag;
895 	unsigned int	len;
896 
897 	/* Current stack structures assume that the rcv buffer is
898 	 * per socket.   For UDP style sockets this is not true as
899 	 * multiple associations may be on a single UDP-style socket.
900 	 * Use the local private area of the skb to track the owning
901 	 * association.
902 	 */
903 
904 	skb = sctp_event2skb(event);
905 	len = skb->len;
906 
907 	if (!skb->data_len)
908 		goto done;
909 
910 	/* Don't forget the fragments. */
911 	for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
912 		/* NOTE:  skb_shinfos are recursive. Although IP returns
913 		 * skb's with only 1 level of fragments, SCTP reassembly can
914 		 * increase the levels.
915 		 */
916 		sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
917 	}
918 
919 done:
920 	sctp_assoc_rwnd_increase(event->asoc, len);
921 	sctp_ulpevent_release_owner(event);
922 }
923 
924 static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
925 {
926 	struct sk_buff *skb, *frag;
927 
928 	skb = sctp_event2skb(event);
929 
930 	if (!skb->data_len)
931 		goto done;
932 
933 	/* Don't forget the fragments. */
934 	for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
935 		/* NOTE:  skb_shinfos are recursive. Although IP returns
936 		 * skb's with only 1 level of fragments, SCTP reassembly can
937 		 * increase the levels.
938 		 */
939 		sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
940 	}
941 
942 done:
943 	sctp_ulpevent_release_owner(event);
944 }
945 
946 /* Free a ulpevent that has an owner.  It includes releasing the reference
947  * to the owner, updating the rwnd in case of a DATA event and freeing the
948  * skb.
949  */
950 void sctp_ulpevent_free(struct sctp_ulpevent *event)
951 {
952 	if (sctp_ulpevent_is_notification(event))
953 		sctp_ulpevent_release_owner(event);
954 	else
955 		sctp_ulpevent_release_data(event);
956 
957 	kfree_skb(sctp_event2skb(event));
958 }
959 
960 /* Purge the skb lists holding ulpevents. */
961 void sctp_queue_purge_ulpevents(struct sk_buff_head *list)
962 {
963 	struct sk_buff *skb;
964 	while ((skb = skb_dequeue(list)) != NULL)
965 		sctp_ulpevent_free(sctp_skb2event(skb));
966 }
967