1 /*- 2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved. 3 * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved. 4 * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * a) Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * b) Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the distribution. 15 * 16 * c) Neither the name of Cisco Systems, Inc. nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $ */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <netinet/sctp_os.h> 39 #include <sys/proc.h> 40 #include <netinet/sctp_var.h> 41 #include <netinet/sctp_sysctl.h> 42 #include <netinet/sctp_header.h> 43 #include <netinet/sctp_pcb.h> 44 #include <netinet/sctputil.h> 45 #include <netinet/sctp_output.h> 46 #include <netinet/sctp_uio.h> 47 #include <netinet/sctputil.h> 48 #include <netinet/sctp_auth.h> 49 #include <netinet/sctp_timer.h> 50 #include <netinet/sctp_asconf.h> 51 #include <netinet/sctp_indata.h> 52 #include <netinet/sctp_bsd_addr.h> 53 #include <netinet/sctp_input.h> 54 #include <netinet/sctp_crc32.h> 55 #include <netinet/udp.h> 56 #include <machine/in_cksum.h> 57 58 59 60 #define SCTP_MAX_GAPS_INARRAY 4 61 struct sack_track { 62 uint8_t right_edge; /* mergable on the right edge */ 63 uint8_t left_edge; /* mergable on the left edge */ 64 uint8_t num_entries; 65 uint8_t spare; 66 struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY]; 67 }; 68 69 struct sack_track sack_array[256] = { 70 {0, 0, 0, 0, /* 0x00 */ 71 {{0, 0}, 72 {0, 0}, 73 {0, 0}, 74 {0, 0} 75 } 76 }, 77 {1, 0, 1, 0, /* 0x01 */ 78 {{0, 0}, 79 {0, 0}, 80 {0, 0}, 81 {0, 0} 82 } 83 }, 84 {0, 0, 1, 0, /* 0x02 */ 85 {{1, 1}, 86 {0, 0}, 87 {0, 0}, 88 {0, 0} 89 } 90 }, 91 {1, 0, 1, 0, /* 0x03 */ 92 {{0, 1}, 93 {0, 0}, 94 {0, 0}, 95 {0, 0} 96 } 97 }, 98 {0, 0, 1, 0, /* 0x04 */ 99 {{2, 2}, 100 {0, 0}, 101 {0, 0}, 102 {0, 0} 103 } 104 }, 105 {1, 0, 2, 0, /* 0x05 */ 106 {{0, 0}, 107 {2, 2}, 108 {0, 0}, 109 {0, 0} 110 } 111 }, 112 {0, 0, 1, 0, /* 0x06 */ 113 {{1, 2}, 114 {0, 0}, 115 {0, 0}, 116 {0, 0} 117 } 118 }, 119 {1, 0, 1, 0, /* 0x07 */ 120 {{0, 2}, 121 {0, 0}, 122 {0, 0}, 123 {0, 0} 124 } 125 }, 126 {0, 0, 1, 0, /* 0x08 */ 127 {{3, 3}, 128 {0, 0}, 129 {0, 0}, 130 {0, 0} 131 } 132 }, 133 {1, 0, 2, 0, /* 0x09 */ 134 {{0, 0}, 135 {3, 3}, 136 {0, 0}, 137 {0, 0} 138 } 139 }, 140 {0, 0, 2, 0, /* 0x0a */ 141 {{1, 1}, 142 {3, 3}, 143 {0, 0}, 144 {0, 0} 145 } 146 }, 147 {1, 0, 2, 0, /* 0x0b */ 148 {{0, 1}, 149 {3, 3}, 150 {0, 0}, 151 {0, 0} 152 } 153 }, 154 {0, 0, 1, 0, /* 0x0c */ 155 {{2, 3}, 156 {0, 0}, 157 {0, 0}, 158 {0, 0} 159 } 160 }, 161 {1, 0, 2, 0, /* 0x0d */ 162 {{0, 0}, 163 {2, 3}, 164 {0, 0}, 165 {0, 0} 166 } 167 }, 168 {0, 0, 1, 0, /* 0x0e */ 169 {{1, 3}, 170 {0, 0}, 171 {0, 0}, 172 {0, 0} 173 } 174 }, 175 {1, 0, 1, 0, /* 0x0f */ 176 {{0, 3}, 177 {0, 0}, 178 {0, 0}, 179 {0, 0} 180 } 181 }, 182 {0, 0, 1, 0, /* 0x10 */ 183 {{4, 4}, 184 {0, 0}, 185 {0, 0}, 186 {0, 0} 187 } 188 }, 189 {1, 0, 2, 0, /* 0x11 */ 190 {{0, 0}, 191 {4, 4}, 192 {0, 0}, 193 {0, 0} 194 } 195 }, 196 {0, 0, 2, 0, /* 0x12 */ 197 {{1, 1}, 198 {4, 4}, 199 {0, 0}, 200 {0, 0} 201 } 202 }, 203 {1, 0, 2, 0, /* 0x13 */ 204 {{0, 1}, 205 {4, 4}, 206 {0, 0}, 207 {0, 0} 208 } 209 }, 210 {0, 0, 2, 0, /* 0x14 */ 211 {{2, 2}, 212 {4, 4}, 213 {0, 0}, 214 {0, 0} 215 } 216 }, 217 {1, 0, 3, 0, /* 0x15 */ 218 {{0, 0}, 219 {2, 2}, 220 {4, 4}, 221 {0, 0} 222 } 223 }, 224 {0, 0, 2, 0, /* 0x16 */ 225 {{1, 2}, 226 {4, 4}, 227 {0, 0}, 228 {0, 0} 229 } 230 }, 231 {1, 0, 2, 0, /* 0x17 */ 232 {{0, 2}, 233 {4, 4}, 234 {0, 0}, 235 {0, 0} 236 } 237 }, 238 {0, 0, 1, 0, /* 0x18 */ 239 {{3, 4}, 240 {0, 0}, 241 {0, 0}, 242 {0, 0} 243 } 244 }, 245 {1, 0, 2, 0, /* 0x19 */ 246 {{0, 0}, 247 {3, 4}, 248 {0, 0}, 249 {0, 0} 250 } 251 }, 252 {0, 0, 2, 0, /* 0x1a */ 253 {{1, 1}, 254 {3, 4}, 255 {0, 0}, 256 {0, 0} 257 } 258 }, 259 {1, 0, 2, 0, /* 0x1b */ 260 {{0, 1}, 261 {3, 4}, 262 {0, 0}, 263 {0, 0} 264 } 265 }, 266 {0, 0, 1, 0, /* 0x1c */ 267 {{2, 4}, 268 {0, 0}, 269 {0, 0}, 270 {0, 0} 271 } 272 }, 273 {1, 0, 2, 0, /* 0x1d */ 274 {{0, 0}, 275 {2, 4}, 276 {0, 0}, 277 {0, 0} 278 } 279 }, 280 {0, 0, 1, 0, /* 0x1e */ 281 {{1, 4}, 282 {0, 0}, 283 {0, 0}, 284 {0, 0} 285 } 286 }, 287 {1, 0, 1, 0, /* 0x1f */ 288 {{0, 4}, 289 {0, 0}, 290 {0, 0}, 291 {0, 0} 292 } 293 }, 294 {0, 0, 1, 0, /* 0x20 */ 295 {{5, 5}, 296 {0, 0}, 297 {0, 0}, 298 {0, 0} 299 } 300 }, 301 {1, 0, 2, 0, /* 0x21 */ 302 {{0, 0}, 303 {5, 5}, 304 {0, 0}, 305 {0, 0} 306 } 307 }, 308 {0, 0, 2, 0, /* 0x22 */ 309 {{1, 1}, 310 {5, 5}, 311 {0, 0}, 312 {0, 0} 313 } 314 }, 315 {1, 0, 2, 0, /* 0x23 */ 316 {{0, 1}, 317 {5, 5}, 318 {0, 0}, 319 {0, 0} 320 } 321 }, 322 {0, 0, 2, 0, /* 0x24 */ 323 {{2, 2}, 324 {5, 5}, 325 {0, 0}, 326 {0, 0} 327 } 328 }, 329 {1, 0, 3, 0, /* 0x25 */ 330 {{0, 0}, 331 {2, 2}, 332 {5, 5}, 333 {0, 0} 334 } 335 }, 336 {0, 0, 2, 0, /* 0x26 */ 337 {{1, 2}, 338 {5, 5}, 339 {0, 0}, 340 {0, 0} 341 } 342 }, 343 {1, 0, 2, 0, /* 0x27 */ 344 {{0, 2}, 345 {5, 5}, 346 {0, 0}, 347 {0, 0} 348 } 349 }, 350 {0, 0, 2, 0, /* 0x28 */ 351 {{3, 3}, 352 {5, 5}, 353 {0, 0}, 354 {0, 0} 355 } 356 }, 357 {1, 0, 3, 0, /* 0x29 */ 358 {{0, 0}, 359 {3, 3}, 360 {5, 5}, 361 {0, 0} 362 } 363 }, 364 {0, 0, 3, 0, /* 0x2a */ 365 {{1, 1}, 366 {3, 3}, 367 {5, 5}, 368 {0, 0} 369 } 370 }, 371 {1, 0, 3, 0, /* 0x2b */ 372 {{0, 1}, 373 {3, 3}, 374 {5, 5}, 375 {0, 0} 376 } 377 }, 378 {0, 0, 2, 0, /* 0x2c */ 379 {{2, 3}, 380 {5, 5}, 381 {0, 0}, 382 {0, 0} 383 } 384 }, 385 {1, 0, 3, 0, /* 0x2d */ 386 {{0, 0}, 387 {2, 3}, 388 {5, 5}, 389 {0, 0} 390 } 391 }, 392 {0, 0, 2, 0, /* 0x2e */ 393 {{1, 3}, 394 {5, 5}, 395 {0, 0}, 396 {0, 0} 397 } 398 }, 399 {1, 0, 2, 0, /* 0x2f */ 400 {{0, 3}, 401 {5, 5}, 402 {0, 0}, 403 {0, 0} 404 } 405 }, 406 {0, 0, 1, 0, /* 0x30 */ 407 {{4, 5}, 408 {0, 0}, 409 {0, 0}, 410 {0, 0} 411 } 412 }, 413 {1, 0, 2, 0, /* 0x31 */ 414 {{0, 0}, 415 {4, 5}, 416 {0, 0}, 417 {0, 0} 418 } 419 }, 420 {0, 0, 2, 0, /* 0x32 */ 421 {{1, 1}, 422 {4, 5}, 423 {0, 0}, 424 {0, 0} 425 } 426 }, 427 {1, 0, 2, 0, /* 0x33 */ 428 {{0, 1}, 429 {4, 5}, 430 {0, 0}, 431 {0, 0} 432 } 433 }, 434 {0, 0, 2, 0, /* 0x34 */ 435 {{2, 2}, 436 {4, 5}, 437 {0, 0}, 438 {0, 0} 439 } 440 }, 441 {1, 0, 3, 0, /* 0x35 */ 442 {{0, 0}, 443 {2, 2}, 444 {4, 5}, 445 {0, 0} 446 } 447 }, 448 {0, 0, 2, 0, /* 0x36 */ 449 {{1, 2}, 450 {4, 5}, 451 {0, 0}, 452 {0, 0} 453 } 454 }, 455 {1, 0, 2, 0, /* 0x37 */ 456 {{0, 2}, 457 {4, 5}, 458 {0, 0}, 459 {0, 0} 460 } 461 }, 462 {0, 0, 1, 0, /* 0x38 */ 463 {{3, 5}, 464 {0, 0}, 465 {0, 0}, 466 {0, 0} 467 } 468 }, 469 {1, 0, 2, 0, /* 0x39 */ 470 {{0, 0}, 471 {3, 5}, 472 {0, 0}, 473 {0, 0} 474 } 475 }, 476 {0, 0, 2, 0, /* 0x3a */ 477 {{1, 1}, 478 {3, 5}, 479 {0, 0}, 480 {0, 0} 481 } 482 }, 483 {1, 0, 2, 0, /* 0x3b */ 484 {{0, 1}, 485 {3, 5}, 486 {0, 0}, 487 {0, 0} 488 } 489 }, 490 {0, 0, 1, 0, /* 0x3c */ 491 {{2, 5}, 492 {0, 0}, 493 {0, 0}, 494 {0, 0} 495 } 496 }, 497 {1, 0, 2, 0, /* 0x3d */ 498 {{0, 0}, 499 {2, 5}, 500 {0, 0}, 501 {0, 0} 502 } 503 }, 504 {0, 0, 1, 0, /* 0x3e */ 505 {{1, 5}, 506 {0, 0}, 507 {0, 0}, 508 {0, 0} 509 } 510 }, 511 {1, 0, 1, 0, /* 0x3f */ 512 {{0, 5}, 513 {0, 0}, 514 {0, 0}, 515 {0, 0} 516 } 517 }, 518 {0, 0, 1, 0, /* 0x40 */ 519 {{6, 6}, 520 {0, 0}, 521 {0, 0}, 522 {0, 0} 523 } 524 }, 525 {1, 0, 2, 0, /* 0x41 */ 526 {{0, 0}, 527 {6, 6}, 528 {0, 0}, 529 {0, 0} 530 } 531 }, 532 {0, 0, 2, 0, /* 0x42 */ 533 {{1, 1}, 534 {6, 6}, 535 {0, 0}, 536 {0, 0} 537 } 538 }, 539 {1, 0, 2, 0, /* 0x43 */ 540 {{0, 1}, 541 {6, 6}, 542 {0, 0}, 543 {0, 0} 544 } 545 }, 546 {0, 0, 2, 0, /* 0x44 */ 547 {{2, 2}, 548 {6, 6}, 549 {0, 0}, 550 {0, 0} 551 } 552 }, 553 {1, 0, 3, 0, /* 0x45 */ 554 {{0, 0}, 555 {2, 2}, 556 {6, 6}, 557 {0, 0} 558 } 559 }, 560 {0, 0, 2, 0, /* 0x46 */ 561 {{1, 2}, 562 {6, 6}, 563 {0, 0}, 564 {0, 0} 565 } 566 }, 567 {1, 0, 2, 0, /* 0x47 */ 568 {{0, 2}, 569 {6, 6}, 570 {0, 0}, 571 {0, 0} 572 } 573 }, 574 {0, 0, 2, 0, /* 0x48 */ 575 {{3, 3}, 576 {6, 6}, 577 {0, 0}, 578 {0, 0} 579 } 580 }, 581 {1, 0, 3, 0, /* 0x49 */ 582 {{0, 0}, 583 {3, 3}, 584 {6, 6}, 585 {0, 0} 586 } 587 }, 588 {0, 0, 3, 0, /* 0x4a */ 589 {{1, 1}, 590 {3, 3}, 591 {6, 6}, 592 {0, 0} 593 } 594 }, 595 {1, 0, 3, 0, /* 0x4b */ 596 {{0, 1}, 597 {3, 3}, 598 {6, 6}, 599 {0, 0} 600 } 601 }, 602 {0, 0, 2, 0, /* 0x4c */ 603 {{2, 3}, 604 {6, 6}, 605 {0, 0}, 606 {0, 0} 607 } 608 }, 609 {1, 0, 3, 0, /* 0x4d */ 610 {{0, 0}, 611 {2, 3}, 612 {6, 6}, 613 {0, 0} 614 } 615 }, 616 {0, 0, 2, 0, /* 0x4e */ 617 {{1, 3}, 618 {6, 6}, 619 {0, 0}, 620 {0, 0} 621 } 622 }, 623 {1, 0, 2, 0, /* 0x4f */ 624 {{0, 3}, 625 {6, 6}, 626 {0, 0}, 627 {0, 0} 628 } 629 }, 630 {0, 0, 2, 0, /* 0x50 */ 631 {{4, 4}, 632 {6, 6}, 633 {0, 0}, 634 {0, 0} 635 } 636 }, 637 {1, 0, 3, 0, /* 0x51 */ 638 {{0, 0}, 639 {4, 4}, 640 {6, 6}, 641 {0, 0} 642 } 643 }, 644 {0, 0, 3, 0, /* 0x52 */ 645 {{1, 1}, 646 {4, 4}, 647 {6, 6}, 648 {0, 0} 649 } 650 }, 651 {1, 0, 3, 0, /* 0x53 */ 652 {{0, 1}, 653 {4, 4}, 654 {6, 6}, 655 {0, 0} 656 } 657 }, 658 {0, 0, 3, 0, /* 0x54 */ 659 {{2, 2}, 660 {4, 4}, 661 {6, 6}, 662 {0, 0} 663 } 664 }, 665 {1, 0, 4, 0, /* 0x55 */ 666 {{0, 0}, 667 {2, 2}, 668 {4, 4}, 669 {6, 6} 670 } 671 }, 672 {0, 0, 3, 0, /* 0x56 */ 673 {{1, 2}, 674 {4, 4}, 675 {6, 6}, 676 {0, 0} 677 } 678 }, 679 {1, 0, 3, 0, /* 0x57 */ 680 {{0, 2}, 681 {4, 4}, 682 {6, 6}, 683 {0, 0} 684 } 685 }, 686 {0, 0, 2, 0, /* 0x58 */ 687 {{3, 4}, 688 {6, 6}, 689 {0, 0}, 690 {0, 0} 691 } 692 }, 693 {1, 0, 3, 0, /* 0x59 */ 694 {{0, 0}, 695 {3, 4}, 696 {6, 6}, 697 {0, 0} 698 } 699 }, 700 {0, 0, 3, 0, /* 0x5a */ 701 {{1, 1}, 702 {3, 4}, 703 {6, 6}, 704 {0, 0} 705 } 706 }, 707 {1, 0, 3, 0, /* 0x5b */ 708 {{0, 1}, 709 {3, 4}, 710 {6, 6}, 711 {0, 0} 712 } 713 }, 714 {0, 0, 2, 0, /* 0x5c */ 715 {{2, 4}, 716 {6, 6}, 717 {0, 0}, 718 {0, 0} 719 } 720 }, 721 {1, 0, 3, 0, /* 0x5d */ 722 {{0, 0}, 723 {2, 4}, 724 {6, 6}, 725 {0, 0} 726 } 727 }, 728 {0, 0, 2, 0, /* 0x5e */ 729 {{1, 4}, 730 {6, 6}, 731 {0, 0}, 732 {0, 0} 733 } 734 }, 735 {1, 0, 2, 0, /* 0x5f */ 736 {{0, 4}, 737 {6, 6}, 738 {0, 0}, 739 {0, 0} 740 } 741 }, 742 {0, 0, 1, 0, /* 0x60 */ 743 {{5, 6}, 744 {0, 0}, 745 {0, 0}, 746 {0, 0} 747 } 748 }, 749 {1, 0, 2, 0, /* 0x61 */ 750 {{0, 0}, 751 {5, 6}, 752 {0, 0}, 753 {0, 0} 754 } 755 }, 756 {0, 0, 2, 0, /* 0x62 */ 757 {{1, 1}, 758 {5, 6}, 759 {0, 0}, 760 {0, 0} 761 } 762 }, 763 {1, 0, 2, 0, /* 0x63 */ 764 {{0, 1}, 765 {5, 6}, 766 {0, 0}, 767 {0, 0} 768 } 769 }, 770 {0, 0, 2, 0, /* 0x64 */ 771 {{2, 2}, 772 {5, 6}, 773 {0, 0}, 774 {0, 0} 775 } 776 }, 777 {1, 0, 3, 0, /* 0x65 */ 778 {{0, 0}, 779 {2, 2}, 780 {5, 6}, 781 {0, 0} 782 } 783 }, 784 {0, 0, 2, 0, /* 0x66 */ 785 {{1, 2}, 786 {5, 6}, 787 {0, 0}, 788 {0, 0} 789 } 790 }, 791 {1, 0, 2, 0, /* 0x67 */ 792 {{0, 2}, 793 {5, 6}, 794 {0, 0}, 795 {0, 0} 796 } 797 }, 798 {0, 0, 2, 0, /* 0x68 */ 799 {{3, 3}, 800 {5, 6}, 801 {0, 0}, 802 {0, 0} 803 } 804 }, 805 {1, 0, 3, 0, /* 0x69 */ 806 {{0, 0}, 807 {3, 3}, 808 {5, 6}, 809 {0, 0} 810 } 811 }, 812 {0, 0, 3, 0, /* 0x6a */ 813 {{1, 1}, 814 {3, 3}, 815 {5, 6}, 816 {0, 0} 817 } 818 }, 819 {1, 0, 3, 0, /* 0x6b */ 820 {{0, 1}, 821 {3, 3}, 822 {5, 6}, 823 {0, 0} 824 } 825 }, 826 {0, 0, 2, 0, /* 0x6c */ 827 {{2, 3}, 828 {5, 6}, 829 {0, 0}, 830 {0, 0} 831 } 832 }, 833 {1, 0, 3, 0, /* 0x6d */ 834 {{0, 0}, 835 {2, 3}, 836 {5, 6}, 837 {0, 0} 838 } 839 }, 840 {0, 0, 2, 0, /* 0x6e */ 841 {{1, 3}, 842 {5, 6}, 843 {0, 0}, 844 {0, 0} 845 } 846 }, 847 {1, 0, 2, 0, /* 0x6f */ 848 {{0, 3}, 849 {5, 6}, 850 {0, 0}, 851 {0, 0} 852 } 853 }, 854 {0, 0, 1, 0, /* 0x70 */ 855 {{4, 6}, 856 {0, 0}, 857 {0, 0}, 858 {0, 0} 859 } 860 }, 861 {1, 0, 2, 0, /* 0x71 */ 862 {{0, 0}, 863 {4, 6}, 864 {0, 0}, 865 {0, 0} 866 } 867 }, 868 {0, 0, 2, 0, /* 0x72 */ 869 {{1, 1}, 870 {4, 6}, 871 {0, 0}, 872 {0, 0} 873 } 874 }, 875 {1, 0, 2, 0, /* 0x73 */ 876 {{0, 1}, 877 {4, 6}, 878 {0, 0}, 879 {0, 0} 880 } 881 }, 882 {0, 0, 2, 0, /* 0x74 */ 883 {{2, 2}, 884 {4, 6}, 885 {0, 0}, 886 {0, 0} 887 } 888 }, 889 {1, 0, 3, 0, /* 0x75 */ 890 {{0, 0}, 891 {2, 2}, 892 {4, 6}, 893 {0, 0} 894 } 895 }, 896 {0, 0, 2, 0, /* 0x76 */ 897 {{1, 2}, 898 {4, 6}, 899 {0, 0}, 900 {0, 0} 901 } 902 }, 903 {1, 0, 2, 0, /* 0x77 */ 904 {{0, 2}, 905 {4, 6}, 906 {0, 0}, 907 {0, 0} 908 } 909 }, 910 {0, 0, 1, 0, /* 0x78 */ 911 {{3, 6}, 912 {0, 0}, 913 {0, 0}, 914 {0, 0} 915 } 916 }, 917 {1, 0, 2, 0, /* 0x79 */ 918 {{0, 0}, 919 {3, 6}, 920 {0, 0}, 921 {0, 0} 922 } 923 }, 924 {0, 0, 2, 0, /* 0x7a */ 925 {{1, 1}, 926 {3, 6}, 927 {0, 0}, 928 {0, 0} 929 } 930 }, 931 {1, 0, 2, 0, /* 0x7b */ 932 {{0, 1}, 933 {3, 6}, 934 {0, 0}, 935 {0, 0} 936 } 937 }, 938 {0, 0, 1, 0, /* 0x7c */ 939 {{2, 6}, 940 {0, 0}, 941 {0, 0}, 942 {0, 0} 943 } 944 }, 945 {1, 0, 2, 0, /* 0x7d */ 946 {{0, 0}, 947 {2, 6}, 948 {0, 0}, 949 {0, 0} 950 } 951 }, 952 {0, 0, 1, 0, /* 0x7e */ 953 {{1, 6}, 954 {0, 0}, 955 {0, 0}, 956 {0, 0} 957 } 958 }, 959 {1, 0, 1, 0, /* 0x7f */ 960 {{0, 6}, 961 {0, 0}, 962 {0, 0}, 963 {0, 0} 964 } 965 }, 966 {0, 1, 1, 0, /* 0x80 */ 967 {{7, 7}, 968 {0, 0}, 969 {0, 0}, 970 {0, 0} 971 } 972 }, 973 {1, 1, 2, 0, /* 0x81 */ 974 {{0, 0}, 975 {7, 7}, 976 {0, 0}, 977 {0, 0} 978 } 979 }, 980 {0, 1, 2, 0, /* 0x82 */ 981 {{1, 1}, 982 {7, 7}, 983 {0, 0}, 984 {0, 0} 985 } 986 }, 987 {1, 1, 2, 0, /* 0x83 */ 988 {{0, 1}, 989 {7, 7}, 990 {0, 0}, 991 {0, 0} 992 } 993 }, 994 {0, 1, 2, 0, /* 0x84 */ 995 {{2, 2}, 996 {7, 7}, 997 {0, 0}, 998 {0, 0} 999 } 1000 }, 1001 {1, 1, 3, 0, /* 0x85 */ 1002 {{0, 0}, 1003 {2, 2}, 1004 {7, 7}, 1005 {0, 0} 1006 } 1007 }, 1008 {0, 1, 2, 0, /* 0x86 */ 1009 {{1, 2}, 1010 {7, 7}, 1011 {0, 0}, 1012 {0, 0} 1013 } 1014 }, 1015 {1, 1, 2, 0, /* 0x87 */ 1016 {{0, 2}, 1017 {7, 7}, 1018 {0, 0}, 1019 {0, 0} 1020 } 1021 }, 1022 {0, 1, 2, 0, /* 0x88 */ 1023 {{3, 3}, 1024 {7, 7}, 1025 {0, 0}, 1026 {0, 0} 1027 } 1028 }, 1029 {1, 1, 3, 0, /* 0x89 */ 1030 {{0, 0}, 1031 {3, 3}, 1032 {7, 7}, 1033 {0, 0} 1034 } 1035 }, 1036 {0, 1, 3, 0, /* 0x8a */ 1037 {{1, 1}, 1038 {3, 3}, 1039 {7, 7}, 1040 {0, 0} 1041 } 1042 }, 1043 {1, 1, 3, 0, /* 0x8b */ 1044 {{0, 1}, 1045 {3, 3}, 1046 {7, 7}, 1047 {0, 0} 1048 } 1049 }, 1050 {0, 1, 2, 0, /* 0x8c */ 1051 {{2, 3}, 1052 {7, 7}, 1053 {0, 0}, 1054 {0, 0} 1055 } 1056 }, 1057 {1, 1, 3, 0, /* 0x8d */ 1058 {{0, 0}, 1059 {2, 3}, 1060 {7, 7}, 1061 {0, 0} 1062 } 1063 }, 1064 {0, 1, 2, 0, /* 0x8e */ 1065 {{1, 3}, 1066 {7, 7}, 1067 {0, 0}, 1068 {0, 0} 1069 } 1070 }, 1071 {1, 1, 2, 0, /* 0x8f */ 1072 {{0, 3}, 1073 {7, 7}, 1074 {0, 0}, 1075 {0, 0} 1076 } 1077 }, 1078 {0, 1, 2, 0, /* 0x90 */ 1079 {{4, 4}, 1080 {7, 7}, 1081 {0, 0}, 1082 {0, 0} 1083 } 1084 }, 1085 {1, 1, 3, 0, /* 0x91 */ 1086 {{0, 0}, 1087 {4, 4}, 1088 {7, 7}, 1089 {0, 0} 1090 } 1091 }, 1092 {0, 1, 3, 0, /* 0x92 */ 1093 {{1, 1}, 1094 {4, 4}, 1095 {7, 7}, 1096 {0, 0} 1097 } 1098 }, 1099 {1, 1, 3, 0, /* 0x93 */ 1100 {{0, 1}, 1101 {4, 4}, 1102 {7, 7}, 1103 {0, 0} 1104 } 1105 }, 1106 {0, 1, 3, 0, /* 0x94 */ 1107 {{2, 2}, 1108 {4, 4}, 1109 {7, 7}, 1110 {0, 0} 1111 } 1112 }, 1113 {1, 1, 4, 0, /* 0x95 */ 1114 {{0, 0}, 1115 {2, 2}, 1116 {4, 4}, 1117 {7, 7} 1118 } 1119 }, 1120 {0, 1, 3, 0, /* 0x96 */ 1121 {{1, 2}, 1122 {4, 4}, 1123 {7, 7}, 1124 {0, 0} 1125 } 1126 }, 1127 {1, 1, 3, 0, /* 0x97 */ 1128 {{0, 2}, 1129 {4, 4}, 1130 {7, 7}, 1131 {0, 0} 1132 } 1133 }, 1134 {0, 1, 2, 0, /* 0x98 */ 1135 {{3, 4}, 1136 {7, 7}, 1137 {0, 0}, 1138 {0, 0} 1139 } 1140 }, 1141 {1, 1, 3, 0, /* 0x99 */ 1142 {{0, 0}, 1143 {3, 4}, 1144 {7, 7}, 1145 {0, 0} 1146 } 1147 }, 1148 {0, 1, 3, 0, /* 0x9a */ 1149 {{1, 1}, 1150 {3, 4}, 1151 {7, 7}, 1152 {0, 0} 1153 } 1154 }, 1155 {1, 1, 3, 0, /* 0x9b */ 1156 {{0, 1}, 1157 {3, 4}, 1158 {7, 7}, 1159 {0, 0} 1160 } 1161 }, 1162 {0, 1, 2, 0, /* 0x9c */ 1163 {{2, 4}, 1164 {7, 7}, 1165 {0, 0}, 1166 {0, 0} 1167 } 1168 }, 1169 {1, 1, 3, 0, /* 0x9d */ 1170 {{0, 0}, 1171 {2, 4}, 1172 {7, 7}, 1173 {0, 0} 1174 } 1175 }, 1176 {0, 1, 2, 0, /* 0x9e */ 1177 {{1, 4}, 1178 {7, 7}, 1179 {0, 0}, 1180 {0, 0} 1181 } 1182 }, 1183 {1, 1, 2, 0, /* 0x9f */ 1184 {{0, 4}, 1185 {7, 7}, 1186 {0, 0}, 1187 {0, 0} 1188 } 1189 }, 1190 {0, 1, 2, 0, /* 0xa0 */ 1191 {{5, 5}, 1192 {7, 7}, 1193 {0, 0}, 1194 {0, 0} 1195 } 1196 }, 1197 {1, 1, 3, 0, /* 0xa1 */ 1198 {{0, 0}, 1199 {5, 5}, 1200 {7, 7}, 1201 {0, 0} 1202 } 1203 }, 1204 {0, 1, 3, 0, /* 0xa2 */ 1205 {{1, 1}, 1206 {5, 5}, 1207 {7, 7}, 1208 {0, 0} 1209 } 1210 }, 1211 {1, 1, 3, 0, /* 0xa3 */ 1212 {{0, 1}, 1213 {5, 5}, 1214 {7, 7}, 1215 {0, 0} 1216 } 1217 }, 1218 {0, 1, 3, 0, /* 0xa4 */ 1219 {{2, 2}, 1220 {5, 5}, 1221 {7, 7}, 1222 {0, 0} 1223 } 1224 }, 1225 {1, 1, 4, 0, /* 0xa5 */ 1226 {{0, 0}, 1227 {2, 2}, 1228 {5, 5}, 1229 {7, 7} 1230 } 1231 }, 1232 {0, 1, 3, 0, /* 0xa6 */ 1233 {{1, 2}, 1234 {5, 5}, 1235 {7, 7}, 1236 {0, 0} 1237 } 1238 }, 1239 {1, 1, 3, 0, /* 0xa7 */ 1240 {{0, 2}, 1241 {5, 5}, 1242 {7, 7}, 1243 {0, 0} 1244 } 1245 }, 1246 {0, 1, 3, 0, /* 0xa8 */ 1247 {{3, 3}, 1248 {5, 5}, 1249 {7, 7}, 1250 {0, 0} 1251 } 1252 }, 1253 {1, 1, 4, 0, /* 0xa9 */ 1254 {{0, 0}, 1255 {3, 3}, 1256 {5, 5}, 1257 {7, 7} 1258 } 1259 }, 1260 {0, 1, 4, 0, /* 0xaa */ 1261 {{1, 1}, 1262 {3, 3}, 1263 {5, 5}, 1264 {7, 7} 1265 } 1266 }, 1267 {1, 1, 4, 0, /* 0xab */ 1268 {{0, 1}, 1269 {3, 3}, 1270 {5, 5}, 1271 {7, 7} 1272 } 1273 }, 1274 {0, 1, 3, 0, /* 0xac */ 1275 {{2, 3}, 1276 {5, 5}, 1277 {7, 7}, 1278 {0, 0} 1279 } 1280 }, 1281 {1, 1, 4, 0, /* 0xad */ 1282 {{0, 0}, 1283 {2, 3}, 1284 {5, 5}, 1285 {7, 7} 1286 } 1287 }, 1288 {0, 1, 3, 0, /* 0xae */ 1289 {{1, 3}, 1290 {5, 5}, 1291 {7, 7}, 1292 {0, 0} 1293 } 1294 }, 1295 {1, 1, 3, 0, /* 0xaf */ 1296 {{0, 3}, 1297 {5, 5}, 1298 {7, 7}, 1299 {0, 0} 1300 } 1301 }, 1302 {0, 1, 2, 0, /* 0xb0 */ 1303 {{4, 5}, 1304 {7, 7}, 1305 {0, 0}, 1306 {0, 0} 1307 } 1308 }, 1309 {1, 1, 3, 0, /* 0xb1 */ 1310 {{0, 0}, 1311 {4, 5}, 1312 {7, 7}, 1313 {0, 0} 1314 } 1315 }, 1316 {0, 1, 3, 0, /* 0xb2 */ 1317 {{1, 1}, 1318 {4, 5}, 1319 {7, 7}, 1320 {0, 0} 1321 } 1322 }, 1323 {1, 1, 3, 0, /* 0xb3 */ 1324 {{0, 1}, 1325 {4, 5}, 1326 {7, 7}, 1327 {0, 0} 1328 } 1329 }, 1330 {0, 1, 3, 0, /* 0xb4 */ 1331 {{2, 2}, 1332 {4, 5}, 1333 {7, 7}, 1334 {0, 0} 1335 } 1336 }, 1337 {1, 1, 4, 0, /* 0xb5 */ 1338 {{0, 0}, 1339 {2, 2}, 1340 {4, 5}, 1341 {7, 7} 1342 } 1343 }, 1344 {0, 1, 3, 0, /* 0xb6 */ 1345 {{1, 2}, 1346 {4, 5}, 1347 {7, 7}, 1348 {0, 0} 1349 } 1350 }, 1351 {1, 1, 3, 0, /* 0xb7 */ 1352 {{0, 2}, 1353 {4, 5}, 1354 {7, 7}, 1355 {0, 0} 1356 } 1357 }, 1358 {0, 1, 2, 0, /* 0xb8 */ 1359 {{3, 5}, 1360 {7, 7}, 1361 {0, 0}, 1362 {0, 0} 1363 } 1364 }, 1365 {1, 1, 3, 0, /* 0xb9 */ 1366 {{0, 0}, 1367 {3, 5}, 1368 {7, 7}, 1369 {0, 0} 1370 } 1371 }, 1372 {0, 1, 3, 0, /* 0xba */ 1373 {{1, 1}, 1374 {3, 5}, 1375 {7, 7}, 1376 {0, 0} 1377 } 1378 }, 1379 {1, 1, 3, 0, /* 0xbb */ 1380 {{0, 1}, 1381 {3, 5}, 1382 {7, 7}, 1383 {0, 0} 1384 } 1385 }, 1386 {0, 1, 2, 0, /* 0xbc */ 1387 {{2, 5}, 1388 {7, 7}, 1389 {0, 0}, 1390 {0, 0} 1391 } 1392 }, 1393 {1, 1, 3, 0, /* 0xbd */ 1394 {{0, 0}, 1395 {2, 5}, 1396 {7, 7}, 1397 {0, 0} 1398 } 1399 }, 1400 {0, 1, 2, 0, /* 0xbe */ 1401 {{1, 5}, 1402 {7, 7}, 1403 {0, 0}, 1404 {0, 0} 1405 } 1406 }, 1407 {1, 1, 2, 0, /* 0xbf */ 1408 {{0, 5}, 1409 {7, 7}, 1410 {0, 0}, 1411 {0, 0} 1412 } 1413 }, 1414 {0, 1, 1, 0, /* 0xc0 */ 1415 {{6, 7}, 1416 {0, 0}, 1417 {0, 0}, 1418 {0, 0} 1419 } 1420 }, 1421 {1, 1, 2, 0, /* 0xc1 */ 1422 {{0, 0}, 1423 {6, 7}, 1424 {0, 0}, 1425 {0, 0} 1426 } 1427 }, 1428 {0, 1, 2, 0, /* 0xc2 */ 1429 {{1, 1}, 1430 {6, 7}, 1431 {0, 0}, 1432 {0, 0} 1433 } 1434 }, 1435 {1, 1, 2, 0, /* 0xc3 */ 1436 {{0, 1}, 1437 {6, 7}, 1438 {0, 0}, 1439 {0, 0} 1440 } 1441 }, 1442 {0, 1, 2, 0, /* 0xc4 */ 1443 {{2, 2}, 1444 {6, 7}, 1445 {0, 0}, 1446 {0, 0} 1447 } 1448 }, 1449 {1, 1, 3, 0, /* 0xc5 */ 1450 {{0, 0}, 1451 {2, 2}, 1452 {6, 7}, 1453 {0, 0} 1454 } 1455 }, 1456 {0, 1, 2, 0, /* 0xc6 */ 1457 {{1, 2}, 1458 {6, 7}, 1459 {0, 0}, 1460 {0, 0} 1461 } 1462 }, 1463 {1, 1, 2, 0, /* 0xc7 */ 1464 {{0, 2}, 1465 {6, 7}, 1466 {0, 0}, 1467 {0, 0} 1468 } 1469 }, 1470 {0, 1, 2, 0, /* 0xc8 */ 1471 {{3, 3}, 1472 {6, 7}, 1473 {0, 0}, 1474 {0, 0} 1475 } 1476 }, 1477 {1, 1, 3, 0, /* 0xc9 */ 1478 {{0, 0}, 1479 {3, 3}, 1480 {6, 7}, 1481 {0, 0} 1482 } 1483 }, 1484 {0, 1, 3, 0, /* 0xca */ 1485 {{1, 1}, 1486 {3, 3}, 1487 {6, 7}, 1488 {0, 0} 1489 } 1490 }, 1491 {1, 1, 3, 0, /* 0xcb */ 1492 {{0, 1}, 1493 {3, 3}, 1494 {6, 7}, 1495 {0, 0} 1496 } 1497 }, 1498 {0, 1, 2, 0, /* 0xcc */ 1499 {{2, 3}, 1500 {6, 7}, 1501 {0, 0}, 1502 {0, 0} 1503 } 1504 }, 1505 {1, 1, 3, 0, /* 0xcd */ 1506 {{0, 0}, 1507 {2, 3}, 1508 {6, 7}, 1509 {0, 0} 1510 } 1511 }, 1512 {0, 1, 2, 0, /* 0xce */ 1513 {{1, 3}, 1514 {6, 7}, 1515 {0, 0}, 1516 {0, 0} 1517 } 1518 }, 1519 {1, 1, 2, 0, /* 0xcf */ 1520 {{0, 3}, 1521 {6, 7}, 1522 {0, 0}, 1523 {0, 0} 1524 } 1525 }, 1526 {0, 1, 2, 0, /* 0xd0 */ 1527 {{4, 4}, 1528 {6, 7}, 1529 {0, 0}, 1530 {0, 0} 1531 } 1532 }, 1533 {1, 1, 3, 0, /* 0xd1 */ 1534 {{0, 0}, 1535 {4, 4}, 1536 {6, 7}, 1537 {0, 0} 1538 } 1539 }, 1540 {0, 1, 3, 0, /* 0xd2 */ 1541 {{1, 1}, 1542 {4, 4}, 1543 {6, 7}, 1544 {0, 0} 1545 } 1546 }, 1547 {1, 1, 3, 0, /* 0xd3 */ 1548 {{0, 1}, 1549 {4, 4}, 1550 {6, 7}, 1551 {0, 0} 1552 } 1553 }, 1554 {0, 1, 3, 0, /* 0xd4 */ 1555 {{2, 2}, 1556 {4, 4}, 1557 {6, 7}, 1558 {0, 0} 1559 } 1560 }, 1561 {1, 1, 4, 0, /* 0xd5 */ 1562 {{0, 0}, 1563 {2, 2}, 1564 {4, 4}, 1565 {6, 7} 1566 } 1567 }, 1568 {0, 1, 3, 0, /* 0xd6 */ 1569 {{1, 2}, 1570 {4, 4}, 1571 {6, 7}, 1572 {0, 0} 1573 } 1574 }, 1575 {1, 1, 3, 0, /* 0xd7 */ 1576 {{0, 2}, 1577 {4, 4}, 1578 {6, 7}, 1579 {0, 0} 1580 } 1581 }, 1582 {0, 1, 2, 0, /* 0xd8 */ 1583 {{3, 4}, 1584 {6, 7}, 1585 {0, 0}, 1586 {0, 0} 1587 } 1588 }, 1589 {1, 1, 3, 0, /* 0xd9 */ 1590 {{0, 0}, 1591 {3, 4}, 1592 {6, 7}, 1593 {0, 0} 1594 } 1595 }, 1596 {0, 1, 3, 0, /* 0xda */ 1597 {{1, 1}, 1598 {3, 4}, 1599 {6, 7}, 1600 {0, 0} 1601 } 1602 }, 1603 {1, 1, 3, 0, /* 0xdb */ 1604 {{0, 1}, 1605 {3, 4}, 1606 {6, 7}, 1607 {0, 0} 1608 } 1609 }, 1610 {0, 1, 2, 0, /* 0xdc */ 1611 {{2, 4}, 1612 {6, 7}, 1613 {0, 0}, 1614 {0, 0} 1615 } 1616 }, 1617 {1, 1, 3, 0, /* 0xdd */ 1618 {{0, 0}, 1619 {2, 4}, 1620 {6, 7}, 1621 {0, 0} 1622 } 1623 }, 1624 {0, 1, 2, 0, /* 0xde */ 1625 {{1, 4}, 1626 {6, 7}, 1627 {0, 0}, 1628 {0, 0} 1629 } 1630 }, 1631 {1, 1, 2, 0, /* 0xdf */ 1632 {{0, 4}, 1633 {6, 7}, 1634 {0, 0}, 1635 {0, 0} 1636 } 1637 }, 1638 {0, 1, 1, 0, /* 0xe0 */ 1639 {{5, 7}, 1640 {0, 0}, 1641 {0, 0}, 1642 {0, 0} 1643 } 1644 }, 1645 {1, 1, 2, 0, /* 0xe1 */ 1646 {{0, 0}, 1647 {5, 7}, 1648 {0, 0}, 1649 {0, 0} 1650 } 1651 }, 1652 {0, 1, 2, 0, /* 0xe2 */ 1653 {{1, 1}, 1654 {5, 7}, 1655 {0, 0}, 1656 {0, 0} 1657 } 1658 }, 1659 {1, 1, 2, 0, /* 0xe3 */ 1660 {{0, 1}, 1661 {5, 7}, 1662 {0, 0}, 1663 {0, 0} 1664 } 1665 }, 1666 {0, 1, 2, 0, /* 0xe4 */ 1667 {{2, 2}, 1668 {5, 7}, 1669 {0, 0}, 1670 {0, 0} 1671 } 1672 }, 1673 {1, 1, 3, 0, /* 0xe5 */ 1674 {{0, 0}, 1675 {2, 2}, 1676 {5, 7}, 1677 {0, 0} 1678 } 1679 }, 1680 {0, 1, 2, 0, /* 0xe6 */ 1681 {{1, 2}, 1682 {5, 7}, 1683 {0, 0}, 1684 {0, 0} 1685 } 1686 }, 1687 {1, 1, 2, 0, /* 0xe7 */ 1688 {{0, 2}, 1689 {5, 7}, 1690 {0, 0}, 1691 {0, 0} 1692 } 1693 }, 1694 {0, 1, 2, 0, /* 0xe8 */ 1695 {{3, 3}, 1696 {5, 7}, 1697 {0, 0}, 1698 {0, 0} 1699 } 1700 }, 1701 {1, 1, 3, 0, /* 0xe9 */ 1702 {{0, 0}, 1703 {3, 3}, 1704 {5, 7}, 1705 {0, 0} 1706 } 1707 }, 1708 {0, 1, 3, 0, /* 0xea */ 1709 {{1, 1}, 1710 {3, 3}, 1711 {5, 7}, 1712 {0, 0} 1713 } 1714 }, 1715 {1, 1, 3, 0, /* 0xeb */ 1716 {{0, 1}, 1717 {3, 3}, 1718 {5, 7}, 1719 {0, 0} 1720 } 1721 }, 1722 {0, 1, 2, 0, /* 0xec */ 1723 {{2, 3}, 1724 {5, 7}, 1725 {0, 0}, 1726 {0, 0} 1727 } 1728 }, 1729 {1, 1, 3, 0, /* 0xed */ 1730 {{0, 0}, 1731 {2, 3}, 1732 {5, 7}, 1733 {0, 0} 1734 } 1735 }, 1736 {0, 1, 2, 0, /* 0xee */ 1737 {{1, 3}, 1738 {5, 7}, 1739 {0, 0}, 1740 {0, 0} 1741 } 1742 }, 1743 {1, 1, 2, 0, /* 0xef */ 1744 {{0, 3}, 1745 {5, 7}, 1746 {0, 0}, 1747 {0, 0} 1748 } 1749 }, 1750 {0, 1, 1, 0, /* 0xf0 */ 1751 {{4, 7}, 1752 {0, 0}, 1753 {0, 0}, 1754 {0, 0} 1755 } 1756 }, 1757 {1, 1, 2, 0, /* 0xf1 */ 1758 {{0, 0}, 1759 {4, 7}, 1760 {0, 0}, 1761 {0, 0} 1762 } 1763 }, 1764 {0, 1, 2, 0, /* 0xf2 */ 1765 {{1, 1}, 1766 {4, 7}, 1767 {0, 0}, 1768 {0, 0} 1769 } 1770 }, 1771 {1, 1, 2, 0, /* 0xf3 */ 1772 {{0, 1}, 1773 {4, 7}, 1774 {0, 0}, 1775 {0, 0} 1776 } 1777 }, 1778 {0, 1, 2, 0, /* 0xf4 */ 1779 {{2, 2}, 1780 {4, 7}, 1781 {0, 0}, 1782 {0, 0} 1783 } 1784 }, 1785 {1, 1, 3, 0, /* 0xf5 */ 1786 {{0, 0}, 1787 {2, 2}, 1788 {4, 7}, 1789 {0, 0} 1790 } 1791 }, 1792 {0, 1, 2, 0, /* 0xf6 */ 1793 {{1, 2}, 1794 {4, 7}, 1795 {0, 0}, 1796 {0, 0} 1797 } 1798 }, 1799 {1, 1, 2, 0, /* 0xf7 */ 1800 {{0, 2}, 1801 {4, 7}, 1802 {0, 0}, 1803 {0, 0} 1804 } 1805 }, 1806 {0, 1, 1, 0, /* 0xf8 */ 1807 {{3, 7}, 1808 {0, 0}, 1809 {0, 0}, 1810 {0, 0} 1811 } 1812 }, 1813 {1, 1, 2, 0, /* 0xf9 */ 1814 {{0, 0}, 1815 {3, 7}, 1816 {0, 0}, 1817 {0, 0} 1818 } 1819 }, 1820 {0, 1, 2, 0, /* 0xfa */ 1821 {{1, 1}, 1822 {3, 7}, 1823 {0, 0}, 1824 {0, 0} 1825 } 1826 }, 1827 {1, 1, 2, 0, /* 0xfb */ 1828 {{0, 1}, 1829 {3, 7}, 1830 {0, 0}, 1831 {0, 0} 1832 } 1833 }, 1834 {0, 1, 1, 0, /* 0xfc */ 1835 {{2, 7}, 1836 {0, 0}, 1837 {0, 0}, 1838 {0, 0} 1839 } 1840 }, 1841 {1, 1, 2, 0, /* 0xfd */ 1842 {{0, 0}, 1843 {2, 7}, 1844 {0, 0}, 1845 {0, 0} 1846 } 1847 }, 1848 {0, 1, 1, 0, /* 0xfe */ 1849 {{1, 7}, 1850 {0, 0}, 1851 {0, 0}, 1852 {0, 0} 1853 } 1854 }, 1855 {1, 1, 1, 0, /* 0xff */ 1856 {{0, 7}, 1857 {0, 0}, 1858 {0, 0}, 1859 {0, 0} 1860 } 1861 } 1862 }; 1863 1864 1865 int 1866 sctp_is_address_in_scope(struct sctp_ifa *ifa, 1867 int ipv4_addr_legal, 1868 int ipv6_addr_legal, 1869 int loopback_scope, 1870 int ipv4_local_scope, 1871 int local_scope, 1872 int site_scope, 1873 int do_update) 1874 { 1875 if ((loopback_scope == 0) && 1876 (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) { 1877 /* 1878 * skip loopback if not in scope * 1879 */ 1880 return (0); 1881 } 1882 switch (ifa->address.sa.sa_family) { 1883 #ifdef INET 1884 case AF_INET: 1885 if (ipv4_addr_legal) { 1886 struct sockaddr_in *sin; 1887 1888 sin = (struct sockaddr_in *)&ifa->address.sin; 1889 if (sin->sin_addr.s_addr == 0) { 1890 /* not in scope , unspecified */ 1891 return (0); 1892 } 1893 if ((ipv4_local_scope == 0) && 1894 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) { 1895 /* private address not in scope */ 1896 return (0); 1897 } 1898 } else { 1899 return (0); 1900 } 1901 break; 1902 #endif 1903 #ifdef INET6 1904 case AF_INET6: 1905 if (ipv6_addr_legal) { 1906 struct sockaddr_in6 *sin6; 1907 1908 /* 1909 * Must update the flags, bummer, which means any 1910 * IFA locks must now be applied HERE <-> 1911 */ 1912 if (do_update) { 1913 sctp_gather_internal_ifa_flags(ifa); 1914 } 1915 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { 1916 return (0); 1917 } 1918 /* ok to use deprecated addresses? */ 1919 sin6 = (struct sockaddr_in6 *)&ifa->address.sin6; 1920 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1921 /* skip unspecifed addresses */ 1922 return (0); 1923 } 1924 if ( /* (local_scope == 0) && */ 1925 (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) { 1926 return (0); 1927 } 1928 if ((site_scope == 0) && 1929 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) { 1930 return (0); 1931 } 1932 } else { 1933 return (0); 1934 } 1935 break; 1936 #endif 1937 default: 1938 return (0); 1939 } 1940 return (1); 1941 } 1942 1943 static struct mbuf * 1944 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa) 1945 { 1946 struct sctp_paramhdr *parmh; 1947 struct mbuf *mret; 1948 int len; 1949 1950 switch (ifa->address.sa.sa_family) { 1951 #ifdef INET 1952 case AF_INET: 1953 len = sizeof(struct sctp_ipv4addr_param); 1954 break; 1955 #endif 1956 #ifdef INET6 1957 case AF_INET6: 1958 len = sizeof(struct sctp_ipv6addr_param); 1959 break; 1960 #endif 1961 default: 1962 return (m); 1963 } 1964 if (M_TRAILINGSPACE(m) >= len) { 1965 /* easy side we just drop it on the end */ 1966 parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m))); 1967 mret = m; 1968 } else { 1969 /* Need more space */ 1970 mret = m; 1971 while (SCTP_BUF_NEXT(mret) != NULL) { 1972 mret = SCTP_BUF_NEXT(mret); 1973 } 1974 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA); 1975 if (SCTP_BUF_NEXT(mret) == NULL) { 1976 /* We are hosed, can't add more addresses */ 1977 return (m); 1978 } 1979 mret = SCTP_BUF_NEXT(mret); 1980 parmh = mtod(mret, struct sctp_paramhdr *); 1981 } 1982 /* now add the parameter */ 1983 switch (ifa->address.sa.sa_family) { 1984 #ifdef INET 1985 case AF_INET: 1986 { 1987 struct sctp_ipv4addr_param *ipv4p; 1988 struct sockaddr_in *sin; 1989 1990 sin = (struct sockaddr_in *)&ifa->address.sin; 1991 ipv4p = (struct sctp_ipv4addr_param *)parmh; 1992 parmh->param_type = htons(SCTP_IPV4_ADDRESS); 1993 parmh->param_length = htons(len); 1994 ipv4p->addr = sin->sin_addr.s_addr; 1995 SCTP_BUF_LEN(mret) += len; 1996 break; 1997 } 1998 #endif 1999 #ifdef INET6 2000 case AF_INET6: 2001 { 2002 struct sctp_ipv6addr_param *ipv6p; 2003 struct sockaddr_in6 *sin6; 2004 2005 sin6 = (struct sockaddr_in6 *)&ifa->address.sin6; 2006 ipv6p = (struct sctp_ipv6addr_param *)parmh; 2007 parmh->param_type = htons(SCTP_IPV6_ADDRESS); 2008 parmh->param_length = htons(len); 2009 memcpy(ipv6p->addr, &sin6->sin6_addr, 2010 sizeof(ipv6p->addr)); 2011 /* clear embedded scope in the address */ 2012 in6_clearscope((struct in6_addr *)ipv6p->addr); 2013 SCTP_BUF_LEN(mret) += len; 2014 break; 2015 } 2016 #endif 2017 default: 2018 return (m); 2019 } 2020 return (mret); 2021 } 2022 2023 2024 struct mbuf * 2025 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_tcb *stcb, 2026 struct sctp_scoping *scope, 2027 struct mbuf *m_at, int cnt_inits_to) 2028 { 2029 struct sctp_vrf *vrf = NULL; 2030 int cnt, limit_out = 0, total_count; 2031 uint32_t vrf_id; 2032 2033 vrf_id = inp->def_vrf_id; 2034 SCTP_IPI_ADDR_RLOCK(); 2035 vrf = sctp_find_vrf(vrf_id); 2036 if (vrf == NULL) { 2037 SCTP_IPI_ADDR_RUNLOCK(); 2038 return (m_at); 2039 } 2040 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 2041 struct sctp_ifa *sctp_ifap; 2042 struct sctp_ifn *sctp_ifnp; 2043 2044 cnt = cnt_inits_to; 2045 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) { 2046 limit_out = 1; 2047 cnt = SCTP_ADDRESS_LIMIT; 2048 goto skip_count; 2049 } 2050 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) { 2051 if ((scope->loopback_scope == 0) && 2052 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) { 2053 /* 2054 * Skip loopback devices if loopback_scope 2055 * not set 2056 */ 2057 continue; 2058 } 2059 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) { 2060 if (sctp_is_addr_restricted(stcb, sctp_ifap)) { 2061 continue; 2062 } 2063 if (sctp_is_address_in_scope(sctp_ifap, 2064 scope->ipv4_addr_legal, 2065 scope->ipv6_addr_legal, 2066 scope->loopback_scope, 2067 scope->ipv4_local_scope, 2068 scope->local_scope, 2069 scope->site_scope, 1) == 0) { 2070 continue; 2071 } 2072 cnt++; 2073 if (cnt > SCTP_ADDRESS_LIMIT) { 2074 break; 2075 } 2076 } 2077 if (cnt > SCTP_ADDRESS_LIMIT) { 2078 break; 2079 } 2080 } 2081 skip_count: 2082 if (cnt > 1) { 2083 total_count = 0; 2084 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) { 2085 cnt = 0; 2086 if ((scope->loopback_scope == 0) && 2087 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) { 2088 /* 2089 * Skip loopback devices if 2090 * loopback_scope not set 2091 */ 2092 continue; 2093 } 2094 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) { 2095 if (sctp_is_addr_restricted(stcb, sctp_ifap)) { 2096 continue; 2097 } 2098 if (sctp_is_address_in_scope(sctp_ifap, 2099 scope->ipv4_addr_legal, 2100 scope->ipv6_addr_legal, 2101 scope->loopback_scope, 2102 scope->ipv4_local_scope, 2103 scope->local_scope, 2104 scope->site_scope, 0) == 0) { 2105 continue; 2106 } 2107 m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap); 2108 if (limit_out) { 2109 cnt++; 2110 total_count++; 2111 if (cnt >= 2) { 2112 /* 2113 * two from each 2114 * address 2115 */ 2116 break; 2117 } 2118 if (total_count > SCTP_ADDRESS_LIMIT) { 2119 /* No more addresses */ 2120 break; 2121 } 2122 } 2123 } 2124 } 2125 } 2126 } else { 2127 struct sctp_laddr *laddr; 2128 2129 cnt = cnt_inits_to; 2130 /* First, how many ? */ 2131 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 2132 if (laddr->ifa == NULL) { 2133 continue; 2134 } 2135 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) 2136 /* 2137 * Address being deleted by the system, dont 2138 * list. 2139 */ 2140 continue; 2141 if (laddr->action == SCTP_DEL_IP_ADDRESS) { 2142 /* 2143 * Address being deleted on this ep don't 2144 * list. 2145 */ 2146 continue; 2147 } 2148 if (sctp_is_address_in_scope(laddr->ifa, 2149 scope->ipv4_addr_legal, 2150 scope->ipv6_addr_legal, 2151 scope->loopback_scope, 2152 scope->ipv4_local_scope, 2153 scope->local_scope, 2154 scope->site_scope, 1) == 0) { 2155 continue; 2156 } 2157 cnt++; 2158 } 2159 if (cnt > SCTP_ADDRESS_LIMIT) { 2160 limit_out = 1; 2161 } 2162 /* 2163 * To get through a NAT we only list addresses if we have 2164 * more than one. That way if you just bind a single address 2165 * we let the source of the init dictate our address. 2166 */ 2167 if (cnt > 1) { 2168 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 2169 cnt = 0; 2170 if (laddr->ifa == NULL) { 2171 continue; 2172 } 2173 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) 2174 continue; 2175 2176 if (sctp_is_address_in_scope(laddr->ifa, 2177 scope->ipv4_addr_legal, 2178 scope->ipv6_addr_legal, 2179 scope->loopback_scope, 2180 scope->ipv4_local_scope, 2181 scope->local_scope, 2182 scope->site_scope, 0) == 0) { 2183 continue; 2184 } 2185 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa); 2186 cnt++; 2187 if (cnt >= SCTP_ADDRESS_LIMIT) { 2188 break; 2189 } 2190 } 2191 } 2192 } 2193 SCTP_IPI_ADDR_RUNLOCK(); 2194 return (m_at); 2195 } 2196 2197 static struct sctp_ifa * 2198 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa, 2199 uint8_t dest_is_loop, 2200 uint8_t dest_is_priv, 2201 sa_family_t fam) 2202 { 2203 uint8_t dest_is_global = 0; 2204 2205 /* dest_is_priv is true if destination is a private address */ 2206 /* dest_is_loop is true if destination is a loopback addresses */ 2207 2208 /** 2209 * Here we determine if its a preferred address. A preferred address 2210 * means it is the same scope or higher scope then the destination. 2211 * L = loopback, P = private, G = global 2212 * ----------------------------------------- 2213 * src | dest | result 2214 * ---------------------------------------- 2215 * L | L | yes 2216 * ----------------------------------------- 2217 * P | L | yes-v4 no-v6 2218 * ----------------------------------------- 2219 * G | L | yes-v4 no-v6 2220 * ----------------------------------------- 2221 * L | P | no 2222 * ----------------------------------------- 2223 * P | P | yes 2224 * ----------------------------------------- 2225 * G | P | no 2226 * ----------------------------------------- 2227 * L | G | no 2228 * ----------------------------------------- 2229 * P | G | no 2230 * ----------------------------------------- 2231 * G | G | yes 2232 * ----------------------------------------- 2233 */ 2234 2235 if (ifa->address.sa.sa_family != fam) { 2236 /* forget mis-matched family */ 2237 return (NULL); 2238 } 2239 if ((dest_is_priv == 0) && (dest_is_loop == 0)) { 2240 dest_is_global = 1; 2241 } 2242 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:"); 2243 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa); 2244 /* Ok the address may be ok */ 2245 #ifdef INET6 2246 if (fam == AF_INET6) { 2247 /* ok to use deprecated addresses? no lets not! */ 2248 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { 2249 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n"); 2250 return (NULL); 2251 } 2252 if (ifa->src_is_priv && !ifa->src_is_loop) { 2253 if (dest_is_loop) { 2254 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n"); 2255 return (NULL); 2256 } 2257 } 2258 if (ifa->src_is_glob) { 2259 if (dest_is_loop) { 2260 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n"); 2261 return (NULL); 2262 } 2263 } 2264 } 2265 #endif 2266 /* 2267 * Now that we know what is what, implement or table this could in 2268 * theory be done slicker (it used to be), but this is 2269 * straightforward and easier to validate :-) 2270 */ 2271 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n", 2272 ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob); 2273 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n", 2274 dest_is_loop, dest_is_priv, dest_is_global); 2275 2276 if ((ifa->src_is_loop) && (dest_is_priv)) { 2277 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n"); 2278 return (NULL); 2279 } 2280 if ((ifa->src_is_glob) && (dest_is_priv)) { 2281 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n"); 2282 return (NULL); 2283 } 2284 if ((ifa->src_is_loop) && (dest_is_global)) { 2285 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n"); 2286 return (NULL); 2287 } 2288 if ((ifa->src_is_priv) && (dest_is_global)) { 2289 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n"); 2290 return (NULL); 2291 } 2292 SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n"); 2293 /* its a preferred address */ 2294 return (ifa); 2295 } 2296 2297 static struct sctp_ifa * 2298 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa, 2299 uint8_t dest_is_loop, 2300 uint8_t dest_is_priv, 2301 sa_family_t fam) 2302 { 2303 uint8_t dest_is_global = 0; 2304 2305 /** 2306 * Here we determine if its a acceptable address. A acceptable 2307 * address means it is the same scope or higher scope but we can 2308 * allow for NAT which means its ok to have a global dest and a 2309 * private src. 2310 * 2311 * L = loopback, P = private, G = global 2312 * ----------------------------------------- 2313 * src | dest | result 2314 * ----------------------------------------- 2315 * L | L | yes 2316 * ----------------------------------------- 2317 * P | L | yes-v4 no-v6 2318 * ----------------------------------------- 2319 * G | L | yes 2320 * ----------------------------------------- 2321 * L | P | no 2322 * ----------------------------------------- 2323 * P | P | yes 2324 * ----------------------------------------- 2325 * G | P | yes - May not work 2326 * ----------------------------------------- 2327 * L | G | no 2328 * ----------------------------------------- 2329 * P | G | yes - May not work 2330 * ----------------------------------------- 2331 * G | G | yes 2332 * ----------------------------------------- 2333 */ 2334 2335 if (ifa->address.sa.sa_family != fam) { 2336 /* forget non matching family */ 2337 SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n", 2338 ifa->address.sa.sa_family, fam); 2339 return (NULL); 2340 } 2341 /* Ok the address may be ok */ 2342 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa); 2343 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n", 2344 dest_is_loop, dest_is_priv); 2345 if ((dest_is_loop == 0) && (dest_is_priv == 0)) { 2346 dest_is_global = 1; 2347 } 2348 #ifdef INET6 2349 if (fam == AF_INET6) { 2350 /* ok to use deprecated addresses? */ 2351 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { 2352 return (NULL); 2353 } 2354 if (ifa->src_is_priv) { 2355 /* Special case, linklocal to loop */ 2356 if (dest_is_loop) 2357 return (NULL); 2358 } 2359 } 2360 #endif 2361 /* 2362 * Now that we know what is what, implement our table. This could in 2363 * theory be done slicker (it used to be), but this is 2364 * straightforward and easier to validate :-) 2365 */ 2366 SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n", 2367 ifa->src_is_loop, 2368 dest_is_priv); 2369 if ((ifa->src_is_loop == 1) && (dest_is_priv)) { 2370 return (NULL); 2371 } 2372 SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n", 2373 ifa->src_is_loop, 2374 dest_is_global); 2375 if ((ifa->src_is_loop == 1) && (dest_is_global)) { 2376 return (NULL); 2377 } 2378 SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n"); 2379 /* its an acceptable address */ 2380 return (ifa); 2381 } 2382 2383 int 2384 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) 2385 { 2386 struct sctp_laddr *laddr; 2387 2388 if (stcb == NULL) { 2389 /* There are no restrictions, no TCB :-) */ 2390 return (0); 2391 } 2392 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) { 2393 if (laddr->ifa == NULL) { 2394 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n", 2395 __FUNCTION__); 2396 continue; 2397 } 2398 if (laddr->ifa == ifa) { 2399 /* Yes it is on the list */ 2400 return (1); 2401 } 2402 } 2403 return (0); 2404 } 2405 2406 2407 int 2408 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa) 2409 { 2410 struct sctp_laddr *laddr; 2411 2412 if (ifa == NULL) 2413 return (0); 2414 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 2415 if (laddr->ifa == NULL) { 2416 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n", 2417 __FUNCTION__); 2418 continue; 2419 } 2420 if ((laddr->ifa == ifa) && laddr->action == 0) 2421 /* same pointer */ 2422 return (1); 2423 } 2424 return (0); 2425 } 2426 2427 2428 2429 static struct sctp_ifa * 2430 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp, 2431 sctp_route_t * ro, 2432 uint32_t vrf_id, 2433 int non_asoc_addr_ok, 2434 uint8_t dest_is_priv, 2435 uint8_t dest_is_loop, 2436 sa_family_t fam) 2437 { 2438 struct sctp_laddr *laddr, *starting_point; 2439 void *ifn; 2440 int resettotop = 0; 2441 struct sctp_ifn *sctp_ifn; 2442 struct sctp_ifa *sctp_ifa, *sifa; 2443 struct sctp_vrf *vrf; 2444 uint32_t ifn_index; 2445 2446 vrf = sctp_find_vrf(vrf_id); 2447 if (vrf == NULL) 2448 return (NULL); 2449 2450 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro); 2451 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro); 2452 sctp_ifn = sctp_find_ifn(ifn, ifn_index); 2453 /* 2454 * first question, is the ifn we will emit on in our list, if so, we 2455 * want such an address. Note that we first looked for a preferred 2456 * address. 2457 */ 2458 if (sctp_ifn) { 2459 /* is a preferred one on the interface we route out? */ 2460 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { 2461 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && 2462 (non_asoc_addr_ok == 0)) 2463 continue; 2464 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, 2465 dest_is_loop, 2466 dest_is_priv, fam); 2467 if (sifa == NULL) 2468 continue; 2469 if (sctp_is_addr_in_ep(inp, sifa)) { 2470 atomic_add_int(&sifa->refcount, 1); 2471 return (sifa); 2472 } 2473 } 2474 } 2475 /* 2476 * ok, now we now need to find one on the list of the addresses. We 2477 * can't get one on the emitting interface so let's find first a 2478 * preferred one. If not that an acceptable one otherwise... we 2479 * return NULL. 2480 */ 2481 starting_point = inp->next_addr_touse; 2482 once_again: 2483 if (inp->next_addr_touse == NULL) { 2484 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list); 2485 resettotop = 1; 2486 } 2487 for (laddr = inp->next_addr_touse; laddr; 2488 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) { 2489 if (laddr->ifa == NULL) { 2490 /* address has been removed */ 2491 continue; 2492 } 2493 if (laddr->action == SCTP_DEL_IP_ADDRESS) { 2494 /* address is being deleted */ 2495 continue; 2496 } 2497 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, 2498 dest_is_priv, fam); 2499 if (sifa == NULL) 2500 continue; 2501 atomic_add_int(&sifa->refcount, 1); 2502 return (sifa); 2503 } 2504 if (resettotop == 0) { 2505 inp->next_addr_touse = NULL; 2506 goto once_again; 2507 } 2508 inp->next_addr_touse = starting_point; 2509 resettotop = 0; 2510 once_again_too: 2511 if (inp->next_addr_touse == NULL) { 2512 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list); 2513 resettotop = 1; 2514 } 2515 /* ok, what about an acceptable address in the inp */ 2516 for (laddr = inp->next_addr_touse; laddr; 2517 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) { 2518 if (laddr->ifa == NULL) { 2519 /* address has been removed */ 2520 continue; 2521 } 2522 if (laddr->action == SCTP_DEL_IP_ADDRESS) { 2523 /* address is being deleted */ 2524 continue; 2525 } 2526 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop, 2527 dest_is_priv, fam); 2528 if (sifa == NULL) 2529 continue; 2530 atomic_add_int(&sifa->refcount, 1); 2531 return (sifa); 2532 } 2533 if (resettotop == 0) { 2534 inp->next_addr_touse = NULL; 2535 goto once_again_too; 2536 } 2537 /* 2538 * no address bound can be a source for the destination we are in 2539 * trouble 2540 */ 2541 return (NULL); 2542 } 2543 2544 2545 2546 static struct sctp_ifa * 2547 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp, 2548 struct sctp_tcb *stcb, 2549 struct sctp_nets *net, 2550 sctp_route_t * ro, 2551 uint32_t vrf_id, 2552 uint8_t dest_is_priv, 2553 uint8_t dest_is_loop, 2554 int non_asoc_addr_ok, 2555 sa_family_t fam) 2556 { 2557 struct sctp_laddr *laddr, *starting_point; 2558 void *ifn; 2559 struct sctp_ifn *sctp_ifn; 2560 struct sctp_ifa *sctp_ifa, *sifa; 2561 uint8_t start_at_beginning = 0; 2562 struct sctp_vrf *vrf; 2563 uint32_t ifn_index; 2564 2565 /* 2566 * first question, is the ifn we will emit on in our list, if so, we 2567 * want that one. 2568 */ 2569 vrf = sctp_find_vrf(vrf_id); 2570 if (vrf == NULL) 2571 return (NULL); 2572 2573 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro); 2574 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro); 2575 sctp_ifn = sctp_find_ifn(ifn, ifn_index); 2576 2577 /* 2578 * first question, is the ifn we will emit on in our list? If so, 2579 * we want that one. First we look for a preferred. Second, we go 2580 * for an acceptable. 2581 */ 2582 if (sctp_ifn) { 2583 /* first try for a preferred address on the ep */ 2584 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { 2585 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0)) 2586 continue; 2587 if (sctp_is_addr_in_ep(inp, sctp_ifa)) { 2588 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam); 2589 if (sifa == NULL) 2590 continue; 2591 if (((non_asoc_addr_ok == 0) && 2592 (sctp_is_addr_restricted(stcb, sifa))) || 2593 (non_asoc_addr_ok && 2594 (sctp_is_addr_restricted(stcb, sifa)) && 2595 (!sctp_is_addr_pending(stcb, sifa)))) { 2596 /* on the no-no list */ 2597 continue; 2598 } 2599 atomic_add_int(&sifa->refcount, 1); 2600 return (sifa); 2601 } 2602 } 2603 /* next try for an acceptable address on the ep */ 2604 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { 2605 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0)) 2606 continue; 2607 if (sctp_is_addr_in_ep(inp, sctp_ifa)) { 2608 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam); 2609 if (sifa == NULL) 2610 continue; 2611 if (((non_asoc_addr_ok == 0) && 2612 (sctp_is_addr_restricted(stcb, sifa))) || 2613 (non_asoc_addr_ok && 2614 (sctp_is_addr_restricted(stcb, sifa)) && 2615 (!sctp_is_addr_pending(stcb, sifa)))) { 2616 /* on the no-no list */ 2617 continue; 2618 } 2619 atomic_add_int(&sifa->refcount, 1); 2620 return (sifa); 2621 } 2622 } 2623 2624 } 2625 /* 2626 * if we can't find one like that then we must look at all addresses 2627 * bound to pick one at first preferable then secondly acceptable. 2628 */ 2629 starting_point = stcb->asoc.last_used_address; 2630 sctp_from_the_top: 2631 if (stcb->asoc.last_used_address == NULL) { 2632 start_at_beginning = 1; 2633 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list); 2634 } 2635 /* search beginning with the last used address */ 2636 for (laddr = stcb->asoc.last_used_address; laddr; 2637 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) { 2638 if (laddr->ifa == NULL) { 2639 /* address has been removed */ 2640 continue; 2641 } 2642 if (laddr->action == SCTP_DEL_IP_ADDRESS) { 2643 /* address is being deleted */ 2644 continue; 2645 } 2646 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam); 2647 if (sifa == NULL) 2648 continue; 2649 if (((non_asoc_addr_ok == 0) && 2650 (sctp_is_addr_restricted(stcb, sifa))) || 2651 (non_asoc_addr_ok && 2652 (sctp_is_addr_restricted(stcb, sifa)) && 2653 (!sctp_is_addr_pending(stcb, sifa)))) { 2654 /* on the no-no list */ 2655 continue; 2656 } 2657 stcb->asoc.last_used_address = laddr; 2658 atomic_add_int(&sifa->refcount, 1); 2659 return (sifa); 2660 } 2661 if (start_at_beginning == 0) { 2662 stcb->asoc.last_used_address = NULL; 2663 goto sctp_from_the_top; 2664 } 2665 /* now try for any higher scope than the destination */ 2666 stcb->asoc.last_used_address = starting_point; 2667 start_at_beginning = 0; 2668 sctp_from_the_top2: 2669 if (stcb->asoc.last_used_address == NULL) { 2670 start_at_beginning = 1; 2671 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list); 2672 } 2673 /* search beginning with the last used address */ 2674 for (laddr = stcb->asoc.last_used_address; laddr; 2675 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) { 2676 if (laddr->ifa == NULL) { 2677 /* address has been removed */ 2678 continue; 2679 } 2680 if (laddr->action == SCTP_DEL_IP_ADDRESS) { 2681 /* address is being deleted */ 2682 continue; 2683 } 2684 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop, 2685 dest_is_priv, fam); 2686 if (sifa == NULL) 2687 continue; 2688 if (((non_asoc_addr_ok == 0) && 2689 (sctp_is_addr_restricted(stcb, sifa))) || 2690 (non_asoc_addr_ok && 2691 (sctp_is_addr_restricted(stcb, sifa)) && 2692 (!sctp_is_addr_pending(stcb, sifa)))) { 2693 /* on the no-no list */ 2694 continue; 2695 } 2696 stcb->asoc.last_used_address = laddr; 2697 atomic_add_int(&sifa->refcount, 1); 2698 return (sifa); 2699 } 2700 if (start_at_beginning == 0) { 2701 stcb->asoc.last_used_address = NULL; 2702 goto sctp_from_the_top2; 2703 } 2704 return (NULL); 2705 } 2706 2707 static struct sctp_ifa * 2708 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn, 2709 struct sctp_tcb *stcb, 2710 int non_asoc_addr_ok, 2711 uint8_t dest_is_loop, 2712 uint8_t dest_is_priv, 2713 int addr_wanted, 2714 sa_family_t fam, 2715 sctp_route_t * ro 2716 ) 2717 { 2718 struct sctp_ifa *ifa, *sifa; 2719 int num_eligible_addr = 0; 2720 2721 #ifdef INET6 2722 struct sockaddr_in6 sin6, lsa6; 2723 2724 if (fam == AF_INET6) { 2725 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6)); 2726 (void)sa6_recoverscope(&sin6); 2727 } 2728 #endif /* INET6 */ 2729 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) { 2730 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && 2731 (non_asoc_addr_ok == 0)) 2732 continue; 2733 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop, 2734 dest_is_priv, fam); 2735 if (sifa == NULL) 2736 continue; 2737 #ifdef INET6 2738 if (fam == AF_INET6 && 2739 dest_is_loop && 2740 sifa->src_is_loop && sifa->src_is_priv) { 2741 /* 2742 * don't allow fe80::1 to be a src on loop ::1, we 2743 * don't list it to the peer so we will get an 2744 * abort. 2745 */ 2746 continue; 2747 } 2748 if (fam == AF_INET6 && 2749 IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) && 2750 IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) { 2751 /* 2752 * link-local <-> link-local must belong to the same 2753 * scope. 2754 */ 2755 memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6)); 2756 (void)sa6_recoverscope(&lsa6); 2757 if (sin6.sin6_scope_id != lsa6.sin6_scope_id) { 2758 continue; 2759 } 2760 } 2761 #endif /* INET6 */ 2762 2763 /* 2764 * Check if the IPv6 address matches to next-hop. In the 2765 * mobile case, old IPv6 address may be not deleted from the 2766 * interface. Then, the interface has previous and new 2767 * addresses. We should use one corresponding to the 2768 * next-hop. (by micchie) 2769 */ 2770 #ifdef INET6 2771 if (stcb && fam == AF_INET6 && 2772 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) { 2773 if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro) 2774 == 0) { 2775 continue; 2776 } 2777 } 2778 #endif 2779 #ifdef INET 2780 /* Avoid topologically incorrect IPv4 address */ 2781 if (stcb && fam == AF_INET && 2782 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) { 2783 if (sctp_v4src_match_nexthop(sifa, ro) == 0) { 2784 continue; 2785 } 2786 } 2787 #endif 2788 if (stcb) { 2789 if (sctp_is_address_in_scope(ifa, 2790 stcb->asoc.ipv4_addr_legal, 2791 stcb->asoc.ipv6_addr_legal, 2792 stcb->asoc.loopback_scope, 2793 stcb->asoc.ipv4_local_scope, 2794 stcb->asoc.local_scope, 2795 stcb->asoc.site_scope, 0) == 0) { 2796 continue; 2797 } 2798 if (((non_asoc_addr_ok == 0) && 2799 (sctp_is_addr_restricted(stcb, sifa))) || 2800 (non_asoc_addr_ok && 2801 (sctp_is_addr_restricted(stcb, sifa)) && 2802 (!sctp_is_addr_pending(stcb, sifa)))) { 2803 /* 2804 * It is restricted for some reason.. 2805 * probably not yet added. 2806 */ 2807 continue; 2808 } 2809 } 2810 if (num_eligible_addr >= addr_wanted) { 2811 return (sifa); 2812 } 2813 num_eligible_addr++; 2814 } 2815 return (NULL); 2816 } 2817 2818 2819 static int 2820 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn, 2821 struct sctp_tcb *stcb, 2822 int non_asoc_addr_ok, 2823 uint8_t dest_is_loop, 2824 uint8_t dest_is_priv, 2825 sa_family_t fam) 2826 { 2827 struct sctp_ifa *ifa, *sifa; 2828 int num_eligible_addr = 0; 2829 2830 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) { 2831 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && 2832 (non_asoc_addr_ok == 0)) { 2833 continue; 2834 } 2835 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop, 2836 dest_is_priv, fam); 2837 if (sifa == NULL) { 2838 continue; 2839 } 2840 if (stcb) { 2841 if (sctp_is_address_in_scope(ifa, 2842 stcb->asoc.ipv4_addr_legal, 2843 stcb->asoc.ipv6_addr_legal, 2844 stcb->asoc.loopback_scope, 2845 stcb->asoc.ipv4_local_scope, 2846 stcb->asoc.local_scope, 2847 stcb->asoc.site_scope, 0) == 0) { 2848 continue; 2849 } 2850 if (((non_asoc_addr_ok == 0) && 2851 (sctp_is_addr_restricted(stcb, sifa))) || 2852 (non_asoc_addr_ok && 2853 (sctp_is_addr_restricted(stcb, sifa)) && 2854 (!sctp_is_addr_pending(stcb, sifa)))) { 2855 /* 2856 * It is restricted for some reason.. 2857 * probably not yet added. 2858 */ 2859 continue; 2860 } 2861 } 2862 num_eligible_addr++; 2863 } 2864 return (num_eligible_addr); 2865 } 2866 2867 static struct sctp_ifa * 2868 sctp_choose_boundall(struct sctp_inpcb *inp, 2869 struct sctp_tcb *stcb, 2870 struct sctp_nets *net, 2871 sctp_route_t * ro, 2872 uint32_t vrf_id, 2873 uint8_t dest_is_priv, 2874 uint8_t dest_is_loop, 2875 int non_asoc_addr_ok, 2876 sa_family_t fam) 2877 { 2878 int cur_addr_num = 0, num_preferred = 0; 2879 void *ifn; 2880 struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn; 2881 struct sctp_ifa *sctp_ifa, *sifa; 2882 uint32_t ifn_index; 2883 struct sctp_vrf *vrf; 2884 2885 #ifdef INET 2886 int retried = 0; 2887 2888 #endif 2889 /*- 2890 * For boundall we can use any address in the association. 2891 * If non_asoc_addr_ok is set we can use any address (at least in 2892 * theory). So we look for preferred addresses first. If we find one, 2893 * we use it. Otherwise we next try to get an address on the 2894 * interface, which we should be able to do (unless non_asoc_addr_ok 2895 * is false and we are routed out that way). In these cases where we 2896 * can't use the address of the interface we go through all the 2897 * ifn's looking for an address we can use and fill that in. Punting 2898 * means we send back address 0, which will probably cause problems 2899 * actually since then IP will fill in the address of the route ifn, 2900 * which means we probably already rejected it.. i.e. here comes an 2901 * abort :-<. 2902 */ 2903 vrf = sctp_find_vrf(vrf_id); 2904 if (vrf == NULL) 2905 return (NULL); 2906 2907 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro); 2908 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro); 2909 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn from route:%p ifn_index:%d\n", ifn, ifn_index); 2910 emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index); 2911 if (sctp_ifn == NULL) { 2912 /* ?? We don't have this guy ?? */ 2913 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n"); 2914 goto bound_all_plan_b; 2915 } 2916 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n", 2917 ifn_index, sctp_ifn->ifn_name); 2918 2919 if (net) { 2920 cur_addr_num = net->indx_of_eligible_next_to_use; 2921 } 2922 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, 2923 stcb, 2924 non_asoc_addr_ok, 2925 dest_is_loop, 2926 dest_is_priv, fam); 2927 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n", 2928 num_preferred, sctp_ifn->ifn_name); 2929 if (num_preferred == 0) { 2930 /* 2931 * no eligible addresses, we must use some other interface 2932 * address if we can find one. 2933 */ 2934 goto bound_all_plan_b; 2935 } 2936 /* 2937 * Ok we have num_eligible_addr set with how many we can use, this 2938 * may vary from call to call due to addresses being deprecated 2939 * etc.. 2940 */ 2941 if (cur_addr_num >= num_preferred) { 2942 cur_addr_num = 0; 2943 } 2944 /* 2945 * select the nth address from the list (where cur_addr_num is the 2946 * nth) and 0 is the first one, 1 is the second one etc... 2947 */ 2948 SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num); 2949 2950 sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop, 2951 dest_is_priv, cur_addr_num, fam, ro); 2952 2953 /* if sctp_ifa is NULL something changed??, fall to plan b. */ 2954 if (sctp_ifa) { 2955 atomic_add_int(&sctp_ifa->refcount, 1); 2956 if (net) { 2957 /* save off where the next one we will want */ 2958 net->indx_of_eligible_next_to_use = cur_addr_num + 1; 2959 } 2960 return (sctp_ifa); 2961 } 2962 /* 2963 * plan_b: Look at all interfaces and find a preferred address. If 2964 * no preferred fall through to plan_c. 2965 */ 2966 bound_all_plan_b: 2967 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n"); 2968 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { 2969 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n", 2970 sctp_ifn->ifn_name); 2971 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { 2972 /* wrong base scope */ 2973 SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n"); 2974 continue; 2975 } 2976 if ((sctp_ifn == looked_at) && looked_at) { 2977 /* already looked at this guy */ 2978 SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n"); 2979 continue; 2980 } 2981 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok, 2982 dest_is_loop, dest_is_priv, fam); 2983 SCTPDBG(SCTP_DEBUG_OUTPUT2, 2984 "Found ifn:%p %d preferred source addresses\n", 2985 ifn, num_preferred); 2986 if (num_preferred == 0) { 2987 /* None on this interface. */ 2988 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n"); 2989 continue; 2990 } 2991 SCTPDBG(SCTP_DEBUG_OUTPUT2, 2992 "num preferred:%d on interface:%p cur_addr_num:%d\n", 2993 num_preferred, sctp_ifn, cur_addr_num); 2994 2995 /* 2996 * Ok we have num_eligible_addr set with how many we can 2997 * use, this may vary from call to call due to addresses 2998 * being deprecated etc.. 2999 */ 3000 if (cur_addr_num >= num_preferred) { 3001 cur_addr_num = 0; 3002 } 3003 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop, 3004 dest_is_priv, cur_addr_num, fam, ro); 3005 if (sifa == NULL) 3006 continue; 3007 if (net) { 3008 net->indx_of_eligible_next_to_use = cur_addr_num + 1; 3009 SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n", 3010 cur_addr_num); 3011 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:"); 3012 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa); 3013 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:"); 3014 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa); 3015 } 3016 atomic_add_int(&sifa->refcount, 1); 3017 return (sifa); 3018 } 3019 #ifdef INET 3020 again_with_private_addresses_allowed: 3021 #endif 3022 /* plan_c: do we have an acceptable address on the emit interface */ 3023 sifa = NULL; 3024 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n"); 3025 if (emit_ifn == NULL) { 3026 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jump to Plan D - no emit_ifn\n"); 3027 goto plan_d; 3028 } 3029 LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) { 3030 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", sctp_ifa); 3031 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && 3032 (non_asoc_addr_ok == 0)) { 3033 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Defer\n"); 3034 continue; 3035 } 3036 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, 3037 dest_is_priv, fam); 3038 if (sifa == NULL) { 3039 SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n"); 3040 continue; 3041 } 3042 if (stcb) { 3043 if (sctp_is_address_in_scope(sifa, 3044 stcb->asoc.ipv4_addr_legal, 3045 stcb->asoc.ipv6_addr_legal, 3046 stcb->asoc.loopback_scope, 3047 stcb->asoc.ipv4_local_scope, 3048 stcb->asoc.local_scope, 3049 stcb->asoc.site_scope, 0) == 0) { 3050 SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n"); 3051 sifa = NULL; 3052 continue; 3053 } 3054 if (((non_asoc_addr_ok == 0) && 3055 (sctp_is_addr_restricted(stcb, sifa))) || 3056 (non_asoc_addr_ok && 3057 (sctp_is_addr_restricted(stcb, sifa)) && 3058 (!sctp_is_addr_pending(stcb, sifa)))) { 3059 /* 3060 * It is restricted for some reason.. 3061 * probably not yet added. 3062 */ 3063 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its resticted\n"); 3064 sifa = NULL; 3065 continue; 3066 } 3067 } else { 3068 printf("Stcb is null - no print\n"); 3069 } 3070 atomic_add_int(&sifa->refcount, 1); 3071 goto out; 3072 } 3073 plan_d: 3074 /* 3075 * plan_d: We are in trouble. No preferred address on the emit 3076 * interface. And not even a preferred address on all interfaces. Go 3077 * out and see if we can find an acceptable address somewhere 3078 * amongst all interfaces. 3079 */ 3080 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", looked_at); 3081 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { 3082 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { 3083 /* wrong base scope */ 3084 continue; 3085 } 3086 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { 3087 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && 3088 (non_asoc_addr_ok == 0)) 3089 continue; 3090 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, 3091 dest_is_loop, 3092 dest_is_priv, fam); 3093 if (sifa == NULL) 3094 continue; 3095 if (stcb) { 3096 if (sctp_is_address_in_scope(sifa, 3097 stcb->asoc.ipv4_addr_legal, 3098 stcb->asoc.ipv6_addr_legal, 3099 stcb->asoc.loopback_scope, 3100 stcb->asoc.ipv4_local_scope, 3101 stcb->asoc.local_scope, 3102 stcb->asoc.site_scope, 0) == 0) { 3103 sifa = NULL; 3104 continue; 3105 } 3106 if (((non_asoc_addr_ok == 0) && 3107 (sctp_is_addr_restricted(stcb, sifa))) || 3108 (non_asoc_addr_ok && 3109 (sctp_is_addr_restricted(stcb, sifa)) && 3110 (!sctp_is_addr_pending(stcb, sifa)))) { 3111 /* 3112 * It is restricted for some 3113 * reason.. probably not yet added. 3114 */ 3115 sifa = NULL; 3116 continue; 3117 } 3118 } 3119 goto out; 3120 } 3121 } 3122 #ifdef INET 3123 if ((retried == 0) && (stcb->asoc.ipv4_local_scope == 0)) { 3124 stcb->asoc.ipv4_local_scope = 1; 3125 retried = 1; 3126 goto again_with_private_addresses_allowed; 3127 } else if (retried == 1) { 3128 stcb->asoc.ipv4_local_scope = 0; 3129 } 3130 #endif 3131 out: 3132 #ifdef INET 3133 if (sifa) { 3134 if (retried == 1) { 3135 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { 3136 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { 3137 /* wrong base scope */ 3138 continue; 3139 } 3140 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { 3141 struct sctp_ifa *tmp_sifa; 3142 3143 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && 3144 (non_asoc_addr_ok == 0)) 3145 continue; 3146 tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, 3147 dest_is_loop, 3148 dest_is_priv, fam); 3149 if (tmp_sifa == NULL) { 3150 continue; 3151 } 3152 if (tmp_sifa == sifa) { 3153 continue; 3154 } 3155 if (stcb) { 3156 if (sctp_is_address_in_scope(tmp_sifa, 3157 stcb->asoc.ipv4_addr_legal, 3158 stcb->asoc.ipv6_addr_legal, 3159 stcb->asoc.loopback_scope, 3160 stcb->asoc.ipv4_local_scope, 3161 stcb->asoc.local_scope, 3162 stcb->asoc.site_scope, 0) == 0) { 3163 continue; 3164 } 3165 if (((non_asoc_addr_ok == 0) && 3166 (sctp_is_addr_restricted(stcb, tmp_sifa))) || 3167 (non_asoc_addr_ok && 3168 (sctp_is_addr_restricted(stcb, tmp_sifa)) && 3169 (!sctp_is_addr_pending(stcb, tmp_sifa)))) { 3170 /* 3171 * It is restricted 3172 * for some reason.. 3173 * probably not yet 3174 * added. 3175 */ 3176 continue; 3177 } 3178 } 3179 if ((tmp_sifa->address.sin.sin_family == AF_INET) && 3180 (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) { 3181 sctp_add_local_addr_restricted(stcb, tmp_sifa); 3182 } 3183 } 3184 } 3185 } 3186 atomic_add_int(&sifa->refcount, 1); 3187 } 3188 #endif 3189 return (sifa); 3190 } 3191 3192 3193 3194 /* tcb may be NULL */ 3195 struct sctp_ifa * 3196 sctp_source_address_selection(struct sctp_inpcb *inp, 3197 struct sctp_tcb *stcb, 3198 sctp_route_t * ro, 3199 struct sctp_nets *net, 3200 int non_asoc_addr_ok, uint32_t vrf_id) 3201 { 3202 struct sctp_ifa *answer; 3203 uint8_t dest_is_priv, dest_is_loop; 3204 sa_family_t fam; 3205 3206 #ifdef INET 3207 struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst; 3208 3209 #endif 3210 #ifdef INET6 3211 struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst; 3212 3213 #endif 3214 3215 /** 3216 * Rules: - Find the route if needed, cache if I can. - Look at 3217 * interface address in route, Is it in the bound list. If so we 3218 * have the best source. - If not we must rotate amongst the 3219 * addresses. 3220 * 3221 * Cavets and issues 3222 * 3223 * Do we need to pay attention to scope. We can have a private address 3224 * or a global address we are sourcing or sending to. So if we draw 3225 * it out 3226 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 3227 * For V4 3228 * ------------------------------------------ 3229 * source * dest * result 3230 * ----------------------------------------- 3231 * <a> Private * Global * NAT 3232 * ----------------------------------------- 3233 * <b> Private * Private * No problem 3234 * ----------------------------------------- 3235 * <c> Global * Private * Huh, How will this work? 3236 * ----------------------------------------- 3237 * <d> Global * Global * No Problem 3238 *------------------------------------------ 3239 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 3240 * For V6 3241 *------------------------------------------ 3242 * source * dest * result 3243 * ----------------------------------------- 3244 * <a> Linklocal * Global * 3245 * ----------------------------------------- 3246 * <b> Linklocal * Linklocal * No problem 3247 * ----------------------------------------- 3248 * <c> Global * Linklocal * Huh, How will this work? 3249 * ----------------------------------------- 3250 * <d> Global * Global * No Problem 3251 *------------------------------------------ 3252 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 3253 * 3254 * And then we add to that what happens if there are multiple addresses 3255 * assigned to an interface. Remember the ifa on a ifn is a linked 3256 * list of addresses. So one interface can have more than one IP 3257 * address. What happens if we have both a private and a global 3258 * address? Do we then use context of destination to sort out which 3259 * one is best? And what about NAT's sending P->G may get you a NAT 3260 * translation, or should you select the G thats on the interface in 3261 * preference. 3262 * 3263 * Decisions: 3264 * 3265 * - count the number of addresses on the interface. 3266 * - if it is one, no problem except case <c>. 3267 * For <a> we will assume a NAT out there. 3268 * - if there are more than one, then we need to worry about scope P 3269 * or G. We should prefer G -> G and P -> P if possible. 3270 * Then as a secondary fall back to mixed types G->P being a last 3271 * ditch one. 3272 * - The above all works for bound all, but bound specific we need to 3273 * use the same concept but instead only consider the bound 3274 * addresses. If the bound set is NOT assigned to the interface then 3275 * we must use rotation amongst the bound addresses.. 3276 */ 3277 if (ro->ro_rt == NULL) { 3278 /* 3279 * Need a route to cache. 3280 */ 3281 SCTP_RTALLOC(ro, vrf_id); 3282 } 3283 if (ro->ro_rt == NULL) { 3284 return (NULL); 3285 } 3286 fam = ro->ro_dst.sa_family; 3287 dest_is_priv = dest_is_loop = 0; 3288 /* Setup our scopes for the destination */ 3289 switch (fam) { 3290 #ifdef INET 3291 case AF_INET: 3292 /* Scope based on outbound address */ 3293 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) { 3294 dest_is_loop = 1; 3295 if (net != NULL) { 3296 /* mark it as local */ 3297 net->addr_is_local = 1; 3298 } 3299 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) { 3300 dest_is_priv = 1; 3301 } 3302 break; 3303 #endif 3304 #ifdef INET6 3305 case AF_INET6: 3306 /* Scope based on outbound address */ 3307 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) || 3308 SCTP_ROUTE_IS_REAL_LOOP(ro)) { 3309 /* 3310 * If the address is a loopback address, which 3311 * consists of "::1" OR "fe80::1%lo0", we are 3312 * loopback scope. But we don't use dest_is_priv 3313 * (link local addresses). 3314 */ 3315 dest_is_loop = 1; 3316 if (net != NULL) { 3317 /* mark it as local */ 3318 net->addr_is_local = 1; 3319 } 3320 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) { 3321 dest_is_priv = 1; 3322 } 3323 break; 3324 #endif 3325 } 3326 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:"); 3327 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst); 3328 SCTP_IPI_ADDR_RLOCK(); 3329 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 3330 /* 3331 * Bound all case 3332 */ 3333 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id, 3334 dest_is_priv, dest_is_loop, 3335 non_asoc_addr_ok, fam); 3336 SCTP_IPI_ADDR_RUNLOCK(); 3337 return (answer); 3338 } 3339 /* 3340 * Subset bound case 3341 */ 3342 if (stcb) { 3343 answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro, 3344 vrf_id, dest_is_priv, 3345 dest_is_loop, 3346 non_asoc_addr_ok, fam); 3347 } else { 3348 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id, 3349 non_asoc_addr_ok, 3350 dest_is_priv, 3351 dest_is_loop, fam); 3352 } 3353 SCTP_IPI_ADDR_RUNLOCK(); 3354 return (answer); 3355 } 3356 3357 static int 3358 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, size_t cpsize) 3359 { 3360 struct cmsghdr cmh; 3361 int tlen, at, found; 3362 struct sctp_sndinfo sndinfo; 3363 struct sctp_prinfo prinfo; 3364 struct sctp_authinfo authinfo; 3365 3366 tlen = SCTP_BUF_LEN(control); 3367 at = 0; 3368 found = 0; 3369 /* 3370 * Independent of how many mbufs, find the c_type inside the control 3371 * structure and copy out the data. 3372 */ 3373 while (at < tlen) { 3374 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) { 3375 /* There is not enough room for one more. */ 3376 return (found); 3377 } 3378 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh); 3379 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) { 3380 /* We dont't have a complete CMSG header. */ 3381 return (found); 3382 } 3383 if (((int)cmh.cmsg_len + at) > tlen) { 3384 /* We don't have the complete CMSG. */ 3385 return (found); 3386 } 3387 if ((cmh.cmsg_level == IPPROTO_SCTP) && 3388 ((c_type == cmh.cmsg_type) || 3389 ((c_type == SCTP_SNDRCV) && 3390 ((cmh.cmsg_type == SCTP_SNDINFO) || 3391 (cmh.cmsg_type == SCTP_PRINFO) || 3392 (cmh.cmsg_type == SCTP_AUTHINFO))))) { 3393 if (c_type == cmh.cmsg_type) { 3394 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) { 3395 return (found); 3396 } 3397 /* It is exactly what we want. Copy it out. */ 3398 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), cpsize, (caddr_t)data); 3399 return (1); 3400 } else { 3401 struct sctp_sndrcvinfo *sndrcvinfo; 3402 3403 sndrcvinfo = (struct sctp_sndrcvinfo *)data; 3404 if (found == 0) { 3405 if (cpsize < sizeof(struct sctp_sndrcvinfo)) { 3406 return (found); 3407 } 3408 memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo)); 3409 } 3410 switch (cmh.cmsg_type) { 3411 case SCTP_SNDINFO: 3412 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_sndinfo)) { 3413 return (found); 3414 } 3415 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_sndinfo), (caddr_t)&sndinfo); 3416 sndrcvinfo->sinfo_stream = sndinfo.snd_sid; 3417 sndrcvinfo->sinfo_flags = sndinfo.snd_flags; 3418 sndrcvinfo->sinfo_ppid = sndinfo.snd_ppid; 3419 sndrcvinfo->sinfo_context = sndinfo.snd_context; 3420 sndrcvinfo->sinfo_assoc_id = sndinfo.snd_assoc_id; 3421 break; 3422 case SCTP_PRINFO: 3423 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_prinfo)) { 3424 return (found); 3425 } 3426 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_prinfo), (caddr_t)&prinfo); 3427 sndrcvinfo->sinfo_timetolive = prinfo.pr_value; 3428 sndrcvinfo->sinfo_flags |= prinfo.pr_policy; 3429 break; 3430 case SCTP_AUTHINFO: 3431 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_authinfo)) { 3432 return (found); 3433 } 3434 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_authinfo), (caddr_t)&authinfo); 3435 sndrcvinfo->sinfo_keynumber_valid = 1; 3436 sndrcvinfo->sinfo_keynumber = authinfo.auth_keyid; 3437 break; 3438 default: 3439 return (found); 3440 } 3441 found = 1; 3442 } 3443 } 3444 at += CMSG_ALIGN(cmh.cmsg_len); 3445 } 3446 return (found); 3447 } 3448 3449 static int 3450 sctp_process_cmsgs_for_init(struct sctp_tcb *stcb, struct mbuf *control, int *error) 3451 { 3452 struct cmsghdr cmh; 3453 int tlen, at; 3454 struct sctp_initmsg initmsg; 3455 3456 #ifdef INET 3457 struct sockaddr_in sin; 3458 3459 #endif 3460 #ifdef INET6 3461 struct sockaddr_in6 sin6; 3462 3463 #endif 3464 3465 tlen = SCTP_BUF_LEN(control); 3466 at = 0; 3467 while (at < tlen) { 3468 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) { 3469 /* There is not enough room for one more. */ 3470 *error = EINVAL; 3471 return (1); 3472 } 3473 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh); 3474 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) { 3475 /* We dont't have a complete CMSG header. */ 3476 *error = EINVAL; 3477 return (1); 3478 } 3479 if (((int)cmh.cmsg_len + at) > tlen) { 3480 /* We don't have the complete CMSG. */ 3481 *error = EINVAL; 3482 return (1); 3483 } 3484 if (cmh.cmsg_level == IPPROTO_SCTP) { 3485 switch (cmh.cmsg_type) { 3486 case SCTP_INIT: 3487 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_initmsg)) { 3488 *error = EINVAL; 3489 return (1); 3490 } 3491 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_initmsg), (caddr_t)&initmsg); 3492 if (initmsg.sinit_max_attempts) 3493 stcb->asoc.max_init_times = initmsg.sinit_max_attempts; 3494 if (initmsg.sinit_num_ostreams) 3495 stcb->asoc.pre_open_streams = initmsg.sinit_num_ostreams; 3496 if (initmsg.sinit_max_instreams) 3497 stcb->asoc.max_inbound_streams = initmsg.sinit_max_instreams; 3498 if (initmsg.sinit_max_init_timeo) 3499 stcb->asoc.initial_init_rto_max = initmsg.sinit_max_init_timeo; 3500 if (stcb->asoc.streamoutcnt < stcb->asoc.pre_open_streams) { 3501 struct sctp_stream_out *tmp_str; 3502 unsigned int i; 3503 3504 /* Default is NOT correct */ 3505 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, default:%d pre_open:%d\n", 3506 stcb->asoc.streamoutcnt, stcb->asoc.pre_open_streams); 3507 SCTP_TCB_UNLOCK(stcb); 3508 SCTP_MALLOC(tmp_str, 3509 struct sctp_stream_out *, 3510 (stcb->asoc.pre_open_streams * sizeof(struct sctp_stream_out)), 3511 SCTP_M_STRMO); 3512 SCTP_TCB_LOCK(stcb); 3513 if (tmp_str != NULL) { 3514 SCTP_FREE(stcb->asoc.strmout, SCTP_M_STRMO); 3515 stcb->asoc.strmout = tmp_str; 3516 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt = stcb->asoc.pre_open_streams; 3517 } else { 3518 stcb->asoc.pre_open_streams = stcb->asoc.streamoutcnt; 3519 } 3520 for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 3521 stcb->asoc.strmout[i].next_sequence_sent = 0; 3522 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue); 3523 stcb->asoc.strmout[i].stream_no = i; 3524 stcb->asoc.strmout[i].last_msg_incomplete = 0; 3525 stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], NULL); 3526 } 3527 } 3528 break; 3529 #ifdef INET 3530 case SCTP_DSTADDRV4: 3531 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in_addr)) { 3532 *error = EINVAL; 3533 return (1); 3534 } 3535 memset(&sin, 0, sizeof(struct sockaddr_in)); 3536 sin.sin_family = AF_INET; 3537 sin.sin_len = sizeof(struct sockaddr_in); 3538 sin.sin_port = stcb->rport; 3539 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in_addr), (caddr_t)&sin.sin_addr); 3540 if ((sin.sin_addr.s_addr == INADDR_ANY) || 3541 (sin.sin_addr.s_addr == INADDR_BROADCAST) || 3542 IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 3543 *error = EINVAL; 3544 return (1); 3545 } 3546 if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, 3547 SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { 3548 *error = ENOBUFS; 3549 return (1); 3550 } 3551 break; 3552 #endif 3553 #ifdef INET6 3554 case SCTP_DSTADDRV6: 3555 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in6_addr)) { 3556 *error = EINVAL; 3557 return (1); 3558 } 3559 memset(&sin6, 0, sizeof(struct sockaddr_in6)); 3560 sin6.sin6_family = AF_INET6; 3561 sin6.sin6_len = sizeof(struct sockaddr_in6); 3562 sin6.sin6_port = stcb->rport; 3563 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr); 3564 if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) || 3565 IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) { 3566 *error = EINVAL; 3567 return (1); 3568 } 3569 #ifdef INET 3570 if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) { 3571 in6_sin6_2_sin(&sin, &sin6); 3572 if ((sin.sin_addr.s_addr == INADDR_ANY) || 3573 (sin.sin_addr.s_addr == INADDR_BROADCAST) || 3574 IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 3575 *error = EINVAL; 3576 return (1); 3577 } 3578 if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, 3579 SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { 3580 *error = ENOBUFS; 3581 return (1); 3582 } 3583 } else 3584 #endif 3585 if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin6, NULL, 3586 SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { 3587 *error = ENOBUFS; 3588 return (1); 3589 } 3590 break; 3591 #endif 3592 default: 3593 break; 3594 } 3595 } 3596 at += CMSG_ALIGN(cmh.cmsg_len); 3597 } 3598 return (0); 3599 } 3600 3601 static struct sctp_tcb * 3602 sctp_findassociation_cmsgs(struct sctp_inpcb **inp_p, 3603 in_port_t port, 3604 struct mbuf *control, 3605 struct sctp_nets **net_p, 3606 int *error) 3607 { 3608 struct cmsghdr cmh; 3609 int tlen, at; 3610 struct sctp_tcb *stcb; 3611 struct sockaddr *addr; 3612 3613 #ifdef INET 3614 struct sockaddr_in sin; 3615 3616 #endif 3617 #ifdef INET6 3618 struct sockaddr_in6 sin6; 3619 3620 #endif 3621 3622 tlen = SCTP_BUF_LEN(control); 3623 at = 0; 3624 while (at < tlen) { 3625 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) { 3626 /* There is not enough room for one more. */ 3627 *error = EINVAL; 3628 return (NULL); 3629 } 3630 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh); 3631 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) { 3632 /* We dont't have a complete CMSG header. */ 3633 *error = EINVAL; 3634 return (NULL); 3635 } 3636 if (((int)cmh.cmsg_len + at) > tlen) { 3637 /* We don't have the complete CMSG. */ 3638 *error = EINVAL; 3639 return (NULL); 3640 } 3641 if (cmh.cmsg_level == IPPROTO_SCTP) { 3642 switch (cmh.cmsg_type) { 3643 #ifdef INET 3644 case SCTP_DSTADDRV4: 3645 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in_addr)) { 3646 *error = EINVAL; 3647 return (NULL); 3648 } 3649 memset(&sin, 0, sizeof(struct sockaddr_in)); 3650 sin.sin_family = AF_INET; 3651 sin.sin_len = sizeof(struct sockaddr_in); 3652 sin.sin_port = port; 3653 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in_addr), (caddr_t)&sin.sin_addr); 3654 addr = (struct sockaddr *)&sin; 3655 break; 3656 #endif 3657 #ifdef INET6 3658 case SCTP_DSTADDRV6: 3659 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in6_addr)) { 3660 *error = EINVAL; 3661 return (NULL); 3662 } 3663 memset(&sin6, 0, sizeof(struct sockaddr_in6)); 3664 sin6.sin6_family = AF_INET6; 3665 sin6.sin6_len = sizeof(struct sockaddr_in6); 3666 sin6.sin6_port = port; 3667 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr); 3668 #ifdef INET 3669 if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) { 3670 in6_sin6_2_sin(&sin, &sin6); 3671 addr = (struct sockaddr *)&sin; 3672 } else 3673 #endif 3674 addr = (struct sockaddr *)&sin6; 3675 break; 3676 #endif 3677 default: 3678 addr = NULL; 3679 break; 3680 } 3681 if (addr) { 3682 stcb = sctp_findassociation_ep_addr(inp_p, addr, net_p, NULL, NULL); 3683 if (stcb != NULL) { 3684 return (stcb); 3685 } 3686 } 3687 } 3688 at += CMSG_ALIGN(cmh.cmsg_len); 3689 } 3690 return (NULL); 3691 } 3692 3693 static struct mbuf * 3694 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset, 3695 struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature) 3696 { 3697 struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret; 3698 struct sctp_state_cookie *stc; 3699 struct sctp_paramhdr *ph; 3700 uint8_t *foo; 3701 int sig_offset; 3702 uint16_t cookie_sz; 3703 3704 mret = NULL; 3705 mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) + 3706 sizeof(struct sctp_paramhdr)), 0, 3707 M_DONTWAIT, 1, MT_DATA); 3708 if (mret == NULL) { 3709 return (NULL); 3710 } 3711 copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT); 3712 if (copy_init == NULL) { 3713 sctp_m_freem(mret); 3714 return (NULL); 3715 } 3716 #ifdef SCTP_MBUF_LOGGING 3717 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 3718 struct mbuf *mat; 3719 3720 mat = copy_init; 3721 while (mat) { 3722 if (SCTP_BUF_IS_EXTENDED(mat)) { 3723 sctp_log_mb(mat, SCTP_MBUF_ICOPY); 3724 } 3725 mat = SCTP_BUF_NEXT(mat); 3726 } 3727 } 3728 #endif 3729 copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL, 3730 M_DONTWAIT); 3731 if (copy_initack == NULL) { 3732 sctp_m_freem(mret); 3733 sctp_m_freem(copy_init); 3734 return (NULL); 3735 } 3736 #ifdef SCTP_MBUF_LOGGING 3737 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 3738 struct mbuf *mat; 3739 3740 mat = copy_initack; 3741 while (mat) { 3742 if (SCTP_BUF_IS_EXTENDED(mat)) { 3743 sctp_log_mb(mat, SCTP_MBUF_ICOPY); 3744 } 3745 mat = SCTP_BUF_NEXT(mat); 3746 } 3747 } 3748 #endif 3749 /* easy side we just drop it on the end */ 3750 ph = mtod(mret, struct sctp_paramhdr *); 3751 SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) + 3752 sizeof(struct sctp_paramhdr); 3753 stc = (struct sctp_state_cookie *)((caddr_t)ph + 3754 sizeof(struct sctp_paramhdr)); 3755 ph->param_type = htons(SCTP_STATE_COOKIE); 3756 ph->param_length = 0; /* fill in at the end */ 3757 /* Fill in the stc cookie data */ 3758 memcpy(stc, stc_in, sizeof(struct sctp_state_cookie)); 3759 3760 /* tack the INIT and then the INIT-ACK onto the chain */ 3761 cookie_sz = 0; 3762 m_at = mret; 3763 for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) { 3764 cookie_sz += SCTP_BUF_LEN(m_at); 3765 if (SCTP_BUF_NEXT(m_at) == NULL) { 3766 SCTP_BUF_NEXT(m_at) = copy_init; 3767 break; 3768 } 3769 } 3770 3771 for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) { 3772 cookie_sz += SCTP_BUF_LEN(m_at); 3773 if (SCTP_BUF_NEXT(m_at) == NULL) { 3774 SCTP_BUF_NEXT(m_at) = copy_initack; 3775 break; 3776 } 3777 } 3778 3779 for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) { 3780 cookie_sz += SCTP_BUF_LEN(m_at); 3781 if (SCTP_BUF_NEXT(m_at) == NULL) { 3782 break; 3783 } 3784 } 3785 sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA); 3786 if (sig == NULL) { 3787 /* no space, so free the entire chain */ 3788 sctp_m_freem(mret); 3789 return (NULL); 3790 } 3791 SCTP_BUF_LEN(sig) = 0; 3792 SCTP_BUF_NEXT(m_at) = sig; 3793 sig_offset = 0; 3794 foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset); 3795 memset(foo, 0, SCTP_SIGNATURE_SIZE); 3796 *signature = foo; 3797 SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE; 3798 cookie_sz += SCTP_SIGNATURE_SIZE; 3799 ph->param_length = htons(cookie_sz); 3800 return (mret); 3801 } 3802 3803 3804 static uint8_t 3805 sctp_get_ect(struct sctp_tcb *stcb, 3806 struct sctp_tmit_chunk *chk) 3807 { 3808 if ((stcb != NULL) && (stcb->asoc.ecn_allowed == 1)) { 3809 return (SCTP_ECT0_BIT); 3810 } else { 3811 return (0); 3812 } 3813 } 3814 3815 static void 3816 sctp_handle_no_route(struct sctp_tcb *stcb, 3817 struct sctp_nets *net, 3818 int so_locked) 3819 { 3820 SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n"); 3821 3822 if (net) { 3823 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was "); 3824 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa); 3825 if (net->dest_state & SCTP_ADDR_CONFIRMED) { 3826 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) { 3827 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net); 3828 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, 3829 stcb, 3830 SCTP_FAILED_THRESHOLD, 3831 (void *)net, 3832 so_locked); 3833 net->dest_state &= ~SCTP_ADDR_REACHABLE; 3834 net->dest_state &= ~SCTP_ADDR_PF; 3835 } 3836 } 3837 if (stcb) { 3838 if (net == stcb->asoc.primary_destination) { 3839 /* need a new primary */ 3840 struct sctp_nets *alt; 3841 3842 alt = sctp_find_alternate_net(stcb, net, 0); 3843 if (alt != net) { 3844 if (stcb->asoc.alternate) { 3845 sctp_free_remote_addr(stcb->asoc.alternate); 3846 } 3847 stcb->asoc.alternate = alt; 3848 atomic_add_int(&stcb->asoc.alternate->ref_count, 1); 3849 if (net->ro._s_addr) { 3850 sctp_free_ifa(net->ro._s_addr); 3851 net->ro._s_addr = NULL; 3852 } 3853 net->src_addr_selected = 0; 3854 } 3855 } 3856 } 3857 } 3858 } 3859 3860 static int 3861 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp, 3862 struct sctp_tcb *stcb, /* may be NULL */ 3863 struct sctp_nets *net, 3864 struct sockaddr *to, 3865 struct mbuf *m, 3866 uint32_t auth_offset, 3867 struct sctp_auth_chunk *auth, 3868 uint16_t auth_keyid, 3869 int nofragment_flag, 3870 int ecn_ok, 3871 struct sctp_tmit_chunk *chk, 3872 int out_of_asoc_ok, 3873 uint16_t src_port, 3874 uint16_t dest_port, 3875 uint32_t v_tag, 3876 uint16_t port, 3877 int so_locked, 3878 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 3879 SCTP_UNUSED 3880 #endif 3881 union sctp_sockstore *over_addr, 3882 struct mbuf *init 3883 ) 3884 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */ 3885 { 3886 /** 3887 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet header 3888 * WITH an SCTPHDR but no IP header, endpoint inp and sa structure: 3889 * - fill in the HMAC digest of any AUTH chunk in the packet. 3890 * - calculate and fill in the SCTP checksum. 3891 * - prepend an IP address header. 3892 * - if boundall use INADDR_ANY. 3893 * - if boundspecific do source address selection. 3894 * - set fragmentation option for ipV4. 3895 * - On return from IP output, check/adjust mtu size of output 3896 * interface and smallest_mtu size as well. 3897 */ 3898 /* Will need ifdefs around this */ 3899 struct mbuf *o_pak; 3900 struct mbuf *newm; 3901 struct sctphdr *sctphdr; 3902 int packet_length; 3903 int ret; 3904 uint32_t vrf_id; 3905 sctp_route_t *ro = NULL; 3906 struct udphdr *udp = NULL; 3907 uint8_t tos_value; 3908 3909 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3910 struct socket *so = NULL; 3911 3912 #endif 3913 3914 if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) { 3915 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT); 3916 sctp_m_freem(m); 3917 return (EFAULT); 3918 } 3919 if (stcb) { 3920 vrf_id = stcb->asoc.vrf_id; 3921 } else { 3922 vrf_id = inp->def_vrf_id; 3923 } 3924 3925 /* fill in the HMAC digest for any AUTH chunk in the packet */ 3926 if ((auth != NULL) && (stcb != NULL)) { 3927 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid); 3928 } 3929 if (net) { 3930 tos_value = net->dscp; 3931 } else if (stcb) { 3932 tos_value = stcb->asoc.default_dscp; 3933 } else { 3934 tos_value = inp->sctp_ep.default_dscp; 3935 } 3936 3937 switch (to->sa_family) { 3938 #ifdef INET 3939 case AF_INET: 3940 { 3941 struct ip *ip = NULL; 3942 sctp_route_t iproute; 3943 int len; 3944 3945 len = sizeof(struct ip) + sizeof(struct sctphdr); 3946 if (port) { 3947 len += sizeof(struct udphdr); 3948 } 3949 newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA); 3950 if (newm == NULL) { 3951 sctp_m_freem(m); 3952 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 3953 return (ENOMEM); 3954 } 3955 SCTP_ALIGN_TO_END(newm, len); 3956 SCTP_BUF_LEN(newm) = len; 3957 SCTP_BUF_NEXT(newm) = m; 3958 m = newm; 3959 if (net != NULL) { 3960 #ifdef INVARIANTS 3961 if (net->flowidset == 0) { 3962 panic("Flow ID not set"); 3963 } 3964 #endif 3965 m->m_pkthdr.flowid = net->flowid; 3966 m->m_flags |= M_FLOWID; 3967 } else { 3968 if ((init != NULL) && (init->m_flags & M_FLOWID)) { 3969 m->m_pkthdr.flowid = init->m_pkthdr.flowid; 3970 m->m_flags |= M_FLOWID; 3971 } 3972 } 3973 packet_length = sctp_calculate_len(m); 3974 ip = mtod(m, struct ip *); 3975 ip->ip_v = IPVERSION; 3976 ip->ip_hl = (sizeof(struct ip) >> 2); 3977 if (tos_value == 0) { 3978 /* 3979 * This means especially, that it is not set 3980 * at the SCTP layer. So use the value from 3981 * the IP layer. 3982 */ 3983 tos_value = inp->ip_inp.inp.inp_ip_tos; 3984 } 3985 tos_value &= 0xfc; 3986 if (ecn_ok) { 3987 tos_value |= sctp_get_ect(stcb, chk); 3988 } 3989 if ((nofragment_flag) && (port == 0)) { 3990 ip->ip_off = IP_DF; 3991 } else 3992 ip->ip_off = 0; 3993 3994 /* FreeBSD has a function for ip_id's */ 3995 ip->ip_id = ip_newid(); 3996 3997 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl; 3998 ip->ip_len = packet_length; 3999 ip->ip_tos = tos_value; 4000 if (port) { 4001 ip->ip_p = IPPROTO_UDP; 4002 } else { 4003 ip->ip_p = IPPROTO_SCTP; 4004 } 4005 ip->ip_sum = 0; 4006 if (net == NULL) { 4007 ro = &iproute; 4008 memset(&iproute, 0, sizeof(iproute)); 4009 memcpy(&ro->ro_dst, to, to->sa_len); 4010 } else { 4011 ro = (sctp_route_t *) & net->ro; 4012 } 4013 /* Now the address selection part */ 4014 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr; 4015 4016 /* call the routine to select the src address */ 4017 if (net && out_of_asoc_ok == 0) { 4018 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) { 4019 sctp_free_ifa(net->ro._s_addr); 4020 net->ro._s_addr = NULL; 4021 net->src_addr_selected = 0; 4022 if (ro->ro_rt) { 4023 RTFREE(ro->ro_rt); 4024 ro->ro_rt = NULL; 4025 } 4026 } 4027 if (net->src_addr_selected == 0) { 4028 /* Cache the source address */ 4029 net->ro._s_addr = sctp_source_address_selection(inp, stcb, 4030 ro, net, 0, 4031 vrf_id); 4032 net->src_addr_selected = 1; 4033 } 4034 if (net->ro._s_addr == NULL) { 4035 /* No route to host */ 4036 net->src_addr_selected = 0; 4037 sctp_handle_no_route(stcb, net, so_locked); 4038 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); 4039 sctp_m_freem(m); 4040 return (EHOSTUNREACH); 4041 } 4042 ip->ip_src = net->ro._s_addr->address.sin.sin_addr; 4043 } else { 4044 if (over_addr == NULL) { 4045 struct sctp_ifa *_lsrc; 4046 4047 _lsrc = sctp_source_address_selection(inp, stcb, ro, 4048 net, 4049 out_of_asoc_ok, 4050 vrf_id); 4051 if (_lsrc == NULL) { 4052 sctp_handle_no_route(stcb, net, so_locked); 4053 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); 4054 sctp_m_freem(m); 4055 return (EHOSTUNREACH); 4056 } 4057 ip->ip_src = _lsrc->address.sin.sin_addr; 4058 sctp_free_ifa(_lsrc); 4059 } else { 4060 ip->ip_src = over_addr->sin.sin_addr; 4061 SCTP_RTALLOC(ro, vrf_id); 4062 } 4063 } 4064 if (port) { 4065 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) { 4066 sctp_handle_no_route(stcb, net, so_locked); 4067 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); 4068 sctp_m_freem(m); 4069 return (EHOSTUNREACH); 4070 } 4071 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip)); 4072 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)); 4073 udp->uh_dport = port; 4074 udp->uh_ulen = htons(packet_length - sizeof(struct ip)); 4075 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP)); 4076 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr)); 4077 } else { 4078 sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip)); 4079 } 4080 4081 sctphdr->src_port = src_port; 4082 sctphdr->dest_port = dest_port; 4083 sctphdr->v_tag = v_tag; 4084 sctphdr->checksum = 0; 4085 4086 /* 4087 * If source address selection fails and we find no 4088 * route then the ip_output should fail as well with 4089 * a NO_ROUTE_TO_HOST type error. We probably should 4090 * catch that somewhere and abort the association 4091 * right away (assuming this is an INIT being sent). 4092 */ 4093 if (ro->ro_rt == NULL) { 4094 /* 4095 * src addr selection failed to find a route 4096 * (or valid source addr), so we can't get 4097 * there from here (yet)! 4098 */ 4099 sctp_handle_no_route(stcb, net, so_locked); 4100 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); 4101 sctp_m_freem(m); 4102 return (EHOSTUNREACH); 4103 } 4104 if (ro != &iproute) { 4105 memcpy(&iproute, ro, sizeof(*ro)); 4106 } 4107 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n", 4108 (uint32_t) (ntohl(ip->ip_src.s_addr))); 4109 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n", 4110 (uint32_t) (ntohl(ip->ip_dst.s_addr))); 4111 SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n", 4112 ro->ro_rt); 4113 4114 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) { 4115 /* failed to prepend data, give up */ 4116 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 4117 sctp_m_freem(m); 4118 return (ENOMEM); 4119 } 4120 #ifdef SCTP_PACKET_LOGGING 4121 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 4122 sctp_packet_log(m, packet_length); 4123 #endif 4124 SCTP_ATTACH_CHAIN(o_pak, m, packet_length); 4125 if (port) { 4126 #if defined(SCTP_WITH_NO_CSUM) 4127 SCTP_STAT_INCR(sctps_sendnocrc); 4128 #else 4129 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) && 4130 (stcb) && 4131 (stcb->asoc.loopback_scope))) { 4132 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr)); 4133 SCTP_STAT_INCR(sctps_sendswcrc); 4134 } else { 4135 SCTP_STAT_INCR(sctps_sendnocrc); 4136 } 4137 #endif 4138 SCTP_ENABLE_UDP_CSUM(o_pak); 4139 } else { 4140 #if defined(SCTP_WITH_NO_CSUM) 4141 SCTP_STAT_INCR(sctps_sendnocrc); 4142 #else 4143 m->m_pkthdr.csum_flags = CSUM_SCTP; 4144 m->m_pkthdr.csum_data = 0; 4145 SCTP_STAT_INCR(sctps_sendhwcrc); 4146 #endif 4147 } 4148 /* send it out. table id is taken from stcb */ 4149 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4150 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) { 4151 so = SCTP_INP_SO(inp); 4152 SCTP_SOCKET_UNLOCK(so, 0); 4153 } 4154 #endif 4155 SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id); 4156 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4157 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) { 4158 atomic_add_int(&stcb->asoc.refcnt, 1); 4159 SCTP_TCB_UNLOCK(stcb); 4160 SCTP_SOCKET_LOCK(so, 0); 4161 SCTP_TCB_LOCK(stcb); 4162 atomic_subtract_int(&stcb->asoc.refcnt, 1); 4163 } 4164 #endif 4165 SCTP_STAT_INCR(sctps_sendpackets); 4166 SCTP_STAT_INCR_COUNTER64(sctps_outpackets); 4167 if (ret) 4168 SCTP_STAT_INCR(sctps_senderrors); 4169 4170 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret); 4171 if (net == NULL) { 4172 /* free tempy routes */ 4173 if (ro->ro_rt) { 4174 RTFREE(ro->ro_rt); 4175 ro->ro_rt = NULL; 4176 } 4177 } else { 4178 /* 4179 * PMTU check versus smallest asoc MTU goes 4180 * here 4181 */ 4182 if ((ro->ro_rt != NULL) && 4183 (net->ro._s_addr)) { 4184 uint32_t mtu; 4185 4186 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt); 4187 if (net->port) { 4188 mtu -= sizeof(struct udphdr); 4189 } 4190 if (mtu && (stcb->asoc.smallest_mtu > mtu)) { 4191 sctp_mtu_size_reset(inp, &stcb->asoc, mtu); 4192 net->mtu = mtu; 4193 } 4194 } else if (ro->ro_rt == NULL) { 4195 /* route was freed */ 4196 if (net->ro._s_addr && 4197 net->src_addr_selected) { 4198 sctp_free_ifa(net->ro._s_addr); 4199 net->ro._s_addr = NULL; 4200 } 4201 net->src_addr_selected = 0; 4202 } 4203 } 4204 return (ret); 4205 } 4206 #endif 4207 #ifdef INET6 4208 case AF_INET6: 4209 { 4210 uint32_t flowlabel, flowinfo; 4211 struct ip6_hdr *ip6h; 4212 struct route_in6 ip6route; 4213 struct ifnet *ifp; 4214 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp; 4215 int prev_scope = 0; 4216 struct sockaddr_in6 lsa6_storage; 4217 int error; 4218 u_short prev_port = 0; 4219 int len; 4220 4221 if (net) { 4222 flowlabel = net->flowlabel; 4223 } else if (stcb) { 4224 flowlabel = stcb->asoc.default_flowlabel; 4225 } else { 4226 flowlabel = inp->sctp_ep.default_flowlabel; 4227 } 4228 if (flowlabel == 0) { 4229 /* 4230 * This means especially, that it is not set 4231 * at the SCTP layer. So use the value from 4232 * the IP layer. 4233 */ 4234 flowlabel = ntohl(((struct in6pcb *)inp)->in6p_flowinfo); 4235 } 4236 flowlabel &= 0x000fffff; 4237 len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr); 4238 if (port) { 4239 len += sizeof(struct udphdr); 4240 } 4241 newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA); 4242 if (newm == NULL) { 4243 sctp_m_freem(m); 4244 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 4245 return (ENOMEM); 4246 } 4247 SCTP_ALIGN_TO_END(newm, len); 4248 SCTP_BUF_LEN(newm) = len; 4249 SCTP_BUF_NEXT(newm) = m; 4250 m = newm; 4251 if (net != NULL) { 4252 #ifdef INVARIANTS 4253 if (net->flowidset == 0) { 4254 panic("Flow ID not set"); 4255 } 4256 #endif 4257 m->m_pkthdr.flowid = net->flowid; 4258 m->m_flags |= M_FLOWID; 4259 } else { 4260 if ((init != NULL) && (init->m_flags & M_FLOWID)) { 4261 m->m_pkthdr.flowid = init->m_pkthdr.flowid; 4262 m->m_flags |= M_FLOWID; 4263 } 4264 } 4265 packet_length = sctp_calculate_len(m); 4266 4267 ip6h = mtod(m, struct ip6_hdr *); 4268 /* protect *sin6 from overwrite */ 4269 sin6 = (struct sockaddr_in6 *)to; 4270 tmp = *sin6; 4271 sin6 = &tmp; 4272 4273 /* KAME hack: embed scopeid */ 4274 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) { 4275 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 4276 return (EINVAL); 4277 } 4278 if (net == NULL) { 4279 memset(&ip6route, 0, sizeof(ip6route)); 4280 ro = (sctp_route_t *) & ip6route; 4281 memcpy(&ro->ro_dst, sin6, sin6->sin6_len); 4282 } else { 4283 ro = (sctp_route_t *) & net->ro; 4284 } 4285 /* 4286 * We assume here that inp_flow is in host byte 4287 * order within the TCB! 4288 */ 4289 if (tos_value == 0) { 4290 /* 4291 * This means especially, that it is not set 4292 * at the SCTP layer. So use the value from 4293 * the IP layer. 4294 */ 4295 tos_value = (ntohl(((struct in6pcb *)inp)->in6p_flowinfo) >> 20) & 0xff; 4296 } 4297 tos_value &= 0xfc; 4298 if (ecn_ok) { 4299 tos_value |= sctp_get_ect(stcb, chk); 4300 } 4301 flowinfo = 0x06; 4302 flowinfo <<= 8; 4303 flowinfo |= tos_value; 4304 flowinfo <<= 20; 4305 flowinfo |= flowlabel; 4306 ip6h->ip6_flow = htonl(flowinfo); 4307 if (port) { 4308 ip6h->ip6_nxt = IPPROTO_UDP; 4309 } else { 4310 ip6h->ip6_nxt = IPPROTO_SCTP; 4311 } 4312 ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr)); 4313 ip6h->ip6_dst = sin6->sin6_addr; 4314 4315 /* 4316 * Add SRC address selection here: we can only reuse 4317 * to a limited degree the kame src-addr-sel, since 4318 * we can try their selection but it may not be 4319 * bound. 4320 */ 4321 bzero(&lsa6_tmp, sizeof(lsa6_tmp)); 4322 lsa6_tmp.sin6_family = AF_INET6; 4323 lsa6_tmp.sin6_len = sizeof(lsa6_tmp); 4324 lsa6 = &lsa6_tmp; 4325 if (net && out_of_asoc_ok == 0) { 4326 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) { 4327 sctp_free_ifa(net->ro._s_addr); 4328 net->ro._s_addr = NULL; 4329 net->src_addr_selected = 0; 4330 if (ro->ro_rt) { 4331 RTFREE(ro->ro_rt); 4332 ro->ro_rt = NULL; 4333 } 4334 } 4335 if (net->src_addr_selected == 0) { 4336 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; 4337 /* KAME hack: embed scopeid */ 4338 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) { 4339 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 4340 return (EINVAL); 4341 } 4342 /* Cache the source address */ 4343 net->ro._s_addr = sctp_source_address_selection(inp, 4344 stcb, 4345 ro, 4346 net, 4347 0, 4348 vrf_id); 4349 (void)sa6_recoverscope(sin6); 4350 net->src_addr_selected = 1; 4351 } 4352 if (net->ro._s_addr == NULL) { 4353 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n"); 4354 net->src_addr_selected = 0; 4355 sctp_handle_no_route(stcb, net, so_locked); 4356 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); 4357 sctp_m_freem(m); 4358 return (EHOSTUNREACH); 4359 } 4360 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr; 4361 } else { 4362 sin6 = (struct sockaddr_in6 *)&ro->ro_dst; 4363 /* KAME hack: embed scopeid */ 4364 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) { 4365 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 4366 return (EINVAL); 4367 } 4368 if (over_addr == NULL) { 4369 struct sctp_ifa *_lsrc; 4370 4371 _lsrc = sctp_source_address_selection(inp, stcb, ro, 4372 net, 4373 out_of_asoc_ok, 4374 vrf_id); 4375 if (_lsrc == NULL) { 4376 sctp_handle_no_route(stcb, net, so_locked); 4377 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); 4378 sctp_m_freem(m); 4379 return (EHOSTUNREACH); 4380 } 4381 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr; 4382 sctp_free_ifa(_lsrc); 4383 } else { 4384 lsa6->sin6_addr = over_addr->sin6.sin6_addr; 4385 SCTP_RTALLOC(ro, vrf_id); 4386 } 4387 (void)sa6_recoverscope(sin6); 4388 } 4389 lsa6->sin6_port = inp->sctp_lport; 4390 4391 if (ro->ro_rt == NULL) { 4392 /* 4393 * src addr selection failed to find a route 4394 * (or valid source addr), so we can't get 4395 * there from here! 4396 */ 4397 sctp_handle_no_route(stcb, net, so_locked); 4398 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); 4399 sctp_m_freem(m); 4400 return (EHOSTUNREACH); 4401 } 4402 /* 4403 * XXX: sa6 may not have a valid sin6_scope_id in 4404 * the non-SCOPEDROUTING case. 4405 */ 4406 bzero(&lsa6_storage, sizeof(lsa6_storage)); 4407 lsa6_storage.sin6_family = AF_INET6; 4408 lsa6_storage.sin6_len = sizeof(lsa6_storage); 4409 lsa6_storage.sin6_addr = lsa6->sin6_addr; 4410 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) { 4411 SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error); 4412 sctp_m_freem(m); 4413 return (error); 4414 } 4415 /* XXX */ 4416 lsa6_storage.sin6_addr = lsa6->sin6_addr; 4417 lsa6_storage.sin6_port = inp->sctp_lport; 4418 lsa6 = &lsa6_storage; 4419 ip6h->ip6_src = lsa6->sin6_addr; 4420 4421 if (port) { 4422 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) { 4423 sctp_handle_no_route(stcb, net, so_locked); 4424 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); 4425 sctp_m_freem(m); 4426 return (EHOSTUNREACH); 4427 } 4428 udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr)); 4429 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)); 4430 udp->uh_dport = port; 4431 udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr)); 4432 udp->uh_sum = 0; 4433 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr)); 4434 } else { 4435 sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr)); 4436 } 4437 4438 sctphdr->src_port = src_port; 4439 sctphdr->dest_port = dest_port; 4440 sctphdr->v_tag = v_tag; 4441 sctphdr->checksum = 0; 4442 4443 /* 4444 * We set the hop limit now since there is a good 4445 * chance that our ro pointer is now filled 4446 */ 4447 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro); 4448 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro); 4449 4450 #ifdef SCTP_DEBUG 4451 /* Copy to be sure something bad is not happening */ 4452 sin6->sin6_addr = ip6h->ip6_dst; 4453 lsa6->sin6_addr = ip6h->ip6_src; 4454 #endif 4455 4456 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n"); 4457 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: "); 4458 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6); 4459 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: "); 4460 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6); 4461 if (net) { 4462 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; 4463 /* 4464 * preserve the port and scope for link 4465 * local send 4466 */ 4467 prev_scope = sin6->sin6_scope_id; 4468 prev_port = sin6->sin6_port; 4469 } 4470 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) { 4471 /* failed to prepend data, give up */ 4472 sctp_m_freem(m); 4473 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 4474 return (ENOMEM); 4475 } 4476 #ifdef SCTP_PACKET_LOGGING 4477 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 4478 sctp_packet_log(m, packet_length); 4479 #endif 4480 SCTP_ATTACH_CHAIN(o_pak, m, packet_length); 4481 if (port) { 4482 #if defined(SCTP_WITH_NO_CSUM) 4483 SCTP_STAT_INCR(sctps_sendnocrc); 4484 #else 4485 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) && 4486 (stcb) && 4487 (stcb->asoc.loopback_scope))) { 4488 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr)); 4489 SCTP_STAT_INCR(sctps_sendswcrc); 4490 } else { 4491 SCTP_STAT_INCR(sctps_sendnocrc); 4492 } 4493 #endif 4494 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) { 4495 udp->uh_sum = 0xffff; 4496 } 4497 } else { 4498 #if defined(SCTP_WITH_NO_CSUM) 4499 SCTP_STAT_INCR(sctps_sendnocrc); 4500 #else 4501 m->m_pkthdr.csum_flags = CSUM_SCTP; 4502 m->m_pkthdr.csum_data = 0; 4503 SCTP_STAT_INCR(sctps_sendhwcrc); 4504 #endif 4505 } 4506 /* send it out. table id is taken from stcb */ 4507 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4508 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) { 4509 so = SCTP_INP_SO(inp); 4510 SCTP_SOCKET_UNLOCK(so, 0); 4511 } 4512 #endif 4513 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id); 4514 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4515 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) { 4516 atomic_add_int(&stcb->asoc.refcnt, 1); 4517 SCTP_TCB_UNLOCK(stcb); 4518 SCTP_SOCKET_LOCK(so, 0); 4519 SCTP_TCB_LOCK(stcb); 4520 atomic_subtract_int(&stcb->asoc.refcnt, 1); 4521 } 4522 #endif 4523 if (net) { 4524 /* for link local this must be done */ 4525 sin6->sin6_scope_id = prev_scope; 4526 sin6->sin6_port = prev_port; 4527 } 4528 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret); 4529 SCTP_STAT_INCR(sctps_sendpackets); 4530 SCTP_STAT_INCR_COUNTER64(sctps_outpackets); 4531 if (ret) { 4532 SCTP_STAT_INCR(sctps_senderrors); 4533 } 4534 if (net == NULL) { 4535 /* Now if we had a temp route free it */ 4536 if (ro->ro_rt) { 4537 RTFREE(ro->ro_rt); 4538 } 4539 } else { 4540 /* 4541 * PMTU check versus smallest asoc MTU goes 4542 * here 4543 */ 4544 if (ro->ro_rt == NULL) { 4545 /* Route was freed */ 4546 if (net->ro._s_addr && 4547 net->src_addr_selected) { 4548 sctp_free_ifa(net->ro._s_addr); 4549 net->ro._s_addr = NULL; 4550 } 4551 net->src_addr_selected = 0; 4552 } 4553 if ((ro->ro_rt != NULL) && 4554 (net->ro._s_addr)) { 4555 uint32_t mtu; 4556 4557 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt); 4558 if (mtu && 4559 (stcb->asoc.smallest_mtu > mtu)) { 4560 sctp_mtu_size_reset(inp, &stcb->asoc, mtu); 4561 net->mtu = mtu; 4562 if (net->port) { 4563 net->mtu -= sizeof(struct udphdr); 4564 } 4565 } 4566 } else if (ifp) { 4567 if (ND_IFINFO(ifp)->linkmtu && 4568 (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) { 4569 sctp_mtu_size_reset(inp, 4570 &stcb->asoc, 4571 ND_IFINFO(ifp)->linkmtu); 4572 } 4573 } 4574 } 4575 return (ret); 4576 } 4577 #endif 4578 default: 4579 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n", 4580 ((struct sockaddr *)to)->sa_family); 4581 sctp_m_freem(m); 4582 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT); 4583 return (EFAULT); 4584 } 4585 } 4586 4587 4588 void 4589 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked 4590 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 4591 SCTP_UNUSED 4592 #endif 4593 ) 4594 { 4595 struct mbuf *m, *m_at, *mp_last; 4596 struct sctp_nets *net; 4597 struct sctp_init_chunk *init; 4598 struct sctp_supported_addr_param *sup_addr; 4599 struct sctp_adaptation_layer_indication *ali; 4600 struct sctp_ecn_supported_param *ecn; 4601 struct sctp_prsctp_supported_param *prsctp; 4602 struct sctp_supported_chunk_types_param *pr_supported; 4603 int cnt_inits_to = 0; 4604 int padval, ret; 4605 int num_ext; 4606 int p_len; 4607 4608 /* INIT's always go to the primary (and usually ONLY address) */ 4609 mp_last = NULL; 4610 net = stcb->asoc.primary_destination; 4611 if (net == NULL) { 4612 net = TAILQ_FIRST(&stcb->asoc.nets); 4613 if (net == NULL) { 4614 /* TSNH */ 4615 return; 4616 } 4617 /* we confirm any address we send an INIT to */ 4618 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 4619 (void)sctp_set_primary_addr(stcb, NULL, net); 4620 } else { 4621 /* we confirm any address we send an INIT to */ 4622 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; 4623 } 4624 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n"); 4625 #ifdef INET6 4626 if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) { 4627 /* 4628 * special hook, if we are sending to link local it will not 4629 * show up in our private address count. 4630 */ 4631 struct sockaddr_in6 *sin6l; 4632 4633 sin6l = &net->ro._l_addr.sin6; 4634 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr)) 4635 cnt_inits_to = 1; 4636 } 4637 #endif 4638 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 4639 /* This case should not happen */ 4640 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n"); 4641 return; 4642 } 4643 /* start the INIT timer */ 4644 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net); 4645 4646 m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA); 4647 if (m == NULL) { 4648 /* No memory, INIT timer will re-attempt. */ 4649 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n"); 4650 return; 4651 } 4652 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk); 4653 /* 4654 * assume peer supports asconf in order to be able to queue local 4655 * address changes while an INIT is in flight and before the assoc 4656 * is established. 4657 */ 4658 stcb->asoc.peer_supports_asconf = 1; 4659 /* Now lets put the SCTP header in place */ 4660 init = mtod(m, struct sctp_init_chunk *); 4661 /* now the chunk header */ 4662 init->ch.chunk_type = SCTP_INITIATION; 4663 init->ch.chunk_flags = 0; 4664 /* fill in later from mbuf we build */ 4665 init->ch.chunk_length = 0; 4666 /* place in my tag */ 4667 init->init.initiate_tag = htonl(stcb->asoc.my_vtag); 4668 /* set up some of the credits. */ 4669 init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0, 4670 SCTP_MINIMAL_RWND)); 4671 4672 init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams); 4673 init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams); 4674 init->init.initial_tsn = htonl(stcb->asoc.init_seq_number); 4675 /* now the address restriction */ 4676 /* XXX Should we take the address family of the socket into account? */ 4677 sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init + 4678 sizeof(*init)); 4679 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE); 4680 #ifdef INET6 4681 #ifdef INET 4682 /* we support 2 types: IPv4/IPv6 */ 4683 sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + 2 * sizeof(uint16_t)); 4684 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS); 4685 sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS); 4686 #else 4687 /* we support 1 type: IPv6 */ 4688 sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + sizeof(uint16_t)); 4689 sup_addr->addr_type[0] = htons(SCTP_IPV6_ADDRESS); 4690 sup_addr->addr_type[1] = htons(0); /* this is the padding */ 4691 #endif 4692 #else 4693 /* we support 1 type: IPv4 */ 4694 sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + sizeof(uint16_t)); 4695 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS); 4696 sup_addr->addr_type[1] = htons(0); /* this is the padding */ 4697 #endif 4698 SCTP_BUF_LEN(m) += sizeof(struct sctp_supported_addr_param); 4699 /* adaptation layer indication parameter */ 4700 ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(struct sctp_supported_addr_param)); 4701 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION); 4702 ali->ph.param_length = htons(sizeof(*ali)); 4703 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator); 4704 SCTP_BUF_LEN(m) += sizeof(*ali); 4705 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali)); 4706 4707 if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) { 4708 /* Add NAT friendly parameter */ 4709 struct sctp_paramhdr *ph; 4710 4711 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m)); 4712 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT); 4713 ph->param_length = htons(sizeof(struct sctp_paramhdr)); 4714 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr); 4715 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph)); 4716 } 4717 /* now any cookie time extensions */ 4718 if (stcb->asoc.cookie_preserve_req) { 4719 struct sctp_cookie_perserve_param *cookie_preserve; 4720 4721 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn); 4722 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE); 4723 cookie_preserve->ph.param_length = htons( 4724 sizeof(*cookie_preserve)); 4725 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req); 4726 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve); 4727 ecn = (struct sctp_ecn_supported_param *)( 4728 (caddr_t)cookie_preserve + sizeof(*cookie_preserve)); 4729 stcb->asoc.cookie_preserve_req = 0; 4730 } 4731 /* ECN parameter */ 4732 if (stcb->asoc.ecn_allowed == 1) { 4733 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE); 4734 ecn->ph.param_length = htons(sizeof(*ecn)); 4735 SCTP_BUF_LEN(m) += sizeof(*ecn); 4736 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn + 4737 sizeof(*ecn)); 4738 } else { 4739 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn); 4740 } 4741 /* And now tell the peer we do pr-sctp */ 4742 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED); 4743 prsctp->ph.param_length = htons(sizeof(*prsctp)); 4744 SCTP_BUF_LEN(m) += sizeof(*prsctp); 4745 4746 /* And now tell the peer we do all the extensions */ 4747 pr_supported = (struct sctp_supported_chunk_types_param *) 4748 ((caddr_t)prsctp + sizeof(*prsctp)); 4749 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT); 4750 num_ext = 0; 4751 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF; 4752 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK; 4753 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN; 4754 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED; 4755 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET; 4756 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) { 4757 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION; 4758 } 4759 if (stcb->asoc.sctp_nr_sack_on_off == 1) { 4760 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK; 4761 } 4762 p_len = sizeof(*pr_supported) + num_ext; 4763 pr_supported->ph.param_length = htons(p_len); 4764 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len); 4765 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len); 4766 4767 4768 /* add authentication parameters */ 4769 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) { 4770 struct sctp_auth_random *randp; 4771 struct sctp_auth_hmac_algo *hmacs; 4772 struct sctp_auth_chunk_list *chunks; 4773 4774 /* attach RANDOM parameter, if available */ 4775 if (stcb->asoc.authinfo.random != NULL) { 4776 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m)); 4777 p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len; 4778 /* random key already contains the header */ 4779 bcopy(stcb->asoc.authinfo.random->key, randp, p_len); 4780 /* zero out any padding required */ 4781 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len); 4782 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len); 4783 } 4784 /* add HMAC_ALGO parameter */ 4785 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m)); 4786 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs, 4787 (uint8_t *) hmacs->hmac_ids); 4788 if (p_len > 0) { 4789 p_len += sizeof(*hmacs); 4790 hmacs->ph.param_type = htons(SCTP_HMAC_LIST); 4791 hmacs->ph.param_length = htons(p_len); 4792 /* zero out any padding required */ 4793 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len); 4794 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len); 4795 } 4796 /* add CHUNKS parameter */ 4797 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m)); 4798 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks, 4799 chunks->chunk_types); 4800 if (p_len > 0) { 4801 p_len += sizeof(*chunks); 4802 chunks->ph.param_type = htons(SCTP_CHUNK_LIST); 4803 chunks->ph.param_length = htons(p_len); 4804 /* zero out any padding required */ 4805 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len); 4806 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len); 4807 } 4808 } 4809 m_at = m; 4810 /* now the addresses */ 4811 { 4812 struct sctp_scoping scp; 4813 4814 /* 4815 * To optimize this we could put the scoping stuff into a 4816 * structure and remove the individual uint8's from the 4817 * assoc structure. Then we could just sifa in the address 4818 * within the stcb.. but for now this is a quick hack to get 4819 * the address stuff teased apart. 4820 */ 4821 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal; 4822 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal; 4823 scp.loopback_scope = stcb->asoc.loopback_scope; 4824 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope; 4825 scp.local_scope = stcb->asoc.local_scope; 4826 scp.site_scope = stcb->asoc.site_scope; 4827 4828 m_at = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_at, cnt_inits_to); 4829 } 4830 4831 /* calulate the size and update pkt header and chunk header */ 4832 p_len = 0; 4833 for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) { 4834 if (SCTP_BUF_NEXT(m_at) == NULL) 4835 mp_last = m_at; 4836 p_len += SCTP_BUF_LEN(m_at); 4837 } 4838 init->ch.chunk_length = htons(p_len); 4839 /* 4840 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return 4841 * here since the timer will drive a retranmission. 4842 */ 4843 4844 /* I don't expect this to execute but we will be safe here */ 4845 padval = p_len % 4; 4846 if ((padval) && (mp_last)) { 4847 /* 4848 * The compiler worries that mp_last may not be set even 4849 * though I think it is impossible :-> however we add 4850 * mp_last here just in case. 4851 */ 4852 ret = sctp_add_pad_tombuf(mp_last, (4 - padval)); 4853 if (ret) { 4854 /* Houston we have a problem, no space */ 4855 sctp_m_freem(m); 4856 return; 4857 } 4858 p_len += padval; 4859 } 4860 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n"); 4861 ret = sctp_lowlevel_chunk_output(inp, stcb, net, 4862 (struct sockaddr *)&net->ro._l_addr, 4863 m, 0, NULL, 0, 0, 0, NULL, 0, 4864 inp->sctp_lport, stcb->rport, htonl(0), 4865 net->port, so_locked, NULL, NULL); 4866 SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret); 4867 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); 4868 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); 4869 } 4870 4871 struct mbuf * 4872 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt, 4873 int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly) 4874 { 4875 /* 4876 * Given a mbuf containing an INIT or INIT-ACK with the param_offset 4877 * being equal to the beginning of the params i.e. (iphlen + 4878 * sizeof(struct sctp_init_msg) parse through the parameters to the 4879 * end of the mbuf verifying that all parameters are known. 4880 * 4881 * For unknown parameters build and return a mbuf with 4882 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop 4883 * processing this chunk stop, and set *abort_processing to 1. 4884 * 4885 * By having param_offset be pre-set to where parameters begin it is 4886 * hoped that this routine may be reused in the future by new 4887 * features. 4888 */ 4889 struct sctp_paramhdr *phdr, params; 4890 4891 struct mbuf *mat, *op_err; 4892 char tempbuf[SCTP_PARAM_BUFFER_SIZE]; 4893 int at, limit, pad_needed; 4894 uint16_t ptype, plen, padded_size; 4895 int err_at; 4896 4897 *abort_processing = 0; 4898 mat = in_initpkt; 4899 err_at = 0; 4900 limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk); 4901 at = param_offset; 4902 op_err = NULL; 4903 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n"); 4904 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params)); 4905 while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) { 4906 ptype = ntohs(phdr->param_type); 4907 plen = ntohs(phdr->param_length); 4908 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) { 4909 /* wacked parameter */ 4910 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen); 4911 goto invalid_size; 4912 } 4913 limit -= SCTP_SIZE32(plen); 4914 /*- 4915 * All parameters for all chunks that we know/understand are 4916 * listed here. We process them other places and make 4917 * appropriate stop actions per the upper bits. However this 4918 * is the generic routine processor's can call to get back 4919 * an operr.. to either incorporate (init-ack) or send. 4920 */ 4921 padded_size = SCTP_SIZE32(plen); 4922 switch (ptype) { 4923 /* Param's with variable size */ 4924 case SCTP_HEARTBEAT_INFO: 4925 case SCTP_STATE_COOKIE: 4926 case SCTP_UNRECOG_PARAM: 4927 case SCTP_ERROR_CAUSE_IND: 4928 /* ok skip fwd */ 4929 at += padded_size; 4930 break; 4931 /* Param's with variable size within a range */ 4932 case SCTP_CHUNK_LIST: 4933 case SCTP_SUPPORTED_CHUNK_EXT: 4934 if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) { 4935 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen); 4936 goto invalid_size; 4937 } 4938 at += padded_size; 4939 break; 4940 case SCTP_SUPPORTED_ADDRTYPE: 4941 if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) { 4942 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen); 4943 goto invalid_size; 4944 } 4945 at += padded_size; 4946 break; 4947 case SCTP_RANDOM: 4948 if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) { 4949 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen); 4950 goto invalid_size; 4951 } 4952 at += padded_size; 4953 break; 4954 case SCTP_SET_PRIM_ADDR: 4955 case SCTP_DEL_IP_ADDRESS: 4956 case SCTP_ADD_IP_ADDRESS: 4957 if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) && 4958 (padded_size != sizeof(struct sctp_asconf_addr_param))) { 4959 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen); 4960 goto invalid_size; 4961 } 4962 at += padded_size; 4963 break; 4964 /* Param's with a fixed size */ 4965 case SCTP_IPV4_ADDRESS: 4966 if (padded_size != sizeof(struct sctp_ipv4addr_param)) { 4967 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen); 4968 goto invalid_size; 4969 } 4970 at += padded_size; 4971 break; 4972 case SCTP_IPV6_ADDRESS: 4973 if (padded_size != sizeof(struct sctp_ipv6addr_param)) { 4974 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen); 4975 goto invalid_size; 4976 } 4977 at += padded_size; 4978 break; 4979 case SCTP_COOKIE_PRESERVE: 4980 if (padded_size != sizeof(struct sctp_cookie_perserve_param)) { 4981 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen); 4982 goto invalid_size; 4983 } 4984 at += padded_size; 4985 break; 4986 case SCTP_HAS_NAT_SUPPORT: 4987 *nat_friendly = 1; 4988 /* fall through */ 4989 case SCTP_PRSCTP_SUPPORTED: 4990 4991 if (padded_size != sizeof(struct sctp_paramhdr)) { 4992 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen); 4993 goto invalid_size; 4994 } 4995 at += padded_size; 4996 break; 4997 case SCTP_ECN_CAPABLE: 4998 if (padded_size != sizeof(struct sctp_ecn_supported_param)) { 4999 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen); 5000 goto invalid_size; 5001 } 5002 at += padded_size; 5003 break; 5004 case SCTP_ULP_ADAPTATION: 5005 if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) { 5006 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen); 5007 goto invalid_size; 5008 } 5009 at += padded_size; 5010 break; 5011 case SCTP_SUCCESS_REPORT: 5012 if (padded_size != sizeof(struct sctp_asconf_paramhdr)) { 5013 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen); 5014 goto invalid_size; 5015 } 5016 at += padded_size; 5017 break; 5018 case SCTP_HOSTNAME_ADDRESS: 5019 { 5020 /* We can NOT handle HOST NAME addresses!! */ 5021 int l_len; 5022 5023 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n"); 5024 *abort_processing = 1; 5025 if (op_err == NULL) { 5026 /* Ok need to try to get a mbuf */ 5027 #ifdef INET6 5028 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); 5029 #else 5030 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); 5031 #endif 5032 l_len += plen; 5033 l_len += sizeof(struct sctp_paramhdr); 5034 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA); 5035 if (op_err) { 5036 SCTP_BUF_LEN(op_err) = 0; 5037 /* 5038 * pre-reserve space for ip 5039 * and sctp header and 5040 * chunk hdr 5041 */ 5042 #ifdef INET6 5043 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 5044 #else 5045 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); 5046 #endif 5047 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 5048 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 5049 } 5050 } 5051 if (op_err) { 5052 /* If we have space */ 5053 struct sctp_paramhdr s; 5054 5055 if (err_at % 4) { 5056 uint32_t cpthis = 0; 5057 5058 pad_needed = 4 - (err_at % 4); 5059 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis); 5060 err_at += pad_needed; 5061 } 5062 s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR); 5063 s.param_length = htons(sizeof(s) + plen); 5064 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s); 5065 err_at += sizeof(s); 5066 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen)); 5067 if (phdr == NULL) { 5068 sctp_m_freem(op_err); 5069 /* 5070 * we are out of memory but 5071 * we still need to have a 5072 * look at what to do (the 5073 * system is in trouble 5074 * though). 5075 */ 5076 return (NULL); 5077 } 5078 m_copyback(op_err, err_at, plen, (caddr_t)phdr); 5079 err_at += plen; 5080 } 5081 return (op_err); 5082 break; 5083 } 5084 default: 5085 /* 5086 * we do not recognize the parameter figure out what 5087 * we do. 5088 */ 5089 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype); 5090 if ((ptype & 0x4000) == 0x4000) { 5091 /* Report bit is set?? */ 5092 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n"); 5093 if (op_err == NULL) { 5094 int l_len; 5095 5096 /* Ok need to try to get an mbuf */ 5097 #ifdef INET6 5098 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); 5099 #else 5100 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); 5101 #endif 5102 l_len += plen; 5103 l_len += sizeof(struct sctp_paramhdr); 5104 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA); 5105 if (op_err) { 5106 SCTP_BUF_LEN(op_err) = 0; 5107 #ifdef INET6 5108 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 5109 #else 5110 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); 5111 #endif 5112 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 5113 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 5114 } 5115 } 5116 if (op_err) { 5117 /* If we have space */ 5118 struct sctp_paramhdr s; 5119 5120 if (err_at % 4) { 5121 uint32_t cpthis = 0; 5122 5123 pad_needed = 4 - (err_at % 4); 5124 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis); 5125 err_at += pad_needed; 5126 } 5127 s.param_type = htons(SCTP_UNRECOG_PARAM); 5128 s.param_length = htons(sizeof(s) + plen); 5129 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s); 5130 err_at += sizeof(s); 5131 if (plen > sizeof(tempbuf)) { 5132 plen = sizeof(tempbuf); 5133 } 5134 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen)); 5135 if (phdr == NULL) { 5136 sctp_m_freem(op_err); 5137 /* 5138 * we are out of memory but 5139 * we still need to have a 5140 * look at what to do (the 5141 * system is in trouble 5142 * though). 5143 */ 5144 op_err = NULL; 5145 goto more_processing; 5146 } 5147 m_copyback(op_err, err_at, plen, (caddr_t)phdr); 5148 err_at += plen; 5149 } 5150 } 5151 more_processing: 5152 if ((ptype & 0x8000) == 0x0000) { 5153 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n"); 5154 return (op_err); 5155 } else { 5156 /* skip this chunk and continue processing */ 5157 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n"); 5158 at += SCTP_SIZE32(plen); 5159 } 5160 break; 5161 5162 } 5163 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params)); 5164 } 5165 return (op_err); 5166 invalid_size: 5167 SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n"); 5168 *abort_processing = 1; 5169 if ((op_err == NULL) && phdr) { 5170 int l_len; 5171 5172 #ifdef INET6 5173 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); 5174 #else 5175 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); 5176 #endif 5177 l_len += (2 * sizeof(struct sctp_paramhdr)); 5178 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA); 5179 if (op_err) { 5180 SCTP_BUF_LEN(op_err) = 0; 5181 #ifdef INET6 5182 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); 5183 #else 5184 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); 5185 #endif 5186 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); 5187 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 5188 } 5189 } 5190 if ((op_err) && phdr) { 5191 struct sctp_paramhdr s; 5192 5193 if (err_at % 4) { 5194 uint32_t cpthis = 0; 5195 5196 pad_needed = 4 - (err_at % 4); 5197 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis); 5198 err_at += pad_needed; 5199 } 5200 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION); 5201 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr)); 5202 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s); 5203 err_at += sizeof(s); 5204 /* Only copy back the p-hdr that caused the issue */ 5205 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr); 5206 } 5207 return (op_err); 5208 } 5209 5210 static int 5211 sctp_are_there_new_addresses(struct sctp_association *asoc, 5212 struct mbuf *in_initpkt, int iphlen, int offset) 5213 { 5214 /* 5215 * Given a INIT packet, look through the packet to verify that there 5216 * are NO new addresses. As we go through the parameters add reports 5217 * of any un-understood parameters that require an error. Also we 5218 * must return (1) to drop the packet if we see a un-understood 5219 * parameter that tells us to drop the chunk. 5220 */ 5221 struct sockaddr *sa_touse; 5222 struct sockaddr *sa; 5223 struct sctp_paramhdr *phdr, params; 5224 uint16_t ptype, plen; 5225 uint8_t fnd; 5226 struct sctp_nets *net; 5227 struct ip *iph; 5228 5229 #ifdef INET 5230 struct sockaddr_in sin4, *sa4; 5231 5232 #endif 5233 #ifdef INET6 5234 struct sockaddr_in6 sin6, *sa6; 5235 struct ip6_hdr *ip6h; 5236 5237 #endif 5238 5239 #ifdef INET 5240 memset(&sin4, 0, sizeof(sin4)); 5241 sin4.sin_family = AF_INET; 5242 sin4.sin_len = sizeof(sin4); 5243 #endif 5244 #ifdef INET6 5245 memset(&sin6, 0, sizeof(sin6)); 5246 sin6.sin6_family = AF_INET6; 5247 sin6.sin6_len = sizeof(sin6); 5248 #endif 5249 sa_touse = NULL; 5250 /* First what about the src address of the pkt ? */ 5251 iph = mtod(in_initpkt, struct ip *); 5252 switch (iph->ip_v) { 5253 #ifdef INET 5254 case IPVERSION: 5255 /* source addr is IPv4 */ 5256 sin4.sin_addr = iph->ip_src; 5257 sa_touse = (struct sockaddr *)&sin4; 5258 break; 5259 #endif 5260 #ifdef INET6 5261 case IPV6_VERSION >> 4: 5262 /* source addr is IPv6 */ 5263 ip6h = mtod(in_initpkt, struct ip6_hdr *); 5264 sin6.sin6_addr = ip6h->ip6_src; 5265 sa_touse = (struct sockaddr *)&sin6; 5266 break; 5267 #endif 5268 default: 5269 return (1); 5270 } 5271 5272 fnd = 0; 5273 TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 5274 sa = (struct sockaddr *)&net->ro._l_addr; 5275 if (sa->sa_family == sa_touse->sa_family) { 5276 #ifdef INET 5277 if (sa->sa_family == AF_INET) { 5278 sa4 = (struct sockaddr_in *)sa; 5279 if (sa4->sin_addr.s_addr == sin4.sin_addr.s_addr) { 5280 fnd = 1; 5281 break; 5282 } 5283 } 5284 #endif 5285 #ifdef INET6 5286 if (sa->sa_family == AF_INET6) { 5287 sa6 = (struct sockaddr_in6 *)sa; 5288 if (SCTP6_ARE_ADDR_EQUAL(sa6, &sin6)) { 5289 fnd = 1; 5290 break; 5291 } 5292 } 5293 #endif 5294 } 5295 } 5296 if (fnd == 0) { 5297 /* New address added! no need to look futher. */ 5298 return (1); 5299 } 5300 /* Ok so far lets munge through the rest of the packet */ 5301 offset += sizeof(struct sctp_init_chunk); 5302 phdr = sctp_get_next_param(in_initpkt, offset, ¶ms, sizeof(params)); 5303 while (phdr) { 5304 sa_touse = NULL; 5305 ptype = ntohs(phdr->param_type); 5306 plen = ntohs(phdr->param_length); 5307 switch (ptype) { 5308 #ifdef INET 5309 case SCTP_IPV4_ADDRESS: 5310 { 5311 struct sctp_ipv4addr_param *p4, p4_buf; 5312 5313 phdr = sctp_get_next_param(in_initpkt, offset, 5314 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf)); 5315 if (plen != sizeof(struct sctp_ipv4addr_param) || 5316 phdr == NULL) { 5317 return (1); 5318 } 5319 p4 = (struct sctp_ipv4addr_param *)phdr; 5320 sin4.sin_addr.s_addr = p4->addr; 5321 sa_touse = (struct sockaddr *)&sin4; 5322 } 5323 #endif 5324 #ifdef INET6 5325 case SCTP_IPV6_ADDRESS: 5326 { 5327 struct sctp_ipv6addr_param *p6, p6_buf; 5328 5329 phdr = sctp_get_next_param(in_initpkt, offset, 5330 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf)); 5331 if (plen != sizeof(struct sctp_ipv6addr_param) || 5332 phdr == NULL) { 5333 return (1); 5334 } 5335 p6 = (struct sctp_ipv6addr_param *)phdr; 5336 memcpy((caddr_t)&sin6.sin6_addr, p6->addr, 5337 sizeof(p6->addr)); 5338 sa_touse = (struct sockaddr *)&sin6; 5339 } 5340 #endif 5341 default: 5342 sa_touse = NULL; 5343 } 5344 if (sa_touse) { 5345 /* ok, sa_touse points to one to check */ 5346 fnd = 0; 5347 TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 5348 sa = (struct sockaddr *)&net->ro._l_addr; 5349 if (sa->sa_family != sa_touse->sa_family) { 5350 continue; 5351 } 5352 #ifdef INET 5353 if (sa->sa_family == AF_INET) { 5354 sa4 = (struct sockaddr_in *)sa; 5355 if (sa4->sin_addr.s_addr == 5356 sin4.sin_addr.s_addr) { 5357 fnd = 1; 5358 break; 5359 } 5360 } 5361 #endif 5362 #ifdef INET6 5363 if (sa->sa_family == AF_INET6) { 5364 sa6 = (struct sockaddr_in6 *)sa; 5365 if (SCTP6_ARE_ADDR_EQUAL( 5366 sa6, &sin6)) { 5367 fnd = 1; 5368 break; 5369 } 5370 } 5371 #endif 5372 } 5373 if (!fnd) { 5374 /* New addr added! no need to look further */ 5375 return (1); 5376 } 5377 } 5378 offset += SCTP_SIZE32(plen); 5379 phdr = sctp_get_next_param(in_initpkt, offset, ¶ms, sizeof(params)); 5380 } 5381 return (0); 5382 } 5383 5384 /* 5385 * Given a MBUF chain that was sent into us containing an INIT. Build a 5386 * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done 5387 * a pullup to include IPv6/4header, SCTP header and initial part of INIT 5388 * message (i.e. the struct sctp_init_msg). 5389 */ 5390 void 5391 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb, 5392 struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh, 5393 struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock) 5394 { 5395 struct sctp_association *asoc; 5396 struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last; 5397 struct sctp_init_ack_chunk *initack; 5398 struct sctp_adaptation_layer_indication *ali; 5399 struct sctp_ecn_supported_param *ecn; 5400 struct sctp_prsctp_supported_param *prsctp; 5401 struct sctp_supported_chunk_types_param *pr_supported; 5402 union sctp_sockstore store, store1, *over_addr; 5403 5404 #ifdef INET 5405 struct sockaddr_in *sin, *to_sin; 5406 5407 #endif 5408 #ifdef INET6 5409 struct sockaddr_in6 *sin6, *to_sin6; 5410 5411 #endif 5412 struct ip *iph; 5413 5414 #ifdef INET6 5415 struct ip6_hdr *ip6; 5416 5417 #endif 5418 struct sockaddr *to; 5419 struct sctp_state_cookie stc; 5420 struct sctp_nets *net = NULL; 5421 uint8_t *signature = NULL; 5422 int cnt_inits_to = 0; 5423 uint16_t his_limit, i_want; 5424 int abort_flag, padval; 5425 int num_ext; 5426 int p_len; 5427 int nat_friendly = 0; 5428 struct socket *so; 5429 5430 if (stcb) 5431 asoc = &stcb->asoc; 5432 else 5433 asoc = NULL; 5434 mp_last = NULL; 5435 if ((asoc != NULL) && 5436 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) && 5437 (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) { 5438 /* new addresses, out of here in non-cookie-wait states */ 5439 /* 5440 * Send a ABORT, we don't add the new address error clause 5441 * though we even set the T bit and copy in the 0 tag.. this 5442 * looks no different than if no listener was present. 5443 */ 5444 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port); 5445 return; 5446 } 5447 abort_flag = 0; 5448 op_err = sctp_arethere_unrecognized_parameters(init_pkt, 5449 (offset + sizeof(struct sctp_init_chunk)), 5450 &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly); 5451 if (abort_flag) { 5452 do_a_abort: 5453 sctp_send_abort(init_pkt, iphlen, sh, 5454 init_chk->init.initiate_tag, op_err, vrf_id, port); 5455 return; 5456 } 5457 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 5458 if (m == NULL) { 5459 /* No memory, INIT timer will re-attempt. */ 5460 if (op_err) 5461 sctp_m_freem(op_err); 5462 return; 5463 } 5464 SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk); 5465 5466 /* the time I built cookie */ 5467 (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered); 5468 5469 /* populate any tie tags */ 5470 if (asoc != NULL) { 5471 /* unlock before tag selections */ 5472 stc.tie_tag_my_vtag = asoc->my_vtag_nonce; 5473 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce; 5474 stc.cookie_life = asoc->cookie_life; 5475 net = asoc->primary_destination; 5476 } else { 5477 stc.tie_tag_my_vtag = 0; 5478 stc.tie_tag_peer_vtag = 0; 5479 /* life I will award this cookie */ 5480 stc.cookie_life = inp->sctp_ep.def_cookie_life; 5481 } 5482 5483 /* copy in the ports for later check */ 5484 stc.myport = sh->dest_port; 5485 stc.peerport = sh->src_port; 5486 5487 /* 5488 * If we wanted to honor cookie life extentions, we would add to 5489 * stc.cookie_life. For now we should NOT honor any extension 5490 */ 5491 stc.site_scope = stc.local_scope = stc.loopback_scope = 0; 5492 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 5493 struct inpcb *in_inp; 5494 5495 /* Its a V6 socket */ 5496 in_inp = (struct inpcb *)inp; 5497 stc.ipv6_addr_legal = 1; 5498 /* Now look at the binding flag to see if V4 will be legal */ 5499 if (SCTP_IPV6_V6ONLY(in_inp) == 0) { 5500 stc.ipv4_addr_legal = 1; 5501 } else { 5502 /* V4 addresses are NOT legal on the association */ 5503 stc.ipv4_addr_legal = 0; 5504 } 5505 } else { 5506 /* Its a V4 socket, no - V6 */ 5507 stc.ipv4_addr_legal = 1; 5508 stc.ipv6_addr_legal = 0; 5509 } 5510 5511 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE 5512 stc.ipv4_scope = 1; 5513 #else 5514 stc.ipv4_scope = 0; 5515 #endif 5516 /* now for scope setup */ 5517 memset((caddr_t)&store, 0, sizeof(store)); 5518 memset((caddr_t)&store1, 0, sizeof(store1)); 5519 #ifdef INET 5520 sin = &store.sin; 5521 to_sin = &store1.sin; 5522 #endif 5523 #ifdef INET6 5524 sin6 = &store.sin6; 5525 to_sin6 = &store1.sin6; 5526 #endif 5527 iph = mtod(init_pkt, struct ip *); 5528 /* establish the to_addr's */ 5529 switch (iph->ip_v) { 5530 #ifdef INET 5531 case IPVERSION: 5532 to_sin->sin_port = sh->dest_port; 5533 to_sin->sin_family = AF_INET; 5534 to_sin->sin_len = sizeof(struct sockaddr_in); 5535 to_sin->sin_addr = iph->ip_dst; 5536 break; 5537 #endif 5538 #ifdef INET6 5539 case IPV6_VERSION >> 4: 5540 ip6 = mtod(init_pkt, struct ip6_hdr *); 5541 to_sin6->sin6_addr = ip6->ip6_dst; 5542 to_sin6->sin6_scope_id = 0; 5543 to_sin6->sin6_port = sh->dest_port; 5544 to_sin6->sin6_family = AF_INET6; 5545 to_sin6->sin6_len = sizeof(struct sockaddr_in6); 5546 break; 5547 #endif 5548 default: 5549 goto do_a_abort; 5550 break; 5551 }; 5552 5553 if (net == NULL) { 5554 to = (struct sockaddr *)&store; 5555 switch (iph->ip_v) { 5556 #ifdef INET 5557 case IPVERSION: 5558 { 5559 sin->sin_family = AF_INET; 5560 sin->sin_len = sizeof(struct sockaddr_in); 5561 sin->sin_port = sh->src_port; 5562 sin->sin_addr = iph->ip_src; 5563 /* lookup address */ 5564 stc.address[0] = sin->sin_addr.s_addr; 5565 stc.address[1] = 0; 5566 stc.address[2] = 0; 5567 stc.address[3] = 0; 5568 stc.addr_type = SCTP_IPV4_ADDRESS; 5569 /* local from address */ 5570 stc.laddress[0] = to_sin->sin_addr.s_addr; 5571 stc.laddress[1] = 0; 5572 stc.laddress[2] = 0; 5573 stc.laddress[3] = 0; 5574 stc.laddr_type = SCTP_IPV4_ADDRESS; 5575 /* scope_id is only for v6 */ 5576 stc.scope_id = 0; 5577 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE 5578 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) { 5579 stc.ipv4_scope = 1; 5580 } 5581 #else 5582 stc.ipv4_scope = 1; 5583 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */ 5584 /* Must use the address in this case */ 5585 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) { 5586 stc.loopback_scope = 1; 5587 stc.ipv4_scope = 1; 5588 stc.site_scope = 1; 5589 stc.local_scope = 0; 5590 } 5591 break; 5592 } 5593 #endif 5594 #ifdef INET6 5595 case IPV6_VERSION >> 4: 5596 { 5597 ip6 = mtod(init_pkt, struct ip6_hdr *); 5598 sin6->sin6_family = AF_INET6; 5599 sin6->sin6_len = sizeof(struct sockaddr_in6); 5600 sin6->sin6_port = sh->src_port; 5601 sin6->sin6_addr = ip6->ip6_src; 5602 /* lookup address */ 5603 memcpy(&stc.address, &sin6->sin6_addr, 5604 sizeof(struct in6_addr)); 5605 sin6->sin6_scope_id = 0; 5606 stc.addr_type = SCTP_IPV6_ADDRESS; 5607 stc.scope_id = 0; 5608 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) { 5609 /* 5610 * FIX ME: does this have scope from 5611 * rcvif? 5612 */ 5613 (void)sa6_recoverscope(sin6); 5614 stc.scope_id = sin6->sin6_scope_id; 5615 sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)); 5616 stc.loopback_scope = 1; 5617 stc.local_scope = 0; 5618 stc.site_scope = 1; 5619 stc.ipv4_scope = 1; 5620 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 5621 /* 5622 * If the new destination is a 5623 * LINK_LOCAL we must have common 5624 * both site and local scope. Don't 5625 * set local scope though since we 5626 * must depend on the source to be 5627 * added implicitly. We cannot 5628 * assure just because we share one 5629 * link that all links are common. 5630 */ 5631 stc.local_scope = 0; 5632 stc.site_scope = 1; 5633 stc.ipv4_scope = 1; 5634 /* 5635 * we start counting for the private 5636 * address stuff at 1. since the 5637 * link local we source from won't 5638 * show up in our scoped count. 5639 */ 5640 cnt_inits_to = 1; 5641 /* 5642 * pull out the scope_id from 5643 * incoming pkt 5644 */ 5645 /* 5646 * FIX ME: does this have scope from 5647 * rcvif? 5648 */ 5649 (void)sa6_recoverscope(sin6); 5650 stc.scope_id = sin6->sin6_scope_id; 5651 sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)); 5652 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) { 5653 /* 5654 * If the new destination is 5655 * SITE_LOCAL then we must have site 5656 * scope in common. 5657 */ 5658 stc.site_scope = 1; 5659 } 5660 memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr)); 5661 stc.laddr_type = SCTP_IPV6_ADDRESS; 5662 break; 5663 } 5664 #endif 5665 default: 5666 /* TSNH */ 5667 goto do_a_abort; 5668 break; 5669 } 5670 } else { 5671 /* set the scope per the existing tcb */ 5672 5673 #ifdef INET6 5674 struct sctp_nets *lnet; 5675 5676 #endif 5677 5678 stc.loopback_scope = asoc->loopback_scope; 5679 stc.ipv4_scope = asoc->ipv4_local_scope; 5680 stc.site_scope = asoc->site_scope; 5681 stc.local_scope = asoc->local_scope; 5682 #ifdef INET6 5683 /* Why do we not consider IPv4 LL addresses? */ 5684 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { 5685 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) { 5686 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) { 5687 /* 5688 * if we have a LL address, start 5689 * counting at 1. 5690 */ 5691 cnt_inits_to = 1; 5692 } 5693 } 5694 } 5695 #endif 5696 /* use the net pointer */ 5697 to = (struct sockaddr *)&net->ro._l_addr; 5698 switch (to->sa_family) { 5699 #ifdef INET 5700 case AF_INET: 5701 sin = (struct sockaddr_in *)to; 5702 stc.address[0] = sin->sin_addr.s_addr; 5703 stc.address[1] = 0; 5704 stc.address[2] = 0; 5705 stc.address[3] = 0; 5706 stc.addr_type = SCTP_IPV4_ADDRESS; 5707 if (net->src_addr_selected == 0) { 5708 /* 5709 * strange case here, the INIT should have 5710 * did the selection. 5711 */ 5712 net->ro._s_addr = sctp_source_address_selection(inp, 5713 stcb, (sctp_route_t *) & net->ro, 5714 net, 0, vrf_id); 5715 if (net->ro._s_addr == NULL) 5716 return; 5717 5718 net->src_addr_selected = 1; 5719 5720 } 5721 stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr; 5722 stc.laddress[1] = 0; 5723 stc.laddress[2] = 0; 5724 stc.laddress[3] = 0; 5725 stc.laddr_type = SCTP_IPV4_ADDRESS; 5726 /* scope_id is only for v6 */ 5727 stc.scope_id = 0; 5728 break; 5729 #endif 5730 #ifdef INET6 5731 case AF_INET6: 5732 sin6 = (struct sockaddr_in6 *)to; 5733 memcpy(&stc.address, &sin6->sin6_addr, 5734 sizeof(struct in6_addr)); 5735 stc.addr_type = SCTP_IPV6_ADDRESS; 5736 stc.scope_id = sin6->sin6_scope_id; 5737 if (net->src_addr_selected == 0) { 5738 /* 5739 * strange case here, the INIT should have 5740 * did the selection. 5741 */ 5742 net->ro._s_addr = sctp_source_address_selection(inp, 5743 stcb, (sctp_route_t *) & net->ro, 5744 net, 0, vrf_id); 5745 if (net->ro._s_addr == NULL) 5746 return; 5747 5748 net->src_addr_selected = 1; 5749 } 5750 memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr, 5751 sizeof(struct in6_addr)); 5752 stc.laddr_type = SCTP_IPV6_ADDRESS; 5753 break; 5754 #endif 5755 } 5756 } 5757 /* Now lets put the SCTP header in place */ 5758 initack = mtod(m, struct sctp_init_ack_chunk *); 5759 /* Save it off for quick ref */ 5760 stc.peers_vtag = init_chk->init.initiate_tag; 5761 /* who are we */ 5762 memcpy(stc.identification, SCTP_VERSION_STRING, 5763 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification))); 5764 memset(stc.reserved, 0, SCTP_RESERVE_SPACE); 5765 /* now the chunk header */ 5766 initack->ch.chunk_type = SCTP_INITIATION_ACK; 5767 initack->ch.chunk_flags = 0; 5768 /* fill in later from mbuf we build */ 5769 initack->ch.chunk_length = 0; 5770 /* place in my tag */ 5771 if ((asoc != NULL) && 5772 ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 5773 (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) || 5774 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) { 5775 /* re-use the v-tags and init-seq here */ 5776 initack->init.initiate_tag = htonl(asoc->my_vtag); 5777 initack->init.initial_tsn = htonl(asoc->init_seq_number); 5778 } else { 5779 uint32_t vtag, itsn; 5780 5781 if (hold_inp_lock) { 5782 SCTP_INP_INCR_REF(inp); 5783 SCTP_INP_RUNLOCK(inp); 5784 } 5785 if (asoc) { 5786 atomic_add_int(&asoc->refcnt, 1); 5787 SCTP_TCB_UNLOCK(stcb); 5788 new_tag: 5789 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1); 5790 if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) { 5791 /* 5792 * Got a duplicate vtag on some guy behind a 5793 * nat make sure we don't use it. 5794 */ 5795 goto new_tag; 5796 } 5797 initack->init.initiate_tag = htonl(vtag); 5798 /* get a TSN to use too */ 5799 itsn = sctp_select_initial_TSN(&inp->sctp_ep); 5800 initack->init.initial_tsn = htonl(itsn); 5801 SCTP_TCB_LOCK(stcb); 5802 atomic_add_int(&asoc->refcnt, -1); 5803 } else { 5804 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1); 5805 initack->init.initiate_tag = htonl(vtag); 5806 /* get a TSN to use too */ 5807 initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep)); 5808 } 5809 if (hold_inp_lock) { 5810 SCTP_INP_RLOCK(inp); 5811 SCTP_INP_DECR_REF(inp); 5812 } 5813 } 5814 /* save away my tag to */ 5815 stc.my_vtag = initack->init.initiate_tag; 5816 5817 /* set up some of the credits. */ 5818 so = inp->sctp_socket; 5819 if (so == NULL) { 5820 /* memory problem */ 5821 sctp_m_freem(m); 5822 return; 5823 } else { 5824 initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND)); 5825 } 5826 /* set what I want */ 5827 his_limit = ntohs(init_chk->init.num_inbound_streams); 5828 /* choose what I want */ 5829 if (asoc != NULL) { 5830 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) { 5831 i_want = asoc->streamoutcnt; 5832 } else { 5833 i_want = inp->sctp_ep.pre_open_stream_count; 5834 } 5835 } else { 5836 i_want = inp->sctp_ep.pre_open_stream_count; 5837 } 5838 if (his_limit < i_want) { 5839 /* I Want more :< */ 5840 initack->init.num_outbound_streams = init_chk->init.num_inbound_streams; 5841 } else { 5842 /* I can have what I want :> */ 5843 initack->init.num_outbound_streams = htons(i_want); 5844 } 5845 /* tell him his limt. */ 5846 initack->init.num_inbound_streams = 5847 htons(inp->sctp_ep.max_open_streams_intome); 5848 5849 /* adaptation layer indication parameter */ 5850 ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack)); 5851 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION); 5852 ali->ph.param_length = htons(sizeof(*ali)); 5853 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator); 5854 SCTP_BUF_LEN(m) += sizeof(*ali); 5855 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali)); 5856 5857 /* ECN parameter */ 5858 if (((asoc != NULL) && (asoc->ecn_allowed == 1)) || 5859 (inp->sctp_ecn_enable == 1)) { 5860 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE); 5861 ecn->ph.param_length = htons(sizeof(*ecn)); 5862 SCTP_BUF_LEN(m) += sizeof(*ecn); 5863 5864 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn + 5865 sizeof(*ecn)); 5866 } else { 5867 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn); 5868 } 5869 /* And now tell the peer we do pr-sctp */ 5870 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED); 5871 prsctp->ph.param_length = htons(sizeof(*prsctp)); 5872 SCTP_BUF_LEN(m) += sizeof(*prsctp); 5873 if (nat_friendly) { 5874 /* Add NAT friendly parameter */ 5875 struct sctp_paramhdr *ph; 5876 5877 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m)); 5878 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT); 5879 ph->param_length = htons(sizeof(struct sctp_paramhdr)); 5880 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr); 5881 } 5882 /* And now tell the peer we do all the extensions */ 5883 pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m)); 5884 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT); 5885 num_ext = 0; 5886 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF; 5887 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK; 5888 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN; 5889 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED; 5890 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET; 5891 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) 5892 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION; 5893 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) 5894 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK; 5895 p_len = sizeof(*pr_supported) + num_ext; 5896 pr_supported->ph.param_length = htons(p_len); 5897 bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len); 5898 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len); 5899 5900 /* add authentication parameters */ 5901 if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) { 5902 struct sctp_auth_random *randp; 5903 struct sctp_auth_hmac_algo *hmacs; 5904 struct sctp_auth_chunk_list *chunks; 5905 uint16_t random_len; 5906 5907 /* generate and add RANDOM parameter */ 5908 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT; 5909 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m)); 5910 randp->ph.param_type = htons(SCTP_RANDOM); 5911 p_len = sizeof(*randp) + random_len; 5912 randp->ph.param_length = htons(p_len); 5913 SCTP_READ_RANDOM(randp->random_data, random_len); 5914 /* zero out any padding required */ 5915 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len); 5916 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len); 5917 5918 /* add HMAC_ALGO parameter */ 5919 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m)); 5920 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs, 5921 (uint8_t *) hmacs->hmac_ids); 5922 if (p_len > 0) { 5923 p_len += sizeof(*hmacs); 5924 hmacs->ph.param_type = htons(SCTP_HMAC_LIST); 5925 hmacs->ph.param_length = htons(p_len); 5926 /* zero out any padding required */ 5927 bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len); 5928 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len); 5929 } 5930 /* add CHUNKS parameter */ 5931 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m)); 5932 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks, 5933 chunks->chunk_types); 5934 if (p_len > 0) { 5935 p_len += sizeof(*chunks); 5936 chunks->ph.param_type = htons(SCTP_CHUNK_LIST); 5937 chunks->ph.param_length = htons(p_len); 5938 /* zero out any padding required */ 5939 bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len); 5940 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len); 5941 } 5942 } 5943 m_at = m; 5944 /* now the addresses */ 5945 { 5946 struct sctp_scoping scp; 5947 5948 /* 5949 * To optimize this we could put the scoping stuff into a 5950 * structure and remove the individual uint8's from the stc 5951 * structure. Then we could just sifa in the address within 5952 * the stc.. but for now this is a quick hack to get the 5953 * address stuff teased apart. 5954 */ 5955 scp.ipv4_addr_legal = stc.ipv4_addr_legal; 5956 scp.ipv6_addr_legal = stc.ipv6_addr_legal; 5957 scp.loopback_scope = stc.loopback_scope; 5958 scp.ipv4_local_scope = stc.ipv4_scope; 5959 scp.local_scope = stc.local_scope; 5960 scp.site_scope = stc.site_scope; 5961 m_at = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_at, cnt_inits_to); 5962 } 5963 5964 /* tack on the operational error if present */ 5965 if (op_err) { 5966 struct mbuf *ol; 5967 int llen; 5968 5969 llen = 0; 5970 ol = op_err; 5971 while (ol) { 5972 llen += SCTP_BUF_LEN(ol); 5973 ol = SCTP_BUF_NEXT(ol); 5974 } 5975 if (llen % 4) { 5976 /* must add a pad to the param */ 5977 uint32_t cpthis = 0; 5978 int padlen; 5979 5980 padlen = 4 - (llen % 4); 5981 m_copyback(op_err, llen, padlen, (caddr_t)&cpthis); 5982 } 5983 while (SCTP_BUF_NEXT(m_at) != NULL) { 5984 m_at = SCTP_BUF_NEXT(m_at); 5985 } 5986 SCTP_BUF_NEXT(m_at) = op_err; 5987 while (SCTP_BUF_NEXT(m_at) != NULL) { 5988 m_at = SCTP_BUF_NEXT(m_at); 5989 } 5990 } 5991 /* pre-calulate the size and update pkt header and chunk header */ 5992 p_len = 0; 5993 for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) { 5994 p_len += SCTP_BUF_LEN(m_tmp); 5995 if (SCTP_BUF_NEXT(m_tmp) == NULL) { 5996 /* m_tmp should now point to last one */ 5997 break; 5998 } 5999 } 6000 6001 /* Now we must build a cookie */ 6002 m_cookie = sctp_add_cookie(inp, init_pkt, offset, m, 0, &stc, &signature); 6003 if (m_cookie == NULL) { 6004 /* memory problem */ 6005 sctp_m_freem(m); 6006 return; 6007 } 6008 /* Now append the cookie to the end and update the space/size */ 6009 SCTP_BUF_NEXT(m_tmp) = m_cookie; 6010 6011 for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) { 6012 p_len += SCTP_BUF_LEN(m_tmp); 6013 if (SCTP_BUF_NEXT(m_tmp) == NULL) { 6014 /* m_tmp should now point to last one */ 6015 mp_last = m_tmp; 6016 break; 6017 } 6018 } 6019 /* 6020 * Place in the size, but we don't include the last pad (if any) in 6021 * the INIT-ACK. 6022 */ 6023 initack->ch.chunk_length = htons(p_len); 6024 6025 /* 6026 * Time to sign the cookie, we don't sign over the cookie signature 6027 * though thus we set trailer. 6028 */ 6029 (void)sctp_hmac_m(SCTP_HMAC, 6030 (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)], 6031 SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr), 6032 (uint8_t *) signature, SCTP_SIGNATURE_SIZE); 6033 /* 6034 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return 6035 * here since the timer will drive a retranmission. 6036 */ 6037 padval = p_len % 4; 6038 if ((padval) && (mp_last)) { 6039 /* see my previous comments on mp_last */ 6040 int ret; 6041 6042 ret = sctp_add_pad_tombuf(mp_last, (4 - padval)); 6043 if (ret) { 6044 /* Houston we have a problem, no space */ 6045 sctp_m_freem(m); 6046 return; 6047 } 6048 p_len += padval; 6049 } 6050 if (stc.loopback_scope) { 6051 over_addr = &store1; 6052 } else { 6053 over_addr = NULL; 6054 } 6055 6056 (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0, 6057 0, NULL, 0, 6058 inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag, 6059 port, SCTP_SO_NOT_LOCKED, over_addr, init_pkt); 6060 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); 6061 } 6062 6063 6064 static void 6065 sctp_prune_prsctp(struct sctp_tcb *stcb, 6066 struct sctp_association *asoc, 6067 struct sctp_sndrcvinfo *srcv, 6068 int dataout) 6069 { 6070 int freed_spc = 0; 6071 struct sctp_tmit_chunk *chk, *nchk; 6072 6073 SCTP_TCB_LOCK_ASSERT(stcb); 6074 if ((asoc->peer_supports_prsctp) && 6075 (asoc->sent_queue_cnt_removeable > 0)) { 6076 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) { 6077 /* 6078 * Look for chunks marked with the PR_SCTP flag AND 6079 * the buffer space flag. If the one being sent is 6080 * equal or greater priority then purge the old one 6081 * and free some space. 6082 */ 6083 if (PR_SCTP_BUF_ENABLED(chk->flags)) { 6084 /* 6085 * This one is PR-SCTP AND buffer space 6086 * limited type 6087 */ 6088 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) { 6089 /* 6090 * Lower numbers equates to higher 6091 * priority so if the one we are 6092 * looking at has a larger or equal 6093 * priority we want to drop the data 6094 * and NOT retransmit it. 6095 */ 6096 if (chk->data) { 6097 /* 6098 * We release the book_size 6099 * if the mbuf is here 6100 */ 6101 int ret_spc; 6102 int cause; 6103 6104 if (chk->sent > SCTP_DATAGRAM_UNSENT) 6105 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT; 6106 else 6107 cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT; 6108 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk, 6109 cause, 6110 SCTP_SO_LOCKED); 6111 freed_spc += ret_spc; 6112 if (freed_spc >= dataout) { 6113 return; 6114 } 6115 } /* if chunk was present */ 6116 } /* if of sufficent priority */ 6117 } /* if chunk has enabled */ 6118 } /* tailqforeach */ 6119 6120 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { 6121 /* Here we must move to the sent queue and mark */ 6122 if (PR_SCTP_BUF_ENABLED(chk->flags)) { 6123 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) { 6124 if (chk->data) { 6125 /* 6126 * We release the book_size 6127 * if the mbuf is here 6128 */ 6129 int ret_spc; 6130 6131 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk, 6132 SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT, 6133 SCTP_SO_LOCKED); 6134 6135 freed_spc += ret_spc; 6136 if (freed_spc >= dataout) { 6137 return; 6138 } 6139 } /* end if chk->data */ 6140 } /* end if right class */ 6141 } /* end if chk pr-sctp */ 6142 } /* tailqforeachsafe (chk) */ 6143 } /* if enabled in asoc */ 6144 } 6145 6146 int 6147 sctp_get_frag_point(struct sctp_tcb *stcb, 6148 struct sctp_association *asoc) 6149 { 6150 int siz, ovh; 6151 6152 /* 6153 * For endpoints that have both v6 and v4 addresses we must reserve 6154 * room for the ipv6 header, for those that are only dealing with V4 6155 * we use a larger frag point. 6156 */ 6157 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 6158 ovh = SCTP_MED_OVERHEAD; 6159 } else { 6160 ovh = SCTP_MED_V4_OVERHEAD; 6161 } 6162 6163 if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu) 6164 siz = asoc->smallest_mtu - ovh; 6165 else 6166 siz = (stcb->asoc.sctp_frag_point - ovh); 6167 /* 6168 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) { 6169 */ 6170 /* A data chunk MUST fit in a cluster */ 6171 /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */ 6172 /* } */ 6173 6174 /* adjust for an AUTH chunk if DATA requires auth */ 6175 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) 6176 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); 6177 6178 if (siz % 4) { 6179 /* make it an even word boundary please */ 6180 siz -= (siz % 4); 6181 } 6182 return (siz); 6183 } 6184 6185 static void 6186 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp) 6187 { 6188 sp->pr_sctp_on = 0; 6189 /* 6190 * We assume that the user wants PR_SCTP_TTL if the user provides a 6191 * positive lifetime but does not specify any PR_SCTP policy. This 6192 * is a BAD assumption and causes problems at least with the 6193 * U-Vancovers MPI folks. I will change this to be no policy means 6194 * NO PR-SCTP. 6195 */ 6196 if (PR_SCTP_ENABLED(sp->sinfo_flags)) { 6197 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags); 6198 sp->pr_sctp_on = 1; 6199 } else { 6200 return; 6201 } 6202 switch (PR_SCTP_POLICY(sp->sinfo_flags)) { 6203 case CHUNK_FLAGS_PR_SCTP_BUF: 6204 /* 6205 * Time to live is a priority stored in tv_sec when doing 6206 * the buffer drop thing. 6207 */ 6208 sp->ts.tv_sec = sp->timetolive; 6209 sp->ts.tv_usec = 0; 6210 break; 6211 case CHUNK_FLAGS_PR_SCTP_TTL: 6212 { 6213 struct timeval tv; 6214 6215 (void)SCTP_GETTIME_TIMEVAL(&sp->ts); 6216 tv.tv_sec = sp->timetolive / 1000; 6217 tv.tv_usec = (sp->timetolive * 1000) % 1000000; 6218 /* 6219 * TODO sctp_constants.h needs alternative time 6220 * macros when _KERNEL is undefined. 6221 */ 6222 timevaladd(&sp->ts, &tv); 6223 } 6224 break; 6225 case CHUNK_FLAGS_PR_SCTP_RTX: 6226 /* 6227 * Time to live is a the number or retransmissions stored in 6228 * tv_sec. 6229 */ 6230 sp->ts.tv_sec = sp->timetolive; 6231 sp->ts.tv_usec = 0; 6232 break; 6233 default: 6234 SCTPDBG(SCTP_DEBUG_USRREQ1, 6235 "Unknown PR_SCTP policy %u.\n", 6236 PR_SCTP_POLICY(sp->sinfo_flags)); 6237 break; 6238 } 6239 } 6240 6241 static int 6242 sctp_msg_append(struct sctp_tcb *stcb, 6243 struct sctp_nets *net, 6244 struct mbuf *m, 6245 struct sctp_sndrcvinfo *srcv, int hold_stcb_lock) 6246 { 6247 int error = 0, holds_lock; 6248 struct mbuf *at; 6249 struct sctp_stream_queue_pending *sp = NULL; 6250 struct sctp_stream_out *strm; 6251 6252 /* 6253 * Given an mbuf chain, put it into the association send queue and 6254 * place it on the wheel 6255 */ 6256 holds_lock = hold_stcb_lock; 6257 if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) { 6258 /* Invalid stream number */ 6259 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 6260 error = EINVAL; 6261 goto out_now; 6262 } 6263 if ((stcb->asoc.stream_locked) && 6264 (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) { 6265 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 6266 error = EINVAL; 6267 goto out_now; 6268 } 6269 strm = &stcb->asoc.strmout[srcv->sinfo_stream]; 6270 /* Now can we send this? */ 6271 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) || 6272 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) || 6273 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) || 6274 (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) { 6275 /* got data while shutting down */ 6276 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); 6277 error = ECONNRESET; 6278 goto out_now; 6279 } 6280 sctp_alloc_a_strmoq(stcb, sp); 6281 if (sp == NULL) { 6282 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 6283 error = ENOMEM; 6284 goto out_now; 6285 } 6286 sp->sinfo_flags = srcv->sinfo_flags; 6287 sp->timetolive = srcv->sinfo_timetolive; 6288 sp->ppid = srcv->sinfo_ppid; 6289 sp->context = srcv->sinfo_context; 6290 sp->strseq = 0; 6291 if (sp->sinfo_flags & SCTP_ADDR_OVER) { 6292 sp->net = net; 6293 atomic_add_int(&sp->net->ref_count, 1); 6294 } else { 6295 sp->net = NULL; 6296 } 6297 (void)SCTP_GETTIME_TIMEVAL(&sp->ts); 6298 sp->stream = srcv->sinfo_stream; 6299 sp->msg_is_complete = 1; 6300 sp->sender_all_done = 1; 6301 sp->some_taken = 0; 6302 sp->data = m; 6303 sp->tail_mbuf = NULL; 6304 sctp_set_prsctp_policy(sp); 6305 /* 6306 * We could in theory (for sendall) sifa the length in, but we would 6307 * still have to hunt through the chain since we need to setup the 6308 * tail_mbuf 6309 */ 6310 sp->length = 0; 6311 for (at = m; at; at = SCTP_BUF_NEXT(at)) { 6312 if (SCTP_BUF_NEXT(at) == NULL) 6313 sp->tail_mbuf = at; 6314 sp->length += SCTP_BUF_LEN(at); 6315 } 6316 if (srcv->sinfo_keynumber_valid) { 6317 sp->auth_keyid = srcv->sinfo_keynumber; 6318 } else { 6319 sp->auth_keyid = stcb->asoc.authinfo.active_keyid; 6320 } 6321 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) { 6322 sctp_auth_key_acquire(stcb, sp->auth_keyid); 6323 sp->holds_key_ref = 1; 6324 } 6325 SCTP_TCB_SEND_LOCK(stcb); 6326 sctp_snd_sb_alloc(stcb, sp->length); 6327 atomic_add_int(&stcb->asoc.stream_queue_cnt, 1); 6328 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next); 6329 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) { 6330 sp->strseq = strm->next_sequence_sent; 6331 strm->next_sequence_sent++; 6332 } 6333 stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp, 1); 6334 m = NULL; 6335 SCTP_TCB_SEND_UNLOCK(stcb); 6336 out_now: 6337 if (m) { 6338 sctp_m_freem(m); 6339 } 6340 return (error); 6341 } 6342 6343 6344 static struct mbuf * 6345 sctp_copy_mbufchain(struct mbuf *clonechain, 6346 struct mbuf *outchain, 6347 struct mbuf **endofchain, 6348 int can_take_mbuf, 6349 int sizeofcpy, 6350 uint8_t copy_by_ref) 6351 { 6352 struct mbuf *m; 6353 struct mbuf *appendchain; 6354 caddr_t cp; 6355 int len; 6356 6357 if (endofchain == NULL) { 6358 /* error */ 6359 error_out: 6360 if (outchain) 6361 sctp_m_freem(outchain); 6362 return (NULL); 6363 } 6364 if (can_take_mbuf) { 6365 appendchain = clonechain; 6366 } else { 6367 if (!copy_by_ref && 6368 (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN))) 6369 ) { 6370 /* Its not in a cluster */ 6371 if (*endofchain == NULL) { 6372 /* lets get a mbuf cluster */ 6373 if (outchain == NULL) { 6374 /* This is the general case */ 6375 new_mbuf: 6376 outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER); 6377 if (outchain == NULL) { 6378 goto error_out; 6379 } 6380 SCTP_BUF_LEN(outchain) = 0; 6381 *endofchain = outchain; 6382 /* get the prepend space */ 6383 SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4)); 6384 } else { 6385 /* 6386 * We really should not get a NULL 6387 * in endofchain 6388 */ 6389 /* find end */ 6390 m = outchain; 6391 while (m) { 6392 if (SCTP_BUF_NEXT(m) == NULL) { 6393 *endofchain = m; 6394 break; 6395 } 6396 m = SCTP_BUF_NEXT(m); 6397 } 6398 /* sanity */ 6399 if (*endofchain == NULL) { 6400 /* 6401 * huh, TSNH XXX maybe we 6402 * should panic 6403 */ 6404 sctp_m_freem(outchain); 6405 goto new_mbuf; 6406 } 6407 } 6408 /* get the new end of length */ 6409 len = M_TRAILINGSPACE(*endofchain); 6410 } else { 6411 /* how much is left at the end? */ 6412 len = M_TRAILINGSPACE(*endofchain); 6413 } 6414 /* Find the end of the data, for appending */ 6415 cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain))); 6416 6417 /* Now lets copy it out */ 6418 if (len >= sizeofcpy) { 6419 /* It all fits, copy it in */ 6420 m_copydata(clonechain, 0, sizeofcpy, cp); 6421 SCTP_BUF_LEN((*endofchain)) += sizeofcpy; 6422 } else { 6423 /* fill up the end of the chain */ 6424 if (len > 0) { 6425 m_copydata(clonechain, 0, len, cp); 6426 SCTP_BUF_LEN((*endofchain)) += len; 6427 /* now we need another one */ 6428 sizeofcpy -= len; 6429 } 6430 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER); 6431 if (m == NULL) { 6432 /* We failed */ 6433 goto error_out; 6434 } 6435 SCTP_BUF_NEXT((*endofchain)) = m; 6436 *endofchain = m; 6437 cp = mtod((*endofchain), caddr_t); 6438 m_copydata(clonechain, len, sizeofcpy, cp); 6439 SCTP_BUF_LEN((*endofchain)) += sizeofcpy; 6440 } 6441 return (outchain); 6442 } else { 6443 /* copy the old fashion way */ 6444 appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT); 6445 #ifdef SCTP_MBUF_LOGGING 6446 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 6447 struct mbuf *mat; 6448 6449 mat = appendchain; 6450 while (mat) { 6451 if (SCTP_BUF_IS_EXTENDED(mat)) { 6452 sctp_log_mb(mat, SCTP_MBUF_ICOPY); 6453 } 6454 mat = SCTP_BUF_NEXT(mat); 6455 } 6456 } 6457 #endif 6458 } 6459 } 6460 if (appendchain == NULL) { 6461 /* error */ 6462 if (outchain) 6463 sctp_m_freem(outchain); 6464 return (NULL); 6465 } 6466 if (outchain) { 6467 /* tack on to the end */ 6468 if (*endofchain != NULL) { 6469 SCTP_BUF_NEXT(((*endofchain))) = appendchain; 6470 } else { 6471 m = outchain; 6472 while (m) { 6473 if (SCTP_BUF_NEXT(m) == NULL) { 6474 SCTP_BUF_NEXT(m) = appendchain; 6475 break; 6476 } 6477 m = SCTP_BUF_NEXT(m); 6478 } 6479 } 6480 /* 6481 * save off the end and update the end-chain postion 6482 */ 6483 m = appendchain; 6484 while (m) { 6485 if (SCTP_BUF_NEXT(m) == NULL) { 6486 *endofchain = m; 6487 break; 6488 } 6489 m = SCTP_BUF_NEXT(m); 6490 } 6491 return (outchain); 6492 } else { 6493 /* save off the end and update the end-chain postion */ 6494 m = appendchain; 6495 while (m) { 6496 if (SCTP_BUF_NEXT(m) == NULL) { 6497 *endofchain = m; 6498 break; 6499 } 6500 m = SCTP_BUF_NEXT(m); 6501 } 6502 return (appendchain); 6503 } 6504 } 6505 6506 int 6507 sctp_med_chunk_output(struct sctp_inpcb *inp, 6508 struct sctp_tcb *stcb, 6509 struct sctp_association *asoc, 6510 int *num_out, 6511 int *reason_code, 6512 int control_only, int from_where, 6513 struct timeval *now, int *now_filled, int frag_point, int so_locked 6514 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 6515 SCTP_UNUSED 6516 #endif 6517 ); 6518 6519 static void 6520 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr, 6521 uint32_t val) 6522 { 6523 struct sctp_copy_all *ca; 6524 struct mbuf *m; 6525 int ret = 0; 6526 int added_control = 0; 6527 int un_sent, do_chunk_output = 1; 6528 struct sctp_association *asoc; 6529 struct sctp_nets *net; 6530 6531 ca = (struct sctp_copy_all *)ptr; 6532 if (ca->m == NULL) { 6533 return; 6534 } 6535 if (ca->inp != inp) { 6536 /* TSNH */ 6537 return; 6538 } 6539 if ((ca->m) && ca->sndlen) { 6540 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT); 6541 if (m == NULL) { 6542 /* can't copy so we are done */ 6543 ca->cnt_failed++; 6544 return; 6545 } 6546 #ifdef SCTP_MBUF_LOGGING 6547 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 6548 struct mbuf *mat; 6549 6550 mat = m; 6551 while (mat) { 6552 if (SCTP_BUF_IS_EXTENDED(mat)) { 6553 sctp_log_mb(mat, SCTP_MBUF_ICOPY); 6554 } 6555 mat = SCTP_BUF_NEXT(mat); 6556 } 6557 } 6558 #endif 6559 } else { 6560 m = NULL; 6561 } 6562 SCTP_TCB_LOCK_ASSERT(stcb); 6563 if (stcb->asoc.alternate) { 6564 net = stcb->asoc.alternate; 6565 } else { 6566 net = stcb->asoc.primary_destination; 6567 } 6568 if (ca->sndrcv.sinfo_flags & SCTP_ABORT) { 6569 /* Abort this assoc with m as the user defined reason */ 6570 if (m) { 6571 struct sctp_paramhdr *ph; 6572 6573 SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT); 6574 if (m) { 6575 ph = mtod(m, struct sctp_paramhdr *); 6576 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); 6577 ph->param_length = htons(ca->sndlen); 6578 } 6579 /* 6580 * We add one here to keep the assoc from 6581 * dis-appearing on us. 6582 */ 6583 atomic_add_int(&stcb->asoc.refcnt, 1); 6584 sctp_abort_an_association(inp, stcb, 6585 SCTP_RESPONSE_TO_USER_REQ, 6586 m, SCTP_SO_NOT_LOCKED); 6587 /* 6588 * sctp_abort_an_association calls sctp_free_asoc() 6589 * free association will NOT free it since we 6590 * incremented the refcnt .. we do this to prevent 6591 * it being freed and things getting tricky since we 6592 * could end up (from free_asoc) calling inpcb_free 6593 * which would get a recursive lock call to the 6594 * iterator lock.. But as a consequence of that the 6595 * stcb will return to us un-locked.. since 6596 * free_asoc returns with either no TCB or the TCB 6597 * unlocked, we must relock.. to unlock in the 6598 * iterator timer :-0 6599 */ 6600 SCTP_TCB_LOCK(stcb); 6601 atomic_add_int(&stcb->asoc.refcnt, -1); 6602 goto no_chunk_output; 6603 } 6604 } else { 6605 if (m) { 6606 ret = sctp_msg_append(stcb, net, m, 6607 &ca->sndrcv, 1); 6608 } 6609 asoc = &stcb->asoc; 6610 if (ca->sndrcv.sinfo_flags & SCTP_EOF) { 6611 /* shutdown this assoc */ 6612 int cnt; 6613 6614 cnt = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED); 6615 6616 if (TAILQ_EMPTY(&asoc->send_queue) && 6617 TAILQ_EMPTY(&asoc->sent_queue) && 6618 (cnt == 0)) { 6619 if (asoc->locked_on_sending) { 6620 goto abort_anyway; 6621 } 6622 /* 6623 * there is nothing queued to send, so I'm 6624 * done... 6625 */ 6626 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 6627 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 6628 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 6629 /* 6630 * only send SHUTDOWN the first time 6631 * through 6632 */ 6633 sctp_send_shutdown(stcb, net); 6634 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 6635 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 6636 } 6637 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT); 6638 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 6639 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, 6640 net); 6641 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, 6642 asoc->primary_destination); 6643 added_control = 1; 6644 do_chunk_output = 0; 6645 } 6646 } else { 6647 /* 6648 * we still got (or just got) data to send, 6649 * so set SHUTDOWN_PENDING 6650 */ 6651 /* 6652 * XXX sockets draft says that SCTP_EOF 6653 * should be sent with no data. currently, 6654 * we will allow user data to be sent first 6655 * and move to SHUTDOWN-PENDING 6656 */ 6657 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 6658 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 6659 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 6660 if (asoc->locked_on_sending) { 6661 /* 6662 * Locked to send out the 6663 * data 6664 */ 6665 struct sctp_stream_queue_pending *sp; 6666 6667 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead); 6668 if (sp) { 6669 if ((sp->length == 0) && (sp->msg_is_complete == 0)) 6670 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT; 6671 } 6672 } 6673 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING; 6674 if (TAILQ_EMPTY(&asoc->send_queue) && 6675 TAILQ_EMPTY(&asoc->sent_queue) && 6676 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) { 6677 abort_anyway: 6678 atomic_add_int(&stcb->asoc.refcnt, 1); 6679 sctp_abort_an_association(stcb->sctp_ep, stcb, 6680 SCTP_RESPONSE_TO_USER_REQ, 6681 NULL, SCTP_SO_NOT_LOCKED); 6682 atomic_add_int(&stcb->asoc.refcnt, -1); 6683 goto no_chunk_output; 6684 } 6685 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, 6686 asoc->primary_destination); 6687 } 6688 } 6689 6690 } 6691 } 6692 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) + 6693 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk))); 6694 6695 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) && 6696 (stcb->asoc.total_flight > 0) && 6697 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) 6698 ) { 6699 do_chunk_output = 0; 6700 } 6701 if (do_chunk_output) 6702 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED); 6703 else if (added_control) { 6704 int num_out = 0, reason = 0, now_filled = 0; 6705 struct timeval now; 6706 int frag_point; 6707 6708 frag_point = sctp_get_frag_point(stcb, &stcb->asoc); 6709 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out, 6710 &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED); 6711 } 6712 no_chunk_output: 6713 if (ret) { 6714 ca->cnt_failed++; 6715 } else { 6716 ca->cnt_sent++; 6717 } 6718 } 6719 6720 static void 6721 sctp_sendall_completes(void *ptr, uint32_t val) 6722 { 6723 struct sctp_copy_all *ca; 6724 6725 ca = (struct sctp_copy_all *)ptr; 6726 /* 6727 * Do a notify here? Kacheong suggests that the notify be done at 6728 * the send time.. so you would push up a notification if any send 6729 * failed. Don't know if this is feasable since the only failures we 6730 * have is "memory" related and if you cannot get an mbuf to send 6731 * the data you surely can't get an mbuf to send up to notify the 6732 * user you can't send the data :-> 6733 */ 6734 6735 /* now free everything */ 6736 sctp_m_freem(ca->m); 6737 SCTP_FREE(ca, SCTP_M_COPYAL); 6738 } 6739 6740 6741 #define MC_ALIGN(m, len) do { \ 6742 SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \ 6743 } while (0) 6744 6745 6746 6747 static struct mbuf * 6748 sctp_copy_out_all(struct uio *uio, int len) 6749 { 6750 struct mbuf *ret, *at; 6751 int left, willcpy, cancpy, error; 6752 6753 ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA); 6754 if (ret == NULL) { 6755 /* TSNH */ 6756 return (NULL); 6757 } 6758 left = len; 6759 SCTP_BUF_LEN(ret) = 0; 6760 /* save space for the data chunk header */ 6761 cancpy = M_TRAILINGSPACE(ret); 6762 willcpy = min(cancpy, left); 6763 at = ret; 6764 while (left > 0) { 6765 /* Align data to the end */ 6766 error = uiomove(mtod(at, caddr_t), willcpy, uio); 6767 if (error) { 6768 err_out_now: 6769 sctp_m_freem(at); 6770 return (NULL); 6771 } 6772 SCTP_BUF_LEN(at) = willcpy; 6773 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0; 6774 left -= willcpy; 6775 if (left > 0) { 6776 SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA); 6777 if (SCTP_BUF_NEXT(at) == NULL) { 6778 goto err_out_now; 6779 } 6780 at = SCTP_BUF_NEXT(at); 6781 SCTP_BUF_LEN(at) = 0; 6782 cancpy = M_TRAILINGSPACE(at); 6783 willcpy = min(cancpy, left); 6784 } 6785 } 6786 return (ret); 6787 } 6788 6789 static int 6790 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m, 6791 struct sctp_sndrcvinfo *srcv) 6792 { 6793 int ret; 6794 struct sctp_copy_all *ca; 6795 6796 SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all), 6797 SCTP_M_COPYAL); 6798 if (ca == NULL) { 6799 sctp_m_freem(m); 6800 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 6801 return (ENOMEM); 6802 } 6803 memset(ca, 0, sizeof(struct sctp_copy_all)); 6804 6805 ca->inp = inp; 6806 if (srcv) { 6807 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo)); 6808 } 6809 /* 6810 * take off the sendall flag, it would be bad if we failed to do 6811 * this :-0 6812 */ 6813 ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL; 6814 /* get length and mbuf chain */ 6815 if (uio) { 6816 ca->sndlen = uio->uio_resid; 6817 ca->m = sctp_copy_out_all(uio, ca->sndlen); 6818 if (ca->m == NULL) { 6819 SCTP_FREE(ca, SCTP_M_COPYAL); 6820 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 6821 return (ENOMEM); 6822 } 6823 } else { 6824 /* Gather the length of the send */ 6825 struct mbuf *mat; 6826 6827 mat = m; 6828 ca->sndlen = 0; 6829 while (m) { 6830 ca->sndlen += SCTP_BUF_LEN(m); 6831 m = SCTP_BUF_NEXT(m); 6832 } 6833 ca->m = mat; 6834 } 6835 ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL, 6836 SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES, 6837 SCTP_ASOC_ANY_STATE, 6838 (void *)ca, 0, 6839 sctp_sendall_completes, inp, 1); 6840 if (ret) { 6841 SCTP_PRINTF("Failed to initiate iterator for sendall\n"); 6842 SCTP_FREE(ca, SCTP_M_COPYAL); 6843 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT); 6844 return (EFAULT); 6845 } 6846 return (0); 6847 } 6848 6849 6850 void 6851 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc) 6852 { 6853 struct sctp_tmit_chunk *chk, *nchk; 6854 6855 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { 6856 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 6857 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); 6858 if (chk->data) { 6859 sctp_m_freem(chk->data); 6860 chk->data = NULL; 6861 } 6862 asoc->ctrl_queue_cnt--; 6863 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 6864 } 6865 } 6866 } 6867 6868 void 6869 sctp_toss_old_asconf(struct sctp_tcb *stcb) 6870 { 6871 struct sctp_association *asoc; 6872 struct sctp_tmit_chunk *chk, *nchk; 6873 struct sctp_asconf_chunk *acp; 6874 6875 asoc = &stcb->asoc; 6876 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) { 6877 /* find SCTP_ASCONF chunk in queue */ 6878 if (chk->rec.chunk_id.id == SCTP_ASCONF) { 6879 if (chk->data) { 6880 acp = mtod(chk->data, struct sctp_asconf_chunk *); 6881 if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) { 6882 /* Not Acked yet */ 6883 break; 6884 } 6885 } 6886 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next); 6887 if (chk->data) { 6888 sctp_m_freem(chk->data); 6889 chk->data = NULL; 6890 } 6891 asoc->ctrl_queue_cnt--; 6892 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 6893 } 6894 } 6895 } 6896 6897 6898 static void 6899 sctp_clean_up_datalist(struct sctp_tcb *stcb, 6900 struct sctp_association *asoc, 6901 struct sctp_tmit_chunk **data_list, 6902 int bundle_at, 6903 struct sctp_nets *net) 6904 { 6905 int i; 6906 struct sctp_tmit_chunk *tp1; 6907 6908 for (i = 0; i < bundle_at; i++) { 6909 /* off of the send queue */ 6910 TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next); 6911 asoc->send_queue_cnt--; 6912 if (i > 0) { 6913 /* 6914 * Any chunk NOT 0 you zap the time chunk 0 gets 6915 * zapped or set based on if a RTO measurment is 6916 * needed. 6917 */ 6918 data_list[i]->do_rtt = 0; 6919 } 6920 /* record time */ 6921 data_list[i]->sent_rcv_time = net->last_sent_time; 6922 data_list[i]->rec.data.cwnd_at_send = net->cwnd; 6923 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq; 6924 if (data_list[i]->whoTo == NULL) { 6925 data_list[i]->whoTo = net; 6926 atomic_add_int(&net->ref_count, 1); 6927 } 6928 /* on to the sent queue */ 6929 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead); 6930 if ((tp1) && SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) { 6931 struct sctp_tmit_chunk *tpp; 6932 6933 /* need to move back */ 6934 back_up_more: 6935 tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next); 6936 if (tpp == NULL) { 6937 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next); 6938 goto all_done; 6939 } 6940 tp1 = tpp; 6941 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) { 6942 goto back_up_more; 6943 } 6944 TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next); 6945 } else { 6946 TAILQ_INSERT_TAIL(&asoc->sent_queue, 6947 data_list[i], 6948 sctp_next); 6949 } 6950 all_done: 6951 /* This does not lower until the cum-ack passes it */ 6952 asoc->sent_queue_cnt++; 6953 if ((asoc->peers_rwnd <= 0) && 6954 (asoc->total_flight == 0) && 6955 (bundle_at == 1)) { 6956 /* Mark the chunk as being a window probe */ 6957 SCTP_STAT_INCR(sctps_windowprobed); 6958 } 6959 #ifdef SCTP_AUDITING_ENABLED 6960 sctp_audit_log(0xC2, 3); 6961 #endif 6962 data_list[i]->sent = SCTP_DATAGRAM_SENT; 6963 data_list[i]->snd_count = 1; 6964 data_list[i]->rec.data.chunk_was_revoked = 0; 6965 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 6966 sctp_misc_ints(SCTP_FLIGHT_LOG_UP, 6967 data_list[i]->whoTo->flight_size, 6968 data_list[i]->book_size, 6969 (uintptr_t) data_list[i]->whoTo, 6970 data_list[i]->rec.data.TSN_seq); 6971 } 6972 sctp_flight_size_increase(data_list[i]); 6973 sctp_total_flight_increase(stcb, data_list[i]); 6974 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) { 6975 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND, 6976 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)); 6977 } 6978 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd, 6979 (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))); 6980 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 6981 /* SWS sender side engages */ 6982 asoc->peers_rwnd = 0; 6983 } 6984 } 6985 if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) { 6986 (*asoc->cc_functions.sctp_cwnd_update_packet_transmitted) (stcb, net); 6987 } 6988 } 6989 6990 static void 6991 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked 6992 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 6993 SCTP_UNUSED 6994 #endif 6995 ) 6996 { 6997 struct sctp_tmit_chunk *chk, *nchk; 6998 6999 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { 7000 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) || 7001 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */ 7002 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) || 7003 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) || 7004 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) || 7005 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) || 7006 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) || 7007 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) || 7008 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) || 7009 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) || 7010 (chk->rec.chunk_id.id == SCTP_ECN_CWR) || 7011 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) { 7012 /* Stray chunks must be cleaned up */ 7013 clean_up_anyway: 7014 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); 7015 if (chk->data) { 7016 sctp_m_freem(chk->data); 7017 chk->data = NULL; 7018 } 7019 asoc->ctrl_queue_cnt--; 7020 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) 7021 asoc->fwd_tsn_cnt--; 7022 sctp_free_a_chunk(stcb, chk, so_locked); 7023 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) { 7024 /* special handling, we must look into the param */ 7025 if (chk != asoc->str_reset) { 7026 goto clean_up_anyway; 7027 } 7028 } 7029 } 7030 } 7031 7032 7033 static int 7034 sctp_can_we_split_this(struct sctp_tcb *stcb, 7035 uint32_t length, 7036 uint32_t goal_mtu, uint32_t frag_point, int eeor_on) 7037 { 7038 /* 7039 * Make a decision on if I should split a msg into multiple parts. 7040 * This is only asked of incomplete messages. 7041 */ 7042 if (eeor_on) { 7043 /* 7044 * If we are doing EEOR we need to always send it if its the 7045 * entire thing, since it might be all the guy is putting in 7046 * the hopper. 7047 */ 7048 if (goal_mtu >= length) { 7049 /*- 7050 * If we have data outstanding, 7051 * we get another chance when the sack 7052 * arrives to transmit - wait for more data 7053 */ 7054 if (stcb->asoc.total_flight == 0) { 7055 /* 7056 * If nothing is in flight, we zero the 7057 * packet counter. 7058 */ 7059 return (length); 7060 } 7061 return (0); 7062 7063 } else { 7064 /* You can fill the rest */ 7065 return (goal_mtu); 7066 } 7067 } 7068 /*- 7069 * For those strange folk that make the send buffer 7070 * smaller than our fragmentation point, we can't 7071 * get a full msg in so we have to allow splitting. 7072 */ 7073 if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) { 7074 return (length); 7075 } 7076 if ((length <= goal_mtu) || 7077 ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) { 7078 /* Sub-optimial residual don't split in non-eeor mode. */ 7079 return (0); 7080 } 7081 /* 7082 * If we reach here length is larger than the goal_mtu. Do we wish 7083 * to split it for the sake of packet putting together? 7084 */ 7085 if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) { 7086 /* Its ok to split it */ 7087 return (min(goal_mtu, frag_point)); 7088 } 7089 /* Nope, can't split */ 7090 return (0); 7091 7092 } 7093 7094 static uint32_t 7095 sctp_move_to_outqueue(struct sctp_tcb *stcb, 7096 struct sctp_stream_out *strq, 7097 uint32_t goal_mtu, 7098 uint32_t frag_point, 7099 int *locked, 7100 int *giveup, 7101 int eeor_mode, 7102 int *bail, 7103 int so_locked 7104 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 7105 SCTP_UNUSED 7106 #endif 7107 ) 7108 { 7109 /* Move from the stream to the send_queue keeping track of the total */ 7110 struct sctp_association *asoc; 7111 struct sctp_stream_queue_pending *sp; 7112 struct sctp_tmit_chunk *chk; 7113 struct sctp_data_chunk *dchkh; 7114 uint32_t to_move, length; 7115 uint8_t rcv_flags = 0; 7116 uint8_t some_taken; 7117 uint8_t send_lock_up = 0; 7118 7119 SCTP_TCB_LOCK_ASSERT(stcb); 7120 asoc = &stcb->asoc; 7121 one_more_time: 7122 /* sa_ignore FREED_MEMORY */ 7123 sp = TAILQ_FIRST(&strq->outqueue); 7124 if (sp == NULL) { 7125 *locked = 0; 7126 if (send_lock_up == 0) { 7127 SCTP_TCB_SEND_LOCK(stcb); 7128 send_lock_up = 1; 7129 } 7130 sp = TAILQ_FIRST(&strq->outqueue); 7131 if (sp) { 7132 goto one_more_time; 7133 } 7134 if (strq->last_msg_incomplete) { 7135 SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n", 7136 strq->stream_no, 7137 strq->last_msg_incomplete); 7138 strq->last_msg_incomplete = 0; 7139 } 7140 to_move = 0; 7141 if (send_lock_up) { 7142 SCTP_TCB_SEND_UNLOCK(stcb); 7143 send_lock_up = 0; 7144 } 7145 goto out_of; 7146 } 7147 if ((sp->msg_is_complete) && (sp->length == 0)) { 7148 if (sp->sender_all_done) { 7149 /* 7150 * We are doing differed cleanup. Last time through 7151 * when we took all the data the sender_all_done was 7152 * not set. 7153 */ 7154 if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) { 7155 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); 7156 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n", 7157 sp->sender_all_done, 7158 sp->length, 7159 sp->msg_is_complete, 7160 sp->put_last_out, 7161 send_lock_up); 7162 } 7163 if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) { 7164 SCTP_TCB_SEND_LOCK(stcb); 7165 send_lock_up = 1; 7166 } 7167 atomic_subtract_int(&asoc->stream_queue_cnt, 1); 7168 TAILQ_REMOVE(&strq->outqueue, sp, next); 7169 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up); 7170 if (sp->net) { 7171 sctp_free_remote_addr(sp->net); 7172 sp->net = NULL; 7173 } 7174 if (sp->data) { 7175 sctp_m_freem(sp->data); 7176 sp->data = NULL; 7177 } 7178 sctp_free_a_strmoq(stcb, sp, so_locked); 7179 /* we can't be locked to it */ 7180 *locked = 0; 7181 stcb->asoc.locked_on_sending = NULL; 7182 if (send_lock_up) { 7183 SCTP_TCB_SEND_UNLOCK(stcb); 7184 send_lock_up = 0; 7185 } 7186 /* back to get the next msg */ 7187 goto one_more_time; 7188 } else { 7189 /* 7190 * sender just finished this but still holds a 7191 * reference 7192 */ 7193 *locked = 1; 7194 *giveup = 1; 7195 to_move = 0; 7196 goto out_of; 7197 } 7198 } else { 7199 /* is there some to get */ 7200 if (sp->length == 0) { 7201 /* no */ 7202 *locked = 1; 7203 *giveup = 1; 7204 to_move = 0; 7205 goto out_of; 7206 } else if (sp->discard_rest) { 7207 if (send_lock_up == 0) { 7208 SCTP_TCB_SEND_LOCK(stcb); 7209 send_lock_up = 1; 7210 } 7211 /* Whack down the size */ 7212 atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length); 7213 if ((stcb->sctp_socket != NULL) && \ 7214 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 7215 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { 7216 atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length); 7217 } 7218 if (sp->data) { 7219 sctp_m_freem(sp->data); 7220 sp->data = NULL; 7221 sp->tail_mbuf = NULL; 7222 } 7223 sp->length = 0; 7224 sp->some_taken = 1; 7225 *locked = 1; 7226 *giveup = 1; 7227 to_move = 0; 7228 goto out_of; 7229 } 7230 } 7231 some_taken = sp->some_taken; 7232 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 7233 sp->msg_is_complete = 1; 7234 } 7235 re_look: 7236 length = sp->length; 7237 if (sp->msg_is_complete) { 7238 /* The message is complete */ 7239 to_move = min(length, frag_point); 7240 if (to_move == length) { 7241 /* All of it fits in the MTU */ 7242 if (sp->some_taken) { 7243 rcv_flags |= SCTP_DATA_LAST_FRAG; 7244 sp->put_last_out = 1; 7245 } else { 7246 rcv_flags |= SCTP_DATA_NOT_FRAG; 7247 sp->put_last_out = 1; 7248 } 7249 } else { 7250 /* Not all of it fits, we fragment */ 7251 if (sp->some_taken == 0) { 7252 rcv_flags |= SCTP_DATA_FIRST_FRAG; 7253 } 7254 sp->some_taken = 1; 7255 } 7256 } else { 7257 to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode); 7258 if (to_move) { 7259 /*- 7260 * We use a snapshot of length in case it 7261 * is expanding during the compare. 7262 */ 7263 uint32_t llen; 7264 7265 llen = length; 7266 if (to_move >= llen) { 7267 to_move = llen; 7268 if (send_lock_up == 0) { 7269 /*- 7270 * We are taking all of an incomplete msg 7271 * thus we need a send lock. 7272 */ 7273 SCTP_TCB_SEND_LOCK(stcb); 7274 send_lock_up = 1; 7275 if (sp->msg_is_complete) { 7276 /* 7277 * the sender finished the 7278 * msg 7279 */ 7280 goto re_look; 7281 } 7282 } 7283 } 7284 if (sp->some_taken == 0) { 7285 rcv_flags |= SCTP_DATA_FIRST_FRAG; 7286 sp->some_taken = 1; 7287 } 7288 } else { 7289 /* Nothing to take. */ 7290 if (sp->some_taken) { 7291 *locked = 1; 7292 } 7293 *giveup = 1; 7294 to_move = 0; 7295 goto out_of; 7296 } 7297 } 7298 7299 /* If we reach here, we can copy out a chunk */ 7300 sctp_alloc_a_chunk(stcb, chk); 7301 if (chk == NULL) { 7302 /* No chunk memory */ 7303 *giveup = 1; 7304 to_move = 0; 7305 goto out_of; 7306 } 7307 /* 7308 * Setup for unordered if needed by looking at the user sent info 7309 * flags. 7310 */ 7311 if (sp->sinfo_flags & SCTP_UNORDERED) { 7312 rcv_flags |= SCTP_DATA_UNORDERED; 7313 } 7314 if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) || 7315 ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) { 7316 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY; 7317 } 7318 /* clear out the chunk before setting up */ 7319 memset(chk, 0, sizeof(*chk)); 7320 chk->rec.data.rcv_flags = rcv_flags; 7321 7322 if (to_move >= length) { 7323 /* we think we can steal the whole thing */ 7324 if ((sp->sender_all_done == 0) && (send_lock_up == 0)) { 7325 SCTP_TCB_SEND_LOCK(stcb); 7326 send_lock_up = 1; 7327 } 7328 if (to_move < sp->length) { 7329 /* bail, it changed */ 7330 goto dont_do_it; 7331 } 7332 chk->data = sp->data; 7333 chk->last_mbuf = sp->tail_mbuf; 7334 /* register the stealing */ 7335 sp->data = sp->tail_mbuf = NULL; 7336 } else { 7337 struct mbuf *m; 7338 7339 dont_do_it: 7340 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT); 7341 chk->last_mbuf = NULL; 7342 if (chk->data == NULL) { 7343 sp->some_taken = some_taken; 7344 sctp_free_a_chunk(stcb, chk, so_locked); 7345 *bail = 1; 7346 to_move = 0; 7347 goto out_of; 7348 } 7349 #ifdef SCTP_MBUF_LOGGING 7350 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 7351 struct mbuf *mat; 7352 7353 mat = chk->data; 7354 while (mat) { 7355 if (SCTP_BUF_IS_EXTENDED(mat)) { 7356 sctp_log_mb(mat, SCTP_MBUF_ICOPY); 7357 } 7358 mat = SCTP_BUF_NEXT(mat); 7359 } 7360 } 7361 #endif 7362 /* Pull off the data */ 7363 m_adj(sp->data, to_move); 7364 /* Now lets work our way down and compact it */ 7365 m = sp->data; 7366 while (m && (SCTP_BUF_LEN(m) == 0)) { 7367 sp->data = SCTP_BUF_NEXT(m); 7368 SCTP_BUF_NEXT(m) = NULL; 7369 if (sp->tail_mbuf == m) { 7370 /*- 7371 * Freeing tail? TSNH since 7372 * we supposedly were taking less 7373 * than the sp->length. 7374 */ 7375 #ifdef INVARIANTS 7376 panic("Huh, freing tail? - TSNH"); 7377 #else 7378 SCTP_PRINTF("Huh, freeing tail? - TSNH\n"); 7379 sp->tail_mbuf = sp->data = NULL; 7380 sp->length = 0; 7381 #endif 7382 7383 } 7384 sctp_m_free(m); 7385 m = sp->data; 7386 } 7387 } 7388 if (SCTP_BUF_IS_EXTENDED(chk->data)) { 7389 chk->copy_by_ref = 1; 7390 } else { 7391 chk->copy_by_ref = 0; 7392 } 7393 /* 7394 * get last_mbuf and counts of mb useage This is ugly but hopefully 7395 * its only one mbuf. 7396 */ 7397 if (chk->last_mbuf == NULL) { 7398 chk->last_mbuf = chk->data; 7399 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) { 7400 chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf); 7401 } 7402 } 7403 if (to_move > length) { 7404 /*- This should not happen either 7405 * since we always lower to_move to the size 7406 * of sp->length if its larger. 7407 */ 7408 #ifdef INVARIANTS 7409 panic("Huh, how can to_move be larger?"); 7410 #else 7411 SCTP_PRINTF("Huh, how can to_move be larger?\n"); 7412 sp->length = 0; 7413 #endif 7414 } else { 7415 atomic_subtract_int(&sp->length, to_move); 7416 } 7417 if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) { 7418 /* Not enough room for a chunk header, get some */ 7419 struct mbuf *m; 7420 7421 m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA); 7422 if (m == NULL) { 7423 /* 7424 * we're in trouble here. _PREPEND below will free 7425 * all the data if there is no leading space, so we 7426 * must put the data back and restore. 7427 */ 7428 if (send_lock_up == 0) { 7429 SCTP_TCB_SEND_LOCK(stcb); 7430 send_lock_up = 1; 7431 } 7432 if (chk->data == NULL) { 7433 /* unsteal the data */ 7434 sp->data = chk->data; 7435 sp->tail_mbuf = chk->last_mbuf; 7436 } else { 7437 struct mbuf *m_tmp; 7438 7439 /* reassemble the data */ 7440 m_tmp = sp->data; 7441 sp->data = chk->data; 7442 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp; 7443 } 7444 sp->some_taken = some_taken; 7445 atomic_add_int(&sp->length, to_move); 7446 chk->data = NULL; 7447 *bail = 1; 7448 sctp_free_a_chunk(stcb, chk, so_locked); 7449 to_move = 0; 7450 goto out_of; 7451 } else { 7452 SCTP_BUF_LEN(m) = 0; 7453 SCTP_BUF_NEXT(m) = chk->data; 7454 chk->data = m; 7455 M_ALIGN(chk->data, 4); 7456 } 7457 } 7458 SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT); 7459 if (chk->data == NULL) { 7460 /* HELP, TSNH since we assured it would not above? */ 7461 #ifdef INVARIANTS 7462 panic("prepend failes HELP?"); 7463 #else 7464 SCTP_PRINTF("prepend fails HELP?\n"); 7465 sctp_free_a_chunk(stcb, chk, so_locked); 7466 #endif 7467 *bail = 1; 7468 to_move = 0; 7469 goto out_of; 7470 } 7471 sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk)); 7472 chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk)); 7473 chk->book_size_scale = 0; 7474 chk->sent = SCTP_DATAGRAM_UNSENT; 7475 7476 chk->flags = 0; 7477 chk->asoc = &stcb->asoc; 7478 chk->pad_inplace = 0; 7479 chk->no_fr_allowed = 0; 7480 chk->rec.data.stream_seq = sp->strseq; 7481 chk->rec.data.stream_number = sp->stream; 7482 chk->rec.data.payloadtype = sp->ppid; 7483 chk->rec.data.context = sp->context; 7484 chk->rec.data.doing_fast_retransmit = 0; 7485 7486 chk->rec.data.timetodrop = sp->ts; 7487 chk->flags = sp->act_flags; 7488 7489 if (sp->net) { 7490 chk->whoTo = sp->net; 7491 atomic_add_int(&chk->whoTo->ref_count, 1); 7492 } else 7493 chk->whoTo = NULL; 7494 7495 if (sp->holds_key_ref) { 7496 chk->auth_keyid = sp->auth_keyid; 7497 sctp_auth_key_acquire(stcb, chk->auth_keyid); 7498 chk->holds_key_ref = 1; 7499 } 7500 chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1); 7501 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) { 7502 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND, 7503 (uintptr_t) stcb, sp->length, 7504 (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq), 7505 chk->rec.data.TSN_seq); 7506 } 7507 dchkh = mtod(chk->data, struct sctp_data_chunk *); 7508 /* 7509 * Put the rest of the things in place now. Size was done earlier in 7510 * previous loop prior to padding. 7511 */ 7512 7513 #ifdef SCTP_ASOCLOG_OF_TSNS 7514 SCTP_TCB_LOCK_ASSERT(stcb); 7515 if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) { 7516 asoc->tsn_out_at = 0; 7517 asoc->tsn_out_wrapped = 1; 7518 } 7519 asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq; 7520 asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number; 7521 asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq; 7522 asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size; 7523 asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags; 7524 asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb; 7525 asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at; 7526 asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2; 7527 asoc->tsn_out_at++; 7528 #endif 7529 7530 dchkh->ch.chunk_type = SCTP_DATA; 7531 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags; 7532 dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq); 7533 dchkh->dp.stream_id = htons(strq->stream_no); 7534 dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq); 7535 dchkh->dp.protocol_id = chk->rec.data.payloadtype; 7536 dchkh->ch.chunk_length = htons(chk->send_size); 7537 /* Now advance the chk->send_size by the actual pad needed. */ 7538 if (chk->send_size < SCTP_SIZE32(chk->book_size)) { 7539 /* need a pad */ 7540 struct mbuf *lm; 7541 int pads; 7542 7543 pads = SCTP_SIZE32(chk->book_size) - chk->send_size; 7544 if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) { 7545 chk->pad_inplace = 1; 7546 } 7547 if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) { 7548 /* pad added an mbuf */ 7549 chk->last_mbuf = lm; 7550 } 7551 chk->send_size += pads; 7552 } 7553 /* We only re-set the policy if it is on */ 7554 if (sp->pr_sctp_on) { 7555 sctp_set_prsctp_policy(sp); 7556 asoc->pr_sctp_cnt++; 7557 chk->pr_sctp_on = 1; 7558 } else { 7559 chk->pr_sctp_on = 0; 7560 } 7561 if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) { 7562 /* All done pull and kill the message */ 7563 atomic_subtract_int(&asoc->stream_queue_cnt, 1); 7564 if (sp->put_last_out == 0) { 7565 SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n"); 7566 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n", 7567 sp->sender_all_done, 7568 sp->length, 7569 sp->msg_is_complete, 7570 sp->put_last_out, 7571 send_lock_up); 7572 } 7573 if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) { 7574 SCTP_TCB_SEND_LOCK(stcb); 7575 send_lock_up = 1; 7576 } 7577 TAILQ_REMOVE(&strq->outqueue, sp, next); 7578 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up); 7579 if (sp->net) { 7580 sctp_free_remote_addr(sp->net); 7581 sp->net = NULL; 7582 } 7583 if (sp->data) { 7584 sctp_m_freem(sp->data); 7585 sp->data = NULL; 7586 } 7587 sctp_free_a_strmoq(stcb, sp, so_locked); 7588 7589 /* we can't be locked to it */ 7590 *locked = 0; 7591 stcb->asoc.locked_on_sending = NULL; 7592 } else { 7593 /* more to go, we are locked */ 7594 *locked = 1; 7595 } 7596 asoc->chunks_on_out_queue++; 7597 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next); 7598 asoc->send_queue_cnt++; 7599 out_of: 7600 if (send_lock_up) { 7601 SCTP_TCB_SEND_UNLOCK(stcb); 7602 send_lock_up = 0; 7603 } 7604 return (to_move); 7605 } 7606 7607 7608 static void 7609 sctp_fill_outqueue(struct sctp_tcb *stcb, 7610 struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now, int so_locked 7611 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 7612 SCTP_UNUSED 7613 #endif 7614 ) 7615 { 7616 struct sctp_association *asoc; 7617 struct sctp_stream_out *strq, *strqn; 7618 int goal_mtu, moved_how_much, total_moved = 0, bail = 0; 7619 int locked, giveup; 7620 7621 SCTP_TCB_LOCK_ASSERT(stcb); 7622 asoc = &stcb->asoc; 7623 #ifdef INET6 7624 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) { 7625 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD; 7626 } else { 7627 /* ?? not sure what else to do */ 7628 goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD; 7629 } 7630 #else 7631 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD; 7632 #endif 7633 /* Need an allowance for the data chunk header too */ 7634 goal_mtu -= sizeof(struct sctp_data_chunk); 7635 7636 /* must make even word boundary */ 7637 goal_mtu &= 0xfffffffc; 7638 if (asoc->locked_on_sending) { 7639 /* We are stuck on one stream until the message completes. */ 7640 strq = asoc->locked_on_sending; 7641 locked = 1; 7642 } else { 7643 strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc); 7644 locked = 0; 7645 } 7646 strqn = strq; 7647 while ((goal_mtu > 0) && strq) { 7648 giveup = 0; 7649 bail = 0; 7650 moved_how_much = sctp_move_to_outqueue(stcb, strq, goal_mtu, frag_point, &locked, 7651 &giveup, eeor_mode, &bail, so_locked); 7652 if (moved_how_much) 7653 stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, moved_how_much); 7654 7655 if (locked) { 7656 asoc->locked_on_sending = strq; 7657 if ((moved_how_much == 0) || (giveup) || bail) 7658 /* no more to move for now */ 7659 break; 7660 } else { 7661 asoc->locked_on_sending = NULL; 7662 if ((giveup) || bail) { 7663 break; 7664 } 7665 strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc); 7666 if (strq == NULL) { 7667 break; 7668 } 7669 } 7670 total_moved += moved_how_much; 7671 goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk)); 7672 goal_mtu &= 0xfffffffc; 7673 } 7674 if (bail) 7675 *quit_now = 1; 7676 7677 stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc); 7678 7679 if (total_moved == 0) { 7680 if ((stcb->asoc.sctp_cmt_on_off == 0) && 7681 (net == stcb->asoc.primary_destination)) { 7682 /* ran dry for primary network net */ 7683 SCTP_STAT_INCR(sctps_primary_randry); 7684 } else if (stcb->asoc.sctp_cmt_on_off > 0) { 7685 /* ran dry with CMT on */ 7686 SCTP_STAT_INCR(sctps_cmt_randry); 7687 } 7688 } 7689 } 7690 7691 void 7692 sctp_fix_ecn_echo(struct sctp_association *asoc) 7693 { 7694 struct sctp_tmit_chunk *chk; 7695 7696 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { 7697 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) { 7698 chk->sent = SCTP_DATAGRAM_UNSENT; 7699 } 7700 } 7701 } 7702 7703 void 7704 sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net) 7705 { 7706 struct sctp_association *asoc; 7707 struct sctp_tmit_chunk *chk; 7708 struct sctp_stream_queue_pending *sp; 7709 unsigned int i; 7710 7711 if (net == NULL) { 7712 return; 7713 } 7714 asoc = &stcb->asoc; 7715 for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 7716 TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) { 7717 if (sp->net == net) { 7718 sctp_free_remote_addr(sp->net); 7719 sp->net = NULL; 7720 } 7721 } 7722 } 7723 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) { 7724 if (chk->whoTo == net) { 7725 sctp_free_remote_addr(chk->whoTo); 7726 chk->whoTo = NULL; 7727 } 7728 } 7729 } 7730 7731 int 7732 sctp_med_chunk_output(struct sctp_inpcb *inp, 7733 struct sctp_tcb *stcb, 7734 struct sctp_association *asoc, 7735 int *num_out, 7736 int *reason_code, 7737 int control_only, int from_where, 7738 struct timeval *now, int *now_filled, int frag_point, int so_locked 7739 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 7740 SCTP_UNUSED 7741 #endif 7742 ) 7743 { 7744 /* 7745 * Ok this is the generic chunk service queue. we must do the 7746 * following: - Service the stream queue that is next, moving any 7747 * message (note I must get a complete message i.e. FIRST/MIDDLE and 7748 * LAST to the out queue in one pass) and assigning TSN's - Check to 7749 * see if the cwnd/rwnd allows any output, if so we go ahead and 7750 * fomulate and send the low level chunks. Making sure to combine 7751 * any control in the control chunk queue also. 7752 */ 7753 struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL; 7754 struct mbuf *outchain, *endoutchain; 7755 struct sctp_tmit_chunk *chk, *nchk; 7756 7757 /* temp arrays for unlinking */ 7758 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING]; 7759 int no_fragmentflg, error; 7760 unsigned int max_rwnd_per_dest, max_send_per_dest; 7761 int one_chunk, hbflag, skip_data_for_this_net; 7762 int asconf, cookie, no_out_cnt; 7763 int bundle_at, ctl_cnt, no_data_chunks, eeor_mode; 7764 unsigned int mtu, r_mtu, omtu, mx_mtu, to_out; 7765 int tsns_sent = 0; 7766 uint32_t auth_offset = 0; 7767 struct sctp_auth_chunk *auth = NULL; 7768 uint16_t auth_keyid; 7769 int override_ok = 1; 7770 int skip_fill_up = 0; 7771 int data_auth_reqd = 0; 7772 7773 /* 7774 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the 7775 * destination. 7776 */ 7777 int quit_now = 0; 7778 7779 *num_out = 0; 7780 auth_keyid = stcb->asoc.authinfo.active_keyid; 7781 7782 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) || 7783 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) || 7784 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) { 7785 eeor_mode = 1; 7786 } else { 7787 eeor_mode = 0; 7788 } 7789 ctl_cnt = no_out_cnt = asconf = cookie = 0; 7790 /* 7791 * First lets prime the pump. For each destination, if there is room 7792 * in the flight size, attempt to pull an MTU's worth out of the 7793 * stream queues into the general send_queue 7794 */ 7795 #ifdef SCTP_AUDITING_ENABLED 7796 sctp_audit_log(0xC2, 2); 7797 #endif 7798 SCTP_TCB_LOCK_ASSERT(stcb); 7799 hbflag = 0; 7800 if ((control_only) || (asoc->stream_reset_outstanding)) 7801 no_data_chunks = 1; 7802 else 7803 no_data_chunks = 0; 7804 7805 /* Nothing to possible to send? */ 7806 if ((TAILQ_EMPTY(&asoc->control_send_queue) || 7807 (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) && 7808 TAILQ_EMPTY(&asoc->asconf_send_queue) && 7809 TAILQ_EMPTY(&asoc->send_queue) && 7810 stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 7811 nothing_to_send: 7812 *reason_code = 9; 7813 return (0); 7814 } 7815 if (asoc->peers_rwnd == 0) { 7816 /* No room in peers rwnd */ 7817 *reason_code = 1; 7818 if (asoc->total_flight > 0) { 7819 /* we are allowed one chunk in flight */ 7820 no_data_chunks = 1; 7821 } 7822 } 7823 if (stcb->asoc.ecn_echo_cnt_onq) { 7824 /* Record where a sack goes, if any */ 7825 if (no_data_chunks && 7826 (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) { 7827 /* Nothing but ECNe to send - we don't do that */ 7828 goto nothing_to_send; 7829 } 7830 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { 7831 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) || 7832 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) { 7833 sack_goes_to = chk->whoTo; 7834 break; 7835 } 7836 } 7837 } 7838 max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets); 7839 if (stcb->sctp_socket) 7840 max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets; 7841 else 7842 max_send_per_dest = 0; 7843 if (no_data_chunks == 0) { 7844 /* How many non-directed chunks are there? */ 7845 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) { 7846 if (chk->whoTo == NULL) { 7847 /* 7848 * We already have non-directed chunks on 7849 * the queue, no need to do a fill-up. 7850 */ 7851 skip_fill_up = 1; 7852 break; 7853 } 7854 } 7855 7856 } 7857 if ((no_data_chunks == 0) && 7858 (skip_fill_up == 0) && 7859 (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) { 7860 TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 7861 /* 7862 * This for loop we are in takes in each net, if 7863 * its's got space in cwnd and has data sent to it 7864 * (when CMT is off) then it calls 7865 * sctp_fill_outqueue for the net. This gets data on 7866 * the send queue for that network. 7867 * 7868 * In sctp_fill_outqueue TSN's are assigned and data is 7869 * copied out of the stream buffers. Note mostly 7870 * copy by reference (we hope). 7871 */ 7872 net->window_probe = 0; 7873 if ((net != stcb->asoc.alternate) && 7874 ((net->dest_state & SCTP_ADDR_PF) || 7875 (!(net->dest_state & SCTP_ADDR_REACHABLE)) || 7876 (net->dest_state & SCTP_ADDR_UNCONFIRMED))) { 7877 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { 7878 sctp_log_cwnd(stcb, net, 1, 7879 SCTP_CWND_LOG_FILL_OUTQ_CALLED); 7880 } 7881 continue; 7882 } 7883 if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) && 7884 (net->flight_size == 0)) { 7885 (*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net); 7886 } 7887 if (net->flight_size >= net->cwnd) { 7888 /* skip this network, no room - can't fill */ 7889 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { 7890 sctp_log_cwnd(stcb, net, 3, 7891 SCTP_CWND_LOG_FILL_OUTQ_CALLED); 7892 } 7893 continue; 7894 } 7895 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { 7896 sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED); 7897 } 7898 sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked); 7899 if (quit_now) { 7900 /* memory alloc failure */ 7901 no_data_chunks = 1; 7902 break; 7903 } 7904 } 7905 } 7906 /* now service each destination and send out what we can for it */ 7907 /* Nothing to send? */ 7908 if (TAILQ_EMPTY(&asoc->control_send_queue) && 7909 TAILQ_EMPTY(&asoc->asconf_send_queue) && 7910 TAILQ_EMPTY(&asoc->send_queue)) { 7911 *reason_code = 8; 7912 return (0); 7913 } 7914 if (asoc->sctp_cmt_on_off > 0) { 7915 /* get the last start point */ 7916 start_at = asoc->last_net_cmt_send_started; 7917 if (start_at == NULL) { 7918 /* null so to beginning */ 7919 start_at = TAILQ_FIRST(&asoc->nets); 7920 } else { 7921 start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next); 7922 if (start_at == NULL) { 7923 start_at = TAILQ_FIRST(&asoc->nets); 7924 } 7925 } 7926 asoc->last_net_cmt_send_started = start_at; 7927 } else { 7928 start_at = TAILQ_FIRST(&asoc->nets); 7929 } 7930 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { 7931 if (chk->whoTo == NULL) { 7932 if (asoc->alternate) { 7933 chk->whoTo = asoc->alternate; 7934 } else { 7935 chk->whoTo = asoc->primary_destination; 7936 } 7937 atomic_add_int(&chk->whoTo->ref_count, 1); 7938 } 7939 } 7940 old_start_at = NULL; 7941 again_one_more_time: 7942 for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) { 7943 /* how much can we send? */ 7944 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */ 7945 if (old_start_at && (old_start_at == net)) { 7946 /* through list ocmpletely. */ 7947 break; 7948 } 7949 tsns_sent = 0xa; 7950 if (TAILQ_EMPTY(&asoc->control_send_queue) && 7951 TAILQ_EMPTY(&asoc->asconf_send_queue) && 7952 (net->flight_size >= net->cwnd)) { 7953 /* 7954 * Nothing on control or asconf and flight is full, 7955 * we can skip even in the CMT case. 7956 */ 7957 continue; 7958 } 7959 ctl_cnt = bundle_at = 0; 7960 endoutchain = outchain = NULL; 7961 no_fragmentflg = 1; 7962 one_chunk = 0; 7963 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { 7964 skip_data_for_this_net = 1; 7965 } else { 7966 skip_data_for_this_net = 0; 7967 } 7968 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) { 7969 /* 7970 * if we have a route and an ifp check to see if we 7971 * have room to send to this guy 7972 */ 7973 struct ifnet *ifp; 7974 7975 ifp = net->ro.ro_rt->rt_ifp; 7976 if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) { 7977 SCTP_STAT_INCR(sctps_ifnomemqueued); 7978 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) { 7979 sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED); 7980 } 7981 continue; 7982 } 7983 } 7984 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) { 7985 #ifdef INET 7986 case AF_INET: 7987 mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr)); 7988 break; 7989 #endif 7990 #ifdef INET6 7991 case AF_INET6: 7992 mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)); 7993 break; 7994 #endif 7995 default: 7996 /* TSNH */ 7997 mtu = net->mtu; 7998 break; 7999 } 8000 mx_mtu = mtu; 8001 to_out = 0; 8002 if (mtu > asoc->peers_rwnd) { 8003 if (asoc->total_flight > 0) { 8004 /* We have a packet in flight somewhere */ 8005 r_mtu = asoc->peers_rwnd; 8006 } else { 8007 /* We are always allowed to send one MTU out */ 8008 one_chunk = 1; 8009 r_mtu = mtu; 8010 } 8011 } else { 8012 r_mtu = mtu; 8013 } 8014 /************************/ 8015 /* ASCONF transmission */ 8016 /************************/ 8017 /* Now first lets go through the asconf queue */ 8018 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) { 8019 if (chk->rec.chunk_id.id != SCTP_ASCONF) { 8020 continue; 8021 } 8022 if (chk->whoTo == NULL) { 8023 if (asoc->alternate == NULL) { 8024 if (asoc->primary_destination != net) { 8025 break; 8026 } 8027 } else { 8028 if (asoc->alternate != net) { 8029 break; 8030 } 8031 } 8032 } else { 8033 if (chk->whoTo != net) { 8034 break; 8035 } 8036 } 8037 if (chk->data == NULL) { 8038 break; 8039 } 8040 if (chk->sent != SCTP_DATAGRAM_UNSENT && 8041 chk->sent != SCTP_DATAGRAM_RESEND) { 8042 break; 8043 } 8044 /* 8045 * if no AUTH is yet included and this chunk 8046 * requires it, make sure to account for it. We 8047 * don't apply the size until the AUTH chunk is 8048 * actually added below in case there is no room for 8049 * this chunk. NOTE: we overload the use of "omtu" 8050 * here 8051 */ 8052 if ((auth == NULL) && 8053 sctp_auth_is_required_chunk(chk->rec.chunk_id.id, 8054 stcb->asoc.peer_auth_chunks)) { 8055 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); 8056 } else 8057 omtu = 0; 8058 /* Here we do NOT factor the r_mtu */ 8059 if ((chk->send_size < (int)(mtu - omtu)) || 8060 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) { 8061 /* 8062 * We probably should glom the mbuf chain 8063 * from the chk->data for control but the 8064 * problem is it becomes yet one more level 8065 * of tracking to do if for some reason 8066 * output fails. Then I have got to 8067 * reconstruct the merged control chain.. el 8068 * yucko.. for now we take the easy way and 8069 * do the copy 8070 */ 8071 /* 8072 * Add an AUTH chunk, if chunk requires it 8073 * save the offset into the chain for AUTH 8074 */ 8075 if ((auth == NULL) && 8076 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id, 8077 stcb->asoc.peer_auth_chunks))) { 8078 outchain = sctp_add_auth_chunk(outchain, 8079 &endoutchain, 8080 &auth, 8081 &auth_offset, 8082 stcb, 8083 chk->rec.chunk_id.id); 8084 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); 8085 } 8086 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 8087 (int)chk->rec.chunk_id.can_take_data, 8088 chk->send_size, chk->copy_by_ref); 8089 if (outchain == NULL) { 8090 *reason_code = 8; 8091 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 8092 return (ENOMEM); 8093 } 8094 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); 8095 /* update our MTU size */ 8096 if (mtu > (chk->send_size + omtu)) 8097 mtu -= (chk->send_size + omtu); 8098 else 8099 mtu = 0; 8100 to_out += (chk->send_size + omtu); 8101 /* Do clear IP_DF ? */ 8102 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) { 8103 no_fragmentflg = 0; 8104 } 8105 if (chk->rec.chunk_id.can_take_data) 8106 chk->data = NULL; 8107 /* 8108 * set hb flag since we can use these for 8109 * RTO 8110 */ 8111 hbflag = 1; 8112 asconf = 1; 8113 /* 8114 * should sysctl this: don't bundle data 8115 * with ASCONF since it requires AUTH 8116 */ 8117 no_data_chunks = 1; 8118 chk->sent = SCTP_DATAGRAM_SENT; 8119 if (chk->whoTo == NULL) { 8120 chk->whoTo = net; 8121 atomic_add_int(&net->ref_count, 1); 8122 } 8123 chk->snd_count++; 8124 if (mtu == 0) { 8125 /* 8126 * Ok we are out of room but we can 8127 * output without effecting the 8128 * flight size since this little guy 8129 * is a control only packet. 8130 */ 8131 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net); 8132 /* 8133 * do NOT clear the asconf flag as 8134 * it is used to do appropriate 8135 * source address selection. 8136 */ 8137 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net, 8138 (struct sockaddr *)&net->ro._l_addr, 8139 outchain, auth_offset, auth, 8140 stcb->asoc.authinfo.active_keyid, 8141 no_fragmentflg, 0, NULL, asconf, 8142 inp->sctp_lport, stcb->rport, 8143 htonl(stcb->asoc.peer_vtag), 8144 net->port, so_locked, NULL, NULL))) { 8145 if (error == ENOBUFS) { 8146 asoc->ifp_had_enobuf = 1; 8147 SCTP_STAT_INCR(sctps_lowlevelerr); 8148 } 8149 if (from_where == 0) { 8150 SCTP_STAT_INCR(sctps_lowlevelerrusr); 8151 } 8152 if (*now_filled == 0) { 8153 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); 8154 *now_filled = 1; 8155 *now = net->last_sent_time; 8156 } else { 8157 net->last_sent_time = *now; 8158 } 8159 hbflag = 0; 8160 /* error, could not output */ 8161 if (error == EHOSTUNREACH) { 8162 /* 8163 * Destination went 8164 * unreachable 8165 * during this send 8166 */ 8167 sctp_move_chunks_from_net(stcb, net); 8168 } 8169 *reason_code = 7; 8170 continue; 8171 } else 8172 asoc->ifp_had_enobuf = 0; 8173 if (*now_filled == 0) { 8174 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); 8175 *now_filled = 1; 8176 *now = net->last_sent_time; 8177 } else { 8178 net->last_sent_time = *now; 8179 } 8180 hbflag = 0; 8181 /* 8182 * increase the number we sent, if a 8183 * cookie is sent we don't tell them 8184 * any was sent out. 8185 */ 8186 outchain = endoutchain = NULL; 8187 auth = NULL; 8188 auth_offset = 0; 8189 if (!no_out_cnt) 8190 *num_out += ctl_cnt; 8191 /* recalc a clean slate and setup */ 8192 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 8193 mtu = (net->mtu - SCTP_MIN_OVERHEAD); 8194 } else { 8195 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD); 8196 } 8197 to_out = 0; 8198 no_fragmentflg = 1; 8199 } 8200 } 8201 } 8202 /************************/ 8203 /* Control transmission */ 8204 /************************/ 8205 /* Now first lets go through the control queue */ 8206 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { 8207 if ((sack_goes_to) && 8208 (chk->rec.chunk_id.id == SCTP_ECN_ECHO) && 8209 (chk->whoTo != sack_goes_to)) { 8210 /* 8211 * if we have a sack in queue, and we are 8212 * looking at an ecn echo that is NOT queued 8213 * to where the sack is going.. 8214 */ 8215 if (chk->whoTo == net) { 8216 /* 8217 * Don't transmit it to where its 8218 * going (current net) 8219 */ 8220 continue; 8221 } else if (sack_goes_to == net) { 8222 /* 8223 * But do transmit it to this 8224 * address 8225 */ 8226 goto skip_net_check; 8227 } 8228 } 8229 if (chk->whoTo == NULL) { 8230 if (asoc->alternate == NULL) { 8231 if (asoc->primary_destination != net) { 8232 continue; 8233 } 8234 } else { 8235 if (asoc->alternate != net) { 8236 continue; 8237 } 8238 } 8239 } else { 8240 if (chk->whoTo != net) { 8241 continue; 8242 } 8243 } 8244 skip_net_check: 8245 if (chk->data == NULL) { 8246 continue; 8247 } 8248 if (chk->sent != SCTP_DATAGRAM_UNSENT) { 8249 /* 8250 * It must be unsent. Cookies and ASCONF's 8251 * hang around but there timers will force 8252 * when marked for resend. 8253 */ 8254 continue; 8255 } 8256 /* 8257 * if no AUTH is yet included and this chunk 8258 * requires it, make sure to account for it. We 8259 * don't apply the size until the AUTH chunk is 8260 * actually added below in case there is no room for 8261 * this chunk. NOTE: we overload the use of "omtu" 8262 * here 8263 */ 8264 if ((auth == NULL) && 8265 sctp_auth_is_required_chunk(chk->rec.chunk_id.id, 8266 stcb->asoc.peer_auth_chunks)) { 8267 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); 8268 } else 8269 omtu = 0; 8270 /* Here we do NOT factor the r_mtu */ 8271 if ((chk->send_size <= (int)(mtu - omtu)) || 8272 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) { 8273 /* 8274 * We probably should glom the mbuf chain 8275 * from the chk->data for control but the 8276 * problem is it becomes yet one more level 8277 * of tracking to do if for some reason 8278 * output fails. Then I have got to 8279 * reconstruct the merged control chain.. el 8280 * yucko.. for now we take the easy way and 8281 * do the copy 8282 */ 8283 /* 8284 * Add an AUTH chunk, if chunk requires it 8285 * save the offset into the chain for AUTH 8286 */ 8287 if ((auth == NULL) && 8288 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id, 8289 stcb->asoc.peer_auth_chunks))) { 8290 outchain = sctp_add_auth_chunk(outchain, 8291 &endoutchain, 8292 &auth, 8293 &auth_offset, 8294 stcb, 8295 chk->rec.chunk_id.id); 8296 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); 8297 } 8298 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 8299 (int)chk->rec.chunk_id.can_take_data, 8300 chk->send_size, chk->copy_by_ref); 8301 if (outchain == NULL) { 8302 *reason_code = 8; 8303 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 8304 return (ENOMEM); 8305 } 8306 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); 8307 /* update our MTU size */ 8308 if (mtu > (chk->send_size + omtu)) 8309 mtu -= (chk->send_size + omtu); 8310 else 8311 mtu = 0; 8312 to_out += (chk->send_size + omtu); 8313 /* Do clear IP_DF ? */ 8314 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) { 8315 no_fragmentflg = 0; 8316 } 8317 if (chk->rec.chunk_id.can_take_data) 8318 chk->data = NULL; 8319 /* Mark things to be removed, if needed */ 8320 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) || 8321 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */ 8322 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) || 8323 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) || 8324 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) || 8325 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) || 8326 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) || 8327 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) || 8328 (chk->rec.chunk_id.id == SCTP_ECN_CWR) || 8329 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) || 8330 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) { 8331 if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) { 8332 hbflag = 1; 8333 } 8334 /* remove these chunks at the end */ 8335 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) || 8336 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) { 8337 /* turn off the timer */ 8338 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) { 8339 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, 8340 inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1); 8341 } 8342 } 8343 ctl_cnt++; 8344 } else { 8345 /* 8346 * Other chunks, since they have 8347 * timers running (i.e. COOKIE) we 8348 * just "trust" that it gets sent or 8349 * retransmitted. 8350 */ 8351 ctl_cnt++; 8352 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 8353 cookie = 1; 8354 no_out_cnt = 1; 8355 } else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) { 8356 /* 8357 * Increment ecne send count 8358 * here this means we may be 8359 * over-zealous in our 8360 * counting if the send 8361 * fails, but its the best 8362 * place to do it (we used 8363 * to do it in the queue of 8364 * the chunk, but that did 8365 * not tell how many times 8366 * it was sent. 8367 */ 8368 SCTP_STAT_INCR(sctps_sendecne); 8369 } 8370 chk->sent = SCTP_DATAGRAM_SENT; 8371 if (chk->whoTo == NULL) { 8372 chk->whoTo = net; 8373 atomic_add_int(&net->ref_count, 1); 8374 } 8375 chk->snd_count++; 8376 } 8377 if (mtu == 0) { 8378 /* 8379 * Ok we are out of room but we can 8380 * output without effecting the 8381 * flight size since this little guy 8382 * is a control only packet. 8383 */ 8384 if (asconf) { 8385 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net); 8386 /* 8387 * do NOT clear the asconf 8388 * flag as it is used to do 8389 * appropriate source 8390 * address selection. 8391 */ 8392 } 8393 if (cookie) { 8394 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net); 8395 cookie = 0; 8396 } 8397 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net, 8398 (struct sockaddr *)&net->ro._l_addr, 8399 outchain, 8400 auth_offset, auth, 8401 stcb->asoc.authinfo.active_keyid, 8402 no_fragmentflg, 0, NULL, asconf, 8403 inp->sctp_lport, stcb->rport, 8404 htonl(stcb->asoc.peer_vtag), 8405 net->port, so_locked, NULL, NULL))) { 8406 if (error == ENOBUFS) { 8407 asoc->ifp_had_enobuf = 1; 8408 SCTP_STAT_INCR(sctps_lowlevelerr); 8409 } 8410 if (from_where == 0) { 8411 SCTP_STAT_INCR(sctps_lowlevelerrusr); 8412 } 8413 /* error, could not output */ 8414 if (hbflag) { 8415 if (*now_filled == 0) { 8416 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); 8417 *now_filled = 1; 8418 *now = net->last_sent_time; 8419 } else { 8420 net->last_sent_time = *now; 8421 } 8422 hbflag = 0; 8423 } 8424 if (error == EHOSTUNREACH) { 8425 /* 8426 * Destination went 8427 * unreachable 8428 * during this send 8429 */ 8430 sctp_move_chunks_from_net(stcb, net); 8431 } 8432 *reason_code = 7; 8433 continue; 8434 } else 8435 asoc->ifp_had_enobuf = 0; 8436 /* Only HB or ASCONF advances time */ 8437 if (hbflag) { 8438 if (*now_filled == 0) { 8439 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); 8440 *now_filled = 1; 8441 *now = net->last_sent_time; 8442 } else { 8443 net->last_sent_time = *now; 8444 } 8445 hbflag = 0; 8446 } 8447 /* 8448 * increase the number we sent, if a 8449 * cookie is sent we don't tell them 8450 * any was sent out. 8451 */ 8452 outchain = endoutchain = NULL; 8453 auth = NULL; 8454 auth_offset = 0; 8455 if (!no_out_cnt) 8456 *num_out += ctl_cnt; 8457 /* recalc a clean slate and setup */ 8458 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 8459 mtu = (net->mtu - SCTP_MIN_OVERHEAD); 8460 } else { 8461 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD); 8462 } 8463 to_out = 0; 8464 no_fragmentflg = 1; 8465 } 8466 } 8467 } 8468 /* JRI: if dest is in PF state, do not send data to it */ 8469 if ((asoc->sctp_cmt_on_off > 0) && 8470 (net != stcb->asoc.alternate) && 8471 (net->dest_state & SCTP_ADDR_PF)) { 8472 goto no_data_fill; 8473 } 8474 if (net->flight_size >= net->cwnd) { 8475 goto no_data_fill; 8476 } 8477 if ((asoc->sctp_cmt_on_off > 0) && 8478 (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) && 8479 (net->flight_size > max_rwnd_per_dest)) { 8480 goto no_data_fill; 8481 } 8482 /* 8483 * We need a specific accounting for the usage of the send 8484 * buffer. We also need to check the number of messages per 8485 * net. For now, this is better than nothing and it disabled 8486 * by default... 8487 */ 8488 if ((asoc->sctp_cmt_on_off > 0) && 8489 (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) && 8490 (max_send_per_dest > 0) && 8491 (net->flight_size > max_send_per_dest)) { 8492 goto no_data_fill; 8493 } 8494 /*********************/ 8495 /* Data transmission */ 8496 /*********************/ 8497 /* 8498 * if AUTH for DATA is required and no AUTH has been added 8499 * yet, account for this in the mtu now... if no data can be 8500 * bundled, this adjustment won't matter anyways since the 8501 * packet will be going out... 8502 */ 8503 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, 8504 stcb->asoc.peer_auth_chunks); 8505 if (data_auth_reqd && (auth == NULL)) { 8506 mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); 8507 } 8508 /* now lets add any data within the MTU constraints */ 8509 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) { 8510 case AF_INET: 8511 if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr))) 8512 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr)); 8513 else 8514 omtu = 0; 8515 break; 8516 #ifdef INET6 8517 case AF_INET6: 8518 if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr))) 8519 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)); 8520 else 8521 omtu = 0; 8522 break; 8523 #endif 8524 default: 8525 /* TSNH */ 8526 omtu = 0; 8527 break; 8528 } 8529 if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) && 8530 (skip_data_for_this_net == 0)) || 8531 (cookie)) { 8532 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { 8533 if (no_data_chunks) { 8534 /* let only control go out */ 8535 *reason_code = 1; 8536 break; 8537 } 8538 if (net->flight_size >= net->cwnd) { 8539 /* skip this net, no room for data */ 8540 *reason_code = 2; 8541 break; 8542 } 8543 if ((chk->whoTo != NULL) && 8544 (chk->whoTo != net)) { 8545 /* Don't send the chunk on this net */ 8546 continue; 8547 } 8548 if (asoc->sctp_cmt_on_off == 0) { 8549 if ((asoc->alternate) && 8550 (asoc->alternate != net) && 8551 (chk->whoTo == NULL)) { 8552 continue; 8553 } else if ((net != asoc->primary_destination) && 8554 (asoc->alternate == NULL) && 8555 (chk->whoTo == NULL)) { 8556 continue; 8557 } 8558 } 8559 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) { 8560 /*- 8561 * strange, we have a chunk that is 8562 * to big for its destination and 8563 * yet no fragment ok flag. 8564 * Something went wrong when the 8565 * PMTU changed...we did not mark 8566 * this chunk for some reason?? I 8567 * will fix it here by letting IP 8568 * fragment it for now and printing 8569 * a warning. This really should not 8570 * happen ... 8571 */ 8572 SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n", 8573 chk->send_size, mtu); 8574 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 8575 } 8576 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && 8577 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) { 8578 struct sctp_data_chunk *dchkh; 8579 8580 dchkh = mtod(chk->data, struct sctp_data_chunk *); 8581 dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY; 8582 } 8583 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) || 8584 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) { 8585 /* ok we will add this one */ 8586 8587 /* 8588 * Add an AUTH chunk, if chunk 8589 * requires it, save the offset into 8590 * the chain for AUTH 8591 */ 8592 if (data_auth_reqd) { 8593 if (auth == NULL) { 8594 outchain = sctp_add_auth_chunk(outchain, 8595 &endoutchain, 8596 &auth, 8597 &auth_offset, 8598 stcb, 8599 SCTP_DATA); 8600 auth_keyid = chk->auth_keyid; 8601 override_ok = 0; 8602 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); 8603 } else if (override_ok) { 8604 /* 8605 * use this data's 8606 * keyid 8607 */ 8608 auth_keyid = chk->auth_keyid; 8609 override_ok = 0; 8610 } else if (auth_keyid != chk->auth_keyid) { 8611 /* 8612 * different keyid, 8613 * so done bundling 8614 */ 8615 break; 8616 } 8617 } 8618 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0, 8619 chk->send_size, chk->copy_by_ref); 8620 if (outchain == NULL) { 8621 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n"); 8622 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 8623 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); 8624 } 8625 *reason_code = 3; 8626 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 8627 return (ENOMEM); 8628 } 8629 /* upate our MTU size */ 8630 /* Do clear IP_DF ? */ 8631 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) { 8632 no_fragmentflg = 0; 8633 } 8634 /* unsigned subtraction of mtu */ 8635 if (mtu > chk->send_size) 8636 mtu -= chk->send_size; 8637 else 8638 mtu = 0; 8639 /* unsigned subtraction of r_mtu */ 8640 if (r_mtu > chk->send_size) 8641 r_mtu -= chk->send_size; 8642 else 8643 r_mtu = 0; 8644 8645 to_out += chk->send_size; 8646 if ((to_out > mx_mtu) && no_fragmentflg) { 8647 #ifdef INVARIANTS 8648 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out); 8649 #else 8650 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n", 8651 mx_mtu, to_out); 8652 #endif 8653 } 8654 chk->window_probe = 0; 8655 data_list[bundle_at++] = chk; 8656 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) { 8657 mtu = 0; 8658 break; 8659 } 8660 if (chk->sent == SCTP_DATAGRAM_UNSENT) { 8661 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) { 8662 SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks); 8663 } else { 8664 SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks); 8665 } 8666 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) && 8667 ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0)) 8668 /* 8669 * Count number of 8670 * user msg's that 8671 * were fragmented 8672 * we do this by 8673 * counting when we 8674 * see a LAST 8675 * fragment only. 8676 */ 8677 SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs); 8678 } 8679 if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) { 8680 if ((one_chunk) && (stcb->asoc.total_flight == 0)) { 8681 data_list[0]->window_probe = 1; 8682 net->window_probe = 1; 8683 } 8684 break; 8685 } 8686 } else { 8687 /* 8688 * Must be sent in order of the 8689 * TSN's (on a network) 8690 */ 8691 break; 8692 } 8693 } /* for (chunk gather loop for this net) */ 8694 } /* if asoc.state OPEN */ 8695 no_data_fill: 8696 /* Is there something to send for this destination? */ 8697 if (outchain) { 8698 /* We may need to start a control timer or two */ 8699 if (asconf) { 8700 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, 8701 stcb, net); 8702 /* 8703 * do NOT clear the asconf flag as it is 8704 * used to do appropriate source address 8705 * selection. 8706 */ 8707 } 8708 if (cookie) { 8709 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net); 8710 cookie = 0; 8711 } 8712 /* must start a send timer if data is being sent */ 8713 if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) { 8714 /* 8715 * no timer running on this destination 8716 * restart it. 8717 */ 8718 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); 8719 } 8720 /* Now send it, if there is anything to send :> */ 8721 if ((error = sctp_lowlevel_chunk_output(inp, 8722 stcb, 8723 net, 8724 (struct sockaddr *)&net->ro._l_addr, 8725 outchain, 8726 auth_offset, 8727 auth, 8728 auth_keyid, 8729 no_fragmentflg, 8730 bundle_at, 8731 data_list[0], 8732 asconf, 8733 inp->sctp_lport, stcb->rport, 8734 htonl(stcb->asoc.peer_vtag), 8735 net->port, so_locked, NULL, NULL))) { 8736 /* error, we could not output */ 8737 if (error == ENOBUFS) { 8738 SCTP_STAT_INCR(sctps_lowlevelerr); 8739 asoc->ifp_had_enobuf = 1; 8740 } 8741 if (from_where == 0) { 8742 SCTP_STAT_INCR(sctps_lowlevelerrusr); 8743 } 8744 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error); 8745 if (hbflag) { 8746 if (*now_filled == 0) { 8747 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); 8748 *now_filled = 1; 8749 *now = net->last_sent_time; 8750 } else { 8751 net->last_sent_time = *now; 8752 } 8753 hbflag = 0; 8754 } 8755 if (error == EHOSTUNREACH) { 8756 /* 8757 * Destination went unreachable 8758 * during this send 8759 */ 8760 sctp_move_chunks_from_net(stcb, net); 8761 } 8762 *reason_code = 6; 8763 /*- 8764 * I add this line to be paranoid. As far as 8765 * I can tell the continue, takes us back to 8766 * the top of the for, but just to make sure 8767 * I will reset these again here. 8768 */ 8769 ctl_cnt = bundle_at = 0; 8770 continue; /* This takes us back to the 8771 * for() for the nets. */ 8772 } else { 8773 asoc->ifp_had_enobuf = 0; 8774 } 8775 outchain = endoutchain = NULL; 8776 auth = NULL; 8777 auth_offset = 0; 8778 if (bundle_at || hbflag) { 8779 /* For data/asconf and hb set time */ 8780 if (*now_filled == 0) { 8781 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); 8782 *now_filled = 1; 8783 *now = net->last_sent_time; 8784 } else { 8785 net->last_sent_time = *now; 8786 } 8787 } 8788 if (!no_out_cnt) { 8789 *num_out += (ctl_cnt + bundle_at); 8790 } 8791 if (bundle_at) { 8792 /* setup for a RTO measurement */ 8793 tsns_sent = data_list[0]->rec.data.TSN_seq; 8794 /* fill time if not already filled */ 8795 if (*now_filled == 0) { 8796 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent); 8797 *now_filled = 1; 8798 *now = asoc->time_last_sent; 8799 } else { 8800 asoc->time_last_sent = *now; 8801 } 8802 if (net->rto_needed) { 8803 data_list[0]->do_rtt = 1; 8804 net->rto_needed = 0; 8805 } 8806 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at); 8807 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net); 8808 } 8809 if (one_chunk) { 8810 break; 8811 } 8812 } 8813 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { 8814 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND); 8815 } 8816 } 8817 if (old_start_at == NULL) { 8818 old_start_at = start_at; 8819 start_at = TAILQ_FIRST(&asoc->nets); 8820 if (old_start_at) 8821 goto again_one_more_time; 8822 } 8823 /* 8824 * At the end there should be no NON timed chunks hanging on this 8825 * queue. 8826 */ 8827 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { 8828 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND); 8829 } 8830 if ((*num_out == 0) && (*reason_code == 0)) { 8831 *reason_code = 4; 8832 } else { 8833 *reason_code = 5; 8834 } 8835 sctp_clean_up_ctl(stcb, asoc, so_locked); 8836 return (0); 8837 } 8838 8839 void 8840 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err) 8841 { 8842 /*- 8843 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of 8844 * the control chunk queue. 8845 */ 8846 struct sctp_chunkhdr *hdr; 8847 struct sctp_tmit_chunk *chk; 8848 struct mbuf *mat; 8849 8850 SCTP_TCB_LOCK_ASSERT(stcb); 8851 sctp_alloc_a_chunk(stcb, chk); 8852 if (chk == NULL) { 8853 /* no memory */ 8854 sctp_m_freem(op_err); 8855 return; 8856 } 8857 chk->copy_by_ref = 0; 8858 SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT); 8859 if (op_err == NULL) { 8860 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 8861 return; 8862 } 8863 chk->send_size = 0; 8864 mat = op_err; 8865 while (mat != NULL) { 8866 chk->send_size += SCTP_BUF_LEN(mat); 8867 mat = SCTP_BUF_NEXT(mat); 8868 } 8869 chk->rec.chunk_id.id = SCTP_OPERATION_ERROR; 8870 chk->rec.chunk_id.can_take_data = 1; 8871 chk->sent = SCTP_DATAGRAM_UNSENT; 8872 chk->snd_count = 0; 8873 chk->flags = 0; 8874 chk->asoc = &stcb->asoc; 8875 chk->data = op_err; 8876 chk->whoTo = NULL; 8877 hdr = mtod(op_err, struct sctp_chunkhdr *); 8878 hdr->chunk_type = SCTP_OPERATION_ERROR; 8879 hdr->chunk_flags = 0; 8880 hdr->chunk_length = htons(chk->send_size); 8881 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, 8882 chk, 8883 sctp_next); 8884 chk->asoc->ctrl_queue_cnt++; 8885 } 8886 8887 int 8888 sctp_send_cookie_echo(struct mbuf *m, 8889 int offset, 8890 struct sctp_tcb *stcb, 8891 struct sctp_nets *net) 8892 { 8893 /*- 8894 * pull out the cookie and put it at the front of the control chunk 8895 * queue. 8896 */ 8897 int at; 8898 struct mbuf *cookie; 8899 struct sctp_paramhdr parm, *phdr; 8900 struct sctp_chunkhdr *hdr; 8901 struct sctp_tmit_chunk *chk; 8902 uint16_t ptype, plen; 8903 8904 /* First find the cookie in the param area */ 8905 cookie = NULL; 8906 at = offset + sizeof(struct sctp_init_chunk); 8907 8908 SCTP_TCB_LOCK_ASSERT(stcb); 8909 do { 8910 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm)); 8911 if (phdr == NULL) { 8912 return (-3); 8913 } 8914 ptype = ntohs(phdr->param_type); 8915 plen = ntohs(phdr->param_length); 8916 if (ptype == SCTP_STATE_COOKIE) { 8917 int pad; 8918 8919 /* found the cookie */ 8920 if ((pad = (plen % 4))) { 8921 plen += 4 - pad; 8922 } 8923 cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT); 8924 if (cookie == NULL) { 8925 /* No memory */ 8926 return (-2); 8927 } 8928 #ifdef SCTP_MBUF_LOGGING 8929 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 8930 struct mbuf *mat; 8931 8932 mat = cookie; 8933 while (mat) { 8934 if (SCTP_BUF_IS_EXTENDED(mat)) { 8935 sctp_log_mb(mat, SCTP_MBUF_ICOPY); 8936 } 8937 mat = SCTP_BUF_NEXT(mat); 8938 } 8939 } 8940 #endif 8941 break; 8942 } 8943 at += SCTP_SIZE32(plen); 8944 } while (phdr); 8945 if (cookie == NULL) { 8946 /* Did not find the cookie */ 8947 return (-3); 8948 } 8949 /* ok, we got the cookie lets change it into a cookie echo chunk */ 8950 8951 /* first the change from param to cookie */ 8952 hdr = mtod(cookie, struct sctp_chunkhdr *); 8953 hdr->chunk_type = SCTP_COOKIE_ECHO; 8954 hdr->chunk_flags = 0; 8955 /* get the chunk stuff now and place it in the FRONT of the queue */ 8956 sctp_alloc_a_chunk(stcb, chk); 8957 if (chk == NULL) { 8958 /* no memory */ 8959 sctp_m_freem(cookie); 8960 return (-5); 8961 } 8962 chk->copy_by_ref = 0; 8963 chk->send_size = plen; 8964 chk->rec.chunk_id.id = SCTP_COOKIE_ECHO; 8965 chk->rec.chunk_id.can_take_data = 0; 8966 chk->sent = SCTP_DATAGRAM_UNSENT; 8967 chk->snd_count = 0; 8968 chk->flags = CHUNK_FLAGS_FRAGMENT_OK; 8969 chk->asoc = &stcb->asoc; 8970 chk->data = cookie; 8971 chk->whoTo = net; 8972 atomic_add_int(&chk->whoTo->ref_count, 1); 8973 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next); 8974 chk->asoc->ctrl_queue_cnt++; 8975 return (0); 8976 } 8977 8978 void 8979 sctp_send_heartbeat_ack(struct sctp_tcb *stcb, 8980 struct mbuf *m, 8981 int offset, 8982 int chk_length, 8983 struct sctp_nets *net) 8984 { 8985 /* 8986 * take a HB request and make it into a HB ack and send it. 8987 */ 8988 struct mbuf *outchain; 8989 struct sctp_chunkhdr *chdr; 8990 struct sctp_tmit_chunk *chk; 8991 8992 8993 if (net == NULL) 8994 /* must have a net pointer */ 8995 return; 8996 8997 outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT); 8998 if (outchain == NULL) { 8999 /* gak out of memory */ 9000 return; 9001 } 9002 #ifdef SCTP_MBUF_LOGGING 9003 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 9004 struct mbuf *mat; 9005 9006 mat = outchain; 9007 while (mat) { 9008 if (SCTP_BUF_IS_EXTENDED(mat)) { 9009 sctp_log_mb(mat, SCTP_MBUF_ICOPY); 9010 } 9011 mat = SCTP_BUF_NEXT(mat); 9012 } 9013 } 9014 #endif 9015 chdr = mtod(outchain, struct sctp_chunkhdr *); 9016 chdr->chunk_type = SCTP_HEARTBEAT_ACK; 9017 chdr->chunk_flags = 0; 9018 if (chk_length % 4) { 9019 /* need pad */ 9020 uint32_t cpthis = 0; 9021 int padlen; 9022 9023 padlen = 4 - (chk_length % 4); 9024 m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis); 9025 } 9026 sctp_alloc_a_chunk(stcb, chk); 9027 if (chk == NULL) { 9028 /* no memory */ 9029 sctp_m_freem(outchain); 9030 return; 9031 } 9032 chk->copy_by_ref = 0; 9033 chk->send_size = chk_length; 9034 chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK; 9035 chk->rec.chunk_id.can_take_data = 1; 9036 chk->sent = SCTP_DATAGRAM_UNSENT; 9037 chk->snd_count = 0; 9038 chk->flags = 0; 9039 chk->asoc = &stcb->asoc; 9040 chk->data = outchain; 9041 chk->whoTo = net; 9042 atomic_add_int(&chk->whoTo->ref_count, 1); 9043 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); 9044 chk->asoc->ctrl_queue_cnt++; 9045 } 9046 9047 void 9048 sctp_send_cookie_ack(struct sctp_tcb *stcb) 9049 { 9050 /* formulate and queue a cookie-ack back to sender */ 9051 struct mbuf *cookie_ack; 9052 struct sctp_chunkhdr *hdr; 9053 struct sctp_tmit_chunk *chk; 9054 9055 cookie_ack = NULL; 9056 SCTP_TCB_LOCK_ASSERT(stcb); 9057 9058 cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER); 9059 if (cookie_ack == NULL) { 9060 /* no mbuf's */ 9061 return; 9062 } 9063 SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD); 9064 sctp_alloc_a_chunk(stcb, chk); 9065 if (chk == NULL) { 9066 /* no memory */ 9067 sctp_m_freem(cookie_ack); 9068 return; 9069 } 9070 chk->copy_by_ref = 0; 9071 chk->send_size = sizeof(struct sctp_chunkhdr); 9072 chk->rec.chunk_id.id = SCTP_COOKIE_ACK; 9073 chk->rec.chunk_id.can_take_data = 1; 9074 chk->sent = SCTP_DATAGRAM_UNSENT; 9075 chk->snd_count = 0; 9076 chk->flags = 0; 9077 chk->asoc = &stcb->asoc; 9078 chk->data = cookie_ack; 9079 if (chk->asoc->last_control_chunk_from != NULL) { 9080 chk->whoTo = chk->asoc->last_control_chunk_from; 9081 atomic_add_int(&chk->whoTo->ref_count, 1); 9082 } else { 9083 chk->whoTo = NULL; 9084 } 9085 hdr = mtod(cookie_ack, struct sctp_chunkhdr *); 9086 hdr->chunk_type = SCTP_COOKIE_ACK; 9087 hdr->chunk_flags = 0; 9088 hdr->chunk_length = htons(chk->send_size); 9089 SCTP_BUF_LEN(cookie_ack) = chk->send_size; 9090 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); 9091 chk->asoc->ctrl_queue_cnt++; 9092 return; 9093 } 9094 9095 9096 void 9097 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net) 9098 { 9099 /* formulate and queue a SHUTDOWN-ACK back to the sender */ 9100 struct mbuf *m_shutdown_ack; 9101 struct sctp_shutdown_ack_chunk *ack_cp; 9102 struct sctp_tmit_chunk *chk; 9103 9104 m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER); 9105 if (m_shutdown_ack == NULL) { 9106 /* no mbuf's */ 9107 return; 9108 } 9109 SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD); 9110 sctp_alloc_a_chunk(stcb, chk); 9111 if (chk == NULL) { 9112 /* no memory */ 9113 sctp_m_freem(m_shutdown_ack); 9114 return; 9115 } 9116 chk->copy_by_ref = 0; 9117 chk->send_size = sizeof(struct sctp_chunkhdr); 9118 chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK; 9119 chk->rec.chunk_id.can_take_data = 1; 9120 chk->sent = SCTP_DATAGRAM_UNSENT; 9121 chk->snd_count = 0; 9122 chk->flags = 0; 9123 chk->asoc = &stcb->asoc; 9124 chk->data = m_shutdown_ack; 9125 chk->whoTo = net; 9126 if (chk->whoTo) { 9127 atomic_add_int(&chk->whoTo->ref_count, 1); 9128 } 9129 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *); 9130 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK; 9131 ack_cp->ch.chunk_flags = 0; 9132 ack_cp->ch.chunk_length = htons(chk->send_size); 9133 SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size; 9134 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); 9135 chk->asoc->ctrl_queue_cnt++; 9136 return; 9137 } 9138 9139 void 9140 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net) 9141 { 9142 /* formulate and queue a SHUTDOWN to the sender */ 9143 struct mbuf *m_shutdown; 9144 struct sctp_shutdown_chunk *shutdown_cp; 9145 struct sctp_tmit_chunk *chk; 9146 9147 m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER); 9148 if (m_shutdown == NULL) { 9149 /* no mbuf's */ 9150 return; 9151 } 9152 SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD); 9153 sctp_alloc_a_chunk(stcb, chk); 9154 if (chk == NULL) { 9155 /* no memory */ 9156 sctp_m_freem(m_shutdown); 9157 return; 9158 } 9159 chk->copy_by_ref = 0; 9160 chk->send_size = sizeof(struct sctp_shutdown_chunk); 9161 chk->rec.chunk_id.id = SCTP_SHUTDOWN; 9162 chk->rec.chunk_id.can_take_data = 1; 9163 chk->sent = SCTP_DATAGRAM_UNSENT; 9164 chk->snd_count = 0; 9165 chk->flags = 0; 9166 chk->asoc = &stcb->asoc; 9167 chk->data = m_shutdown; 9168 chk->whoTo = net; 9169 if (chk->whoTo) { 9170 atomic_add_int(&chk->whoTo->ref_count, 1); 9171 } 9172 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *); 9173 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN; 9174 shutdown_cp->ch.chunk_flags = 0; 9175 shutdown_cp->ch.chunk_length = htons(chk->send_size); 9176 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn); 9177 SCTP_BUF_LEN(m_shutdown) = chk->send_size; 9178 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); 9179 chk->asoc->ctrl_queue_cnt++; 9180 return; 9181 } 9182 9183 void 9184 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked) 9185 { 9186 /* 9187 * formulate and queue an ASCONF to the peer. ASCONF parameters 9188 * should be queued on the assoc queue. 9189 */ 9190 struct sctp_tmit_chunk *chk; 9191 struct mbuf *m_asconf; 9192 int len; 9193 9194 SCTP_TCB_LOCK_ASSERT(stcb); 9195 9196 if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) && 9197 (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) { 9198 /* can't send a new one if there is one in flight already */ 9199 return; 9200 } 9201 /* compose an ASCONF chunk, maximum length is PMTU */ 9202 m_asconf = sctp_compose_asconf(stcb, &len, addr_locked); 9203 if (m_asconf == NULL) { 9204 return; 9205 } 9206 sctp_alloc_a_chunk(stcb, chk); 9207 if (chk == NULL) { 9208 /* no memory */ 9209 sctp_m_freem(m_asconf); 9210 return; 9211 } 9212 chk->copy_by_ref = 0; 9213 chk->data = m_asconf; 9214 chk->send_size = len; 9215 chk->rec.chunk_id.id = SCTP_ASCONF; 9216 chk->rec.chunk_id.can_take_data = 0; 9217 chk->sent = SCTP_DATAGRAM_UNSENT; 9218 chk->snd_count = 0; 9219 chk->flags = CHUNK_FLAGS_FRAGMENT_OK; 9220 chk->asoc = &stcb->asoc; 9221 chk->whoTo = net; 9222 if (chk->whoTo) { 9223 atomic_add_int(&chk->whoTo->ref_count, 1); 9224 } 9225 TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next); 9226 chk->asoc->ctrl_queue_cnt++; 9227 return; 9228 } 9229 9230 void 9231 sctp_send_asconf_ack(struct sctp_tcb *stcb) 9232 { 9233 /* 9234 * formulate and queue a asconf-ack back to sender. the asconf-ack 9235 * must be stored in the tcb. 9236 */ 9237 struct sctp_tmit_chunk *chk; 9238 struct sctp_asconf_ack *ack, *latest_ack; 9239 struct mbuf *m_ack, *m; 9240 struct sctp_nets *net = NULL; 9241 9242 SCTP_TCB_LOCK_ASSERT(stcb); 9243 /* Get the latest ASCONF-ACK */ 9244 latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead); 9245 if (latest_ack == NULL) { 9246 return; 9247 } 9248 if (latest_ack->last_sent_to != NULL && 9249 latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) { 9250 /* we're doing a retransmission */ 9251 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0); 9252 if (net == NULL) { 9253 /* no alternate */ 9254 if (stcb->asoc.last_control_chunk_from == NULL) { 9255 if (stcb->asoc.alternate) { 9256 net = stcb->asoc.alternate; 9257 } else { 9258 net = stcb->asoc.primary_destination; 9259 } 9260 } else { 9261 net = stcb->asoc.last_control_chunk_from; 9262 } 9263 } 9264 } else { 9265 /* normal case */ 9266 if (stcb->asoc.last_control_chunk_from == NULL) { 9267 if (stcb->asoc.alternate) { 9268 net = stcb->asoc.alternate; 9269 } else { 9270 net = stcb->asoc.primary_destination; 9271 } 9272 } else { 9273 net = stcb->asoc.last_control_chunk_from; 9274 } 9275 } 9276 latest_ack->last_sent_to = net; 9277 9278 TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) { 9279 if (ack->data == NULL) { 9280 continue; 9281 } 9282 /* copy the asconf_ack */ 9283 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT); 9284 if (m_ack == NULL) { 9285 /* couldn't copy it */ 9286 return; 9287 } 9288 #ifdef SCTP_MBUF_LOGGING 9289 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 9290 struct mbuf *mat; 9291 9292 mat = m_ack; 9293 while (mat) { 9294 if (SCTP_BUF_IS_EXTENDED(mat)) { 9295 sctp_log_mb(mat, SCTP_MBUF_ICOPY); 9296 } 9297 mat = SCTP_BUF_NEXT(mat); 9298 } 9299 } 9300 #endif 9301 9302 sctp_alloc_a_chunk(stcb, chk); 9303 if (chk == NULL) { 9304 /* no memory */ 9305 if (m_ack) 9306 sctp_m_freem(m_ack); 9307 return; 9308 } 9309 chk->copy_by_ref = 0; 9310 9311 chk->whoTo = net; 9312 if (chk->whoTo) { 9313 atomic_add_int(&chk->whoTo->ref_count, 1); 9314 } 9315 chk->data = m_ack; 9316 chk->send_size = 0; 9317 /* Get size */ 9318 m = m_ack; 9319 chk->send_size = ack->len; 9320 chk->rec.chunk_id.id = SCTP_ASCONF_ACK; 9321 chk->rec.chunk_id.can_take_data = 1; 9322 chk->sent = SCTP_DATAGRAM_UNSENT; 9323 chk->snd_count = 0; 9324 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; /* XXX */ 9325 chk->asoc = &stcb->asoc; 9326 9327 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); 9328 chk->asoc->ctrl_queue_cnt++; 9329 } 9330 return; 9331 } 9332 9333 9334 static int 9335 sctp_chunk_retransmission(struct sctp_inpcb *inp, 9336 struct sctp_tcb *stcb, 9337 struct sctp_association *asoc, 9338 int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked 9339 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 9340 SCTP_UNUSED 9341 #endif 9342 ) 9343 { 9344 /*- 9345 * send out one MTU of retransmission. If fast_retransmit is 9346 * happening we ignore the cwnd. Otherwise we obey the cwnd and 9347 * rwnd. For a Cookie or Asconf in the control chunk queue we 9348 * retransmit them by themselves. 9349 * 9350 * For data chunks we will pick out the lowest TSN's in the sent_queue 9351 * marked for resend and bundle them all together (up to a MTU of 9352 * destination). The address to send to should have been 9353 * selected/changed where the retransmission was marked (i.e. in FR 9354 * or t3-timeout routines). 9355 */ 9356 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING]; 9357 struct sctp_tmit_chunk *chk, *fwd; 9358 struct mbuf *m, *endofchain; 9359 struct sctp_nets *net = NULL; 9360 uint32_t tsns_sent = 0; 9361 int no_fragmentflg, bundle_at, cnt_thru; 9362 unsigned int mtu; 9363 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started; 9364 struct sctp_auth_chunk *auth = NULL; 9365 uint32_t auth_offset = 0; 9366 uint16_t auth_keyid; 9367 int override_ok = 1; 9368 int data_auth_reqd = 0; 9369 uint32_t dmtu = 0; 9370 9371 SCTP_TCB_LOCK_ASSERT(stcb); 9372 tmr_started = ctl_cnt = bundle_at = error = 0; 9373 no_fragmentflg = 1; 9374 fwd_tsn = 0; 9375 *cnt_out = 0; 9376 fwd = NULL; 9377 endofchain = m = NULL; 9378 auth_keyid = stcb->asoc.authinfo.active_keyid; 9379 #ifdef SCTP_AUDITING_ENABLED 9380 sctp_audit_log(0xC3, 1); 9381 #endif 9382 if ((TAILQ_EMPTY(&asoc->sent_queue)) && 9383 (TAILQ_EMPTY(&asoc->control_send_queue))) { 9384 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n", 9385 asoc->sent_queue_retran_cnt); 9386 asoc->sent_queue_cnt = 0; 9387 asoc->sent_queue_cnt_removeable = 0; 9388 /* send back 0/0 so we enter normal transmission */ 9389 *cnt_out = 0; 9390 return (0); 9391 } 9392 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { 9393 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) || 9394 (chk->rec.chunk_id.id == SCTP_STREAM_RESET) || 9395 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) { 9396 if (chk->sent != SCTP_DATAGRAM_RESEND) { 9397 continue; 9398 } 9399 if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) { 9400 if (chk != asoc->str_reset) { 9401 /* 9402 * not eligible for retran if its 9403 * not ours 9404 */ 9405 continue; 9406 } 9407 } 9408 ctl_cnt++; 9409 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) { 9410 fwd_tsn = 1; 9411 fwd = chk; 9412 } 9413 /* 9414 * Add an AUTH chunk, if chunk requires it save the 9415 * offset into the chain for AUTH 9416 */ 9417 if ((auth == NULL) && 9418 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id, 9419 stcb->asoc.peer_auth_chunks))) { 9420 m = sctp_add_auth_chunk(m, &endofchain, 9421 &auth, &auth_offset, 9422 stcb, 9423 chk->rec.chunk_id.id); 9424 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); 9425 } 9426 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref); 9427 break; 9428 } 9429 } 9430 one_chunk = 0; 9431 cnt_thru = 0; 9432 /* do we have control chunks to retransmit? */ 9433 if (m != NULL) { 9434 /* Start a timer no matter if we suceed or fail */ 9435 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) { 9436 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo); 9437 } else if (chk->rec.chunk_id.id == SCTP_ASCONF) 9438 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo); 9439 chk->snd_count++; /* update our count */ 9440 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo, 9441 (struct sockaddr *)&chk->whoTo->ro._l_addr, m, 9442 auth_offset, auth, stcb->asoc.authinfo.active_keyid, 9443 no_fragmentflg, 0, NULL, 0, 9444 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag), 9445 chk->whoTo->port, so_locked, NULL, NULL))) { 9446 SCTP_STAT_INCR(sctps_lowlevelerr); 9447 return (error); 9448 } 9449 m = endofchain = NULL; 9450 auth = NULL; 9451 auth_offset = 0; 9452 /* 9453 * We don't want to mark the net->sent time here since this 9454 * we use this for HB and retrans cannot measure RTT 9455 */ 9456 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */ 9457 *cnt_out += 1; 9458 chk->sent = SCTP_DATAGRAM_SENT; 9459 sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt); 9460 if (fwd_tsn == 0) { 9461 return (0); 9462 } else { 9463 /* Clean up the fwd-tsn list */ 9464 sctp_clean_up_ctl(stcb, asoc, so_locked); 9465 return (0); 9466 } 9467 } 9468 /* 9469 * Ok, it is just data retransmission we need to do or that and a 9470 * fwd-tsn with it all. 9471 */ 9472 if (TAILQ_EMPTY(&asoc->sent_queue)) { 9473 return (SCTP_RETRAN_DONE); 9474 } 9475 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) || 9476 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) { 9477 /* not yet open, resend the cookie and that is it */ 9478 return (1); 9479 } 9480 #ifdef SCTP_AUDITING_ENABLED 9481 sctp_auditing(20, inp, stcb, NULL); 9482 #endif 9483 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks); 9484 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) { 9485 if (chk->sent != SCTP_DATAGRAM_RESEND) { 9486 /* No, not sent to this net or not ready for rtx */ 9487 continue; 9488 } 9489 if (chk->data == NULL) { 9490 printf("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n", 9491 chk->rec.data.TSN_seq, chk->snd_count, chk->sent); 9492 continue; 9493 } 9494 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) && 9495 (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) { 9496 /* Gak, we have exceeded max unlucky retran, abort! */ 9497 SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n", 9498 chk->snd_count, 9499 SCTP_BASE_SYSCTL(sctp_max_retran_chunk)); 9500 atomic_add_int(&stcb->asoc.refcnt, 1); 9501 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked); 9502 SCTP_TCB_LOCK(stcb); 9503 atomic_subtract_int(&stcb->asoc.refcnt, 1); 9504 return (SCTP_RETRAN_EXIT); 9505 } 9506 /* pick up the net */ 9507 net = chk->whoTo; 9508 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 9509 mtu = (net->mtu - SCTP_MIN_OVERHEAD); 9510 } else { 9511 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD; 9512 } 9513 9514 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) { 9515 /* No room in peers rwnd */ 9516 uint32_t tsn; 9517 9518 tsn = asoc->last_acked_seq + 1; 9519 if (tsn == chk->rec.data.TSN_seq) { 9520 /* 9521 * we make a special exception for this 9522 * case. The peer has no rwnd but is missing 9523 * the lowest chunk.. which is probably what 9524 * is holding up the rwnd. 9525 */ 9526 goto one_chunk_around; 9527 } 9528 return (1); 9529 } 9530 one_chunk_around: 9531 if (asoc->peers_rwnd < mtu) { 9532 one_chunk = 1; 9533 if ((asoc->peers_rwnd == 0) && 9534 (asoc->total_flight == 0)) { 9535 chk->window_probe = 1; 9536 chk->whoTo->window_probe = 1; 9537 } 9538 } 9539 #ifdef SCTP_AUDITING_ENABLED 9540 sctp_audit_log(0xC3, 2); 9541 #endif 9542 bundle_at = 0; 9543 m = NULL; 9544 net->fast_retran_ip = 0; 9545 if (chk->rec.data.doing_fast_retransmit == 0) { 9546 /* 9547 * if no FR in progress skip destination that have 9548 * flight_size > cwnd. 9549 */ 9550 if (net->flight_size >= net->cwnd) { 9551 continue; 9552 } 9553 } else { 9554 /* 9555 * Mark the destination net to have FR recovery 9556 * limits put on it. 9557 */ 9558 *fr_done = 1; 9559 net->fast_retran_ip = 1; 9560 } 9561 9562 /* 9563 * if no AUTH is yet included and this chunk requires it, 9564 * make sure to account for it. We don't apply the size 9565 * until the AUTH chunk is actually added below in case 9566 * there is no room for this chunk. 9567 */ 9568 if (data_auth_reqd && (auth == NULL)) { 9569 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); 9570 } else 9571 dmtu = 0; 9572 9573 if ((chk->send_size <= (mtu - dmtu)) || 9574 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) { 9575 /* ok we will add this one */ 9576 if (data_auth_reqd) { 9577 if (auth == NULL) { 9578 m = sctp_add_auth_chunk(m, 9579 &endofchain, 9580 &auth, 9581 &auth_offset, 9582 stcb, 9583 SCTP_DATA); 9584 auth_keyid = chk->auth_keyid; 9585 override_ok = 0; 9586 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); 9587 } else if (override_ok) { 9588 auth_keyid = chk->auth_keyid; 9589 override_ok = 0; 9590 } else if (chk->auth_keyid != auth_keyid) { 9591 /* different keyid, so done bundling */ 9592 break; 9593 } 9594 } 9595 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref); 9596 if (m == NULL) { 9597 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 9598 return (ENOMEM); 9599 } 9600 /* Do clear IP_DF ? */ 9601 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) { 9602 no_fragmentflg = 0; 9603 } 9604 /* upate our MTU size */ 9605 if (mtu > (chk->send_size + dmtu)) 9606 mtu -= (chk->send_size + dmtu); 9607 else 9608 mtu = 0; 9609 data_list[bundle_at++] = chk; 9610 if (one_chunk && (asoc->total_flight <= 0)) { 9611 SCTP_STAT_INCR(sctps_windowprobed); 9612 } 9613 } 9614 if (one_chunk == 0) { 9615 /* 9616 * now are there anymore forward from chk to pick 9617 * up? 9618 */ 9619 fwd = TAILQ_NEXT(chk, sctp_next); 9620 while (fwd) { 9621 if (fwd->sent != SCTP_DATAGRAM_RESEND) { 9622 /* Nope, not for retran */ 9623 fwd = TAILQ_NEXT(fwd, sctp_next); 9624 continue; 9625 } 9626 if (fwd->whoTo != net) { 9627 /* Nope, not the net in question */ 9628 fwd = TAILQ_NEXT(fwd, sctp_next); 9629 continue; 9630 } 9631 if (data_auth_reqd && (auth == NULL)) { 9632 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); 9633 } else 9634 dmtu = 0; 9635 if (fwd->send_size <= (mtu - dmtu)) { 9636 if (data_auth_reqd) { 9637 if (auth == NULL) { 9638 m = sctp_add_auth_chunk(m, 9639 &endofchain, 9640 &auth, 9641 &auth_offset, 9642 stcb, 9643 SCTP_DATA); 9644 auth_keyid = fwd->auth_keyid; 9645 override_ok = 0; 9646 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); 9647 } else if (override_ok) { 9648 auth_keyid = fwd->auth_keyid; 9649 override_ok = 0; 9650 } else if (fwd->auth_keyid != auth_keyid) { 9651 /* 9652 * different keyid, 9653 * so done bundling 9654 */ 9655 break; 9656 } 9657 } 9658 m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref); 9659 if (m == NULL) { 9660 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 9661 return (ENOMEM); 9662 } 9663 /* Do clear IP_DF ? */ 9664 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) { 9665 no_fragmentflg = 0; 9666 } 9667 /* upate our MTU size */ 9668 if (mtu > (fwd->send_size + dmtu)) 9669 mtu -= (fwd->send_size + dmtu); 9670 else 9671 mtu = 0; 9672 data_list[bundle_at++] = fwd; 9673 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) { 9674 break; 9675 } 9676 fwd = TAILQ_NEXT(fwd, sctp_next); 9677 } else { 9678 /* can't fit so we are done */ 9679 break; 9680 } 9681 } 9682 } 9683 /* Is there something to send for this destination? */ 9684 if (m) { 9685 /* 9686 * No matter if we fail/or suceed we should start a 9687 * timer. A failure is like a lost IP packet :-) 9688 */ 9689 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 9690 /* 9691 * no timer running on this destination 9692 * restart it. 9693 */ 9694 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); 9695 tmr_started = 1; 9696 } 9697 /* Now lets send it, if there is anything to send :> */ 9698 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net, 9699 (struct sockaddr *)&net->ro._l_addr, m, 9700 auth_offset, auth, auth_keyid, 9701 no_fragmentflg, 0, NULL, 0, 9702 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag), 9703 net->port, so_locked, NULL, NULL))) { 9704 /* error, we could not output */ 9705 SCTP_STAT_INCR(sctps_lowlevelerr); 9706 return (error); 9707 } 9708 m = endofchain = NULL; 9709 auth = NULL; 9710 auth_offset = 0; 9711 /* For HB's */ 9712 /* 9713 * We don't want to mark the net->sent time here 9714 * since this we use this for HB and retrans cannot 9715 * measure RTT 9716 */ 9717 /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */ 9718 9719 /* For auto-close */ 9720 cnt_thru++; 9721 if (*now_filled == 0) { 9722 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent); 9723 *now = asoc->time_last_sent; 9724 *now_filled = 1; 9725 } else { 9726 asoc->time_last_sent = *now; 9727 } 9728 *cnt_out += bundle_at; 9729 #ifdef SCTP_AUDITING_ENABLED 9730 sctp_audit_log(0xC4, bundle_at); 9731 #endif 9732 if (bundle_at) { 9733 tsns_sent = data_list[0]->rec.data.TSN_seq; 9734 } 9735 for (i = 0; i < bundle_at; i++) { 9736 SCTP_STAT_INCR(sctps_sendretransdata); 9737 data_list[i]->sent = SCTP_DATAGRAM_SENT; 9738 /* 9739 * When we have a revoked data, and we 9740 * retransmit it, then we clear the revoked 9741 * flag since this flag dictates if we 9742 * subtracted from the fs 9743 */ 9744 if (data_list[i]->rec.data.chunk_was_revoked) { 9745 /* Deflate the cwnd */ 9746 data_list[i]->whoTo->cwnd -= data_list[i]->book_size; 9747 data_list[i]->rec.data.chunk_was_revoked = 0; 9748 } 9749 data_list[i]->snd_count++; 9750 sctp_ucount_decr(asoc->sent_queue_retran_cnt); 9751 /* record the time */ 9752 data_list[i]->sent_rcv_time = asoc->time_last_sent; 9753 if (data_list[i]->book_size_scale) { 9754 /* 9755 * need to double the book size on 9756 * this one 9757 */ 9758 data_list[i]->book_size_scale = 0; 9759 /* 9760 * Since we double the booksize, we 9761 * must also double the output queue 9762 * size, since this get shrunk when 9763 * we free by this amount. 9764 */ 9765 atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size); 9766 data_list[i]->book_size *= 2; 9767 9768 9769 } else { 9770 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) { 9771 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND, 9772 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)); 9773 } 9774 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd, 9775 (uint32_t) (data_list[i]->send_size + 9776 SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))); 9777 } 9778 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 9779 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND, 9780 data_list[i]->whoTo->flight_size, 9781 data_list[i]->book_size, 9782 (uintptr_t) data_list[i]->whoTo, 9783 data_list[i]->rec.data.TSN_seq); 9784 } 9785 sctp_flight_size_increase(data_list[i]); 9786 sctp_total_flight_increase(stcb, data_list[i]); 9787 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 9788 /* SWS sender side engages */ 9789 asoc->peers_rwnd = 0; 9790 } 9791 if ((i == 0) && 9792 (data_list[i]->rec.data.doing_fast_retransmit)) { 9793 SCTP_STAT_INCR(sctps_sendfastretrans); 9794 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) && 9795 (tmr_started == 0)) { 9796 /*- 9797 * ok we just fast-retrans'd 9798 * the lowest TSN, i.e the 9799 * first on the list. In 9800 * this case we want to give 9801 * some more time to get a 9802 * SACK back without a 9803 * t3-expiring. 9804 */ 9805 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net, 9806 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4); 9807 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); 9808 } 9809 } 9810 } 9811 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { 9812 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND); 9813 } 9814 #ifdef SCTP_AUDITING_ENABLED 9815 sctp_auditing(21, inp, stcb, NULL); 9816 #endif 9817 } else { 9818 /* None will fit */ 9819 return (1); 9820 } 9821 if (asoc->sent_queue_retran_cnt <= 0) { 9822 /* all done we have no more to retran */ 9823 asoc->sent_queue_retran_cnt = 0; 9824 break; 9825 } 9826 if (one_chunk) { 9827 /* No more room in rwnd */ 9828 return (1); 9829 } 9830 /* stop the for loop here. we sent out a packet */ 9831 break; 9832 } 9833 return (0); 9834 } 9835 9836 9837 static int 9838 sctp_timer_validation(struct sctp_inpcb *inp, 9839 struct sctp_tcb *stcb, 9840 struct sctp_association *asoc, 9841 int ret) 9842 { 9843 struct sctp_nets *net; 9844 9845 /* Validate that a timer is running somewhere */ 9846 TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 9847 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 9848 /* Here is a timer */ 9849 return (ret); 9850 } 9851 } 9852 SCTP_TCB_LOCK_ASSERT(stcb); 9853 /* Gak, we did not have a timer somewhere */ 9854 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n"); 9855 if (asoc->alternate) { 9856 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->alternate); 9857 } else { 9858 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination); 9859 } 9860 return (ret); 9861 } 9862 9863 void 9864 sctp_chunk_output(struct sctp_inpcb *inp, 9865 struct sctp_tcb *stcb, 9866 int from_where, 9867 int so_locked 9868 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 9869 SCTP_UNUSED 9870 #endif 9871 ) 9872 { 9873 /*- 9874 * Ok this is the generic chunk service queue. we must do the 9875 * following: 9876 * - See if there are retransmits pending, if so we must 9877 * do these first. 9878 * - Service the stream queue that is next, moving any 9879 * message (note I must get a complete message i.e. 9880 * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning 9881 * TSN's 9882 * - Check to see if the cwnd/rwnd allows any output, if so we 9883 * go ahead and fomulate and send the low level chunks. Making sure 9884 * to combine any control in the control chunk queue also. 9885 */ 9886 struct sctp_association *asoc; 9887 struct sctp_nets *net; 9888 int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0; 9889 unsigned int burst_cnt = 0; 9890 struct timeval now; 9891 int now_filled = 0; 9892 int nagle_on; 9893 int frag_point = sctp_get_frag_point(stcb, &stcb->asoc); 9894 int un_sent = 0; 9895 int fr_done; 9896 unsigned int tot_frs = 0; 9897 9898 asoc = &stcb->asoc; 9899 /* The Nagle algorithm is only applied when handling a send call. */ 9900 if (from_where == SCTP_OUTPUT_FROM_USR_SEND) { 9901 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) { 9902 nagle_on = 0; 9903 } else { 9904 nagle_on = 1; 9905 } 9906 } else { 9907 nagle_on = 0; 9908 } 9909 SCTP_TCB_LOCK_ASSERT(stcb); 9910 9911 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); 9912 9913 if ((un_sent <= 0) && 9914 (TAILQ_EMPTY(&asoc->control_send_queue)) && 9915 (TAILQ_EMPTY(&asoc->asconf_send_queue)) && 9916 (asoc->sent_queue_retran_cnt == 0)) { 9917 /* Nothing to do unless there is something to be sent left */ 9918 return; 9919 } 9920 /* 9921 * Do we have something to send, data or control AND a sack timer 9922 * running, if so piggy-back the sack. 9923 */ 9924 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) { 9925 sctp_send_sack(stcb, so_locked); 9926 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer); 9927 } 9928 while (asoc->sent_queue_retran_cnt) { 9929 /*- 9930 * Ok, it is retransmission time only, we send out only ONE 9931 * packet with a single call off to the retran code. 9932 */ 9933 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) { 9934 /*- 9935 * Special hook for handling cookiess discarded 9936 * by peer that carried data. Send cookie-ack only 9937 * and then the next call with get the retran's. 9938 */ 9939 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, 9940 from_where, 9941 &now, &now_filled, frag_point, so_locked); 9942 return; 9943 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) { 9944 /* if its not from a HB then do it */ 9945 fr_done = 0; 9946 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked); 9947 if (fr_done) { 9948 tot_frs++; 9949 } 9950 } else { 9951 /* 9952 * its from any other place, we don't allow retran 9953 * output (only control) 9954 */ 9955 ret = 1; 9956 } 9957 if (ret > 0) { 9958 /* Can't send anymore */ 9959 /*- 9960 * now lets push out control by calling med-level 9961 * output once. this assures that we WILL send HB's 9962 * if queued too. 9963 */ 9964 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, 9965 from_where, 9966 &now, &now_filled, frag_point, so_locked); 9967 #ifdef SCTP_AUDITING_ENABLED 9968 sctp_auditing(8, inp, stcb, NULL); 9969 #endif 9970 (void)sctp_timer_validation(inp, stcb, asoc, ret); 9971 return; 9972 } 9973 if (ret < 0) { 9974 /*- 9975 * The count was off.. retran is not happening so do 9976 * the normal retransmission. 9977 */ 9978 #ifdef SCTP_AUDITING_ENABLED 9979 sctp_auditing(9, inp, stcb, NULL); 9980 #endif 9981 if (ret == SCTP_RETRAN_EXIT) { 9982 return; 9983 } 9984 break; 9985 } 9986 if (from_where == SCTP_OUTPUT_FROM_T3) { 9987 /* Only one transmission allowed out of a timeout */ 9988 #ifdef SCTP_AUDITING_ENABLED 9989 sctp_auditing(10, inp, stcb, NULL); 9990 #endif 9991 /* Push out any control */ 9992 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where, 9993 &now, &now_filled, frag_point, so_locked); 9994 return; 9995 } 9996 if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) { 9997 /* Hit FR burst limit */ 9998 return; 9999 } 10000 if ((num_out == 0) && (ret == 0)) { 10001 /* No more retrans to send */ 10002 break; 10003 } 10004 } 10005 #ifdef SCTP_AUDITING_ENABLED 10006 sctp_auditing(12, inp, stcb, NULL); 10007 #endif 10008 /* Check for bad destinations, if they exist move chunks around. */ 10009 TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 10010 if (!(net->dest_state & SCTP_ADDR_REACHABLE)) { 10011 /*- 10012 * if possible move things off of this address we 10013 * still may send below due to the dormant state but 10014 * we try to find an alternate address to send to 10015 * and if we have one we move all queued data on the 10016 * out wheel to this alternate address. 10017 */ 10018 if (net->ref_count > 1) 10019 sctp_move_chunks_from_net(stcb, net); 10020 } else { 10021 /*- 10022 * if ((asoc->sat_network) || (net->addr_is_local)) 10023 * { burst_limit = asoc->max_burst * 10024 * SCTP_SAT_NETWORK_BURST_INCR; } 10025 */ 10026 if (asoc->max_burst > 0) { 10027 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) { 10028 if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) { 10029 /* 10030 * JRS - Use the congestion 10031 * control given in the 10032 * congestion control module 10033 */ 10034 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst); 10035 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) { 10036 sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED); 10037 } 10038 SCTP_STAT_INCR(sctps_maxburstqueued); 10039 } 10040 net->fast_retran_ip = 0; 10041 } else { 10042 if (net->flight_size == 0) { 10043 /* 10044 * Should be decaying the 10045 * cwnd here 10046 */ 10047 ; 10048 } 10049 } 10050 } 10051 } 10052 10053 } 10054 burst_cnt = 0; 10055 do { 10056 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out, 10057 &reason_code, 0, from_where, 10058 &now, &now_filled, frag_point, so_locked); 10059 if (error) { 10060 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error); 10061 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) { 10062 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP); 10063 } 10064 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { 10065 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES); 10066 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES); 10067 } 10068 break; 10069 } 10070 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out); 10071 10072 tot_out += num_out; 10073 burst_cnt++; 10074 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { 10075 sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES); 10076 if (num_out == 0) { 10077 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES); 10078 } 10079 } 10080 if (nagle_on) { 10081 /* 10082 * When the Nagle algorithm is used, look at how 10083 * much is unsent, then if its smaller than an MTU 10084 * and we have data in flight we stop, except if we 10085 * are handling a fragmented user message. 10086 */ 10087 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) + 10088 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk))); 10089 if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) && 10090 (stcb->asoc.total_flight > 0) && 10091 ((stcb->asoc.locked_on_sending == NULL) || 10092 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) { 10093 break; 10094 } 10095 } 10096 if (TAILQ_EMPTY(&asoc->control_send_queue) && 10097 TAILQ_EMPTY(&asoc->send_queue) && 10098 stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { 10099 /* Nothing left to send */ 10100 break; 10101 } 10102 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) { 10103 /* Nothing left to send */ 10104 break; 10105 } 10106 } while (num_out && 10107 ((asoc->max_burst == 0) || 10108 SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) || 10109 (burst_cnt < asoc->max_burst))); 10110 10111 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) { 10112 if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) { 10113 SCTP_STAT_INCR(sctps_maxburstqueued); 10114 asoc->burst_limit_applied = 1; 10115 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) { 10116 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED); 10117 } 10118 } else { 10119 asoc->burst_limit_applied = 0; 10120 } 10121 } 10122 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { 10123 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES); 10124 } 10125 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n", 10126 tot_out); 10127 10128 /*- 10129 * Now we need to clean up the control chunk chain if a ECNE is on 10130 * it. It must be marked as UNSENT again so next call will continue 10131 * to send it until such time that we get a CWR, to remove it. 10132 */ 10133 if (stcb->asoc.ecn_echo_cnt_onq) 10134 sctp_fix_ecn_echo(asoc); 10135 return; 10136 } 10137 10138 10139 int 10140 sctp_output(inp, m, addr, control, p, flags) 10141 struct sctp_inpcb *inp; 10142 struct mbuf *m; 10143 struct sockaddr *addr; 10144 struct mbuf *control; 10145 struct thread *p; 10146 int flags; 10147 { 10148 if (inp == NULL) { 10149 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); 10150 return (EINVAL); 10151 } 10152 if (inp->sctp_socket == NULL) { 10153 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); 10154 return (EINVAL); 10155 } 10156 return (sctp_sosend(inp->sctp_socket, 10157 addr, 10158 (struct uio *)NULL, 10159 m, 10160 control, 10161 flags, p 10162 )); 10163 } 10164 10165 void 10166 send_forward_tsn(struct sctp_tcb *stcb, 10167 struct sctp_association *asoc) 10168 { 10169 struct sctp_tmit_chunk *chk; 10170 struct sctp_forward_tsn_chunk *fwdtsn; 10171 uint32_t advance_peer_ack_point; 10172 10173 SCTP_TCB_LOCK_ASSERT(stcb); 10174 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { 10175 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) { 10176 /* mark it to unsent */ 10177 chk->sent = SCTP_DATAGRAM_UNSENT; 10178 chk->snd_count = 0; 10179 /* Do we correct its output location? */ 10180 if (chk->whoTo) { 10181 sctp_free_remote_addr(chk->whoTo); 10182 chk->whoTo = NULL; 10183 } 10184 goto sctp_fill_in_rest; 10185 } 10186 } 10187 /* Ok if we reach here we must build one */ 10188 sctp_alloc_a_chunk(stcb, chk); 10189 if (chk == NULL) { 10190 return; 10191 } 10192 asoc->fwd_tsn_cnt++; 10193 chk->copy_by_ref = 0; 10194 chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN; 10195 chk->rec.chunk_id.can_take_data = 0; 10196 chk->asoc = asoc; 10197 chk->whoTo = NULL; 10198 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 10199 if (chk->data == NULL) { 10200 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 10201 return; 10202 } 10203 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 10204 chk->sent = SCTP_DATAGRAM_UNSENT; 10205 chk->snd_count = 0; 10206 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next); 10207 asoc->ctrl_queue_cnt++; 10208 sctp_fill_in_rest: 10209 /*- 10210 * Here we go through and fill out the part that deals with 10211 * stream/seq of the ones we skip. 10212 */ 10213 SCTP_BUF_LEN(chk->data) = 0; 10214 { 10215 struct sctp_tmit_chunk *at, *tp1, *last; 10216 struct sctp_strseq *strseq; 10217 unsigned int cnt_of_space, i, ovh; 10218 unsigned int space_needed; 10219 unsigned int cnt_of_skipped = 0; 10220 10221 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) { 10222 if (at->sent != SCTP_FORWARD_TSN_SKIP) { 10223 /* no more to look at */ 10224 break; 10225 } 10226 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) { 10227 /* We don't report these */ 10228 continue; 10229 } 10230 cnt_of_skipped++; 10231 } 10232 space_needed = (sizeof(struct sctp_forward_tsn_chunk) + 10233 (cnt_of_skipped * sizeof(struct sctp_strseq))); 10234 10235 cnt_of_space = M_TRAILINGSPACE(chk->data); 10236 10237 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 10238 ovh = SCTP_MIN_OVERHEAD; 10239 } else { 10240 ovh = SCTP_MIN_V4_OVERHEAD; 10241 } 10242 if (cnt_of_space > (asoc->smallest_mtu - ovh)) { 10243 /* trim to a mtu size */ 10244 cnt_of_space = asoc->smallest_mtu - ovh; 10245 } 10246 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) { 10247 sctp_misc_ints(SCTP_FWD_TSN_CHECK, 10248 0xff, 0, cnt_of_skipped, 10249 asoc->advanced_peer_ack_point); 10250 10251 } 10252 advance_peer_ack_point = asoc->advanced_peer_ack_point; 10253 if (cnt_of_space < space_needed) { 10254 /*- 10255 * ok we must trim down the chunk by lowering the 10256 * advance peer ack point. 10257 */ 10258 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) { 10259 sctp_misc_ints(SCTP_FWD_TSN_CHECK, 10260 0xff, 0xff, cnt_of_space, 10261 space_needed); 10262 } 10263 cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk); 10264 cnt_of_skipped /= sizeof(struct sctp_strseq); 10265 /*- 10266 * Go through and find the TSN that will be the one 10267 * we report. 10268 */ 10269 at = TAILQ_FIRST(&asoc->sent_queue); 10270 for (i = 0; i < cnt_of_skipped; i++) { 10271 tp1 = TAILQ_NEXT(at, sctp_next); 10272 if (tp1 == NULL) { 10273 break; 10274 } 10275 at = tp1; 10276 } 10277 if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) { 10278 sctp_misc_ints(SCTP_FWD_TSN_CHECK, 10279 0xff, cnt_of_skipped, at->rec.data.TSN_seq, 10280 asoc->advanced_peer_ack_point); 10281 } 10282 last = at; 10283 /*- 10284 * last now points to last one I can report, update 10285 * peer ack point 10286 */ 10287 if (last) 10288 advance_peer_ack_point = last->rec.data.TSN_seq; 10289 space_needed = sizeof(struct sctp_forward_tsn_chunk) + 10290 cnt_of_skipped * sizeof(struct sctp_strseq); 10291 } 10292 chk->send_size = space_needed; 10293 /* Setup the chunk */ 10294 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *); 10295 fwdtsn->ch.chunk_length = htons(chk->send_size); 10296 fwdtsn->ch.chunk_flags = 0; 10297 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN; 10298 fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point); 10299 SCTP_BUF_LEN(chk->data) = chk->send_size; 10300 fwdtsn++; 10301 /*- 10302 * Move pointer to after the fwdtsn and transfer to the 10303 * strseq pointer. 10304 */ 10305 strseq = (struct sctp_strseq *)fwdtsn; 10306 /*- 10307 * Now populate the strseq list. This is done blindly 10308 * without pulling out duplicate stream info. This is 10309 * inefficent but won't harm the process since the peer will 10310 * look at these in sequence and will thus release anything. 10311 * It could mean we exceed the PMTU and chop off some that 10312 * we could have included.. but this is unlikely (aka 1432/4 10313 * would mean 300+ stream seq's would have to be reported in 10314 * one FWD-TSN. With a bit of work we can later FIX this to 10315 * optimize and pull out duplcates.. but it does add more 10316 * overhead. So for now... not! 10317 */ 10318 at = TAILQ_FIRST(&asoc->sent_queue); 10319 for (i = 0; i < cnt_of_skipped; i++) { 10320 tp1 = TAILQ_NEXT(at, sctp_next); 10321 if (tp1 == NULL) 10322 break; 10323 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) { 10324 /* We don't report these */ 10325 i--; 10326 at = tp1; 10327 continue; 10328 } 10329 if (at->rec.data.TSN_seq == advance_peer_ack_point) { 10330 at->rec.data.fwd_tsn_cnt = 0; 10331 } 10332 strseq->stream = ntohs(at->rec.data.stream_number); 10333 strseq->sequence = ntohs(at->rec.data.stream_seq); 10334 strseq++; 10335 at = tp1; 10336 } 10337 } 10338 return; 10339 10340 } 10341 10342 void 10343 sctp_send_sack(struct sctp_tcb *stcb, int so_locked 10344 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 10345 SCTP_UNUSED 10346 #endif 10347 ) 10348 { 10349 /*- 10350 * Queue up a SACK or NR-SACK in the control queue. 10351 * We must first check to see if a SACK or NR-SACK is 10352 * somehow on the control queue. 10353 * If so, we will take and and remove the old one. 10354 */ 10355 struct sctp_association *asoc; 10356 struct sctp_tmit_chunk *chk, *a_chk; 10357 struct sctp_sack_chunk *sack; 10358 struct sctp_nr_sack_chunk *nr_sack; 10359 struct sctp_gap_ack_block *gap_descriptor; 10360 struct sack_track *selector; 10361 int mergeable = 0; 10362 int offset; 10363 caddr_t limit; 10364 uint32_t *dup; 10365 int limit_reached = 0; 10366 unsigned int i, siz, j; 10367 unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space; 10368 int num_dups = 0; 10369 int space_req; 10370 uint32_t highest_tsn; 10371 uint8_t flags; 10372 uint8_t type; 10373 uint8_t tsn_map; 10374 10375 if ((stcb->asoc.sctp_nr_sack_on_off == 1) && 10376 (stcb->asoc.peer_supports_nr_sack == 1)) { 10377 type = SCTP_NR_SELECTIVE_ACK; 10378 } else { 10379 type = SCTP_SELECTIVE_ACK; 10380 } 10381 a_chk = NULL; 10382 asoc = &stcb->asoc; 10383 SCTP_TCB_LOCK_ASSERT(stcb); 10384 if (asoc->last_data_chunk_from == NULL) { 10385 /* Hmm we never received anything */ 10386 return; 10387 } 10388 sctp_slide_mapping_arrays(stcb); 10389 sctp_set_rwnd(stcb, asoc); 10390 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { 10391 if (chk->rec.chunk_id.id == type) { 10392 /* Hmm, found a sack already on queue, remove it */ 10393 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); 10394 asoc->ctrl_queue_cnt--; 10395 a_chk = chk; 10396 if (a_chk->data) { 10397 sctp_m_freem(a_chk->data); 10398 a_chk->data = NULL; 10399 } 10400 if (a_chk->whoTo) { 10401 sctp_free_remote_addr(a_chk->whoTo); 10402 a_chk->whoTo = NULL; 10403 } 10404 break; 10405 } 10406 } 10407 if (a_chk == NULL) { 10408 sctp_alloc_a_chunk(stcb, a_chk); 10409 if (a_chk == NULL) { 10410 /* No memory so we drop the idea, and set a timer */ 10411 if (stcb->asoc.delayed_ack) { 10412 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, 10413 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5); 10414 sctp_timer_start(SCTP_TIMER_TYPE_RECV, 10415 stcb->sctp_ep, stcb, NULL); 10416 } else { 10417 stcb->asoc.send_sack = 1; 10418 } 10419 return; 10420 } 10421 a_chk->copy_by_ref = 0; 10422 a_chk->rec.chunk_id.id = type; 10423 a_chk->rec.chunk_id.can_take_data = 1; 10424 } 10425 /* Clear our pkt counts */ 10426 asoc->data_pkts_seen = 0; 10427 10428 a_chk->asoc = asoc; 10429 a_chk->snd_count = 0; 10430 a_chk->send_size = 0; /* fill in later */ 10431 a_chk->sent = SCTP_DATAGRAM_UNSENT; 10432 a_chk->whoTo = NULL; 10433 10434 if ((asoc->numduptsns) || 10435 (!(asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE))) { 10436 /*- 10437 * Ok, we have some duplicates or the destination for the 10438 * sack is unreachable, lets see if we can select an 10439 * alternate than asoc->last_data_chunk_from 10440 */ 10441 if ((asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE) && 10442 (asoc->used_alt_onsack > asoc->numnets)) { 10443 /* We used an alt last time, don't this time */ 10444 a_chk->whoTo = NULL; 10445 } else { 10446 asoc->used_alt_onsack++; 10447 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0); 10448 } 10449 if (a_chk->whoTo == NULL) { 10450 /* Nope, no alternate */ 10451 a_chk->whoTo = asoc->last_data_chunk_from; 10452 asoc->used_alt_onsack = 0; 10453 } 10454 } else { 10455 /* 10456 * No duplicates so we use the last place we received data 10457 * from. 10458 */ 10459 asoc->used_alt_onsack = 0; 10460 a_chk->whoTo = asoc->last_data_chunk_from; 10461 } 10462 if (a_chk->whoTo) { 10463 atomic_add_int(&a_chk->whoTo->ref_count, 1); 10464 } 10465 if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) { 10466 highest_tsn = asoc->highest_tsn_inside_map; 10467 } else { 10468 highest_tsn = asoc->highest_tsn_inside_nr_map; 10469 } 10470 if (highest_tsn == asoc->cumulative_tsn) { 10471 /* no gaps */ 10472 if (type == SCTP_SELECTIVE_ACK) { 10473 space_req = sizeof(struct sctp_sack_chunk); 10474 } else { 10475 space_req = sizeof(struct sctp_nr_sack_chunk); 10476 } 10477 } else { 10478 /* gaps get a cluster */ 10479 space_req = MCLBYTES; 10480 } 10481 /* Ok now lets formulate a MBUF with our sack */ 10482 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA); 10483 if ((a_chk->data == NULL) || 10484 (a_chk->whoTo == NULL)) { 10485 /* rats, no mbuf memory */ 10486 if (a_chk->data) { 10487 /* was a problem with the destination */ 10488 sctp_m_freem(a_chk->data); 10489 a_chk->data = NULL; 10490 } 10491 sctp_free_a_chunk(stcb, a_chk, so_locked); 10492 /* sa_ignore NO_NULL_CHK */ 10493 if (stcb->asoc.delayed_ack) { 10494 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, 10495 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6); 10496 sctp_timer_start(SCTP_TIMER_TYPE_RECV, 10497 stcb->sctp_ep, stcb, NULL); 10498 } else { 10499 stcb->asoc.send_sack = 1; 10500 } 10501 return; 10502 } 10503 /* ok, lets go through and fill it in */ 10504 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD); 10505 space = M_TRAILINGSPACE(a_chk->data); 10506 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) { 10507 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD); 10508 } 10509 limit = mtod(a_chk->data, caddr_t); 10510 limit += space; 10511 10512 flags = 0; 10513 10514 if ((asoc->sctp_cmt_on_off > 0) && 10515 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) { 10516 /*- 10517 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been 10518 * received, then set high bit to 1, else 0. Reset 10519 * pkts_rcvd. 10520 */ 10521 flags |= (asoc->cmt_dac_pkts_rcvd << 6); 10522 asoc->cmt_dac_pkts_rcvd = 0; 10523 } 10524 #ifdef SCTP_ASOCLOG_OF_TSNS 10525 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn; 10526 stcb->asoc.cumack_log_atsnt++; 10527 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) { 10528 stcb->asoc.cumack_log_atsnt = 0; 10529 } 10530 #endif 10531 /* reset the readers interpretation */ 10532 stcb->freed_by_sorcv_sincelast = 0; 10533 10534 if (type == SCTP_SELECTIVE_ACK) { 10535 sack = mtod(a_chk->data, struct sctp_sack_chunk *); 10536 nr_sack = NULL; 10537 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk)); 10538 if (highest_tsn > asoc->mapping_array_base_tsn) { 10539 siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8; 10540 } else { 10541 siz = (((MAX_TSN - highest_tsn) + 1) + highest_tsn + 7) / 8; 10542 } 10543 } else { 10544 sack = NULL; 10545 nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *); 10546 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk)); 10547 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) { 10548 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8; 10549 } else { 10550 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8; 10551 } 10552 } 10553 10554 if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) { 10555 offset = 1; 10556 } else { 10557 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn; 10558 } 10559 if (((type == SCTP_SELECTIVE_ACK) && 10560 SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) || 10561 ((type == SCTP_NR_SELECTIVE_ACK) && 10562 SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) { 10563 /* we have a gap .. maybe */ 10564 for (i = 0; i < siz; i++) { 10565 tsn_map = asoc->mapping_array[i]; 10566 if (type == SCTP_SELECTIVE_ACK) { 10567 tsn_map |= asoc->nr_mapping_array[i]; 10568 } 10569 if (i == 0) { 10570 /* 10571 * Clear all bits corresponding to TSNs 10572 * smaller or equal to the cumulative TSN. 10573 */ 10574 tsn_map &= (~0 << (1 - offset)); 10575 } 10576 selector = &sack_array[tsn_map]; 10577 if (mergeable && selector->right_edge) { 10578 /* 10579 * Backup, left and right edges were ok to 10580 * merge. 10581 */ 10582 num_gap_blocks--; 10583 gap_descriptor--; 10584 } 10585 if (selector->num_entries == 0) 10586 mergeable = 0; 10587 else { 10588 for (j = 0; j < selector->num_entries; j++) { 10589 if (mergeable && selector->right_edge) { 10590 /* 10591 * do a merge by NOT setting 10592 * the left side 10593 */ 10594 mergeable = 0; 10595 } else { 10596 /* 10597 * no merge, set the left 10598 * side 10599 */ 10600 mergeable = 0; 10601 gap_descriptor->start = htons((selector->gaps[j].start + offset)); 10602 } 10603 gap_descriptor->end = htons((selector->gaps[j].end + offset)); 10604 num_gap_blocks++; 10605 gap_descriptor++; 10606 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) { 10607 /* no more room */ 10608 limit_reached = 1; 10609 break; 10610 } 10611 } 10612 if (selector->left_edge) { 10613 mergeable = 1; 10614 } 10615 } 10616 if (limit_reached) { 10617 /* Reached the limit stop */ 10618 break; 10619 } 10620 offset += 8; 10621 } 10622 } 10623 if ((type == SCTP_NR_SELECTIVE_ACK) && 10624 (limit_reached == 0)) { 10625 10626 mergeable = 0; 10627 10628 if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) { 10629 siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8; 10630 } else { 10631 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8; 10632 } 10633 10634 if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) { 10635 offset = 1; 10636 } else { 10637 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn; 10638 } 10639 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) { 10640 /* we have a gap .. maybe */ 10641 for (i = 0; i < siz; i++) { 10642 tsn_map = asoc->nr_mapping_array[i]; 10643 if (i == 0) { 10644 /* 10645 * Clear all bits corresponding to 10646 * TSNs smaller or equal to the 10647 * cumulative TSN. 10648 */ 10649 tsn_map &= (~0 << (1 - offset)); 10650 } 10651 selector = &sack_array[tsn_map]; 10652 if (mergeable && selector->right_edge) { 10653 /* 10654 * Backup, left and right edges were 10655 * ok to merge. 10656 */ 10657 num_nr_gap_blocks--; 10658 gap_descriptor--; 10659 } 10660 if (selector->num_entries == 0) 10661 mergeable = 0; 10662 else { 10663 for (j = 0; j < selector->num_entries; j++) { 10664 if (mergeable && selector->right_edge) { 10665 /* 10666 * do a merge by NOT 10667 * setting the left 10668 * side 10669 */ 10670 mergeable = 0; 10671 } else { 10672 /* 10673 * no merge, set the 10674 * left side 10675 */ 10676 mergeable = 0; 10677 gap_descriptor->start = htons((selector->gaps[j].start + offset)); 10678 } 10679 gap_descriptor->end = htons((selector->gaps[j].end + offset)); 10680 num_nr_gap_blocks++; 10681 gap_descriptor++; 10682 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) { 10683 /* no more room */ 10684 limit_reached = 1; 10685 break; 10686 } 10687 } 10688 if (selector->left_edge) { 10689 mergeable = 1; 10690 } 10691 } 10692 if (limit_reached) { 10693 /* Reached the limit stop */ 10694 break; 10695 } 10696 offset += 8; 10697 } 10698 } 10699 } 10700 /* now we must add any dups we are going to report. */ 10701 if ((limit_reached == 0) && (asoc->numduptsns)) { 10702 dup = (uint32_t *) gap_descriptor; 10703 for (i = 0; i < asoc->numduptsns; i++) { 10704 *dup = htonl(asoc->dup_tsns[i]); 10705 dup++; 10706 num_dups++; 10707 if (((caddr_t)dup + sizeof(uint32_t)) > limit) { 10708 /* no more room */ 10709 break; 10710 } 10711 } 10712 asoc->numduptsns = 0; 10713 } 10714 /* 10715 * now that the chunk is prepared queue it to the control chunk 10716 * queue. 10717 */ 10718 if (type == SCTP_SELECTIVE_ACK) { 10719 a_chk->send_size = sizeof(struct sctp_sack_chunk) + 10720 (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) + 10721 num_dups * sizeof(int32_t); 10722 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size; 10723 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn); 10724 sack->sack.a_rwnd = htonl(asoc->my_rwnd); 10725 sack->sack.num_gap_ack_blks = htons(num_gap_blocks); 10726 sack->sack.num_dup_tsns = htons(num_dups); 10727 sack->ch.chunk_type = type; 10728 sack->ch.chunk_flags = flags; 10729 sack->ch.chunk_length = htons(a_chk->send_size); 10730 } else { 10731 a_chk->send_size = sizeof(struct sctp_nr_sack_chunk) + 10732 (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) + 10733 num_dups * sizeof(int32_t); 10734 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size; 10735 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn); 10736 nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd); 10737 nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks); 10738 nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks); 10739 nr_sack->nr_sack.num_dup_tsns = htons(num_dups); 10740 nr_sack->nr_sack.reserved = 0; 10741 nr_sack->ch.chunk_type = type; 10742 nr_sack->ch.chunk_flags = flags; 10743 nr_sack->ch.chunk_length = htons(a_chk->send_size); 10744 } 10745 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next); 10746 asoc->my_last_reported_rwnd = asoc->my_rwnd; 10747 asoc->ctrl_queue_cnt++; 10748 asoc->send_sack = 0; 10749 SCTP_STAT_INCR(sctps_sendsacks); 10750 return; 10751 } 10752 10753 void 10754 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked 10755 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 10756 SCTP_UNUSED 10757 #endif 10758 ) 10759 { 10760 struct mbuf *m_abort; 10761 struct mbuf *m_out = NULL, *m_end = NULL; 10762 struct sctp_abort_chunk *abort = NULL; 10763 int sz; 10764 uint32_t auth_offset = 0; 10765 struct sctp_auth_chunk *auth = NULL; 10766 struct sctp_nets *net; 10767 10768 /*- 10769 * Add an AUTH chunk, if chunk requires it and save the offset into 10770 * the chain for AUTH 10771 */ 10772 if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION, 10773 stcb->asoc.peer_auth_chunks)) { 10774 m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset, 10775 stcb, SCTP_ABORT_ASSOCIATION); 10776 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); 10777 } 10778 SCTP_TCB_LOCK_ASSERT(stcb); 10779 m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER); 10780 if (m_abort == NULL) { 10781 /* no mbuf's */ 10782 if (m_out) 10783 sctp_m_freem(m_out); 10784 return; 10785 } 10786 /* link in any error */ 10787 SCTP_BUF_NEXT(m_abort) = operr; 10788 sz = 0; 10789 if (operr) { 10790 struct mbuf *n; 10791 10792 n = operr; 10793 while (n) { 10794 sz += SCTP_BUF_LEN(n); 10795 n = SCTP_BUF_NEXT(n); 10796 } 10797 } 10798 SCTP_BUF_LEN(m_abort) = sizeof(*abort); 10799 if (m_out == NULL) { 10800 /* NO Auth chunk prepended, so reserve space in front */ 10801 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD); 10802 m_out = m_abort; 10803 } else { 10804 /* Put AUTH chunk at the front of the chain */ 10805 SCTP_BUF_NEXT(m_end) = m_abort; 10806 } 10807 if (stcb->asoc.alternate) { 10808 net = stcb->asoc.alternate; 10809 } else { 10810 net = stcb->asoc.primary_destination; 10811 } 10812 /* fill in the ABORT chunk */ 10813 abort = mtod(m_abort, struct sctp_abort_chunk *); 10814 abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION; 10815 abort->ch.chunk_flags = 0; 10816 abort->ch.chunk_length = htons(sizeof(*abort) + sz); 10817 10818 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net, 10819 (struct sockaddr *)&net->ro._l_addr, 10820 m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, NULL, 0, 10821 stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag), 10822 stcb->asoc.primary_destination->port, so_locked, NULL, NULL); 10823 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); 10824 } 10825 10826 void 10827 sctp_send_shutdown_complete(struct sctp_tcb *stcb, 10828 struct sctp_nets *net, 10829 int reflect_vtag) 10830 { 10831 /* formulate and SEND a SHUTDOWN-COMPLETE */ 10832 struct mbuf *m_shutdown_comp; 10833 struct sctp_shutdown_complete_chunk *shutdown_complete; 10834 uint32_t vtag; 10835 uint8_t flags; 10836 10837 m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER); 10838 if (m_shutdown_comp == NULL) { 10839 /* no mbuf's */ 10840 return; 10841 } 10842 if (reflect_vtag) { 10843 flags = SCTP_HAD_NO_TCB; 10844 vtag = stcb->asoc.my_vtag; 10845 } else { 10846 flags = 0; 10847 vtag = stcb->asoc.peer_vtag; 10848 } 10849 shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *); 10850 shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE; 10851 shutdown_complete->ch.chunk_flags = flags; 10852 shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk)); 10853 SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk); 10854 (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net, 10855 (struct sockaddr *)&net->ro._l_addr, 10856 m_shutdown_comp, 0, NULL, 0, 1, 0, NULL, 0, 10857 stcb->sctp_ep->sctp_lport, stcb->rport, 10858 htonl(vtag), 10859 net->port, SCTP_SO_NOT_LOCKED, NULL, NULL); 10860 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); 10861 return; 10862 } 10863 10864 void 10865 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh, 10866 uint32_t vrf_id, uint16_t port) 10867 { 10868 /* formulate and SEND a SHUTDOWN-COMPLETE */ 10869 struct mbuf *o_pak; 10870 struct mbuf *mout; 10871 struct ip *iph; 10872 struct udphdr *udp = NULL; 10873 int offset_out, len, mlen; 10874 struct sctp_shutdown_complete_msg *comp_cp; 10875 10876 #ifdef INET 10877 struct ip *iph_out; 10878 10879 #endif 10880 #ifdef INET6 10881 struct ip6_hdr *ip6, *ip6_out; 10882 10883 #endif 10884 10885 iph = mtod(m, struct ip *); 10886 switch (iph->ip_v) { 10887 #ifdef INET 10888 case IPVERSION: 10889 len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg)); 10890 break; 10891 #endif 10892 #ifdef INET6 10893 case IPV6_VERSION >> 4: 10894 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg)); 10895 break; 10896 #endif 10897 default: 10898 return; 10899 } 10900 if (port) { 10901 len += sizeof(struct udphdr); 10902 } 10903 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA); 10904 if (mout == NULL) { 10905 return; 10906 } 10907 SCTP_BUF_RESV_UF(mout, max_linkhdr); 10908 SCTP_BUF_LEN(mout) = len; 10909 SCTP_BUF_NEXT(mout) = NULL; 10910 if (m->m_flags & M_FLOWID) { 10911 mout->m_pkthdr.flowid = m->m_pkthdr.flowid; 10912 mout->m_flags |= M_FLOWID; 10913 } 10914 #ifdef INET 10915 iph_out = NULL; 10916 #endif 10917 #ifdef INET6 10918 ip6_out = NULL; 10919 #endif 10920 offset_out = 0; 10921 10922 switch (iph->ip_v) { 10923 #ifdef INET 10924 case IPVERSION: 10925 iph_out = mtod(mout, struct ip *); 10926 10927 /* Fill in the IP header for the ABORT */ 10928 iph_out->ip_v = IPVERSION; 10929 iph_out->ip_hl = (sizeof(struct ip) / 4); 10930 iph_out->ip_tos = (u_char)0; 10931 iph_out->ip_id = 0; 10932 iph_out->ip_off = 0; 10933 iph_out->ip_ttl = MAXTTL; 10934 if (port) { 10935 iph_out->ip_p = IPPROTO_UDP; 10936 } else { 10937 iph_out->ip_p = IPPROTO_SCTP; 10938 } 10939 iph_out->ip_src.s_addr = iph->ip_dst.s_addr; 10940 iph_out->ip_dst.s_addr = iph->ip_src.s_addr; 10941 10942 /* let IP layer calculate this */ 10943 iph_out->ip_sum = 0; 10944 offset_out += sizeof(*iph_out); 10945 comp_cp = (struct sctp_shutdown_complete_msg *)( 10946 (caddr_t)iph_out + offset_out); 10947 break; 10948 #endif 10949 #ifdef INET6 10950 case IPV6_VERSION >> 4: 10951 ip6 = (struct ip6_hdr *)iph; 10952 ip6_out = mtod(mout, struct ip6_hdr *); 10953 10954 /* Fill in the IPv6 header for the ABORT */ 10955 ip6_out->ip6_flow = ip6->ip6_flow; 10956 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim); 10957 if (port) { 10958 ip6_out->ip6_nxt = IPPROTO_UDP; 10959 } else { 10960 ip6_out->ip6_nxt = IPPROTO_SCTP; 10961 } 10962 ip6_out->ip6_src = ip6->ip6_dst; 10963 ip6_out->ip6_dst = ip6->ip6_src; 10964 /* 10965 * ?? The old code had both the iph len + payload, I think 10966 * this is wrong and would never have worked 10967 */ 10968 ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg); 10969 offset_out += sizeof(*ip6_out); 10970 comp_cp = (struct sctp_shutdown_complete_msg *)( 10971 (caddr_t)ip6_out + offset_out); 10972 break; 10973 #endif /* INET6 */ 10974 default: 10975 /* Currently not supported. */ 10976 sctp_m_freem(mout); 10977 return; 10978 } 10979 if (port) { 10980 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) { 10981 sctp_m_freem(mout); 10982 return; 10983 } 10984 udp = (struct udphdr *)comp_cp; 10985 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)); 10986 udp->uh_dport = port; 10987 udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr)); 10988 #ifdef INET 10989 if (iph_out) 10990 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP)); 10991 #endif 10992 offset_out += sizeof(struct udphdr); 10993 comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr)); 10994 } 10995 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) { 10996 /* no mbuf's */ 10997 sctp_m_freem(mout); 10998 return; 10999 } 11000 /* Now copy in and fill in the ABORT tags etc. */ 11001 comp_cp->sh.src_port = sh->dest_port; 11002 comp_cp->sh.dest_port = sh->src_port; 11003 comp_cp->sh.checksum = 0; 11004 comp_cp->sh.v_tag = sh->v_tag; 11005 comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB; 11006 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE; 11007 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk)); 11008 11009 #ifdef INET 11010 if (iph_out != NULL) { 11011 sctp_route_t ro; 11012 int ret; 11013 11014 mlen = SCTP_BUF_LEN(mout); 11015 bzero(&ro, sizeof ro); 11016 /* set IPv4 length */ 11017 iph_out->ip_len = mlen; 11018 #ifdef SCTP_PACKET_LOGGING 11019 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 11020 sctp_packet_log(mout, mlen); 11021 #endif 11022 if (port) { 11023 #if defined(SCTP_WITH_NO_CSUM) 11024 SCTP_STAT_INCR(sctps_sendnocrc); 11025 #else 11026 comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out); 11027 SCTP_STAT_INCR(sctps_sendswcrc); 11028 #endif 11029 SCTP_ENABLE_UDP_CSUM(mout); 11030 } else { 11031 #if defined(SCTP_WITH_NO_CSUM) 11032 SCTP_STAT_INCR(sctps_sendnocrc); 11033 #else 11034 mout->m_pkthdr.csum_flags = CSUM_SCTP; 11035 mout->m_pkthdr.csum_data = 0; 11036 SCTP_STAT_INCR(sctps_sendhwcrc); 11037 #endif 11038 } 11039 SCTP_ATTACH_CHAIN(o_pak, mout, mlen); 11040 /* out it goes */ 11041 SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id); 11042 11043 /* Free the route if we got one back */ 11044 if (ro.ro_rt) 11045 RTFREE(ro.ro_rt); 11046 } 11047 #endif 11048 #ifdef INET6 11049 if (ip6_out != NULL) { 11050 struct route_in6 ro; 11051 int ret; 11052 struct ifnet *ifp = NULL; 11053 11054 bzero(&ro, sizeof(ro)); 11055 mlen = SCTP_BUF_LEN(mout); 11056 #ifdef SCTP_PACKET_LOGGING 11057 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 11058 sctp_packet_log(mout, mlen); 11059 #endif 11060 SCTP_ATTACH_CHAIN(o_pak, mout, mlen); 11061 if (port) { 11062 #if defined(SCTP_WITH_NO_CSUM) 11063 SCTP_STAT_INCR(sctps_sendnocrc); 11064 #else 11065 comp_cp->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr)); 11066 SCTP_STAT_INCR(sctps_sendswcrc); 11067 #endif 11068 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), mlen - sizeof(struct ip6_hdr))) == 0) { 11069 udp->uh_sum = 0xffff; 11070 } 11071 } else { 11072 #if defined(SCTP_WITH_NO_CSUM) 11073 SCTP_STAT_INCR(sctps_sendnocrc); 11074 #else 11075 mout->m_pkthdr.csum_flags = CSUM_SCTP; 11076 mout->m_pkthdr.csum_data = 0; 11077 SCTP_STAT_INCR(sctps_sendhwcrc); 11078 #endif 11079 } 11080 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id); 11081 11082 /* Free the route if we got one back */ 11083 if (ro.ro_rt) 11084 RTFREE(ro.ro_rt); 11085 } 11086 #endif 11087 SCTP_STAT_INCR(sctps_sendpackets); 11088 SCTP_STAT_INCR_COUNTER64(sctps_outpackets); 11089 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); 11090 return; 11091 11092 } 11093 11094 void 11095 sctp_send_hb(struct sctp_tcb *stcb, struct sctp_nets *net, int so_locked 11096 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 11097 SCTP_UNUSED 11098 #endif 11099 ) 11100 { 11101 struct sctp_tmit_chunk *chk; 11102 struct sctp_heartbeat_chunk *hb; 11103 struct timeval now; 11104 11105 SCTP_TCB_LOCK_ASSERT(stcb); 11106 if (net == NULL) { 11107 return; 11108 } 11109 (void)SCTP_GETTIME_TIMEVAL(&now); 11110 switch (net->ro._l_addr.sa.sa_family) { 11111 #ifdef INET 11112 case AF_INET: 11113 break; 11114 #endif 11115 #ifdef INET6 11116 case AF_INET6: 11117 break; 11118 #endif 11119 default: 11120 return; 11121 } 11122 sctp_alloc_a_chunk(stcb, chk); 11123 if (chk == NULL) { 11124 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n"); 11125 return; 11126 } 11127 chk->copy_by_ref = 0; 11128 chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST; 11129 chk->rec.chunk_id.can_take_data = 1; 11130 chk->asoc = &stcb->asoc; 11131 chk->send_size = sizeof(struct sctp_heartbeat_chunk); 11132 11133 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER); 11134 if (chk->data == NULL) { 11135 sctp_free_a_chunk(stcb, chk, so_locked); 11136 return; 11137 } 11138 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 11139 SCTP_BUF_LEN(chk->data) = chk->send_size; 11140 chk->sent = SCTP_DATAGRAM_UNSENT; 11141 chk->snd_count = 0; 11142 chk->whoTo = net; 11143 atomic_add_int(&chk->whoTo->ref_count, 1); 11144 /* Now we have a mbuf that we can fill in with the details */ 11145 hb = mtod(chk->data, struct sctp_heartbeat_chunk *); 11146 memset(hb, 0, sizeof(struct sctp_heartbeat_chunk)); 11147 /* fill out chunk header */ 11148 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST; 11149 hb->ch.chunk_flags = 0; 11150 hb->ch.chunk_length = htons(chk->send_size); 11151 /* Fill out hb parameter */ 11152 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO); 11153 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param)); 11154 hb->heartbeat.hb_info.time_value_1 = now.tv_sec; 11155 hb->heartbeat.hb_info.time_value_2 = now.tv_usec; 11156 /* Did our user request this one, put it in */ 11157 hb->heartbeat.hb_info.addr_family = net->ro._l_addr.sa.sa_family; 11158 hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len; 11159 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { 11160 /* 11161 * we only take from the entropy pool if the address is not 11162 * confirmed. 11163 */ 11164 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep); 11165 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep); 11166 } else { 11167 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0; 11168 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0; 11169 } 11170 switch (net->ro._l_addr.sa.sa_family) { 11171 #ifdef INET 11172 case AF_INET: 11173 memcpy(hb->heartbeat.hb_info.address, 11174 &net->ro._l_addr.sin.sin_addr, 11175 sizeof(net->ro._l_addr.sin.sin_addr)); 11176 break; 11177 #endif 11178 #ifdef INET6 11179 case AF_INET6: 11180 memcpy(hb->heartbeat.hb_info.address, 11181 &net->ro._l_addr.sin6.sin6_addr, 11182 sizeof(net->ro._l_addr.sin6.sin6_addr)); 11183 break; 11184 #endif 11185 default: 11186 return; 11187 break; 11188 } 11189 net->hb_responded = 0; 11190 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next); 11191 stcb->asoc.ctrl_queue_cnt++; 11192 SCTP_STAT_INCR(sctps_sendheartbeat); 11193 return; 11194 } 11195 11196 void 11197 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net, 11198 uint32_t high_tsn) 11199 { 11200 struct sctp_association *asoc; 11201 struct sctp_ecne_chunk *ecne; 11202 struct sctp_tmit_chunk *chk; 11203 11204 if (net == NULL) { 11205 return; 11206 } 11207 asoc = &stcb->asoc; 11208 SCTP_TCB_LOCK_ASSERT(stcb); 11209 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { 11210 if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) { 11211 /* found a previous ECN_ECHO update it if needed */ 11212 uint32_t cnt, ctsn; 11213 11214 ecne = mtod(chk->data, struct sctp_ecne_chunk *); 11215 ctsn = ntohl(ecne->tsn); 11216 if (SCTP_TSN_GT(high_tsn, ctsn)) { 11217 ecne->tsn = htonl(high_tsn); 11218 SCTP_STAT_INCR(sctps_queue_upd_ecne); 11219 } 11220 cnt = ntohl(ecne->num_pkts_since_cwr); 11221 cnt++; 11222 ecne->num_pkts_since_cwr = htonl(cnt); 11223 return; 11224 } 11225 } 11226 /* nope could not find one to update so we must build one */ 11227 sctp_alloc_a_chunk(stcb, chk); 11228 if (chk == NULL) { 11229 return; 11230 } 11231 chk->copy_by_ref = 0; 11232 SCTP_STAT_INCR(sctps_queue_upd_ecne); 11233 chk->rec.chunk_id.id = SCTP_ECN_ECHO; 11234 chk->rec.chunk_id.can_take_data = 0; 11235 chk->asoc = &stcb->asoc; 11236 chk->send_size = sizeof(struct sctp_ecne_chunk); 11237 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER); 11238 if (chk->data == NULL) { 11239 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 11240 return; 11241 } 11242 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 11243 SCTP_BUF_LEN(chk->data) = chk->send_size; 11244 chk->sent = SCTP_DATAGRAM_UNSENT; 11245 chk->snd_count = 0; 11246 chk->whoTo = net; 11247 atomic_add_int(&chk->whoTo->ref_count, 1); 11248 11249 stcb->asoc.ecn_echo_cnt_onq++; 11250 ecne = mtod(chk->data, struct sctp_ecne_chunk *); 11251 ecne->ch.chunk_type = SCTP_ECN_ECHO; 11252 ecne->ch.chunk_flags = 0; 11253 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk)); 11254 ecne->tsn = htonl(high_tsn); 11255 ecne->num_pkts_since_cwr = htonl(1); 11256 TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next); 11257 asoc->ctrl_queue_cnt++; 11258 } 11259 11260 void 11261 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net, 11262 struct mbuf *m, int iphlen, int bad_crc) 11263 { 11264 struct sctp_association *asoc; 11265 struct sctp_pktdrop_chunk *drp; 11266 struct sctp_tmit_chunk *chk; 11267 uint8_t *datap; 11268 int len; 11269 int was_trunc = 0; 11270 struct ip *iph; 11271 11272 #ifdef INET6 11273 struct ip6_hdr *ip6h; 11274 11275 #endif 11276 int fullsz = 0, extra = 0; 11277 long spc; 11278 int offset; 11279 struct sctp_chunkhdr *ch, chunk_buf; 11280 unsigned int chk_length; 11281 11282 if (!stcb) { 11283 return; 11284 } 11285 asoc = &stcb->asoc; 11286 SCTP_TCB_LOCK_ASSERT(stcb); 11287 if (asoc->peer_supports_pktdrop == 0) { 11288 /*- 11289 * peer must declare support before I send one. 11290 */ 11291 return; 11292 } 11293 if (stcb->sctp_socket == NULL) { 11294 return; 11295 } 11296 sctp_alloc_a_chunk(stcb, chk); 11297 if (chk == NULL) { 11298 return; 11299 } 11300 chk->copy_by_ref = 0; 11301 iph = mtod(m, struct ip *); 11302 if (iph == NULL) { 11303 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 11304 return; 11305 } 11306 switch (iph->ip_v) { 11307 #ifdef INET 11308 case IPVERSION: 11309 /* IPv4 */ 11310 len = chk->send_size = iph->ip_len; 11311 break; 11312 #endif 11313 #ifdef INET6 11314 case IPV6_VERSION >> 4: 11315 /* IPv6 */ 11316 ip6h = mtod(m, struct ip6_hdr *); 11317 len = chk->send_size = htons(ip6h->ip6_plen); 11318 break; 11319 #endif 11320 default: 11321 return; 11322 } 11323 /* Validate that we do not have an ABORT in here. */ 11324 offset = iphlen + sizeof(struct sctphdr); 11325 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, 11326 sizeof(*ch), (uint8_t *) & chunk_buf); 11327 while (ch != NULL) { 11328 chk_length = ntohs(ch->chunk_length); 11329 if (chk_length < sizeof(*ch)) { 11330 /* break to abort land */ 11331 break; 11332 } 11333 switch (ch->chunk_type) { 11334 case SCTP_PACKET_DROPPED: 11335 case SCTP_ABORT_ASSOCIATION: 11336 case SCTP_INITIATION_ACK: 11337 /** 11338 * We don't respond with an PKT-DROP to an ABORT 11339 * or PKT-DROP. We also do not respond to an 11340 * INIT-ACK, because we can't know if the initiation 11341 * tag is correct or not. 11342 */ 11343 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 11344 return; 11345 default: 11346 break; 11347 } 11348 offset += SCTP_SIZE32(chk_length); 11349 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, 11350 sizeof(*ch), (uint8_t *) & chunk_buf); 11351 } 11352 11353 if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) > 11354 min(stcb->asoc.smallest_mtu, MCLBYTES)) { 11355 /* 11356 * only send 1 mtu worth, trim off the excess on the end. 11357 */ 11358 fullsz = len - extra; 11359 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD; 11360 was_trunc = 1; 11361 } 11362 chk->asoc = &stcb->asoc; 11363 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 11364 if (chk->data == NULL) { 11365 jump_out: 11366 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 11367 return; 11368 } 11369 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 11370 drp = mtod(chk->data, struct sctp_pktdrop_chunk *); 11371 if (drp == NULL) { 11372 sctp_m_freem(chk->data); 11373 chk->data = NULL; 11374 goto jump_out; 11375 } 11376 chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) + 11377 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD)); 11378 chk->book_size_scale = 0; 11379 if (was_trunc) { 11380 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED; 11381 drp->trunc_len = htons(fullsz); 11382 /* 11383 * Len is already adjusted to size minus overhead above take 11384 * out the pkt_drop chunk itself from it. 11385 */ 11386 chk->send_size = len - sizeof(struct sctp_pktdrop_chunk); 11387 len = chk->send_size; 11388 } else { 11389 /* no truncation needed */ 11390 drp->ch.chunk_flags = 0; 11391 drp->trunc_len = htons(0); 11392 } 11393 if (bad_crc) { 11394 drp->ch.chunk_flags |= SCTP_BADCRC; 11395 } 11396 chk->send_size += sizeof(struct sctp_pktdrop_chunk); 11397 SCTP_BUF_LEN(chk->data) = chk->send_size; 11398 chk->sent = SCTP_DATAGRAM_UNSENT; 11399 chk->snd_count = 0; 11400 if (net) { 11401 /* we should hit here */ 11402 chk->whoTo = net; 11403 atomic_add_int(&chk->whoTo->ref_count, 1); 11404 } else { 11405 chk->whoTo = NULL; 11406 } 11407 chk->rec.chunk_id.id = SCTP_PACKET_DROPPED; 11408 chk->rec.chunk_id.can_take_data = 1; 11409 drp->ch.chunk_type = SCTP_PACKET_DROPPED; 11410 drp->ch.chunk_length = htons(chk->send_size); 11411 spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket); 11412 if (spc < 0) { 11413 spc = 0; 11414 } 11415 drp->bottle_bw = htonl(spc); 11416 if (asoc->my_rwnd) { 11417 drp->current_onq = htonl(asoc->size_on_reasm_queue + 11418 asoc->size_on_all_streams + 11419 asoc->my_rwnd_control_len + 11420 stcb->sctp_socket->so_rcv.sb_cc); 11421 } else { 11422 /*- 11423 * If my rwnd is 0, possibly from mbuf depletion as well as 11424 * space used, tell the peer there is NO space aka onq == bw 11425 */ 11426 drp->current_onq = htonl(spc); 11427 } 11428 drp->reserved = 0; 11429 datap = drp->data; 11430 m_copydata(m, iphlen, len, (caddr_t)datap); 11431 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next); 11432 asoc->ctrl_queue_cnt++; 11433 } 11434 11435 void 11436 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override) 11437 { 11438 struct sctp_association *asoc; 11439 struct sctp_cwr_chunk *cwr; 11440 struct sctp_tmit_chunk *chk; 11441 11442 asoc = &stcb->asoc; 11443 SCTP_TCB_LOCK_ASSERT(stcb); 11444 if (net == NULL) { 11445 return; 11446 } 11447 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { 11448 if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) { 11449 /* 11450 * found a previous CWR queued to same destination 11451 * update it if needed 11452 */ 11453 uint32_t ctsn; 11454 11455 cwr = mtod(chk->data, struct sctp_cwr_chunk *); 11456 ctsn = ntohl(cwr->tsn); 11457 if (SCTP_TSN_GT(high_tsn, ctsn)) { 11458 cwr->tsn = htonl(high_tsn); 11459 } 11460 if (override & SCTP_CWR_REDUCE_OVERRIDE) { 11461 /* Make sure override is carried */ 11462 cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE; 11463 } 11464 return; 11465 } 11466 } 11467 sctp_alloc_a_chunk(stcb, chk); 11468 if (chk == NULL) { 11469 return; 11470 } 11471 chk->copy_by_ref = 0; 11472 chk->rec.chunk_id.id = SCTP_ECN_CWR; 11473 chk->rec.chunk_id.can_take_data = 1; 11474 chk->asoc = &stcb->asoc; 11475 chk->send_size = sizeof(struct sctp_cwr_chunk); 11476 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER); 11477 if (chk->data == NULL) { 11478 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 11479 return; 11480 } 11481 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 11482 SCTP_BUF_LEN(chk->data) = chk->send_size; 11483 chk->sent = SCTP_DATAGRAM_UNSENT; 11484 chk->snd_count = 0; 11485 chk->whoTo = net; 11486 atomic_add_int(&chk->whoTo->ref_count, 1); 11487 cwr = mtod(chk->data, struct sctp_cwr_chunk *); 11488 cwr->ch.chunk_type = SCTP_ECN_CWR; 11489 cwr->ch.chunk_flags = override; 11490 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk)); 11491 cwr->tsn = htonl(high_tsn); 11492 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next); 11493 asoc->ctrl_queue_cnt++; 11494 } 11495 11496 void 11497 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk, 11498 int number_entries, uint16_t * list, 11499 uint32_t seq, uint32_t resp_seq, uint32_t last_sent) 11500 { 11501 int len, old_len, i; 11502 struct sctp_stream_reset_out_request *req_out; 11503 struct sctp_chunkhdr *ch; 11504 11505 ch = mtod(chk->data, struct sctp_chunkhdr *); 11506 11507 11508 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); 11509 11510 /* get to new offset for the param. */ 11511 req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len); 11512 /* now how long will this param be? */ 11513 len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries)); 11514 req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST); 11515 req_out->ph.param_length = htons(len); 11516 req_out->request_seq = htonl(seq); 11517 req_out->response_seq = htonl(resp_seq); 11518 req_out->send_reset_at_tsn = htonl(last_sent); 11519 if (number_entries) { 11520 for (i = 0; i < number_entries; i++) { 11521 req_out->list_of_streams[i] = htons(list[i]); 11522 } 11523 } 11524 if (SCTP_SIZE32(len) > len) { 11525 /*- 11526 * Need to worry about the pad we may end up adding to the 11527 * end. This is easy since the struct is either aligned to 4 11528 * bytes or 2 bytes off. 11529 */ 11530 req_out->list_of_streams[number_entries] = 0; 11531 } 11532 /* now fix the chunk length */ 11533 ch->chunk_length = htons(len + old_len); 11534 chk->book_size = len + old_len; 11535 chk->book_size_scale = 0; 11536 chk->send_size = SCTP_SIZE32(chk->book_size); 11537 SCTP_BUF_LEN(chk->data) = chk->send_size; 11538 return; 11539 } 11540 11541 11542 void 11543 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk, 11544 int number_entries, uint16_t * list, 11545 uint32_t seq) 11546 { 11547 int len, old_len, i; 11548 struct sctp_stream_reset_in_request *req_in; 11549 struct sctp_chunkhdr *ch; 11550 11551 ch = mtod(chk->data, struct sctp_chunkhdr *); 11552 11553 11554 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); 11555 11556 /* get to new offset for the param. */ 11557 req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len); 11558 /* now how long will this param be? */ 11559 len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries)); 11560 req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST); 11561 req_in->ph.param_length = htons(len); 11562 req_in->request_seq = htonl(seq); 11563 if (number_entries) { 11564 for (i = 0; i < number_entries; i++) { 11565 req_in->list_of_streams[i] = htons(list[i]); 11566 } 11567 } 11568 if (SCTP_SIZE32(len) > len) { 11569 /*- 11570 * Need to worry about the pad we may end up adding to the 11571 * end. This is easy since the struct is either aligned to 4 11572 * bytes or 2 bytes off. 11573 */ 11574 req_in->list_of_streams[number_entries] = 0; 11575 } 11576 /* now fix the chunk length */ 11577 ch->chunk_length = htons(len + old_len); 11578 chk->book_size = len + old_len; 11579 chk->book_size_scale = 0; 11580 chk->send_size = SCTP_SIZE32(chk->book_size); 11581 SCTP_BUF_LEN(chk->data) = chk->send_size; 11582 return; 11583 } 11584 11585 11586 void 11587 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk, 11588 uint32_t seq) 11589 { 11590 int len, old_len; 11591 struct sctp_stream_reset_tsn_request *req_tsn; 11592 struct sctp_chunkhdr *ch; 11593 11594 ch = mtod(chk->data, struct sctp_chunkhdr *); 11595 11596 11597 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); 11598 11599 /* get to new offset for the param. */ 11600 req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len); 11601 /* now how long will this param be? */ 11602 len = sizeof(struct sctp_stream_reset_tsn_request); 11603 req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST); 11604 req_tsn->ph.param_length = htons(len); 11605 req_tsn->request_seq = htonl(seq); 11606 11607 /* now fix the chunk length */ 11608 ch->chunk_length = htons(len + old_len); 11609 chk->send_size = len + old_len; 11610 chk->book_size = SCTP_SIZE32(chk->send_size); 11611 chk->book_size_scale = 0; 11612 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 11613 return; 11614 } 11615 11616 void 11617 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk, 11618 uint32_t resp_seq, uint32_t result) 11619 { 11620 int len, old_len; 11621 struct sctp_stream_reset_response *resp; 11622 struct sctp_chunkhdr *ch; 11623 11624 ch = mtod(chk->data, struct sctp_chunkhdr *); 11625 11626 11627 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); 11628 11629 /* get to new offset for the param. */ 11630 resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len); 11631 /* now how long will this param be? */ 11632 len = sizeof(struct sctp_stream_reset_response); 11633 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE); 11634 resp->ph.param_length = htons(len); 11635 resp->response_seq = htonl(resp_seq); 11636 resp->result = ntohl(result); 11637 11638 /* now fix the chunk length */ 11639 ch->chunk_length = htons(len + old_len); 11640 chk->book_size = len + old_len; 11641 chk->book_size_scale = 0; 11642 chk->send_size = SCTP_SIZE32(chk->book_size); 11643 SCTP_BUF_LEN(chk->data) = chk->send_size; 11644 return; 11645 11646 } 11647 11648 11649 void 11650 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk, 11651 uint32_t resp_seq, uint32_t result, 11652 uint32_t send_una, uint32_t recv_next) 11653 { 11654 int len, old_len; 11655 struct sctp_stream_reset_response_tsn *resp; 11656 struct sctp_chunkhdr *ch; 11657 11658 ch = mtod(chk->data, struct sctp_chunkhdr *); 11659 11660 11661 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); 11662 11663 /* get to new offset for the param. */ 11664 resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len); 11665 /* now how long will this param be? */ 11666 len = sizeof(struct sctp_stream_reset_response_tsn); 11667 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE); 11668 resp->ph.param_length = htons(len); 11669 resp->response_seq = htonl(resp_seq); 11670 resp->result = htonl(result); 11671 resp->senders_next_tsn = htonl(send_una); 11672 resp->receivers_next_tsn = htonl(recv_next); 11673 11674 /* now fix the chunk length */ 11675 ch->chunk_length = htons(len + old_len); 11676 chk->book_size = len + old_len; 11677 chk->send_size = SCTP_SIZE32(chk->book_size); 11678 chk->book_size_scale = 0; 11679 SCTP_BUF_LEN(chk->data) = chk->send_size; 11680 return; 11681 } 11682 11683 static void 11684 sctp_add_a_stream(struct sctp_tmit_chunk *chk, 11685 uint32_t seq, 11686 uint16_t adding) 11687 { 11688 int len, old_len; 11689 struct sctp_chunkhdr *ch; 11690 struct sctp_stream_reset_add_strm *addstr; 11691 11692 ch = mtod(chk->data, struct sctp_chunkhdr *); 11693 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); 11694 11695 /* get to new offset for the param. */ 11696 addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len); 11697 /* now how long will this param be? */ 11698 len = sizeof(struct sctp_stream_reset_add_strm); 11699 11700 /* Fill it out. */ 11701 addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_STREAMS); 11702 addstr->ph.param_length = htons(len); 11703 addstr->request_seq = htonl(seq); 11704 addstr->number_of_streams = htons(adding); 11705 addstr->reserved = 0; 11706 11707 /* now fix the chunk length */ 11708 ch->chunk_length = htons(len + old_len); 11709 chk->send_size = len + old_len; 11710 chk->book_size = SCTP_SIZE32(chk->send_size); 11711 chk->book_size_scale = 0; 11712 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); 11713 return; 11714 } 11715 11716 int 11717 sctp_send_str_reset_req(struct sctp_tcb *stcb, 11718 int number_entries, uint16_t * list, 11719 uint8_t send_out_req, 11720 uint32_t resp_seq, 11721 uint8_t send_in_req, 11722 uint8_t send_tsn_req, 11723 uint8_t add_stream, 11724 uint16_t adding 11725 ) 11726 { 11727 11728 struct sctp_association *asoc; 11729 struct sctp_tmit_chunk *chk; 11730 struct sctp_chunkhdr *ch; 11731 uint32_t seq; 11732 11733 asoc = &stcb->asoc; 11734 if (asoc->stream_reset_outstanding) { 11735 /*- 11736 * Already one pending, must get ACK back to clear the flag. 11737 */ 11738 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY); 11739 return (EBUSY); 11740 } 11741 if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) && 11742 (add_stream == 0)) { 11743 /* nothing to do */ 11744 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); 11745 return (EINVAL); 11746 } 11747 if (send_tsn_req && (send_out_req || send_in_req)) { 11748 /* error, can't do that */ 11749 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); 11750 return (EINVAL); 11751 } 11752 sctp_alloc_a_chunk(stcb, chk); 11753 if (chk == NULL) { 11754 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 11755 return (ENOMEM); 11756 } 11757 chk->copy_by_ref = 0; 11758 chk->rec.chunk_id.id = SCTP_STREAM_RESET; 11759 chk->rec.chunk_id.can_take_data = 0; 11760 chk->asoc = &stcb->asoc; 11761 chk->book_size = sizeof(struct sctp_chunkhdr); 11762 chk->send_size = SCTP_SIZE32(chk->book_size); 11763 chk->book_size_scale = 0; 11764 11765 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 11766 if (chk->data == NULL) { 11767 sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED); 11768 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 11769 return (ENOMEM); 11770 } 11771 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); 11772 11773 /* setup chunk parameters */ 11774 chk->sent = SCTP_DATAGRAM_UNSENT; 11775 chk->snd_count = 0; 11776 if (stcb->asoc.alternate) { 11777 chk->whoTo = stcb->asoc.alternate; 11778 } else { 11779 chk->whoTo = stcb->asoc.primary_destination; 11780 } 11781 atomic_add_int(&chk->whoTo->ref_count, 1); 11782 ch = mtod(chk->data, struct sctp_chunkhdr *); 11783 ch->chunk_type = SCTP_STREAM_RESET; 11784 ch->chunk_flags = 0; 11785 ch->chunk_length = htons(chk->book_size); 11786 SCTP_BUF_LEN(chk->data) = chk->send_size; 11787 11788 seq = stcb->asoc.str_reset_seq_out; 11789 if (send_out_req) { 11790 sctp_add_stream_reset_out(chk, number_entries, list, 11791 seq, resp_seq, (stcb->asoc.sending_seq - 1)); 11792 asoc->stream_reset_out_is_outstanding = 1; 11793 seq++; 11794 asoc->stream_reset_outstanding++; 11795 } 11796 if (add_stream) { 11797 sctp_add_a_stream(chk, seq, adding); 11798 seq++; 11799 asoc->stream_reset_outstanding++; 11800 } 11801 if (send_in_req) { 11802 sctp_add_stream_reset_in(chk, number_entries, list, seq); 11803 asoc->stream_reset_outstanding++; 11804 } 11805 if (send_tsn_req) { 11806 sctp_add_stream_reset_tsn(chk, seq); 11807 asoc->stream_reset_outstanding++; 11808 } 11809 asoc->str_reset = chk; 11810 11811 /* insert the chunk for sending */ 11812 TAILQ_INSERT_TAIL(&asoc->control_send_queue, 11813 chk, 11814 sctp_next); 11815 asoc->ctrl_queue_cnt++; 11816 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); 11817 return (0); 11818 } 11819 11820 void 11821 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag, 11822 struct mbuf *err_cause, uint32_t vrf_id, uint16_t port) 11823 { 11824 /*- 11825 * Formulate the abort message, and send it back down. 11826 */ 11827 struct mbuf *o_pak; 11828 struct mbuf *mout; 11829 struct sctp_abort_msg *abm; 11830 struct ip *iph; 11831 struct udphdr *udp; 11832 int iphlen_out, len; 11833 11834 #ifdef INET 11835 struct ip *iph_out; 11836 11837 #endif 11838 #ifdef INET6 11839 struct ip6_hdr *ip6, *ip6_out; 11840 11841 #endif 11842 11843 /* don't respond to ABORT with ABORT */ 11844 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) { 11845 if (err_cause) 11846 sctp_m_freem(err_cause); 11847 return; 11848 } 11849 iph = mtod(m, struct ip *); 11850 switch (iph->ip_v) { 11851 #ifdef INET 11852 case IPVERSION: 11853 len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg)); 11854 break; 11855 #endif 11856 #ifdef INET6 11857 case IPV6_VERSION >> 4: 11858 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg)); 11859 break; 11860 #endif 11861 default: 11862 if (err_cause) { 11863 sctp_m_freem(err_cause); 11864 } 11865 return; 11866 } 11867 if (port) { 11868 len += sizeof(struct udphdr); 11869 } 11870 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA); 11871 if (mout == NULL) { 11872 if (err_cause) { 11873 sctp_m_freem(err_cause); 11874 } 11875 return; 11876 } 11877 SCTP_BUF_RESV_UF(mout, max_linkhdr); 11878 SCTP_BUF_LEN(mout) = len; 11879 SCTP_BUF_NEXT(mout) = err_cause; 11880 if (m->m_flags & M_FLOWID) { 11881 mout->m_pkthdr.flowid = m->m_pkthdr.flowid; 11882 mout->m_flags |= M_FLOWID; 11883 } 11884 #ifdef INET 11885 iph_out = NULL; 11886 #endif 11887 #ifdef INET6 11888 ip6_out = NULL; 11889 #endif 11890 switch (iph->ip_v) { 11891 #ifdef INET 11892 case IPVERSION: 11893 iph_out = mtod(mout, struct ip *); 11894 11895 /* Fill in the IP header for the ABORT */ 11896 iph_out->ip_v = IPVERSION; 11897 iph_out->ip_hl = (sizeof(struct ip) / 4); 11898 iph_out->ip_tos = (u_char)0; 11899 iph_out->ip_id = 0; 11900 iph_out->ip_off = 0; 11901 iph_out->ip_ttl = MAXTTL; 11902 if (port) { 11903 iph_out->ip_p = IPPROTO_UDP; 11904 } else { 11905 iph_out->ip_p = IPPROTO_SCTP; 11906 } 11907 iph_out->ip_src.s_addr = iph->ip_dst.s_addr; 11908 iph_out->ip_dst.s_addr = iph->ip_src.s_addr; 11909 /* let IP layer calculate this */ 11910 iph_out->ip_sum = 0; 11911 11912 iphlen_out = sizeof(*iph_out); 11913 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out); 11914 break; 11915 #endif 11916 #ifdef INET6 11917 case IPV6_VERSION >> 4: 11918 ip6 = (struct ip6_hdr *)iph; 11919 ip6_out = mtod(mout, struct ip6_hdr *); 11920 11921 /* Fill in the IP6 header for the ABORT */ 11922 ip6_out->ip6_flow = ip6->ip6_flow; 11923 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim); 11924 if (port) { 11925 ip6_out->ip6_nxt = IPPROTO_UDP; 11926 } else { 11927 ip6_out->ip6_nxt = IPPROTO_SCTP; 11928 } 11929 ip6_out->ip6_src = ip6->ip6_dst; 11930 ip6_out->ip6_dst = ip6->ip6_src; 11931 11932 iphlen_out = sizeof(*ip6_out); 11933 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out); 11934 break; 11935 #endif /* INET6 */ 11936 default: 11937 /* Currently not supported */ 11938 sctp_m_freem(mout); 11939 return; 11940 } 11941 11942 udp = (struct udphdr *)abm; 11943 if (port) { 11944 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) { 11945 sctp_m_freem(mout); 11946 return; 11947 } 11948 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)); 11949 udp->uh_dport = port; 11950 /* set udp->uh_ulen later */ 11951 udp->uh_sum = 0; 11952 iphlen_out += sizeof(struct udphdr); 11953 abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr)); 11954 } 11955 abm->sh.src_port = sh->dest_port; 11956 abm->sh.dest_port = sh->src_port; 11957 abm->sh.checksum = 0; 11958 if (vtag == 0) { 11959 abm->sh.v_tag = sh->v_tag; 11960 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB; 11961 } else { 11962 abm->sh.v_tag = htonl(vtag); 11963 abm->msg.ch.chunk_flags = 0; 11964 } 11965 abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION; 11966 11967 if (err_cause) { 11968 struct mbuf *m_tmp = err_cause; 11969 int err_len = 0; 11970 11971 /* get length of the err_cause chain */ 11972 while (m_tmp != NULL) { 11973 err_len += SCTP_BUF_LEN(m_tmp); 11974 m_tmp = SCTP_BUF_NEXT(m_tmp); 11975 } 11976 len = SCTP_BUF_LEN(mout) + err_len; 11977 if (err_len % 4) { 11978 /* need pad at end of chunk */ 11979 uint32_t cpthis = 0; 11980 int padlen; 11981 11982 padlen = 4 - (len % 4); 11983 m_copyback(mout, len, padlen, (caddr_t)&cpthis); 11984 len += padlen; 11985 } 11986 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len); 11987 } else { 11988 len = SCTP_BUF_LEN(mout); 11989 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch)); 11990 } 11991 11992 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) { 11993 /* no mbuf's */ 11994 sctp_m_freem(mout); 11995 return; 11996 } 11997 #ifdef INET 11998 if (iph_out != NULL) { 11999 sctp_route_t ro; 12000 int ret; 12001 12002 /* zap the stack pointer to the route */ 12003 bzero(&ro, sizeof ro); 12004 if (port) { 12005 udp->uh_ulen = htons(len - sizeof(struct ip)); 12006 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP)); 12007 } 12008 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n"); 12009 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh); 12010 /* set IPv4 length */ 12011 iph_out->ip_len = len; 12012 /* out it goes */ 12013 #ifdef SCTP_PACKET_LOGGING 12014 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 12015 sctp_packet_log(mout, len); 12016 #endif 12017 SCTP_ATTACH_CHAIN(o_pak, mout, len); 12018 if (port) { 12019 #if defined(SCTP_WITH_NO_CSUM) 12020 SCTP_STAT_INCR(sctps_sendnocrc); 12021 #else 12022 abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out); 12023 SCTP_STAT_INCR(sctps_sendswcrc); 12024 #endif 12025 SCTP_ENABLE_UDP_CSUM(o_pak); 12026 } else { 12027 #if defined(SCTP_WITH_NO_CSUM) 12028 SCTP_STAT_INCR(sctps_sendnocrc); 12029 #else 12030 mout->m_pkthdr.csum_flags = CSUM_SCTP; 12031 mout->m_pkthdr.csum_data = 0; 12032 SCTP_STAT_INCR(sctps_sendhwcrc); 12033 #endif 12034 } 12035 SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id); 12036 12037 /* Free the route if we got one back */ 12038 if (ro.ro_rt) 12039 RTFREE(ro.ro_rt); 12040 } 12041 #endif 12042 #ifdef INET6 12043 if (ip6_out != NULL) { 12044 struct route_in6 ro; 12045 int ret; 12046 struct ifnet *ifp = NULL; 12047 12048 /* zap the stack pointer to the route */ 12049 bzero(&ro, sizeof(ro)); 12050 if (port) { 12051 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr)); 12052 } 12053 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n"); 12054 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh); 12055 ip6_out->ip6_plen = len - sizeof(*ip6_out); 12056 #ifdef SCTP_PACKET_LOGGING 12057 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 12058 sctp_packet_log(mout, len); 12059 #endif 12060 SCTP_ATTACH_CHAIN(o_pak, mout, len); 12061 if (port) { 12062 #if defined(SCTP_WITH_NO_CSUM) 12063 SCTP_STAT_INCR(sctps_sendnocrc); 12064 #else 12065 abm->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr)); 12066 SCTP_STAT_INCR(sctps_sendswcrc); 12067 #endif 12068 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) { 12069 udp->uh_sum = 0xffff; 12070 } 12071 } else { 12072 #if defined(SCTP_WITH_NO_CSUM) 12073 SCTP_STAT_INCR(sctps_sendnocrc); 12074 #else 12075 mout->m_pkthdr.csum_flags = CSUM_SCTP; 12076 mout->m_pkthdr.csum_data = 0; 12077 SCTP_STAT_INCR(sctps_sendhwcrc); 12078 #endif 12079 } 12080 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id); 12081 12082 /* Free the route if we got one back */ 12083 if (ro.ro_rt) 12084 RTFREE(ro.ro_rt); 12085 } 12086 #endif 12087 SCTP_STAT_INCR(sctps_sendpackets); 12088 SCTP_STAT_INCR_COUNTER64(sctps_outpackets); 12089 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); 12090 } 12091 12092 void 12093 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag, 12094 uint32_t vrf_id, uint16_t port) 12095 { 12096 struct mbuf *o_pak; 12097 struct sctphdr *sh, *sh_out; 12098 struct sctp_chunkhdr *ch; 12099 struct ip *iph; 12100 struct udphdr *udp = NULL; 12101 struct mbuf *mout; 12102 int iphlen_out, len; 12103 12104 #ifdef INET 12105 struct ip *iph_out; 12106 12107 #endif 12108 #ifdef INET6 12109 struct ip6_hdr *ip6, *ip6_out; 12110 12111 #endif 12112 12113 iph = mtod(m, struct ip *); 12114 sh = (struct sctphdr *)((caddr_t)iph + iphlen); 12115 switch (iph->ip_v) { 12116 #ifdef INET 12117 case IPVERSION: 12118 len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)); 12119 break; 12120 #endif 12121 #ifdef INET6 12122 case IPV6_VERSION >> 4: 12123 len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)); 12124 break; 12125 #endif 12126 default: 12127 if (scm) { 12128 sctp_m_freem(scm); 12129 } 12130 return; 12131 } 12132 if (port) { 12133 len += sizeof(struct udphdr); 12134 } 12135 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA); 12136 if (mout == NULL) { 12137 if (scm) { 12138 sctp_m_freem(scm); 12139 } 12140 return; 12141 } 12142 SCTP_BUF_RESV_UF(mout, max_linkhdr); 12143 SCTP_BUF_LEN(mout) = len; 12144 SCTP_BUF_NEXT(mout) = scm; 12145 if (m->m_flags & M_FLOWID) { 12146 mout->m_pkthdr.flowid = m->m_pkthdr.flowid; 12147 mout->m_flags |= M_FLOWID; 12148 } 12149 #ifdef INET 12150 iph_out = NULL; 12151 #endif 12152 #ifdef INET6 12153 ip6_out = NULL; 12154 #endif 12155 switch (iph->ip_v) { 12156 #ifdef INET 12157 case IPVERSION: 12158 iph_out = mtod(mout, struct ip *); 12159 12160 /* Fill in the IP header for the ABORT */ 12161 iph_out->ip_v = IPVERSION; 12162 iph_out->ip_hl = (sizeof(struct ip) / 4); 12163 iph_out->ip_tos = (u_char)0; 12164 iph_out->ip_id = 0; 12165 iph_out->ip_off = 0; 12166 iph_out->ip_ttl = MAXTTL; 12167 if (port) { 12168 iph_out->ip_p = IPPROTO_UDP; 12169 } else { 12170 iph_out->ip_p = IPPROTO_SCTP; 12171 } 12172 iph_out->ip_src.s_addr = iph->ip_dst.s_addr; 12173 iph_out->ip_dst.s_addr = iph->ip_src.s_addr; 12174 /* let IP layer calculate this */ 12175 iph_out->ip_sum = 0; 12176 12177 iphlen_out = sizeof(struct ip); 12178 sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out); 12179 break; 12180 #endif 12181 #ifdef INET6 12182 case IPV6_VERSION >> 4: 12183 ip6 = (struct ip6_hdr *)iph; 12184 ip6_out = mtod(mout, struct ip6_hdr *); 12185 12186 /* Fill in the IP6 header for the ABORT */ 12187 ip6_out->ip6_flow = ip6->ip6_flow; 12188 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim); 12189 if (port) { 12190 ip6_out->ip6_nxt = IPPROTO_UDP; 12191 } else { 12192 ip6_out->ip6_nxt = IPPROTO_SCTP; 12193 } 12194 ip6_out->ip6_src = ip6->ip6_dst; 12195 ip6_out->ip6_dst = ip6->ip6_src; 12196 12197 iphlen_out = sizeof(struct ip6_hdr); 12198 sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out); 12199 break; 12200 #endif /* INET6 */ 12201 default: 12202 /* Currently not supported */ 12203 sctp_m_freem(mout); 12204 return; 12205 } 12206 12207 udp = (struct udphdr *)sh_out; 12208 if (port) { 12209 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) { 12210 sctp_m_freem(mout); 12211 return; 12212 } 12213 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)); 12214 udp->uh_dport = port; 12215 /* set udp->uh_ulen later */ 12216 udp->uh_sum = 0; 12217 iphlen_out += sizeof(struct udphdr); 12218 sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr)); 12219 } 12220 sh_out->src_port = sh->dest_port; 12221 sh_out->dest_port = sh->src_port; 12222 sh_out->v_tag = vtag; 12223 sh_out->checksum = 0; 12224 12225 ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr)); 12226 ch->chunk_type = SCTP_OPERATION_ERROR; 12227 ch->chunk_flags = 0; 12228 12229 if (scm) { 12230 struct mbuf *m_tmp = scm; 12231 int cause_len = 0; 12232 12233 /* get length of the err_cause chain */ 12234 while (m_tmp != NULL) { 12235 cause_len += SCTP_BUF_LEN(m_tmp); 12236 m_tmp = SCTP_BUF_NEXT(m_tmp); 12237 } 12238 len = SCTP_BUF_LEN(mout) + cause_len; 12239 if (cause_len % 4) { 12240 /* need pad at end of chunk */ 12241 uint32_t cpthis = 0; 12242 int padlen; 12243 12244 padlen = 4 - (len % 4); 12245 m_copyback(mout, len, padlen, (caddr_t)&cpthis); 12246 len += padlen; 12247 } 12248 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len); 12249 } else { 12250 len = SCTP_BUF_LEN(mout); 12251 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr)); 12252 } 12253 12254 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) { 12255 /* no mbuf's */ 12256 sctp_m_freem(mout); 12257 return; 12258 } 12259 #ifdef INET 12260 if (iph_out != NULL) { 12261 sctp_route_t ro; 12262 int ret; 12263 12264 /* zap the stack pointer to the route */ 12265 bzero(&ro, sizeof ro); 12266 if (port) { 12267 udp->uh_ulen = htons(len - sizeof(struct ip)); 12268 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP)); 12269 } 12270 /* set IPv4 length */ 12271 iph_out->ip_len = len; 12272 /* out it goes */ 12273 #ifdef SCTP_PACKET_LOGGING 12274 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 12275 sctp_packet_log(mout, len); 12276 #endif 12277 SCTP_ATTACH_CHAIN(o_pak, mout, len); 12278 if (port) { 12279 #if defined(SCTP_WITH_NO_CSUM) 12280 SCTP_STAT_INCR(sctps_sendnocrc); 12281 #else 12282 sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out); 12283 SCTP_STAT_INCR(sctps_sendswcrc); 12284 #endif 12285 SCTP_ENABLE_UDP_CSUM(o_pak); 12286 } else { 12287 #if defined(SCTP_WITH_NO_CSUM) 12288 SCTP_STAT_INCR(sctps_sendnocrc); 12289 #else 12290 mout->m_pkthdr.csum_flags = CSUM_SCTP; 12291 mout->m_pkthdr.csum_data = 0; 12292 SCTP_STAT_INCR(sctps_sendhwcrc); 12293 #endif 12294 } 12295 SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id); 12296 12297 /* Free the route if we got one back */ 12298 if (ro.ro_rt) 12299 RTFREE(ro.ro_rt); 12300 } 12301 #endif 12302 #ifdef INET6 12303 if (ip6_out != NULL) { 12304 struct route_in6 ro; 12305 int ret; 12306 struct ifnet *ifp = NULL; 12307 12308 /* zap the stack pointer to the route */ 12309 bzero(&ro, sizeof(ro)); 12310 if (port) { 12311 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr)); 12312 } 12313 ip6_out->ip6_plen = len - sizeof(*ip6_out); 12314 #ifdef SCTP_PACKET_LOGGING 12315 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) 12316 sctp_packet_log(mout, len); 12317 #endif 12318 SCTP_ATTACH_CHAIN(o_pak, mout, len); 12319 if (port) { 12320 #if defined(SCTP_WITH_NO_CSUM) 12321 SCTP_STAT_INCR(sctps_sendnocrc); 12322 #else 12323 sh_out->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr)); 12324 SCTP_STAT_INCR(sctps_sendswcrc); 12325 #endif 12326 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) { 12327 udp->uh_sum = 0xffff; 12328 } 12329 } else { 12330 #if defined(SCTP_WITH_NO_CSUM) 12331 SCTP_STAT_INCR(sctps_sendnocrc); 12332 #else 12333 mout->m_pkthdr.csum_flags = CSUM_SCTP; 12334 mout->m_pkthdr.csum_data = 0; 12335 SCTP_STAT_INCR(sctps_sendhwcrc); 12336 #endif 12337 } 12338 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id); 12339 12340 /* Free the route if we got one back */ 12341 if (ro.ro_rt) 12342 RTFREE(ro.ro_rt); 12343 } 12344 #endif 12345 SCTP_STAT_INCR(sctps_sendpackets); 12346 SCTP_STAT_INCR_COUNTER64(sctps_outpackets); 12347 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); 12348 } 12349 12350 static struct mbuf * 12351 sctp_copy_resume(struct sctp_stream_queue_pending *sp, 12352 struct uio *uio, 12353 struct sctp_sndrcvinfo *srcv, 12354 int max_send_len, 12355 int user_marks_eor, 12356 int *error, 12357 uint32_t * sndout, 12358 struct mbuf **new_tail) 12359 { 12360 struct mbuf *m; 12361 12362 m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0, 12363 (M_PKTHDR | (user_marks_eor ? M_EOR : 0))); 12364 if (m == NULL) { 12365 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 12366 *error = ENOMEM; 12367 } else { 12368 *sndout = m_length(m, NULL); 12369 *new_tail = m_last(m); 12370 } 12371 return (m); 12372 } 12373 12374 static int 12375 sctp_copy_one(struct sctp_stream_queue_pending *sp, 12376 struct uio *uio, 12377 int resv_upfront) 12378 { 12379 int left; 12380 12381 left = sp->length; 12382 sp->data = m_uiotombuf(uio, M_WAITOK, sp->length, 12383 resv_upfront, 0); 12384 if (sp->data == NULL) { 12385 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 12386 return (ENOMEM); 12387 } 12388 sp->tail_mbuf = m_last(sp->data); 12389 return (0); 12390 } 12391 12392 12393 12394 static struct sctp_stream_queue_pending * 12395 sctp_copy_it_in(struct sctp_tcb *stcb, 12396 struct sctp_association *asoc, 12397 struct sctp_sndrcvinfo *srcv, 12398 struct uio *uio, 12399 struct sctp_nets *net, 12400 int max_send_len, 12401 int user_marks_eor, 12402 int *error, 12403 int non_blocking) 12404 { 12405 /*- 12406 * This routine must be very careful in its work. Protocol 12407 * processing is up and running so care must be taken to spl...() 12408 * when you need to do something that may effect the stcb/asoc. The 12409 * sb is locked however. When data is copied the protocol processing 12410 * should be enabled since this is a slower operation... 12411 */ 12412 struct sctp_stream_queue_pending *sp = NULL; 12413 int resv_in_first; 12414 12415 *error = 0; 12416 /* Now can we send this? */ 12417 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) || 12418 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) || 12419 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) || 12420 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) { 12421 /* got data while shutting down */ 12422 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); 12423 *error = ECONNRESET; 12424 goto out_now; 12425 } 12426 sctp_alloc_a_strmoq(stcb, sp); 12427 if (sp == NULL) { 12428 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 12429 *error = ENOMEM; 12430 goto out_now; 12431 } 12432 sp->act_flags = 0; 12433 sp->sender_all_done = 0; 12434 sp->sinfo_flags = srcv->sinfo_flags; 12435 sp->timetolive = srcv->sinfo_timetolive; 12436 sp->ppid = srcv->sinfo_ppid; 12437 sp->context = srcv->sinfo_context; 12438 sp->strseq = 0; 12439 (void)SCTP_GETTIME_TIMEVAL(&sp->ts); 12440 12441 sp->stream = srcv->sinfo_stream; 12442 sp->length = min(uio->uio_resid, max_send_len); 12443 if ((sp->length == (uint32_t) uio->uio_resid) && 12444 ((user_marks_eor == 0) || 12445 (srcv->sinfo_flags & SCTP_EOF) || 12446 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) { 12447 sp->msg_is_complete = 1; 12448 } else { 12449 sp->msg_is_complete = 0; 12450 } 12451 sp->sender_all_done = 0; 12452 sp->some_taken = 0; 12453 sp->put_last_out = 0; 12454 resv_in_first = sizeof(struct sctp_data_chunk); 12455 sp->data = sp->tail_mbuf = NULL; 12456 if (sp->length == 0) { 12457 *error = 0; 12458 goto skip_copy; 12459 } 12460 if (srcv->sinfo_keynumber_valid) { 12461 sp->auth_keyid = srcv->sinfo_keynumber; 12462 } else { 12463 sp->auth_keyid = stcb->asoc.authinfo.active_keyid; 12464 } 12465 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) { 12466 sctp_auth_key_acquire(stcb, sp->auth_keyid); 12467 sp->holds_key_ref = 1; 12468 } 12469 *error = sctp_copy_one(sp, uio, resv_in_first); 12470 skip_copy: 12471 if (*error) { 12472 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED); 12473 sp = NULL; 12474 } else { 12475 if (sp->sinfo_flags & SCTP_ADDR_OVER) { 12476 sp->net = net; 12477 atomic_add_int(&sp->net->ref_count, 1); 12478 } else { 12479 sp->net = NULL; 12480 } 12481 sctp_set_prsctp_policy(sp); 12482 } 12483 out_now: 12484 return (sp); 12485 } 12486 12487 12488 int 12489 sctp_sosend(struct socket *so, 12490 struct sockaddr *addr, 12491 struct uio *uio, 12492 struct mbuf *top, 12493 struct mbuf *control, 12494 int flags, 12495 struct thread *p 12496 ) 12497 { 12498 int error, use_sndinfo = 0; 12499 struct sctp_sndrcvinfo sndrcvninfo; 12500 struct sockaddr *addr_to_use; 12501 12502 #if defined(INET) && defined(INET6) 12503 struct sockaddr_in sin; 12504 12505 #endif 12506 12507 if (control) { 12508 /* process cmsg snd/rcv info (maybe a assoc-id) */ 12509 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&sndrcvninfo, control, 12510 sizeof(sndrcvninfo))) { 12511 /* got one */ 12512 use_sndinfo = 1; 12513 } 12514 } 12515 addr_to_use = addr; 12516 #if defined(INET) && defined(INET6) 12517 if ((addr) && (addr->sa_family == AF_INET6)) { 12518 struct sockaddr_in6 *sin6; 12519 12520 sin6 = (struct sockaddr_in6 *)addr; 12521 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 12522 in6_sin6_2_sin(&sin, sin6); 12523 addr_to_use = (struct sockaddr *)&sin; 12524 } 12525 } 12526 #endif 12527 error = sctp_lower_sosend(so, addr_to_use, uio, top, 12528 control, 12529 flags, 12530 use_sndinfo ? &sndrcvninfo : NULL 12531 ,p 12532 ); 12533 return (error); 12534 } 12535 12536 12537 int 12538 sctp_lower_sosend(struct socket *so, 12539 struct sockaddr *addr, 12540 struct uio *uio, 12541 struct mbuf *i_pak, 12542 struct mbuf *control, 12543 int flags, 12544 struct sctp_sndrcvinfo *srcv 12545 , 12546 struct thread *p 12547 ) 12548 { 12549 unsigned int sndlen = 0, max_len; 12550 int error, len; 12551 struct mbuf *top = NULL; 12552 int queue_only = 0, queue_only_for_init = 0; 12553 int free_cnt_applied = 0; 12554 int un_sent; 12555 int now_filled = 0; 12556 unsigned int inqueue_bytes = 0; 12557 struct sctp_block_entry be; 12558 struct sctp_inpcb *inp; 12559 struct sctp_tcb *stcb = NULL; 12560 struct timeval now; 12561 struct sctp_nets *net; 12562 struct sctp_association *asoc; 12563 struct sctp_inpcb *t_inp; 12564 int user_marks_eor; 12565 int create_lock_applied = 0; 12566 int nagle_applies = 0; 12567 int some_on_control = 0; 12568 int got_all_of_the_send = 0; 12569 int hold_tcblock = 0; 12570 int non_blocking = 0; 12571 uint32_t local_add_more, local_soresv = 0; 12572 uint16_t port; 12573 uint16_t sinfo_flags; 12574 sctp_assoc_t sinfo_assoc_id; 12575 12576 error = 0; 12577 net = NULL; 12578 stcb = NULL; 12579 asoc = NULL; 12580 12581 t_inp = inp = (struct sctp_inpcb *)so->so_pcb; 12582 if (inp == NULL) { 12583 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); 12584 error = EINVAL; 12585 if (i_pak) { 12586 SCTP_RELEASE_PKT(i_pak); 12587 } 12588 return (error); 12589 } 12590 if ((uio == NULL) && (i_pak == NULL)) { 12591 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 12592 return (EINVAL); 12593 } 12594 user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR); 12595 atomic_add_int(&inp->total_sends, 1); 12596 if (uio) { 12597 if (uio->uio_resid < 0) { 12598 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 12599 return (EINVAL); 12600 } 12601 sndlen = uio->uio_resid; 12602 } else { 12603 top = SCTP_HEADER_TO_CHAIN(i_pak); 12604 sndlen = SCTP_HEADER_LEN(i_pak); 12605 } 12606 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n", 12607 addr, 12608 sndlen); 12609 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 12610 (inp->sctp_socket->so_qlimit)) { 12611 /* The listener can NOT send */ 12612 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN); 12613 error = ENOTCONN; 12614 goto out_unlocked; 12615 } 12616 /** 12617 * Pre-screen address, if one is given the sin-len 12618 * must be set correctly! 12619 */ 12620 if (addr) { 12621 union sctp_sockstore *raddr = (union sctp_sockstore *)addr; 12622 12623 switch (raddr->sa.sa_family) { 12624 #if defined(INET) 12625 case AF_INET: 12626 if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) { 12627 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 12628 error = EINVAL; 12629 goto out_unlocked; 12630 } 12631 port = raddr->sin.sin_port; 12632 break; 12633 #endif 12634 #if defined(INET6) 12635 case AF_INET6: 12636 if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) { 12637 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 12638 error = EINVAL; 12639 goto out_unlocked; 12640 } 12641 port = raddr->sin6.sin6_port; 12642 break; 12643 #endif 12644 default: 12645 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAFNOSUPPORT); 12646 error = EAFNOSUPPORT; 12647 goto out_unlocked; 12648 } 12649 } else 12650 port = 0; 12651 12652 if (srcv) { 12653 sinfo_flags = srcv->sinfo_flags; 12654 sinfo_assoc_id = srcv->sinfo_assoc_id; 12655 if (INVALID_SINFO_FLAG(sinfo_flags) || 12656 PR_SCTP_INVALID_POLICY(sinfo_flags)) { 12657 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 12658 error = EINVAL; 12659 goto out_unlocked; 12660 } 12661 if (srcv->sinfo_flags) 12662 SCTP_STAT_INCR(sctps_sends_with_flags); 12663 } else { 12664 sinfo_flags = inp->def_send.sinfo_flags; 12665 sinfo_assoc_id = inp->def_send.sinfo_assoc_id; 12666 } 12667 if (sinfo_flags & SCTP_SENDALL) { 12668 /* its a sendall */ 12669 error = sctp_sendall(inp, uio, top, srcv); 12670 top = NULL; 12671 goto out_unlocked; 12672 } 12673 if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) { 12674 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 12675 error = EINVAL; 12676 goto out_unlocked; 12677 } 12678 /* now we must find the assoc */ 12679 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) || 12680 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 12681 SCTP_INP_RLOCK(inp); 12682 stcb = LIST_FIRST(&inp->sctp_asoc_list); 12683 if (stcb) { 12684 SCTP_TCB_LOCK(stcb); 12685 hold_tcblock = 1; 12686 } 12687 SCTP_INP_RUNLOCK(inp); 12688 } else if (sinfo_assoc_id) { 12689 stcb = sctp_findassociation_ep_asocid(inp, sinfo_assoc_id, 0); 12690 } else if (addr) { 12691 /*- 12692 * Since we did not use findep we must 12693 * increment it, and if we don't find a tcb 12694 * decrement it. 12695 */ 12696 SCTP_INP_WLOCK(inp); 12697 SCTP_INP_INCR_REF(inp); 12698 SCTP_INP_WUNLOCK(inp); 12699 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL); 12700 if (stcb == NULL) { 12701 SCTP_INP_WLOCK(inp); 12702 SCTP_INP_DECR_REF(inp); 12703 SCTP_INP_WUNLOCK(inp); 12704 } else { 12705 hold_tcblock = 1; 12706 } 12707 } 12708 if ((stcb == NULL) && (addr)) { 12709 /* Possible implicit send? */ 12710 SCTP_ASOC_CREATE_LOCK(inp); 12711 create_lock_applied = 1; 12712 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 12713 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 12714 /* Should I really unlock ? */ 12715 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); 12716 error = EINVAL; 12717 goto out_unlocked; 12718 12719 } 12720 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) && 12721 (addr->sa_family == AF_INET6)) { 12722 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 12723 error = EINVAL; 12724 goto out_unlocked; 12725 } 12726 SCTP_INP_WLOCK(inp); 12727 SCTP_INP_INCR_REF(inp); 12728 SCTP_INP_WUNLOCK(inp); 12729 /* With the lock applied look again */ 12730 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL); 12731 if ((stcb == NULL) && (control != NULL) && (port > 0)) { 12732 stcb = sctp_findassociation_cmsgs(&t_inp, port, control, &net, &error); 12733 } 12734 if (stcb == NULL) { 12735 SCTP_INP_WLOCK(inp); 12736 SCTP_INP_DECR_REF(inp); 12737 SCTP_INP_WUNLOCK(inp); 12738 } else { 12739 hold_tcblock = 1; 12740 } 12741 if (error) { 12742 goto out_unlocked; 12743 } 12744 if (t_inp != inp) { 12745 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN); 12746 error = ENOTCONN; 12747 goto out_unlocked; 12748 } 12749 } 12750 if (stcb == NULL) { 12751 if (addr == NULL) { 12752 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT); 12753 error = ENOENT; 12754 goto out_unlocked; 12755 } else { 12756 /* We must go ahead and start the INIT process */ 12757 uint32_t vrf_id; 12758 12759 if ((sinfo_flags & SCTP_ABORT) || 12760 ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) { 12761 /*- 12762 * User asks to abort a non-existant assoc, 12763 * or EOF a non-existant assoc with no data 12764 */ 12765 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT); 12766 error = ENOENT; 12767 goto out_unlocked; 12768 } 12769 /* get an asoc/stcb struct */ 12770 vrf_id = inp->def_vrf_id; 12771 #ifdef INVARIANTS 12772 if (create_lock_applied == 0) { 12773 panic("Error, should hold create lock and I don't?"); 12774 } 12775 #endif 12776 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, 12777 p 12778 ); 12779 if (stcb == NULL) { 12780 /* Error is setup for us in the call */ 12781 goto out_unlocked; 12782 } 12783 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 12784 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 12785 /* 12786 * Set the connected flag so we can queue 12787 * data 12788 */ 12789 soisconnecting(so); 12790 } 12791 hold_tcblock = 1; 12792 if (create_lock_applied) { 12793 SCTP_ASOC_CREATE_UNLOCK(inp); 12794 create_lock_applied = 0; 12795 } else { 12796 SCTP_PRINTF("Huh-3? create lock should have been on??\n"); 12797 } 12798 /* 12799 * Turn on queue only flag to prevent data from 12800 * being sent 12801 */ 12802 queue_only = 1; 12803 asoc = &stcb->asoc; 12804 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT); 12805 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); 12806 12807 /* initialize authentication params for the assoc */ 12808 sctp_initialize_auth_params(inp, stcb); 12809 12810 if (control) { 12811 if (sctp_process_cmsgs_for_init(stcb, control, &error)) { 12812 sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_7); 12813 hold_tcblock = 0; 12814 stcb = NULL; 12815 goto out_unlocked; 12816 } 12817 } 12818 /* out with the INIT */ 12819 queue_only_for_init = 1; 12820 /*- 12821 * we may want to dig in after this call and adjust the MTU 12822 * value. It defaulted to 1500 (constant) but the ro 12823 * structure may now have an update and thus we may need to 12824 * change it BEFORE we append the message. 12825 */ 12826 } 12827 } else 12828 asoc = &stcb->asoc; 12829 if (srcv == NULL) 12830 srcv = (struct sctp_sndrcvinfo *)&asoc->def_send; 12831 if (srcv->sinfo_flags & SCTP_ADDR_OVER) { 12832 if (addr) 12833 net = sctp_findnet(stcb, addr); 12834 else 12835 net = NULL; 12836 if ((net == NULL) || 12837 ((port != 0) && (port != stcb->rport))) { 12838 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 12839 error = EINVAL; 12840 goto out_unlocked; 12841 } 12842 } else { 12843 if (stcb->asoc.alternate) { 12844 net = stcb->asoc.alternate; 12845 } else { 12846 net = stcb->asoc.primary_destination; 12847 } 12848 } 12849 atomic_add_int(&stcb->total_sends, 1); 12850 /* Keep the stcb from being freed under our feet */ 12851 atomic_add_int(&asoc->refcnt, 1); 12852 free_cnt_applied = 1; 12853 12854 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) { 12855 if (sndlen > asoc->smallest_mtu) { 12856 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE); 12857 error = EMSGSIZE; 12858 goto out_unlocked; 12859 } 12860 } 12861 if ((SCTP_SO_IS_NBIO(so) 12862 || (flags & MSG_NBIO) 12863 )) { 12864 non_blocking = 1; 12865 } 12866 /* would we block? */ 12867 if (non_blocking) { 12868 if (hold_tcblock == 0) { 12869 SCTP_TCB_LOCK(stcb); 12870 hold_tcblock = 1; 12871 } 12872 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk)); 12873 if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) || 12874 (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) { 12875 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK); 12876 if (sndlen > SCTP_SB_LIMIT_SND(so)) 12877 error = EMSGSIZE; 12878 else 12879 error = EWOULDBLOCK; 12880 goto out_unlocked; 12881 } 12882 stcb->asoc.sb_send_resv += sndlen; 12883 SCTP_TCB_UNLOCK(stcb); 12884 hold_tcblock = 0; 12885 } else { 12886 atomic_add_int(&stcb->asoc.sb_send_resv, sndlen); 12887 } 12888 local_soresv = sndlen; 12889 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 12890 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); 12891 error = ECONNRESET; 12892 goto out_unlocked; 12893 } 12894 if (create_lock_applied) { 12895 SCTP_ASOC_CREATE_UNLOCK(inp); 12896 create_lock_applied = 0; 12897 } 12898 if (asoc->stream_reset_outstanding) { 12899 /* 12900 * Can't queue any data while stream reset is underway. 12901 */ 12902 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN); 12903 error = EAGAIN; 12904 goto out_unlocked; 12905 } 12906 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 12907 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 12908 queue_only = 1; 12909 } 12910 /* we are now done with all control */ 12911 if (control) { 12912 sctp_m_freem(control); 12913 control = NULL; 12914 } 12915 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) || 12916 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) || 12917 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) || 12918 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) { 12919 if (srcv->sinfo_flags & SCTP_ABORT) { 12920 ; 12921 } else { 12922 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); 12923 error = ECONNRESET; 12924 goto out_unlocked; 12925 } 12926 } 12927 /* Ok, we will attempt a msgsnd :> */ 12928 if (p) { 12929 p->td_ru.ru_msgsnd++; 12930 } 12931 /* Are we aborting? */ 12932 if (srcv->sinfo_flags & SCTP_ABORT) { 12933 struct mbuf *mm; 12934 int tot_demand, tot_out = 0, max_out; 12935 12936 SCTP_STAT_INCR(sctps_sends_with_abort); 12937 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || 12938 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { 12939 /* It has to be up before we abort */ 12940 /* how big is the user initiated abort? */ 12941 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 12942 error = EINVAL; 12943 goto out; 12944 } 12945 if (hold_tcblock) { 12946 SCTP_TCB_UNLOCK(stcb); 12947 hold_tcblock = 0; 12948 } 12949 if (top) { 12950 struct mbuf *cntm = NULL; 12951 12952 mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA); 12953 if (sndlen != 0) { 12954 cntm = top; 12955 while (cntm) { 12956 tot_out += SCTP_BUF_LEN(cntm); 12957 cntm = SCTP_BUF_NEXT(cntm); 12958 } 12959 } 12960 tot_demand = (tot_out + sizeof(struct sctp_paramhdr)); 12961 } else { 12962 /* Must fit in a MTU */ 12963 tot_out = sndlen; 12964 tot_demand = (tot_out + sizeof(struct sctp_paramhdr)); 12965 if (tot_demand > SCTP_DEFAULT_ADD_MORE) { 12966 /* To big */ 12967 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE); 12968 error = EMSGSIZE; 12969 goto out; 12970 } 12971 mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA); 12972 } 12973 if (mm == NULL) { 12974 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM); 12975 error = ENOMEM; 12976 goto out; 12977 } 12978 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr); 12979 max_out -= sizeof(struct sctp_abort_msg); 12980 if (tot_out > max_out) { 12981 tot_out = max_out; 12982 } 12983 if (mm) { 12984 struct sctp_paramhdr *ph; 12985 12986 /* now move forward the data pointer */ 12987 ph = mtod(mm, struct sctp_paramhdr *); 12988 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); 12989 ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out)); 12990 ph++; 12991 SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr); 12992 if (top == NULL) { 12993 error = uiomove((caddr_t)ph, (int)tot_out, uio); 12994 if (error) { 12995 /*- 12996 * Here if we can't get his data we 12997 * still abort we just don't get to 12998 * send the users note :-0 12999 */ 13000 sctp_m_freem(mm); 13001 mm = NULL; 13002 } 13003 } else { 13004 if (sndlen != 0) { 13005 SCTP_BUF_NEXT(mm) = top; 13006 } 13007 } 13008 } 13009 if (hold_tcblock == 0) { 13010 SCTP_TCB_LOCK(stcb); 13011 hold_tcblock = 1; 13012 } 13013 atomic_add_int(&stcb->asoc.refcnt, -1); 13014 free_cnt_applied = 0; 13015 /* release this lock, otherwise we hang on ourselves */ 13016 sctp_abort_an_association(stcb->sctp_ep, stcb, 13017 SCTP_RESPONSE_TO_USER_REQ, 13018 mm, SCTP_SO_LOCKED); 13019 /* now relock the stcb so everything is sane */ 13020 hold_tcblock = 0; 13021 stcb = NULL; 13022 /* 13023 * In this case top is already chained to mm avoid double 13024 * free, since we free it below if top != NULL and driver 13025 * would free it after sending the packet out 13026 */ 13027 if (sndlen != 0) { 13028 top = NULL; 13029 } 13030 goto out_unlocked; 13031 } 13032 /* Calculate the maximum we can send */ 13033 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk)); 13034 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) { 13035 if (non_blocking) { 13036 /* we already checked for non-blocking above. */ 13037 max_len = sndlen; 13038 } else { 13039 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes; 13040 } 13041 } else { 13042 max_len = 0; 13043 } 13044 if (hold_tcblock) { 13045 SCTP_TCB_UNLOCK(stcb); 13046 hold_tcblock = 0; 13047 } 13048 /* Is the stream no. valid? */ 13049 if (srcv->sinfo_stream >= asoc->streamoutcnt) { 13050 /* Invalid stream number */ 13051 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 13052 error = EINVAL; 13053 goto out_unlocked; 13054 } 13055 if (asoc->strmout == NULL) { 13056 /* huh? software error */ 13057 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT); 13058 error = EFAULT; 13059 goto out_unlocked; 13060 } 13061 /* Unless E_EOR mode is on, we must make a send FIT in one call. */ 13062 if ((user_marks_eor == 0) && 13063 (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) { 13064 /* It will NEVER fit */ 13065 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE); 13066 error = EMSGSIZE; 13067 goto out_unlocked; 13068 } 13069 if ((uio == NULL) && user_marks_eor) { 13070 /*- 13071 * We do not support eeor mode for 13072 * sending with mbuf chains (like sendfile). 13073 */ 13074 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 13075 error = EINVAL; 13076 goto out_unlocked; 13077 } 13078 if (user_marks_eor) { 13079 local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold)); 13080 } else { 13081 /*- 13082 * For non-eeor the whole message must fit in 13083 * the socket send buffer. 13084 */ 13085 local_add_more = sndlen; 13086 } 13087 len = 0; 13088 if (non_blocking) { 13089 goto skip_preblock; 13090 } 13091 if (((max_len <= local_add_more) && 13092 (SCTP_SB_LIMIT_SND(so) >= local_add_more)) || 13093 (max_len == 0) || 13094 ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) { 13095 /* No room right now ! */ 13096 SOCKBUF_LOCK(&so->so_snd); 13097 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk)); 13098 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) || 13099 ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) { 13100 SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n", 13101 (unsigned int)SCTP_SB_LIMIT_SND(so), 13102 inqueue_bytes, 13103 local_add_more, 13104 stcb->asoc.stream_queue_cnt, 13105 stcb->asoc.chunks_on_out_queue, 13106 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)); 13107 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { 13108 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, so, asoc, sndlen); 13109 } 13110 be.error = 0; 13111 stcb->block_entry = &be; 13112 error = sbwait(&so->so_snd); 13113 stcb->block_entry = NULL; 13114 if (error || so->so_error || be.error) { 13115 if (error == 0) { 13116 if (so->so_error) 13117 error = so->so_error; 13118 if (be.error) { 13119 error = be.error; 13120 } 13121 } 13122 SOCKBUF_UNLOCK(&so->so_snd); 13123 goto out_unlocked; 13124 } 13125 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { 13126 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK, 13127 so, asoc, stcb->asoc.total_output_queue_size); 13128 } 13129 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 13130 goto out_unlocked; 13131 } 13132 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk)); 13133 } 13134 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) { 13135 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes; 13136 } else { 13137 max_len = 0; 13138 } 13139 SOCKBUF_UNLOCK(&so->so_snd); 13140 } 13141 skip_preblock: 13142 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 13143 goto out_unlocked; 13144 } 13145 /* 13146 * sndlen covers for mbuf case uio_resid covers for the non-mbuf 13147 * case NOTE: uio will be null when top/mbuf is passed 13148 */ 13149 if (sndlen == 0) { 13150 if (srcv->sinfo_flags & SCTP_EOF) { 13151 got_all_of_the_send = 1; 13152 goto dataless_eof; 13153 } else { 13154 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 13155 error = EINVAL; 13156 goto out; 13157 } 13158 } 13159 if (top == NULL) { 13160 struct sctp_stream_queue_pending *sp; 13161 struct sctp_stream_out *strm; 13162 uint32_t sndout; 13163 13164 SCTP_TCB_SEND_LOCK(stcb); 13165 if ((asoc->stream_locked) && 13166 (asoc->stream_locked_on != srcv->sinfo_stream)) { 13167 SCTP_TCB_SEND_UNLOCK(stcb); 13168 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); 13169 error = EINVAL; 13170 goto out; 13171 } 13172 SCTP_TCB_SEND_UNLOCK(stcb); 13173 13174 strm = &stcb->asoc.strmout[srcv->sinfo_stream]; 13175 if (strm->last_msg_incomplete == 0) { 13176 do_a_copy_in: 13177 sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking); 13178 if ((sp == NULL) || (error)) { 13179 goto out; 13180 } 13181 SCTP_TCB_SEND_LOCK(stcb); 13182 if (sp->msg_is_complete) { 13183 strm->last_msg_incomplete = 0; 13184 asoc->stream_locked = 0; 13185 } else { 13186 /* 13187 * Just got locked to this guy in case of an 13188 * interrupt. 13189 */ 13190 strm->last_msg_incomplete = 1; 13191 asoc->stream_locked = 1; 13192 asoc->stream_locked_on = srcv->sinfo_stream; 13193 sp->sender_all_done = 0; 13194 } 13195 sctp_snd_sb_alloc(stcb, sp->length); 13196 atomic_add_int(&asoc->stream_queue_cnt, 1); 13197 if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) { 13198 sp->strseq = strm->next_sequence_sent; 13199 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) { 13200 sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN, 13201 (uintptr_t) stcb, sp->length, 13202 (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0); 13203 } 13204 strm->next_sequence_sent++; 13205 } else { 13206 SCTP_STAT_INCR(sctps_sends_with_unord); 13207 } 13208 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next); 13209 stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp, 1); 13210 SCTP_TCB_SEND_UNLOCK(stcb); 13211 } else { 13212 SCTP_TCB_SEND_LOCK(stcb); 13213 sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead); 13214 SCTP_TCB_SEND_UNLOCK(stcb); 13215 if (sp == NULL) { 13216 /* ???? Huh ??? last msg is gone */ 13217 #ifdef INVARIANTS 13218 panic("Warning: Last msg marked incomplete, yet nothing left?"); 13219 #else 13220 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n"); 13221 strm->last_msg_incomplete = 0; 13222 #endif 13223 goto do_a_copy_in; 13224 13225 } 13226 } 13227 while (uio->uio_resid > 0) { 13228 /* How much room do we have? */ 13229 struct mbuf *new_tail, *mm; 13230 13231 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size) 13232 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size; 13233 else 13234 max_len = 0; 13235 13236 if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) || 13237 (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) || 13238 (uio->uio_resid && (uio->uio_resid <= (int)max_len))) { 13239 sndout = 0; 13240 new_tail = NULL; 13241 if (hold_tcblock) { 13242 SCTP_TCB_UNLOCK(stcb); 13243 hold_tcblock = 0; 13244 } 13245 mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail); 13246 if ((mm == NULL) || error) { 13247 if (mm) { 13248 sctp_m_freem(mm); 13249 } 13250 goto out; 13251 } 13252 /* Update the mbuf and count */ 13253 SCTP_TCB_SEND_LOCK(stcb); 13254 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 13255 /* 13256 * we need to get out. Peer probably 13257 * aborted. 13258 */ 13259 sctp_m_freem(mm); 13260 if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) { 13261 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); 13262 error = ECONNRESET; 13263 } 13264 SCTP_TCB_SEND_UNLOCK(stcb); 13265 goto out; 13266 } 13267 if (sp->tail_mbuf) { 13268 /* tack it to the end */ 13269 SCTP_BUF_NEXT(sp->tail_mbuf) = mm; 13270 sp->tail_mbuf = new_tail; 13271 } else { 13272 /* A stolen mbuf */ 13273 sp->data = mm; 13274 sp->tail_mbuf = new_tail; 13275 } 13276 sctp_snd_sb_alloc(stcb, sndout); 13277 atomic_add_int(&sp->length, sndout); 13278 len += sndout; 13279 13280 /* Did we reach EOR? */ 13281 if ((uio->uio_resid == 0) && 13282 ((user_marks_eor == 0) || 13283 (srcv->sinfo_flags & SCTP_EOF) || 13284 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) { 13285 sp->msg_is_complete = 1; 13286 } else { 13287 sp->msg_is_complete = 0; 13288 } 13289 SCTP_TCB_SEND_UNLOCK(stcb); 13290 } 13291 if (uio->uio_resid == 0) { 13292 /* got it all? */ 13293 continue; 13294 } 13295 /* PR-SCTP? */ 13296 if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) { 13297 /* 13298 * This is ugly but we must assure locking 13299 * order 13300 */ 13301 if (hold_tcblock == 0) { 13302 SCTP_TCB_LOCK(stcb); 13303 hold_tcblock = 1; 13304 } 13305 sctp_prune_prsctp(stcb, asoc, srcv, sndlen); 13306 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk)); 13307 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size) 13308 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes; 13309 else 13310 max_len = 0; 13311 if (max_len > 0) { 13312 continue; 13313 } 13314 SCTP_TCB_UNLOCK(stcb); 13315 hold_tcblock = 0; 13316 } 13317 /* wait for space now */ 13318 if (non_blocking) { 13319 /* Non-blocking io in place out */ 13320 goto skip_out_eof; 13321 } 13322 /* What about the INIT, send it maybe */ 13323 if (queue_only_for_init) { 13324 if (hold_tcblock == 0) { 13325 SCTP_TCB_LOCK(stcb); 13326 hold_tcblock = 1; 13327 } 13328 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) { 13329 /* a collision took us forward? */ 13330 queue_only = 0; 13331 } else { 13332 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); 13333 SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT); 13334 queue_only = 1; 13335 } 13336 } 13337 if ((net->flight_size > net->cwnd) && 13338 (asoc->sctp_cmt_on_off == 0)) { 13339 SCTP_STAT_INCR(sctps_send_cwnd_avoid); 13340 queue_only = 1; 13341 } else if (asoc->ifp_had_enobuf) { 13342 SCTP_STAT_INCR(sctps_ifnomemqueued); 13343 if (net->flight_size > (2 * net->mtu)) { 13344 queue_only = 1; 13345 } 13346 asoc->ifp_had_enobuf = 0; 13347 } 13348 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) + 13349 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk))); 13350 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) && 13351 (stcb->asoc.total_flight > 0) && 13352 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) && 13353 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) { 13354 13355 /*- 13356 * Ok, Nagle is set on and we have data outstanding. 13357 * Don't send anything and let SACKs drive out the 13358 * data unless wen have a "full" segment to send. 13359 */ 13360 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) { 13361 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED); 13362 } 13363 SCTP_STAT_INCR(sctps_naglequeued); 13364 nagle_applies = 1; 13365 } else { 13366 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) { 13367 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) 13368 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED); 13369 } 13370 SCTP_STAT_INCR(sctps_naglesent); 13371 nagle_applies = 0; 13372 } 13373 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { 13374 13375 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only, 13376 nagle_applies, un_sent); 13377 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size, 13378 stcb->asoc.total_flight, 13379 stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count); 13380 } 13381 if (queue_only_for_init) 13382 queue_only_for_init = 0; 13383 if ((queue_only == 0) && (nagle_applies == 0)) { 13384 /*- 13385 * need to start chunk output 13386 * before blocking.. note that if 13387 * a lock is already applied, then 13388 * the input via the net is happening 13389 * and I don't need to start output :-D 13390 */ 13391 if (hold_tcblock == 0) { 13392 if (SCTP_TCB_TRYLOCK(stcb)) { 13393 hold_tcblock = 1; 13394 sctp_chunk_output(inp, 13395 stcb, 13396 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); 13397 } 13398 } else { 13399 sctp_chunk_output(inp, 13400 stcb, 13401 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); 13402 } 13403 if (hold_tcblock == 1) { 13404 SCTP_TCB_UNLOCK(stcb); 13405 hold_tcblock = 0; 13406 } 13407 } 13408 SOCKBUF_LOCK(&so->so_snd); 13409 /*- 13410 * This is a bit strange, but I think it will 13411 * work. The total_output_queue_size is locked and 13412 * protected by the TCB_LOCK, which we just released. 13413 * There is a race that can occur between releasing it 13414 * above, and me getting the socket lock, where sacks 13415 * come in but we have not put the SB_WAIT on the 13416 * so_snd buffer to get the wakeup. After the LOCK 13417 * is applied the sack_processing will also need to 13418 * LOCK the so->so_snd to do the actual sowwakeup(). So 13419 * once we have the socket buffer lock if we recheck the 13420 * size we KNOW we will get to sleep safely with the 13421 * wakeup flag in place. 13422 */ 13423 if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size + 13424 min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) { 13425 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { 13426 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK, 13427 so, asoc, uio->uio_resid); 13428 } 13429 be.error = 0; 13430 stcb->block_entry = &be; 13431 error = sbwait(&so->so_snd); 13432 stcb->block_entry = NULL; 13433 13434 if (error || so->so_error || be.error) { 13435 if (error == 0) { 13436 if (so->so_error) 13437 error = so->so_error; 13438 if (be.error) { 13439 error = be.error; 13440 } 13441 } 13442 SOCKBUF_UNLOCK(&so->so_snd); 13443 goto out_unlocked; 13444 } 13445 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { 13446 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK, 13447 so, asoc, stcb->asoc.total_output_queue_size); 13448 } 13449 } 13450 SOCKBUF_UNLOCK(&so->so_snd); 13451 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 13452 goto out_unlocked; 13453 } 13454 } 13455 SCTP_TCB_SEND_LOCK(stcb); 13456 if (sp) { 13457 if (sp->msg_is_complete == 0) { 13458 strm->last_msg_incomplete = 1; 13459 asoc->stream_locked = 1; 13460 asoc->stream_locked_on = srcv->sinfo_stream; 13461 } else { 13462 sp->sender_all_done = 1; 13463 strm->last_msg_incomplete = 0; 13464 asoc->stream_locked = 0; 13465 } 13466 } else { 13467 SCTP_PRINTF("Huh no sp TSNH?\n"); 13468 strm->last_msg_incomplete = 0; 13469 asoc->stream_locked = 0; 13470 } 13471 SCTP_TCB_SEND_UNLOCK(stcb); 13472 if (uio->uio_resid == 0) { 13473 got_all_of_the_send = 1; 13474 } 13475 } else { 13476 /* We send in a 0, since we do NOT have any locks */ 13477 error = sctp_msg_append(stcb, net, top, srcv, 0); 13478 top = NULL; 13479 if (srcv->sinfo_flags & SCTP_EOF) { 13480 /* 13481 * This should only happen for Panda for the mbuf 13482 * send case, which does NOT yet support EEOR mode. 13483 * Thus, we can just set this flag to do the proper 13484 * EOF handling. 13485 */ 13486 got_all_of_the_send = 1; 13487 } 13488 } 13489 if (error) { 13490 goto out; 13491 } 13492 dataless_eof: 13493 /* EOF thing ? */ 13494 if ((srcv->sinfo_flags & SCTP_EOF) && 13495 (got_all_of_the_send == 1) && 13496 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) { 13497 int cnt; 13498 13499 SCTP_STAT_INCR(sctps_sends_with_eof); 13500 error = 0; 13501 if (hold_tcblock == 0) { 13502 SCTP_TCB_LOCK(stcb); 13503 hold_tcblock = 1; 13504 } 13505 cnt = sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED); 13506 if (TAILQ_EMPTY(&asoc->send_queue) && 13507 TAILQ_EMPTY(&asoc->sent_queue) && 13508 (cnt == 0)) { 13509 if (asoc->locked_on_sending) { 13510 goto abort_anyway; 13511 } 13512 /* there is nothing queued to send, so I'm done... */ 13513 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 13514 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 13515 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 13516 struct sctp_nets *netp; 13517 13518 if (stcb->asoc.alternate) { 13519 netp = stcb->asoc.alternate; 13520 } else { 13521 netp = stcb->asoc.primary_destination; 13522 } 13523 /* only send SHUTDOWN the first time through */ 13524 sctp_send_shutdown(stcb, netp); 13525 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { 13526 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 13527 } 13528 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT); 13529 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 13530 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, 13531 netp); 13532 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, 13533 asoc->primary_destination); 13534 } 13535 } else { 13536 /*- 13537 * we still got (or just got) data to send, so set 13538 * SHUTDOWN_PENDING 13539 */ 13540 /*- 13541 * XXX sockets draft says that SCTP_EOF should be 13542 * sent with no data. currently, we will allow user 13543 * data to be sent first and move to 13544 * SHUTDOWN-PENDING 13545 */ 13546 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 13547 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && 13548 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 13549 if (hold_tcblock == 0) { 13550 SCTP_TCB_LOCK(stcb); 13551 hold_tcblock = 1; 13552 } 13553 if (asoc->locked_on_sending) { 13554 /* Locked to send out the data */ 13555 struct sctp_stream_queue_pending *sp; 13556 13557 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead); 13558 if (sp) { 13559 if ((sp->length == 0) && (sp->msg_is_complete == 0)) 13560 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT; 13561 } 13562 } 13563 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING; 13564 if (TAILQ_EMPTY(&asoc->send_queue) && 13565 TAILQ_EMPTY(&asoc->sent_queue) && 13566 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) { 13567 abort_anyway: 13568 if (free_cnt_applied) { 13569 atomic_add_int(&stcb->asoc.refcnt, -1); 13570 free_cnt_applied = 0; 13571 } 13572 sctp_abort_an_association(stcb->sctp_ep, stcb, 13573 SCTP_RESPONSE_TO_USER_REQ, 13574 NULL, SCTP_SO_LOCKED); 13575 /* 13576 * now relock the stcb so everything 13577 * is sane 13578 */ 13579 hold_tcblock = 0; 13580 stcb = NULL; 13581 goto out; 13582 } 13583 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, 13584 asoc->primary_destination); 13585 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY); 13586 } 13587 } 13588 } 13589 skip_out_eof: 13590 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { 13591 some_on_control = 1; 13592 } 13593 if (queue_only_for_init) { 13594 if (hold_tcblock == 0) { 13595 SCTP_TCB_LOCK(stcb); 13596 hold_tcblock = 1; 13597 } 13598 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) { 13599 /* a collision took us forward? */ 13600 queue_only = 0; 13601 } else { 13602 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); 13603 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT); 13604 queue_only = 1; 13605 } 13606 } 13607 if ((net->flight_size > net->cwnd) && 13608 (stcb->asoc.sctp_cmt_on_off == 0)) { 13609 SCTP_STAT_INCR(sctps_send_cwnd_avoid); 13610 queue_only = 1; 13611 } else if (asoc->ifp_had_enobuf) { 13612 SCTP_STAT_INCR(sctps_ifnomemqueued); 13613 if (net->flight_size > (2 * net->mtu)) { 13614 queue_only = 1; 13615 } 13616 asoc->ifp_had_enobuf = 0; 13617 } 13618 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) + 13619 (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk))); 13620 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) && 13621 (stcb->asoc.total_flight > 0) && 13622 (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) && 13623 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) { 13624 /*- 13625 * Ok, Nagle is set on and we have data outstanding. 13626 * Don't send anything and let SACKs drive out the 13627 * data unless wen have a "full" segment to send. 13628 */ 13629 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) { 13630 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED); 13631 } 13632 SCTP_STAT_INCR(sctps_naglequeued); 13633 nagle_applies = 1; 13634 } else { 13635 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) { 13636 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) 13637 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED); 13638 } 13639 SCTP_STAT_INCR(sctps_naglesent); 13640 nagle_applies = 0; 13641 } 13642 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { 13643 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only, 13644 nagle_applies, un_sent); 13645 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size, 13646 stcb->asoc.total_flight, 13647 stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count); 13648 } 13649 if (queue_only_for_init) 13650 queue_only_for_init = 0; 13651 if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) { 13652 /* we can attempt to send too. */ 13653 if (hold_tcblock == 0) { 13654 /* 13655 * If there is activity recv'ing sacks no need to 13656 * send 13657 */ 13658 if (SCTP_TCB_TRYLOCK(stcb)) { 13659 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); 13660 hold_tcblock = 1; 13661 } 13662 } else { 13663 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); 13664 } 13665 } else if ((queue_only == 0) && 13666 (stcb->asoc.peers_rwnd == 0) && 13667 (stcb->asoc.total_flight == 0)) { 13668 /* We get to have a probe outstanding */ 13669 if (hold_tcblock == 0) { 13670 hold_tcblock = 1; 13671 SCTP_TCB_LOCK(stcb); 13672 } 13673 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); 13674 } else if (some_on_control) { 13675 int num_out, reason, frag_point; 13676 13677 /* Here we do control only */ 13678 if (hold_tcblock == 0) { 13679 hold_tcblock = 1; 13680 SCTP_TCB_LOCK(stcb); 13681 } 13682 frag_point = sctp_get_frag_point(stcb, &stcb->asoc); 13683 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out, 13684 &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED); 13685 } 13686 SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n", 13687 queue_only, stcb->asoc.peers_rwnd, un_sent, 13688 stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue, 13689 stcb->asoc.total_output_queue_size, error); 13690 13691 out: 13692 out_unlocked: 13693 13694 if (local_soresv && stcb) { 13695 atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen); 13696 local_soresv = 0; 13697 } 13698 if (create_lock_applied) { 13699 SCTP_ASOC_CREATE_UNLOCK(inp); 13700 create_lock_applied = 0; 13701 } 13702 if ((stcb) && hold_tcblock) { 13703 SCTP_TCB_UNLOCK(stcb); 13704 } 13705 if (stcb && free_cnt_applied) { 13706 atomic_add_int(&stcb->asoc.refcnt, -1); 13707 } 13708 #ifdef INVARIANTS 13709 if (stcb) { 13710 if (mtx_owned(&stcb->tcb_mtx)) { 13711 panic("Leaving with tcb mtx owned?"); 13712 } 13713 if (mtx_owned(&stcb->tcb_send_mtx)) { 13714 panic("Leaving with tcb send mtx owned?"); 13715 } 13716 } 13717 #endif 13718 #ifdef INVARIANTS 13719 if (inp) { 13720 sctp_validate_no_locks(inp); 13721 } else { 13722 printf("Warning - inp is NULL so cant validate locks\n"); 13723 } 13724 #endif 13725 if (top) { 13726 sctp_m_freem(top); 13727 } 13728 if (control) { 13729 sctp_m_freem(control); 13730 } 13731 return (error); 13732 } 13733 13734 13735 /* 13736 * generate an AUTHentication chunk, if required 13737 */ 13738 struct mbuf * 13739 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end, 13740 struct sctp_auth_chunk **auth_ret, uint32_t * offset, 13741 struct sctp_tcb *stcb, uint8_t chunk) 13742 { 13743 struct mbuf *m_auth; 13744 struct sctp_auth_chunk *auth; 13745 int chunk_len; 13746 13747 if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) || 13748 (stcb == NULL)) 13749 return (m); 13750 13751 /* sysctl disabled auth? */ 13752 if (SCTP_BASE_SYSCTL(sctp_auth_disable)) 13753 return (m); 13754 13755 /* peer doesn't do auth... */ 13756 if (!stcb->asoc.peer_supports_auth) { 13757 return (m); 13758 } 13759 /* does the requested chunk require auth? */ 13760 if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) { 13761 return (m); 13762 } 13763 m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER); 13764 if (m_auth == NULL) { 13765 /* no mbuf's */ 13766 return (m); 13767 } 13768 /* reserve some space if this will be the first mbuf */ 13769 if (m == NULL) 13770 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD); 13771 /* fill in the AUTH chunk details */ 13772 auth = mtod(m_auth, struct sctp_auth_chunk *); 13773 bzero(auth, sizeof(*auth)); 13774 auth->ch.chunk_type = SCTP_AUTHENTICATION; 13775 auth->ch.chunk_flags = 0; 13776 chunk_len = sizeof(*auth) + 13777 sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id); 13778 auth->ch.chunk_length = htons(chunk_len); 13779 auth->hmac_id = htons(stcb->asoc.peer_hmac_id); 13780 /* key id and hmac digest will be computed and filled in upon send */ 13781 13782 /* save the offset where the auth was inserted into the chain */ 13783 if (m != NULL) { 13784 struct mbuf *cn; 13785 13786 *offset = 0; 13787 cn = m; 13788 while (cn) { 13789 *offset += SCTP_BUF_LEN(cn); 13790 cn = SCTP_BUF_NEXT(cn); 13791 } 13792 } else 13793 *offset = 0; 13794 13795 /* update length and return pointer to the auth chunk */ 13796 SCTP_BUF_LEN(m_auth) = chunk_len; 13797 m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0); 13798 if (auth_ret != NULL) 13799 *auth_ret = auth; 13800 13801 return (m); 13802 } 13803 13804 #ifdef INET6 13805 int 13806 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro) 13807 { 13808 struct nd_prefix *pfx = NULL; 13809 struct nd_pfxrouter *pfxrtr = NULL; 13810 struct sockaddr_in6 gw6; 13811 13812 if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6) 13813 return (0); 13814 13815 /* get prefix entry of address */ 13816 LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) { 13817 if (pfx->ndpr_stateflags & NDPRF_DETACHED) 13818 continue; 13819 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr, 13820 &src6->sin6_addr, &pfx->ndpr_mask)) 13821 break; 13822 } 13823 /* no prefix entry in the prefix list */ 13824 if (pfx == NULL) { 13825 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for "); 13826 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6); 13827 return (0); 13828 } 13829 SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is "); 13830 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6); 13831 13832 /* search installed gateway from prefix entry */ 13833 for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr = 13834 pfxrtr->pfr_next) { 13835 memset(&gw6, 0, sizeof(struct sockaddr_in6)); 13836 gw6.sin6_family = AF_INET6; 13837 gw6.sin6_len = sizeof(struct sockaddr_in6); 13838 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr, 13839 sizeof(struct in6_addr)); 13840 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is "); 13841 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6); 13842 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is "); 13843 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway); 13844 if (sctp_cmpaddr((struct sockaddr *)&gw6, 13845 ro->ro_rt->rt_gateway)) { 13846 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n"); 13847 return (1); 13848 } 13849 } 13850 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n"); 13851 return (0); 13852 } 13853 13854 #endif 13855 13856 int 13857 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro) 13858 { 13859 #ifdef INET 13860 struct sockaddr_in *sin, *mask; 13861 struct ifaddr *ifa; 13862 struct in_addr srcnetaddr, gwnetaddr; 13863 13864 if (ro == NULL || ro->ro_rt == NULL || 13865 sifa->address.sa.sa_family != AF_INET) { 13866 return (0); 13867 } 13868 ifa = (struct ifaddr *)sifa->ifa; 13869 mask = (struct sockaddr_in *)(ifa->ifa_netmask); 13870 sin = (struct sockaddr_in *)&sifa->address.sin; 13871 srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr); 13872 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is "); 13873 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa); 13874 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr); 13875 13876 sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 13877 gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr); 13878 SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is "); 13879 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway); 13880 SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr); 13881 if (srcnetaddr.s_addr == gwnetaddr.s_addr) { 13882 return (1); 13883 } 13884 #endif 13885 return (0); 13886 } 13887