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 int rx_count; 689 690 /* 691 * check to see if we need to make space for this 692 * new skb, expand the rcvbuffer if needed, or drop 693 * the frame 694 */ 695 if (asoc->ep->rcvbuf_policy) 696 rx_count = atomic_read(&asoc->rmem_alloc); 697 else 698 rx_count = atomic_read(&asoc->base.sk->sk_rmem_alloc); 699 700 if (rx_count >= asoc->base.sk->sk_rcvbuf) { 701 702 if ((asoc->base.sk->sk_userlocks & SOCK_RCVBUF_LOCK) || 703 (!sk_rmem_schedule(asoc->base.sk, chunk->skb->truesize))) 704 goto fail; 705 } 706 707 /* Clone the original skb, sharing the data. */ 708 skb = skb_clone(chunk->skb, gfp); 709 if (!skb) 710 goto fail; 711 712 /* First calculate the padding, so we don't inadvertently 713 * pass up the wrong length to the user. 714 * 715 * RFC 2960 - Section 3.2 Chunk Field Descriptions 716 * 717 * The total length of a chunk(including Type, Length and Value fields) 718 * MUST be a multiple of 4 bytes. If the length of the chunk is not a 719 * multiple of 4 bytes, the sender MUST pad the chunk with all zero 720 * bytes and this padding is not included in the chunk length field. 721 * The sender should never pad with more than 3 bytes. The receiver 722 * MUST ignore the padding bytes. 723 */ 724 len = ntohs(chunk->chunk_hdr->length); 725 padding = WORD_ROUND(len) - len; 726 727 /* Fixup cloned skb with just this chunks data. */ 728 skb_trim(skb, chunk->chunk_end - padding - skb->data); 729 730 /* Embed the event fields inside the cloned skb. */ 731 event = sctp_skb2event(skb); 732 733 /* Initialize event with flags 0 and correct length 734 * Since this is a clone of the original skb, only account for 735 * the data of this chunk as other chunks will be accounted separately. 736 */ 737 sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff)); 738 739 sctp_ulpevent_receive_data(event, asoc); 740 741 event->stream = ntohs(chunk->subh.data_hdr->stream); 742 event->ssn = ntohs(chunk->subh.data_hdr->ssn); 743 event->ppid = chunk->subh.data_hdr->ppid; 744 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) { 745 event->flags |= SCTP_UNORDERED; 746 event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map); 747 } 748 event->tsn = ntohl(chunk->subh.data_hdr->tsn); 749 event->msg_flags |= chunk->chunk_hdr->flags; 750 event->iif = sctp_chunk_iif(chunk); 751 752 fail: 753 return event; 754 } 755 756 /* Create a partial delivery related event. 757 * 758 * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT 759 * 760 * When a receiver is engaged in a partial delivery of a 761 * message this notification will be used to indicate 762 * various events. 763 */ 764 struct sctp_ulpevent *sctp_ulpevent_make_pdapi( 765 const struct sctp_association *asoc, __u32 indication, 766 gfp_t gfp) 767 { 768 struct sctp_ulpevent *event; 769 struct sctp_pdapi_event *pd; 770 struct sk_buff *skb; 771 772 event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event), 773 MSG_NOTIFICATION, gfp); 774 if (!event) 775 goto fail; 776 777 skb = sctp_event2skb(event); 778 pd = (struct sctp_pdapi_event *) 779 skb_put(skb, sizeof(struct sctp_pdapi_event)); 780 781 /* pdapi_type 782 * It should be SCTP_PARTIAL_DELIVERY_EVENT 783 * 784 * pdapi_flags: 16 bits (unsigned integer) 785 * Currently unused. 786 */ 787 pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT; 788 pd->pdapi_flags = 0; 789 790 /* pdapi_length: 32 bits (unsigned integer) 791 * 792 * This field is the total length of the notification data, including 793 * the notification header. It will generally be sizeof (struct 794 * sctp_pdapi_event). 795 */ 796 pd->pdapi_length = sizeof(struct sctp_pdapi_event); 797 798 /* pdapi_indication: 32 bits (unsigned integer) 799 * 800 * This field holds the indication being sent to the application. 801 */ 802 pd->pdapi_indication = indication; 803 804 /* pdapi_assoc_id: sizeof (sctp_assoc_t) 805 * 806 * The association id field, holds the identifier for the association. 807 */ 808 sctp_ulpevent_set_owner(event, asoc); 809 pd->pdapi_assoc_id = sctp_assoc2id(asoc); 810 811 return event; 812 fail: 813 return NULL; 814 } 815 816 struct sctp_ulpevent *sctp_ulpevent_make_authkey( 817 const struct sctp_association *asoc, __u16 key_id, 818 __u32 indication, gfp_t gfp) 819 { 820 struct sctp_ulpevent *event; 821 struct sctp_authkey_event *ak; 822 struct sk_buff *skb; 823 824 event = sctp_ulpevent_new(sizeof(struct sctp_authkey_event), 825 MSG_NOTIFICATION, gfp); 826 if (!event) 827 goto fail; 828 829 skb = sctp_event2skb(event); 830 ak = (struct sctp_authkey_event *) 831 skb_put(skb, sizeof(struct sctp_authkey_event)); 832 833 ak->auth_type = SCTP_AUTHENTICATION_INDICATION; 834 ak->auth_flags = 0; 835 ak->auth_length = sizeof(struct sctp_authkey_event); 836 837 ak->auth_keynumber = key_id; 838 ak->auth_altkeynumber = 0; 839 ak->auth_indication = indication; 840 841 /* 842 * The association id field, holds the identifier for the association. 843 */ 844 sctp_ulpevent_set_owner(event, asoc); 845 ak->auth_assoc_id = sctp_assoc2id(asoc); 846 847 return event; 848 fail: 849 return NULL; 850 } 851 852 853 /* Return the notification type, assuming this is a notification 854 * event. 855 */ 856 __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event) 857 { 858 union sctp_notification *notification; 859 struct sk_buff *skb; 860 861 skb = sctp_event2skb((struct sctp_ulpevent *)event); 862 notification = (union sctp_notification *) skb->data; 863 return notification->sn_header.sn_type; 864 } 865 866 /* Copy out the sndrcvinfo into a msghdr. */ 867 void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event, 868 struct msghdr *msghdr) 869 { 870 struct sctp_sndrcvinfo sinfo; 871 872 if (sctp_ulpevent_is_notification(event)) 873 return; 874 875 /* Sockets API Extensions for SCTP 876 * Section 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV) 877 * 878 * sinfo_stream: 16 bits (unsigned integer) 879 * 880 * For recvmsg() the SCTP stack places the message's stream number in 881 * this value. 882 */ 883 sinfo.sinfo_stream = event->stream; 884 /* sinfo_ssn: 16 bits (unsigned integer) 885 * 886 * For recvmsg() this value contains the stream sequence number that 887 * the remote endpoint placed in the DATA chunk. For fragmented 888 * messages this is the same number for all deliveries of the message 889 * (if more than one recvmsg() is needed to read the message). 890 */ 891 sinfo.sinfo_ssn = event->ssn; 892 /* sinfo_ppid: 32 bits (unsigned integer) 893 * 894 * In recvmsg() this value is 895 * the same information that was passed by the upper layer in the peer 896 * application. Please note that byte order issues are NOT accounted 897 * for and this information is passed opaquely by the SCTP stack from 898 * one end to the other. 899 */ 900 sinfo.sinfo_ppid = event->ppid; 901 /* sinfo_flags: 16 bits (unsigned integer) 902 * 903 * This field may contain any of the following flags and is composed of 904 * a bitwise OR of these values. 905 * 906 * recvmsg() flags: 907 * 908 * SCTP_UNORDERED - This flag is present when the message was sent 909 * non-ordered. 910 */ 911 sinfo.sinfo_flags = event->flags; 912 /* sinfo_tsn: 32 bit (unsigned integer) 913 * 914 * For the receiving side, this field holds a TSN that was 915 * assigned to one of the SCTP Data Chunks. 916 */ 917 sinfo.sinfo_tsn = event->tsn; 918 /* sinfo_cumtsn: 32 bit (unsigned integer) 919 * 920 * This field will hold the current cumulative TSN as 921 * known by the underlying SCTP layer. Note this field is 922 * ignored when sending and only valid for a receive 923 * operation when sinfo_flags are set to SCTP_UNORDERED. 924 */ 925 sinfo.sinfo_cumtsn = event->cumtsn; 926 /* sinfo_assoc_id: sizeof (sctp_assoc_t) 927 * 928 * The association handle field, sinfo_assoc_id, holds the identifier 929 * for the association announced in the COMMUNICATION_UP notification. 930 * All notifications for a given association have the same identifier. 931 * Ignored for one-to-one style sockets. 932 */ 933 sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc); 934 935 /* context value that is set via SCTP_CONTEXT socket option. */ 936 sinfo.sinfo_context = event->asoc->default_rcv_context; 937 938 /* These fields are not used while receiving. */ 939 sinfo.sinfo_timetolive = 0; 940 941 put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV, 942 sizeof(struct sctp_sndrcvinfo), (void *)&sinfo); 943 } 944 945 /* Do accounting for bytes received and hold a reference to the association 946 * for each skb. 947 */ 948 static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event, 949 struct sctp_association *asoc) 950 { 951 struct sk_buff *skb, *frag; 952 953 skb = sctp_event2skb(event); 954 /* Set the owner and charge rwnd for bytes received. */ 955 sctp_ulpevent_set_owner(event, asoc); 956 sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb)); 957 958 if (!skb->data_len) 959 return; 960 961 /* Note: Not clearing the entire event struct as this is just a 962 * fragment of the real event. However, we still need to do rwnd 963 * accounting. 964 * In general, the skb passed from IP can have only 1 level of 965 * fragments. But we allow multiple levels of fragments. 966 */ 967 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) { 968 sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc); 969 } 970 } 971 972 /* Do accounting for bytes just read by user and release the references to 973 * the association. 974 */ 975 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event) 976 { 977 struct sk_buff *skb, *frag; 978 unsigned int len; 979 980 /* Current stack structures assume that the rcv buffer is 981 * per socket. For UDP style sockets this is not true as 982 * multiple associations may be on a single UDP-style socket. 983 * Use the local private area of the skb to track the owning 984 * association. 985 */ 986 987 skb = sctp_event2skb(event); 988 len = skb->len; 989 990 if (!skb->data_len) 991 goto done; 992 993 /* Don't forget the fragments. */ 994 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) { 995 /* NOTE: skb_shinfos are recursive. Although IP returns 996 * skb's with only 1 level of fragments, SCTP reassembly can 997 * increase the levels. 998 */ 999 sctp_ulpevent_release_frag_data(sctp_skb2event(frag)); 1000 } 1001 1002 done: 1003 sctp_assoc_rwnd_increase(event->asoc, len); 1004 sctp_ulpevent_release_owner(event); 1005 } 1006 1007 static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event) 1008 { 1009 struct sk_buff *skb, *frag; 1010 1011 skb = sctp_event2skb(event); 1012 1013 if (!skb->data_len) 1014 goto done; 1015 1016 /* Don't forget the fragments. */ 1017 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) { 1018 /* NOTE: skb_shinfos are recursive. Although IP returns 1019 * skb's with only 1 level of fragments, SCTP reassembly can 1020 * increase the levels. 1021 */ 1022 sctp_ulpevent_release_frag_data(sctp_skb2event(frag)); 1023 } 1024 1025 done: 1026 sctp_ulpevent_release_owner(event); 1027 } 1028 1029 /* Free a ulpevent that has an owner. It includes releasing the reference 1030 * to the owner, updating the rwnd in case of a DATA event and freeing the 1031 * skb. 1032 */ 1033 void sctp_ulpevent_free(struct sctp_ulpevent *event) 1034 { 1035 if (sctp_ulpevent_is_notification(event)) 1036 sctp_ulpevent_release_owner(event); 1037 else 1038 sctp_ulpevent_release_data(event); 1039 1040 kfree_skb(sctp_event2skb(event)); 1041 } 1042 1043 /* Purge the skb lists holding ulpevents. */ 1044 void sctp_queue_purge_ulpevents(struct sk_buff_head *list) 1045 { 1046 struct sk_buff *skb; 1047 while ((skb = skb_dequeue(list)) != NULL) 1048 sctp_ulpevent_free(sctp_skb2event(skb)); 1049 } 1050