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