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