| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * a) Redistributions of source code must retain the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer. |
| 13 | * |
| 14 | * b) Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the distribution. |
| 17 | * |
| 18 | * c) Neither the name of Cisco Systems, Inc. nor the names of its |
| 19 | * contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 32 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | */ |
| 34 |
| 35 | #ifdef __FreeBSD__ |
| 36 | #include <sys/cdefs.h> |
| 37 | __FBSDID("$FreeBSD: head/sys/netinet/sctp_output.c 357197 2020-01-28 10:09:05Z tuexen $"); |
| 38 | #endif |
| 39 |
| 40 | #include <netinet/sctp_os.h> |
| 41 | #ifdef __FreeBSD__ |
| 42 | #include <sys/proc.h> |
| 43 | #endif |
| 44 | #include <netinet/sctp_var.h> |
| 45 | #include <netinet/sctp_sysctl.h> |
| 46 | #include <netinet/sctp_header.h> |
| 47 | #include <netinet/sctp_pcb.h> |
| 48 | #include <netinet/sctputil.h> |
| 49 | #include <netinet/sctp_output.h> |
| 50 | #include <netinet/sctp_uio.h> |
| 51 | #include <netinet/sctputil.h> |
| 52 | #include <netinet/sctp_auth.h> |
| 53 | #include <netinet/sctp_timer.h> |
| 54 | #include <netinet/sctp_asconf.h> |
| 55 | #include <netinet/sctp_indata.h> |
| 56 | #include <netinet/sctp_bsd_addr.h> |
| 57 | #include <netinet/sctp_input.h> |
| 58 | #include <netinet/sctp_crc32.h> |
| 59 | #if defined(__FreeBSD__) |
| 60 | #include <netinet/sctp_kdtrace.h> |
| 61 | #endif |
| 62 | #if defined(__Userspace_os_Linux) |
| 63 | #define __FAVOR_BSD /* (on Ubuntu at least) enables UDP header field names like BSD in RFC 768 */ |
| 64 | #endif |
| 65 | #if defined(INET) || defined(INET6) |
| 66 | #if !defined(__Userspace_os_Windows) |
| 67 | #include <netinet/udp.h> |
| 68 | #endif |
| 69 | #endif |
| 70 | #if defined(__APPLE__) |
| 71 | #include <netinet/in.h> |
| 72 | #endif |
| 73 | #if defined(__FreeBSD__) |
| 74 | #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 |
| 75 | #include <netinet/udp_var.h> |
| 76 | #endif |
| 77 | #include <machine/in_cksum.h> |
| 78 | #endif |
| 79 | #if defined(__Userspace__) && defined(INET6) |
| 80 | #include <netinet6/sctp6_var.h> |
| 81 | #endif |
| 82 |
| 83 | #if defined(__APPLE__) |
| 84 | #define APPLE_FILE_NO 3 |
| 85 | #endif |
| 86 |
| 87 | #if defined(__APPLE__) |
| 88 | #if !(defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)) |
| 89 | #define SCTP_MAX_LINKHDR 16 |
| 90 | #endif |
| 91 | #endif |
| 92 |
| 93 | #define SCTP_MAX_GAPS_INARRAY 4 |
| 94 | struct sack_track { |
| 95 | uint8_t right_edge; /* mergable on the right edge */ |
| 96 | uint8_t left_edge; /* mergable on the left edge */ |
| 97 | uint8_t num_entries; |
| 98 | uint8_t spare; |
| 99 | struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY]; |
| 100 | }; |
| 101 |
| 102 | const struct sack_track sack_array[256] = { |
| 103 | {0, 0, 0, 0, /* 0x00 */ |
| 104 | {{0, 0}, |
| 105 | {0, 0}, |
| 106 | {0, 0}, |
| 107 | {0, 0} |
| 108 | } |
| 109 | }, |
| 110 | {1, 0, 1, 0, /* 0x01 */ |
| 111 | {{0, 0}, |
| 112 | {0, 0}, |
| 113 | {0, 0}, |
| 114 | {0, 0} |
| 115 | } |
| 116 | }, |
| 117 | {0, 0, 1, 0, /* 0x02 */ |
| 118 | {{1, 1}, |
| 119 | {0, 0}, |
| 120 | {0, 0}, |
| 121 | {0, 0} |
| 122 | } |
| 123 | }, |
| 124 | {1, 0, 1, 0, /* 0x03 */ |
| 125 | {{0, 1}, |
| 126 | {0, 0}, |
| 127 | {0, 0}, |
| 128 | {0, 0} |
| 129 | } |
| 130 | }, |
| 131 | {0, 0, 1, 0, /* 0x04 */ |
| 132 | {{2, 2}, |
| 133 | {0, 0}, |
| 134 | {0, 0}, |
| 135 | {0, 0} |
| 136 | } |
| 137 | }, |
| 138 | {1, 0, 2, 0, /* 0x05 */ |
| 139 | {{0, 0}, |
| 140 | {2, 2}, |
| 141 | {0, 0}, |
| 142 | {0, 0} |
| 143 | } |
| 144 | }, |
| 145 | {0, 0, 1, 0, /* 0x06 */ |
| 146 | {{1, 2}, |
| 147 | {0, 0}, |
| 148 | {0, 0}, |
| 149 | {0, 0} |
| 150 | } |
| 151 | }, |
| 152 | {1, 0, 1, 0, /* 0x07 */ |
| 153 | {{0, 2}, |
| 154 | {0, 0}, |
| 155 | {0, 0}, |
| 156 | {0, 0} |
| 157 | } |
| 158 | }, |
| 159 | {0, 0, 1, 0, /* 0x08 */ |
| 160 | {{3, 3}, |
| 161 | {0, 0}, |
| 162 | {0, 0}, |
| 163 | {0, 0} |
| 164 | } |
| 165 | }, |
| 166 | {1, 0, 2, 0, /* 0x09 */ |
| 167 | {{0, 0}, |
| 168 | {3, 3}, |
| 169 | {0, 0}, |
| 170 | {0, 0} |
| 171 | } |
| 172 | }, |
| 173 | {0, 0, 2, 0, /* 0x0a */ |
| 174 | {{1, 1}, |
| 175 | {3, 3}, |
| 176 | {0, 0}, |
| 177 | {0, 0} |
| 178 | } |
| 179 | }, |
| 180 | {1, 0, 2, 0, /* 0x0b */ |
| 181 | {{0, 1}, |
| 182 | {3, 3}, |
| 183 | {0, 0}, |
| 184 | {0, 0} |
| 185 | } |
| 186 | }, |
| 187 | {0, 0, 1, 0, /* 0x0c */ |
| 188 | {{2, 3}, |
| 189 | {0, 0}, |
| 190 | {0, 0}, |
| 191 | {0, 0} |
| 192 | } |
| 193 | }, |
| 194 | {1, 0, 2, 0, /* 0x0d */ |
| 195 | {{0, 0}, |
| 196 | {2, 3}, |
| 197 | {0, 0}, |
| 198 | {0, 0} |
| 199 | } |
| 200 | }, |
| 201 | {0, 0, 1, 0, /* 0x0e */ |
| 202 | {{1, 3}, |
| 203 | {0, 0}, |
| 204 | {0, 0}, |
| 205 | {0, 0} |
| 206 | } |
| 207 | }, |
| 208 | {1, 0, 1, 0, /* 0x0f */ |
| 209 | {{0, 3}, |
| 210 | {0, 0}, |
| 211 | {0, 0}, |
| 212 | {0, 0} |
| 213 | } |
| 214 | }, |
| 215 | {0, 0, 1, 0, /* 0x10 */ |
| 216 | {{4, 4}, |
| 217 | {0, 0}, |
| 218 | {0, 0}, |
| 219 | {0, 0} |
| 220 | } |
| 221 | }, |
| 222 | {1, 0, 2, 0, /* 0x11 */ |
| 223 | {{0, 0}, |
| 224 | {4, 4}, |
| 225 | {0, 0}, |
| 226 | {0, 0} |
| 227 | } |
| 228 | }, |
| 229 | {0, 0, 2, 0, /* 0x12 */ |
| 230 | {{1, 1}, |
| 231 | {4, 4}, |
| 232 | {0, 0}, |
| 233 | {0, 0} |
| 234 | } |
| 235 | }, |
| 236 | {1, 0, 2, 0, /* 0x13 */ |
| 237 | {{0, 1}, |
| 238 | {4, 4}, |
| 239 | {0, 0}, |
| 240 | {0, 0} |
| 241 | } |
| 242 | }, |
| 243 | {0, 0, 2, 0, /* 0x14 */ |
| 244 | {{2, 2}, |
| 245 | {4, 4}, |
| 246 | {0, 0}, |
| 247 | {0, 0} |
| 248 | } |
| 249 | }, |
| 250 | {1, 0, 3, 0, /* 0x15 */ |
| 251 | {{0, 0}, |
| 252 | {2, 2}, |
| 253 | {4, 4}, |
| 254 | {0, 0} |
| 255 | } |
| 256 | }, |
| 257 | {0, 0, 2, 0, /* 0x16 */ |
| 258 | {{1, 2}, |
| 259 | {4, 4}, |
| 260 | {0, 0}, |
| 261 | {0, 0} |
| 262 | } |
| 263 | }, |
| 264 | {1, 0, 2, 0, /* 0x17 */ |
| 265 | {{0, 2}, |
| 266 | {4, 4}, |
| 267 | {0, 0}, |
| 268 | {0, 0} |
| 269 | } |
| 270 | }, |
| 271 | {0, 0, 1, 0, /* 0x18 */ |
| 272 | {{3, 4}, |
| 273 | {0, 0}, |
| 274 | {0, 0}, |
| 275 | {0, 0} |
| 276 | } |
| 277 | }, |
| 278 | {1, 0, 2, 0, /* 0x19 */ |
| 279 | {{0, 0}, |
| 280 | {3, 4}, |
| 281 | {0, 0}, |
| 282 | {0, 0} |
| 283 | } |
| 284 | }, |
| 285 | {0, 0, 2, 0, /* 0x1a */ |
| 286 | {{1, 1}, |
| 287 | {3, 4}, |
| 288 | {0, 0}, |
| 289 | {0, 0} |
| 290 | } |
| 291 | }, |
| 292 | {1, 0, 2, 0, /* 0x1b */ |
| 293 | {{0, 1}, |
| 294 | {3, 4}, |
| 295 | {0, 0}, |
| 296 | {0, 0} |
| 297 | } |
| 298 | }, |
| 299 | {0, 0, 1, 0, /* 0x1c */ |
| 300 | {{2, 4}, |
| 301 | {0, 0}, |
| 302 | {0, 0}, |
| 303 | {0, 0} |
| 304 | } |
| 305 | }, |
| 306 | {1, 0, 2, 0, /* 0x1d */ |
| 307 | {{0, 0}, |
| 308 | {2, 4}, |
| 309 | {0, 0}, |
| 310 | {0, 0} |
| 311 | } |
| 312 | }, |
| 313 | {0, 0, 1, 0, /* 0x1e */ |
| 314 | {{1, 4}, |
| 315 | {0, 0}, |
| 316 | {0, 0}, |
| 317 | {0, 0} |
| 318 | } |
| 319 | }, |
| 320 | {1, 0, 1, 0, /* 0x1f */ |
| 321 | {{0, 4}, |
| 322 | {0, 0}, |
| 323 | {0, 0}, |
| 324 | {0, 0} |
| 325 | } |
| 326 | }, |
| 327 | {0, 0, 1, 0, /* 0x20 */ |
| 328 | {{5, 5}, |
| 329 | {0, 0}, |
| 330 | {0, 0}, |
| 331 | {0, 0} |
| 332 | } |
| 333 | }, |
| 334 | {1, 0, 2, 0, /* 0x21 */ |
| 335 | {{0, 0}, |
| 336 | {5, 5}, |
| 337 | {0, 0}, |
| 338 | {0, 0} |
| 339 | } |
| 340 | }, |
| 341 | {0, 0, 2, 0, /* 0x22 */ |
| 342 | {{1, 1}, |
| 343 | {5, 5}, |
| 344 | {0, 0}, |
| 345 | {0, 0} |
| 346 | } |
| 347 | }, |
| 348 | {1, 0, 2, 0, /* 0x23 */ |
| 349 | {{0, 1}, |
| 350 | {5, 5}, |
| 351 | {0, 0}, |
| 352 | {0, 0} |
| 353 | } |
| 354 | }, |
| 355 | {0, 0, 2, 0, /* 0x24 */ |
| 356 | {{2, 2}, |
| 357 | {5, 5}, |
| 358 | {0, 0}, |
| 359 | {0, 0} |
| 360 | } |
| 361 | }, |
| 362 | {1, 0, 3, 0, /* 0x25 */ |
| 363 | {{0, 0}, |
| 364 | {2, 2}, |
| 365 | {5, 5}, |
| 366 | {0, 0} |
| 367 | } |
| 368 | }, |
| 369 | {0, 0, 2, 0, /* 0x26 */ |
| 370 | {{1, 2}, |
| 371 | {5, 5}, |
| 372 | {0, 0}, |
| 373 | {0, 0} |
| 374 | } |
| 375 | }, |
| 376 | {1, 0, 2, 0, /* 0x27 */ |
| 377 | {{0, 2}, |
| 378 | {5, 5}, |
| 379 | {0, 0}, |
| 380 | {0, 0} |
| 381 | } |
| 382 | }, |
| 383 | {0, 0, 2, 0, /* 0x28 */ |
| 384 | {{3, 3}, |
| 385 | {5, 5}, |
| 386 | {0, 0}, |
| 387 | {0, 0} |
| 388 | } |
| 389 | }, |
| 390 | {1, 0, 3, 0, /* 0x29 */ |
| 391 | {{0, 0}, |
| 392 | {3, 3}, |
| 393 | {5, 5}, |
| 394 | {0, 0} |
| 395 | } |
| 396 | }, |
| 397 | {0, 0, 3, 0, /* 0x2a */ |
| 398 | {{1, 1}, |
| 399 | {3, 3}, |
| 400 | {5, 5}, |
| 401 | {0, 0} |
| 402 | } |
| 403 | }, |
| 404 | {1, 0, 3, 0, /* 0x2b */ |
| 405 | {{0, 1}, |
| 406 | {3, 3}, |
| 407 | {5, 5}, |
| 408 | {0, 0} |
| 409 | } |
| 410 | }, |
| 411 | {0, 0, 2, 0, /* 0x2c */ |
| 412 | {{2, 3}, |
| 413 | {5, 5}, |
| 414 | {0, 0}, |
| 415 | {0, 0} |
| 416 | } |
| 417 | }, |
| 418 | {1, 0, 3, 0, /* 0x2d */ |
| 419 | {{0, 0}, |
| 420 | {2, 3}, |
| 421 | {5, 5}, |
| 422 | {0, 0} |
| 423 | } |
| 424 | }, |
| 425 | {0, 0, 2, 0, /* 0x2e */ |
| 426 | {{1, 3}, |
| 427 | {5, 5}, |
| 428 | {0, 0}, |
| 429 | {0, 0} |
| 430 | } |
| 431 | }, |
| 432 | {1, 0, 2, 0, /* 0x2f */ |
| 433 | {{0, 3}, |
| 434 | {5, 5}, |
| 435 | {0, 0}, |
| 436 | {0, 0} |
| 437 | } |
| 438 | }, |
| 439 | {0, 0, 1, 0, /* 0x30 */ |
| 440 | {{4, 5}, |
| 441 | {0, 0}, |
| 442 | {0, 0}, |
| 443 | {0, 0} |
| 444 | } |
| 445 | }, |
| 446 | {1, 0, 2, 0, /* 0x31 */ |
| 447 | {{0, 0}, |
| 448 | {4, 5}, |
| 449 | {0, 0}, |
| 450 | {0, 0} |
| 451 | } |
| 452 | }, |
| 453 | {0, 0, 2, 0, /* 0x32 */ |
| 454 | {{1, 1}, |
| 455 | {4, 5}, |
| 456 | {0, 0}, |
| 457 | {0, 0} |
| 458 | } |
| 459 | }, |
| 460 | {1, 0, 2, 0, /* 0x33 */ |
| 461 | {{0, 1}, |
| 462 | {4, 5}, |
| 463 | {0, 0}, |
| 464 | {0, 0} |
| 465 | } |
| 466 | }, |
| 467 | {0, 0, 2, 0, /* 0x34 */ |
| 468 | {{2, 2}, |
| 469 | {4, 5}, |
| 470 | {0, 0}, |
| 471 | {0, 0} |
| 472 | } |
| 473 | }, |
| 474 | {1, 0, 3, 0, /* 0x35 */ |
| 475 | {{0, 0}, |
| 476 | {2, 2}, |
| 477 | {4, 5}, |
| 478 | {0, 0} |
| 479 | } |
| 480 | }, |
| 481 | {0, 0, 2, 0, /* 0x36 */ |
| 482 | {{1, 2}, |
| 483 | {4, 5}, |
| 484 | {0, 0}, |
| 485 | {0, 0} |
| 486 | } |
| 487 | }, |
| 488 | {1, 0, 2, 0, /* 0x37 */ |
| 489 | {{0, 2}, |
| 490 | {4, 5}, |
| 491 | {0, 0}, |
| 492 | {0, 0} |
| 493 | } |
| 494 | }, |
| 495 | {0, 0, 1, 0, /* 0x38 */ |
| 496 | {{3, 5}, |
| 497 | {0, 0}, |
| 498 | {0, 0}, |
| 499 | {0, 0} |
| 500 | } |
| 501 | }, |
| 502 | {1, 0, 2, 0, /* 0x39 */ |
| 503 | {{0, 0}, |
| 504 | {3, 5}, |
| 505 | {0, 0}, |
| 506 | {0, 0} |
| 507 | } |
| 508 | }, |
| 509 | {0, 0, 2, 0, /* 0x3a */ |
| 510 | {{1, 1}, |
| 511 | {3, 5}, |
| 512 | {0, 0}, |
| 513 | {0, 0} |
| 514 | } |
| 515 | }, |
| 516 | {1, 0, 2, 0, /* 0x3b */ |
| 517 | {{0, 1}, |
| 518 | {3, 5}, |
| 519 | {0, 0}, |
| 520 | {0, 0} |
| 521 | } |
| 522 | }, |
| 523 | {0, 0, 1, 0, /* 0x3c */ |
| 524 | {{2, 5}, |
| 525 | {0, 0}, |
| 526 | {0, 0}, |
| 527 | {0, 0} |
| 528 | } |
| 529 | }, |
| 530 | {1, 0, 2, 0, /* 0x3d */ |
| 531 | {{0, 0}, |
| 532 | {2, 5}, |
| 533 | {0, 0}, |
| 534 | {0, 0} |
| 535 | } |
| 536 | }, |
| 537 | {0, 0, 1, 0, /* 0x3e */ |
| 538 | {{1, 5}, |
| 539 | {0, 0}, |
| 540 | {0, 0}, |
| 541 | {0, 0} |
| 542 | } |
| 543 | }, |
| 544 | {1, 0, 1, 0, /* 0x3f */ |
| 545 | {{0, 5}, |
| 546 | {0, 0}, |
| 547 | {0, 0}, |
| 548 | {0, 0} |
| 549 | } |
| 550 | }, |
| 551 | {0, 0, 1, 0, /* 0x40 */ |
| 552 | {{6, 6}, |
| 553 | {0, 0}, |
| 554 | {0, 0}, |
| 555 | {0, 0} |
| 556 | } |
| 557 | }, |
| 558 | {1, 0, 2, 0, /* 0x41 */ |
| 559 | {{0, 0}, |
| 560 | {6, 6}, |
| 561 | {0, 0}, |
| 562 | {0, 0} |
| 563 | } |
| 564 | }, |
| 565 | {0, 0, 2, 0, /* 0x42 */ |
| 566 | {{1, 1}, |
| 567 | {6, 6}, |
| 568 | {0, 0}, |
| 569 | {0, 0} |
| 570 | } |
| 571 | }, |
| 572 | {1, 0, 2, 0, /* 0x43 */ |
| 573 | {{0, 1}, |
| 574 | {6, 6}, |
| 575 | {0, 0}, |
| 576 | {0, 0} |
| 577 | } |
| 578 | }, |
| 579 | {0, 0, 2, 0, /* 0x44 */ |
| 580 | {{2, 2}, |
| 581 | {6, 6}, |
| 582 | {0, 0}, |
| 583 | {0, 0} |
| 584 | } |
| 585 | }, |
| 586 | {1, 0, 3, 0, /* 0x45 */ |
| 587 | {{0, 0}, |
| 588 | {2, 2}, |
| 589 | {6, 6}, |
| 590 | {0, 0} |
| 591 | } |
| 592 | }, |
| 593 | {0, 0, 2, 0, /* 0x46 */ |
| 594 | {{1, 2}, |
| 595 | {6, 6}, |
| 596 | {0, 0}, |
| 597 | {0, 0} |
| 598 | } |
| 599 | }, |
| 600 | {1, 0, 2, 0, /* 0x47 */ |
| 601 | {{0, 2}, |
| 602 | {6, 6}, |
| 603 | {0, 0}, |
| 604 | {0, 0} |
| 605 | } |
| 606 | }, |
| 607 | {0, 0, 2, 0, /* 0x48 */ |
| 608 | {{3, 3}, |
| 609 | {6, 6}, |
| 610 | {0, 0}, |
| 611 | {0, 0} |
| 612 | } |
| 613 | }, |
| 614 | {1, 0, 3, 0, /* 0x49 */ |
| 615 | {{0, 0}, |
| 616 | {3, 3}, |
| 617 | {6, 6}, |
| 618 | {0, 0} |
| 619 | } |
| 620 | }, |
| 621 | {0, 0, 3, 0, /* 0x4a */ |
| 622 | {{1, 1}, |
| 623 | {3, 3}, |
| 624 | {6, 6}, |
| 625 | {0, 0} |
| 626 | } |
| 627 | }, |
| 628 | {1, 0, 3, 0, /* 0x4b */ |
| 629 | {{0, 1}, |
| 630 | {3, 3}, |
| 631 | {6, 6}, |
| 632 | {0, 0} |
| 633 | } |
| 634 | }, |
| 635 | {0, 0, 2, 0, /* 0x4c */ |
| 636 | {{2, 3}, |
| 637 | {6, 6}, |
| 638 | {0, 0}, |
| 639 | {0, 0} |
| 640 | } |
| 641 | }, |
| 642 | {1, 0, 3, 0, /* 0x4d */ |
| 643 | {{0, 0}, |
| 644 | {2, 3}, |
| 645 | {6, 6}, |
| 646 | {0, 0} |
| 647 | } |
| 648 | }, |
| 649 | {0, 0, 2, 0, /* 0x4e */ |
| 650 | {{1, 3}, |
| 651 | {6, 6}, |
| 652 | {0, 0}, |
| 653 | {0, 0} |
| 654 | } |
| 655 | }, |
| 656 | {1, 0, 2, 0, /* 0x4f */ |
| 657 | {{0, 3}, |
| 658 | {6, 6}, |
| 659 | {0, 0}, |
| 660 | {0, 0} |
| 661 | } |
| 662 | }, |
| 663 | {0, 0, 2, 0, /* 0x50 */ |
| 664 | {{4, 4}, |
| 665 | {6, 6}, |
| 666 | {0, 0}, |
| 667 | {0, 0} |
| 668 | } |
| 669 | }, |
| 670 | {1, 0, 3, 0, /* 0x51 */ |
| 671 | {{0, 0}, |
| 672 | {4, 4}, |
| 673 | {6, 6}, |
| 674 | {0, 0} |
| 675 | } |
| 676 | }, |
| 677 | {0, 0, 3, 0, /* 0x52 */ |
| 678 | {{1, 1}, |
| 679 | {4, 4}, |
| 680 | {6, 6}, |
| 681 | {0, 0} |
| 682 | } |
| 683 | }, |
| 684 | {1, 0, 3, 0, /* 0x53 */ |
| 685 | {{0, 1}, |
| 686 | {4, 4}, |
| 687 | {6, 6}, |
| 688 | {0, 0} |
| 689 | } |
| 690 | }, |
| 691 | {0, 0, 3, 0, /* 0x54 */ |
| 692 | {{2, 2}, |
| 693 | {4, 4}, |
| 694 | {6, 6}, |
| 695 | {0, 0} |
| 696 | } |
| 697 | }, |
| 698 | {1, 0, 4, 0, /* 0x55 */ |
| 699 | {{0, 0}, |
| 700 | {2, 2}, |
| 701 | {4, 4}, |
| 702 | {6, 6} |
| 703 | } |
| 704 | }, |
| 705 | {0, 0, 3, 0, /* 0x56 */ |
| 706 | {{1, 2}, |
| 707 | {4, 4}, |
| 708 | {6, 6}, |
| 709 | {0, 0} |
| 710 | } |
| 711 | }, |
| 712 | {1, 0, 3, 0, /* 0x57 */ |
| 713 | {{0, 2}, |
| 714 | {4, 4}, |
| 715 | {6, 6}, |
| 716 | {0, 0} |
| 717 | } |
| 718 | }, |
| 719 | {0, 0, 2, 0, /* 0x58 */ |
| 720 | {{3, 4}, |
| 721 | {6, 6}, |
| 722 | {0, 0}, |
| 723 | {0, 0} |
| 724 | } |
| 725 | }, |
| 726 | {1, 0, 3, 0, /* 0x59 */ |
| 727 | {{0, 0}, |
| 728 | {3, 4}, |
| 729 | {6, 6}, |
| 730 | {0, 0} |
| 731 | } |
| 732 | }, |
| 733 | {0, 0, 3, 0, /* 0x5a */ |
| 734 | {{1, 1}, |
| 735 | {3, 4}, |
| 736 | {6, 6}, |
| 737 | {0, 0} |
| 738 | } |
| 739 | }, |
| 740 | {1, 0, 3, 0, /* 0x5b */ |
| 741 | {{0, 1}, |
| 742 | {3, 4}, |
| 743 | {6, 6}, |
| 744 | {0, 0} |
| 745 | } |
| 746 | }, |
| 747 | {0, 0, 2, 0, /* 0x5c */ |
| 748 | {{2, 4}, |
| 749 | {6, 6}, |
| 750 | {0, 0}, |
| 751 | {0, 0} |
| 752 | } |
| 753 | }, |
| 754 | {1, 0, 3, 0, /* 0x5d */ |
| 755 | {{0, 0}, |
| 756 | {2, 4}, |
| 757 | {6, 6}, |
| 758 | {0, 0} |
| 759 | } |
| 760 | }, |
| 761 | {0, 0, 2, 0, /* 0x5e */ |
| 762 | {{1, 4}, |
| 763 | {6, 6}, |
| 764 | {0, 0}, |
| 765 | {0, 0} |
| 766 | } |
| 767 | }, |
| 768 | {1, 0, 2, 0, /* 0x5f */ |
| 769 | {{0, 4}, |
| 770 | {6, 6}, |
| 771 | {0, 0}, |
| 772 | {0, 0} |
| 773 | } |
| 774 | }, |
| 775 | {0, 0, 1, 0, /* 0x60 */ |
| 776 | {{5, 6}, |
| 777 | {0, 0}, |
| 778 | {0, 0}, |
| 779 | {0, 0} |
| 780 | } |
| 781 | }, |
| 782 | {1, 0, 2, 0, /* 0x61 */ |
| 783 | {{0, 0}, |
| 784 | {5, 6}, |
| 785 | {0, 0}, |
| 786 | {0, 0} |
| 787 | } |
| 788 | }, |
| 789 | {0, 0, 2, 0, /* 0x62 */ |
| 790 | {{1, 1}, |
| 791 | {5, 6}, |
| 792 | {0, 0}, |
| 793 | {0, 0} |
| 794 | } |
| 795 | }, |
| 796 | {1, 0, 2, 0, /* 0x63 */ |
| 797 | {{0, 1}, |
| 798 | {5, 6}, |
| 799 | {0, 0}, |
| 800 | {0, 0} |
| 801 | } |
| 802 | }, |
| 803 | {0, 0, 2, 0, /* 0x64 */ |
| 804 | {{2, 2}, |
| 805 | {5, 6}, |
| 806 | {0, 0}, |
| 807 | {0, 0} |
| 808 | } |
| 809 | }, |
| 810 | {1, 0, 3, 0, /* 0x65 */ |
| 811 | {{0, 0}, |
| 812 | {2, 2}, |
| 813 | {5, 6}, |
| 814 | {0, 0} |
| 815 | } |
| 816 | }, |
| 817 | {0, 0, 2, 0, /* 0x66 */ |
| 818 | {{1, 2}, |
| 819 | {5, 6}, |
| 820 | {0, 0}, |
| 821 | {0, 0} |
| 822 | } |
| 823 | }, |
| 824 | {1, 0, 2, 0, /* 0x67 */ |
| 825 | {{0, 2}, |
| 826 | {5, 6}, |
| 827 | {0, 0}, |
| 828 | {0, 0} |
| 829 | } |
| 830 | }, |
| 831 | {0, 0, 2, 0, /* 0x68 */ |
| 832 | {{3, 3}, |
| 833 | {5, 6}, |
| 834 | {0, 0}, |
| 835 | {0, 0} |
| 836 | } |
| 837 | }, |
| 838 | {1, 0, 3, 0, /* 0x69 */ |
| 839 | {{0, 0}, |
| 840 | {3, 3}, |
| 841 | {5, 6}, |
| 842 | {0, 0} |
| 843 | } |
| 844 | }, |
| 845 | {0, 0, 3, 0, /* 0x6a */ |
| 846 | {{1, 1}, |
| 847 | {3, 3}, |
| 848 | {5, 6}, |
| 849 | {0, 0} |
| 850 | } |
| 851 | }, |
| 852 | {1, 0, 3, 0, /* 0x6b */ |
| 853 | {{0, 1}, |
| 854 | {3, 3}, |
| 855 | {5, 6}, |
| 856 | {0, 0} |
| 857 | } |
| 858 | }, |
| 859 | {0, 0, 2, 0, /* 0x6c */ |
| 860 | {{2, 3}, |
| 861 | {5, 6}, |
| 862 | {0, 0}, |
| 863 | {0, 0} |
| 864 | } |
| 865 | }, |
| 866 | {1, 0, 3, 0, /* 0x6d */ |
| 867 | {{0, 0}, |
| 868 | {2, 3}, |
| 869 | {5, 6}, |
| 870 | {0, 0} |
| 871 | } |
| 872 | }, |
| 873 | {0, 0, 2, 0, /* 0x6e */ |
| 874 | {{1, 3}, |
| 875 | {5, 6}, |
| 876 | {0, 0}, |
| 877 | {0, 0} |
| 878 | } |
| 879 | }, |
| 880 | {1, 0, 2, 0, /* 0x6f */ |
| 881 | {{0, 3}, |
| 882 | {5, 6}, |
| 883 | {0, 0}, |
| 884 | {0, 0} |
| 885 | } |
| 886 | }, |
| 887 | {0, 0, 1, 0, /* 0x70 */ |
| 888 | {{4, 6}, |
| 889 | {0, 0}, |
| 890 | {0, 0}, |
| 891 | {0, 0} |
| 892 | } |
| 893 | }, |
| 894 | {1, 0, 2, 0, /* 0x71 */ |
| 895 | {{0, 0}, |
| 896 | {4, 6}, |
| 897 | {0, 0}, |
| 898 | {0, 0} |
| 899 | } |
| 900 | }, |
| 901 | {0, 0, 2, 0, /* 0x72 */ |
| 902 | {{1, 1}, |
| 903 | {4, 6}, |
| 904 | {0, 0}, |
| 905 | {0, 0} |
| 906 | } |
| 907 | }, |
| 908 | {1, 0, 2, 0, /* 0x73 */ |
| 909 | {{0, 1}, |
| 910 | {4, 6}, |
| 911 | {0, 0}, |
| 912 | {0, 0} |
| 913 | } |
| 914 | }, |
| 915 | {0, 0, 2, 0, /* 0x74 */ |
| 916 | {{2, 2}, |
| 917 | {4, 6}, |
| 918 | {0, 0}, |
| 919 | {0, 0} |
| 920 | } |
| 921 | }, |
| 922 | {1, 0, 3, 0, /* 0x75 */ |
| 923 | {{0, 0}, |
| 924 | {2, 2}, |
| 925 | {4, 6}, |
| 926 | {0, 0} |
| 927 | } |
| 928 | }, |
| 929 | {0, 0, 2, 0, /* 0x76 */ |
| 930 | {{1, 2}, |
| 931 | {4, 6}, |
| 932 | {0, 0}, |
| 933 | {0, 0} |
| 934 | } |
| 935 | }, |
| 936 | {1, 0, 2, 0, /* 0x77 */ |
| 937 | {{0, 2}, |
| 938 | {4, 6}, |
| 939 | {0, 0}, |
| 940 | {0, 0} |
| 941 | } |
| 942 | }, |
| 943 | {0, 0, 1, 0, /* 0x78 */ |
| 944 | {{3, 6}, |
| 945 | {0, 0}, |
| 946 | {0, 0}, |
| 947 | {0, 0} |
| 948 | } |
| 949 | }, |
| 950 | {1, 0, 2, 0, /* 0x79 */ |
| 951 | {{0, 0}, |
| 952 | {3, 6}, |
| 953 | {0, 0}, |
| 954 | {0, 0} |
| 955 | } |
| 956 | }, |
| 957 | {0, 0, 2, 0, /* 0x7a */ |
| 958 | {{1, 1}, |
| 959 | {3, 6}, |
| 960 | {0, 0}, |
| 961 | {0, 0} |
| 962 | } |
| 963 | }, |
| 964 | {1, 0, 2, 0, /* 0x7b */ |
| 965 | {{0, 1}, |
| 966 | {3, 6}, |
| 967 | {0, 0}, |
| 968 | {0, 0} |
| 969 | } |
| 970 | }, |
| 971 | {0, 0, 1, 0, /* 0x7c */ |
| 972 | {{2, 6}, |
| 973 | {0, 0}, |
| 974 | {0, 0}, |
| 975 | {0, 0} |
| 976 | } |
| 977 | }, |
| 978 | {1, 0, 2, 0, /* 0x7d */ |
| 979 | {{0, 0}, |
| 980 | {2, 6}, |
| 981 | {0, 0}, |
| 982 | {0, 0} |
| 983 | } |
| 984 | }, |
| 985 | {0, 0, 1, 0, /* 0x7e */ |
| 986 | {{1, 6}, |
| 987 | {0, 0}, |
| 988 | {0, 0}, |
| 989 | {0, 0} |
| 990 | } |
| 991 | }, |
| 992 | {1, 0, 1, 0, /* 0x7f */ |
| 993 | {{0, 6}, |
| 994 | {0, 0}, |
| 995 | {0, 0}, |
| 996 | {0, 0} |
| 997 | } |
| 998 | }, |
| 999 | {0, 1, 1, 0, /* 0x80 */ |
| 1000 | {{7, 7}, |
| 1001 | {0, 0}, |
| 1002 | {0, 0}, |
| 1003 | {0, 0} |
| 1004 | } |
| 1005 | }, |
| 1006 | {1, 1, 2, 0, /* 0x81 */ |
| 1007 | {{0, 0}, |
| 1008 | {7, 7}, |
| 1009 | {0, 0}, |
| 1010 | {0, 0} |
| 1011 | } |
| 1012 | }, |
| 1013 | {0, 1, 2, 0, /* 0x82 */ |
| 1014 | {{1, 1}, |
| 1015 | {7, 7}, |
| 1016 | {0, 0}, |
| 1017 | {0, 0} |
| 1018 | } |
| 1019 | }, |
| 1020 | {1, 1, 2, 0, /* 0x83 */ |
| 1021 | {{0, 1}, |
| 1022 | {7, 7}, |
| 1023 | {0, 0}, |
| 1024 | {0, 0} |
| 1025 | } |
| 1026 | }, |
| 1027 | {0, 1, 2, 0, /* 0x84 */ |
| 1028 | {{2, 2}, |
| 1029 | {7, 7}, |
| 1030 | {0, 0}, |
| 1031 | {0, 0} |
| 1032 | } |
| 1033 | }, |
| 1034 | {1, 1, 3, 0, /* 0x85 */ |
| 1035 | {{0, 0}, |
| 1036 | {2, 2}, |
| 1037 | {7, 7}, |
| 1038 | {0, 0} |
| 1039 | } |
| 1040 | }, |
| 1041 | {0, 1, 2, 0, /* 0x86 */ |
| 1042 | {{1, 2}, |
| 1043 | {7, 7}, |
| 1044 | {0, 0}, |
| 1045 | {0, 0} |
| 1046 | } |
| 1047 | }, |
| 1048 | {1, 1, 2, 0, /* 0x87 */ |
| 1049 | {{0, 2}, |
| 1050 | {7, 7}, |
| 1051 | {0, 0}, |
| 1052 | {0, 0} |
| 1053 | } |
| 1054 | }, |
| 1055 | {0, 1, 2, 0, /* 0x88 */ |
| 1056 | {{3, 3}, |
| 1057 | {7, 7}, |
| 1058 | {0, 0}, |
| 1059 | {0, 0} |
| 1060 | } |
| 1061 | }, |
| 1062 | {1, 1, 3, 0, /* 0x89 */ |
| 1063 | {{0, 0}, |
| 1064 | {3, 3}, |
| 1065 | {7, 7}, |
| 1066 | {0, 0} |
| 1067 | } |
| 1068 | }, |
| 1069 | {0, 1, 3, 0, /* 0x8a */ |
| 1070 | {{1, 1}, |
| 1071 | {3, 3}, |
| 1072 | {7, 7}, |
| 1073 | {0, 0} |
| 1074 | } |
| 1075 | }, |
| 1076 | {1, 1, 3, 0, /* 0x8b */ |
| 1077 | {{0, 1}, |
| 1078 | {3, 3}, |
| 1079 | {7, 7}, |
| 1080 | {0, 0} |
| 1081 | } |
| 1082 | }, |
| 1083 | {0, 1, 2, 0, /* 0x8c */ |
| 1084 | {{2, 3}, |
| 1085 | {7, 7}, |
| 1086 | {0, 0}, |
| 1087 | {0, 0} |
| 1088 | } |
| 1089 | }, |
| 1090 | {1, 1, 3, 0, /* 0x8d */ |
| 1091 | {{0, 0}, |
| 1092 | {2, 3}, |
| 1093 | {7, 7}, |
| 1094 | {0, 0} |
| 1095 | } |
| 1096 | }, |
| 1097 | {0, 1, 2, 0, /* 0x8e */ |
| 1098 | {{1, 3}, |
| 1099 | {7, 7}, |
| 1100 | {0, 0}, |
| 1101 | {0, 0} |
| 1102 | } |
| 1103 | }, |
| 1104 | {1, 1, 2, 0, /* 0x8f */ |
| 1105 | {{0, 3}, |
| 1106 | {7, 7}, |
| 1107 | {0, 0}, |
| 1108 | {0, 0} |
| 1109 | } |
| 1110 | }, |
| 1111 | {0, 1, 2, 0, /* 0x90 */ |
| 1112 | {{4, 4}, |
| 1113 | {7, 7}, |
| 1114 | {0, 0}, |
| 1115 | {0, 0} |
| 1116 | } |
| 1117 | }, |
| 1118 | {1, 1, 3, 0, /* 0x91 */ |
| 1119 | {{0, 0}, |
| 1120 | {4, 4}, |
| 1121 | {7, 7}, |
| 1122 | {0, 0} |
| 1123 | } |
| 1124 | }, |
| 1125 | {0, 1, 3, 0, /* 0x92 */ |
| 1126 | {{1, 1}, |
| 1127 | {4, 4}, |
| 1128 | {7, 7}, |
| 1129 | {0, 0} |
| 1130 | } |
| 1131 | }, |
| 1132 | {1, 1, 3, 0, /* 0x93 */ |
| 1133 | {{0, 1}, |
| 1134 | {4, 4}, |
| 1135 | {7, 7}, |
| 1136 | {0, 0} |
| 1137 | } |
| 1138 | }, |
| 1139 | {0, 1, 3, 0, /* 0x94 */ |
| 1140 | {{2, 2}, |
| 1141 | {4, 4}, |
| 1142 | {7, 7}, |
| 1143 | {0, 0} |
| 1144 | } |
| 1145 | }, |
| 1146 | {1, 1, 4, 0, /* 0x95 */ |
| 1147 | {{0, 0}, |
| 1148 | {2, 2}, |
| 1149 | {4, 4}, |
| 1150 | {7, 7} |
| 1151 | } |
| 1152 | }, |
| 1153 | {0, 1, 3, 0, /* 0x96 */ |
| 1154 | {{1, 2}, |
| 1155 | {4, 4}, |
| 1156 | {7, 7}, |
| 1157 | {0, 0} |
| 1158 | } |
| 1159 | }, |
| 1160 | {1, 1, 3, 0, /* 0x97 */ |
| 1161 | {{0, 2}, |
| 1162 | {4, 4}, |
| 1163 | {7, 7}, |
| 1164 | {0, 0} |
| 1165 | } |
| 1166 | }, |
| 1167 | {0, 1, 2, 0, /* 0x98 */ |
| 1168 | {{3, 4}, |
| 1169 | {7, 7}, |
| 1170 | {0, 0}, |
| 1171 | {0, 0} |
| 1172 | } |
| 1173 | }, |
| 1174 | {1, 1, 3, 0, /* 0x99 */ |
| 1175 | {{0, 0}, |
| 1176 | {3, 4}, |
| 1177 | {7, 7}, |
| 1178 | {0, 0} |
| 1179 | } |
| 1180 | }, |
| 1181 | {0, 1, 3, 0, /* 0x9a */ |
| 1182 | {{1, 1}, |
| 1183 | {3, 4}, |
| 1184 | {7, 7}, |
| 1185 | {0, 0} |
| 1186 | } |
| 1187 | }, |
| 1188 | {1, 1, 3, 0, /* 0x9b */ |
| 1189 | {{0, 1}, |
| 1190 | {3, 4}, |
| 1191 | {7, 7}, |
| 1192 | {0, 0} |
| 1193 | } |
| 1194 | }, |
| 1195 | {0, 1, 2, 0, /* 0x9c */ |
| 1196 | {{2, 4}, |
| 1197 | {7, 7}, |
| 1198 | {0, 0}, |
| 1199 | {0, 0} |
| 1200 | } |
| 1201 | }, |
| 1202 | {1, 1, 3, 0, /* 0x9d */ |
| 1203 | {{0, 0}, |
| 1204 | {2, 4}, |
| 1205 | {7, 7}, |
| 1206 | {0, 0} |
| 1207 | } |
| 1208 | }, |
| 1209 | {0, 1, 2, 0, /* 0x9e */ |
| 1210 | {{1, 4}, |
| 1211 | {7, 7}, |
| 1212 | {0, 0}, |
| 1213 | {0, 0} |
| 1214 | } |
| 1215 | }, |
| 1216 | {1, 1, 2, 0, /* 0x9f */ |
| 1217 | {{0, 4}, |
| 1218 | {7, 7}, |
| 1219 | {0, 0}, |
| 1220 | {0, 0} |
| 1221 | } |
| 1222 | }, |
| 1223 | {0, 1, 2, 0, /* 0xa0 */ |
| 1224 | {{5, 5}, |
| 1225 | {7, 7}, |
| 1226 | {0, 0}, |
| 1227 | {0, 0} |
| 1228 | } |
| 1229 | }, |
| 1230 | {1, 1, 3, 0, /* 0xa1 */ |
| 1231 | {{0, 0}, |
| 1232 | {5, 5}, |
| 1233 | {7, 7}, |
| 1234 | {0, 0} |
| 1235 | } |
| 1236 | }, |
| 1237 | {0, 1, 3, 0, /* 0xa2 */ |
| 1238 | {{1, 1}, |
| 1239 | {5, 5}, |
| 1240 | {7, 7}, |
| 1241 | {0, 0} |
| 1242 | } |
| 1243 | }, |
| 1244 | {1, 1, 3, 0, /* 0xa3 */ |
| 1245 | {{0, 1}, |
| 1246 | {5, 5}, |
| 1247 | {7, 7}, |
| 1248 | {0, 0} |
| 1249 | } |
| 1250 | }, |
| 1251 | {0, 1, 3, 0, /* 0xa4 */ |
| 1252 | {{2, 2}, |
| 1253 | {5, 5}, |
| 1254 | {7, 7}, |
| 1255 | {0, 0} |
| 1256 | } |
| 1257 | }, |
| 1258 | {1, 1, 4, 0, /* 0xa5 */ |
| 1259 | {{0, 0}, |
| 1260 | {2, 2}, |
| 1261 | {5, 5}, |
| 1262 | {7, 7} |
| 1263 | } |
| 1264 | }, |
| 1265 | {0, 1, 3, 0, /* 0xa6 */ |
| 1266 | {{1, 2}, |
| 1267 | {5, 5}, |
| 1268 | {7, 7}, |
| 1269 | {0, 0} |
| 1270 | } |
| 1271 | }, |
| 1272 | {1, 1, 3, 0, /* 0xa7 */ |
| 1273 | {{0, 2}, |
| 1274 | {5, 5}, |
| 1275 | {7, 7}, |
| 1276 | {0, 0} |
| 1277 | } |
| 1278 | }, |
| 1279 | {0, 1, 3, 0, /* 0xa8 */ |
| 1280 | {{3, 3}, |
| 1281 | {5, 5}, |
| 1282 | {7, 7}, |
| 1283 | {0, 0} |
| 1284 | } |
| 1285 | }, |
| 1286 | {1, 1, 4, 0, /* 0xa9 */ |
| 1287 | {{0, 0}, |
| 1288 | {3, 3}, |
| 1289 | {5, 5}, |
| 1290 | {7, 7} |
| 1291 | } |
| 1292 | }, |
| 1293 | {0, 1, 4, 0, /* 0xaa */ |
| 1294 | {{1, 1}, |
| 1295 | {3, 3}, |
| 1296 | {5, 5}, |
| 1297 | {7, 7} |
| 1298 | } |
| 1299 | }, |
| 1300 | {1, 1, 4, 0, /* 0xab */ |
| 1301 | {{0, 1}, |
| 1302 | {3, 3}, |
| 1303 | {5, 5}, |
| 1304 | {7, 7} |
| 1305 | } |
| 1306 | }, |
| 1307 | {0, 1, 3, 0, /* 0xac */ |
| 1308 | {{2, 3}, |
| 1309 | {5, 5}, |
| 1310 | {7, 7}, |
| 1311 | {0, 0} |
| 1312 | } |
| 1313 | }, |
| 1314 | {1, 1, 4, 0, /* 0xad */ |
| 1315 | {{0, 0}, |
| 1316 | {2, 3}, |
| 1317 | {5, 5}, |
| 1318 | {7, 7} |
| 1319 | } |
| 1320 | }, |
| 1321 | {0, 1, 3, 0, /* 0xae */ |
| 1322 | {{1, 3}, |
| 1323 | {5, 5}, |
| 1324 | {7, 7}, |
| 1325 | {0, 0} |
| 1326 | } |
| 1327 | }, |
| 1328 | {1, 1, 3, 0, /* 0xaf */ |
| 1329 | {{0, 3}, |
| 1330 | {5, 5}, |
| 1331 | {7, 7}, |
| 1332 | {0, 0} |
| 1333 | } |
| 1334 | }, |
| 1335 | {0, 1, 2, 0, /* 0xb0 */ |
| 1336 | {{4, 5}, |
| 1337 | {7, 7}, |
| 1338 | {0, 0}, |
| 1339 | {0, 0} |
| 1340 | } |
| 1341 | }, |
| 1342 | {1, 1, 3, 0, /* 0xb1 */ |
| 1343 | {{0, 0}, |
| 1344 | {4, 5}, |
| 1345 | {7, 7}, |
| 1346 | {0, 0} |
| 1347 | } |
| 1348 | }, |
| 1349 | {0, 1, 3, 0, /* 0xb2 */ |
| 1350 | {{1, 1}, |
| 1351 | {4, 5}, |
| 1352 | {7, 7}, |
| 1353 | {0, 0} |
| 1354 | } |
| 1355 | }, |
| 1356 | {1, 1, 3, 0, /* 0xb3 */ |
| 1357 | {{0, 1}, |
| 1358 | {4, 5}, |
| 1359 | {7, 7}, |
| 1360 | {0, 0} |
| 1361 | } |
| 1362 | }, |
| 1363 | {0, 1, 3, 0, /* 0xb4 */ |
| 1364 | {{2, 2}, |
| 1365 | {4, 5}, |
| 1366 | {7, 7}, |
| 1367 | {0, 0} |
| 1368 | } |
| 1369 | }, |
| 1370 | {1, 1, 4, 0, /* 0xb5 */ |
| 1371 | {{0, 0}, |
| 1372 | {2, 2}, |
| 1373 | {4, 5}, |
| 1374 | {7, 7} |
| 1375 | } |
| 1376 | }, |
| 1377 | {0, 1, 3, 0, /* 0xb6 */ |
| 1378 | {{1, 2}, |
| 1379 | {4, 5}, |
| 1380 | {7, 7}, |
| 1381 | {0, 0} |
| 1382 | } |
| 1383 | }, |
| 1384 | {1, 1, 3, 0, /* 0xb7 */ |
| 1385 | {{0, 2}, |
| 1386 | {4, 5}, |
| 1387 | {7, 7}, |
| 1388 | {0, 0} |
| 1389 | } |
| 1390 | }, |
| 1391 | {0, 1, 2, 0, /* 0xb8 */ |
| 1392 | {{3, 5}, |
| 1393 | {7, 7}, |
| 1394 | {0, 0}, |
| 1395 | {0, 0} |
| 1396 | } |
| 1397 | }, |
| 1398 | {1, 1, 3, 0, /* 0xb9 */ |
| 1399 | {{0, 0}, |
| 1400 | {3, 5}, |
| 1401 | {7, 7}, |
| 1402 | {0, 0} |
| 1403 | } |
| 1404 | }, |
| 1405 | {0, 1, 3, 0, /* 0xba */ |
| 1406 | {{1, 1}, |
| 1407 | {3, 5}, |
| 1408 | {7, 7}, |
| 1409 | {0, 0} |
| 1410 | } |
| 1411 | }, |
| 1412 | {1, 1, 3, 0, /* 0xbb */ |
| 1413 | {{0, 1}, |
| 1414 | {3, 5}, |
| 1415 | {7, 7}, |
| 1416 | {0, 0} |
| 1417 | } |
| 1418 | }, |
| 1419 | {0, 1, 2, 0, /* 0xbc */ |
| 1420 | {{2, 5}, |
| 1421 | {7, 7}, |
| 1422 | {0, 0}, |
| 1423 | {0, 0} |
| 1424 | } |
| 1425 | }, |
| 1426 | {1, 1, 3, 0, /* 0xbd */ |
| 1427 | {{0, 0}, |
| 1428 | {2, 5}, |
| 1429 | {7, 7}, |
| 1430 | {0, 0} |
| 1431 | } |
| 1432 | }, |
| 1433 | {0, 1, 2, 0, /* 0xbe */ |
| 1434 | {{1, 5}, |
| 1435 | {7, 7}, |
| 1436 | {0, 0}, |
| 1437 | {0, 0} |
| 1438 | } |
| 1439 | }, |
| 1440 | {1, 1, 2, 0, /* 0xbf */ |
| 1441 | {{0, 5}, |
| 1442 | {7, 7}, |
| 1443 | {0, 0}, |
| 1444 | {0, 0} |
| 1445 | } |
| 1446 | }, |
| 1447 | {0, 1, 1, 0, /* 0xc0 */ |
| 1448 | {{6, 7}, |
| 1449 | {0, 0}, |
| 1450 | {0, 0}, |
| 1451 | {0, 0} |
| 1452 | } |
| 1453 | }, |
| 1454 | {1, 1, 2, 0, /* 0xc1 */ |
| 1455 | {{0, 0}, |
| 1456 | {6, 7}, |
| 1457 | {0, 0}, |
| 1458 | {0, 0} |
| 1459 | } |
| 1460 | }, |
| 1461 | {0, 1, 2, 0, /* 0xc2 */ |
| 1462 | {{1, 1}, |
| 1463 | {6, 7}, |
| 1464 | {0, 0}, |
| 1465 | {0, 0} |
| 1466 | } |
| 1467 | }, |
| 1468 | {1, 1, 2, 0, /* 0xc3 */ |
| 1469 | {{0, 1}, |
| 1470 | {6, 7}, |
| 1471 | {0, 0}, |
| 1472 | {0, 0} |
| 1473 | } |
| 1474 | }, |
| 1475 | {0, 1, 2, 0, /* 0xc4 */ |
| 1476 | {{2, 2}, |
| 1477 | {6, 7}, |
| 1478 | {0, 0}, |
| 1479 | {0, 0} |
| 1480 | } |
| 1481 | }, |
| 1482 | {1, 1, 3, 0, /* 0xc5 */ |
| 1483 | {{0, 0}, |
| 1484 | {2, 2}, |
| 1485 | {6, 7}, |
| 1486 | {0, 0} |
| 1487 | } |
| 1488 | }, |
| 1489 | {0, 1, 2, 0, /* 0xc6 */ |
| 1490 | {{1, 2}, |
| 1491 | {6, 7}, |
| 1492 | {0, 0}, |
| 1493 | {0, 0} |
| 1494 | } |
| 1495 | }, |
| 1496 | {1, 1, 2, 0, /* 0xc7 */ |
| 1497 | {{0, 2}, |
| 1498 | {6, 7}, |
| 1499 | {0, 0}, |
| 1500 | {0, 0} |
| 1501 | } |
| 1502 | }, |
| 1503 | {0, 1, 2, 0, /* 0xc8 */ |
| 1504 | {{3, 3}, |
| 1505 | {6, 7}, |
| 1506 | {0, 0}, |
| 1507 | {0, 0} |
| 1508 | } |
| 1509 | }, |
| 1510 | {1, 1, 3, 0, /* 0xc9 */ |
| 1511 | {{0, 0}, |
| 1512 | {3, 3}, |
| 1513 | {6, 7}, |
| 1514 | {0, 0} |
| 1515 | } |
| 1516 | }, |
| 1517 | {0, 1, 3, 0, /* 0xca */ |
| 1518 | {{1, 1}, |
| 1519 | {3, 3}, |
| 1520 | {6, 7}, |
| 1521 | {0, 0} |
| 1522 | } |
| 1523 | }, |
| 1524 | {1, 1, 3, 0, /* 0xcb */ |
| 1525 | {{0, 1}, |
| 1526 | {3, 3}, |
| 1527 | {6, 7}, |
| 1528 | {0, 0} |
| 1529 | } |
| 1530 | }, |
| 1531 | {0, 1, 2, 0, /* 0xcc */ |
| 1532 | {{2, 3}, |
| 1533 | {6, 7}, |
| 1534 | {0, 0}, |
| 1535 | {0, 0} |
| 1536 | } |
| 1537 | }, |
| 1538 | {1, 1, 3, 0, /* 0xcd */ |
| 1539 | {{0, 0}, |
| 1540 | {2, 3}, |
| 1541 | {6, 7}, |
| 1542 | {0, 0} |
| 1543 | } |
| 1544 | }, |
| 1545 | {0, 1, 2, 0, /* 0xce */ |
| 1546 | {{1, 3}, |
| 1547 | {6, 7}, |
| 1548 | {0, 0}, |
| 1549 | {0, 0} |
| 1550 | } |
| 1551 | }, |
| 1552 | {1, 1, 2, 0, /* 0xcf */ |
| 1553 | {{0, 3}, |
| 1554 | {6, 7}, |
| 1555 | {0, 0}, |
| 1556 | {0, 0} |
| 1557 | } |
| 1558 | }, |
| 1559 | {0, 1, 2, 0, /* 0xd0 */ |
| 1560 | {{4, 4}, |
| 1561 | {6, 7}, |
| 1562 | {0, 0}, |
| 1563 | {0, 0} |
| 1564 | } |
| 1565 | }, |
| 1566 | {1, 1, 3, 0, /* 0xd1 */ |
| 1567 | {{0, 0}, |
| 1568 | {4, 4}, |
| 1569 | {6, 7}, |
| 1570 | {0, 0} |
| 1571 | } |
| 1572 | }, |
| 1573 | {0, 1, 3, 0, /* 0xd2 */ |
| 1574 | {{1, 1}, |
| 1575 | {4, 4}, |
| 1576 | {6, 7}, |
| 1577 | {0, 0} |
| 1578 | } |
| 1579 | }, |
| 1580 | {1, 1, 3, 0, /* 0xd3 */ |
| 1581 | {{0, 1}, |
| 1582 | {4, 4}, |
| 1583 | {6, 7}, |
| 1584 | {0, 0} |
| 1585 | } |
| 1586 | }, |
| 1587 | {0, 1, 3, 0, /* 0xd4 */ |
| 1588 | {{2, 2}, |
| 1589 | {4, 4}, |
| 1590 | {6, 7}, |
| 1591 | {0, 0} |
| 1592 | } |
| 1593 | }, |
| 1594 | {1, 1, 4, 0, /* 0xd5 */ |
| 1595 | {{0, 0}, |
| 1596 | {2, 2}, |
| 1597 | {4, 4}, |
| 1598 | {6, 7} |
| 1599 | } |
| 1600 | }, |
| 1601 | {0, 1, 3, 0, /* 0xd6 */ |
| 1602 | {{1, 2}, |
| 1603 | {4, 4}, |
| 1604 | {6, 7}, |
| 1605 | {0, 0} |
| 1606 | } |
| 1607 | }, |
| 1608 | {1, 1, 3, 0, /* 0xd7 */ |
| 1609 | {{0, 2}, |
| 1610 | {4, 4}, |
| 1611 | {6, 7}, |
| 1612 | {0, 0} |
| 1613 | } |
| 1614 | }, |
| 1615 | {0, 1, 2, 0, /* 0xd8 */ |
| 1616 | {{3, 4}, |
| 1617 | {6, 7}, |
| 1618 | {0, 0}, |
| 1619 | {0, 0} |
| 1620 | } |
| 1621 | }, |
| 1622 | {1, 1, 3, 0, /* 0xd9 */ |
| 1623 | {{0, 0}, |
| 1624 | {3, 4}, |
| 1625 | {6, 7}, |
| 1626 | {0, 0} |
| 1627 | } |
| 1628 | }, |
| 1629 | {0, 1, 3, 0, /* 0xda */ |
| 1630 | {{1, 1}, |
| 1631 | {3, 4}, |
| 1632 | {6, 7}, |
| 1633 | {0, 0} |
| 1634 | } |
| 1635 | }, |
| 1636 | {1, 1, 3, 0, /* 0xdb */ |
| 1637 | {{0, 1}, |
| 1638 | {3, 4}, |
| 1639 | {6, 7}, |
| 1640 | {0, 0} |
| 1641 | } |
| 1642 | }, |
| 1643 | {0, 1, 2, 0, /* 0xdc */ |
| 1644 | {{2, 4}, |
| 1645 | {6, 7}, |
| 1646 | {0, 0}, |
| 1647 | {0, 0} |
| 1648 | } |
| 1649 | }, |
| 1650 | {1, 1, 3, 0, /* 0xdd */ |
| 1651 | {{0, 0}, |
| 1652 | {2, 4}, |
| 1653 | {6, 7}, |
| 1654 | {0, 0} |
| 1655 | } |
| 1656 | }, |
| 1657 | {0, 1, 2, 0, /* 0xde */ |
| 1658 | {{1, 4}, |
| 1659 | {6, 7}, |
| 1660 | {0, 0}, |
| 1661 | {0, 0} |
| 1662 | } |
| 1663 | }, |
| 1664 | {1, 1, 2, 0, /* 0xdf */ |
| 1665 | {{0, 4}, |
| 1666 | {6, 7}, |
| 1667 | {0, 0}, |
| 1668 | {0, 0} |
| 1669 | } |
| 1670 | }, |
| 1671 | {0, 1, 1, 0, /* 0xe0 */ |
| 1672 | {{5, 7}, |
| 1673 | {0, 0}, |
| 1674 | {0, 0}, |
| 1675 | {0, 0} |
| 1676 | } |
| 1677 | }, |
| 1678 | {1, 1, 2, 0, /* 0xe1 */ |
| 1679 | {{0, 0}, |
| 1680 | {5, 7}, |
| 1681 | {0, 0}, |
| 1682 | {0, 0} |
| 1683 | } |
| 1684 | }, |
| 1685 | {0, 1, 2, 0, /* 0xe2 */ |
| 1686 | {{1, 1}, |
| 1687 | {5, 7}, |
| 1688 | {0, 0}, |
| 1689 | {0, 0} |
| 1690 | } |
| 1691 | }, |
| 1692 | {1, 1, 2, 0, /* 0xe3 */ |
| 1693 | {{0, 1}, |
| 1694 | {5, 7}, |
| 1695 | {0, 0}, |
| 1696 | {0, 0} |
| 1697 | } |
| 1698 | }, |
| 1699 | {0, 1, 2, 0, /* 0xe4 */ |
| 1700 | {{2, 2}, |
| 1701 | {5, 7}, |
| 1702 | {0, 0}, |
| 1703 | {0, 0} |
| 1704 | } |
| 1705 | }, |
| 1706 | {1, 1, 3, 0, /* 0xe5 */ |
| 1707 | {{0, 0}, |
| 1708 | {2, 2}, |
| 1709 | {5, 7}, |
| 1710 | {0, 0} |
| 1711 | } |
| 1712 | }, |
| 1713 | {0, 1, 2, 0, /* 0xe6 */ |
| 1714 | {{1, 2}, |
| 1715 | {5, 7}, |
| 1716 | {0, 0}, |
| 1717 | {0, 0} |
| 1718 | } |
| 1719 | }, |
| 1720 | {1, 1, 2, 0, /* 0xe7 */ |
| 1721 | {{0, 2}, |
| 1722 | {5, 7}, |
| 1723 | {0, 0}, |
| 1724 | {0, 0} |
| 1725 | } |
| 1726 | }, |
| 1727 | {0, 1, 2, 0, /* 0xe8 */ |
| 1728 | {{3, 3}, |
| 1729 | {5, 7}, |
| 1730 | {0, 0}, |
| 1731 | {0, 0} |
| 1732 | } |
| 1733 | }, |
| 1734 | {1, 1, 3, 0, /* 0xe9 */ |
| 1735 | {{0, 0}, |
| 1736 | {3, 3}, |
| 1737 | {5, 7}, |
| 1738 | {0, 0} |
| 1739 | } |
| 1740 | }, |
| 1741 | {0, 1, 3, 0, /* 0xea */ |
| 1742 | {{1, 1}, |
| 1743 | {3, 3}, |
| 1744 | {5, 7}, |
| 1745 | {0, 0} |
| 1746 | } |
| 1747 | }, |
| 1748 | {1, 1, 3, 0, /* 0xeb */ |
| 1749 | {{0, 1}, |
| 1750 | {3, 3}, |
| 1751 | {5, 7}, |
| 1752 | {0, 0} |
| 1753 | } |
| 1754 | }, |
| 1755 | {0, 1, 2, 0, /* 0xec */ |
| 1756 | {{2, 3}, |
| 1757 | {5, 7}, |
| 1758 | {0, 0}, |
| 1759 | {0, 0} |
| 1760 | } |
| 1761 | }, |
| 1762 | {1, 1, 3, 0, /* 0xed */ |
| 1763 | {{0, 0}, |
| 1764 | {2, 3}, |
| 1765 | {5, 7}, |
| 1766 | {0, 0} |
| 1767 | } |
| 1768 | }, |
| 1769 | {0, 1, 2, 0, /* 0xee */ |
| 1770 | {{1, 3}, |
| 1771 | {5, 7}, |
| 1772 | {0, 0}, |
| 1773 | {0, 0} |
| 1774 | } |
| 1775 | }, |
| 1776 | {1, 1, 2, 0, /* 0xef */ |
| 1777 | {{0, 3}, |
| 1778 | {5, 7}, |
| 1779 | {0, 0}, |
| 1780 | {0, 0} |
| 1781 | } |
| 1782 | }, |
| 1783 | {0, 1, 1, 0, /* 0xf0 */ |
| 1784 | {{4, 7}, |
| 1785 | {0, 0}, |
| 1786 | {0, 0}, |
| 1787 | {0, 0} |
| 1788 | } |
| 1789 | }, |
| 1790 | {1, 1, 2, 0, /* 0xf1 */ |
| 1791 | {{0, 0}, |
| 1792 | {4, 7}, |
| 1793 | {0, 0}, |
| 1794 | {0, 0} |
| 1795 | } |
| 1796 | }, |
| 1797 | {0, 1, 2, 0, /* 0xf2 */ |
| 1798 | {{1, 1}, |
| 1799 | {4, 7}, |
| 1800 | {0, 0}, |
| 1801 | {0, 0} |
| 1802 | } |
| 1803 | }, |
| 1804 | {1, 1, 2, 0, /* 0xf3 */ |
| 1805 | {{0, 1}, |
| 1806 | {4, 7}, |
| 1807 | {0, 0}, |
| 1808 | {0, 0} |
| 1809 | } |
| 1810 | }, |
| 1811 | {0, 1, 2, 0, /* 0xf4 */ |
| 1812 | {{2, 2}, |
| 1813 | {4, 7}, |
| 1814 | {0, 0}, |
| 1815 | {0, 0} |
| 1816 | } |
| 1817 | }, |
| 1818 | {1, 1, 3, 0, /* 0xf5 */ |
| 1819 | {{0, 0}, |
| 1820 | {2, 2}, |
| 1821 | {4, 7}, |
| 1822 | {0, 0} |
| 1823 | } |
| 1824 | }, |
| 1825 | {0, 1, 2, 0, /* 0xf6 */ |
| 1826 | {{1, 2}, |
| 1827 | {4, 7}, |
| 1828 | {0, 0}, |
| 1829 | {0, 0} |
| 1830 | } |
| 1831 | }, |
| 1832 | {1, 1, 2, 0, /* 0xf7 */ |
| 1833 | {{0, 2}, |
| 1834 | {4, 7}, |
| 1835 | {0, 0}, |
| 1836 | {0, 0} |
| 1837 | } |
| 1838 | }, |
| 1839 | {0, 1, 1, 0, /* 0xf8 */ |
| 1840 | {{3, 7}, |
| 1841 | {0, 0}, |
| 1842 | {0, 0}, |
| 1843 | {0, 0} |
| 1844 | } |
| 1845 | }, |
| 1846 | {1, 1, 2, 0, /* 0xf9 */ |
| 1847 | {{0, 0}, |
| 1848 | {3, 7}, |
| 1849 | {0, 0}, |
| 1850 | {0, 0} |
| 1851 | } |
| 1852 | }, |
| 1853 | {0, 1, 2, 0, /* 0xfa */ |
| 1854 | {{1, 1}, |
| 1855 | {3, 7}, |
| 1856 | {0, 0}, |
| 1857 | {0, 0} |
| 1858 | } |
| 1859 | }, |
| 1860 | {1, 1, 2, 0, /* 0xfb */ |
| 1861 | {{0, 1}, |
| 1862 | {3, 7}, |
| 1863 | {0, 0}, |
| 1864 | {0, 0} |
| 1865 | } |
| 1866 | }, |
| 1867 | {0, 1, 1, 0, /* 0xfc */ |
| 1868 | {{2, 7}, |
| 1869 | {0, 0}, |
| 1870 | {0, 0}, |
| 1871 | {0, 0} |
| 1872 | } |
| 1873 | }, |
| 1874 | {1, 1, 2, 0, /* 0xfd */ |
| 1875 | {{0, 0}, |
| 1876 | {2, 7}, |
| 1877 | {0, 0}, |
| 1878 | {0, 0} |
| 1879 | } |
| 1880 | }, |
| 1881 | {0, 1, 1, 0, /* 0xfe */ |
| 1882 | {{1, 7}, |
| 1883 | {0, 0}, |
| 1884 | {0, 0}, |
| 1885 | {0, 0} |
| 1886 | } |
| 1887 | }, |
| 1888 | {1, 1, 1, 0, /* 0xff */ |
| 1889 | {{0, 7}, |
| 1890 | {0, 0}, |
| 1891 | {0, 0}, |
| 1892 | {0, 0} |
| 1893 | } |
| 1894 | } |
| 1895 | }; |
| 1896 |
| 1897 |
| 1898 | int |
| 1899 | sctp_is_address_in_scope(struct sctp_ifa *ifa, |
| 1900 | struct sctp_scoping *scope, |
| 1901 | int do_update) |
| 1902 | { |
| 1903 | if ((scope->loopback_scope == 0) && |
| 1904 | (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) { |
| 1905 | /* |
| 1906 | * skip loopback if not in scope * |
| 1907 | */ |
| 1908 | return (0); |
| 1909 | } |
| 1910 | switch (ifa->address.sa.sa_family) { |
| 1911 | #ifdef INET |
| 1912 | case AF_INET: |
| 1913 | if (scope->ipv4_addr_legal) { |
| 1914 | struct sockaddr_in *sin; |
| 1915 |
| 1916 | sin = &ifa->address.sin; |
| 1917 | if (sin->sin_addr.s_addr == 0) { |
| 1918 | /* not in scope , unspecified */ |
| 1919 | return (0); |
| 1920 | } |
| 1921 | if ((scope->ipv4_local_scope == 0) && |
| 1922 | (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) { |
| 1923 | /* private address not in scope */ |
| 1924 | return (0); |
| 1925 | } |
| 1926 | } else { |
| 1927 | return (0); |
| 1928 | } |
| 1929 | break; |
| 1930 | #endif |
| 1931 | #ifdef INET6 |
| 1932 | case AF_INET6: |
| 1933 | if (scope->ipv6_addr_legal) { |
| 1934 | struct sockaddr_in6 *sin6; |
| 1935 |
| 1936 | #if !defined(__Panda__) |
| 1937 | /* Must update the flags, bummer, which |
| 1938 | * means any IFA locks must now be applied HERE <-> |
| 1939 | */ |
| 1940 | if (do_update) { |
| 1941 | sctp_gather_internal_ifa_flags(ifa); |
| 1942 | } |
| 1943 | #endif |
| 1944 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
| 1945 | return (0); |
| 1946 | } |
| 1947 | /* ok to use deprecated addresses? */ |
| 1948 | sin6 = &ifa->address.sin6; |
| 1949 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
| 1950 | /* skip unspecifed addresses */ |
| 1951 | return (0); |
| 1952 | } |
| 1953 | if ( /* (local_scope == 0) && */ |
| 1954 | (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) { |
| 1955 | return (0); |
| 1956 | } |
| 1957 | if ((scope->site_scope == 0) && |
| 1958 | (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) { |
| 1959 | return (0); |
| 1960 | } |
| 1961 | } else { |
| 1962 | return (0); |
| 1963 | } |
| 1964 | break; |
| 1965 | #endif |
| 1966 | #if defined(__Userspace__) |
| 1967 | case AF_CONN: |
| 1968 | if (!scope->conn_addr_legal) { |
| 1969 | return (0); |
| 1970 | } |
| 1971 | break; |
| 1972 | #endif |
| 1973 | default: |
| 1974 | return (0); |
| 1975 | } |
| 1976 | return (1); |
| 1977 | } |
| 1978 |
| 1979 | static struct mbuf * |
| 1980 | sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa, uint16_t *len) |
| 1981 | { |
| 1982 | #if defined(INET) || defined(INET6) |
| 1983 | struct sctp_paramhdr *paramh; |
| 1984 | struct mbuf *mret; |
| 1985 | uint16_t plen; |
| 1986 | #endif |
| 1987 |
| 1988 | switch (ifa->address.sa.sa_family) { |
| 1989 | #ifdef INET |
| 1990 | case AF_INET: |
| 1991 | plen = (uint16_t)sizeof(struct sctp_ipv4addr_param); |
| 1992 | break; |
| 1993 | #endif |
| 1994 | #ifdef INET6 |
| 1995 | case AF_INET6: |
| 1996 | plen = (uint16_t)sizeof(struct sctp_ipv6addr_param); |
| 1997 | break; |
| 1998 | #endif |
| 1999 | default: |
| 2000 | return (m); |
| 2001 | } |
| 2002 | #if defined(INET) || defined(INET6) |
| 2003 | if (M_TRAILINGSPACE(m) >= plen) { |
| 2004 | /* easy side we just drop it on the end */ |
| 2005 | paramh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m))); |
| 2006 | mret = m; |
| 2007 | } else { |
| 2008 | /* Need more space */ |
| 2009 | mret = m; |
| 2010 | while (SCTP_BUF_NEXT(mret) != NULL) { |
| 2011 | mret = SCTP_BUF_NEXT(mret); |
| 2012 | } |
| 2013 | SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(plen, 0, M_NOWAIT, 1, MT_DATA); |
| 2014 | if (SCTP_BUF_NEXT(mret) == NULL) { |
| 2015 | /* We are hosed, can't add more addresses */ |
| 2016 | return (m); |
| 2017 | } |
| 2018 | mret = SCTP_BUF_NEXT(mret); |
| 2019 | paramh = mtod(mret, struct sctp_paramhdr *); |
| 2020 | } |
| 2021 | /* now add the parameter */ |
| 2022 | switch (ifa->address.sa.sa_family) { |
| 2023 | #ifdef INET |
| 2024 | case AF_INET: |
| 2025 | { |
| 2026 | struct sctp_ipv4addr_param *ipv4p; |
| 2027 | struct sockaddr_in *sin; |
| 2028 |
| 2029 | sin = &ifa->address.sin; |
| 2030 | ipv4p = (struct sctp_ipv4addr_param *)paramh; |
| 2031 | paramh->param_type = htons(SCTP_IPV4_ADDRESS); |
| 2032 | paramh->param_length = htons(plen); |
| 2033 | ipv4p->addr = sin->sin_addr.s_addr; |
| 2034 | SCTP_BUF_LEN(mret) += plen; |
| 2035 | break; |
| 2036 | } |
| 2037 | #endif |
| 2038 | #ifdef INET6 |
| 2039 | case AF_INET6: |
| 2040 | { |
| 2041 | struct sctp_ipv6addr_param *ipv6p; |
| 2042 | struct sockaddr_in6 *sin6; |
| 2043 |
| 2044 | sin6 = &ifa->address.sin6; |
| 2045 | ipv6p = (struct sctp_ipv6addr_param *)paramh; |
| 2046 | paramh->param_type = htons(SCTP_IPV6_ADDRESS); |
| 2047 | paramh->param_length = htons(plen); |
| 2048 | memcpy(ipv6p->addr, &sin6->sin6_addr, |
| 2049 | sizeof(ipv6p->addr)); |
| 2050 | #if defined(SCTP_EMBEDDED_V6_SCOPE) |
| 2051 | /* clear embedded scope in the address */ |
| 2052 | in6_clearscope((struct in6_addr *)ipv6p->addr); |
| 2053 | #endif |
| 2054 | SCTP_BUF_LEN(mret) += plen; |
| 2055 | break; |
| 2056 | } |
| 2057 | #endif |
| 2058 | default: |
| 2059 | return (m); |
| 2060 | } |
| 2061 | if (len != NULL) { |
| 2062 | *len += plen; |
| 2063 | } |
| 2064 | return (mret); |
| 2065 | #endif |
| 2066 | } |
| 2067 |
| 2068 |
| 2069 | struct mbuf * |
| 2070 | sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_tcb *stcb, |
| 2071 | struct sctp_scoping *scope, |
| 2072 | struct mbuf *m_at, int cnt_inits_to, |
| 2073 | uint16_t *padding_len, uint16_t *chunk_len) |
| 2074 | { |
| 2075 | struct sctp_vrf *vrf = NULL; |
| 2076 | int cnt, limit_out = 0, total_count; |
| 2077 | uint32_t vrf_id; |
| 2078 |
| 2079 | vrf_id = inp->def_vrf_id; |
| 2080 | SCTP_IPI_ADDR_RLOCK(); |
| 2081 | vrf = sctp_find_vrf(vrf_id); |
| 2082 | if (vrf == NULL) { |
| 2083 | SCTP_IPI_ADDR_RUNLOCK(); |
| 2084 | return (m_at); |
| 2085 | } |
| 2086 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
| 2087 | struct sctp_ifa *sctp_ifap; |
| 2088 | struct sctp_ifn *sctp_ifnp; |
| 2089 |
| 2090 | cnt = cnt_inits_to; |
| 2091 | if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) { |
| 2092 | limit_out = 1; |
| 2093 | cnt = SCTP_ADDRESS_LIMIT; |
| 2094 | goto skip_count; |
| 2095 | } |
| 2096 | LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) { |
| 2097 | if ((scope->loopback_scope == 0) && |
| 2098 | SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) { |
| 2099 | /* |
| 2100 | * Skip loopback devices if loopback_scope |
| 2101 | * not set |
| 2102 | */ |
| 2103 | continue; |
| 2104 | } |
| 2105 | LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) { |
| 2106 | #if defined(__FreeBSD__) |
| 2107 | #ifdef INET |
| 2108 | if ((sctp_ifap->address.sa.sa_family == AF_INET) && |
| 2109 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 2110 | &sctp_ifap->address.sin.sin_addr) != 0)) { |
| 2111 | continue; |
| 2112 | } |
| 2113 | #endif |
| 2114 | #ifdef INET6 |
| 2115 | if ((sctp_ifap->address.sa.sa_family == AF_INET6) && |
| 2116 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 2117 | &sctp_ifap->address.sin6.sin6_addr) != 0)) { |
| 2118 | continue; |
| 2119 | } |
| 2120 | #endif |
| 2121 | #endif |
| 2122 | if (sctp_is_addr_restricted(stcb, sctp_ifap)) { |
| 2123 | continue; |
| 2124 | } |
| 2125 | #if defined(__Userspace__) |
| 2126 | if (sctp_ifap->address.sa.sa_family == AF_CONN) { |
| 2127 | continue; |
| 2128 | } |
| 2129 | #endif |
| 2130 | if (sctp_is_address_in_scope(sctp_ifap, scope, 1) == 0) { |
| 2131 | continue; |
| 2132 | } |
| 2133 | cnt++; |
| 2134 | if (cnt > SCTP_ADDRESS_LIMIT) { |
| 2135 | break; |
| 2136 | } |
| 2137 | } |
| 2138 | if (cnt > SCTP_ADDRESS_LIMIT) { |
| 2139 | break; |
| 2140 | } |
| 2141 | } |
| 2142 | skip_count: |
| 2143 | if (cnt > 1) { |
| 2144 | total_count = 0; |
| 2145 | LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) { |
| 2146 | cnt = 0; |
| 2147 | if ((scope->loopback_scope == 0) && |
| 2148 | SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) { |
| 2149 | /* |
| 2150 | * Skip loopback devices if |
| 2151 | * loopback_scope not set |
| 2152 | */ |
| 2153 | continue; |
| 2154 | } |
| 2155 | LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) { |
| 2156 | #if defined(__FreeBSD__) |
| 2157 | #ifdef INET |
| 2158 | if ((sctp_ifap->address.sa.sa_family == AF_INET) && |
| 2159 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 2160 | &sctp_ifap->address.sin.sin_addr) != 0)) { |
| 2161 | continue; |
| 2162 | } |
| 2163 | #endif |
| 2164 | #ifdef INET6 |
| 2165 | if ((sctp_ifap->address.sa.sa_family == AF_INET6) && |
| 2166 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 2167 | &sctp_ifap->address.sin6.sin6_addr) != 0)) { |
| 2168 | continue; |
| 2169 | } |
| 2170 | #endif |
| 2171 | #endif |
| 2172 | if (sctp_is_addr_restricted(stcb, sctp_ifap)) { |
| 2173 | continue; |
| 2174 | } |
| 2175 | #if defined(__Userspace__) |
| 2176 | if (sctp_ifap->address.sa.sa_family == AF_CONN) { |
| 2177 | continue; |
| 2178 | } |
| 2179 | #endif |
| 2180 | if (sctp_is_address_in_scope(sctp_ifap, |
| 2181 | scope, 0) == 0) { |
| 2182 | continue; |
| 2183 | } |
| 2184 | if ((chunk_len != NULL) && |
| 2185 | (padding_len != NULL) && |
| 2186 | (*padding_len > 0)) { |
| 2187 | memset(mtod(m_at, caddr_t) + *chunk_len, 0, *padding_len); |
| 2188 | SCTP_BUF_LEN(m_at) += *padding_len; |
| 2189 | *chunk_len += *padding_len; |
| 2190 | *padding_len = 0; |
| 2191 | } |
| 2192 | m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap, chunk_len); |
| 2193 | if (limit_out) { |
| 2194 | cnt++; |
| 2195 | total_count++; |
| 2196 | if (cnt >= 2) { |
| 2197 | /* two from each address */ |
| 2198 | break; |
| 2199 | } |
| 2200 | if (total_count > SCTP_ADDRESS_LIMIT) { |
| 2201 | /* No more addresses */ |
| 2202 | break; |
| 2203 | } |
| 2204 | } |
| 2205 | } |
| 2206 | } |
| 2207 | } |
| 2208 | } else { |
| 2209 | struct sctp_laddr *laddr; |
| 2210 |
| 2211 | cnt = cnt_inits_to; |
| 2212 | /* First, how many ? */ |
| 2213 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 2214 | if (laddr->ifa == NULL) { |
| 2215 | continue; |
| 2216 | } |
| 2217 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) |
| 2218 | /* Address being deleted by the system, dont |
| 2219 | * list. |
| 2220 | */ |
| 2221 | continue; |
| 2222 | if (laddr->action == SCTP_DEL_IP_ADDRESS) { |
| 2223 | /* Address being deleted on this ep |
| 2224 | * don't list. |
| 2225 | */ |
| 2226 | continue; |
| 2227 | } |
| 2228 | #if defined(__Userspace__) |
| 2229 | if (laddr->ifa->address.sa.sa_family == AF_CONN) { |
| 2230 | continue; |
| 2231 | } |
| 2232 | #endif |
| 2233 | if (sctp_is_address_in_scope(laddr->ifa, |
| 2234 | scope, 1) == 0) { |
| 2235 | continue; |
| 2236 | } |
| 2237 | cnt++; |
| 2238 | } |
| 2239 | /* |
| 2240 | * To get through a NAT we only list addresses if we have |
| 2241 | * more than one. That way if you just bind a single address |
| 2242 | * we let the source of the init dictate our address. |
| 2243 | */ |
| 2244 | if (cnt > 1) { |
| 2245 | cnt = cnt_inits_to; |
| 2246 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 2247 | if (laddr->ifa == NULL) { |
| 2248 | continue; |
| 2249 | } |
| 2250 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
| 2251 | continue; |
| 2252 | } |
| 2253 | #if defined(__Userspace__) |
| 2254 | if (laddr->ifa->address.sa.sa_family == AF_CONN) { |
| 2255 | continue; |
| 2256 | } |
| 2257 | #endif |
| 2258 | if (sctp_is_address_in_scope(laddr->ifa, |
| 2259 | scope, 0) == 0) { |
| 2260 | continue; |
| 2261 | } |
| 2262 | if ((chunk_len != NULL) && |
| 2263 | (padding_len != NULL) && |
| 2264 | (*padding_len > 0)) { |
| 2265 | memset(mtod(m_at, caddr_t) + *chunk_len, 0, *padding_len); |
| 2266 | SCTP_BUF_LEN(m_at) += *padding_len; |
| 2267 | *chunk_len += *padding_len; |
| 2268 | *padding_len = 0; |
| 2269 | } |
| 2270 | m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa, chunk_len); |
| 2271 | cnt++; |
| 2272 | if (cnt >= SCTP_ADDRESS_LIMIT) { |
| 2273 | break; |
| 2274 | } |
| 2275 | } |
| 2276 | } |
| 2277 | } |
| 2278 | SCTP_IPI_ADDR_RUNLOCK(); |
| 2279 | return (m_at); |
| 2280 | } |
| 2281 |
| 2282 | static struct sctp_ifa * |
| 2283 | sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa, |
| 2284 | uint8_t dest_is_loop, |
| 2285 | uint8_t dest_is_priv, |
| 2286 | sa_family_t fam) |
| 2287 | { |
| 2288 | uint8_t dest_is_global = 0; |
| 2289 | /* dest_is_priv is true if destination is a private address */ |
| 2290 | /* dest_is_loop is true if destination is a loopback addresses */ |
| 2291 |
| 2292 | /** |
| 2293 | * Here we determine if its a preferred address. A preferred address |
| 2294 | * means it is the same scope or higher scope then the destination. |
| 2295 | * L = loopback, P = private, G = global |
| 2296 | * ----------------------------------------- |
| 2297 | * src | dest | result |
| 2298 | * ---------------------------------------- |
| 2299 | * L | L | yes |
| 2300 | * ----------------------------------------- |
| 2301 | * P | L | yes-v4 no-v6 |
| 2302 | * ----------------------------------------- |
| 2303 | * G | L | yes-v4 no-v6 |
| 2304 | * ----------------------------------------- |
| 2305 | * L | P | no |
| 2306 | * ----------------------------------------- |
| 2307 | * P | P | yes |
| 2308 | * ----------------------------------------- |
| 2309 | * G | P | no |
| 2310 | * ----------------------------------------- |
| 2311 | * L | G | no |
| 2312 | * ----------------------------------------- |
| 2313 | * P | G | no |
| 2314 | * ----------------------------------------- |
| 2315 | * G | G | yes |
| 2316 | * ----------------------------------------- |
| 2317 | */ |
| 2318 |
| 2319 | if (ifa->address.sa.sa_family != fam) { |
| 2320 | /* forget mis-matched family */ |
| 2321 | return (NULL); |
| 2322 | } |
| 2323 | if ((dest_is_priv == 0) && (dest_is_loop == 0)) { |
| 2324 | dest_is_global = 1; |
| 2325 | } |
| 2326 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:"); |
| 2327 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa); |
| 2328 | /* Ok the address may be ok */ |
| 2329 | #ifdef INET6 |
| 2330 | if (fam == AF_INET6) { |
| 2331 | /* ok to use deprecated addresses? no lets not! */ |
| 2332 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
| 2333 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n"); |
| 2334 | return (NULL); |
| 2335 | } |
| 2336 | if (ifa->src_is_priv && !ifa->src_is_loop) { |
| 2337 | if (dest_is_loop) { |
| 2338 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n"); |
| 2339 | return (NULL); |
| 2340 | } |
| 2341 | } |
| 2342 | if (ifa->src_is_glob) { |
| 2343 | if (dest_is_loop) { |
| 2344 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n"); |
| 2345 | return (NULL); |
| 2346 | } |
| 2347 | } |
| 2348 | } |
| 2349 | #endif |
| 2350 | /* Now that we know what is what, implement or table |
| 2351 | * this could in theory be done slicker (it used to be), but this |
| 2352 | * is straightforward and easier to validate :-) |
| 2353 | */ |
| 2354 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n", |
| 2355 | ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob); |
| 2356 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n", |
| 2357 | dest_is_loop, dest_is_priv, dest_is_global); |
| 2358 |
| 2359 | if ((ifa->src_is_loop) && (dest_is_priv)) { |
| 2360 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n"); |
| 2361 | return (NULL); |
| 2362 | } |
| 2363 | if ((ifa->src_is_glob) && (dest_is_priv)) { |
| 2364 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n"); |
| 2365 | return (NULL); |
| 2366 | } |
| 2367 | if ((ifa->src_is_loop) && (dest_is_global)) { |
| 2368 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n"); |
| 2369 | return (NULL); |
| 2370 | } |
| 2371 | if ((ifa->src_is_priv) && (dest_is_global)) { |
| 2372 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n"); |
| 2373 | return (NULL); |
| 2374 | } |
| 2375 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n"); |
| 2376 | /* its a preferred address */ |
| 2377 | return (ifa); |
| 2378 | } |
| 2379 |
| 2380 | static struct sctp_ifa * |
| 2381 | sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa, |
| 2382 | uint8_t dest_is_loop, |
| 2383 | uint8_t dest_is_priv, |
| 2384 | sa_family_t fam) |
| 2385 | { |
| 2386 | uint8_t dest_is_global = 0; |
| 2387 |
| 2388 | /** |
| 2389 | * Here we determine if its a acceptable address. A acceptable |
| 2390 | * address means it is the same scope or higher scope but we can |
| 2391 | * allow for NAT which means its ok to have a global dest and a |
| 2392 | * private src. |
| 2393 | * |
| 2394 | * L = loopback, P = private, G = global |
| 2395 | * ----------------------------------------- |
| 2396 | * src | dest | result |
| 2397 | * ----------------------------------------- |
| 2398 | * L | L | yes |
| 2399 | * ----------------------------------------- |
| 2400 | * P | L | yes-v4 no-v6 |
| 2401 | * ----------------------------------------- |
| 2402 | * G | L | yes |
| 2403 | * ----------------------------------------- |
| 2404 | * L | P | no |
| 2405 | * ----------------------------------------- |
| 2406 | * P | P | yes |
| 2407 | * ----------------------------------------- |
| 2408 | * G | P | yes - May not work |
| 2409 | * ----------------------------------------- |
| 2410 | * L | G | no |
| 2411 | * ----------------------------------------- |
| 2412 | * P | G | yes - May not work |
| 2413 | * ----------------------------------------- |
| 2414 | * G | G | yes |
| 2415 | * ----------------------------------------- |
| 2416 | */ |
| 2417 |
| 2418 | if (ifa->address.sa.sa_family != fam) { |
| 2419 | /* forget non matching family */ |
| 2420 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n", |
| 2421 | ifa->address.sa.sa_family, fam); |
| 2422 | return (NULL); |
| 2423 | } |
| 2424 | /* Ok the address may be ok */ |
| 2425 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa); |
| 2426 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n", |
| 2427 | dest_is_loop, dest_is_priv); |
| 2428 | if ((dest_is_loop == 0) && (dest_is_priv == 0)) { |
| 2429 | dest_is_global = 1; |
| 2430 | } |
| 2431 | #ifdef INET6 |
| 2432 | if (fam == AF_INET6) { |
| 2433 | /* ok to use deprecated addresses? */ |
| 2434 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
| 2435 | return (NULL); |
| 2436 | } |
| 2437 | if (ifa->src_is_priv) { |
| 2438 | /* Special case, linklocal to loop */ |
| 2439 | if (dest_is_loop) |
| 2440 | return (NULL); |
| 2441 | } |
| 2442 | } |
| 2443 | #endif |
| 2444 | /* |
| 2445 | * Now that we know what is what, implement our table. |
| 2446 | * This could in theory be done slicker (it used to be), but this |
| 2447 | * is straightforward and easier to validate :-) |
| 2448 | */ |
| 2449 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n", |
| 2450 | ifa->src_is_loop, |
| 2451 | dest_is_priv); |
| 2452 | if ((ifa->src_is_loop == 1) && (dest_is_priv)) { |
| 2453 | return (NULL); |
| 2454 | } |
| 2455 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n", |
| 2456 | ifa->src_is_loop, |
| 2457 | dest_is_global); |
| 2458 | if ((ifa->src_is_loop == 1) && (dest_is_global)) { |
| 2459 | return (NULL); |
| 2460 | } |
| 2461 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n"); |
| 2462 | /* its an acceptable address */ |
| 2463 | return (ifa); |
| 2464 | } |
| 2465 |
| 2466 | int |
| 2467 | sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) |
| 2468 | { |
| 2469 | struct sctp_laddr *laddr; |
| 2470 |
| 2471 | if (stcb == NULL) { |
| 2472 | /* There are no restrictions, no TCB :-) */ |
| 2473 | return (0); |
| 2474 | } |
| 2475 | LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) { |
| 2476 | if (laddr->ifa == NULL) { |
| 2477 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n", |
| 2478 | __func__); |
| 2479 | continue; |
| 2480 | } |
| 2481 | if (laddr->ifa == ifa) { |
| 2482 | /* Yes it is on the list */ |
| 2483 | return (1); |
| 2484 | } |
| 2485 | } |
| 2486 | return (0); |
| 2487 | } |
| 2488 |
| 2489 |
| 2490 | int |
| 2491 | sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa) |
| 2492 | { |
| 2493 | struct sctp_laddr *laddr; |
| 2494 |
| 2495 | if (ifa == NULL) |
| 2496 | return (0); |
| 2497 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 2498 | if (laddr->ifa == NULL) { |
| 2499 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n", |
| 2500 | __func__); |
| 2501 | continue; |
| 2502 | } |
| 2503 | if ((laddr->ifa == ifa) && laddr->action == 0) |
| 2504 | /* same pointer */ |
| 2505 | return (1); |
| 2506 | } |
| 2507 | return (0); |
| 2508 | } |
| 2509 |
| 2510 |
| 2511 |
| 2512 | static struct sctp_ifa * |
| 2513 | sctp_choose_boundspecific_inp(struct sctp_inpcb *inp, |
| 2514 | sctp_route_t *ro, |
| 2515 | uint32_t vrf_id, |
| 2516 | int non_asoc_addr_ok, |
| 2517 | uint8_t dest_is_priv, |
| 2518 | uint8_t dest_is_loop, |
| 2519 | sa_family_t fam) |
| 2520 | { |
| 2521 | struct sctp_laddr *laddr, *starting_point; |
| 2522 | void *ifn; |
| 2523 | int resettotop = 0; |
| 2524 | struct sctp_ifn *sctp_ifn; |
| 2525 | struct sctp_ifa *sctp_ifa, *sifa; |
| 2526 | struct sctp_vrf *vrf; |
| 2527 | uint32_t ifn_index; |
| 2528 |
| 2529 | vrf = sctp_find_vrf(vrf_id); |
| 2530 | if (vrf == NULL) |
| 2531 | return (NULL); |
| 2532 |
| 2533 | ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro); |
| 2534 | ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro); |
| 2535 | sctp_ifn = sctp_find_ifn(ifn, ifn_index); |
| 2536 | /* |
| 2537 | * first question, is the ifn we will emit on in our list, if so, we |
| 2538 | * want such an address. Note that we first looked for a |
| 2539 | * preferred address. |
| 2540 | */ |
| 2541 | if (sctp_ifn) { |
| 2542 | /* is a preferred one on the interface we route out? */ |
| 2543 | LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { |
| 2544 | #if defined(__FreeBSD__) |
| 2545 | #ifdef INET |
| 2546 | if ((sctp_ifa->address.sa.sa_family == AF_INET) && |
| 2547 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 2548 | &sctp_ifa->address.sin.sin_addr) != 0)) { |
| 2549 | continue; |
| 2550 | } |
| 2551 | #endif |
| 2552 | #ifdef INET6 |
| 2553 | if ((sctp_ifa->address.sa.sa_family == AF_INET6) && |
| 2554 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 2555 | &sctp_ifa->address.sin6.sin6_addr) != 0)) { |
| 2556 | continue; |
| 2557 | } |
| 2558 | #endif |
| 2559 | #endif |
| 2560 | if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && |
| 2561 | (non_asoc_addr_ok == 0)) |
| 2562 | continue; |
| 2563 | sifa = sctp_is_ifa_addr_preferred(sctp_ifa, |
| 2564 | dest_is_loop, |
| 2565 | dest_is_priv, fam); |
| 2566 | if (sifa == NULL) |
| 2567 | continue; |
| 2568 | if (sctp_is_addr_in_ep(inp, sifa)) { |
| 2569 | atomic_add_int(&sifa->refcount, 1); |
| 2570 | return (sifa); |
| 2571 | } |
| 2572 | } |
| 2573 | } |
| 2574 | /* |
| 2575 | * ok, now we now need to find one on the list of the addresses. |
| 2576 | * We can't get one on the emitting interface so let's find first |
| 2577 | * a preferred one. If not that an acceptable one otherwise... |
| 2578 | * we return NULL. |
| 2579 | */ |
| 2580 | starting_point = inp->next_addr_touse; |
| 2581 | once_again: |
| 2582 | if (inp->next_addr_touse == NULL) { |
| 2583 | inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list); |
| 2584 | resettotop = 1; |
| 2585 | } |
| 2586 | for (laddr = inp->next_addr_touse; laddr; |
| 2587 | laddr = LIST_NEXT(laddr, sctp_nxt_addr)) { |
| 2588 | if (laddr->ifa == NULL) { |
| 2589 | /* address has been removed */ |
| 2590 | continue; |
| 2591 | } |
| 2592 | if (laddr->action == SCTP_DEL_IP_ADDRESS) { |
| 2593 | /* address is being deleted */ |
| 2594 | continue; |
| 2595 | } |
| 2596 | sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, |
| 2597 | dest_is_priv, fam); |
| 2598 | if (sifa == NULL) |
| 2599 | continue; |
| 2600 | atomic_add_int(&sifa->refcount, 1); |
| 2601 | return (sifa); |
| 2602 | } |
| 2603 | if (resettotop == 0) { |
| 2604 | inp->next_addr_touse = NULL; |
| 2605 | goto once_again; |
| 2606 | } |
| 2607 |
| 2608 | inp->next_addr_touse = starting_point; |
| 2609 | resettotop = 0; |
| 2610 | once_again_too: |
| 2611 | if (inp->next_addr_touse == NULL) { |
| 2612 | inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list); |
| 2613 | resettotop = 1; |
| 2614 | } |
| 2615 |
| 2616 | /* ok, what about an acceptable address in the inp */ |
| 2617 | for (laddr = inp->next_addr_touse; laddr; |
| 2618 | laddr = LIST_NEXT(laddr, sctp_nxt_addr)) { |
| 2619 | if (laddr->ifa == NULL) { |
| 2620 | /* address has been removed */ |
| 2621 | continue; |
| 2622 | } |
| 2623 | if (laddr->action == SCTP_DEL_IP_ADDRESS) { |
| 2624 | /* address is being deleted */ |
| 2625 | continue; |
| 2626 | } |
| 2627 | sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop, |
| 2628 | dest_is_priv, fam); |
| 2629 | if (sifa == NULL) |
| 2630 | continue; |
| 2631 | atomic_add_int(&sifa->refcount, 1); |
| 2632 | return (sifa); |
| 2633 | } |
| 2634 | if (resettotop == 0) { |
| 2635 | inp->next_addr_touse = NULL; |
| 2636 | goto once_again_too; |
| 2637 | } |
| 2638 |
| 2639 | /* |
| 2640 | * no address bound can be a source for the destination we are in |
| 2641 | * trouble |
| 2642 | */ |
| 2643 | return (NULL); |
| 2644 | } |
| 2645 |
| 2646 |
| 2647 |
| 2648 | static struct sctp_ifa * |
| 2649 | sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp, |
| 2650 | struct sctp_tcb *stcb, |
| 2651 | sctp_route_t *ro, |
| 2652 | uint32_t vrf_id, |
| 2653 | uint8_t dest_is_priv, |
| 2654 | uint8_t dest_is_loop, |
| 2655 | int non_asoc_addr_ok, |
| 2656 | sa_family_t fam) |
| 2657 | { |
| 2658 | struct sctp_laddr *laddr, *starting_point; |
| 2659 | void *ifn; |
| 2660 | struct sctp_ifn *sctp_ifn; |
| 2661 | struct sctp_ifa *sctp_ifa, *sifa; |
| 2662 | uint8_t start_at_beginning = 0; |
| 2663 | struct sctp_vrf *vrf; |
| 2664 | uint32_t ifn_index; |
| 2665 |
| 2666 | /* |
| 2667 | * first question, is the ifn we will emit on in our list, if so, we |
| 2668 | * want that one. |
| 2669 | */ |
| 2670 | vrf = sctp_find_vrf(vrf_id); |
| 2671 | if (vrf == NULL) |
| 2672 | return (NULL); |
| 2673 |
| 2674 | ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro); |
| 2675 | ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro); |
| 2676 | sctp_ifn = sctp_find_ifn( ifn, ifn_index); |
| 2677 |
| 2678 | /* |
| 2679 | * first question, is the ifn we will emit on in our list? If so, |
| 2680 | * we want that one. First we look for a preferred. Second, we go |
| 2681 | * for an acceptable. |
| 2682 | */ |
| 2683 | if (sctp_ifn) { |
| 2684 | /* first try for a preferred address on the ep */ |
| 2685 | LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { |
| 2686 | #if defined(__FreeBSD__) |
| 2687 | #ifdef INET |
| 2688 | if ((sctp_ifa->address.sa.sa_family == AF_INET) && |
| 2689 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 2690 | &sctp_ifa->address.sin.sin_addr) != 0)) { |
| 2691 | continue; |
| 2692 | } |
| 2693 | #endif |
| 2694 | #ifdef INET6 |
| 2695 | if ((sctp_ifa->address.sa.sa_family == AF_INET6) && |
| 2696 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 2697 | &sctp_ifa->address.sin6.sin6_addr) != 0)) { |
| 2698 | continue; |
| 2699 | } |
| 2700 | #endif |
| 2701 | #endif |
| 2702 | if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0)) |
| 2703 | continue; |
| 2704 | if (sctp_is_addr_in_ep(inp, sctp_ifa)) { |
| 2705 | sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam); |
| 2706 | if (sifa == NULL) |
| 2707 | continue; |
| 2708 | if (((non_asoc_addr_ok == 0) && |
| 2709 | (sctp_is_addr_restricted(stcb, sifa))) || |
| 2710 | (non_asoc_addr_ok && |
| 2711 | (sctp_is_addr_restricted(stcb, sifa)) && |
| 2712 | (!sctp_is_addr_pending(stcb, sifa)))) { |
| 2713 | /* on the no-no list */ |
| 2714 | continue; |
| 2715 | } |
| 2716 | atomic_add_int(&sifa->refcount, 1); |
| 2717 | return (sifa); |
| 2718 | } |
| 2719 | } |
| 2720 | /* next try for an acceptable address on the ep */ |
| 2721 | LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { |
| 2722 | #if defined(__FreeBSD__) |
| 2723 | #ifdef INET |
| 2724 | if ((sctp_ifa->address.sa.sa_family == AF_INET) && |
| 2725 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 2726 | &sctp_ifa->address.sin.sin_addr) != 0)) { |
| 2727 | continue; |
| 2728 | } |
| 2729 | #endif |
| 2730 | #ifdef INET6 |
| 2731 | if ((sctp_ifa->address.sa.sa_family == AF_INET6) && |
| 2732 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 2733 | &sctp_ifa->address.sin6.sin6_addr) != 0)) { |
| 2734 | continue; |
| 2735 | } |
| 2736 | #endif |
| 2737 | #endif |
| 2738 | if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0)) |
| 2739 | continue; |
| 2740 | if (sctp_is_addr_in_ep(inp, sctp_ifa)) { |
| 2741 | sifa= sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv,fam); |
| 2742 | if (sifa == NULL) |
| 2743 | continue; |
| 2744 | if (((non_asoc_addr_ok == 0) && |
| 2745 | (sctp_is_addr_restricted(stcb, sifa))) || |
| 2746 | (non_asoc_addr_ok && |
| 2747 | (sctp_is_addr_restricted(stcb, sifa)) && |
| 2748 | (!sctp_is_addr_pending(stcb, sifa)))) { |
| 2749 | /* on the no-no list */ |
| 2750 | continue; |
| 2751 | } |
| 2752 | atomic_add_int(&sifa->refcount, 1); |
| 2753 | return (sifa); |
| 2754 | } |
| 2755 | } |
| 2756 |
| 2757 | } |
| 2758 | /* |
| 2759 | * if we can't find one like that then we must look at all |
| 2760 | * addresses bound to pick one at first preferable then |
| 2761 | * secondly acceptable. |
| 2762 | */ |
| 2763 | starting_point = stcb->asoc.last_used_address; |
| 2764 | sctp_from_the_top: |
| 2765 | if (stcb->asoc.last_used_address == NULL) { |
| 2766 | start_at_beginning = 1; |
| 2767 | stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list); |
| 2768 | } |
| 2769 | /* search beginning with the last used address */ |
| 2770 | for (laddr = stcb->asoc.last_used_address; laddr; |
| 2771 | laddr = LIST_NEXT(laddr, sctp_nxt_addr)) { |
| 2772 | if (laddr->ifa == NULL) { |
| 2773 | /* address has been removed */ |
| 2774 | continue; |
| 2775 | } |
| 2776 | if (laddr->action == SCTP_DEL_IP_ADDRESS) { |
| 2777 | /* address is being deleted */ |
| 2778 | continue; |
| 2779 | } |
| 2780 | sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam); |
| 2781 | if (sifa == NULL) |
| 2782 | continue; |
| 2783 | if (((non_asoc_addr_ok == 0) && |
| 2784 | (sctp_is_addr_restricted(stcb, sifa))) || |
| 2785 | (non_asoc_addr_ok && |
| 2786 | (sctp_is_addr_restricted(stcb, sifa)) && |
| 2787 | (!sctp_is_addr_pending(stcb, sifa)))) { |
| 2788 | /* on the no-no list */ |
| 2789 | continue; |
| 2790 | } |
| 2791 | stcb->asoc.last_used_address = laddr; |
| 2792 | atomic_add_int(&sifa->refcount, 1); |
| 2793 | return (sifa); |
| 2794 | } |
| 2795 | if (start_at_beginning == 0) { |
| 2796 | stcb->asoc.last_used_address = NULL; |
| 2797 | goto sctp_from_the_top; |
| 2798 | } |
| 2799 | /* now try for any higher scope than the destination */ |
| 2800 | stcb->asoc.last_used_address = starting_point; |
| 2801 | start_at_beginning = 0; |
| 2802 | sctp_from_the_top2: |
| 2803 | if (stcb->asoc.last_used_address == NULL) { |
| 2804 | start_at_beginning = 1; |
| 2805 | stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list); |
| 2806 | } |
| 2807 | /* search beginning with the last used address */ |
| 2808 | for (laddr = stcb->asoc.last_used_address; laddr; |
| 2809 | laddr = LIST_NEXT(laddr, sctp_nxt_addr)) { |
| 2810 | if (laddr->ifa == NULL) { |
| 2811 | /* address has been removed */ |
| 2812 | continue; |
| 2813 | } |
| 2814 | if (laddr->action == SCTP_DEL_IP_ADDRESS) { |
| 2815 | /* address is being deleted */ |
| 2816 | continue; |
| 2817 | } |
| 2818 | sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop, |
| 2819 | dest_is_priv, fam); |
| 2820 | if (sifa == NULL) |
| 2821 | continue; |
| 2822 | if (((non_asoc_addr_ok == 0) && |
| 2823 | (sctp_is_addr_restricted(stcb, sifa))) || |
| 2824 | (non_asoc_addr_ok && |
| 2825 | (sctp_is_addr_restricted(stcb, sifa)) && |
| 2826 | (!sctp_is_addr_pending(stcb, sifa)))) { |
| 2827 | /* on the no-no list */ |
| 2828 | continue; |
| 2829 | } |
| 2830 | stcb->asoc.last_used_address = laddr; |
| 2831 | atomic_add_int(&sifa->refcount, 1); |
| 2832 | return (sifa); |
| 2833 | } |
| 2834 | if (start_at_beginning == 0) { |
| 2835 | stcb->asoc.last_used_address = NULL; |
| 2836 | goto sctp_from_the_top2; |
| 2837 | } |
| 2838 | return (NULL); |
| 2839 | } |
| 2840 |
| 2841 | static struct sctp_ifa * |
| 2842 | sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn, |
| 2843 | #if defined(__FreeBSD__) |
| 2844 | struct sctp_inpcb *inp, |
| 2845 | #else |
| 2846 | struct sctp_inpcb *inp SCTP_UNUSED, |
| 2847 | #endif |
| 2848 | struct sctp_tcb *stcb, |
| 2849 | int non_asoc_addr_ok, |
| 2850 | uint8_t dest_is_loop, |
| 2851 | uint8_t dest_is_priv, |
| 2852 | int addr_wanted, |
| 2853 | sa_family_t fam, |
| 2854 | sctp_route_t *ro |
| 2855 | ) |
| 2856 | { |
| 2857 | struct sctp_ifa *ifa, *sifa; |
| 2858 | int num_eligible_addr = 0; |
| 2859 | #ifdef INET6 |
| 2860 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 2861 | struct sockaddr_in6 sin6, lsa6; |
| 2862 |
| 2863 | if (fam == AF_INET6) { |
| 2864 | memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6)); |
| 2865 | #ifdef SCTP_KAME |
| 2866 | (void)sa6_recoverscope(&sin6); |
| 2867 | #else |
| 2868 | (void)in6_recoverscope(&sin6, &sin6.sin6_addr, NULL); |
| 2869 | #endif /* SCTP_KAME */ |
| 2870 | } |
| 2871 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 2872 | #endif /* INET6 */ |
| 2873 | LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) { |
| 2874 | #if defined(__FreeBSD__) |
| 2875 | #ifdef INET |
| 2876 | if ((ifa->address.sa.sa_family == AF_INET) && |
| 2877 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 2878 | &ifa->address.sin.sin_addr) != 0)) { |
| 2879 | continue; |
| 2880 | } |
| 2881 | #endif |
| 2882 | #ifdef INET6 |
| 2883 | if ((ifa->address.sa.sa_family == AF_INET6) && |
| 2884 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 2885 | &ifa->address.sin6.sin6_addr) != 0)) { |
| 2886 | continue; |
| 2887 | } |
| 2888 | #endif |
| 2889 | #endif |
| 2890 | if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && |
| 2891 | (non_asoc_addr_ok == 0)) |
| 2892 | continue; |
| 2893 | sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop, |
| 2894 | dest_is_priv, fam); |
| 2895 | if (sifa == NULL) |
| 2896 | continue; |
| 2897 | #ifdef INET6 |
| 2898 | if (fam == AF_INET6 && |
| 2899 | dest_is_loop && |
| 2900 | sifa->src_is_loop && sifa->src_is_priv) { |
| 2901 | /* don't allow fe80::1 to be a src on loop ::1, we don't list it |
| 2902 | * to the peer so we will get an abort. |
| 2903 | */ |
| 2904 | continue; |
| 2905 | } |
| 2906 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 2907 | if (fam == AF_INET6 && |
| 2908 | IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) && |
| 2909 | IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) { |
| 2910 | /* link-local <-> link-local must belong to the same scope. */ |
| 2911 | memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6)); |
| 2912 | #ifdef SCTP_KAME |
| 2913 | (void)sa6_recoverscope(&lsa6); |
| 2914 | #else |
| 2915 | (void)in6_recoverscope(&lsa6, &lsa6.sin6_addr, NULL); |
| 2916 | #endif /* SCTP_KAME */ |
| 2917 | if (sin6.sin6_scope_id != lsa6.sin6_scope_id) { |
| 2918 | continue; |
| 2919 | } |
| 2920 | } |
| 2921 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 2922 | #endif /* INET6 */ |
| 2923 |
| 2924 | #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__) |
| 2925 | /* Check if the IPv6 address matches to next-hop. |
| 2926 | In the mobile case, old IPv6 address may be not deleted |
| 2927 | from the interface. Then, the interface has previous and |
| 2928 | new addresses. We should use one corresponding to the |
| 2929 | next-hop. (by micchie) |
| 2930 | */ |
| 2931 | #ifdef INET6 |
| 2932 | if (stcb && fam == AF_INET6 && |
| 2933 | sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) { |
| 2934 | if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro) |
| 2935 | == 0) { |
| 2936 | continue; |
| 2937 | } |
| 2938 | } |
| 2939 | #endif |
| 2940 | #ifdef INET |
| 2941 | /* Avoid topologically incorrect IPv4 address */ |
| 2942 | if (stcb && fam == AF_INET && |
| 2943 | sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) { |
| 2944 | if (sctp_v4src_match_nexthop(sifa, ro) == 0) { |
| 2945 | continue; |
| 2946 | } |
| 2947 | } |
| 2948 | #endif |
| 2949 | #endif |
| 2950 | if (stcb) { |
| 2951 | if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) { |
| 2952 | continue; |
| 2953 | } |
| 2954 | if (((non_asoc_addr_ok == 0) && |
| 2955 | (sctp_is_addr_restricted(stcb, sifa))) || |
| 2956 | (non_asoc_addr_ok && |
| 2957 | (sctp_is_addr_restricted(stcb, sifa)) && |
| 2958 | (!sctp_is_addr_pending(stcb, sifa)))) { |
| 2959 | /* |
| 2960 | * It is restricted for some reason.. |
| 2961 | * probably not yet added. |
| 2962 | */ |
| 2963 | continue; |
| 2964 | } |
| 2965 | } |
| 2966 | if (num_eligible_addr >= addr_wanted) { |
| 2967 | return (sifa); |
| 2968 | } |
| 2969 | num_eligible_addr++; |
| 2970 | } |
| 2971 | return (NULL); |
| 2972 | } |
| 2973 |
| 2974 |
| 2975 | static int |
| 2976 | sctp_count_num_preferred_boundall(struct sctp_ifn *ifn, |
| 2977 | #if defined(__FreeBSD__) |
| 2978 | struct sctp_inpcb *inp, |
| 2979 | #else |
| 2980 | struct sctp_inpcb *inp SCTP_UNUSED, |
| 2981 | #endif |
| 2982 | struct sctp_tcb *stcb, |
| 2983 | int non_asoc_addr_ok, |
| 2984 | uint8_t dest_is_loop, |
| 2985 | uint8_t dest_is_priv, |
| 2986 | sa_family_t fam) |
| 2987 | { |
| 2988 | struct sctp_ifa *ifa, *sifa; |
| 2989 | int num_eligible_addr = 0; |
| 2990 |
| 2991 | LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) { |
| 2992 | #if defined(__FreeBSD__) |
| 2993 | #ifdef INET |
| 2994 | if ((ifa->address.sa.sa_family == AF_INET) && |
| 2995 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 2996 | &ifa->address.sin.sin_addr) != 0)) { |
| 2997 | continue; |
| 2998 | } |
| 2999 | #endif |
| 3000 | #ifdef INET6 |
| 3001 | if ((ifa->address.sa.sa_family == AF_INET6) && |
| 3002 | (stcb != NULL) && |
| 3003 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 3004 | &ifa->address.sin6.sin6_addr) != 0)) { |
| 3005 | continue; |
| 3006 | } |
| 3007 | #endif |
| 3008 | #endif |
| 3009 | if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && |
| 3010 | (non_asoc_addr_ok == 0)) { |
| 3011 | continue; |
| 3012 | } |
| 3013 | sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop, |
| 3014 | dest_is_priv, fam); |
| 3015 | if (sifa == NULL) { |
| 3016 | continue; |
| 3017 | } |
| 3018 | if (stcb) { |
| 3019 | if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) { |
| 3020 | continue; |
| 3021 | } |
| 3022 | if (((non_asoc_addr_ok == 0) && |
| 3023 | (sctp_is_addr_restricted(stcb, sifa))) || |
| 3024 | (non_asoc_addr_ok && |
| 3025 | (sctp_is_addr_restricted(stcb, sifa)) && |
| 3026 | (!sctp_is_addr_pending(stcb, sifa)))) { |
| 3027 | /* |
| 3028 | * It is restricted for some reason.. |
| 3029 | * probably not yet added. |
| 3030 | */ |
| 3031 | continue; |
| 3032 | } |
| 3033 | } |
| 3034 | num_eligible_addr++; |
| 3035 | } |
| 3036 | return (num_eligible_addr); |
| 3037 | } |
| 3038 |
| 3039 | static struct sctp_ifa * |
| 3040 | sctp_choose_boundall(struct sctp_inpcb *inp, |
| 3041 | struct sctp_tcb *stcb, |
| 3042 | struct sctp_nets *net, |
| 3043 | sctp_route_t *ro, |
| 3044 | uint32_t vrf_id, |
| 3045 | uint8_t dest_is_priv, |
| 3046 | uint8_t dest_is_loop, |
| 3047 | int non_asoc_addr_ok, |
| 3048 | sa_family_t fam) |
| 3049 | { |
| 3050 | int cur_addr_num = 0, num_preferred = 0; |
| 3051 | void *ifn; |
| 3052 | struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn; |
| 3053 | struct sctp_ifa *sctp_ifa, *sifa; |
| 3054 | uint32_t ifn_index; |
| 3055 | struct sctp_vrf *vrf; |
| 3056 | #ifdef INET |
| 3057 | int retried = 0; |
| 3058 | #endif |
| 3059 |
| 3060 | /*- |
| 3061 | * For boundall we can use any address in the association. |
| 3062 | * If non_asoc_addr_ok is set we can use any address (at least in |
| 3063 | * theory). So we look for preferred addresses first. If we find one, |
| 3064 | * we use it. Otherwise we next try to get an address on the |
| 3065 | * interface, which we should be able to do (unless non_asoc_addr_ok |
| 3066 | * is false and we are routed out that way). In these cases where we |
| 3067 | * can't use the address of the interface we go through all the |
| 3068 | * ifn's looking for an address we can use and fill that in. Punting |
| 3069 | * means we send back address 0, which will probably cause problems |
| 3070 | * actually since then IP will fill in the address of the route ifn, |
| 3071 | * which means we probably already rejected it.. i.e. here comes an |
| 3072 | * abort :-<. |
| 3073 | */ |
| 3074 | vrf = sctp_find_vrf(vrf_id); |
| 3075 | if (vrf == NULL) |
| 3076 | return (NULL); |
| 3077 |
| 3078 | ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro); |
| 3079 | ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro); |
| 3080 | SCTPDBG(SCTP_DEBUG_OUTPUT2,"ifn from route:%p ifn_index:%d\n", ifn, ifn_index); |
| 3081 | emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index); |
| 3082 | if (sctp_ifn == NULL) { |
| 3083 | /* ?? We don't have this guy ?? */ |
| 3084 | SCTPDBG(SCTP_DEBUG_OUTPUT2,"No ifn emit interface?\n"); |
| 3085 | goto bound_all_plan_b; |
| 3086 | } |
| 3087 | SCTPDBG(SCTP_DEBUG_OUTPUT2,"ifn_index:%d name:%s is emit interface\n", |
| 3088 | ifn_index, sctp_ifn->ifn_name); |
| 3089 |
| 3090 | if (net) { |
| 3091 | cur_addr_num = net->indx_of_eligible_next_to_use; |
| 3092 | } |
| 3093 | num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, |
| 3094 | inp, stcb, |
| 3095 | non_asoc_addr_ok, |
| 3096 | dest_is_loop, |
| 3097 | dest_is_priv, fam); |
| 3098 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n", |
| 3099 | num_preferred, sctp_ifn->ifn_name); |
| 3100 | if (num_preferred == 0) { |
| 3101 | /* |
| 3102 | * no eligible addresses, we must use some other interface |
| 3103 | * address if we can find one. |
| 3104 | */ |
| 3105 | goto bound_all_plan_b; |
| 3106 | } |
| 3107 | /* |
| 3108 | * Ok we have num_eligible_addr set with how many we can use, this |
| 3109 | * may vary from call to call due to addresses being deprecated |
| 3110 | * etc.. |
| 3111 | */ |
| 3112 | if (cur_addr_num >= num_preferred) { |
| 3113 | cur_addr_num = 0; |
| 3114 | } |
| 3115 | /* |
| 3116 | * select the nth address from the list (where cur_addr_num is the |
| 3117 | * nth) and 0 is the first one, 1 is the second one etc... |
| 3118 | */ |
| 3119 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num); |
| 3120 |
| 3121 | sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop, |
| 3122 | dest_is_priv, cur_addr_num, fam, ro); |
| 3123 |
| 3124 | /* if sctp_ifa is NULL something changed??, fall to plan b. */ |
| 3125 | if (sctp_ifa) { |
| 3126 | atomic_add_int(&sctp_ifa->refcount, 1); |
| 3127 | if (net) { |
| 3128 | /* save off where the next one we will want */ |
| 3129 | net->indx_of_eligible_next_to_use = cur_addr_num + 1; |
| 3130 | } |
| 3131 | return (sctp_ifa); |
| 3132 | } |
| 3133 | /* |
| 3134 | * plan_b: Look at all interfaces and find a preferred address. If |
| 3135 | * no preferred fall through to plan_c. |
| 3136 | */ |
| 3137 | bound_all_plan_b: |
| 3138 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n"); |
| 3139 | LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { |
| 3140 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n", |
| 3141 | sctp_ifn->ifn_name); |
| 3142 | if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { |
| 3143 | /* wrong base scope */ |
| 3144 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n"); |
| 3145 | continue; |
| 3146 | } |
| 3147 | if ((sctp_ifn == looked_at) && looked_at) { |
| 3148 | /* already looked at this guy */ |
| 3149 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n"); |
| 3150 | continue; |
| 3151 | } |
| 3152 | num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, |
| 3153 | dest_is_loop, dest_is_priv, fam); |
| 3154 | SCTPDBG(SCTP_DEBUG_OUTPUT2, |
| 3155 | "Found ifn:%p %d preferred source addresses\n", |
| 3156 | ifn, num_preferred); |
| 3157 | if (num_preferred == 0) { |
| 3158 | /* None on this interface. */ |
| 3159 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "No preferred -- skipping to next\n"); |
| 3160 | continue; |
| 3161 | } |
| 3162 | SCTPDBG(SCTP_DEBUG_OUTPUT2, |
| 3163 | "num preferred:%d on interface:%p cur_addr_num:%d\n", |
| 3164 | num_preferred, (void *)sctp_ifn, cur_addr_num); |
| 3165 |
| 3166 | /* |
| 3167 | * Ok we have num_eligible_addr set with how many we can |
| 3168 | * use, this may vary from call to call due to addresses |
| 3169 | * being deprecated etc.. |
| 3170 | */ |
| 3171 | if (cur_addr_num >= num_preferred) { |
| 3172 | cur_addr_num = 0; |
| 3173 | } |
| 3174 | sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop, |
| 3175 | dest_is_priv, cur_addr_num, fam, ro); |
| 3176 | if (sifa == NULL) |
| 3177 | continue; |
| 3178 | if (net) { |
| 3179 | net->indx_of_eligible_next_to_use = cur_addr_num + 1; |
| 3180 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n", |
| 3181 | cur_addr_num); |
| 3182 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:"); |
| 3183 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa); |
| 3184 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:"); |
| 3185 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa); |
| 3186 | } |
| 3187 | atomic_add_int(&sifa->refcount, 1); |
| 3188 | return (sifa); |
| 3189 | } |
| 3190 | #ifdef INET |
| 3191 | again_with_private_addresses_allowed: |
| 3192 | #endif |
| 3193 | /* plan_c: do we have an acceptable address on the emit interface */ |
| 3194 | sifa = NULL; |
| 3195 | SCTPDBG(SCTP_DEBUG_OUTPUT2,"Trying Plan C: find acceptable on interface\n"); |
| 3196 | if (emit_ifn == NULL) { |
| 3197 | SCTPDBG(SCTP_DEBUG_OUTPUT2,"Jump to Plan D - no emit_ifn\n"); |
| 3198 | goto plan_d; |
| 3199 | } |
| 3200 | LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) { |
| 3201 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", (void *)sctp_ifa); |
| 3202 | #if defined(__FreeBSD__) |
| 3203 | #ifdef INET |
| 3204 | if ((sctp_ifa->address.sa.sa_family == AF_INET) && |
| 3205 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 3206 | &sctp_ifa->address.sin.sin_addr) != 0)) { |
| 3207 | SCTPDBG(SCTP_DEBUG_OUTPUT2,"Jailed\n"); |
| 3208 | continue; |
| 3209 | } |
| 3210 | #endif |
| 3211 | #ifdef INET6 |
| 3212 | if ((sctp_ifa->address.sa.sa_family == AF_INET6) && |
| 3213 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 3214 | &sctp_ifa->address.sin6.sin6_addr) != 0)) { |
| 3215 | SCTPDBG(SCTP_DEBUG_OUTPUT2,"Jailed\n"); |
| 3216 | continue; |
| 3217 | } |
| 3218 | #endif |
| 3219 | #endif |
| 3220 | if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && |
| 3221 | (non_asoc_addr_ok == 0)) { |
| 3222 | SCTPDBG(SCTP_DEBUG_OUTPUT2,"Defer\n"); |
| 3223 | continue; |
| 3224 | } |
| 3225 | sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, |
| 3226 | dest_is_priv, fam); |
| 3227 | if (sifa == NULL) { |
| 3228 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n"); |
| 3229 | continue; |
| 3230 | } |
| 3231 | if (stcb) { |
| 3232 | if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) { |
| 3233 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n"); |
| 3234 | sifa = NULL; |
| 3235 | continue; |
| 3236 | } |
| 3237 | if (((non_asoc_addr_ok == 0) && |
| 3238 | (sctp_is_addr_restricted(stcb, sifa))) || |
| 3239 | (non_asoc_addr_ok && |
| 3240 | (sctp_is_addr_restricted(stcb, sifa)) && |
| 3241 | (!sctp_is_addr_pending(stcb, sifa)))) { |
| 3242 | /* |
| 3243 | * It is restricted for some |
| 3244 | * reason.. probably not yet added. |
| 3245 | */ |
| 3246 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its restricted\n"); |
| 3247 | sifa = NULL; |
| 3248 | continue; |
| 3249 | } |
| 3250 | } |
| 3251 | atomic_add_int(&sifa->refcount, 1); |
| 3252 | goto out; |
| 3253 | } |
| 3254 | plan_d: |
| 3255 | /* |
| 3256 | * plan_d: We are in trouble. No preferred address on the emit |
| 3257 | * interface. And not even a preferred address on all interfaces. |
| 3258 | * Go out and see if we can find an acceptable address somewhere |
| 3259 | * amongst all interfaces. |
| 3260 | */ |
| 3261 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", (void *)looked_at); |
| 3262 | LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { |
| 3263 | if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { |
| 3264 | /* wrong base scope */ |
| 3265 | continue; |
| 3266 | } |
| 3267 | LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { |
| 3268 | #if defined(__FreeBSD__) |
| 3269 | #ifdef INET |
| 3270 | if ((sctp_ifa->address.sa.sa_family == AF_INET) && |
| 3271 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 3272 | &sctp_ifa->address.sin.sin_addr) != 0)) { |
| 3273 | continue; |
| 3274 | } |
| 3275 | #endif |
| 3276 | #ifdef INET6 |
| 3277 | if ((sctp_ifa->address.sa.sa_family == AF_INET6) && |
| 3278 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 3279 | &sctp_ifa->address.sin6.sin6_addr) != 0)) { |
| 3280 | continue; |
| 3281 | } |
| 3282 | #endif |
| 3283 | #endif |
| 3284 | if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && |
| 3285 | (non_asoc_addr_ok == 0)) |
| 3286 | continue; |
| 3287 | sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, |
| 3288 | dest_is_loop, |
| 3289 | dest_is_priv, fam); |
| 3290 | if (sifa == NULL) |
| 3291 | continue; |
| 3292 | if (stcb) { |
| 3293 | if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) { |
| 3294 | sifa = NULL; |
| 3295 | continue; |
| 3296 | } |
| 3297 | if (((non_asoc_addr_ok == 0) && |
| 3298 | (sctp_is_addr_restricted(stcb, sifa))) || |
| 3299 | (non_asoc_addr_ok && |
| 3300 | (sctp_is_addr_restricted(stcb, sifa)) && |
| 3301 | (!sctp_is_addr_pending(stcb, sifa)))) { |
| 3302 | /* |
| 3303 | * It is restricted for some |
| 3304 | * reason.. probably not yet added. |
| 3305 | */ |
| 3306 | sifa = NULL; |
| 3307 | continue; |
| 3308 | } |
| 3309 | } |
| 3310 | goto out; |
| 3311 | } |
| 3312 | } |
| 3313 | #ifdef INET |
| 3314 | if (stcb) { |
| 3315 | if ((retried == 0) && (stcb->asoc.scope.ipv4_local_scope == 0)) { |
| 3316 | stcb->asoc.scope.ipv4_local_scope = 1; |
| 3317 | retried = 1; |
| 3318 | goto again_with_private_addresses_allowed; |
| 3319 | } else if (retried == 1) { |
| 3320 | stcb->asoc.scope.ipv4_local_scope = 0; |
| 3321 | } |
| 3322 | } |
| 3323 | #endif |
| 3324 | out: |
| 3325 | #ifdef INET |
| 3326 | if (sifa) { |
| 3327 | if (retried == 1) { |
| 3328 | LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { |
| 3329 | if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { |
| 3330 | /* wrong base scope */ |
| 3331 | continue; |
| 3332 | } |
| 3333 | LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { |
| 3334 | struct sctp_ifa *tmp_sifa; |
| 3335 |
| 3336 | #if defined(__FreeBSD__) |
| 3337 | #ifdef INET |
| 3338 | if ((sctp_ifa->address.sa.sa_family == AF_INET) && |
| 3339 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 3340 | &sctp_ifa->address.sin.sin_addr) != 0)) { |
| 3341 | continue; |
| 3342 | } |
| 3343 | #endif |
| 3344 | #ifdef INET6 |
| 3345 | if ((sctp_ifa->address.sa.sa_family == AF_INET6) && |
| 3346 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 3347 | &sctp_ifa->address.sin6.sin6_addr) != 0)) { |
| 3348 | continue; |
| 3349 | } |
| 3350 | #endif |
| 3351 | #endif |
| 3352 | if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && |
| 3353 | (non_asoc_addr_ok == 0)) |
| 3354 | continue; |
| 3355 | tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, |
| 3356 | dest_is_loop, |
| 3357 | dest_is_priv, fam); |
| 3358 | if (tmp_sifa == NULL) { |
| 3359 | continue; |
| 3360 | } |
| 3361 | if (tmp_sifa == sifa) { |
| 3362 | continue; |
| 3363 | } |
| 3364 | if (stcb) { |
| 3365 | if (sctp_is_address_in_scope(tmp_sifa, |
| 3366 | &stcb->asoc.scope, 0) == 0) { |
| 3367 | continue; |
| 3368 | } |
| 3369 | if (((non_asoc_addr_ok == 0) && |
| 3370 | (sctp_is_addr_restricted(stcb, tmp_sifa))) || |
| 3371 | (non_asoc_addr_ok && |
| 3372 | (sctp_is_addr_restricted(stcb, tmp_sifa)) && |
| 3373 | (!sctp_is_addr_pending(stcb, tmp_sifa)))) { |
| 3374 | /* |
| 3375 | * It is restricted for some |
| 3376 | * reason.. probably not yet added. |
| 3377 | */ |
| 3378 | continue; |
| 3379 | } |
| 3380 | } |
| 3381 | if ((tmp_sifa->address.sin.sin_family == AF_INET) && |
| 3382 | (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) { |
| 3383 | sctp_add_local_addr_restricted(stcb, tmp_sifa); |
| 3384 | } |
| 3385 | } |
| 3386 | } |
| 3387 | } |
| 3388 | atomic_add_int(&sifa->refcount, 1); |
| 3389 | } |
| 3390 | #endif |
| 3391 | return (sifa); |
| 3392 | } |
| 3393 |
| 3394 |
| 3395 |
| 3396 | /* tcb may be NULL */ |
| 3397 | struct sctp_ifa * |
| 3398 | sctp_source_address_selection(struct sctp_inpcb *inp, |
| 3399 | struct sctp_tcb *stcb, |
| 3400 | sctp_route_t *ro, |
| 3401 | struct sctp_nets *net, |
| 3402 | int non_asoc_addr_ok, uint32_t vrf_id) |
| 3403 | { |
| 3404 | struct sctp_ifa *answer; |
| 3405 | uint8_t dest_is_priv, dest_is_loop; |
| 3406 | sa_family_t fam; |
| 3407 | #ifdef INET |
| 3408 | struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst; |
| 3409 | #endif |
| 3410 | #ifdef INET6 |
| 3411 | struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst; |
| 3412 | #endif |
| 3413 |
| 3414 | /** |
| 3415 | * Rules: |
| 3416 | * - Find the route if needed, cache if I can. |
| 3417 | * - Look at interface address in route, Is it in the bound list. If so we |
| 3418 | * have the best source. |
| 3419 | * - If not we must rotate amongst the addresses. |
| 3420 | * |
| 3421 | * Cavets and issues |
| 3422 | * |
| 3423 | * Do we need to pay attention to scope. We can have a private address |
| 3424 | * or a global address we are sourcing or sending to. So if we draw |
| 3425 | * it out |
| 3426 | * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz |
| 3427 | * For V4 |
| 3428 | * ------------------------------------------ |
| 3429 | * source * dest * result |
| 3430 | * ----------------------------------------- |
| 3431 | * <a> Private * Global * NAT |
| 3432 | * ----------------------------------------- |
| 3433 | * <b> Private * Private * No problem |
| 3434 | * ----------------------------------------- |
| 3435 | * <c> Global * Private * Huh, How will this work? |
| 3436 | * ----------------------------------------- |
| 3437 | * <d> Global * Global * No Problem |
| 3438 | *------------------------------------------ |
| 3439 | * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz |
| 3440 | * For V6 |
| 3441 | *------------------------------------------ |
| 3442 | * source * dest * result |
| 3443 | * ----------------------------------------- |
| 3444 | * <a> Linklocal * Global * |
| 3445 | * ----------------------------------------- |
| 3446 | * <b> Linklocal * Linklocal * No problem |
| 3447 | * ----------------------------------------- |
| 3448 | * <c> Global * Linklocal * Huh, How will this work? |
| 3449 | * ----------------------------------------- |
| 3450 | * <d> Global * Global * No Problem |
| 3451 | *------------------------------------------ |
| 3452 | * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz |
| 3453 | * |
| 3454 | * And then we add to that what happens if there are multiple addresses |
| 3455 | * assigned to an interface. Remember the ifa on a ifn is a linked |
| 3456 | * list of addresses. So one interface can have more than one IP |
| 3457 | * address. What happens if we have both a private and a global |
| 3458 | * address? Do we then use context of destination to sort out which |
| 3459 | * one is best? And what about NAT's sending P->G may get you a NAT |
| 3460 | * translation, or should you select the G thats on the interface in |
| 3461 | * preference. |
| 3462 | * |
| 3463 | * Decisions: |
| 3464 | * |
| 3465 | * - count the number of addresses on the interface. |
| 3466 | * - if it is one, no problem except case <c>. |
| 3467 | * For <a> we will assume a NAT out there. |
| 3468 | * - if there are more than one, then we need to worry about scope P |
| 3469 | * or G. We should prefer G -> G and P -> P if possible. |
| 3470 | * Then as a secondary fall back to mixed types G->P being a last |
| 3471 | * ditch one. |
| 3472 | * - The above all works for bound all, but bound specific we need to |
| 3473 | * use the same concept but instead only consider the bound |
| 3474 | * addresses. If the bound set is NOT assigned to the interface then |
| 3475 | * we must use rotation amongst the bound addresses.. |
| 3476 | */ |
| 3477 | if (ro->ro_rt == NULL) { |
| 3478 | /* |
| 3479 | * Need a route to cache. |
| 3480 | */ |
| 3481 | SCTP_RTALLOC(ro, vrf_id, inp->fibnum); |
| 3482 | } |
| 3483 | if (ro->ro_rt == NULL) { |
| 3484 | return (NULL); |
| 3485 | } |
| 3486 | #if defined(__Userspace_os_Windows) |
| 3487 | /* On Windows the sa_family is U_SHORT or ADDRESS_FAMILY */ |
| 3488 | fam = (sa_family_t)ro->ro_dst.sa_family; |
| 3489 | #else |
| 3490 | fam = ro->ro_dst.sa_family; |
| 3491 | #endif |
| 3492 | dest_is_priv = dest_is_loop = 0; |
| 3493 | /* Setup our scopes for the destination */ |
| 3494 | switch (fam) { |
| 3495 | #ifdef INET |
| 3496 | case AF_INET: |
| 3497 | /* Scope based on outbound address */ |
| 3498 | if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) { |
| 3499 | dest_is_loop = 1; |
| 3500 | if (net != NULL) { |
| 3501 | /* mark it as local */ |
| 3502 | net->addr_is_local = 1; |
| 3503 | } |
| 3504 | } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) { |
| 3505 | dest_is_priv = 1; |
| 3506 | } |
| 3507 | break; |
| 3508 | #endif |
| 3509 | #ifdef INET6 |
| 3510 | case AF_INET6: |
| 3511 | /* Scope based on outbound address */ |
| 3512 | #if defined(__Userspace_os_Windows) |
| 3513 | if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) { |
| 3514 | #else |
| 3515 | if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) || |
| 3516 | SCTP_ROUTE_IS_REAL_LOOP(ro)) { |
| 3517 | #endif |
| 3518 | /* |
| 3519 | * If the address is a loopback address, which |
| 3520 | * consists of "::1" OR "fe80::1%lo0", we are loopback |
| 3521 | * scope. But we don't use dest_is_priv (link local |
| 3522 | * addresses). |
| 3523 | */ |
| 3524 | dest_is_loop = 1; |
| 3525 | if (net != NULL) { |
| 3526 | /* mark it as local */ |
| 3527 | net->addr_is_local = 1; |
| 3528 | } |
| 3529 | } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) { |
| 3530 | dest_is_priv = 1; |
| 3531 | } |
| 3532 | break; |
| 3533 | #endif |
| 3534 | } |
| 3535 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:"); |
| 3536 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst); |
| 3537 | SCTP_IPI_ADDR_RLOCK(); |
| 3538 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
| 3539 | /* |
| 3540 | * Bound all case |
| 3541 | */ |
| 3542 | answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id, |
| 3543 | dest_is_priv, dest_is_loop, |
| 3544 | non_asoc_addr_ok, fam); |
| 3545 | SCTP_IPI_ADDR_RUNLOCK(); |
| 3546 | return (answer); |
| 3547 | } |
| 3548 | /* |
| 3549 | * Subset bound case |
| 3550 | */ |
| 3551 | if (stcb) { |
| 3552 | answer = sctp_choose_boundspecific_stcb(inp, stcb, ro, |
| 3553 | vrf_id, dest_is_priv, |
| 3554 | dest_is_loop, |
| 3555 | non_asoc_addr_ok, fam); |
| 3556 | } else { |
| 3557 | answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id, |
| 3558 | non_asoc_addr_ok, |
| 3559 | dest_is_priv, |
| 3560 | dest_is_loop, fam); |
| 3561 | } |
| 3562 | SCTP_IPI_ADDR_RUNLOCK(); |
| 3563 | return (answer); |
| 3564 | } |
| 3565 |
| 3566 | static int |
| 3567 | sctp_find_cmsg(int c_type, void *data, struct mbuf *control, size_t cpsize) |
| 3568 | { |
| 3569 | #if defined(__Userspace_os_Windows) |
| 3570 | WSACMSGHDR cmh; |
| 3571 | #else |
| 3572 | struct cmsghdr cmh; |
| 3573 | #endif |
| 3574 | struct sctp_sndinfo sndinfo; |
| 3575 | struct sctp_prinfo prinfo; |
| 3576 | struct sctp_authinfo authinfo; |
| 3577 | int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off; |
| 3578 | int found; |
| 3579 |
| 3580 | /* |
| 3581 | * Independent of how many mbufs, find the c_type inside the control |
| 3582 | * structure and copy out the data. |
| 3583 | */ |
| 3584 | found = 0; |
| 3585 | tot_len = SCTP_BUF_LEN(control); |
| 3586 | for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) { |
| 3587 | rem_len = tot_len - off; |
| 3588 | if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) { |
| 3589 | /* There is not enough room for one more. */ |
| 3590 | return (found); |
| 3591 | } |
| 3592 | m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh); |
| 3593 | if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) { |
| 3594 | /* We dont't have a complete CMSG header. */ |
| 3595 | return (found); |
| 3596 | } |
| 3597 | if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) { |
| 3598 | /* We don't have the complete CMSG. */ |
| 3599 | return (found); |
| 3600 | } |
| 3601 | cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh)); |
| 3602 | cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh)); |
| 3603 | if ((cmh.cmsg_level == IPPROTO_SCTP) && |
| 3604 | ((c_type == cmh.cmsg_type) || |
| 3605 | ((c_type == SCTP_SNDRCV) && |
| 3606 | ((cmh.cmsg_type == SCTP_SNDINFO) || |
| 3607 | (cmh.cmsg_type == SCTP_PRINFO) || |
| 3608 | (cmh.cmsg_type == SCTP_AUTHINFO))))) { |
| 3609 | if (c_type == cmh.cmsg_type) { |
| 3610 | if (cpsize > INT_MAX) { |
| 3611 | return (found); |
| 3612 | } |
| 3613 | if (cmsg_data_len < (int)cpsize) { |
| 3614 | return (found); |
| 3615 | } |
| 3616 | /* It is exactly what we want. Copy it out. */ |
| 3617 | m_copydata(control, cmsg_data_off, (int)cpsize, (caddr_t)data); |
| 3618 | return (1); |
| 3619 | } else { |
| 3620 | struct sctp_sndrcvinfo *sndrcvinfo; |
| 3621 |
| 3622 | sndrcvinfo = (struct sctp_sndrcvinfo *)data; |
| 3623 | if (found == 0) { |
| 3624 | if (cpsize < sizeof(struct sctp_sndrcvinfo)) { |
| 3625 | return (found); |
| 3626 | } |
| 3627 | memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo)); |
| 3628 | } |
| 3629 | switch (cmh.cmsg_type) { |
| 3630 | case SCTP_SNDINFO: |
| 3631 | if (cmsg_data_len < (int)sizeof(struct sctp_sndinfo)) { |
| 3632 | return (found); |
| 3633 | } |
| 3634 | m_copydata(control, cmsg_data_off, sizeof(struct sctp_sndinfo), (caddr_t)&sndinfo); |
| 3635 | sndrcvinfo->sinfo_stream = sndinfo.snd_sid; |
| 3636 | sndrcvinfo->sinfo_flags = sndinfo.snd_flags; |
| 3637 | sndrcvinfo->sinfo_ppid = sndinfo.snd_ppid; |
| 3638 | sndrcvinfo->sinfo_context = sndinfo.snd_context; |
| 3639 | sndrcvinfo->sinfo_assoc_id = sndinfo.snd_assoc_id; |
| 3640 | break; |
| 3641 | case SCTP_PRINFO: |
| 3642 | if (cmsg_data_len < (int)sizeof(struct sctp_prinfo)) { |
| 3643 | return (found); |
| 3644 | } |
| 3645 | m_copydata(control, cmsg_data_off, sizeof(struct sctp_prinfo), (caddr_t)&prinfo); |
| 3646 | if (prinfo.pr_policy != SCTP_PR_SCTP_NONE) { |
| 3647 | sndrcvinfo->sinfo_timetolive = prinfo.pr_value; |
| 3648 | } else { |
| 3649 | sndrcvinfo->sinfo_timetolive = 0; |
| 3650 | } |
| 3651 | sndrcvinfo->sinfo_flags |= prinfo.pr_policy; |
| 3652 | break; |
| 3653 | case SCTP_AUTHINFO: |
| 3654 | if (cmsg_data_len < (int)sizeof(struct sctp_authinfo)) { |
| 3655 | return (found); |
| 3656 | } |
| 3657 | m_copydata(control, cmsg_data_off, sizeof(struct sctp_authinfo), (caddr_t)&authinfo); |
| 3658 | sndrcvinfo->sinfo_keynumber_valid = 1; |
| 3659 | sndrcvinfo->sinfo_keynumber = authinfo.auth_keynumber; |
| 3660 | break; |
| 3661 | default: |
| 3662 | return (found); |
| 3663 | } |
| 3664 | found = 1; |
| 3665 | } |
| 3666 | } |
| 3667 | } |
| 3668 | return (found); |
| 3669 | } |
| 3670 |
| 3671 | static int |
| 3672 | sctp_process_cmsgs_for_init(struct sctp_tcb *stcb, struct mbuf *control, int *error) |
| 3673 | { |
| 3674 | #if defined(__Userspace_os_Windows) |
| 3675 | WSACMSGHDR cmh; |
| 3676 | #else |
| 3677 | struct cmsghdr cmh; |
| 3678 | #endif |
| 3679 | struct sctp_initmsg initmsg; |
| 3680 | #ifdef INET |
| 3681 | struct sockaddr_in sin; |
| 3682 | #endif |
| 3683 | #ifdef INET6 |
| 3684 | struct sockaddr_in6 sin6; |
| 3685 | #endif |
| 3686 | int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off; |
| 3687 |
| 3688 | tot_len = SCTP_BUF_LEN(control); |
| 3689 | for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) { |
| 3690 | rem_len = tot_len - off; |
| 3691 | if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) { |
| 3692 | /* There is not enough room for one more. */ |
| 3693 | *error = EINVAL; |
| 3694 | return (1); |
| 3695 | } |
| 3696 | m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh); |
| 3697 | if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) { |
| 3698 | /* We dont't have a complete CMSG header. */ |
| 3699 | *error = EINVAL; |
| 3700 | return (1); |
| 3701 | } |
| 3702 | if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) { |
| 3703 | /* We don't have the complete CMSG. */ |
| 3704 | *error = EINVAL; |
| 3705 | return (1); |
| 3706 | } |
| 3707 | cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh)); |
| 3708 | cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh)); |
| 3709 | if (cmh.cmsg_level == IPPROTO_SCTP) { |
| 3710 | switch (cmh.cmsg_type) { |
| 3711 | case SCTP_INIT: |
| 3712 | if (cmsg_data_len < (int)sizeof(struct sctp_initmsg)) { |
| 3713 | *error = EINVAL; |
| 3714 | return (1); |
| 3715 | } |
| 3716 | m_copydata(control, cmsg_data_off, sizeof(struct sctp_initmsg), (caddr_t)&initmsg); |
| 3717 | if (initmsg.sinit_max_attempts) |
| 3718 | stcb->asoc.max_init_times = initmsg.sinit_max_attempts; |
| 3719 | if (initmsg.sinit_num_ostreams) |
| 3720 | stcb->asoc.pre_open_streams = initmsg.sinit_num_ostreams; |
| 3721 | if (initmsg.sinit_max_instreams) |
| 3722 | stcb->asoc.max_inbound_streams = initmsg.sinit_max_instreams; |
| 3723 | if (initmsg.sinit_max_init_timeo) |
| 3724 | stcb->asoc.initial_init_rto_max = initmsg.sinit_max_init_timeo; |
| 3725 | if (stcb->asoc.streamoutcnt < stcb->asoc.pre_open_streams) { |
| 3726 | struct sctp_stream_out *tmp_str; |
| 3727 | unsigned int i; |
| 3728 | #if defined(SCTP_DETAILED_STR_STATS) |
| 3729 | int j; |
| 3730 | #endif |
| 3731 |
| 3732 | /* Default is NOT correct */ |
| 3733 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, default:%d pre_open:%d\n", |
| 3734 | stcb->asoc.streamoutcnt, stcb->asoc.pre_open_streams); |
| 3735 | SCTP_TCB_UNLOCK(stcb); |
| 3736 | SCTP_MALLOC(tmp_str, |
| 3737 | struct sctp_stream_out *, |
| 3738 | (stcb->asoc.pre_open_streams * sizeof(struct sctp_stream_out)), |
| 3739 | SCTP_M_STRMO); |
| 3740 | SCTP_TCB_LOCK(stcb); |
| 3741 | if (tmp_str != NULL) { |
| 3742 | SCTP_FREE(stcb->asoc.strmout, SCTP_M_STRMO); |
| 3743 | stcb->asoc.strmout = tmp_str; |
| 3744 | stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt = stcb->asoc.pre_open_streams; |
| 3745 | } else { |
| 3746 | stcb->asoc.pre_open_streams = stcb->asoc.streamoutcnt; |
| 3747 | } |
| 3748 | for (i = 0; i < stcb->asoc.streamoutcnt; i++) { |
| 3749 | TAILQ_INIT(&stcb->asoc.strmout[i].outqueue); |
| 3750 | stcb->asoc.strmout[i].chunks_on_queues = 0; |
| 3751 | stcb->asoc.strmout[i].next_mid_ordered = 0; |
| 3752 | stcb->asoc.strmout[i].next_mid_unordered = 0; |
| 3753 | #if defined(SCTP_DETAILED_STR_STATS) |
| 3754 | for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) { |
| 3755 | stcb->asoc.strmout[i].abandoned_sent[j] = 0; |
| 3756 | stcb->asoc.strmout[i].abandoned_unsent[j] = 0; |
| 3757 | } |
| 3758 | #else |
| 3759 | stcb->asoc.strmout[i].abandoned_sent[0] = 0; |
| 3760 | stcb->asoc.strmout[i].abandoned_unsent[0] = 0; |
| 3761 | #endif |
| 3762 | stcb->asoc.strmout[i].sid = i; |
| 3763 | stcb->asoc.strmout[i].last_msg_incomplete = 0; |
| 3764 | stcb->asoc.strmout[i].state = SCTP_STREAM_OPENING; |
| 3765 | stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL); |
| 3766 | } |
| 3767 | } |
| 3768 | break; |
| 3769 | #ifdef INET |
| 3770 | case SCTP_DSTADDRV4: |
| 3771 | if (cmsg_data_len < (int)sizeof(struct in_addr)) { |
| 3772 | *error = EINVAL; |
| 3773 | return (1); |
| 3774 | } |
| 3775 | memset(&sin, 0, sizeof(struct sockaddr_in)); |
| 3776 | sin.sin_family = AF_INET; |
| 3777 | #ifdef HAVE_SIN_LEN |
| 3778 | sin.sin_len = sizeof(struct sockaddr_in); |
| 3779 | #endif |
| 3780 | sin.sin_port = stcb->rport; |
| 3781 | m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr); |
| 3782 | if ((sin.sin_addr.s_addr == INADDR_ANY) || |
| 3783 | (sin.sin_addr.s_addr == INADDR_BROADCAST) || |
| 3784 | IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { |
| 3785 | *error = EINVAL; |
| 3786 | return (1); |
| 3787 | } |
| 3788 | if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port, |
| 3789 | SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { |
| 3790 | *error = ENOBUFS; |
| 3791 | return (1); |
| 3792 | } |
| 3793 | break; |
| 3794 | #endif |
| 3795 | #ifdef INET6 |
| 3796 | case SCTP_DSTADDRV6: |
| 3797 | if (cmsg_data_len < (int)sizeof(struct in6_addr)) { |
| 3798 | *error = EINVAL; |
| 3799 | return (1); |
| 3800 | } |
| 3801 | memset(&sin6, 0, sizeof(struct sockaddr_in6)); |
| 3802 | sin6.sin6_family = AF_INET6; |
| 3803 | #ifdef HAVE_SIN6_LEN |
| 3804 | sin6.sin6_len = sizeof(struct sockaddr_in6); |
| 3805 | #endif |
| 3806 | sin6.sin6_port = stcb->rport; |
| 3807 | m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr); |
| 3808 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) || |
| 3809 | IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) { |
| 3810 | *error = EINVAL; |
| 3811 | return (1); |
| 3812 | } |
| 3813 | #ifdef INET |
| 3814 | if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) { |
| 3815 | in6_sin6_2_sin(&sin, &sin6); |
| 3816 | if ((sin.sin_addr.s_addr == INADDR_ANY) || |
| 3817 | (sin.sin_addr.s_addr == INADDR_BROADCAST) || |
| 3818 | IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { |
| 3819 | *error = EINVAL; |
| 3820 | return (1); |
| 3821 | } |
| 3822 | if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port, |
| 3823 | SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { |
| 3824 | *error = ENOBUFS; |
| 3825 | return (1); |
| 3826 | } |
| 3827 | } else |
| 3828 | #endif |
| 3829 | if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin6, NULL, stcb->asoc.port, |
| 3830 | SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { |
| 3831 | *error = ENOBUFS; |
| 3832 | return (1); |
| 3833 | } |
| 3834 | break; |
| 3835 | #endif |
| 3836 | default: |
| 3837 | break; |
| 3838 | } |
| 3839 | } |
| 3840 | } |
| 3841 | return (0); |
| 3842 | } |
| 3843 |
| 3844 | #if defined(INET) || defined(INET6) |
| 3845 | static struct sctp_tcb * |
| 3846 | sctp_findassociation_cmsgs(struct sctp_inpcb **inp_p, |
| 3847 | uint16_t port, |
| 3848 | struct mbuf *control, |
| 3849 | struct sctp_nets **net_p, |
| 3850 | int *error) |
| 3851 | { |
| 3852 | #if defined(__Userspace_os_Windows) |
| 3853 | WSACMSGHDR cmh; |
| 3854 | #else |
| 3855 | struct cmsghdr cmh; |
| 3856 | #endif |
| 3857 | struct sctp_tcb *stcb; |
| 3858 | struct sockaddr *addr; |
| 3859 | #ifdef INET |
| 3860 | struct sockaddr_in sin; |
| 3861 | #endif |
| 3862 | #ifdef INET6 |
| 3863 | struct sockaddr_in6 sin6; |
| 3864 | #endif |
| 3865 | int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off; |
| 3866 |
| 3867 | tot_len = SCTP_BUF_LEN(control); |
| 3868 | for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) { |
| 3869 | rem_len = tot_len - off; |
| 3870 | if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) { |
| 3871 | /* There is not enough room for one more. */ |
| 3872 | *error = EINVAL; |
| 3873 | return (NULL); |
| 3874 | } |
| 3875 | m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh); |
| 3876 | if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) { |
| 3877 | /* We dont't have a complete CMSG header. */ |
| 3878 | *error = EINVAL; |
| 3879 | return (NULL); |
| 3880 | } |
| 3881 | if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) { |
| 3882 | /* We don't have the complete CMSG. */ |
| 3883 | *error = EINVAL; |
| 3884 | return (NULL); |
| 3885 | } |
| 3886 | cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh)); |
| 3887 | cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh)); |
| 3888 | if (cmh.cmsg_level == IPPROTO_SCTP) { |
| 3889 | switch (cmh.cmsg_type) { |
| 3890 | #ifdef INET |
| 3891 | case SCTP_DSTADDRV4: |
| 3892 | if (cmsg_data_len < (int)sizeof(struct in_addr)) { |
| 3893 | *error = EINVAL; |
| 3894 | return (NULL); |
| 3895 | } |
| 3896 | memset(&sin, 0, sizeof(struct sockaddr_in)); |
| 3897 | sin.sin_family = AF_INET; |
| 3898 | #ifdef HAVE_SIN_LEN |
| 3899 | sin.sin_len = sizeof(struct sockaddr_in); |
| 3900 | #endif |
| 3901 | sin.sin_port = port; |
| 3902 | m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr); |
| 3903 | addr = (struct sockaddr *)&sin; |
| 3904 | break; |
| 3905 | #endif |
| 3906 | #ifdef INET6 |
| 3907 | case SCTP_DSTADDRV6: |
| 3908 | if (cmsg_data_len < (int)sizeof(struct in6_addr)) { |
| 3909 | *error = EINVAL; |
| 3910 | return (NULL); |
| 3911 | } |
| 3912 | memset(&sin6, 0, sizeof(struct sockaddr_in6)); |
| 3913 | sin6.sin6_family = AF_INET6; |
| 3914 | #ifdef HAVE_SIN6_LEN |
| 3915 | sin6.sin6_len = sizeof(struct sockaddr_in6); |
| 3916 | #endif |
| 3917 | sin6.sin6_port = port; |
| 3918 | m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr); |
| 3919 | #ifdef INET |
| 3920 | if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) { |
| 3921 | in6_sin6_2_sin(&sin, &sin6); |
| 3922 | addr = (struct sockaddr *)&sin; |
| 3923 | } else |
| 3924 | #endif |
| 3925 | addr = (struct sockaddr *)&sin6; |
| 3926 | break; |
| 3927 | #endif |
| 3928 | default: |
| 3929 | addr = NULL; |
| 3930 | break; |
| 3931 | } |
| 3932 | if (addr) { |
| 3933 | stcb = sctp_findassociation_ep_addr(inp_p, addr, net_p, NULL, NULL); |
| 3934 | if (stcb != NULL) { |
| 3935 | return (stcb); |
| 3936 | } |
| 3937 | } |
| 3938 | } |
| 3939 | } |
| 3940 | return (NULL); |
| 3941 | } |
| 3942 | #endif |
| 3943 |
| 3944 | static struct mbuf * |
| 3945 | sctp_add_cookie(struct mbuf *init, int init_offset, |
| 3946 | struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t **signature) |
| 3947 | { |
| 3948 | struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret; |
| 3949 | struct sctp_state_cookie *stc; |
| 3950 | struct sctp_paramhdr *ph; |
| 3951 | uint8_t *foo; |
| 3952 | int sig_offset; |
| 3953 | uint16_t cookie_sz; |
| 3954 |
| 3955 | mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) + |
| 3956 | sizeof(struct sctp_paramhdr)), 0, |
| 3957 | M_NOWAIT, 1, MT_DATA); |
| 3958 | if (mret == NULL) { |
| 3959 | return (NULL); |
| 3960 | } |
| 3961 | copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_NOWAIT); |
| 3962 | if (copy_init == NULL) { |
| 3963 | sctp_m_freem(mret); |
| 3964 | return (NULL); |
| 3965 | } |
| 3966 | #ifdef SCTP_MBUF_LOGGING |
| 3967 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { |
| 3968 | sctp_log_mbc(copy_init, SCTP_MBUF_ICOPY); |
| 3969 | } |
| 3970 | #endif |
| 3971 | copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL, |
| 3972 | M_NOWAIT); |
| 3973 | if (copy_initack == NULL) { |
| 3974 | sctp_m_freem(mret); |
| 3975 | sctp_m_freem(copy_init); |
| 3976 | return (NULL); |
| 3977 | } |
| 3978 | #ifdef SCTP_MBUF_LOGGING |
| 3979 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { |
| 3980 | sctp_log_mbc(copy_initack, SCTP_MBUF_ICOPY); |
| 3981 | } |
| 3982 | #endif |
| 3983 | /* easy side we just drop it on the end */ |
| 3984 | ph = mtod(mret, struct sctp_paramhdr *); |
| 3985 | SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) + |
| 3986 | sizeof(struct sctp_paramhdr); |
| 3987 | stc = (struct sctp_state_cookie *)((caddr_t)ph + |
| 3988 | sizeof(struct sctp_paramhdr)); |
| 3989 | ph->param_type = htons(SCTP_STATE_COOKIE); |
| 3990 | ph->param_length = 0; /* fill in at the end */ |
| 3991 | /* Fill in the stc cookie data */ |
| 3992 | memcpy(stc, stc_in, sizeof(struct sctp_state_cookie)); |
| 3993 |
| 3994 | /* tack the INIT and then the INIT-ACK onto the chain */ |
| 3995 | cookie_sz = 0; |
| 3996 | for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) { |
| 3997 | cookie_sz += SCTP_BUF_LEN(m_at); |
| 3998 | if (SCTP_BUF_NEXT(m_at) == NULL) { |
| 3999 | SCTP_BUF_NEXT(m_at) = copy_init; |
| 4000 | break; |
| 4001 | } |
| 4002 | } |
| 4003 | for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) { |
| 4004 | cookie_sz += SCTP_BUF_LEN(m_at); |
| 4005 | if (SCTP_BUF_NEXT(m_at) == NULL) { |
| 4006 | SCTP_BUF_NEXT(m_at) = copy_initack; |
| 4007 | break; |
| 4008 | } |
| 4009 | } |
| 4010 | for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) { |
| 4011 | cookie_sz += SCTP_BUF_LEN(m_at); |
| 4012 | if (SCTP_BUF_NEXT(m_at) == NULL) { |
| 4013 | break; |
| 4014 | } |
| 4015 | } |
| 4016 | sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_NOWAIT, 1, MT_DATA); |
| 4017 | if (sig == NULL) { |
| 4018 | /* no space, so free the entire chain */ |
| 4019 | sctp_m_freem(mret); |
| 4020 | return (NULL); |
| 4021 | } |
| 4022 | SCTP_BUF_LEN(sig) = 0; |
| 4023 | SCTP_BUF_NEXT(m_at) = sig; |
| 4024 | sig_offset = 0; |
| 4025 | foo = (uint8_t *) (mtod(sig, caddr_t) + sig_offset); |
| 4026 | memset(foo, 0, SCTP_SIGNATURE_SIZE); |
| 4027 | *signature = foo; |
| 4028 | SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE; |
| 4029 | cookie_sz += SCTP_SIGNATURE_SIZE; |
| 4030 | ph->param_length = htons(cookie_sz); |
| 4031 | return (mret); |
| 4032 | } |
| 4033 |
| 4034 |
| 4035 | static uint8_t |
| 4036 | sctp_get_ect(struct sctp_tcb *stcb) |
| 4037 | { |
| 4038 | if ((stcb != NULL) && (stcb->asoc.ecn_supported == 1)) { |
| 4039 | return (SCTP_ECT0_BIT); |
| 4040 | } else { |
| 4041 | return (0); |
| 4042 | } |
| 4043 | } |
| 4044 |
| 4045 | #if defined(INET) || defined(INET6) |
| 4046 | static void |
| 4047 | sctp_handle_no_route(struct sctp_tcb *stcb, |
| 4048 | struct sctp_nets *net, |
| 4049 | int so_locked) |
| 4050 | { |
| 4051 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n"); |
| 4052 |
| 4053 | if (net) { |
| 4054 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was "); |
| 4055 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa); |
| 4056 | if (net->dest_state & SCTP_ADDR_CONFIRMED) { |
| 4057 | if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) { |
| 4058 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", (void *)net); |
| 4059 | sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, |
| 4060 | stcb, 0, |
| 4061 | (void *)net, |
| 4062 | so_locked); |
| 4063 | net->dest_state &= ~SCTP_ADDR_REACHABLE; |
| 4064 | net->dest_state &= ~SCTP_ADDR_PF; |
| 4065 | } |
| 4066 | } |
| 4067 | if (stcb) { |
| 4068 | if (net == stcb->asoc.primary_destination) { |
| 4069 | /* need a new primary */ |
| 4070 | struct sctp_nets *alt; |
| 4071 |
| 4072 | alt = sctp_find_alternate_net(stcb, net, 0); |
| 4073 | if (alt != net) { |
| 4074 | if (stcb->asoc.alternate) { |
| 4075 | sctp_free_remote_addr(stcb->asoc.alternate); |
| 4076 | } |
| 4077 | stcb->asoc.alternate = alt; |
| 4078 | atomic_add_int(&stcb->asoc.alternate->ref_count, 1); |
| 4079 | if (net->ro._s_addr) { |
| 4080 | sctp_free_ifa(net->ro._s_addr); |
| 4081 | net->ro._s_addr = NULL; |
| 4082 | } |
| 4083 | net->src_addr_selected = 0; |
| 4084 | } |
| 4085 | } |
| 4086 | } |
| 4087 | } |
| 4088 | } |
| 4089 | #endif |
| 4090 |
| 4091 | static int |
| 4092 | sctp_lowlevel_chunk_output(struct sctp_inpcb *inp, |
| 4093 | struct sctp_tcb *stcb, /* may be NULL */ |
| 4094 | struct sctp_nets *net, |
| 4095 | struct sockaddr *to, |
| 4096 | struct mbuf *m, |
| 4097 | uint32_t auth_offset, |
| 4098 | struct sctp_auth_chunk *auth, |
| 4099 | uint16_t auth_keyid, |
| 4100 | int nofragment_flag, |
| 4101 | int ecn_ok, |
| 4102 | int out_of_asoc_ok, |
| 4103 | uint16_t src_port, |
| 4104 | uint16_t dest_port, |
| 4105 | uint32_t v_tag, |
| 4106 | uint16_t port, |
| 4107 | union sctp_sockstore *over_addr, |
| 4108 | #if defined(__FreeBSD__) |
| 4109 | uint8_t mflowtype, uint32_t mflowid, |
| 4110 | #endif |
| 4111 | #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) |
| 4112 | int so_locked SCTP_UNUSED |
| 4113 | #else |
| 4114 | int so_locked |
| 4115 | #endif |
| 4116 | ) |
| 4117 | /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */ |
| 4118 | { |
| 4119 | /** |
| 4120 | * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet header |
| 4121 | * WITH an SCTPHDR but no IP header, endpoint inp and sa structure: |
| 4122 | * - fill in the HMAC digest of any AUTH chunk in the packet. |
| 4123 | * - calculate and fill in the SCTP checksum. |
| 4124 | * - prepend an IP address header. |
| 4125 | * - if boundall use INADDR_ANY. |
| 4126 | * - if boundspecific do source address selection. |
| 4127 | * - set fragmentation option for ipV4. |
| 4128 | * - On return from IP output, check/adjust mtu size of output |
| 4129 | * interface and smallest_mtu size as well. |
| 4130 | */ |
| 4131 | /* Will need ifdefs around this */ |
| 4132 | #ifdef __Panda__ |
| 4133 | pakhandle_type o_pak; |
| 4134 | #endif |
| 4135 | struct mbuf *newm; |
| 4136 | struct sctphdr *sctphdr; |
| 4137 | int packet_length; |
| 4138 | int ret; |
| 4139 | #if defined(INET) || defined(INET6) |
| 4140 | uint32_t vrf_id; |
| 4141 | #endif |
| 4142 | #if defined(INET) || defined(INET6) |
| 4143 | #if !defined(__Panda__) |
| 4144 | struct mbuf *o_pak; |
| 4145 | #endif |
| 4146 | sctp_route_t *ro = NULL; |
| 4147 | struct udphdr *udp = NULL; |
| 4148 | #endif |
| 4149 | uint8_t tos_value; |
| 4150 | #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) |
| 4151 | struct socket *so = NULL; |
| 4152 | #endif |
| 4153 |
| 4154 | #if defined(__APPLE__) |
| 4155 | if (so_locked) { |
| 4156 | sctp_lock_assert(SCTP_INP_SO(inp)); |
| 4157 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 4158 | } else { |
| 4159 | sctp_unlock_assert(SCTP_INP_SO(inp)); |
| 4160 | } |
| 4161 | #endif |
| 4162 | if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) { |
| 4163 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT); |
| 4164 | sctp_m_freem(m); |
| 4165 | return (EFAULT); |
| 4166 | } |
| 4167 | #if defined(INET) || defined(INET6) |
| 4168 | if (stcb) { |
| 4169 | vrf_id = stcb->asoc.vrf_id; |
| 4170 | } else { |
| 4171 | vrf_id = inp->def_vrf_id; |
| 4172 | } |
| 4173 | #endif |
| 4174 | /* fill in the HMAC digest for any AUTH chunk in the packet */ |
| 4175 | if ((auth != NULL) && (stcb != NULL)) { |
| 4176 | sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid); |
| 4177 | } |
| 4178 |
| 4179 | if (net) { |
| 4180 | tos_value = net->dscp; |
| 4181 | } else if (stcb) { |
| 4182 | tos_value = stcb->asoc.default_dscp; |
| 4183 | } else { |
| 4184 | tos_value = inp->sctp_ep.default_dscp; |
| 4185 | } |
| 4186 |
| 4187 | switch (to->sa_family) { |
| 4188 | #ifdef INET |
| 4189 | case AF_INET: |
| 4190 | { |
| 4191 | struct ip *ip = NULL; |
| 4192 | sctp_route_t iproute; |
| 4193 | int len; |
| 4194 |
| 4195 | len = SCTP_MIN_V4_OVERHEAD; |
| 4196 | if (port) { |
| 4197 | len += sizeof(struct udphdr); |
| 4198 | } |
| 4199 | newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA); |
| 4200 | if (newm == NULL) { |
| 4201 | sctp_m_freem(m); |
| 4202 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 4203 | return (ENOMEM); |
| 4204 | } |
| 4205 | SCTP_ALIGN_TO_END(newm, len); |
| 4206 | SCTP_BUF_LEN(newm) = len; |
| 4207 | SCTP_BUF_NEXT(newm) = m; |
| 4208 | m = newm; |
| 4209 | #if defined(__FreeBSD__) |
| 4210 | if (net != NULL) { |
| 4211 | m->m_pkthdr.flowid = net->flowid; |
| 4212 | M_HASHTYPE_SET(m, net->flowtype); |
| 4213 | } else { |
| 4214 | m->m_pkthdr.flowid = mflowid; |
| 4215 | M_HASHTYPE_SET(m, mflowtype); |
| 4216 | } |
| 4217 | #endif |
| 4218 | packet_length = sctp_calculate_len(m); |
| 4219 | ip = mtod(m, struct ip *); |
| 4220 | ip->ip_v = IPVERSION; |
| 4221 | ip->ip_hl = (sizeof(struct ip) >> 2); |
| 4222 | if (tos_value == 0) { |
| 4223 | /* |
| 4224 | * This means especially, that it is not set at the |
| 4225 | * SCTP layer. So use the value from the IP layer. |
| 4226 | */ |
| 4227 | #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Panda__) || defined(__Windows__) || defined(__Userspace__) |
| 4228 | tos_value = inp->ip_inp.inp.inp_ip_tos; |
| 4229 | #else |
| 4230 | tos_value = inp->inp_ip_tos; |
| 4231 | #endif |
| 4232 | } |
| 4233 | tos_value &= 0xfc; |
| 4234 | if (ecn_ok) { |
| 4235 | tos_value |= sctp_get_ect(stcb); |
| 4236 | } |
| 4237 | if ((nofragment_flag) && (port == 0)) { |
| 4238 | #if defined(__FreeBSD__) |
| 4239 | #if __FreeBSD_version >= 1000000 |
| 4240 | ip->ip_off = htons(IP_DF); |
| 4241 | #else |
| 4242 | ip->ip_off = IP_DF; |
| 4243 | #endif |
| 4244 | #elif defined(WITH_CONVERT_IP_OFF) || defined(__APPLE__) || defined(__Userspace_os_Darwin) |
| 4245 | ip->ip_off = IP_DF; |
| 4246 | #else |
| 4247 | ip->ip_off = htons(IP_DF); |
| 4248 | #endif |
| 4249 | } else { |
| 4250 | #if defined(__FreeBSD__) && __FreeBSD_version >= 1000000 |
| 4251 | ip->ip_off = htons(0); |
| 4252 | #else |
| 4253 | ip->ip_off = 0; |
| 4254 | #endif |
| 4255 | } |
| 4256 | #if defined(__FreeBSD__) |
| 4257 | /* FreeBSD has a function for ip_id's */ |
| 4258 | ip_fillid(ip); |
| 4259 | #elif defined(__APPLE__) |
| 4260 | #if RANDOM_IP_ID |
| 4261 | ip->ip_id = ip_randomid(); |
| 4262 | #else |
| 4263 | ip->ip_id = htons(ip_id++); |
| 4264 | #endif |
| 4265 | #elif defined(__Userspace__) |
| 4266 | ip->ip_id = htons(SCTP_IP_ID(inp)++); |
| 4267 | #else |
| 4268 | ip->ip_id = SCTP_IP_ID(inp)++; |
| 4269 | #endif |
| 4270 |
| 4271 | #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Panda__) || defined(__Windows__) || defined(__Userspace__) |
| 4272 | ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl; |
| 4273 | #else |
| 4274 | ip->ip_ttl = inp->inp_ip_ttl; |
| 4275 | #endif |
| 4276 | #if defined(__FreeBSD__) && __FreeBSD_version >= 1000000 |
| 4277 | ip->ip_len = htons(packet_length); |
| 4278 | #else |
| 4279 | ip->ip_len = packet_length; |
| 4280 | #endif |
| 4281 | ip->ip_tos = tos_value; |
| 4282 | if (port) { |
| 4283 | ip->ip_p = IPPROTO_UDP; |
| 4284 | } else { |
| 4285 | ip->ip_p = IPPROTO_SCTP; |
| 4286 | } |
| 4287 | ip->ip_sum = 0; |
| 4288 | if (net == NULL) { |
| 4289 | ro = &iproute; |
| 4290 | memset(&iproute, 0, sizeof(iproute)); |
| 4291 | #ifdef HAVE_SA_LEN |
| 4292 | memcpy(&ro->ro_dst, to, to->sa_len); |
| 4293 | #else |
| 4294 | memcpy(&ro->ro_dst, to, sizeof(struct sockaddr_in)); |
| 4295 | #endif |
| 4296 | } else { |
| 4297 | ro = (sctp_route_t *)&net->ro; |
| 4298 | } |
| 4299 | /* Now the address selection part */ |
| 4300 | ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr; |
| 4301 |
| 4302 | /* call the routine to select the src address */ |
| 4303 | if (net && out_of_asoc_ok == 0) { |
| 4304 | if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED|SCTP_ADDR_IFA_UNUSEABLE))) { |
| 4305 | sctp_free_ifa(net->ro._s_addr); |
| 4306 | net->ro._s_addr = NULL; |
| 4307 | net->src_addr_selected = 0; |
| 4308 | if (ro->ro_rt) { |
| 4309 | RTFREE(ro->ro_rt); |
| 4310 | ro->ro_rt = NULL; |
| 4311 | } |
| 4312 | } |
| 4313 | if (net->src_addr_selected == 0) { |
| 4314 | /* Cache the source address */ |
| 4315 | net->ro._s_addr = sctp_source_address_selection(inp,stcb, |
| 4316 | ro, net, 0, |
| 4317 | vrf_id); |
| 4318 | net->src_addr_selected = 1; |
| 4319 | } |
| 4320 | if (net->ro._s_addr == NULL) { |
| 4321 | /* No route to host */ |
| 4322 | net->src_addr_selected = 0; |
| 4323 | sctp_handle_no_route(stcb, net, so_locked); |
| 4324 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); |
| 4325 | sctp_m_freem(m); |
| 4326 | return (EHOSTUNREACH); |
| 4327 | } |
| 4328 | ip->ip_src = net->ro._s_addr->address.sin.sin_addr; |
| 4329 | } else { |
| 4330 | if (over_addr == NULL) { |
| 4331 | struct sctp_ifa *_lsrc; |
| 4332 |
| 4333 | _lsrc = sctp_source_address_selection(inp, stcb, ro, |
| 4334 | net, |
| 4335 | out_of_asoc_ok, |
| 4336 | vrf_id); |
| 4337 | if (_lsrc == NULL) { |
| 4338 | sctp_handle_no_route(stcb, net, so_locked); |
| 4339 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); |
| 4340 | sctp_m_freem(m); |
| 4341 | return (EHOSTUNREACH); |
| 4342 | } |
| 4343 | ip->ip_src = _lsrc->address.sin.sin_addr; |
| 4344 | sctp_free_ifa(_lsrc); |
| 4345 | } else { |
| 4346 | ip->ip_src = over_addr->sin.sin_addr; |
| 4347 | SCTP_RTALLOC(ro, vrf_id, inp->fibnum); |
| 4348 | } |
| 4349 | } |
| 4350 | if (port) { |
| 4351 | if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) { |
| 4352 | sctp_handle_no_route(stcb, net, so_locked); |
| 4353 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); |
| 4354 | sctp_m_freem(m); |
| 4355 | return (EHOSTUNREACH); |
| 4356 | } |
| 4357 | udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip)); |
| 4358 | udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)); |
| 4359 | udp->uh_dport = port; |
| 4360 | udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip))); |
| 4361 | #if !defined(__Windows__) && !defined(__Userspace__) |
| 4362 | #if defined(__FreeBSD__) && ((__FreeBSD_version > 803000 && __FreeBSD_version < 900000) || __FreeBSD_version > 900000) |
| 4363 | if (V_udp_cksum) { |
| 4364 | udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP)); |
| 4365 | } else { |
| 4366 | udp->uh_sum = 0; |
| 4367 | } |
| 4368 | #else |
| 4369 | udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP)); |
| 4370 | #endif |
| 4371 | #else |
| 4372 | udp->uh_sum = 0; |
| 4373 | #endif |
| 4374 | sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr)); |
| 4375 | } else { |
| 4376 | sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip)); |
| 4377 | } |
| 4378 |
| 4379 | sctphdr->src_port = src_port; |
| 4380 | sctphdr->dest_port = dest_port; |
| 4381 | sctphdr->v_tag = v_tag; |
| 4382 | sctphdr->checksum = 0; |
| 4383 |
| 4384 | /* |
| 4385 | * If source address selection fails and we find no route |
| 4386 | * then the ip_output should fail as well with a |
| 4387 | * NO_ROUTE_TO_HOST type error. We probably should catch |
| 4388 | * that somewhere and abort the association right away |
| 4389 | * (assuming this is an INIT being sent). |
| 4390 | */ |
| 4391 | if (ro->ro_rt == NULL) { |
| 4392 | /* |
| 4393 | * src addr selection failed to find a route (or |
| 4394 | * valid source addr), so we can't get there from |
| 4395 | * here (yet)! |
| 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 | if (ro != &iproute) { |
| 4403 | memcpy(&iproute, ro, sizeof(*ro)); |
| 4404 | } |
| 4405 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n", |
| 4406 | (uint32_t) (ntohl(ip->ip_src.s_addr))); |
| 4407 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n", |
| 4408 | (uint32_t)(ntohl(ip->ip_dst.s_addr))); |
| 4409 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n", |
| 4410 | (void *)ro->ro_rt); |
| 4411 |
| 4412 | if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) { |
| 4413 | /* failed to prepend data, give up */ |
| 4414 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 4415 | sctp_m_freem(m); |
| 4416 | return (ENOMEM); |
| 4417 | } |
| 4418 | SCTP_ATTACH_CHAIN(o_pak, m, packet_length); |
| 4419 | if (port) { |
| 4420 | sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr)); |
| 4421 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 4422 | #if !defined(__Windows__) && !defined(__Userspace__) |
| 4423 | #if defined(__FreeBSD__) && ((__FreeBSD_version > 803000 && __FreeBSD_version < 900000) || __FreeBSD_version > 900000) |
| 4424 | if (V_udp_cksum) { |
| 4425 | SCTP_ENABLE_UDP_CSUM(o_pak); |
| 4426 | } |
| 4427 | #else |
| 4428 | SCTP_ENABLE_UDP_CSUM(o_pak); |
| 4429 | #endif |
| 4430 | #endif |
| 4431 | } else { |
| 4432 | #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 |
| 4433 | m->m_pkthdr.csum_flags = CSUM_SCTP; |
| 4434 | m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum); |
| 4435 | SCTP_STAT_INCR(sctps_sendhwcrc); |
| 4436 | #else |
| 4437 | if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) && |
| 4438 | (stcb) && (stcb->asoc.scope.loopback_scope))) { |
| 4439 | sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip)); |
| 4440 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 4441 | } else { |
| 4442 | SCTP_STAT_INCR(sctps_sendhwcrc); |
| 4443 | } |
| 4444 | #endif |
| 4445 | } |
| 4446 | #ifdef SCTP_PACKET_LOGGING |
| 4447 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) |
| 4448 | sctp_packet_log(o_pak); |
| 4449 | #endif |
| 4450 | /* send it out. table id is taken from stcb */ |
| 4451 | #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) |
| 4452 | if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) { |
| 4453 | so = SCTP_INP_SO(inp); |
| 4454 | SCTP_SOCKET_UNLOCK(so, 0); |
| 4455 | } |
| 4456 | #endif |
| 4457 | #if defined(__FreeBSD__) |
| 4458 | SCTP_PROBE5(send, NULL, stcb, ip, stcb, sctphdr); |
| 4459 | #endif |
| 4460 | SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id); |
| 4461 | #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) |
| 4462 | if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) { |
| 4463 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 4464 | SCTP_TCB_UNLOCK(stcb); |
| 4465 | SCTP_SOCKET_LOCK(so, 0); |
| 4466 | SCTP_TCB_LOCK(stcb); |
| 4467 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
| 4468 | } |
| 4469 | #endif |
| 4470 | #if defined(__FreeBSD__) |
| 4471 | if (port) { |
| 4472 | UDPSTAT_INC(udps_opackets); |
| 4473 | } |
| 4474 | #endif |
| 4475 | SCTP_STAT_INCR(sctps_sendpackets); |
| 4476 | SCTP_STAT_INCR_COUNTER64(sctps_outpackets); |
| 4477 | if (ret) |
| 4478 | SCTP_STAT_INCR(sctps_senderrors); |
| 4479 |
| 4480 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret); |
| 4481 | if (net == NULL) { |
| 4482 | /* free tempy routes */ |
| 4483 | #if defined(__FreeBSD__) && __FreeBSD_version > 901000 |
| 4484 | RO_RTFREE(ro); |
| 4485 | #else |
| 4486 | if (ro->ro_rt) { |
| 4487 | RTFREE(ro->ro_rt); |
| 4488 | ro->ro_rt = NULL; |
| 4489 | } |
| 4490 | #endif |
| 4491 | } else { |
| 4492 | if ((ro->ro_rt != NULL) && (net->ro._s_addr) && |
| 4493 | ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) { |
| 4494 | uint32_t mtu; |
| 4495 |
| 4496 | mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt); |
| 4497 | if (mtu > 0) { |
| 4498 | if (net->port) { |
| 4499 | mtu -= sizeof(struct udphdr); |
| 4500 | } |
| 4501 | if (mtu < net->mtu) { |
| 4502 | if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) { |
| 4503 | sctp_mtu_size_reset(inp, &stcb->asoc, mtu); |
| 4504 | } |
| 4505 | net->mtu = mtu; |
| 4506 | } |
| 4507 | } |
| 4508 | } else if (ro->ro_rt == NULL) { |
| 4509 | /* route was freed */ |
| 4510 | if (net->ro._s_addr && |
| 4511 | net->src_addr_selected) { |
| 4512 | sctp_free_ifa(net->ro._s_addr); |
| 4513 | net->ro._s_addr = NULL; |
| 4514 | } |
| 4515 | net->src_addr_selected = 0; |
| 4516 | } |
| 4517 | } |
| 4518 | return (ret); |
| 4519 | } |
| 4520 | #endif |
| 4521 | #ifdef INET6 |
| 4522 | case AF_INET6: |
| 4523 | { |
| 4524 | uint32_t flowlabel, flowinfo; |
| 4525 | struct ip6_hdr *ip6h; |
| 4526 | struct route_in6 ip6route; |
| 4527 | #if !(defined(__Panda__) || defined(__Userspace__)) |
| 4528 | struct ifnet *ifp; |
| 4529 | #endif |
| 4530 | struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp; |
| 4531 | int prev_scope = 0; |
| 4532 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4533 | struct sockaddr_in6 lsa6_storage; |
| 4534 | int error; |
| 4535 | #endif |
| 4536 | u_short prev_port = 0; |
| 4537 | int len; |
| 4538 |
| 4539 | if (net) { |
| 4540 | flowlabel = net->flowlabel; |
| 4541 | } else if (stcb) { |
| 4542 | flowlabel = stcb->asoc.default_flowlabel; |
| 4543 | } else { |
| 4544 | flowlabel = inp->sctp_ep.default_flowlabel; |
| 4545 | } |
| 4546 | if (flowlabel == 0) { |
| 4547 | /* |
| 4548 | * This means especially, that it is not set at the |
| 4549 | * SCTP layer. So use the value from the IP layer. |
| 4550 | */ |
| 4551 | #if defined(__APPLE__) && (!defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)) |
| 4552 | flowlabel = ntohl(inp->ip_inp.inp.inp_flow); |
| 4553 | #else |
| 4554 | flowlabel = ntohl(((struct inpcb *)inp)->inp_flow); |
| 4555 | #endif |
| 4556 | } |
| 4557 | flowlabel &= 0x000fffff; |
| 4558 | len = SCTP_MIN_OVERHEAD; |
| 4559 | if (port) { |
| 4560 | len += sizeof(struct udphdr); |
| 4561 | } |
| 4562 | newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA); |
| 4563 | if (newm == NULL) { |
| 4564 | sctp_m_freem(m); |
| 4565 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 4566 | return (ENOMEM); |
| 4567 | } |
| 4568 | SCTP_ALIGN_TO_END(newm, len); |
| 4569 | SCTP_BUF_LEN(newm) = len; |
| 4570 | SCTP_BUF_NEXT(newm) = m; |
| 4571 | m = newm; |
| 4572 | #if defined(__FreeBSD__) |
| 4573 | if (net != NULL) { |
| 4574 | m->m_pkthdr.flowid = net->flowid; |
| 4575 | M_HASHTYPE_SET(m, net->flowtype); |
| 4576 | } else { |
| 4577 | m->m_pkthdr.flowid = mflowid; |
| 4578 | M_HASHTYPE_SET(m, mflowtype); |
| 4579 | } |
| 4580 | #endif |
| 4581 | packet_length = sctp_calculate_len(m); |
| 4582 |
| 4583 | ip6h = mtod(m, struct ip6_hdr *); |
| 4584 | /* protect *sin6 from overwrite */ |
| 4585 | sin6 = (struct sockaddr_in6 *)to; |
| 4586 | tmp = *sin6; |
| 4587 | sin6 = &tmp; |
| 4588 |
| 4589 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4590 | /* KAME hack: embed scopeid */ |
| 4591 | #if defined(__APPLE__) |
| 4592 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
| 4593 | if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0) |
| 4594 | #else |
| 4595 | if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL) != 0) |
| 4596 | #endif |
| 4597 | #elif defined(SCTP_KAME) |
| 4598 | if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) |
| 4599 | #else |
| 4600 | if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) |
| 4601 | #endif |
| 4602 | { |
| 4603 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 4604 | sctp_m_freem(m); |
| 4605 | return (EINVAL); |
| 4606 | } |
| 4607 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4608 | if (net == NULL) { |
| 4609 | memset(&ip6route, 0, sizeof(ip6route)); |
| 4610 | ro = (sctp_route_t *)&ip6route; |
| 4611 | #ifdef HAVE_SIN6_LEN |
| 4612 | memcpy(&ro->ro_dst, sin6, sin6->sin6_len); |
| 4613 | #else |
| 4614 | memcpy(&ro->ro_dst, sin6, sizeof(struct sockaddr_in6)); |
| 4615 | #endif |
| 4616 | } else { |
| 4617 | ro = (sctp_route_t *)&net->ro; |
| 4618 | } |
| 4619 | /* |
| 4620 | * We assume here that inp_flow is in host byte order within |
| 4621 | * the TCB! |
| 4622 | */ |
| 4623 | if (tos_value == 0) { |
| 4624 | /* |
| 4625 | * This means especially, that it is not set at the |
| 4626 | * SCTP layer. So use the value from the IP layer. |
| 4627 | */ |
| 4628 | #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Panda__) || defined(__Windows__) || defined(__Userspace__) |
| 4629 | #if defined(__APPLE__) && (!defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)) |
| 4630 | tos_value = (ntohl(inp->ip_inp.inp.inp_flow) >> 20) & 0xff; |
| 4631 | #else |
| 4632 | tos_value = (ntohl(((struct inpcb *)inp)->inp_flow) >> 20) & 0xff; |
| 4633 | #endif |
| 4634 | #endif |
| 4635 | } |
| 4636 | tos_value &= 0xfc; |
| 4637 | if (ecn_ok) { |
| 4638 | tos_value |= sctp_get_ect(stcb); |
| 4639 | } |
| 4640 | flowinfo = 0x06; |
| 4641 | flowinfo <<= 8; |
| 4642 | flowinfo |= tos_value; |
| 4643 | flowinfo <<= 20; |
| 4644 | flowinfo |= flowlabel; |
| 4645 | ip6h->ip6_flow = htonl(flowinfo); |
| 4646 | if (port) { |
| 4647 | ip6h->ip6_nxt = IPPROTO_UDP; |
| 4648 | } else { |
| 4649 | ip6h->ip6_nxt = IPPROTO_SCTP; |
| 4650 | } |
| 4651 | ip6h->ip6_plen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr))); |
| 4652 | ip6h->ip6_dst = sin6->sin6_addr; |
| 4653 |
| 4654 | /* |
| 4655 | * Add SRC address selection here: we can only reuse to a |
| 4656 | * limited degree the kame src-addr-sel, since we can try |
| 4657 | * their selection but it may not be bound. |
| 4658 | */ |
| 4659 | memset(&lsa6_tmp, 0, sizeof(lsa6_tmp)); |
| 4660 | lsa6_tmp.sin6_family = AF_INET6; |
| 4661 | #ifdef HAVE_SIN6_LEN |
| 4662 | lsa6_tmp.sin6_len = sizeof(lsa6_tmp); |
| 4663 | #endif |
| 4664 | lsa6 = &lsa6_tmp; |
| 4665 | if (net && out_of_asoc_ok == 0) { |
| 4666 | if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED|SCTP_ADDR_IFA_UNUSEABLE))) { |
| 4667 | sctp_free_ifa(net->ro._s_addr); |
| 4668 | net->ro._s_addr = NULL; |
| 4669 | net->src_addr_selected = 0; |
| 4670 | if (ro->ro_rt) { |
| 4671 | RTFREE(ro->ro_rt); |
| 4672 | ro->ro_rt = NULL; |
| 4673 | } |
| 4674 | } |
| 4675 | if (net->src_addr_selected == 0) { |
| 4676 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4677 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
| 4678 | /* KAME hack: embed scopeid */ |
| 4679 | #if defined(__APPLE__) |
| 4680 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
| 4681 | if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0) |
| 4682 | #else |
| 4683 | if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL) != 0) |
| 4684 | #endif |
| 4685 | #elif defined(SCTP_KAME) |
| 4686 | if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) |
| 4687 | #else |
| 4688 | if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) |
| 4689 | #endif |
| 4690 | { |
| 4691 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 4692 | sctp_m_freem(m); |
| 4693 | return (EINVAL); |
| 4694 | } |
| 4695 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4696 | /* Cache the source address */ |
| 4697 | net->ro._s_addr = sctp_source_address_selection(inp, |
| 4698 | stcb, |
| 4699 | ro, |
| 4700 | net, |
| 4701 | 0, |
| 4702 | vrf_id); |
| 4703 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4704 | #ifdef SCTP_KAME |
| 4705 | (void)sa6_recoverscope(sin6); |
| 4706 | #else |
| 4707 | (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL); |
| 4708 | #endif /* SCTP_KAME */ |
| 4709 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4710 | net->src_addr_selected = 1; |
| 4711 | } |
| 4712 | if (net->ro._s_addr == NULL) { |
| 4713 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n"); |
| 4714 | net->src_addr_selected = 0; |
| 4715 | sctp_handle_no_route(stcb, net, so_locked); |
| 4716 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); |
| 4717 | sctp_m_freem(m); |
| 4718 | return (EHOSTUNREACH); |
| 4719 | } |
| 4720 | lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr; |
| 4721 | } else { |
| 4722 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4723 | sin6 = (struct sockaddr_in6 *)&ro->ro_dst; |
| 4724 | /* KAME hack: embed scopeid */ |
| 4725 | #if defined(__APPLE__) |
| 4726 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
| 4727 | if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0) |
| 4728 | #else |
| 4729 | if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL) != 0) |
| 4730 | #endif |
| 4731 | #elif defined(SCTP_KAME) |
| 4732 | if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) |
| 4733 | #else |
| 4734 | if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) |
| 4735 | #endif |
| 4736 | { |
| 4737 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 4738 | sctp_m_freem(m); |
| 4739 | return (EINVAL); |
| 4740 | } |
| 4741 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4742 | if (over_addr == NULL) { |
| 4743 | struct sctp_ifa *_lsrc; |
| 4744 |
| 4745 | _lsrc = sctp_source_address_selection(inp, stcb, ro, |
| 4746 | net, |
| 4747 | out_of_asoc_ok, |
| 4748 | vrf_id); |
| 4749 | if (_lsrc == NULL) { |
| 4750 | sctp_handle_no_route(stcb, net, so_locked); |
| 4751 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); |
| 4752 | sctp_m_freem(m); |
| 4753 | return (EHOSTUNREACH); |
| 4754 | } |
| 4755 | lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr; |
| 4756 | sctp_free_ifa(_lsrc); |
| 4757 | } else { |
| 4758 | lsa6->sin6_addr = over_addr->sin6.sin6_addr; |
| 4759 | SCTP_RTALLOC(ro, vrf_id, inp->fibnum); |
| 4760 | } |
| 4761 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4762 | #ifdef SCTP_KAME |
| 4763 | (void)sa6_recoverscope(sin6); |
| 4764 | #else |
| 4765 | (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL); |
| 4766 | #endif /* SCTP_KAME */ |
| 4767 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4768 | } |
| 4769 | lsa6->sin6_port = inp->sctp_lport; |
| 4770 |
| 4771 | if (ro->ro_rt == NULL) { |
| 4772 | /* |
| 4773 | * src addr selection failed to find a route (or |
| 4774 | * valid source addr), so we can't get there from |
| 4775 | * here! |
| 4776 | */ |
| 4777 | sctp_handle_no_route(stcb, net, so_locked); |
| 4778 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); |
| 4779 | sctp_m_freem(m); |
| 4780 | return (EHOSTUNREACH); |
| 4781 | } |
| 4782 | #ifndef SCOPEDROUTING |
| 4783 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4784 | /* |
| 4785 | * XXX: sa6 may not have a valid sin6_scope_id in the |
| 4786 | * non-SCOPEDROUTING case. |
| 4787 | */ |
| 4788 | memset(&lsa6_storage, 0, sizeof(lsa6_storage)); |
| 4789 | lsa6_storage.sin6_family = AF_INET6; |
| 4790 | #ifdef HAVE_SIN6_LEN |
| 4791 | lsa6_storage.sin6_len = sizeof(lsa6_storage); |
| 4792 | #endif |
| 4793 | #ifdef SCTP_KAME |
| 4794 | lsa6_storage.sin6_addr = lsa6->sin6_addr; |
| 4795 | if ((error = sa6_recoverscope(&lsa6_storage)) != 0) { |
| 4796 | #else |
| 4797 | if ((error = in6_recoverscope(&lsa6_storage, &lsa6->sin6_addr, |
| 4798 | NULL)) != 0) { |
| 4799 | #endif /* SCTP_KAME */ |
| 4800 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error); |
| 4801 | sctp_m_freem(m); |
| 4802 | return (error); |
| 4803 | } |
| 4804 | /* XXX */ |
| 4805 | lsa6_storage.sin6_addr = lsa6->sin6_addr; |
| 4806 | lsa6_storage.sin6_port = inp->sctp_lport; |
| 4807 | lsa6 = &lsa6_storage; |
| 4808 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4809 | #endif /* SCOPEDROUTING */ |
| 4810 | ip6h->ip6_src = lsa6->sin6_addr; |
| 4811 |
| 4812 | if (port) { |
| 4813 | if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) { |
| 4814 | sctp_handle_no_route(stcb, net, so_locked); |
| 4815 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); |
| 4816 | sctp_m_freem(m); |
| 4817 | return (EHOSTUNREACH); |
| 4818 | } |
| 4819 | udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr)); |
| 4820 | udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)); |
| 4821 | udp->uh_dport = port; |
| 4822 | udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr))); |
| 4823 | udp->uh_sum = 0; |
| 4824 | sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr)); |
| 4825 | } else { |
| 4826 | sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr)); |
| 4827 | } |
| 4828 |
| 4829 | sctphdr->src_port = src_port; |
| 4830 | sctphdr->dest_port = dest_port; |
| 4831 | sctphdr->v_tag = v_tag; |
| 4832 | sctphdr->checksum = 0; |
| 4833 |
| 4834 | /* |
| 4835 | * We set the hop limit now since there is a good chance |
| 4836 | * that our ro pointer is now filled |
| 4837 | */ |
| 4838 | ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro); |
| 4839 | #if !(defined(__Panda__) || defined(__Userspace__)) |
| 4840 | ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro); |
| 4841 | #endif |
| 4842 |
| 4843 | #ifdef SCTP_DEBUG |
| 4844 | /* Copy to be sure something bad is not happening */ |
| 4845 | sin6->sin6_addr = ip6h->ip6_dst; |
| 4846 | lsa6->sin6_addr = ip6h->ip6_src; |
| 4847 | #endif |
| 4848 |
| 4849 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n"); |
| 4850 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: "); |
| 4851 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6); |
| 4852 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: "); |
| 4853 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6); |
| 4854 | if (net) { |
| 4855 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
| 4856 | /* preserve the port and scope for link local send */ |
| 4857 | prev_scope = sin6->sin6_scope_id; |
| 4858 | prev_port = sin6->sin6_port; |
| 4859 | } |
| 4860 |
| 4861 | if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) { |
| 4862 | /* failed to prepend data, give up */ |
| 4863 | sctp_m_freem(m); |
| 4864 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 4865 | return (ENOMEM); |
| 4866 | } |
| 4867 | SCTP_ATTACH_CHAIN(o_pak, m, packet_length); |
| 4868 | if (port) { |
| 4869 | sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr)); |
| 4870 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 4871 | #if defined(__Windows__) |
| 4872 | udp->uh_sum = 0; |
| 4873 | #elif !defined(__Userspace__) |
| 4874 | if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) { |
| 4875 | udp->uh_sum = 0xffff; |
| 4876 | } |
| 4877 | #endif |
| 4878 | } else { |
| 4879 | #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 |
| 4880 | #if __FreeBSD_version < 900000 |
| 4881 | sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr)); |
| 4882 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 4883 | #else |
| 4884 | #if __FreeBSD_version > 901000 |
| 4885 | m->m_pkthdr.csum_flags = CSUM_SCTP_IPV6; |
| 4886 | #else |
| 4887 | m->m_pkthdr.csum_flags = CSUM_SCTP; |
| 4888 | #endif |
| 4889 | m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum); |
| 4890 | SCTP_STAT_INCR(sctps_sendhwcrc); |
| 4891 | #endif |
| 4892 | #else |
| 4893 | if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) && |
| 4894 | (stcb) && (stcb->asoc.scope.loopback_scope))) { |
| 4895 | sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr)); |
| 4896 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 4897 | } else { |
| 4898 | SCTP_STAT_INCR(sctps_sendhwcrc); |
| 4899 | } |
| 4900 | #endif |
| 4901 | } |
| 4902 | /* send it out. table id is taken from stcb */ |
| 4903 | #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) |
| 4904 | if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) { |
| 4905 | so = SCTP_INP_SO(inp); |
| 4906 | SCTP_SOCKET_UNLOCK(so, 0); |
| 4907 | } |
| 4908 | #endif |
| 4909 | #ifdef SCTP_PACKET_LOGGING |
| 4910 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) |
| 4911 | sctp_packet_log(o_pak); |
| 4912 | #endif |
| 4913 | #if !(defined(__Panda__) || defined(__Userspace__)) |
| 4914 | #if defined(__FreeBSD__) |
| 4915 | SCTP_PROBE5(send, NULL, stcb, ip6h, stcb, sctphdr); |
| 4916 | #endif |
| 4917 | SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id); |
| 4918 | #else |
| 4919 | SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, NULL, stcb, vrf_id); |
| 4920 | #endif |
| 4921 | #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) |
| 4922 | if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) { |
| 4923 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 4924 | SCTP_TCB_UNLOCK(stcb); |
| 4925 | SCTP_SOCKET_LOCK(so, 0); |
| 4926 | SCTP_TCB_LOCK(stcb); |
| 4927 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
| 4928 | } |
| 4929 | #endif |
| 4930 | if (net) { |
| 4931 | /* for link local this must be done */ |
| 4932 | sin6->sin6_scope_id = prev_scope; |
| 4933 | sin6->sin6_port = prev_port; |
| 4934 | } |
| 4935 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret); |
| 4936 | #if defined(__FreeBSD__) |
| 4937 | if (port) { |
| 4938 | UDPSTAT_INC(udps_opackets); |
| 4939 | } |
| 4940 | #endif |
| 4941 | SCTP_STAT_INCR(sctps_sendpackets); |
| 4942 | SCTP_STAT_INCR_COUNTER64(sctps_outpackets); |
| 4943 | if (ret) { |
| 4944 | SCTP_STAT_INCR(sctps_senderrors); |
| 4945 | } |
| 4946 | if (net == NULL) { |
| 4947 | /* Now if we had a temp route free it */ |
| 4948 | #if defined(__FreeBSD__) && __FreeBSD_version > 901000 |
| 4949 | RO_RTFREE(ro); |
| 4950 | #else |
| 4951 | if (ro->ro_rt) { |
| 4952 | RTFREE(ro->ro_rt); |
| 4953 | ro->ro_rt = NULL; |
| 4954 | } |
| 4955 | #endif |
| 4956 | } else { |
| 4957 | /* PMTU check versus smallest asoc MTU goes here */ |
| 4958 | if (ro->ro_rt == NULL) { |
| 4959 | /* Route was freed */ |
| 4960 | if (net->ro._s_addr && |
| 4961 | net->src_addr_selected) { |
| 4962 | sctp_free_ifa(net->ro._s_addr); |
| 4963 | net->ro._s_addr = NULL; |
| 4964 | } |
| 4965 | net->src_addr_selected = 0; |
| 4966 | } |
| 4967 | if ((ro->ro_rt != NULL) && (net->ro._s_addr) && |
| 4968 | ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) { |
| 4969 | uint32_t mtu; |
| 4970 |
| 4971 | mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt); |
| 4972 | if (mtu > 0) { |
| 4973 | if (net->port) { |
| 4974 | mtu -= sizeof(struct udphdr); |
| 4975 | } |
| 4976 | if (mtu < net->mtu) { |
| 4977 | if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) { |
| 4978 | sctp_mtu_size_reset(inp, &stcb->asoc, mtu); |
| 4979 | } |
| 4980 | net->mtu = mtu; |
| 4981 | } |
| 4982 | } |
| 4983 | } |
| 4984 | #if !defined(__Panda__) && !defined(__Userspace__) |
| 4985 | else if (ifp) { |
| 4986 | #if defined(__Windows__) |
| 4987 | #define ND_IFINFO(ifp) (ifp) |
| 4988 | #define linkmtu if_mtu |
| 4989 | #endif |
| 4990 | if (ND_IFINFO(ifp)->linkmtu && |
| 4991 | (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) { |
| 4992 | sctp_mtu_size_reset(inp, |
| 4993 | &stcb->asoc, |
| 4994 | ND_IFINFO(ifp)->linkmtu); |
| 4995 | } |
| 4996 | } |
| 4997 | #endif |
| 4998 | } |
| 4999 | return (ret); |
| 5000 | } |
| 5001 | #endif |
| 5002 | #if defined(__Userspace__) |
| 5003 | case AF_CONN: |
| 5004 | { |
| 5005 | char *buffer; |
| 5006 | struct sockaddr_conn *sconn; |
| 5007 | int len; |
| 5008 |
| 5009 | sconn = (struct sockaddr_conn *)to; |
| 5010 | len = sizeof(struct sctphdr); |
| 5011 | newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA); |
| 5012 | if (newm == NULL) { |
| 5013 | sctp_m_freem(m); |
| 5014 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 5015 | return (ENOMEM); |
| 5016 | } |
| 5017 | SCTP_ALIGN_TO_END(newm, len); |
| 5018 | SCTP_BUF_LEN(newm) = len; |
| 5019 | SCTP_BUF_NEXT(newm) = m; |
| 5020 | m = newm; |
| 5021 | packet_length = sctp_calculate_len(m); |
| 5022 | sctphdr = mtod(m, struct sctphdr *); |
| 5023 | sctphdr->src_port = src_port; |
| 5024 | sctphdr->dest_port = dest_port; |
| 5025 | sctphdr->v_tag = v_tag; |
| 5026 | sctphdr->checksum = 0; |
| 5027 | if (SCTP_BASE_VAR(crc32c_offloaded) == 0) { |
| 5028 | sctphdr->checksum = sctp_calculate_cksum(m, 0); |
| 5029 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 5030 | } else { |
| 5031 | SCTP_STAT_INCR(sctps_sendhwcrc); |
| 5032 | } |
| 5033 | if (tos_value == 0) { |
| 5034 | tos_value = inp->ip_inp.inp.inp_ip_tos; |
| 5035 | } |
| 5036 | tos_value &= 0xfc; |
| 5037 | if (ecn_ok) { |
| 5038 | tos_value |= sctp_get_ect(stcb); |
| 5039 | } |
| 5040 | /* Don't alloc/free for each packet */ |
| 5041 | if ((buffer = malloc(packet_length)) != NULL) { |
| 5042 | m_copydata(m, 0, packet_length, buffer); |
| 5043 | ret = SCTP_BASE_VAR(conn_output)(sconn->sconn_addr, buffer, packet_length, tos_value, nofragment_flag); |
| 5044 | free(buffer); |
| 5045 | } else { |
| 5046 | ret = ENOMEM; |
| 5047 | } |
| 5048 | sctp_m_freem(m); |
| 5049 | return (ret); |
| 5050 | } |
| 5051 | #endif |
| 5052 | default: |
| 5053 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n", |
| 5054 | ((struct sockaddr *)to)->sa_family); |
| 5055 | sctp_m_freem(m); |
| 5056 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT); |
| 5057 | return (EFAULT); |
| 5058 | } |
| 5059 | } |
| 5060 |
| 5061 |
| 5062 | void |
| 5063 | sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked |
| 5064 | #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) |
| 5065 | SCTP_UNUSED |
| 5066 | #endif |
| 5067 | ) |
| 5068 | { |
| 5069 | struct mbuf *m, *m_last; |
| 5070 | struct sctp_nets *net; |
| 5071 | struct sctp_init_chunk *init; |
| 5072 | struct sctp_supported_addr_param *sup_addr; |
| 5073 | struct sctp_adaptation_layer_indication *ali; |
| 5074 | struct sctp_supported_chunk_types_param *pr_supported; |
| 5075 | struct sctp_paramhdr *ph; |
| 5076 | int cnt_inits_to = 0; |
| 5077 | int error; |
| 5078 | uint16_t num_ext, chunk_len, padding_len, parameter_len; |
| 5079 |
| 5080 | #if defined(__APPLE__) |
| 5081 | if (so_locked) { |
| 5082 | sctp_lock_assert(SCTP_INP_SO(inp)); |
| 5083 | } else { |
| 5084 | sctp_unlock_assert(SCTP_INP_SO(inp)); |
| 5085 | } |
| 5086 | #endif |
| 5087 | /* INIT's always go to the primary (and usually ONLY address) */ |
| 5088 | net = stcb->asoc.primary_destination; |
| 5089 | if (net == NULL) { |
| 5090 | net = TAILQ_FIRST(&stcb->asoc.nets); |
| 5091 | if (net == NULL) { |
| 5092 | /* TSNH */ |
| 5093 | return; |
| 5094 | } |
| 5095 | /* we confirm any address we send an INIT to */ |
| 5096 | net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; |
| 5097 | (void)sctp_set_primary_addr(stcb, NULL, net); |
| 5098 | } else { |
| 5099 | /* we confirm any address we send an INIT to */ |
| 5100 | net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; |
| 5101 | } |
| 5102 | SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n"); |
| 5103 | #ifdef INET6 |
| 5104 | if (net->ro._l_addr.sa.sa_family == AF_INET6) { |
| 5105 | /* |
| 5106 | * special hook, if we are sending to link local it will not |
| 5107 | * show up in our private address count. |
| 5108 | */ |
| 5109 | if (IN6_IS_ADDR_LINKLOCAL(&net->ro._l_addr.sin6.sin6_addr)) |
| 5110 | cnt_inits_to = 1; |
| 5111 | } |
| 5112 | #endif |
| 5113 | if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { |
| 5114 | /* This case should not happen */ |
| 5115 | SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n"); |
| 5116 | return; |
| 5117 | } |
| 5118 | /* start the INIT timer */ |
| 5119 | sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net); |
| 5120 |
| 5121 | m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_NOWAIT, 1, MT_DATA); |
| 5122 | if (m == NULL) { |
| 5123 | /* No memory, INIT timer will re-attempt. */ |
| 5124 | SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n"); |
| 5125 | return; |
| 5126 | } |
| 5127 | chunk_len = (uint16_t)sizeof(struct sctp_init_chunk); |
| 5128 | padding_len = 0; |
| 5129 | /* Now lets put the chunk header in place */ |
| 5130 | init = mtod(m, struct sctp_init_chunk *); |
| 5131 | /* now the chunk header */ |
| 5132 | init->ch.chunk_type = SCTP_INITIATION; |
| 5133 | init->ch.chunk_flags = 0; |
| 5134 | /* fill in later from mbuf we build */ |
| 5135 | init->ch.chunk_length = 0; |
| 5136 | /* place in my tag */ |
| 5137 | init->init.initiate_tag = htonl(stcb->asoc.my_vtag); |
| 5138 | /* set up some of the credits. */ |
| 5139 | init->init.a_rwnd = htonl(max(inp->sctp_socket?SCTP_SB_LIMIT_RCV(inp->sctp_socket):0, |
| 5140 | SCTP_MINIMAL_RWND)); |
| 5141 | init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams); |
| 5142 | init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams); |
| 5143 | init->init.initial_tsn = htonl(stcb->asoc.init_seq_number); |
| 5144 |
| 5145 | /* Adaptation layer indication parameter */ |
| 5146 | if (inp->sctp_ep.adaptation_layer_indicator_provided) { |
| 5147 | parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication); |
| 5148 | ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t) + chunk_len); |
| 5149 | ali->ph.param_type = htons(SCTP_ULP_ADAPTATION); |
| 5150 | ali->ph.param_length = htons(parameter_len); |
| 5151 | ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator); |
| 5152 | chunk_len += parameter_len; |
| 5153 | } |
| 5154 |
| 5155 | /* ECN parameter */ |
| 5156 | if (stcb->asoc.ecn_supported == 1) { |
| 5157 | parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); |
| 5158 | ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len); |
| 5159 | ph->param_type = htons(SCTP_ECN_CAPABLE); |
| 5160 | ph->param_length = htons(parameter_len); |
| 5161 | chunk_len += parameter_len; |
| 5162 | } |
| 5163 |
| 5164 | /* PR-SCTP supported parameter */ |
| 5165 | if (stcb->asoc.prsctp_supported == 1) { |
| 5166 | parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); |
| 5167 | ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len); |
| 5168 | ph->param_type = htons(SCTP_PRSCTP_SUPPORTED); |
| 5169 | ph->param_length = htons(parameter_len); |
| 5170 | chunk_len += parameter_len; |
| 5171 | } |
| 5172 |
| 5173 | /* Add NAT friendly parameter. */ |
| 5174 | if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) { |
| 5175 | parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); |
| 5176 | ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len); |
| 5177 | ph->param_type = htons(SCTP_HAS_NAT_SUPPORT); |
| 5178 | ph->param_length = htons(parameter_len); |
| 5179 | chunk_len += parameter_len; |
| 5180 | } |
| 5181 |
| 5182 | /* And now tell the peer which extensions we support */ |
| 5183 | num_ext = 0; |
| 5184 | pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t) + chunk_len); |
| 5185 | if (stcb->asoc.prsctp_supported == 1) { |
| 5186 | pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN; |
| 5187 | if (stcb->asoc.idata_supported) { |
| 5188 | pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN; |
| 5189 | } |
| 5190 | } |
| 5191 | if (stcb->asoc.auth_supported == 1) { |
| 5192 | pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION; |
| 5193 | } |
| 5194 | if (stcb->asoc.asconf_supported == 1) { |
| 5195 | pr_supported->chunk_types[num_ext++] = SCTP_ASCONF; |
| 5196 | pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK; |
| 5197 | } |
| 5198 | if (stcb->asoc.reconfig_supported == 1) { |
| 5199 | pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET; |
| 5200 | } |
| 5201 | if (stcb->asoc.idata_supported) { |
| 5202 | pr_supported->chunk_types[num_ext++] = SCTP_IDATA; |
| 5203 | } |
| 5204 | if (stcb->asoc.nrsack_supported == 1) { |
| 5205 | pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK; |
| 5206 | } |
| 5207 | if (stcb->asoc.pktdrop_supported == 1) { |
| 5208 | pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED; |
| 5209 | } |
| 5210 | if (num_ext > 0) { |
| 5211 | parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext; |
| 5212 | pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT); |
| 5213 | pr_supported->ph.param_length = htons(parameter_len); |
| 5214 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 5215 | chunk_len += parameter_len; |
| 5216 | } |
| 5217 | /* add authentication parameters */ |
| 5218 | if (stcb->asoc.auth_supported) { |
| 5219 | /* attach RANDOM parameter, if available */ |
| 5220 | if (stcb->asoc.authinfo.random != NULL) { |
| 5221 | struct sctp_auth_random *randp; |
| 5222 |
| 5223 | if (padding_len > 0) { |
| 5224 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 5225 | chunk_len += padding_len; |
| 5226 | padding_len = 0; |
| 5227 | } |
| 5228 | randp = (struct sctp_auth_random *)(mtod(m, caddr_t) + chunk_len); |
| 5229 | parameter_len = (uint16_t)sizeof(struct sctp_auth_random) + stcb->asoc.authinfo.random_len; |
| 5230 | /* random key already contains the header */ |
| 5231 | memcpy(randp, stcb->asoc.authinfo.random->key, parameter_len); |
| 5232 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 5233 | chunk_len += parameter_len; |
| 5234 | } |
| 5235 | /* add HMAC_ALGO parameter */ |
| 5236 | if (stcb->asoc.local_hmacs != NULL) { |
| 5237 | struct sctp_auth_hmac_algo *hmacs; |
| 5238 |
| 5239 | if (padding_len > 0) { |
| 5240 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 5241 | chunk_len += padding_len; |
| 5242 | padding_len = 0; |
| 5243 | } |
| 5244 | hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t) + chunk_len); |
| 5245 | parameter_len = (uint16_t)(sizeof(struct sctp_auth_hmac_algo) + |
| 5246 | stcb->asoc.local_hmacs->num_algo * sizeof(uint16_t)); |
| 5247 | hmacs->ph.param_type = htons(SCTP_HMAC_LIST); |
| 5248 | hmacs->ph.param_length = htons(parameter_len); |
| 5249 | sctp_serialize_hmaclist(stcb->asoc.local_hmacs, (uint8_t *)hmacs->hmac_ids); |
| 5250 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 5251 | chunk_len += parameter_len; |
| 5252 | } |
| 5253 | /* add CHUNKS parameter */ |
| 5254 | if (stcb->asoc.local_auth_chunks != NULL) { |
| 5255 | struct sctp_auth_chunk_list *chunks; |
| 5256 |
| 5257 | if (padding_len > 0) { |
| 5258 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 5259 | chunk_len += padding_len; |
| 5260 | padding_len = 0; |
| 5261 | } |
| 5262 | chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t) + chunk_len); |
| 5263 | parameter_len = (uint16_t)(sizeof(struct sctp_auth_chunk_list) + |
| 5264 | sctp_auth_get_chklist_size(stcb->asoc.local_auth_chunks)); |
| 5265 | chunks->ph.param_type = htons(SCTP_CHUNK_LIST); |
| 5266 | chunks->ph.param_length = htons(parameter_len); |
| 5267 | sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks, chunks->chunk_types); |
| 5268 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 5269 | chunk_len += parameter_len; |
| 5270 | } |
| 5271 | } |
| 5272 |
| 5273 | /* now any cookie time extensions */ |
| 5274 | if (stcb->asoc.cookie_preserve_req) { |
| 5275 | struct sctp_cookie_perserve_param *cookie_preserve; |
| 5276 |
| 5277 | if (padding_len > 0) { |
| 5278 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 5279 | chunk_len += padding_len; |
| 5280 | padding_len = 0; |
| 5281 | } |
| 5282 | parameter_len = (uint16_t)sizeof(struct sctp_cookie_perserve_param); |
| 5283 | cookie_preserve = (struct sctp_cookie_perserve_param *)(mtod(m, caddr_t) + chunk_len); |
| 5284 | cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE); |
| 5285 | cookie_preserve->ph.param_length = htons(parameter_len); |
| 5286 | cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req); |
| 5287 | stcb->asoc.cookie_preserve_req = 0; |
| 5288 | chunk_len += parameter_len; |
| 5289 | } |
| 5290 |
| 5291 | if (stcb->asoc.scope.ipv4_addr_legal || stcb->asoc.scope.ipv6_addr_legal) { |
| 5292 | uint8_t i; |
| 5293 |
| 5294 | if (padding_len > 0) { |
| 5295 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 5296 | chunk_len += padding_len; |
| 5297 | padding_len = 0; |
| 5298 | } |
| 5299 | parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); |
| 5300 | if (stcb->asoc.scope.ipv4_addr_legal) { |
| 5301 | parameter_len += (uint16_t)sizeof(uint16_t); |
| 5302 | } |
| 5303 | if (stcb->asoc.scope.ipv6_addr_legal) { |
| 5304 | parameter_len += (uint16_t)sizeof(uint16_t); |
| 5305 | } |
| 5306 | sup_addr = (struct sctp_supported_addr_param *)(mtod(m, caddr_t) + chunk_len); |
| 5307 | sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE); |
| 5308 | sup_addr->ph.param_length = htons(parameter_len); |
| 5309 | i = 0; |
| 5310 | if (stcb->asoc.scope.ipv4_addr_legal) { |
| 5311 | sup_addr->addr_type[i++] = htons(SCTP_IPV4_ADDRESS); |
| 5312 | } |
| 5313 | if (stcb->asoc.scope.ipv6_addr_legal) { |
| 5314 | sup_addr->addr_type[i++] = htons(SCTP_IPV6_ADDRESS); |
| 5315 | } |
| 5316 | padding_len = 4 - 2 * i; |
| 5317 | chunk_len += parameter_len; |
| 5318 | } |
| 5319 |
| 5320 | SCTP_BUF_LEN(m) = chunk_len; |
| 5321 | /* now the addresses */ |
| 5322 | /* To optimize this we could put the scoping stuff |
| 5323 | * into a structure and remove the individual uint8's from |
| 5324 | * the assoc structure. Then we could just sifa in the |
| 5325 | * address within the stcb. But for now this is a quick |
| 5326 | * hack to get the address stuff teased apart. |
| 5327 | */ |
| 5328 | m_last = sctp_add_addresses_to_i_ia(inp, stcb, &stcb->asoc.scope, |
| 5329 | m, cnt_inits_to, |
| 5330 | &padding_len, &chunk_len); |
| 5331 |
| 5332 | init->ch.chunk_length = htons(chunk_len); |
| 5333 | if (padding_len > 0) { |
| 5334 | if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) { |
| 5335 | sctp_m_freem(m); |
| 5336 | return; |
| 5337 | } |
| 5338 | } |
| 5339 | SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n"); |
| 5340 | if ((error = sctp_lowlevel_chunk_output(inp, stcb, net, |
| 5341 | (struct sockaddr *)&net->ro._l_addr, |
| 5342 | m, 0, NULL, 0, 0, 0, 0, |
| 5343 | inp->sctp_lport, stcb->rport, htonl(0), |
| 5344 | net->port, NULL, |
| 5345 | #if defined(__FreeBSD__) |
| 5346 | 0, 0, |
| 5347 | #endif |
| 5348 | so_locked))) { |
| 5349 | SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error); |
| 5350 | if (error == ENOBUFS) { |
| 5351 | stcb->asoc.ifp_had_enobuf = 1; |
| 5352 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 5353 | } |
| 5354 | } else { |
| 5355 | stcb->asoc.ifp_had_enobuf = 0; |
| 5356 | } |
| 5357 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 5358 | (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); |
| 5359 | } |
| 5360 |
| 5361 | struct mbuf * |
| 5362 | sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt, |
| 5363 | int param_offset, int *abort_processing, |
| 5364 | struct sctp_chunkhdr *cp, |
| 5365 | int *nat_friendly, |
| 5366 | int *cookie_found) |
| 5367 | { |
| 5368 | /* |
| 5369 | * Given a mbuf containing an INIT or INIT-ACK with the param_offset |
| 5370 | * being equal to the beginning of the params i.e. (iphlen + |
| 5371 | * sizeof(struct sctp_init_msg) parse through the parameters to the |
| 5372 | * end of the mbuf verifying that all parameters are known. |
| 5373 | * |
| 5374 | * For unknown parameters build and return a mbuf with |
| 5375 | * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop |
| 5376 | * processing this chunk stop, and set *abort_processing to 1. |
| 5377 | * |
| 5378 | * By having param_offset be pre-set to where parameters begin it is |
| 5379 | * hoped that this routine may be reused in the future by new |
| 5380 | * features. |
| 5381 | */ |
| 5382 | struct sctp_paramhdr *phdr, params; |
| 5383 |
| 5384 | struct mbuf *mat, *m_tmp, *op_err, *op_err_last; |
| 5385 | int at, limit, pad_needed; |
| 5386 | uint16_t ptype, plen, padded_size; |
| 5387 |
| 5388 | *abort_processing = 0; |
| 5389 | if (cookie_found != NULL) { |
| 5390 | *cookie_found = 0; |
| 5391 | } |
| 5392 | mat = in_initpkt; |
| 5393 | limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk); |
| 5394 | at = param_offset; |
| 5395 | op_err = NULL; |
| 5396 | op_err_last = NULL; |
| 5397 | pad_needed = 0; |
| 5398 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n"); |
| 5399 | phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params)); |
| 5400 | while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) { |
| 5401 | ptype = ntohs(phdr->param_type); |
| 5402 | plen = ntohs(phdr->param_length); |
| 5403 | if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) { |
| 5404 | /* wacked parameter */ |
| 5405 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen); |
| 5406 | goto invalid_size; |
| 5407 | } |
| 5408 | limit -= SCTP_SIZE32(plen); |
| 5409 | /*- |
| 5410 | * All parameters for all chunks that we know/understand are |
| 5411 | * listed here. We process them other places and make |
| 5412 | * appropriate stop actions per the upper bits. However this |
| 5413 | * is the generic routine processor's can call to get back |
| 5414 | * an operr.. to either incorporate (init-ack) or send. |
| 5415 | */ |
| 5416 | padded_size = SCTP_SIZE32(plen); |
| 5417 | switch (ptype) { |
| 5418 | /* Param's with variable size */ |
| 5419 | case SCTP_HEARTBEAT_INFO: |
| 5420 | case SCTP_UNRECOG_PARAM: |
| 5421 | case SCTP_ERROR_CAUSE_IND: |
| 5422 | /* ok skip fwd */ |
| 5423 | at += padded_size; |
| 5424 | break; |
| 5425 | case SCTP_STATE_COOKIE: |
| 5426 | if (cookie_found != NULL) { |
| 5427 | *cookie_found = 1; |
| 5428 | } |
| 5429 | at += padded_size; |
| 5430 | break; |
| 5431 | /* Param's with variable size within a range */ |
| 5432 | case SCTP_CHUNK_LIST: |
| 5433 | case SCTP_SUPPORTED_CHUNK_EXT: |
| 5434 | if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) { |
| 5435 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen); |
| 5436 | goto invalid_size; |
| 5437 | } |
| 5438 | at += padded_size; |
| 5439 | break; |
| 5440 | case SCTP_SUPPORTED_ADDRTYPE: |
| 5441 | if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) { |
| 5442 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen); |
| 5443 | goto invalid_size; |
| 5444 | } |
| 5445 | at += padded_size; |
| 5446 | break; |
| 5447 | case SCTP_RANDOM: |
| 5448 | if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) { |
| 5449 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen); |
| 5450 | goto invalid_size; |
| 5451 | } |
| 5452 | at += padded_size; |
| 5453 | break; |
| 5454 | case SCTP_SET_PRIM_ADDR: |
| 5455 | case SCTP_DEL_IP_ADDRESS: |
| 5456 | case SCTP_ADD_IP_ADDRESS: |
| 5457 | if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) && |
| 5458 | (padded_size != sizeof(struct sctp_asconf_addr_param))) { |
| 5459 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen); |
| 5460 | goto invalid_size; |
| 5461 | } |
| 5462 | at += padded_size; |
| 5463 | break; |
| 5464 | /* Param's with a fixed size */ |
| 5465 | case SCTP_IPV4_ADDRESS: |
| 5466 | if (padded_size != sizeof(struct sctp_ipv4addr_param)) { |
| 5467 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen); |
| 5468 | goto invalid_size; |
| 5469 | } |
| 5470 | at += padded_size; |
| 5471 | break; |
| 5472 | case SCTP_IPV6_ADDRESS: |
| 5473 | if (padded_size != sizeof(struct sctp_ipv6addr_param)) { |
| 5474 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen); |
| 5475 | goto invalid_size; |
| 5476 | } |
| 5477 | at += padded_size; |
| 5478 | break; |
| 5479 | case SCTP_COOKIE_PRESERVE: |
| 5480 | if (padded_size != sizeof(struct sctp_cookie_perserve_param)) { |
| 5481 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen); |
| 5482 | goto invalid_size; |
| 5483 | } |
| 5484 | at += padded_size; |
| 5485 | break; |
| 5486 | case SCTP_HAS_NAT_SUPPORT: |
| 5487 | *nat_friendly = 1; |
| 5488 | /* fall through */ |
| 5489 | case SCTP_PRSCTP_SUPPORTED: |
| 5490 | if (padded_size != sizeof(struct sctp_paramhdr)) { |
| 5491 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen); |
| 5492 | goto invalid_size; |
| 5493 | } |
| 5494 | at += padded_size; |
| 5495 | break; |
| 5496 | case SCTP_ECN_CAPABLE: |
| 5497 | if (padded_size != sizeof(struct sctp_paramhdr)) { |
| 5498 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen); |
| 5499 | goto invalid_size; |
| 5500 | } |
| 5501 | at += padded_size; |
| 5502 | break; |
| 5503 | case SCTP_ULP_ADAPTATION: |
| 5504 | if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) { |
| 5505 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen); |
| 5506 | goto invalid_size; |
| 5507 | } |
| 5508 | at += padded_size; |
| 5509 | break; |
| 5510 | case SCTP_SUCCESS_REPORT: |
| 5511 | if (padded_size != sizeof(struct sctp_asconf_paramhdr)) { |
| 5512 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen); |
| 5513 | goto invalid_size; |
| 5514 | } |
| 5515 | at += padded_size; |
| 5516 | break; |
| 5517 | case SCTP_HOSTNAME_ADDRESS: |
| 5518 | { |
| 5519 | /* Hostname parameters are deprecated. */ |
| 5520 | struct sctp_gen_error_cause *cause; |
| 5521 | int l_len; |
| 5522 |
| 5523 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n"); |
| 5524 | *abort_processing = 1; |
| 5525 | sctp_m_freem(op_err); |
| 5526 | op_err = NULL; |
| 5527 | op_err_last = NULL; |
| 5528 | #ifdef INET6 |
| 5529 | l_len = SCTP_MIN_OVERHEAD; |
| 5530 | #else |
| 5531 | l_len = SCTP_MIN_V4_OVERHEAD; |
| 5532 | #endif |
| 5533 | l_len += sizeof(struct sctp_chunkhdr); |
| 5534 | l_len += sizeof(struct sctp_gen_error_cause); |
| 5535 | op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA); |
| 5536 | if (op_err != NULL) { |
| 5537 | /* |
| 5538 | * Pre-reserve space for IP, SCTP, and |
| 5539 | * chunk header. |
| 5540 | */ |
| 5541 | #ifdef INET6 |
| 5542 | SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); |
| 5543 | #else |
| 5544 | SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); |
| 5545 | #endif |
| 5546 | SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); |
| 5547 | SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); |
| 5548 | SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause); |
| 5549 | cause = mtod(op_err, struct sctp_gen_error_cause *); |
| 5550 | cause->code = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR); |
| 5551 | cause->length = htons((uint16_t)(sizeof(struct sctp_gen_error_cause) + plen)); |
| 5552 | SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT); |
| 5553 | if (SCTP_BUF_NEXT(op_err) == NULL) { |
| 5554 | sctp_m_freem(op_err); |
| 5555 | op_err = NULL; |
| 5556 | op_err_last = NULL; |
| 5557 | } |
| 5558 | } |
| 5559 | return (op_err); |
| 5560 | break; |
| 5561 | } |
| 5562 | default: |
| 5563 | /* |
| 5564 | * we do not recognize the parameter figure out what |
| 5565 | * we do. |
| 5566 | */ |
| 5567 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype); |
| 5568 | if ((ptype & 0x4000) == 0x4000) { |
| 5569 | /* Report bit is set?? */ |
| 5570 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n"); |
| 5571 | if (op_err == NULL) { |
| 5572 | int l_len; |
| 5573 | /* Ok need to try to get an mbuf */ |
| 5574 | #ifdef INET6 |
| 5575 | l_len = SCTP_MIN_OVERHEAD; |
| 5576 | #else |
| 5577 | l_len = SCTP_MIN_V4_OVERHEAD; |
| 5578 | #endif |
| 5579 | l_len += sizeof(struct sctp_chunkhdr); |
| 5580 | l_len += sizeof(struct sctp_paramhdr); |
| 5581 | op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA); |
| 5582 | if (op_err) { |
| 5583 | SCTP_BUF_LEN(op_err) = 0; |
| 5584 | #ifdef INET6 |
| 5585 | SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); |
| 5586 | #else |
| 5587 | SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); |
| 5588 | #endif |
| 5589 | SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); |
| 5590 | SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); |
| 5591 | op_err_last = op_err; |
| 5592 | } |
| 5593 | } |
| 5594 | if (op_err != NULL) { |
| 5595 | /* If we have space */ |
| 5596 | struct sctp_paramhdr *param; |
| 5597 |
| 5598 | if (pad_needed > 0) { |
| 5599 | op_err_last = sctp_add_pad_tombuf(op_err_last, pad_needed); |
| 5600 | } |
| 5601 | if (op_err_last == NULL) { |
| 5602 | sctp_m_freem(op_err); |
| 5603 | op_err = NULL; |
| 5604 | op_err_last = NULL; |
| 5605 | goto more_processing; |
| 5606 | } |
| 5607 | if (M_TRAILINGSPACE(op_err_last) < (int)sizeof(struct sctp_paramhdr)) { |
| 5608 | m_tmp = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA); |
| 5609 | if (m_tmp == NULL) { |
| 5610 | sctp_m_freem(op_err); |
| 5611 | op_err = NULL; |
| 5612 | op_err_last = NULL; |
| 5613 | goto more_processing; |
| 5614 | } |
| 5615 | SCTP_BUF_LEN(m_tmp) = 0; |
| 5616 | SCTP_BUF_NEXT(m_tmp) = NULL; |
| 5617 | SCTP_BUF_NEXT(op_err_last) = m_tmp; |
| 5618 | op_err_last = m_tmp; |
| 5619 | } |
| 5620 | param = (struct sctp_paramhdr *)(mtod(op_err_last, caddr_t) + SCTP_BUF_LEN(op_err_last)); |
| 5621 | param->param_type = htons(SCTP_UNRECOG_PARAM); |
| 5622 | param->param_length = htons((uint16_t)sizeof(struct sctp_paramhdr) + plen); |
| 5623 | SCTP_BUF_LEN(op_err_last) += sizeof(struct sctp_paramhdr); |
| 5624 | SCTP_BUF_NEXT(op_err_last) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT); |
| 5625 | if (SCTP_BUF_NEXT(op_err_last) == NULL) { |
| 5626 | sctp_m_freem(op_err); |
| 5627 | op_err = NULL; |
| 5628 | op_err_last = NULL; |
| 5629 | goto more_processing; |
| 5630 | } else { |
| 5631 | while (SCTP_BUF_NEXT(op_err_last) != NULL) { |
| 5632 | op_err_last = SCTP_BUF_NEXT(op_err_last); |
| 5633 | } |
| 5634 | } |
| 5635 | if (plen % 4 != 0) { |
| 5636 | pad_needed = 4 - (plen % 4); |
| 5637 | } else { |
| 5638 | pad_needed = 0; |
| 5639 | } |
| 5640 | } |
| 5641 | } |
| 5642 | more_processing: |
| 5643 | if ((ptype & 0x8000) == 0x0000) { |
| 5644 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n"); |
| 5645 | return (op_err); |
| 5646 | } else { |
| 5647 | /* skip this chunk and continue processing */ |
| 5648 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n"); |
| 5649 | at += SCTP_SIZE32(plen); |
| 5650 | } |
| 5651 | break; |
| 5652 |
| 5653 | } |
| 5654 | phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params)); |
| 5655 | } |
| 5656 | return (op_err); |
| 5657 | invalid_size: |
| 5658 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n"); |
| 5659 | *abort_processing = 1; |
| 5660 | sctp_m_freem(op_err); |
| 5661 | op_err = NULL; |
| 5662 | op_err_last = NULL; |
| 5663 | if (phdr != NULL) { |
| 5664 | struct sctp_paramhdr *param; |
| 5665 | int l_len; |
| 5666 | #ifdef INET6 |
| 5667 | l_len = SCTP_MIN_OVERHEAD; |
| 5668 | #else |
| 5669 | l_len = SCTP_MIN_V4_OVERHEAD; |
| 5670 | #endif |
| 5671 | l_len += sizeof(struct sctp_chunkhdr); |
| 5672 | l_len += (2 * sizeof(struct sctp_paramhdr)); |
| 5673 | op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA); |
| 5674 | if (op_err) { |
| 5675 | SCTP_BUF_LEN(op_err) = 0; |
| 5676 | #ifdef INET6 |
| 5677 | SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); |
| 5678 | #else |
| 5679 | SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); |
| 5680 | #endif |
| 5681 | SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); |
| 5682 | SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); |
| 5683 | SCTP_BUF_LEN(op_err) = 2 * sizeof(struct sctp_paramhdr); |
| 5684 | param = mtod(op_err, struct sctp_paramhdr *); |
| 5685 | param->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION); |
| 5686 | param->param_length = htons(2 * sizeof(struct sctp_paramhdr)); |
| 5687 | param++; |
| 5688 | param->param_type = htons(ptype); |
| 5689 | param->param_length = htons(plen); |
| 5690 | } |
| 5691 | } |
| 5692 | return (op_err); |
| 5693 | } |
| 5694 |
| 5695 | static int |
| 5696 | sctp_are_there_new_addresses(struct sctp_association *asoc, |
| 5697 | struct mbuf *in_initpkt, int offset, struct sockaddr *src) |
| 5698 | { |
| 5699 | /* |
| 5700 | * Given a INIT packet, look through the packet to verify that there |
| 5701 | * are NO new addresses. As we go through the parameters add reports |
| 5702 | * of any un-understood parameters that require an error. Also we |
| 5703 | * must return (1) to drop the packet if we see a un-understood |
| 5704 | * parameter that tells us to drop the chunk. |
| 5705 | */ |
| 5706 | struct sockaddr *sa_touse; |
| 5707 | struct sockaddr *sa; |
| 5708 | struct sctp_paramhdr *phdr, params; |
| 5709 | uint16_t ptype, plen; |
| 5710 | uint8_t fnd; |
| 5711 | struct sctp_nets *net; |
| 5712 | int check_src; |
| 5713 | #ifdef INET |
| 5714 | struct sockaddr_in sin4, *sa4; |
| 5715 | #endif |
| 5716 | #ifdef INET6 |
| 5717 | struct sockaddr_in6 sin6, *sa6; |
| 5718 | #endif |
| 5719 | #if defined(__Userspace__) |
| 5720 | struct sockaddr_conn *sac; |
| 5721 | #endif |
| 5722 |
| 5723 | #ifdef INET |
| 5724 | memset(&sin4, 0, sizeof(sin4)); |
| 5725 | sin4.sin_family = AF_INET; |
| 5726 | #ifdef HAVE_SIN_LEN |
| 5727 | sin4.sin_len = sizeof(sin4); |
| 5728 | #endif |
| 5729 | #endif |
| 5730 | #ifdef INET6 |
| 5731 | memset(&sin6, 0, sizeof(sin6)); |
| 5732 | sin6.sin6_family = AF_INET6; |
| 5733 | #ifdef HAVE_SIN6_LEN |
| 5734 | sin6.sin6_len = sizeof(sin6); |
| 5735 | #endif |
| 5736 | #endif |
| 5737 | /* First what about the src address of the pkt ? */ |
| 5738 | check_src = 0; |
| 5739 | switch (src->sa_family) { |
| 5740 | #ifdef INET |
| 5741 | case AF_INET: |
| 5742 | if (asoc->scope.ipv4_addr_legal) { |
| 5743 | check_src = 1; |
| 5744 | } |
| 5745 | break; |
| 5746 | #endif |
| 5747 | #ifdef INET6 |
| 5748 | case AF_INET6: |
| 5749 | if (asoc->scope.ipv6_addr_legal) { |
| 5750 | check_src = 1; |
| 5751 | } |
| 5752 | break; |
| 5753 | #endif |
| 5754 | #if defined(__Userspace__) |
| 5755 | case AF_CONN: |
| 5756 | if (asoc->scope.conn_addr_legal) { |
| 5757 | check_src = 1; |
| 5758 | } |
| 5759 | break; |
| 5760 | #endif |
| 5761 | default: |
| 5762 | /* TSNH */ |
| 5763 | break; |
| 5764 | } |
| 5765 | if (check_src) { |
| 5766 | fnd = 0; |
| 5767 | TAILQ_FOREACH(net, &asoc->nets, sctp_next) { |
| 5768 | sa = (struct sockaddr *)&net->ro._l_addr; |
| 5769 | if (sa->sa_family == src->sa_family) { |
| 5770 | #ifdef INET |
| 5771 | if (sa->sa_family == AF_INET) { |
| 5772 | struct sockaddr_in *src4; |
| 5773 |
| 5774 | sa4 = (struct sockaddr_in *)sa; |
| 5775 | src4 = (struct sockaddr_in *)src; |
| 5776 | if (sa4->sin_addr.s_addr == src4->sin_addr.s_addr) { |
| 5777 | fnd = 1; |
| 5778 | break; |
| 5779 | } |
| 5780 | } |
| 5781 | #endif |
| 5782 | #ifdef INET6 |
| 5783 | if (sa->sa_family == AF_INET6) { |
| 5784 | struct sockaddr_in6 *src6; |
| 5785 |
| 5786 | sa6 = (struct sockaddr_in6 *)sa; |
| 5787 | src6 = (struct sockaddr_in6 *)src; |
| 5788 | if (SCTP6_ARE_ADDR_EQUAL(sa6, src6)) { |
| 5789 | fnd = 1; |
| 5790 | break; |
| 5791 | } |
| 5792 | } |
| 5793 | #endif |
| 5794 | #if defined(__Userspace__) |
| 5795 | if (sa->sa_family == AF_CONN) { |
| 5796 | struct sockaddr_conn *srcc; |
| 5797 |
| 5798 | sac = (struct sockaddr_conn *)sa; |
| 5799 | srcc = (struct sockaddr_conn *)src; |
| 5800 | if (sac->sconn_addr == srcc->sconn_addr) { |
| 5801 | fnd = 1; |
| 5802 | break; |
| 5803 | } |
| 5804 | } |
| 5805 | #endif |
| 5806 | } |
| 5807 | } |
| 5808 | if (fnd == 0) { |
| 5809 | /* New address added! no need to look further. */ |
| 5810 | return (1); |
| 5811 | } |
| 5812 | } |
| 5813 | /* Ok so far lets munge through the rest of the packet */ |
| 5814 | offset += sizeof(struct sctp_init_chunk); |
| 5815 | phdr = sctp_get_next_param(in_initpkt, offset, ¶ms, sizeof(params)); |
| 5816 | while (phdr) { |
| 5817 | sa_touse = NULL; |
| 5818 | ptype = ntohs(phdr->param_type); |
| 5819 | plen = ntohs(phdr->param_length); |
| 5820 | switch (ptype) { |
| 5821 | #ifdef INET |
| 5822 | case SCTP_IPV4_ADDRESS: |
| 5823 | { |
| 5824 | struct sctp_ipv4addr_param *p4, p4_buf; |
| 5825 |
| 5826 | if (plen != sizeof(struct sctp_ipv4addr_param)) { |
| 5827 | return (1); |
| 5828 | } |
| 5829 | phdr = sctp_get_next_param(in_initpkt, offset, |
| 5830 | (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf)); |
| 5831 | if (phdr == NULL) { |
| 5832 | return (1); |
| 5833 | } |
| 5834 | if (asoc->scope.ipv4_addr_legal) { |
| 5835 | p4 = (struct sctp_ipv4addr_param *)phdr; |
| 5836 | sin4.sin_addr.s_addr = p4->addr; |
| 5837 | sa_touse = (struct sockaddr *)&sin4; |
| 5838 | } |
| 5839 | break; |
| 5840 | } |
| 5841 | #endif |
| 5842 | #ifdef INET6 |
| 5843 | case SCTP_IPV6_ADDRESS: |
| 5844 | { |
| 5845 | struct sctp_ipv6addr_param *p6, p6_buf; |
| 5846 |
| 5847 | if (plen != sizeof(struct sctp_ipv6addr_param)) { |
| 5848 | return (1); |
| 5849 | } |
| 5850 | phdr = sctp_get_next_param(in_initpkt, offset, |
| 5851 | (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf)); |
| 5852 | if (phdr == NULL) { |
| 5853 | return (1); |
| 5854 | } |
| 5855 | if (asoc->scope.ipv6_addr_legal) { |
| 5856 | p6 = (struct sctp_ipv6addr_param *)phdr; |
| 5857 | memcpy((caddr_t)&sin6.sin6_addr, p6->addr, |
| 5858 | sizeof(p6->addr)); |
| 5859 | sa_touse = (struct sockaddr *)&sin6; |
| 5860 | } |
| 5861 | break; |
| 5862 | } |
| 5863 | #endif |
| 5864 | default: |
| 5865 | sa_touse = NULL; |
| 5866 | break; |
| 5867 | } |
| 5868 | if (sa_touse) { |
| 5869 | /* ok, sa_touse points to one to check */ |
| 5870 | fnd = 0; |
| 5871 | TAILQ_FOREACH(net, &asoc->nets, sctp_next) { |
| 5872 | sa = (struct sockaddr *)&net->ro._l_addr; |
| 5873 | if (sa->sa_family != sa_touse->sa_family) { |
| 5874 | continue; |
| 5875 | } |
| 5876 | #ifdef INET |
| 5877 | if (sa->sa_family == AF_INET) { |
| 5878 | sa4 = (struct sockaddr_in *)sa; |
| 5879 | if (sa4->sin_addr.s_addr == |
| 5880 | sin4.sin_addr.s_addr) { |
| 5881 | fnd = 1; |
| 5882 | break; |
| 5883 | } |
| 5884 | } |
| 5885 | #endif |
| 5886 | #ifdef INET6 |
| 5887 | if (sa->sa_family == AF_INET6) { |
| 5888 | sa6 = (struct sockaddr_in6 *)sa; |
| 5889 | if (SCTP6_ARE_ADDR_EQUAL( |
| 5890 | sa6, &sin6)) { |
| 5891 | fnd = 1; |
| 5892 | break; |
| 5893 | } |
| 5894 | } |
| 5895 | #endif |
| 5896 | } |
| 5897 | if (!fnd) { |
| 5898 | /* New addr added! no need to look further */ |
| 5899 | return (1); |
| 5900 | } |
| 5901 | } |
| 5902 | offset += SCTP_SIZE32(plen); |
| 5903 | phdr = sctp_get_next_param(in_initpkt, offset, ¶ms, sizeof(params)); |
| 5904 | } |
| 5905 | return (0); |
| 5906 | } |
| 5907 |
| 5908 | /* |
| 5909 | * Given a MBUF chain that was sent into us containing an INIT. Build a |
| 5910 | * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done |
| 5911 | * a pullup to include IPv6/4header, SCTP header and initial part of INIT |
| 5912 | * message (i.e. the struct sctp_init_msg). |
| 5913 | */ |
| 5914 | void |
| 5915 | sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb, |
| 5916 | struct sctp_nets *src_net, struct mbuf *init_pkt, |
| 5917 | int iphlen, int offset, |
| 5918 | struct sockaddr *src, struct sockaddr *dst, |
| 5919 | struct sctphdr *sh, struct sctp_init_chunk *init_chk, |
| 5920 | #if defined(__FreeBSD__) |
| 5921 | uint8_t mflowtype, uint32_t mflowid, |
| 5922 | #endif |
| 5923 | uint32_t vrf_id, uint16_t port) |
| 5924 | { |
| 5925 | struct sctp_association *asoc; |
| 5926 | struct mbuf *m, *m_tmp, *m_last, *m_cookie, *op_err; |
| 5927 | struct sctp_init_ack_chunk *initack; |
| 5928 | struct sctp_adaptation_layer_indication *ali; |
| 5929 | struct sctp_supported_chunk_types_param *pr_supported; |
| 5930 | struct sctp_paramhdr *ph; |
| 5931 | union sctp_sockstore *over_addr; |
| 5932 | struct sctp_scoping scp; |
| 5933 | struct timeval now; |
| 5934 | #ifdef INET |
| 5935 | struct sockaddr_in *dst4 = (struct sockaddr_in *)dst; |
| 5936 | struct sockaddr_in *src4 = (struct sockaddr_in *)src; |
| 5937 | struct sockaddr_in *sin; |
| 5938 | #endif |
| 5939 | #ifdef INET6 |
| 5940 | struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst; |
| 5941 | struct sockaddr_in6 *src6 = (struct sockaddr_in6 *)src; |
| 5942 | struct sockaddr_in6 *sin6; |
| 5943 | #endif |
| 5944 | #if defined(__Userspace__) |
| 5945 | struct sockaddr_conn *dstconn = (struct sockaddr_conn *)dst; |
| 5946 | struct sockaddr_conn *srcconn = (struct sockaddr_conn *)src; |
| 5947 | struct sockaddr_conn *sconn; |
| 5948 | #endif |
| 5949 | struct sockaddr *to; |
| 5950 | struct sctp_state_cookie stc; |
| 5951 | struct sctp_nets *net = NULL; |
| 5952 | uint8_t *signature = NULL; |
| 5953 | int cnt_inits_to = 0; |
| 5954 | uint16_t his_limit, i_want; |
| 5955 | int abort_flag; |
| 5956 | int nat_friendly = 0; |
| 5957 | int error; |
| 5958 | struct socket *so; |
| 5959 | uint16_t num_ext, chunk_len, padding_len, parameter_len; |
| 5960 |
| 5961 | if (stcb) { |
| 5962 | asoc = &stcb->asoc; |
| 5963 | } else { |
| 5964 | asoc = NULL; |
| 5965 | } |
| 5966 | if ((asoc != NULL) && |
| 5967 | (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT)) { |
| 5968 | if (sctp_are_there_new_addresses(asoc, init_pkt, offset, src)) { |
| 5969 | /* |
| 5970 | * new addresses, out of here in non-cookie-wait states |
| 5971 | * |
| 5972 | * Send an ABORT, without the new address error cause. |
| 5973 | * This looks no different than if no listener |
| 5974 | * was present. |
| 5975 | */ |
| 5976 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 5977 | "Address added"); |
| 5978 | sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err, |
| 5979 | #if defined(__FreeBSD__) |
| 5980 | mflowtype, mflowid, inp->fibnum, |
| 5981 | #endif |
| 5982 | vrf_id, port); |
| 5983 | return; |
| 5984 | } |
| 5985 | if (src_net != NULL && (src_net->port != port)) { |
| 5986 | /* |
| 5987 | * change of remote encapsulation port, out of here in |
| 5988 | * non-cookie-wait states |
| 5989 | * |
| 5990 | * Send an ABORT, without an specific error cause. |
| 5991 | * This looks no different than if no listener |
| 5992 | * was present. |
| 5993 | */ |
| 5994 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 5995 | "Remote encapsulation port changed"); |
| 5996 | sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err, |
| 5997 | #if defined(__FreeBSD__) |
| 5998 | mflowtype, mflowid, inp->fibnum, |
| 5999 | #endif |
| 6000 | vrf_id, port); |
| 6001 | return; |
| 6002 | } |
| 6003 | } |
| 6004 | abort_flag = 0; |
| 6005 | op_err = sctp_arethere_unrecognized_parameters(init_pkt, |
| 6006 | (offset + sizeof(struct sctp_init_chunk)), |
| 6007 | &abort_flag, |
| 6008 | (struct sctp_chunkhdr *)init_chk, |
| 6009 | &nat_friendly, NULL); |
| 6010 | if (abort_flag) { |
| 6011 | do_a_abort: |
| 6012 | if (op_err == NULL) { |
| 6013 | char msg[SCTP_DIAG_INFO_LEN]; |
| 6014 |
| 6015 | snprintf(msg, sizeof(msg), "%s:%d at %s", __FILE__, __LINE__, __func__); |
| 6016 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 6017 | msg); |
| 6018 | } |
| 6019 | sctp_send_abort(init_pkt, iphlen, src, dst, sh, |
| 6020 | init_chk->init.initiate_tag, op_err, |
| 6021 | #if defined(__FreeBSD__) |
| 6022 | mflowtype, mflowid, inp->fibnum, |
| 6023 | #endif |
| 6024 | vrf_id, port); |
| 6025 | return; |
| 6026 | } |
| 6027 | m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); |
| 6028 | if (m == NULL) { |
| 6029 | /* No memory, INIT timer will re-attempt. */ |
| 6030 | sctp_m_freem(op_err); |
| 6031 | return; |
| 6032 | } |
| 6033 | chunk_len = (uint16_t)sizeof(struct sctp_init_ack_chunk); |
| 6034 | padding_len = 0; |
| 6035 |
| 6036 | /* |
| 6037 | * We might not overwrite the identification[] completely and on |
| 6038 | * some platforms time_entered will contain some padding. |
| 6039 | * Therefore zero out the cookie to avoid putting |
| 6040 | * uninitialized memory on the wire. |
| 6041 | */ |
| 6042 | memset(&stc, 0, sizeof(struct sctp_state_cookie)); |
| 6043 |
| 6044 | /* the time I built cookie */ |
| 6045 | (void)SCTP_GETTIME_TIMEVAL(&now); |
| 6046 | stc.time_entered.tv_sec = now.tv_sec; |
| 6047 | stc.time_entered.tv_usec = now.tv_usec; |
| 6048 |
| 6049 | /* populate any tie tags */ |
| 6050 | if (asoc != NULL) { |
| 6051 | /* unlock before tag selections */ |
| 6052 | stc.tie_tag_my_vtag = asoc->my_vtag_nonce; |
| 6053 | stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce; |
| 6054 | stc.cookie_life = asoc->cookie_life; |
| 6055 | net = asoc->primary_destination; |
| 6056 | } else { |
| 6057 | stc.tie_tag_my_vtag = 0; |
| 6058 | stc.tie_tag_peer_vtag = 0; |
| 6059 | /* life I will award this cookie */ |
| 6060 | stc.cookie_life = inp->sctp_ep.def_cookie_life; |
| 6061 | } |
| 6062 |
| 6063 | /* copy in the ports for later check */ |
| 6064 | stc.myport = sh->dest_port; |
| 6065 | stc.peerport = sh->src_port; |
| 6066 |
| 6067 | /* |
| 6068 | * If we wanted to honor cookie life extensions, we would add to |
| 6069 | * stc.cookie_life. For now we should NOT honor any extension |
| 6070 | */ |
| 6071 | stc.site_scope = stc.local_scope = stc.loopback_scope = 0; |
| 6072 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { |
| 6073 | stc.ipv6_addr_legal = 1; |
| 6074 | if (SCTP_IPV6_V6ONLY(inp)) { |
| 6075 | stc.ipv4_addr_legal = 0; |
| 6076 | } else { |
| 6077 | stc.ipv4_addr_legal = 1; |
| 6078 | } |
| 6079 | #if defined(__Userspace__) |
| 6080 | stc.conn_addr_legal = 0; |
| 6081 | #endif |
| 6082 | } else { |
| 6083 | stc.ipv6_addr_legal = 0; |
| 6084 | #if defined(__Userspace__) |
| 6085 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) { |
| 6086 | stc.conn_addr_legal = 1; |
| 6087 | stc.ipv4_addr_legal = 0; |
| 6088 | } else { |
| 6089 | stc.conn_addr_legal = 0; |
| 6090 | stc.ipv4_addr_legal = 1; |
| 6091 | } |
| 6092 | #else |
| 6093 | stc.ipv4_addr_legal = 1; |
| 6094 | #endif |
| 6095 | } |
| 6096 | stc.ipv4_scope = 0; |
| 6097 | if (net == NULL) { |
| 6098 | to = src; |
| 6099 | switch (dst->sa_family) { |
| 6100 | #ifdef INET |
| 6101 | case AF_INET: |
| 6102 | { |
| 6103 | /* lookup address */ |
| 6104 | stc.address[0] = src4->sin_addr.s_addr; |
| 6105 | stc.address[1] = 0; |
| 6106 | stc.address[2] = 0; |
| 6107 | stc.address[3] = 0; |
| 6108 | stc.addr_type = SCTP_IPV4_ADDRESS; |
| 6109 | /* local from address */ |
| 6110 | stc.laddress[0] = dst4->sin_addr.s_addr; |
| 6111 | stc.laddress[1] = 0; |
| 6112 | stc.laddress[2] = 0; |
| 6113 | stc.laddress[3] = 0; |
| 6114 | stc.laddr_type = SCTP_IPV4_ADDRESS; |
| 6115 | /* scope_id is only for v6 */ |
| 6116 | stc.scope_id = 0; |
| 6117 | if ((IN4_ISPRIVATE_ADDRESS(&src4->sin_addr)) || |
| 6118 | (IN4_ISPRIVATE_ADDRESS(&dst4->sin_addr))){ |
| 6119 | stc.ipv4_scope = 1; |
| 6120 | } |
| 6121 | /* Must use the address in this case */ |
| 6122 | if (sctp_is_address_on_local_host(src, vrf_id)) { |
| 6123 | stc.loopback_scope = 1; |
| 6124 | stc.ipv4_scope = 1; |
| 6125 | stc.site_scope = 1; |
| 6126 | stc.local_scope = 0; |
| 6127 | } |
| 6128 | break; |
| 6129 | } |
| 6130 | #endif |
| 6131 | #ifdef INET6 |
| 6132 | case AF_INET6: |
| 6133 | { |
| 6134 | stc.addr_type = SCTP_IPV6_ADDRESS; |
| 6135 | memcpy(&stc.address, &src6->sin6_addr, sizeof(struct in6_addr)); |
| 6136 | #if defined(__FreeBSD__) && (((__FreeBSD_version < 900000) && (__FreeBSD_version >= 804000)) || (__FreeBSD_version > 900000)) |
| 6137 | stc.scope_id = ntohs(in6_getscope(&src6->sin6_addr)); |
| 6138 | #else |
| 6139 | stc.scope_id = 0; |
| 6140 | #endif |
| 6141 | if (sctp_is_address_on_local_host(src, vrf_id)) { |
| 6142 | stc.loopback_scope = 1; |
| 6143 | stc.local_scope = 0; |
| 6144 | stc.site_scope = 1; |
| 6145 | stc.ipv4_scope = 1; |
| 6146 | } else if (IN6_IS_ADDR_LINKLOCAL(&src6->sin6_addr) || |
| 6147 | IN6_IS_ADDR_LINKLOCAL(&dst6->sin6_addr)) { |
| 6148 | /* |
| 6149 | * If the new destination or source is a |
| 6150 | * LINK_LOCAL we must have common both site and |
| 6151 | * local scope. Don't set local scope though |
| 6152 | * since we must depend on the source to be |
| 6153 | * added implicitly. We cannot assure just |
| 6154 | * because we share one link that all links are |
| 6155 | * common. |
| 6156 | */ |
| 6157 | #if defined(__APPLE__) |
| 6158 | /* Mac OS X currently doesn't have in6_getscope() */ |
| 6159 | stc.scope_id = src6->sin6_addr.s6_addr16[1]; |
| 6160 | #endif |
| 6161 | stc.local_scope = 0; |
| 6162 | stc.site_scope = 1; |
| 6163 | stc.ipv4_scope = 1; |
| 6164 | /* |
| 6165 | * we start counting for the private address |
| 6166 | * stuff at 1. since the link local we |
| 6167 | * source from won't show up in our scoped |
| 6168 | * count. |
| 6169 | */ |
| 6170 | cnt_inits_to = 1; |
| 6171 | /* pull out the scope_id from incoming pkt */ |
| 6172 | } else if (IN6_IS_ADDR_SITELOCAL(&src6->sin6_addr) || |
| 6173 | IN6_IS_ADDR_SITELOCAL(&dst6->sin6_addr)) { |
| 6174 | /* |
| 6175 | * If the new destination or source is |
| 6176 | * SITE_LOCAL then we must have site scope in |
| 6177 | * common. |
| 6178 | */ |
| 6179 | stc.site_scope = 1; |
| 6180 | } |
| 6181 | memcpy(&stc.laddress, &dst6->sin6_addr, sizeof(struct in6_addr)); |
| 6182 | stc.laddr_type = SCTP_IPV6_ADDRESS; |
| 6183 | break; |
| 6184 | } |
| 6185 | #endif |
| 6186 | #if defined(__Userspace__) |
| 6187 | case AF_CONN: |
| 6188 | { |
| 6189 | /* lookup address */ |
| 6190 | stc.address[0] = 0; |
| 6191 | stc.address[1] = 0; |
| 6192 | stc.address[2] = 0; |
| 6193 | stc.address[3] = 0; |
| 6194 | memcpy(&stc.address, &srcconn->sconn_addr, sizeof(void *)); |
| 6195 | stc.addr_type = SCTP_CONN_ADDRESS; |
| 6196 | /* local from address */ |
| 6197 | stc.laddress[0] = 0; |
| 6198 | stc.laddress[1] = 0; |
| 6199 | stc.laddress[2] = 0; |
| 6200 | stc.laddress[3] = 0; |
| 6201 | memcpy(&stc.laddress, &dstconn->sconn_addr, sizeof(void *)); |
| 6202 | stc.laddr_type = SCTP_CONN_ADDRESS; |
| 6203 | /* scope_id is only for v6 */ |
| 6204 | stc.scope_id = 0; |
| 6205 | break; |
| 6206 | } |
| 6207 | #endif |
| 6208 | default: |
| 6209 | /* TSNH */ |
| 6210 | goto do_a_abort; |
| 6211 | break; |
| 6212 | } |
| 6213 | } else { |
| 6214 | /* set the scope per the existing tcb */ |
| 6215 |
| 6216 | #ifdef INET6 |
| 6217 | struct sctp_nets *lnet; |
| 6218 | #endif |
| 6219 |
| 6220 | stc.loopback_scope = asoc->scope.loopback_scope; |
| 6221 | stc.ipv4_scope = asoc->scope.ipv4_local_scope; |
| 6222 | stc.site_scope = asoc->scope.site_scope; |
| 6223 | stc.local_scope = asoc->scope.local_scope; |
| 6224 | #ifdef INET6 |
| 6225 | /* Why do we not consider IPv4 LL addresses? */ |
| 6226 | TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { |
| 6227 | if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) { |
| 6228 | if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) { |
| 6229 | /* |
| 6230 | * if we have a LL address, start |
| 6231 | * counting at 1. |
| 6232 | */ |
| 6233 | cnt_inits_to = 1; |
| 6234 | } |
| 6235 | } |
| 6236 | } |
| 6237 | #endif |
| 6238 | /* use the net pointer */ |
| 6239 | to = (struct sockaddr *)&net->ro._l_addr; |
| 6240 | switch (to->sa_family) { |
| 6241 | #ifdef INET |
| 6242 | case AF_INET: |
| 6243 | sin = (struct sockaddr_in *)to; |
| 6244 | stc.address[0] = sin->sin_addr.s_addr; |
| 6245 | stc.address[1] = 0; |
| 6246 | stc.address[2] = 0; |
| 6247 | stc.address[3] = 0; |
| 6248 | stc.addr_type = SCTP_IPV4_ADDRESS; |
| 6249 | if (net->src_addr_selected == 0) { |
| 6250 | /* |
| 6251 | * strange case here, the INIT should have |
| 6252 | * did the selection. |
| 6253 | */ |
| 6254 | net->ro._s_addr = sctp_source_address_selection(inp, |
| 6255 | stcb, (sctp_route_t *)&net->ro, |
| 6256 | net, 0, vrf_id); |
| 6257 | if (net->ro._s_addr == NULL) { |
| 6258 | sctp_m_freem(op_err); |
| 6259 | sctp_m_freem(m); |
| 6260 | return; |
| 6261 | } |
| 6262 |
| 6263 | net->src_addr_selected = 1; |
| 6264 |
| 6265 | } |
| 6266 | stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr; |
| 6267 | stc.laddress[1] = 0; |
| 6268 | stc.laddress[2] = 0; |
| 6269 | stc.laddress[3] = 0; |
| 6270 | stc.laddr_type = SCTP_IPV4_ADDRESS; |
| 6271 | /* scope_id is only for v6 */ |
| 6272 | stc.scope_id = 0; |
| 6273 | break; |
| 6274 | #endif |
| 6275 | #ifdef INET6 |
| 6276 | case AF_INET6: |
| 6277 | sin6 = (struct sockaddr_in6 *)to; |
| 6278 | memcpy(&stc.address, &sin6->sin6_addr, |
| 6279 | sizeof(struct in6_addr)); |
| 6280 | stc.addr_type = SCTP_IPV6_ADDRESS; |
| 6281 | stc.scope_id = sin6->sin6_scope_id; |
| 6282 | if (net->src_addr_selected == 0) { |
| 6283 | /* |
| 6284 | * strange case here, the INIT should have |
| 6285 | * done the selection. |
| 6286 | */ |
| 6287 | net->ro._s_addr = sctp_source_address_selection(inp, |
| 6288 | stcb, (sctp_route_t *)&net->ro, |
| 6289 | net, 0, vrf_id); |
| 6290 | if (net->ro._s_addr == NULL) { |
| 6291 | sctp_m_freem(op_err); |
| 6292 | sctp_m_freem(m); |
| 6293 | return; |
| 6294 | } |
| 6295 |
| 6296 | net->src_addr_selected = 1; |
| 6297 | } |
| 6298 | memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr, |
| 6299 | sizeof(struct in6_addr)); |
| 6300 | stc.laddr_type = SCTP_IPV6_ADDRESS; |
| 6301 | break; |
| 6302 | #endif |
| 6303 | #if defined(__Userspace__) |
| 6304 | case AF_CONN: |
| 6305 | sconn = (struct sockaddr_conn *)to; |
| 6306 | stc.address[0] = 0; |
| 6307 | stc.address[1] = 0; |
| 6308 | stc.address[2] = 0; |
| 6309 | stc.address[3] = 0; |
| 6310 | memcpy(&stc.address, &sconn->sconn_addr, sizeof(void *)); |
| 6311 | stc.addr_type = SCTP_CONN_ADDRESS; |
| 6312 | stc.laddress[0] = 0; |
| 6313 | stc.laddress[1] = 0; |
| 6314 | stc.laddress[2] = 0; |
| 6315 | stc.laddress[3] = 0; |
| 6316 | memcpy(&stc.laddress, &sconn->sconn_addr, sizeof(void *)); |
| 6317 | stc.laddr_type = SCTP_CONN_ADDRESS; |
| 6318 | stc.scope_id = 0; |
| 6319 | break; |
| 6320 | #endif |
| 6321 | } |
| 6322 | } |
| 6323 | /* Now lets put the SCTP header in place */ |
| 6324 | initack = mtod(m, struct sctp_init_ack_chunk *); |
| 6325 | /* Save it off for quick ref */ |
| 6326 | stc.peers_vtag = ntohl(init_chk->init.initiate_tag); |
| 6327 | /* who are we */ |
| 6328 | memcpy(stc.identification, SCTP_VERSION_STRING, |
| 6329 | min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification))); |
| 6330 | memset(stc.reserved, 0, SCTP_RESERVE_SPACE); |
| 6331 | /* now the chunk header */ |
| 6332 | initack->ch.chunk_type = SCTP_INITIATION_ACK; |
| 6333 | initack->ch.chunk_flags = 0; |
| 6334 | /* fill in later from mbuf we build */ |
| 6335 | initack->ch.chunk_length = 0; |
| 6336 | /* place in my tag */ |
| 6337 | if ((asoc != NULL) && |
| 6338 | ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) || |
| 6339 | (SCTP_GET_STATE(stcb) == SCTP_STATE_INUSE) || |
| 6340 | (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED))) { |
| 6341 | /* re-use the v-tags and init-seq here */ |
| 6342 | initack->init.initiate_tag = htonl(asoc->my_vtag); |
| 6343 | initack->init.initial_tsn = htonl(asoc->init_seq_number); |
| 6344 | } else { |
| 6345 | uint32_t vtag, itsn; |
| 6346 |
| 6347 | if (asoc) { |
| 6348 | atomic_add_int(&asoc->refcnt, 1); |
| 6349 | SCTP_TCB_UNLOCK(stcb); |
| 6350 | new_tag: |
| 6351 | vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1); |
| 6352 | if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) { |
| 6353 | /* Got a duplicate vtag on some guy behind a nat |
| 6354 | * make sure we don't use it. |
| 6355 | */ |
| 6356 | goto new_tag; |
| 6357 | } |
| 6358 | initack->init.initiate_tag = htonl(vtag); |
| 6359 | /* get a TSN to use too */ |
| 6360 | itsn = sctp_select_initial_TSN(&inp->sctp_ep); |
| 6361 | initack->init.initial_tsn = htonl(itsn); |
| 6362 | SCTP_TCB_LOCK(stcb); |
| 6363 | atomic_add_int(&asoc->refcnt, -1); |
| 6364 | } else { |
| 6365 | SCTP_INP_INCR_REF(inp); |
| 6366 | SCTP_INP_RUNLOCK(inp); |
| 6367 | vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1); |
| 6368 | initack->init.initiate_tag = htonl(vtag); |
| 6369 | /* get a TSN to use too */ |
| 6370 | initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep)); |
| 6371 | SCTP_INP_RLOCK(inp); |
| 6372 | SCTP_INP_DECR_REF(inp); |
| 6373 | } |
| 6374 | } |
| 6375 | /* save away my tag to */ |
| 6376 | stc.my_vtag = initack->init.initiate_tag; |
| 6377 |
| 6378 | /* set up some of the credits. */ |
| 6379 | so = inp->sctp_socket; |
| 6380 | if (so == NULL) { |
| 6381 | /* memory problem */ |
| 6382 | sctp_m_freem(op_err); |
| 6383 | sctp_m_freem(m); |
| 6384 | return; |
| 6385 | } else { |
| 6386 | initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND)); |
| 6387 | } |
| 6388 | /* set what I want */ |
| 6389 | his_limit = ntohs(init_chk->init.num_inbound_streams); |
| 6390 | /* choose what I want */ |
| 6391 | if (asoc != NULL) { |
| 6392 | if (asoc->streamoutcnt > asoc->pre_open_streams) { |
| 6393 | i_want = asoc->streamoutcnt; |
| 6394 | } else { |
| 6395 | i_want = asoc->pre_open_streams; |
| 6396 | } |
| 6397 | } else { |
| 6398 | i_want = inp->sctp_ep.pre_open_stream_count; |
| 6399 | } |
| 6400 | if (his_limit < i_want) { |
| 6401 | /* I Want more :< */ |
| 6402 | initack->init.num_outbound_streams = init_chk->init.num_inbound_streams; |
| 6403 | } else { |
| 6404 | /* I can have what I want :> */ |
| 6405 | initack->init.num_outbound_streams = htons(i_want); |
| 6406 | } |
| 6407 | /* tell him his limit. */ |
| 6408 | initack->init.num_inbound_streams = |
| 6409 | htons(inp->sctp_ep.max_open_streams_intome); |
| 6410 |
| 6411 | /* adaptation layer indication parameter */ |
| 6412 | if (inp->sctp_ep.adaptation_layer_indicator_provided) { |
| 6413 | parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication); |
| 6414 | ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t) + chunk_len); |
| 6415 | ali->ph.param_type = htons(SCTP_ULP_ADAPTATION); |
| 6416 | ali->ph.param_length = htons(parameter_len); |
| 6417 | ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator); |
| 6418 | chunk_len += parameter_len; |
| 6419 | } |
| 6420 |
| 6421 | /* ECN parameter */ |
| 6422 | if (((asoc != NULL) && (asoc->ecn_supported == 1)) || |
| 6423 | ((asoc == NULL) && (inp->ecn_supported == 1))) { |
| 6424 | parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); |
| 6425 | ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len); |
| 6426 | ph->param_type = htons(SCTP_ECN_CAPABLE); |
| 6427 | ph->param_length = htons(parameter_len); |
| 6428 | chunk_len += parameter_len; |
| 6429 | } |
| 6430 |
| 6431 | /* PR-SCTP supported parameter */ |
| 6432 | if (((asoc != NULL) && (asoc->prsctp_supported == 1)) || |
| 6433 | ((asoc == NULL) && (inp->prsctp_supported == 1))) { |
| 6434 | parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); |
| 6435 | ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len); |
| 6436 | ph->param_type = htons(SCTP_PRSCTP_SUPPORTED); |
| 6437 | ph->param_length = htons(parameter_len); |
| 6438 | chunk_len += parameter_len; |
| 6439 | } |
| 6440 |
| 6441 | /* Add NAT friendly parameter */ |
| 6442 | if (nat_friendly) { |
| 6443 | parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); |
| 6444 | ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len); |
| 6445 | ph->param_type = htons(SCTP_HAS_NAT_SUPPORT); |
| 6446 | ph->param_length = htons(parameter_len); |
| 6447 | chunk_len += parameter_len; |
| 6448 | } |
| 6449 |
| 6450 | /* And now tell the peer which extensions we support */ |
| 6451 | num_ext = 0; |
| 6452 | pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t) + chunk_len); |
| 6453 | if (((asoc != NULL) && (asoc->prsctp_supported == 1)) || |
| 6454 | ((asoc == NULL) && (inp->prsctp_supported == 1))) { |
| 6455 | pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN; |
| 6456 | if (((asoc != NULL) && (asoc->idata_supported == 1)) || |
| 6457 | ((asoc == NULL) && (inp->idata_supported == 1))) { |
| 6458 | pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN; |
| 6459 | } |
| 6460 | } |
| 6461 | if (((asoc != NULL) && (asoc->auth_supported == 1)) || |
| 6462 | ((asoc == NULL) && (inp->auth_supported == 1))) { |
| 6463 | pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION; |
| 6464 | } |
| 6465 | if (((asoc != NULL) && (asoc->asconf_supported == 1)) || |
| 6466 | ((asoc == NULL) && (inp->asconf_supported == 1))) { |
| 6467 | pr_supported->chunk_types[num_ext++] = SCTP_ASCONF; |
| 6468 | pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK; |
| 6469 | } |
| 6470 | if (((asoc != NULL) && (asoc->reconfig_supported == 1)) || |
| 6471 | ((asoc == NULL) && (inp->reconfig_supported == 1))) { |
| 6472 | pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET; |
| 6473 | } |
| 6474 | if (((asoc != NULL) && (asoc->idata_supported == 1)) || |
| 6475 | ((asoc == NULL) && (inp->idata_supported == 1))) { |
| 6476 | pr_supported->chunk_types[num_ext++] = SCTP_IDATA; |
| 6477 | } |
| 6478 | if (((asoc != NULL) && (asoc->nrsack_supported == 1)) || |
| 6479 | ((asoc == NULL) && (inp->nrsack_supported == 1))) { |
| 6480 | pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK; |
| 6481 | } |
| 6482 | if (((asoc != NULL) && (asoc->pktdrop_supported == 1)) || |
| 6483 | ((asoc == NULL) && (inp->pktdrop_supported == 1))) { |
| 6484 | pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED; |
| 6485 | } |
| 6486 | if (num_ext > 0) { |
| 6487 | parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext; |
| 6488 | pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT); |
| 6489 | pr_supported->ph.param_length = htons(parameter_len); |
| 6490 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 6491 | chunk_len += parameter_len; |
| 6492 | } |
| 6493 |
| 6494 | /* add authentication parameters */ |
| 6495 | if (((asoc != NULL) && (asoc->auth_supported == 1)) || |
| 6496 | ((asoc == NULL) && (inp->auth_supported == 1))) { |
| 6497 | struct sctp_auth_random *randp; |
| 6498 | struct sctp_auth_hmac_algo *hmacs; |
| 6499 | struct sctp_auth_chunk_list *chunks; |
| 6500 |
| 6501 | if (padding_len > 0) { |
| 6502 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 6503 | chunk_len += padding_len; |
| 6504 | padding_len = 0; |
| 6505 | } |
| 6506 | /* generate and add RANDOM parameter */ |
| 6507 | randp = (struct sctp_auth_random *)(mtod(m, caddr_t) + chunk_len); |
| 6508 | parameter_len = (uint16_t)sizeof(struct sctp_auth_random) + |
| 6509 | SCTP_AUTH_RANDOM_SIZE_DEFAULT; |
| 6510 | randp->ph.param_type = htons(SCTP_RANDOM); |
| 6511 | randp->ph.param_length = htons(parameter_len); |
| 6512 | SCTP_READ_RANDOM(randp->random_data, SCTP_AUTH_RANDOM_SIZE_DEFAULT); |
| 6513 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 6514 | chunk_len += parameter_len; |
| 6515 |
| 6516 | if (padding_len > 0) { |
| 6517 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 6518 | chunk_len += padding_len; |
| 6519 | padding_len = 0; |
| 6520 | } |
| 6521 | /* add HMAC_ALGO parameter */ |
| 6522 | hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t) + chunk_len); |
| 6523 | parameter_len = (uint16_t)sizeof(struct sctp_auth_hmac_algo) + |
| 6524 | sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs, |
| 6525 | (uint8_t *)hmacs->hmac_ids); |
| 6526 | hmacs->ph.param_type = htons(SCTP_HMAC_LIST); |
| 6527 | hmacs->ph.param_length = htons(parameter_len); |
| 6528 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 6529 | chunk_len += parameter_len; |
| 6530 |
| 6531 | if (padding_len > 0) { |
| 6532 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 6533 | chunk_len += padding_len; |
| 6534 | padding_len = 0; |
| 6535 | } |
| 6536 | /* add CHUNKS parameter */ |
| 6537 | chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t) + chunk_len); |
| 6538 | parameter_len = (uint16_t)sizeof(struct sctp_auth_chunk_list) + |
| 6539 | sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks, |
| 6540 | chunks->chunk_types); |
| 6541 | chunks->ph.param_type = htons(SCTP_CHUNK_LIST); |
| 6542 | chunks->ph.param_length = htons(parameter_len); |
| 6543 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 6544 | chunk_len += parameter_len; |
| 6545 | } |
| 6546 | SCTP_BUF_LEN(m) = chunk_len; |
| 6547 | m_last = m; |
| 6548 | /* now the addresses */ |
| 6549 | /* To optimize this we could put the scoping stuff |
| 6550 | * into a structure and remove the individual uint8's from |
| 6551 | * the stc structure. Then we could just sifa in the |
| 6552 | * address within the stc.. but for now this is a quick |
| 6553 | * hack to get the address stuff teased apart. |
| 6554 | */ |
| 6555 | scp.ipv4_addr_legal = stc.ipv4_addr_legal; |
| 6556 | scp.ipv6_addr_legal = stc.ipv6_addr_legal; |
| 6557 | #if defined(__Userspace__) |
| 6558 | scp.conn_addr_legal = stc.conn_addr_legal; |
| 6559 | #endif |
| 6560 | scp.loopback_scope = stc.loopback_scope; |
| 6561 | scp.ipv4_local_scope = stc.ipv4_scope; |
| 6562 | scp.local_scope = stc.local_scope; |
| 6563 | scp.site_scope = stc.site_scope; |
| 6564 | m_last = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_last, |
| 6565 | cnt_inits_to, |
| 6566 | &padding_len, &chunk_len); |
| 6567 | /* padding_len can only be positive, if no addresses have been added */ |
| 6568 | if (padding_len > 0) { |
| 6569 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 6570 | chunk_len += padding_len; |
| 6571 | SCTP_BUF_LEN(m) += padding_len; |
| 6572 | padding_len = 0; |
| 6573 | } |
| 6574 |
| 6575 | /* tack on the operational error if present */ |
| 6576 | if (op_err) { |
| 6577 | parameter_len = 0; |
| 6578 | for (m_tmp = op_err; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) { |
| 6579 | parameter_len += SCTP_BUF_LEN(m_tmp); |
| 6580 | } |
| 6581 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 6582 | SCTP_BUF_NEXT(m_last) = op_err; |
| 6583 | while (SCTP_BUF_NEXT(m_last) != NULL) { |
| 6584 | m_last = SCTP_BUF_NEXT(m_last); |
| 6585 | } |
| 6586 | chunk_len += parameter_len; |
| 6587 | } |
| 6588 | if (padding_len > 0) { |
| 6589 | m_last = sctp_add_pad_tombuf(m_last, padding_len); |
| 6590 | if (m_last == NULL) { |
| 6591 | /* Houston we have a problem, no space */ |
| 6592 | sctp_m_freem(m); |
| 6593 | return; |
| 6594 | } |
| 6595 | chunk_len += padding_len; |
| 6596 | padding_len = 0; |
| 6597 | } |
| 6598 | /* Now we must build a cookie */ |
| 6599 | m_cookie = sctp_add_cookie(init_pkt, offset, m, 0, &stc, &signature); |
| 6600 | if (m_cookie == NULL) { |
| 6601 | /* memory problem */ |
| 6602 | sctp_m_freem(m); |
| 6603 | return; |
| 6604 | } |
| 6605 | /* Now append the cookie to the end and update the space/size */ |
| 6606 | SCTP_BUF_NEXT(m_last) = m_cookie; |
| 6607 | parameter_len = 0; |
| 6608 | for (m_tmp = m_cookie; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) { |
| 6609 | parameter_len += SCTP_BUF_LEN(m_tmp); |
| 6610 | if (SCTP_BUF_NEXT(m_tmp) == NULL) { |
| 6611 | m_last = m_tmp; |
| 6612 | } |
| 6613 | } |
| 6614 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 6615 | chunk_len += parameter_len; |
| 6616 |
| 6617 | /* Place in the size, but we don't include |
| 6618 | * the last pad (if any) in the INIT-ACK. |
| 6619 | */ |
| 6620 | initack->ch.chunk_length = htons(chunk_len); |
| 6621 |
| 6622 | /* Time to sign the cookie, we don't sign over the cookie |
| 6623 | * signature though thus we set trailer. |
| 6624 | */ |
| 6625 | (void)sctp_hmac_m(SCTP_HMAC, |
| 6626 | (uint8_t *)inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)], |
| 6627 | SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr), |
| 6628 | (uint8_t *)signature, SCTP_SIGNATURE_SIZE); |
| 6629 | /* |
| 6630 | * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return |
| 6631 | * here since the timer will drive a retranmission. |
| 6632 | */ |
| 6633 | if (padding_len > 0) { |
| 6634 | if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) { |
| 6635 | sctp_m_freem(m); |
| 6636 | return; |
| 6637 | } |
| 6638 | } |
| 6639 | if (stc.loopback_scope) { |
| 6640 | over_addr = (union sctp_sockstore *)dst; |
| 6641 | } else { |
| 6642 | over_addr = NULL; |
| 6643 | } |
| 6644 |
| 6645 | if ((error = sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0, |
| 6646 | 0, 0, |
| 6647 | inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag, |
| 6648 | port, over_addr, |
| 6649 | #if defined(__FreeBSD__) |
| 6650 | mflowtype, mflowid, |
| 6651 | #endif |
| 6652 | SCTP_SO_NOT_LOCKED))) { |
| 6653 | SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error); |
| 6654 | if (error == ENOBUFS) { |
| 6655 | if (asoc != NULL) { |
| 6656 | asoc->ifp_had_enobuf = 1; |
| 6657 | } |
| 6658 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 6659 | } |
| 6660 | } else { |
| 6661 | if (asoc != NULL) { |
| 6662 | asoc->ifp_had_enobuf = 0; |
| 6663 | } |
| 6664 | } |
| 6665 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 6666 | } |
| 6667 |
| 6668 |
| 6669 | static void |
| 6670 | sctp_prune_prsctp(struct sctp_tcb *stcb, |
| 6671 | struct sctp_association *asoc, |
| 6672 | struct sctp_sndrcvinfo *srcv, |
| 6673 | int dataout) |
| 6674 | { |
| 6675 | int freed_spc = 0; |
| 6676 | struct sctp_tmit_chunk *chk, *nchk; |
| 6677 |
| 6678 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 6679 | if ((asoc->prsctp_supported) && |
| 6680 | (asoc->sent_queue_cnt_removeable > 0)) { |
| 6681 | TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) { |
| 6682 | /* |
| 6683 | * Look for chunks marked with the PR_SCTP flag AND |
| 6684 | * the buffer space flag. If the one being sent is |
| 6685 | * equal or greater priority then purge the old one |
| 6686 | * and free some space. |
| 6687 | */ |
| 6688 | if (PR_SCTP_BUF_ENABLED(chk->flags)) { |
| 6689 | /* |
| 6690 | * This one is PR-SCTP AND buffer space |
| 6691 | * limited type |
| 6692 | */ |
| 6693 | if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) { |
| 6694 | /* |
| 6695 | * Lower numbers equates to higher |
| 6696 | * priority so if the one we are |
| 6697 | * looking at has a larger or equal |
| 6698 | * priority we want to drop the data |
| 6699 | * and NOT retransmit it. |
| 6700 | */ |
| 6701 | if (chk->data) { |
| 6702 | /* |
| 6703 | * We release the book_size |
| 6704 | * if the mbuf is here |
| 6705 | */ |
| 6706 | int ret_spc; |
| 6707 | uint8_t sent; |
| 6708 |
| 6709 | if (chk->sent > SCTP_DATAGRAM_UNSENT) |
| 6710 | sent = 1; |
| 6711 | else |
| 6712 | sent = 0; |
| 6713 | ret_spc = sctp_release_pr_sctp_chunk(stcb, chk, |
| 6714 | sent, |
| 6715 | SCTP_SO_LOCKED); |
| 6716 | freed_spc += ret_spc; |
| 6717 | if (freed_spc >= dataout) { |
| 6718 | return; |
| 6719 | } |
| 6720 | } /* if chunk was present */ |
| 6721 | } /* if of sufficient priority */ |
| 6722 | } /* if chunk has enabled */ |
| 6723 | } /* tailqforeach */ |
| 6724 |
| 6725 | TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { |
| 6726 | /* Here we must move to the sent queue and mark */ |
| 6727 | if (PR_SCTP_BUF_ENABLED(chk->flags)) { |
| 6728 | if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) { |
| 6729 | if (chk->data) { |
| 6730 | /* |
| 6731 | * We release the book_size |
| 6732 | * if the mbuf is here |
| 6733 | */ |
| 6734 | int ret_spc; |
| 6735 |
| 6736 | ret_spc = sctp_release_pr_sctp_chunk(stcb, chk, |
| 6737 | 0, SCTP_SO_LOCKED); |
| 6738 |
| 6739 | freed_spc += ret_spc; |
| 6740 | if (freed_spc >= dataout) { |
| 6741 | return; |
| 6742 | } |
| 6743 | } /* end if chk->data */ |
| 6744 | } /* end if right class */ |
| 6745 | } /* end if chk pr-sctp */ |
| 6746 | } /* tailqforeachsafe (chk) */ |
| 6747 | } /* if enabled in asoc */ |
| 6748 | } |
| 6749 |
| 6750 | int |
| 6751 | sctp_get_frag_point(struct sctp_tcb *stcb, |
| 6752 | struct sctp_association *asoc) |
| 6753 | { |
| 6754 | int siz, ovh; |
| 6755 |
| 6756 | /* |
| 6757 | * For endpoints that have both v6 and v4 addresses we must reserve |
| 6758 | * room for the ipv6 header, for those that are only dealing with V4 |
| 6759 | * we use a larger frag point. |
| 6760 | */ |
| 6761 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { |
| 6762 | ovh = SCTP_MIN_OVERHEAD; |
| 6763 | } else { |
| 6764 | #if defined(__Userspace__) |
| 6765 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) { |
| 6766 | ovh = sizeof(struct sctphdr); |
| 6767 | } else { |
| 6768 | ovh = SCTP_MIN_V4_OVERHEAD; |
| 6769 | } |
| 6770 | #else |
| 6771 | ovh = SCTP_MIN_V4_OVERHEAD; |
| 6772 | #endif |
| 6773 | } |
| 6774 | ovh += SCTP_DATA_CHUNK_OVERHEAD(stcb); |
| 6775 | if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu) |
| 6776 | siz = asoc->smallest_mtu - ovh; |
| 6777 | else |
| 6778 | siz = (stcb->asoc.sctp_frag_point - ovh); |
| 6779 | /* |
| 6780 | * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) { |
| 6781 | */ |
| 6782 | /* A data chunk MUST fit in a cluster */ |
| 6783 | /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */ |
| 6784 | /* } */ |
| 6785 |
| 6786 | /* adjust for an AUTH chunk if DATA requires auth */ |
| 6787 | if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) |
| 6788 | siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); |
| 6789 |
| 6790 | if (siz % 4) { |
| 6791 | /* make it an even word boundary please */ |
| 6792 | siz -= (siz % 4); |
| 6793 | } |
| 6794 | return (siz); |
| 6795 | } |
| 6796 |
| 6797 | static void |
| 6798 | sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp) |
| 6799 | { |
| 6800 | /* |
| 6801 | * We assume that the user wants PR_SCTP_TTL if the user |
| 6802 | * provides a positive lifetime but does not specify any |
| 6803 | * PR_SCTP policy. |
| 6804 | */ |
| 6805 | if (PR_SCTP_ENABLED(sp->sinfo_flags)) { |
| 6806 | sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags); |
| 6807 | } else if (sp->timetolive > 0) { |
| 6808 | sp->sinfo_flags |= SCTP_PR_SCTP_TTL; |
| 6809 | sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags); |
| 6810 | } else { |
| 6811 | return; |
| 6812 | } |
| 6813 | switch (PR_SCTP_POLICY(sp->sinfo_flags)) { |
| 6814 | case CHUNK_FLAGS_PR_SCTP_BUF: |
| 6815 | /* |
| 6816 | * Time to live is a priority stored in tv_sec when |
| 6817 | * doing the buffer drop thing. |
| 6818 | */ |
| 6819 | sp->ts.tv_sec = sp->timetolive; |
| 6820 | sp->ts.tv_usec = 0; |
| 6821 | break; |
| 6822 | case CHUNK_FLAGS_PR_SCTP_TTL: |
| 6823 | { |
| 6824 | struct timeval tv; |
| 6825 | (void)SCTP_GETTIME_TIMEVAL(&sp->ts); |
| 6826 | tv.tv_sec = sp->timetolive / 1000; |
| 6827 | tv.tv_usec = (sp->timetolive * 1000) % 1000000; |
| 6828 | /* TODO sctp_constants.h needs alternative time macros when |
| 6829 | * _KERNEL is undefined. |
| 6830 | */ |
| 6831 | #ifndef __FreeBSD__ |
| 6832 | timeradd(&sp->ts, &tv, &sp->ts); |
| 6833 | #else |
| 6834 | timevaladd(&sp->ts, &tv); |
| 6835 | #endif |
| 6836 | } |
| 6837 | break; |
| 6838 | case CHUNK_FLAGS_PR_SCTP_RTX: |
| 6839 | /* |
| 6840 | * Time to live is a the number or retransmissions |
| 6841 | * stored in tv_sec. |
| 6842 | */ |
| 6843 | sp->ts.tv_sec = sp->timetolive; |
| 6844 | sp->ts.tv_usec = 0; |
| 6845 | break; |
| 6846 | default: |
| 6847 | SCTPDBG(SCTP_DEBUG_USRREQ1, |
| 6848 | "Unknown PR_SCTP policy %u.\n", |
| 6849 | PR_SCTP_POLICY(sp->sinfo_flags)); |
| 6850 | break; |
| 6851 | } |
| 6852 | } |
| 6853 |
| 6854 | static int |
| 6855 | sctp_msg_append(struct sctp_tcb *stcb, |
| 6856 | struct sctp_nets *net, |
| 6857 | struct mbuf *m, |
| 6858 | struct sctp_sndrcvinfo *srcv, int hold_stcb_lock) |
| 6859 | { |
| 6860 | int error = 0; |
| 6861 | struct mbuf *at; |
| 6862 | struct sctp_stream_queue_pending *sp = NULL; |
| 6863 | struct sctp_stream_out *strm; |
| 6864 |
| 6865 | /* Given an mbuf chain, put it |
| 6866 | * into the association send queue and |
| 6867 | * place it on the wheel |
| 6868 | */ |
| 6869 | if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) { |
| 6870 | /* Invalid stream number */ |
| 6871 | SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 6872 | error = EINVAL; |
| 6873 | goto out_now; |
| 6874 | } |
| 6875 | if ((stcb->asoc.stream_locked) && |
| 6876 | (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) { |
| 6877 | SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 6878 | error = EINVAL; |
| 6879 | goto out_now; |
| 6880 | } |
| 6881 | strm = &stcb->asoc.strmout[srcv->sinfo_stream]; |
| 6882 | /* Now can we send this? */ |
| 6883 | if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) || |
| 6884 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) || |
| 6885 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) || |
| 6886 | (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) { |
| 6887 | /* got data while shutting down */ |
| 6888 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); |
| 6889 | error = ECONNRESET; |
| 6890 | goto out_now; |
| 6891 | } |
| 6892 | sctp_alloc_a_strmoq(stcb, sp); |
| 6893 | if (sp == NULL) { |
| 6894 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 6895 | error = ENOMEM; |
| 6896 | goto out_now; |
| 6897 | } |
| 6898 | sp->sinfo_flags = srcv->sinfo_flags; |
| 6899 | sp->timetolive = srcv->sinfo_timetolive; |
| 6900 | sp->ppid = srcv->sinfo_ppid; |
| 6901 | sp->context = srcv->sinfo_context; |
| 6902 | sp->fsn = 0; |
| 6903 | if (sp->sinfo_flags & SCTP_ADDR_OVER) { |
| 6904 | sp->net = net; |
| 6905 | atomic_add_int(&sp->net->ref_count, 1); |
| 6906 | } else { |
| 6907 | sp->net = NULL; |
| 6908 | } |
| 6909 | (void)SCTP_GETTIME_TIMEVAL(&sp->ts); |
| 6910 | sp->sid = srcv->sinfo_stream; |
| 6911 | sp->msg_is_complete = 1; |
| 6912 | sp->sender_all_done = 1; |
| 6913 | sp->some_taken = 0; |
| 6914 | sp->data = m; |
| 6915 | sp->tail_mbuf = NULL; |
| 6916 | sctp_set_prsctp_policy(sp); |
| 6917 | /* We could in theory (for sendall) sifa the length |
| 6918 | * in, but we would still have to hunt through the |
| 6919 | * chain since we need to setup the tail_mbuf |
| 6920 | */ |
| 6921 | sp->length = 0; |
| 6922 | for (at = m; at; at = SCTP_BUF_NEXT(at)) { |
| 6923 | if (SCTP_BUF_NEXT(at) == NULL) |
| 6924 | sp->tail_mbuf = at; |
| 6925 | sp->length += SCTP_BUF_LEN(at); |
| 6926 | } |
| 6927 | if (srcv->sinfo_keynumber_valid) { |
| 6928 | sp->auth_keyid = srcv->sinfo_keynumber; |
| 6929 | } else { |
| 6930 | sp->auth_keyid = stcb->asoc.authinfo.active_keyid; |
| 6931 | } |
| 6932 | if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) { |
| 6933 | sctp_auth_key_acquire(stcb, sp->auth_keyid); |
| 6934 | sp->holds_key_ref = 1; |
| 6935 | } |
| 6936 | if (hold_stcb_lock == 0) { |
| 6937 | SCTP_TCB_SEND_LOCK(stcb); |
| 6938 | } |
| 6939 | sctp_snd_sb_alloc(stcb, sp->length); |
| 6940 | atomic_add_int(&stcb->asoc.stream_queue_cnt, 1); |
| 6941 | TAILQ_INSERT_TAIL(&strm->outqueue, sp, next); |
| 6942 | stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp, 1); |
| 6943 | m = NULL; |
| 6944 | if (hold_stcb_lock == 0) { |
| 6945 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 6946 | } |
| 6947 | out_now: |
| 6948 | if (m) { |
| 6949 | sctp_m_freem(m); |
| 6950 | } |
| 6951 | return (error); |
| 6952 | } |
| 6953 |
| 6954 |
| 6955 | static struct mbuf * |
| 6956 | sctp_copy_mbufchain(struct mbuf *clonechain, |
| 6957 | struct mbuf *outchain, |
| 6958 | struct mbuf **endofchain, |
| 6959 | int can_take_mbuf, |
| 6960 | int sizeofcpy, |
| 6961 | uint8_t copy_by_ref) |
| 6962 | { |
| 6963 | struct mbuf *m; |
| 6964 | struct mbuf *appendchain; |
| 6965 | caddr_t cp; |
| 6966 | int len; |
| 6967 |
| 6968 | if (endofchain == NULL) { |
| 6969 | /* error */ |
| 6970 | error_out: |
| 6971 | if (outchain) |
| 6972 | sctp_m_freem(outchain); |
| 6973 | return (NULL); |
| 6974 | } |
| 6975 | if (can_take_mbuf) { |
| 6976 | appendchain = clonechain; |
| 6977 | } else { |
| 6978 | if (!copy_by_ref && |
| 6979 | #if defined(__Panda__) |
| 6980 | 0 |
| 6981 | #else |
| 6982 | (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN))) |
| 6983 | #endif |
| 6984 | ) { |
| 6985 | /* Its not in a cluster */ |
| 6986 | if (*endofchain == NULL) { |
| 6987 | /* lets get a mbuf cluster */ |
| 6988 | if (outchain == NULL) { |
| 6989 | /* This is the general case */ |
| 6990 | new_mbuf: |
| 6991 | outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER); |
| 6992 | if (outchain == NULL) { |
| 6993 | goto error_out; |
| 6994 | } |
| 6995 | SCTP_BUF_LEN(outchain) = 0; |
| 6996 | *endofchain = outchain; |
| 6997 | /* get the prepend space */ |
| 6998 | SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV+4)); |
| 6999 | } else { |
| 7000 | /* We really should not get a NULL in endofchain */ |
| 7001 | /* find end */ |
| 7002 | m = outchain; |
| 7003 | while (m) { |
| 7004 | if (SCTP_BUF_NEXT(m) == NULL) { |
| 7005 | *endofchain = m; |
| 7006 | break; |
| 7007 | } |
| 7008 | m = SCTP_BUF_NEXT(m); |
| 7009 | } |
| 7010 | /* sanity */ |
| 7011 | if (*endofchain == NULL) { |
| 7012 | /* huh, TSNH XXX maybe we should panic */ |
| 7013 | sctp_m_freem(outchain); |
| 7014 | goto new_mbuf; |
| 7015 | } |
| 7016 | } |
| 7017 | /* get the new end of length */ |
| 7018 | len = (int)M_TRAILINGSPACE(*endofchain); |
| 7019 | } else { |
| 7020 | /* how much is left at the end? */ |
| 7021 | len = (int)M_TRAILINGSPACE(*endofchain); |
| 7022 | } |
| 7023 | /* Find the end of the data, for appending */ |
| 7024 | cp = (mtod((*endofchain), caddr_t) + SCTP_BUF_LEN((*endofchain))); |
| 7025 |
| 7026 | /* Now lets copy it out */ |
| 7027 | if (len >= sizeofcpy) { |
| 7028 | /* It all fits, copy it in */ |
| 7029 | m_copydata(clonechain, 0, sizeofcpy, cp); |
| 7030 | SCTP_BUF_LEN((*endofchain)) += sizeofcpy; |
| 7031 | } else { |
| 7032 | /* fill up the end of the chain */ |
| 7033 | if (len > 0) { |
| 7034 | m_copydata(clonechain, 0, len, cp); |
| 7035 | SCTP_BUF_LEN((*endofchain)) += len; |
| 7036 | /* now we need another one */ |
| 7037 | sizeofcpy -= len; |
| 7038 | } |
| 7039 | m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER); |
| 7040 | if (m == NULL) { |
| 7041 | /* We failed */ |
| 7042 | goto error_out; |
| 7043 | } |
| 7044 | SCTP_BUF_NEXT((*endofchain)) = m; |
| 7045 | *endofchain = m; |
| 7046 | cp = mtod((*endofchain), caddr_t); |
| 7047 | m_copydata(clonechain, len, sizeofcpy, cp); |
| 7048 | SCTP_BUF_LEN((*endofchain)) += sizeofcpy; |
| 7049 | } |
| 7050 | return (outchain); |
| 7051 | } else { |
| 7052 | /* copy the old fashion way */ |
| 7053 | appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_NOWAIT); |
| 7054 | #ifdef SCTP_MBUF_LOGGING |
| 7055 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { |
| 7056 | sctp_log_mbc(appendchain, SCTP_MBUF_ICOPY); |
| 7057 | } |
| 7058 | #endif |
| 7059 | } |
| 7060 | } |
| 7061 | if (appendchain == NULL) { |
| 7062 | /* error */ |
| 7063 | if (outchain) |
| 7064 | sctp_m_freem(outchain); |
| 7065 | return (NULL); |
| 7066 | } |
| 7067 | if (outchain) { |
| 7068 | /* tack on to the end */ |
| 7069 | if (*endofchain != NULL) { |
| 7070 | SCTP_BUF_NEXT(((*endofchain))) = appendchain; |
| 7071 | } else { |
| 7072 | m = outchain; |
| 7073 | while (m) { |
| 7074 | if (SCTP_BUF_NEXT(m) == NULL) { |
| 7075 | SCTP_BUF_NEXT(m) = appendchain; |
| 7076 | break; |
| 7077 | } |
| 7078 | m = SCTP_BUF_NEXT(m); |
| 7079 | } |
| 7080 | } |
| 7081 | /* |
| 7082 | * save off the end and update the end-chain |
| 7083 | * position |
| 7084 | */ |
| 7085 | m = appendchain; |
| 7086 | while (m) { |
| 7087 | if (SCTP_BUF_NEXT(m) == NULL) { |
| 7088 | *endofchain = m; |
| 7089 | break; |
| 7090 | } |
| 7091 | m = SCTP_BUF_NEXT(m); |
| 7092 | } |
| 7093 | return (outchain); |
| 7094 | } else { |
| 7095 | /* save off the end and update the end-chain position */ |
| 7096 | m = appendchain; |
| 7097 | while (m) { |
| 7098 | if (SCTP_BUF_NEXT(m) == NULL) { |
| 7099 | *endofchain = m; |
| 7100 | break; |
| 7101 | } |
| 7102 | m = SCTP_BUF_NEXT(m); |
| 7103 | } |
| 7104 | return (appendchain); |
| 7105 | } |
| 7106 | } |
| 7107 |
| 7108 | static int |
| 7109 | sctp_med_chunk_output(struct sctp_inpcb *inp, |
| 7110 | struct sctp_tcb *stcb, |
| 7111 | struct sctp_association *asoc, |
| 7112 | int *num_out, |
| 7113 | int *reason_code, |
| 7114 | int control_only, int from_where, |
| 7115 | struct timeval *now, int *now_filled, int frag_point, int so_locked |
| 7116 | #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) |
| 7117 | SCTP_UNUSED |
| 7118 | #endif |
| 7119 | ); |
| 7120 |
| 7121 | static void |
| 7122 | sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr, |
| 7123 | uint32_t val SCTP_UNUSED) |
| 7124 | { |
| 7125 | struct sctp_copy_all *ca; |
| 7126 | struct mbuf *m; |
| 7127 | int ret = 0; |
| 7128 | int added_control = 0; |
| 7129 | int un_sent, do_chunk_output = 1; |
| 7130 | struct sctp_association *asoc; |
| 7131 | struct sctp_nets *net; |
| 7132 |
| 7133 | ca = (struct sctp_copy_all *)ptr; |
| 7134 | if (ca->m == NULL) { |
| 7135 | return; |
| 7136 | } |
| 7137 | if (ca->inp != inp) { |
| 7138 | /* TSNH */ |
| 7139 | return; |
| 7140 | } |
| 7141 | if (ca->sndlen > 0) { |
| 7142 | m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_NOWAIT); |
| 7143 | if (m == NULL) { |
| 7144 | /* can't copy so we are done */ |
| 7145 | ca->cnt_failed++; |
| 7146 | return; |
| 7147 | } |
| 7148 | #ifdef SCTP_MBUF_LOGGING |
| 7149 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { |
| 7150 | sctp_log_mbc(m, SCTP_MBUF_ICOPY); |
| 7151 | } |
| 7152 | #endif |
| 7153 | } else { |
| 7154 | m = NULL; |
| 7155 | } |
| 7156 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 7157 | if (stcb->asoc.alternate) { |
| 7158 | net = stcb->asoc.alternate; |
| 7159 | } else { |
| 7160 | net = stcb->asoc.primary_destination; |
| 7161 | } |
| 7162 | if (ca->sndrcv.sinfo_flags & SCTP_ABORT) { |
| 7163 | /* Abort this assoc with m as the user defined reason */ |
| 7164 | if (m != NULL) { |
| 7165 | SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_NOWAIT); |
| 7166 | } else { |
| 7167 | m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), |
| 7168 | 0, M_NOWAIT, 1, MT_DATA); |
| 7169 | SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr); |
| 7170 | } |
| 7171 | if (m != NULL) { |
| 7172 | struct sctp_paramhdr *ph; |
| 7173 |
| 7174 | ph = mtod(m, struct sctp_paramhdr *); |
| 7175 | ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); |
| 7176 | ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + ca->sndlen)); |
| 7177 | } |
| 7178 | /* We add one here to keep the assoc from |
| 7179 | * dis-appearing on us. |
| 7180 | */ |
| 7181 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 7182 | sctp_abort_an_association(inp, stcb, m, SCTP_SO_NOT_LOCKED); |
| 7183 | /* sctp_abort_an_association calls sctp_free_asoc() |
| 7184 | * free association will NOT free it since we |
| 7185 | * incremented the refcnt .. we do this to prevent |
| 7186 | * it being freed and things getting tricky since |
| 7187 | * we could end up (from free_asoc) calling inpcb_free |
| 7188 | * which would get a recursive lock call to the |
| 7189 | * iterator lock.. But as a consequence of that the |
| 7190 | * stcb will return to us un-locked.. since free_asoc |
| 7191 | * returns with either no TCB or the TCB unlocked, we |
| 7192 | * must relock.. to unlock in the iterator timer :-0 |
| 7193 | */ |
| 7194 | SCTP_TCB_LOCK(stcb); |
| 7195 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 7196 | goto no_chunk_output; |
| 7197 | } else { |
| 7198 | if (m) { |
| 7199 | ret = sctp_msg_append(stcb, net, m, |
| 7200 | &ca->sndrcv, 1); |
| 7201 | } |
| 7202 | asoc = &stcb->asoc; |
| 7203 | if (ca->sndrcv.sinfo_flags & SCTP_EOF) { |
| 7204 | /* shutdown this assoc */ |
| 7205 | if (TAILQ_EMPTY(&asoc->send_queue) && |
| 7206 | TAILQ_EMPTY(&asoc->sent_queue) && |
| 7207 | sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED) == 0) { |
| 7208 | if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) { |
| 7209 | goto abort_anyway; |
| 7210 | } |
| 7211 | /* there is nothing queued to send, so I'm done... */ |
| 7212 | if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) && |
| 7213 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) && |
| 7214 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { |
| 7215 | /* only send SHUTDOWN the first time through */ |
| 7216 | if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) { |
| 7217 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
| 7218 | } |
| 7219 | SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT); |
| 7220 | sctp_stop_timers_for_shutdown(stcb); |
| 7221 | sctp_send_shutdown(stcb, net); |
| 7222 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, |
| 7223 | net); |
| 7224 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, |
| 7225 | asoc->primary_destination); |
| 7226 | added_control = 1; |
| 7227 | do_chunk_output = 0; |
| 7228 | } |
| 7229 | } else { |
| 7230 | /* |
| 7231 | * we still got (or just got) data to send, so set |
| 7232 | * SHUTDOWN_PENDING |
| 7233 | */ |
| 7234 | /* |
| 7235 | * XXX sockets draft says that SCTP_EOF should be |
| 7236 | * sent with no data. currently, we will allow user |
| 7237 | * data to be sent first and move to |
| 7238 | * SHUTDOWN-PENDING |
| 7239 | */ |
| 7240 | if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) && |
| 7241 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) && |
| 7242 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { |
| 7243 | if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) { |
| 7244 | SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT); |
| 7245 | } |
| 7246 | SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING); |
| 7247 | if (TAILQ_EMPTY(&asoc->send_queue) && |
| 7248 | TAILQ_EMPTY(&asoc->sent_queue) && |
| 7249 | (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) { |
| 7250 | struct mbuf *op_err; |
| 7251 | char msg[SCTP_DIAG_INFO_LEN]; |
| 7252 |
| 7253 | abort_anyway: |
| 7254 | snprintf(msg, sizeof(msg), |
| 7255 | "%s:%d at %s", __FILE__, __LINE__, __func__); |
| 7256 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 7257 | msg); |
| 7258 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 7259 | sctp_abort_an_association(stcb->sctp_ep, stcb, |
| 7260 | op_err, SCTP_SO_NOT_LOCKED); |
| 7261 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 7262 | goto no_chunk_output; |
| 7263 | } |
| 7264 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, |
| 7265 | asoc->primary_destination); |
| 7266 | } |
| 7267 | } |
| 7268 |
| 7269 | } |
| 7270 | } |
| 7271 | un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) + |
| 7272 | (stcb->asoc.stream_queue_cnt * SCTP_DATA_CHUNK_OVERHEAD(stcb))); |
| 7273 |
| 7274 | if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) && |
| 7275 | (stcb->asoc.total_flight > 0) && |
| 7276 | (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) { |
| 7277 | do_chunk_output = 0; |
| 7278 | } |
| 7279 | if (do_chunk_output) |
| 7280 | sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED); |
| 7281 | else if (added_control) { |
| 7282 | int num_out, reason, now_filled = 0; |
| 7283 | struct timeval now; |
| 7284 | int frag_point; |
| 7285 |
| 7286 | frag_point = sctp_get_frag_point(stcb, &stcb->asoc); |
| 7287 | (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out, |
| 7288 | &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED); |
| 7289 | } |
| 7290 | no_chunk_output: |
| 7291 | if (ret) { |
| 7292 | ca->cnt_failed++; |
| 7293 | } else { |
| 7294 | ca->cnt_sent++; |
| 7295 | } |
| 7296 | } |
| 7297 |
| 7298 | static void |
| 7299 | sctp_sendall_completes(void *ptr, uint32_t val SCTP_UNUSED) |
| 7300 | { |
| 7301 | struct sctp_copy_all *ca; |
| 7302 |
| 7303 | ca = (struct sctp_copy_all *)ptr; |
| 7304 | /* |
| 7305 | * Do a notify here? Kacheong suggests that the notify be done at |
| 7306 | * the send time.. so you would push up a notification if any send |
| 7307 | * failed. Don't know if this is feasible since the only failures we |
| 7308 | * have is "memory" related and if you cannot get an mbuf to send |
| 7309 | * the data you surely can't get an mbuf to send up to notify the |
| 7310 | * user you can't send the data :-> |
| 7311 | */ |
| 7312 |
| 7313 | /* now free everything */ |
| 7314 | if (ca->inp) { |
| 7315 | /* Lets clear the flag to allow others to run. */ |
| 7316 | ca->inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP; |
| 7317 | } |
| 7318 | sctp_m_freem(ca->m); |
| 7319 | SCTP_FREE(ca, SCTP_M_COPYAL); |
| 7320 | } |
| 7321 |
| 7322 | static struct mbuf * |
| 7323 | sctp_copy_out_all(struct uio *uio, ssize_t len) |
| 7324 | { |
| 7325 | struct mbuf *ret, *at; |
| 7326 | ssize_t left, willcpy, cancpy, error; |
| 7327 |
| 7328 | ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAITOK, 1, MT_DATA); |
| 7329 | if (ret == NULL) { |
| 7330 | /* TSNH */ |
| 7331 | return (NULL); |
| 7332 | } |
| 7333 | left = len; |
| 7334 | SCTP_BUF_LEN(ret) = 0; |
| 7335 | /* save space for the data chunk header */ |
| 7336 | cancpy = (int)M_TRAILINGSPACE(ret); |
| 7337 | willcpy = min(cancpy, left); |
| 7338 | at = ret; |
| 7339 | while (left > 0) { |
| 7340 | /* Align data to the end */ |
| 7341 | error = uiomove(mtod(at, caddr_t), (int)willcpy, uio); |
| 7342 | if (error) { |
| 7343 | err_out_now: |
| 7344 | sctp_m_freem(at); |
| 7345 | return (NULL); |
| 7346 | } |
| 7347 | SCTP_BUF_LEN(at) = (int)willcpy; |
| 7348 | SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0; |
| 7349 | left -= willcpy; |
| 7350 | if (left > 0) { |
| 7351 | SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg((unsigned int)left, 0, M_WAITOK, 1, MT_DATA); |
| 7352 | if (SCTP_BUF_NEXT(at) == NULL) { |
| 7353 | goto err_out_now; |
| 7354 | } |
| 7355 | at = SCTP_BUF_NEXT(at); |
| 7356 | SCTP_BUF_LEN(at) = 0; |
| 7357 | cancpy = (int)M_TRAILINGSPACE(at); |
| 7358 | willcpy = min(cancpy, left); |
| 7359 | } |
| 7360 | } |
| 7361 | return (ret); |
| 7362 | } |
| 7363 |
| 7364 | static int |
| 7365 | sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m, |
| 7366 | struct sctp_sndrcvinfo *srcv) |
| 7367 | { |
| 7368 | int ret; |
| 7369 | struct sctp_copy_all *ca; |
| 7370 |
| 7371 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SND_ITERATOR_UP) { |
| 7372 | /* There is another. */ |
| 7373 | return (EBUSY); |
| 7374 | } |
| 7375 | #if defined(__APPLE__) |
| 7376 | #if defined(APPLE_LEOPARD) |
| 7377 | if (uio->uio_resid > SCTP_BASE_SYSCTL(sctp_sendall_limit)) { |
| 7378 | #else |
| 7379 | if (uio_resid(uio) > SCTP_BASE_SYSCTL(sctp_sendall_limit)) { |
| 7380 | #endif |
| 7381 | #else |
| 7382 | if (uio->uio_resid > (ssize_t)SCTP_BASE_SYSCTL(sctp_sendall_limit)) { |
| 7383 | #endif |
| 7384 | /* You must not be larger than the limit! */ |
| 7385 | return (EMSGSIZE); |
| 7386 | } |
| 7387 | SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all), |
| 7388 | SCTP_M_COPYAL); |
| 7389 | if (ca == NULL) { |
| 7390 | sctp_m_freem(m); |
| 7391 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 7392 | return (ENOMEM); |
| 7393 | } |
| 7394 | memset(ca, 0, sizeof(struct sctp_copy_all)); |
| 7395 |
| 7396 | ca->inp = inp; |
| 7397 | if (srcv) { |
| 7398 | memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo)); |
| 7399 | } |
| 7400 | /* |
| 7401 | * take off the sendall flag, it would be bad if we failed to do |
| 7402 | * this :-0 |
| 7403 | */ |
| 7404 | ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL; |
| 7405 | /* get length and mbuf chain */ |
| 7406 | if (uio) { |
| 7407 | #if defined(__APPLE__) |
| 7408 | #if defined(APPLE_LEOPARD) |
| 7409 | ca->sndlen = uio->uio_resid; |
| 7410 | #else |
| 7411 | ca->sndlen = uio_resid(uio); |
| 7412 | #endif |
| 7413 | #else |
| 7414 | ca->sndlen = uio->uio_resid; |
| 7415 | #endif |
| 7416 | #if defined(__APPLE__) |
| 7417 | SCTP_SOCKET_UNLOCK(SCTP_INP_SO(inp), 0); |
| 7418 | #endif |
| 7419 | ca->m = sctp_copy_out_all(uio, ca->sndlen); |
| 7420 | #if defined(__APPLE__) |
| 7421 | SCTP_SOCKET_LOCK(SCTP_INP_SO(inp), 0); |
| 7422 | #endif |
| 7423 | if (ca->m == NULL) { |
| 7424 | SCTP_FREE(ca, SCTP_M_COPYAL); |
| 7425 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 7426 | return (ENOMEM); |
| 7427 | } |
| 7428 | } else { |
| 7429 | /* Gather the length of the send */ |
| 7430 | struct mbuf *mat; |
| 7431 |
| 7432 | ca->sndlen = 0; |
| 7433 | for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) { |
| 7434 | ca->sndlen += SCTP_BUF_LEN(mat); |
| 7435 | } |
| 7436 | } |
| 7437 | inp->sctp_flags |= SCTP_PCB_FLAGS_SND_ITERATOR_UP; |
| 7438 | ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL, |
| 7439 | SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES, |
| 7440 | SCTP_ASOC_ANY_STATE, |
| 7441 | (void *)ca, 0, |
| 7442 | sctp_sendall_completes, inp, 1); |
| 7443 | if (ret) { |
| 7444 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP; |
| 7445 | SCTP_FREE(ca, SCTP_M_COPYAL); |
| 7446 | SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT); |
| 7447 | return (EFAULT); |
| 7448 | } |
| 7449 | return (0); |
| 7450 | } |
| 7451 |
| 7452 |
| 7453 | void |
| 7454 | sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc) |
| 7455 | { |
| 7456 | struct sctp_tmit_chunk *chk, *nchk; |
| 7457 |
| 7458 | TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { |
| 7459 | if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) { |
| 7460 | TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); |
| 7461 | asoc->ctrl_queue_cnt--; |
| 7462 | if (chk->data) { |
| 7463 | sctp_m_freem(chk->data); |
| 7464 | chk->data = NULL; |
| 7465 | } |
| 7466 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 7467 | } |
| 7468 | } |
| 7469 | } |
| 7470 |
| 7471 | void |
| 7472 | sctp_toss_old_asconf(struct sctp_tcb *stcb) |
| 7473 | { |
| 7474 | struct sctp_association *asoc; |
| 7475 | struct sctp_tmit_chunk *chk, *nchk; |
| 7476 | struct sctp_asconf_chunk *acp; |
| 7477 |
| 7478 | asoc = &stcb->asoc; |
| 7479 | TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) { |
| 7480 | /* find SCTP_ASCONF chunk in queue */ |
| 7481 | if (chk->rec.chunk_id.id == SCTP_ASCONF) { |
| 7482 | if (chk->data) { |
| 7483 | acp = mtod(chk->data, struct sctp_asconf_chunk *); |
| 7484 | if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) { |
| 7485 | /* Not Acked yet */ |
| 7486 | break; |
| 7487 | } |
| 7488 | } |
| 7489 | TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next); |
| 7490 | asoc->ctrl_queue_cnt--; |
| 7491 | if (chk->data) { |
| 7492 | sctp_m_freem(chk->data); |
| 7493 | chk->data = NULL; |
| 7494 | } |
| 7495 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 7496 | } |
| 7497 | } |
| 7498 | } |
| 7499 |
| 7500 |
| 7501 | static void |
| 7502 | sctp_clean_up_datalist(struct sctp_tcb *stcb, |
| 7503 | struct sctp_association *asoc, |
| 7504 | struct sctp_tmit_chunk **data_list, |
| 7505 | int bundle_at, |
| 7506 | struct sctp_nets *net) |
| 7507 | { |
| 7508 | int i; |
| 7509 | struct sctp_tmit_chunk *tp1; |
| 7510 |
| 7511 | for (i = 0; i < bundle_at; i++) { |
| 7512 | /* off of the send queue */ |
| 7513 | TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next); |
| 7514 | asoc->send_queue_cnt--; |
| 7515 | if (i > 0) { |
| 7516 | /* |
| 7517 | * Any chunk NOT 0 you zap the time chunk 0 gets |
| 7518 | * zapped or set based on if a RTO measurment is |
| 7519 | * needed. |
| 7520 | */ |
| 7521 | data_list[i]->do_rtt = 0; |
| 7522 | } |
| 7523 | /* record time */ |
| 7524 | data_list[i]->sent_rcv_time = net->last_sent_time; |
| 7525 | data_list[i]->rec.data.cwnd_at_send = net->cwnd; |
| 7526 | data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.tsn; |
| 7527 | if (data_list[i]->whoTo == NULL) { |
| 7528 | data_list[i]->whoTo = net; |
| 7529 | atomic_add_int(&net->ref_count, 1); |
| 7530 | } |
| 7531 | /* on to the sent queue */ |
| 7532 | tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead); |
| 7533 | if ((tp1) && SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) { |
| 7534 | struct sctp_tmit_chunk *tpp; |
| 7535 |
| 7536 | /* need to move back */ |
| 7537 | back_up_more: |
| 7538 | tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next); |
| 7539 | if (tpp == NULL) { |
| 7540 | TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next); |
| 7541 | goto all_done; |
| 7542 | } |
| 7543 | tp1 = tpp; |
| 7544 | if (SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) { |
| 7545 | goto back_up_more; |
| 7546 | } |
| 7547 | TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next); |
| 7548 | } else { |
| 7549 | TAILQ_INSERT_TAIL(&asoc->sent_queue, |
| 7550 | data_list[i], |
| 7551 | sctp_next); |
| 7552 | } |
| 7553 | all_done: |
| 7554 | /* This does not lower until the cum-ack passes it */ |
| 7555 | asoc->sent_queue_cnt++; |
| 7556 | if ((asoc->peers_rwnd <= 0) && |
| 7557 | (asoc->total_flight == 0) && |
| 7558 | (bundle_at == 1)) { |
| 7559 | /* Mark the chunk as being a window probe */ |
| 7560 | SCTP_STAT_INCR(sctps_windowprobed); |
| 7561 | } |
| 7562 | #ifdef SCTP_AUDITING_ENABLED |
| 7563 | sctp_audit_log(0xC2, 3); |
| 7564 | #endif |
| 7565 | data_list[i]->sent = SCTP_DATAGRAM_SENT; |
| 7566 | data_list[i]->snd_count = 1; |
| 7567 | data_list[i]->rec.data.chunk_was_revoked = 0; |
| 7568 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { |
| 7569 | sctp_misc_ints(SCTP_FLIGHT_LOG_UP, |
| 7570 | data_list[i]->whoTo->flight_size, |
| 7571 | data_list[i]->book_size, |
| 7572 | (uint32_t)(uintptr_t)data_list[i]->whoTo, |
| 7573 | data_list[i]->rec.data.tsn); |
| 7574 | } |
| 7575 | sctp_flight_size_increase(data_list[i]); |
| 7576 | sctp_total_flight_increase(stcb, data_list[i]); |
| 7577 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) { |
| 7578 | sctp_log_rwnd(SCTP_DECREASE_PEER_RWND, |
| 7579 | asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)); |
| 7580 | } |
| 7581 | asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd, |
| 7582 | (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))); |
| 7583 | if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) { |
| 7584 | /* SWS sender side engages */ |
| 7585 | asoc->peers_rwnd = 0; |
| 7586 | } |
| 7587 | } |
| 7588 | if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) { |
| 7589 | (*asoc->cc_functions.sctp_cwnd_update_packet_transmitted)(stcb, net); |
| 7590 | } |
| 7591 | } |
| 7592 |
| 7593 | static void |
| 7594 | sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked |
| 7595 | #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) |
| 7596 | SCTP_UNUSED |
| 7597 | #endif |
| 7598 | ) |
| 7599 | { |
| 7600 | struct sctp_tmit_chunk *chk, *nchk; |
| 7601 |
| 7602 | TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { |
| 7603 | if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) || |
| 7604 | (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */ |
| 7605 | (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) || |
| 7606 | (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) || |
| 7607 | (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) || |
| 7608 | (chk->rec.chunk_id.id == SCTP_SHUTDOWN) || |
| 7609 | (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) || |
| 7610 | (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) || |
| 7611 | (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) || |
| 7612 | (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) || |
| 7613 | (chk->rec.chunk_id.id == SCTP_ECN_CWR) || |
| 7614 | (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) { |
| 7615 | /* Stray chunks must be cleaned up */ |
| 7616 | clean_up_anyway: |
| 7617 | TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); |
| 7618 | asoc->ctrl_queue_cnt--; |
| 7619 | if (chk->data) { |
| 7620 | sctp_m_freem(chk->data); |
| 7621 | chk->data = NULL; |
| 7622 | } |
| 7623 | if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) { |
| 7624 | asoc->fwd_tsn_cnt--; |
| 7625 | } |
| 7626 | sctp_free_a_chunk(stcb, chk, so_locked); |
| 7627 | } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) { |
| 7628 | /* special handling, we must look into the param */ |
| 7629 | if (chk != asoc->str_reset) { |
| 7630 | goto clean_up_anyway; |
| 7631 | } |
| 7632 | } |
| 7633 | } |
| 7634 | } |
| 7635 |
| 7636 | static uint32_t |
| 7637 | sctp_can_we_split_this(struct sctp_tcb *stcb, uint32_t length, |
| 7638 | uint32_t space_left, uint32_t frag_point, int eeor_on) |
| 7639 | { |
| 7640 | /* Make a decision on if I should split a |
| 7641 | * msg into multiple parts. This is only asked of |
| 7642 | * incomplete messages. |
| 7643 | */ |
| 7644 | if (eeor_on) { |
| 7645 | /* If we are doing EEOR we need to always send |
| 7646 | * it if its the entire thing, since it might |
| 7647 | * be all the guy is putting in the hopper. |
| 7648 | */ |
| 7649 | if (space_left >= length) { |
| 7650 | /*- |
| 7651 | * If we have data outstanding, |
| 7652 | * we get another chance when the sack |
| 7653 | * arrives to transmit - wait for more data |
| 7654 | */ |
| 7655 | if (stcb->asoc.total_flight == 0) { |
| 7656 | /* If nothing is in flight, we zero |
| 7657 | * the packet counter. |
| 7658 | */ |
| 7659 | return (length); |
| 7660 | } |
| 7661 | return (0); |
| 7662 |
| 7663 | } else { |
| 7664 | /* You can fill the rest */ |
| 7665 | return (space_left); |
| 7666 | } |
| 7667 | } |
| 7668 | /*- |
| 7669 | * For those strange folk that make the send buffer |
| 7670 | * smaller than our fragmentation point, we can't |
| 7671 | * get a full msg in so we have to allow splitting. |
| 7672 | */ |
| 7673 | if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) { |
| 7674 | return (length); |
| 7675 | } |
| 7676 | if ((length <= space_left) || |
| 7677 | ((length - space_left) < SCTP_BASE_SYSCTL(sctp_min_residual))) { |
| 7678 | /* Sub-optimial residual don't split in non-eeor mode. */ |
| 7679 | return (0); |
| 7680 | } |
| 7681 | /* If we reach here length is larger |
| 7682 | * than the space_left. Do we wish to split |
| 7683 | * it for the sake of packet putting together? |
| 7684 | */ |
| 7685 | if (space_left >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) { |
| 7686 | /* Its ok to split it */ |
| 7687 | return (min(space_left, frag_point)); |
| 7688 | } |
| 7689 | /* Nope, can't split */ |
| 7690 | return (0); |
| 7691 | } |
| 7692 |
| 7693 | static uint32_t |
| 7694 | sctp_move_to_outqueue(struct sctp_tcb *stcb, |
| 7695 | struct sctp_stream_out *strq, |
| 7696 | uint32_t space_left, |
| 7697 | uint32_t frag_point, |
| 7698 | int *giveup, |
| 7699 | int eeor_mode, |
| 7700 | int *bail, |
| 7701 | int so_locked |
| 7702 | #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) |
| 7703 | SCTP_UNUSED |
| 7704 | #endif |
| 7705 | ) |
| 7706 | { |
| 7707 | /* Move from the stream to the send_queue keeping track of the total */ |
| 7708 | struct sctp_association *asoc; |
| 7709 | struct sctp_stream_queue_pending *sp; |
| 7710 | struct sctp_tmit_chunk *chk; |
| 7711 | struct sctp_data_chunk *dchkh=NULL; |
| 7712 | struct sctp_idata_chunk *ndchkh=NULL; |
| 7713 | uint32_t to_move, length; |
| 7714 | int leading; |
| 7715 | uint8_t rcv_flags = 0; |
| 7716 | uint8_t some_taken; |
| 7717 | uint8_t send_lock_up = 0; |
| 7718 |
| 7719 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 7720 | asoc = &stcb->asoc; |
| 7721 | one_more_time: |
| 7722 | /*sa_ignore FREED_MEMORY*/ |
| 7723 | sp = TAILQ_FIRST(&strq->outqueue); |
| 7724 | if (sp == NULL) { |
| 7725 | if (send_lock_up == 0) { |
| 7726 | SCTP_TCB_SEND_LOCK(stcb); |
| 7727 | send_lock_up = 1; |
| 7728 | } |
| 7729 | sp = TAILQ_FIRST(&strq->outqueue); |
| 7730 | if (sp) { |
| 7731 | goto one_more_time; |
| 7732 | } |
| 7733 | if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_EXPLICIT_EOR) == 0) && |
| 7734 | (stcb->asoc.idata_supported == 0) && |
| 7735 | (strq->last_msg_incomplete)) { |
| 7736 | SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n", |
| 7737 | strq->sid, |
| 7738 | strq->last_msg_incomplete); |
| 7739 | strq->last_msg_incomplete = 0; |
| 7740 | } |
| 7741 | to_move = 0; |
| 7742 | if (send_lock_up) { |
| 7743 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 7744 | send_lock_up = 0; |
| 7745 | } |
| 7746 | goto out_of; |
| 7747 | } |
| 7748 | if ((sp->msg_is_complete) && (sp->length == 0)) { |
| 7749 | if (sp->sender_all_done) { |
| 7750 | /* We are doing deferred cleanup. Last |
| 7751 | * time through when we took all the data |
| 7752 | * the sender_all_done was not set. |
| 7753 | */ |
| 7754 | if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) { |
| 7755 | SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); |
| 7756 | SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n", |
| 7757 | sp->sender_all_done, |
| 7758 | sp->length, |
| 7759 | sp->msg_is_complete, |
| 7760 | sp->put_last_out, |
| 7761 | send_lock_up); |
| 7762 | } |
| 7763 | if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) { |
| 7764 | SCTP_TCB_SEND_LOCK(stcb); |
| 7765 | send_lock_up = 1; |
| 7766 | } |
| 7767 | atomic_subtract_int(&asoc->stream_queue_cnt, 1); |
| 7768 | TAILQ_REMOVE(&strq->outqueue, sp, next); |
| 7769 | stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up); |
| 7770 | if ((strq->state == SCTP_STREAM_RESET_PENDING) && |
| 7771 | (strq->chunks_on_queues == 0) && |
| 7772 | TAILQ_EMPTY(&strq->outqueue)) { |
| 7773 | stcb->asoc.trigger_reset = 1; |
| 7774 | } |
| 7775 | if (sp->net) { |
| 7776 | sctp_free_remote_addr(sp->net); |
| 7777 | sp->net = NULL; |
| 7778 | } |
| 7779 | if (sp->data) { |
| 7780 | sctp_m_freem(sp->data); |
| 7781 | sp->data = NULL; |
| 7782 | } |
| 7783 | sctp_free_a_strmoq(stcb, sp, so_locked); |
| 7784 | /* we can't be locked to it */ |
| 7785 | if (send_lock_up) { |
| 7786 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 7787 | send_lock_up = 0; |
| 7788 | } |
| 7789 | /* back to get the next msg */ |
| 7790 | goto one_more_time; |
| 7791 | } else { |
| 7792 | /* sender just finished this but |
| 7793 | * still holds a reference |
| 7794 | */ |
| 7795 | *giveup = 1; |
| 7796 | to_move = 0; |
| 7797 | goto out_of; |
| 7798 | } |
| 7799 | } else { |
| 7800 | /* is there some to get */ |
| 7801 | if (sp->length == 0) { |
| 7802 | /* no */ |
| 7803 | *giveup = 1; |
| 7804 | to_move = 0; |
| 7805 | goto out_of; |
| 7806 | } else if (sp->discard_rest) { |
| 7807 | if (send_lock_up == 0) { |
| 7808 | SCTP_TCB_SEND_LOCK(stcb); |
| 7809 | send_lock_up = 1; |
| 7810 | } |
| 7811 | /* Whack down the size */ |
| 7812 | atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length); |
| 7813 | if ((stcb->sctp_socket != NULL) && |
| 7814 | ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || |
| 7815 | (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { |
| 7816 | atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length); |
| 7817 | } |
| 7818 | if (sp->data) { |
| 7819 | sctp_m_freem(sp->data); |
| 7820 | sp->data = NULL; |
| 7821 | sp->tail_mbuf = NULL; |
| 7822 | } |
| 7823 | sp->length = 0; |
| 7824 | sp->some_taken = 1; |
| 7825 | *giveup = 1; |
| 7826 | to_move = 0; |
| 7827 | goto out_of; |
| 7828 | } |
| 7829 | } |
| 7830 | some_taken = sp->some_taken; |
| 7831 | re_look: |
| 7832 | length = sp->length; |
| 7833 | if (sp->msg_is_complete) { |
| 7834 | /* The message is complete */ |
| 7835 | to_move = min(length, frag_point); |
| 7836 | if (to_move == length) { |
| 7837 | /* All of it fits in the MTU */ |
| 7838 | if (sp->some_taken) { |
| 7839 | rcv_flags |= SCTP_DATA_LAST_FRAG; |
| 7840 | } else { |
| 7841 | rcv_flags |= SCTP_DATA_NOT_FRAG; |
| 7842 | } |
| 7843 | sp->put_last_out = 1; |
| 7844 | if (sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) { |
| 7845 | rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY; |
| 7846 | } |
| 7847 | } else { |
| 7848 | /* Not all of it fits, we fragment */ |
| 7849 | if (sp->some_taken == 0) { |
| 7850 | rcv_flags |= SCTP_DATA_FIRST_FRAG; |
| 7851 | } |
| 7852 | sp->some_taken = 1; |
| 7853 | } |
| 7854 | } else { |
| 7855 | to_move = sctp_can_we_split_this(stcb, length, space_left, frag_point, eeor_mode); |
| 7856 | if (to_move) { |
| 7857 | /*- |
| 7858 | * We use a snapshot of length in case it |
| 7859 | * is expanding during the compare. |
| 7860 | */ |
| 7861 | uint32_t llen; |
| 7862 |
| 7863 | llen = length; |
| 7864 | if (to_move >= llen) { |
| 7865 | to_move = llen; |
| 7866 | if (send_lock_up == 0) { |
| 7867 | /*- |
| 7868 | * We are taking all of an incomplete msg |
| 7869 | * thus we need a send lock. |
| 7870 | */ |
| 7871 | SCTP_TCB_SEND_LOCK(stcb); |
| 7872 | send_lock_up = 1; |
| 7873 | if (sp->msg_is_complete) { |
| 7874 | /* the sender finished the msg */ |
| 7875 | goto re_look; |
| 7876 | } |
| 7877 | } |
| 7878 | } |
| 7879 | if (sp->some_taken == 0) { |
| 7880 | rcv_flags |= SCTP_DATA_FIRST_FRAG; |
| 7881 | sp->some_taken = 1; |
| 7882 | } |
| 7883 | } else { |
| 7884 | /* Nothing to take. */ |
| 7885 | *giveup = 1; |
| 7886 | to_move = 0; |
| 7887 | goto out_of; |
| 7888 | } |
| 7889 | } |
| 7890 |
| 7891 | /* If we reach here, we can copy out a chunk */ |
| 7892 | sctp_alloc_a_chunk(stcb, chk); |
| 7893 | if (chk == NULL) { |
| 7894 | /* No chunk memory */ |
| 7895 | *giveup = 1; |
| 7896 | to_move = 0; |
| 7897 | goto out_of; |
| 7898 | } |
| 7899 | /* Setup for unordered if needed by looking |
| 7900 | * at the user sent info flags. |
| 7901 | */ |
| 7902 | if (sp->sinfo_flags & SCTP_UNORDERED) { |
| 7903 | rcv_flags |= SCTP_DATA_UNORDERED; |
| 7904 | } |
| 7905 | if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && |
| 7906 | (sp->sinfo_flags & SCTP_EOF) == SCTP_EOF) { |
| 7907 | rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY; |
| 7908 | } |
| 7909 | /* clear out the chunk before setting up */ |
| 7910 | memset(chk, 0, sizeof(*chk)); |
| 7911 | chk->rec.data.rcv_flags = rcv_flags; |
| 7912 |
| 7913 | if (to_move >= length) { |
| 7914 | /* we think we can steal the whole thing */ |
| 7915 | if ((sp->sender_all_done == 0) && (send_lock_up == 0)) { |
| 7916 | SCTP_TCB_SEND_LOCK(stcb); |
| 7917 | send_lock_up = 1; |
| 7918 | } |
| 7919 | if (to_move < sp->length) { |
| 7920 | /* bail, it changed */ |
| 7921 | goto dont_do_it; |
| 7922 | } |
| 7923 | chk->data = sp->data; |
| 7924 | chk->last_mbuf = sp->tail_mbuf; |
| 7925 | /* register the stealing */ |
| 7926 | sp->data = sp->tail_mbuf = NULL; |
| 7927 | } else { |
| 7928 | struct mbuf *m; |
| 7929 | dont_do_it: |
| 7930 | chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_NOWAIT); |
| 7931 | chk->last_mbuf = NULL; |
| 7932 | if (chk->data == NULL) { |
| 7933 | sp->some_taken = some_taken; |
| 7934 | sctp_free_a_chunk(stcb, chk, so_locked); |
| 7935 | *bail = 1; |
| 7936 | to_move = 0; |
| 7937 | goto out_of; |
| 7938 | } |
| 7939 | #ifdef SCTP_MBUF_LOGGING |
| 7940 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { |
| 7941 | sctp_log_mbc(chk->data, SCTP_MBUF_ICOPY); |
| 7942 | } |
| 7943 | #endif |
| 7944 | /* Pull off the data */ |
| 7945 | m_adj(sp->data, to_move); |
| 7946 | /* Now lets work our way down and compact it */ |
| 7947 | m = sp->data; |
| 7948 | while (m && (SCTP_BUF_LEN(m) == 0)) { |
| 7949 | sp->data = SCTP_BUF_NEXT(m); |
| 7950 | SCTP_BUF_NEXT(m) = NULL; |
| 7951 | if (sp->tail_mbuf == m) { |
| 7952 | /*- |
| 7953 | * Freeing tail? TSNH since |
| 7954 | * we supposedly were taking less |
| 7955 | * than the sp->length. |
| 7956 | */ |
| 7957 | #ifdef INVARIANTS |
| 7958 | panic("Huh, freing tail? - TSNH"); |
| 7959 | #else |
| 7960 | SCTP_PRINTF("Huh, freeing tail? - TSNH\n"); |
| 7961 | sp->tail_mbuf = sp->data = NULL; |
| 7962 | sp->length = 0; |
| 7963 | #endif |
| 7964 |
| 7965 | } |
| 7966 | sctp_m_free(m); |
| 7967 | m = sp->data; |
| 7968 | } |
| 7969 | } |
| 7970 | if (SCTP_BUF_IS_EXTENDED(chk->data)) { |
| 7971 | chk->copy_by_ref = 1; |
| 7972 | } else { |
| 7973 | chk->copy_by_ref = 0; |
| 7974 | } |
| 7975 | /* get last_mbuf and counts of mb usage |
| 7976 | * This is ugly but hopefully its only one mbuf. |
| 7977 | */ |
| 7978 | if (chk->last_mbuf == NULL) { |
| 7979 | chk->last_mbuf = chk->data; |
| 7980 | while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) { |
| 7981 | chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf); |
| 7982 | } |
| 7983 | } |
| 7984 |
| 7985 | if (to_move > length) { |
| 7986 | /*- This should not happen either |
| 7987 | * since we always lower to_move to the size |
| 7988 | * of sp->length if its larger. |
| 7989 | */ |
| 7990 | #ifdef INVARIANTS |
| 7991 | panic("Huh, how can to_move be larger?"); |
| 7992 | #else |
| 7993 | SCTP_PRINTF("Huh, how can to_move be larger?\n"); |
| 7994 | sp->length = 0; |
| 7995 | #endif |
| 7996 | } else { |
| 7997 | atomic_subtract_int(&sp->length, to_move); |
| 7998 | } |
| 7999 | leading = SCTP_DATA_CHUNK_OVERHEAD(stcb); |
| 8000 | if (M_LEADINGSPACE(chk->data) < leading) { |
| 8001 | /* Not enough room for a chunk header, get some */ |
| 8002 | struct mbuf *m; |
| 8003 |
| 8004 | m = sctp_get_mbuf_for_msg(1, 0, M_NOWAIT, 1, MT_DATA); |
| 8005 | if (m == NULL) { |
| 8006 | /* |
| 8007 | * we're in trouble here. _PREPEND below will free |
| 8008 | * all the data if there is no leading space, so we |
| 8009 | * must put the data back and restore. |
| 8010 | */ |
| 8011 | if (send_lock_up == 0) { |
| 8012 | SCTP_TCB_SEND_LOCK(stcb); |
| 8013 | send_lock_up = 1; |
| 8014 | } |
| 8015 | if (sp->data == NULL) { |
| 8016 | /* unsteal the data */ |
| 8017 | sp->data = chk->data; |
| 8018 | sp->tail_mbuf = chk->last_mbuf; |
| 8019 | } else { |
| 8020 | struct mbuf *m_tmp; |
| 8021 | /* reassemble the data */ |
| 8022 | m_tmp = sp->data; |
| 8023 | sp->data = chk->data; |
| 8024 | SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp; |
| 8025 | } |
| 8026 | sp->some_taken = some_taken; |
| 8027 | atomic_add_int(&sp->length, to_move); |
| 8028 | chk->data = NULL; |
| 8029 | *bail = 1; |
| 8030 | sctp_free_a_chunk(stcb, chk, so_locked); |
| 8031 | to_move = 0; |
| 8032 | goto out_of; |
| 8033 | } else { |
| 8034 | SCTP_BUF_LEN(m) = 0; |
| 8035 | SCTP_BUF_NEXT(m) = chk->data; |
| 8036 | chk->data = m; |
| 8037 | M_ALIGN(chk->data, 4); |
| 8038 | } |
| 8039 | } |
| 8040 | SCTP_BUF_PREPEND(chk->data, SCTP_DATA_CHUNK_OVERHEAD(stcb), M_NOWAIT); |
| 8041 | if (chk->data == NULL) { |
| 8042 | /* HELP, TSNH since we assured it would not above? */ |
| 8043 | #ifdef INVARIANTS |
| 8044 | panic("prepend failes HELP?"); |
| 8045 | #else |
| 8046 | SCTP_PRINTF("prepend fails HELP?\n"); |
| 8047 | sctp_free_a_chunk(stcb, chk, so_locked); |
| 8048 | #endif |
| 8049 | *bail = 1; |
| 8050 | to_move = 0; |
| 8051 | goto out_of; |
| 8052 | } |
| 8053 | sctp_snd_sb_alloc(stcb, SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 8054 | chk->book_size = chk->send_size = (uint16_t)(to_move + SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 8055 | chk->book_size_scale = 0; |
| 8056 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 8057 |
| 8058 | chk->flags = 0; |
| 8059 | chk->asoc = &stcb->asoc; |
| 8060 | chk->pad_inplace = 0; |
| 8061 | chk->no_fr_allowed = 0; |
| 8062 | if (stcb->asoc.idata_supported == 0) { |
| 8063 | if (rcv_flags & SCTP_DATA_UNORDERED) { |
| 8064 | /* Just use 0. The receiver ignores the values. */ |
| 8065 | chk->rec.data.mid = 0; |
| 8066 | } else { |
| 8067 | chk->rec.data.mid = strq->next_mid_ordered; |
| 8068 | if (rcv_flags & SCTP_DATA_LAST_FRAG) { |
| 8069 | strq->next_mid_ordered++; |
| 8070 | } |
| 8071 | } |
| 8072 | } else { |
| 8073 | if (rcv_flags & SCTP_DATA_UNORDERED) { |
| 8074 | chk->rec.data.mid = strq->next_mid_unordered; |
| 8075 | if (rcv_flags & SCTP_DATA_LAST_FRAG) { |
| 8076 | strq->next_mid_unordered++; |
| 8077 | } |
| 8078 | } else { |
| 8079 | chk->rec.data.mid = strq->next_mid_ordered; |
| 8080 | if (rcv_flags & SCTP_DATA_LAST_FRAG) { |
| 8081 | strq->next_mid_ordered++; |
| 8082 | } |
| 8083 | } |
| 8084 | } |
| 8085 | chk->rec.data.sid = sp->sid; |
| 8086 | chk->rec.data.ppid = sp->ppid; |
| 8087 | chk->rec.data.context = sp->context; |
| 8088 | chk->rec.data.doing_fast_retransmit = 0; |
| 8089 |
| 8090 | chk->rec.data.timetodrop = sp->ts; |
| 8091 | chk->flags = sp->act_flags; |
| 8092 |
| 8093 | if (sp->net) { |
| 8094 | chk->whoTo = sp->net; |
| 8095 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 8096 | } else |
| 8097 | chk->whoTo = NULL; |
| 8098 |
| 8099 | if (sp->holds_key_ref) { |
| 8100 | chk->auth_keyid = sp->auth_keyid; |
| 8101 | sctp_auth_key_acquire(stcb, chk->auth_keyid); |
| 8102 | chk->holds_key_ref = 1; |
| 8103 | } |
| 8104 | #if defined(__FreeBSD__) || defined(__Panda__) |
| 8105 | chk->rec.data.tsn = atomic_fetchadd_int(&asoc->sending_seq, 1); |
| 8106 | #else |
| 8107 | chk->rec.data.tsn = asoc->sending_seq++; |
| 8108 | #endif |
| 8109 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) { |
| 8110 | sctp_misc_ints(SCTP_STRMOUT_LOG_SEND, |
| 8111 | (uint32_t)(uintptr_t)stcb, sp->length, |
| 8112 | (uint32_t)((chk->rec.data.sid << 16) | (0x0000ffff & chk->rec.data.mid)), |
| 8113 | chk->rec.data.tsn); |
| 8114 | } |
| 8115 | if (stcb->asoc.idata_supported == 0) { |
| 8116 | dchkh = mtod(chk->data, struct sctp_data_chunk *); |
| 8117 | } else { |
| 8118 | ndchkh = mtod(chk->data, struct sctp_idata_chunk *); |
| 8119 | } |
| 8120 | /* |
| 8121 | * Put the rest of the things in place now. Size was done |
| 8122 | * earlier in previous loop prior to padding. |
| 8123 | */ |
| 8124 |
| 8125 | #ifdef SCTP_ASOCLOG_OF_TSNS |
| 8126 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 8127 | if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) { |
| 8128 | asoc->tsn_out_at = 0; |
| 8129 | asoc->tsn_out_wrapped = 1; |
| 8130 | } |
| 8131 | asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.tsn; |
| 8132 | asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.sid; |
| 8133 | asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.mid; |
| 8134 | asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size; |
| 8135 | asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags; |
| 8136 | asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb; |
| 8137 | asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at; |
| 8138 | asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2; |
| 8139 | asoc->tsn_out_at++; |
| 8140 | #endif |
| 8141 | if (stcb->asoc.idata_supported == 0) { |
| 8142 | dchkh->ch.chunk_type = SCTP_DATA; |
| 8143 | dchkh->ch.chunk_flags = chk->rec.data.rcv_flags; |
| 8144 | dchkh->dp.tsn = htonl(chk->rec.data.tsn); |
| 8145 | dchkh->dp.sid = htons(strq->sid); |
| 8146 | dchkh->dp.ssn = htons((uint16_t)chk->rec.data.mid); |
| 8147 | dchkh->dp.ppid = chk->rec.data.ppid; |
| 8148 | dchkh->ch.chunk_length = htons(chk->send_size); |
| 8149 | } else { |
| 8150 | ndchkh->ch.chunk_type = SCTP_IDATA; |
| 8151 | ndchkh->ch.chunk_flags = chk->rec.data.rcv_flags; |
| 8152 | ndchkh->dp.tsn = htonl(chk->rec.data.tsn); |
| 8153 | ndchkh->dp.sid = htons(strq->sid); |
| 8154 | ndchkh->dp.reserved = htons(0); |
| 8155 | ndchkh->dp.mid = htonl(chk->rec.data.mid); |
| 8156 | if (sp->fsn == 0) |
| 8157 | ndchkh->dp.ppid_fsn.ppid = chk->rec.data.ppid; |
| 8158 | else |
| 8159 | ndchkh->dp.ppid_fsn.fsn = htonl(sp->fsn); |
| 8160 | sp->fsn++; |
| 8161 | ndchkh->ch.chunk_length = htons(chk->send_size); |
| 8162 | } |
| 8163 | /* Now advance the chk->send_size by the actual pad needed. */ |
| 8164 | if (chk->send_size < SCTP_SIZE32(chk->book_size)) { |
| 8165 | /* need a pad */ |
| 8166 | struct mbuf *lm; |
| 8167 | int pads; |
| 8168 |
| 8169 | pads = SCTP_SIZE32(chk->book_size) - chk->send_size; |
| 8170 | lm = sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf); |
| 8171 | if (lm != NULL) { |
| 8172 | chk->last_mbuf = lm; |
| 8173 | chk->pad_inplace = 1; |
| 8174 | } |
| 8175 | chk->send_size += pads; |
| 8176 | } |
| 8177 | if (PR_SCTP_ENABLED(chk->flags)) { |
| 8178 | asoc->pr_sctp_cnt++; |
| 8179 | } |
| 8180 | if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) { |
| 8181 | /* All done pull and kill the message */ |
| 8182 | if (sp->put_last_out == 0) { |
| 8183 | SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n"); |
| 8184 | SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n", |
| 8185 | sp->sender_all_done, |
| 8186 | sp->length, |
| 8187 | sp->msg_is_complete, |
| 8188 | sp->put_last_out, |
| 8189 | send_lock_up); |
| 8190 | } |
| 8191 | if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) { |
| 8192 | SCTP_TCB_SEND_LOCK(stcb); |
| 8193 | send_lock_up = 1; |
| 8194 | } |
| 8195 | atomic_subtract_int(&asoc->stream_queue_cnt, 1); |
| 8196 | TAILQ_REMOVE(&strq->outqueue, sp, next); |
| 8197 | stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up); |
| 8198 | if ((strq->state == SCTP_STREAM_RESET_PENDING) && |
| 8199 | (strq->chunks_on_queues == 0) && |
| 8200 | TAILQ_EMPTY(&strq->outqueue)) { |
| 8201 | stcb->asoc.trigger_reset = 1; |
| 8202 | } |
| 8203 | if (sp->net) { |
| 8204 | sctp_free_remote_addr(sp->net); |
| 8205 | sp->net = NULL; |
| 8206 | } |
| 8207 | if (sp->data) { |
| 8208 | sctp_m_freem(sp->data); |
| 8209 | sp->data = NULL; |
| 8210 | } |
| 8211 | sctp_free_a_strmoq(stcb, sp, so_locked); |
| 8212 | } |
| 8213 | asoc->chunks_on_out_queue++; |
| 8214 | strq->chunks_on_queues++; |
| 8215 | TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next); |
| 8216 | asoc->send_queue_cnt++; |
| 8217 | out_of: |
| 8218 | if (send_lock_up) { |
| 8219 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 8220 | } |
| 8221 | return (to_move); |
| 8222 | } |
| 8223 |
| 8224 |
| 8225 | static void |
| 8226 | sctp_fill_outqueue(struct sctp_tcb *stcb, |
| 8227 | struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now, int so_locked |
| 8228 | #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) |
| 8229 | SCTP_UNUSED |
| 8230 | #endif |
| 8231 | ) |
| 8232 | { |
| 8233 | struct sctp_association *asoc; |
| 8234 | struct sctp_stream_out *strq; |
| 8235 | uint32_t space_left, moved, total_moved; |
| 8236 | int bail, giveup; |
| 8237 |
| 8238 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 8239 | asoc = &stcb->asoc; |
| 8240 | total_moved = 0; |
| 8241 | switch (net->ro._l_addr.sa.sa_family) { |
| 8242 | #ifdef INET |
| 8243 | case AF_INET: |
| 8244 | space_left = net->mtu - SCTP_MIN_V4_OVERHEAD; |
| 8245 | break; |
| 8246 | #endif |
| 8247 | #ifdef INET6 |
| 8248 | case AF_INET6: |
| 8249 | space_left = net->mtu - SCTP_MIN_OVERHEAD; |
| 8250 | break; |
| 8251 | #endif |
| 8252 | #if defined(__Userspace__) |
| 8253 | case AF_CONN: |
| 8254 | space_left = net->mtu - sizeof(struct sctphdr); |
| 8255 | break; |
| 8256 | #endif |
| 8257 | default: |
| 8258 | /* TSNH */ |
| 8259 | space_left = net->mtu; |
| 8260 | break; |
| 8261 | } |
| 8262 | /* Need an allowance for the data chunk header too */ |
| 8263 | space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb); |
| 8264 |
| 8265 | /* must make even word boundary */ |
| 8266 | space_left &= 0xfffffffc; |
| 8267 | strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc); |
| 8268 | giveup = 0; |
| 8269 | bail = 0; |
| 8270 | while ((space_left > 0) && (strq != NULL)) { |
| 8271 | moved = sctp_move_to_outqueue(stcb, strq, space_left, frag_point, |
| 8272 | &giveup, eeor_mode, &bail, so_locked); |
| 8273 | stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, moved); |
| 8274 | if ((giveup != 0) || (bail != 0)) { |
| 8275 | break; |
| 8276 | } |
| 8277 | strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc); |
| 8278 | total_moved += moved; |
| 8279 | space_left -= moved; |
| 8280 | if (space_left >= SCTP_DATA_CHUNK_OVERHEAD(stcb)) { |
| 8281 | space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb); |
| 8282 | } else { |
| 8283 | space_left = 0; |
| 8284 | } |
| 8285 | space_left &= 0xfffffffc; |
| 8286 | } |
| 8287 | if (bail != 0) |
| 8288 | *quit_now = 1; |
| 8289 |
| 8290 | stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc); |
| 8291 |
| 8292 | if (total_moved == 0) { |
| 8293 | if ((stcb->asoc.sctp_cmt_on_off == 0) && |
| 8294 | (net == stcb->asoc.primary_destination)) { |
| 8295 | /* ran dry for primary network net */ |
| 8296 | SCTP_STAT_INCR(sctps_primary_randry); |
| 8297 | } else if (stcb->asoc.sctp_cmt_on_off > 0) { |
| 8298 | /* ran dry with CMT on */ |
| 8299 | SCTP_STAT_INCR(sctps_cmt_randry); |
| 8300 | } |
| 8301 | } |
| 8302 | } |
| 8303 |
| 8304 | void |
| 8305 | sctp_fix_ecn_echo(struct sctp_association *asoc) |
| 8306 | { |
| 8307 | struct sctp_tmit_chunk *chk; |
| 8308 |
| 8309 | TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { |
| 8310 | if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) { |
| 8311 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 8312 | } |
| 8313 | } |
| 8314 | } |
| 8315 |
| 8316 | void |
| 8317 | sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net) |
| 8318 | { |
| 8319 | struct sctp_association *asoc; |
| 8320 | struct sctp_tmit_chunk *chk; |
| 8321 | struct sctp_stream_queue_pending *sp; |
| 8322 | unsigned int i; |
| 8323 |
| 8324 | if (net == NULL) { |
| 8325 | return; |
| 8326 | } |
| 8327 | asoc = &stcb->asoc; |
| 8328 | for (i = 0; i < stcb->asoc.streamoutcnt; i++) { |
| 8329 | TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) { |
| 8330 | if (sp->net == net) { |
| 8331 | sctp_free_remote_addr(sp->net); |
| 8332 | sp->net = NULL; |
| 8333 | } |
| 8334 | } |
| 8335 | } |
| 8336 | TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) { |
| 8337 | if (chk->whoTo == net) { |
| 8338 | sctp_free_remote_addr(chk->whoTo); |
| 8339 | chk->whoTo = NULL; |
| 8340 | } |
| 8341 | } |
| 8342 | } |
| 8343 |
| 8344 | int |
| 8345 | sctp_med_chunk_output(struct sctp_inpcb *inp, |
| 8346 | struct sctp_tcb *stcb, |
| 8347 | struct sctp_association *asoc, |
| 8348 | int *num_out, |
| 8349 | int *reason_code, |
| 8350 | int control_only, int from_where, |
| 8351 | struct timeval *now, int *now_filled, int frag_point, int so_locked |
| 8352 | #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) |
| 8353 | SCTP_UNUSED |
| 8354 | #endif |
| 8355 | ) |
| 8356 | { |
| 8357 | /** |
| 8358 | * Ok this is the generic chunk service queue. we must do the |
| 8359 | * following: |
| 8360 | * - Service the stream queue that is next, moving any |
| 8361 | * message (note I must get a complete message i.e. FIRST/MIDDLE and |
| 8362 | * LAST to the out queue in one pass) and assigning TSN's. This |
| 8363 | * only applys though if the peer does not support NDATA. For NDATA |
| 8364 | * chunks its ok to not send the entire message ;-) |
| 8365 | * - Check to see if the cwnd/rwnd allows any output, if so we go ahead and |
| 8366 | * fomulate and send the low level chunks. Making sure to combine |
| 8367 | * any control in the control chunk queue also. |
| 8368 | */ |
| 8369 | struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL; |
| 8370 | struct mbuf *outchain, *endoutchain; |
| 8371 | struct sctp_tmit_chunk *chk, *nchk; |
| 8372 |
| 8373 | /* temp arrays for unlinking */ |
| 8374 | struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING]; |
| 8375 | int no_fragmentflg, error; |
| 8376 | unsigned int max_rwnd_per_dest, max_send_per_dest; |
| 8377 | int one_chunk, hbflag, skip_data_for_this_net; |
| 8378 | int asconf, cookie, no_out_cnt; |
| 8379 | int bundle_at, ctl_cnt, no_data_chunks, eeor_mode; |
| 8380 | unsigned int mtu, r_mtu, omtu, mx_mtu, to_out; |
| 8381 | int tsns_sent = 0; |
| 8382 | uint32_t auth_offset; |
| 8383 | struct sctp_auth_chunk *auth; |
| 8384 | uint16_t auth_keyid; |
| 8385 | int override_ok = 1; |
| 8386 | int skip_fill_up = 0; |
| 8387 | int data_auth_reqd = 0; |
| 8388 | /* JRS 5/14/07 - Add flag for whether a heartbeat is sent to |
| 8389 | the destination. */ |
| 8390 | int quit_now = 0; |
| 8391 |
| 8392 | #if defined(__APPLE__) |
| 8393 | if (so_locked) { |
| 8394 | sctp_lock_assert(SCTP_INP_SO(inp)); |
| 8395 | } else { |
| 8396 | sctp_unlock_assert(SCTP_INP_SO(inp)); |
| 8397 | } |
| 8398 | #endif |
| 8399 | *num_out = 0; |
| 8400 | *reason_code = 0; |
| 8401 | auth_keyid = stcb->asoc.authinfo.active_keyid; |
| 8402 | if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) || |
| 8403 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) || |
| 8404 | (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) { |
| 8405 | eeor_mode = 1; |
| 8406 | } else { |
| 8407 | eeor_mode = 0; |
| 8408 | } |
| 8409 | ctl_cnt = no_out_cnt = asconf = cookie = 0; |
| 8410 | /* |
| 8411 | * First lets prime the pump. For each destination, if there is room |
| 8412 | * in the flight size, attempt to pull an MTU's worth out of the |
| 8413 | * stream queues into the general send_queue |
| 8414 | */ |
| 8415 | #ifdef SCTP_AUDITING_ENABLED |
| 8416 | sctp_audit_log(0xC2, 2); |
| 8417 | #endif |
| 8418 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 8419 | hbflag = 0; |
| 8420 | if (control_only) |
| 8421 | no_data_chunks = 1; |
| 8422 | else |
| 8423 | no_data_chunks = 0; |
| 8424 |
| 8425 | /* Nothing to possible to send? */ |
| 8426 | if ((TAILQ_EMPTY(&asoc->control_send_queue) || |
| 8427 | (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) && |
| 8428 | TAILQ_EMPTY(&asoc->asconf_send_queue) && |
| 8429 | TAILQ_EMPTY(&asoc->send_queue) && |
| 8430 | sctp_is_there_unsent_data(stcb, so_locked) == 0) { |
| 8431 | nothing_to_send: |
| 8432 | *reason_code = 9; |
| 8433 | return (0); |
| 8434 | } |
| 8435 | if (asoc->peers_rwnd == 0) { |
| 8436 | /* No room in peers rwnd */ |
| 8437 | *reason_code = 1; |
| 8438 | if (asoc->total_flight > 0) { |
| 8439 | /* we are allowed one chunk in flight */ |
| 8440 | no_data_chunks = 1; |
| 8441 | } |
| 8442 | } |
| 8443 | if (stcb->asoc.ecn_echo_cnt_onq) { |
| 8444 | /* Record where a sack goes, if any */ |
| 8445 | if (no_data_chunks && |
| 8446 | (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) { |
| 8447 | /* Nothing but ECNe to send - we don't do that */ |
| 8448 | goto nothing_to_send; |
| 8449 | } |
| 8450 | TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { |
| 8451 | if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) || |
| 8452 | (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) { |
| 8453 | sack_goes_to = chk->whoTo; |
| 8454 | break; |
| 8455 | } |
| 8456 | } |
| 8457 | } |
| 8458 | max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets); |
| 8459 | if (stcb->sctp_socket) |
| 8460 | max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets; |
| 8461 | else |
| 8462 | max_send_per_dest = 0; |
| 8463 | if (no_data_chunks == 0) { |
| 8464 | /* How many non-directed chunks are there? */ |
| 8465 | TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) { |
| 8466 | if (chk->whoTo == NULL) { |
| 8467 | /* We already have non-directed |
| 8468 | * chunks on the queue, no need |
| 8469 | * to do a fill-up. |
| 8470 | */ |
| 8471 | skip_fill_up = 1; |
| 8472 | break; |
| 8473 | } |
| 8474 | } |
| 8475 |
| 8476 | } |
| 8477 | if ((no_data_chunks == 0) && |
| 8478 | (skip_fill_up == 0) && |
| 8479 | (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) { |
| 8480 | TAILQ_FOREACH(net, &asoc->nets, sctp_next) { |
| 8481 | /* |
| 8482 | * This for loop we are in takes in |
| 8483 | * each net, if its's got space in cwnd and |
| 8484 | * has data sent to it (when CMT is off) then it |
| 8485 | * calls sctp_fill_outqueue for the net. This gets |
| 8486 | * data on the send queue for that network. |
| 8487 | * |
| 8488 | * In sctp_fill_outqueue TSN's are assigned and |
| 8489 | * data is copied out of the stream buffers. Note |
| 8490 | * mostly copy by reference (we hope). |
| 8491 | */ |
| 8492 | net->window_probe = 0; |
| 8493 | if ((net != stcb->asoc.alternate) && |
| 8494 | ((net->dest_state & SCTP_ADDR_PF) || |
| 8495 | (!(net->dest_state & SCTP_ADDR_REACHABLE)) || |
| 8496 | (net->dest_state & SCTP_ADDR_UNCONFIRMED))) { |
| 8497 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 8498 | sctp_log_cwnd(stcb, net, 1, |
| 8499 | SCTP_CWND_LOG_FILL_OUTQ_CALLED); |
| 8500 | } |
| 8501 | continue; |
| 8502 | } |
| 8503 | if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) && |
| 8504 | (net->flight_size == 0)) { |
| 8505 | (*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins)(stcb, net); |
| 8506 | } |
| 8507 | if (net->flight_size >= net->cwnd) { |
| 8508 | /* skip this network, no room - can't fill */ |
| 8509 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 8510 | sctp_log_cwnd(stcb, net, 3, |
| 8511 | SCTP_CWND_LOG_FILL_OUTQ_CALLED); |
| 8512 | } |
| 8513 | continue; |
| 8514 | } |
| 8515 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 8516 | sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED); |
| 8517 | } |
| 8518 | sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked); |
| 8519 | if (quit_now) { |
| 8520 | /* memory alloc failure */ |
| 8521 | no_data_chunks = 1; |
| 8522 | break; |
| 8523 | } |
| 8524 | } |
| 8525 | } |
| 8526 | /* now service each destination and send out what we can for it */ |
| 8527 | /* Nothing to send? */ |
| 8528 | if (TAILQ_EMPTY(&asoc->control_send_queue) && |
| 8529 | TAILQ_EMPTY(&asoc->asconf_send_queue) && |
| 8530 | TAILQ_EMPTY(&asoc->send_queue)) { |
| 8531 | *reason_code = 8; |
| 8532 | return (0); |
| 8533 | } |
| 8534 |
| 8535 | if (asoc->sctp_cmt_on_off > 0) { |
| 8536 | /* get the last start point */ |
| 8537 | start_at = asoc->last_net_cmt_send_started; |
| 8538 | if (start_at == NULL) { |
| 8539 | /* null so to beginning */ |
| 8540 | start_at = TAILQ_FIRST(&asoc->nets); |
| 8541 | } else { |
| 8542 | start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next); |
| 8543 | if (start_at == NULL) { |
| 8544 | start_at = TAILQ_FIRST(&asoc->nets); |
| 8545 | } |
| 8546 | } |
| 8547 | asoc->last_net_cmt_send_started = start_at; |
| 8548 | } else { |
| 8549 | start_at = TAILQ_FIRST(&asoc->nets); |
| 8550 | } |
| 8551 | TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { |
| 8552 | if (chk->whoTo == NULL) { |
| 8553 | if (asoc->alternate) { |
| 8554 | chk->whoTo = asoc->alternate; |
| 8555 | } else { |
| 8556 | chk->whoTo = asoc->primary_destination; |
| 8557 | } |
| 8558 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 8559 | } |
| 8560 | } |
| 8561 | old_start_at = NULL; |
| 8562 | again_one_more_time: |
| 8563 | for (net = start_at ; net != NULL; net = TAILQ_NEXT(net, sctp_next)) { |
| 8564 | /* how much can we send? */ |
| 8565 | /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */ |
| 8566 | if (old_start_at && (old_start_at == net)) { |
| 8567 | /* through list ocmpletely. */ |
| 8568 | break; |
| 8569 | } |
| 8570 | tsns_sent = 0xa; |
| 8571 | if (TAILQ_EMPTY(&asoc->control_send_queue) && |
| 8572 | TAILQ_EMPTY(&asoc->asconf_send_queue) && |
| 8573 | (net->flight_size >= net->cwnd)) { |
| 8574 | /* Nothing on control or asconf and flight is full, we can skip |
| 8575 | * even in the CMT case. |
| 8576 | */ |
| 8577 | continue; |
| 8578 | } |
| 8579 | bundle_at = 0; |
| 8580 | endoutchain = outchain = NULL; |
| 8581 | auth = NULL; |
| 8582 | auth_offset = 0; |
| 8583 | no_fragmentflg = 1; |
| 8584 | one_chunk = 0; |
| 8585 | if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { |
| 8586 | skip_data_for_this_net = 1; |
| 8587 | } else { |
| 8588 | skip_data_for_this_net = 0; |
| 8589 | } |
| 8590 | switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) { |
| 8591 | #ifdef INET |
| 8592 | case AF_INET: |
| 8593 | mtu = net->mtu - SCTP_MIN_V4_OVERHEAD; |
| 8594 | break; |
| 8595 | #endif |
| 8596 | #ifdef INET6 |
| 8597 | case AF_INET6: |
| 8598 | mtu = net->mtu - SCTP_MIN_OVERHEAD; |
| 8599 | break; |
| 8600 | #endif |
| 8601 | #if defined(__Userspace__) |
| 8602 | case AF_CONN: |
| 8603 | mtu = net->mtu - sizeof(struct sctphdr); |
| 8604 | break; |
| 8605 | #endif |
| 8606 | default: |
| 8607 | /* TSNH */ |
| 8608 | mtu = net->mtu; |
| 8609 | break; |
| 8610 | } |
| 8611 | mx_mtu = mtu; |
| 8612 | to_out = 0; |
| 8613 | if (mtu > asoc->peers_rwnd) { |
| 8614 | if (asoc->total_flight > 0) { |
| 8615 | /* We have a packet in flight somewhere */ |
| 8616 | r_mtu = asoc->peers_rwnd; |
| 8617 | } else { |
| 8618 | /* We are always allowed to send one MTU out */ |
| 8619 | one_chunk = 1; |
| 8620 | r_mtu = mtu; |
| 8621 | } |
| 8622 | } else { |
| 8623 | r_mtu = mtu; |
| 8624 | } |
| 8625 | error = 0; |
| 8626 | /************************/ |
| 8627 | /* ASCONF transmission */ |
| 8628 | /************************/ |
| 8629 | /* Now first lets go through the asconf queue */ |
| 8630 | TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) { |
| 8631 | if (chk->rec.chunk_id.id != SCTP_ASCONF) { |
| 8632 | continue; |
| 8633 | } |
| 8634 | if (chk->whoTo == NULL) { |
| 8635 | if (asoc->alternate == NULL) { |
| 8636 | if (asoc->primary_destination != net) { |
| 8637 | break; |
| 8638 | } |
| 8639 | } else { |
| 8640 | if (asoc->alternate != net) { |
| 8641 | break; |
| 8642 | } |
| 8643 | } |
| 8644 | } else { |
| 8645 | if (chk->whoTo != net) { |
| 8646 | break; |
| 8647 | } |
| 8648 | } |
| 8649 | if (chk->data == NULL) { |
| 8650 | break; |
| 8651 | } |
| 8652 | if (chk->sent != SCTP_DATAGRAM_UNSENT && |
| 8653 | chk->sent != SCTP_DATAGRAM_RESEND) { |
| 8654 | break; |
| 8655 | } |
| 8656 | /* |
| 8657 | * if no AUTH is yet included and this chunk |
| 8658 | * requires it, make sure to account for it. We |
| 8659 | * don't apply the size until the AUTH chunk is |
| 8660 | * actually added below in case there is no room for |
| 8661 | * this chunk. NOTE: we overload the use of "omtu" |
| 8662 | * here |
| 8663 | */ |
| 8664 | if ((auth == NULL) && |
| 8665 | sctp_auth_is_required_chunk(chk->rec.chunk_id.id, |
| 8666 | stcb->asoc.peer_auth_chunks)) { |
| 8667 | omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); |
| 8668 | } else |
| 8669 | omtu = 0; |
| 8670 | /* Here we do NOT factor the r_mtu */ |
| 8671 | if ((chk->send_size < (int)(mtu - omtu)) || |
| 8672 | (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) { |
| 8673 | /* |
| 8674 | * We probably should glom the mbuf chain |
| 8675 | * from the chk->data for control but the |
| 8676 | * problem is it becomes yet one more level |
| 8677 | * of tracking to do if for some reason |
| 8678 | * output fails. Then I have got to |
| 8679 | * reconstruct the merged control chain.. el |
| 8680 | * yucko.. for now we take the easy way and |
| 8681 | * do the copy |
| 8682 | */ |
| 8683 | /* |
| 8684 | * Add an AUTH chunk, if chunk requires it |
| 8685 | * save the offset into the chain for AUTH |
| 8686 | */ |
| 8687 | if ((auth == NULL) && |
| 8688 | (sctp_auth_is_required_chunk(chk->rec.chunk_id.id, |
| 8689 | stcb->asoc.peer_auth_chunks))) { |
| 8690 | outchain = sctp_add_auth_chunk(outchain, |
| 8691 | &endoutchain, |
| 8692 | &auth, |
| 8693 | &auth_offset, |
| 8694 | stcb, |
| 8695 | chk->rec.chunk_id.id); |
| 8696 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 8697 | } |
| 8698 | outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, |
| 8699 | (int)chk->rec.chunk_id.can_take_data, |
| 8700 | chk->send_size, chk->copy_by_ref); |
| 8701 | if (outchain == NULL) { |
| 8702 | *reason_code = 8; |
| 8703 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 8704 | return (ENOMEM); |
| 8705 | } |
| 8706 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 8707 | /* update our MTU size */ |
| 8708 | if (mtu > (chk->send_size + omtu)) |
| 8709 | mtu -= (chk->send_size + omtu); |
| 8710 | else |
| 8711 | mtu = 0; |
| 8712 | to_out += (chk->send_size + omtu); |
| 8713 | /* Do clear IP_DF ? */ |
| 8714 | if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) { |
| 8715 | no_fragmentflg = 0; |
| 8716 | } |
| 8717 | if (chk->rec.chunk_id.can_take_data) |
| 8718 | chk->data = NULL; |
| 8719 | /* |
| 8720 | * set hb flag since we can |
| 8721 | * use these for RTO |
| 8722 | */ |
| 8723 | hbflag = 1; |
| 8724 | asconf = 1; |
| 8725 | /* |
| 8726 | * should sysctl this: don't |
| 8727 | * bundle data with ASCONF |
| 8728 | * since it requires AUTH |
| 8729 | */ |
| 8730 | no_data_chunks = 1; |
| 8731 | chk->sent = SCTP_DATAGRAM_SENT; |
| 8732 | if (chk->whoTo == NULL) { |
| 8733 | chk->whoTo = net; |
| 8734 | atomic_add_int(&net->ref_count, 1); |
| 8735 | } |
| 8736 | chk->snd_count++; |
| 8737 | if (mtu == 0) { |
| 8738 | /* |
| 8739 | * Ok we are out of room but we can |
| 8740 | * output without effecting the |
| 8741 | * flight size since this little guy |
| 8742 | * is a control only packet. |
| 8743 | */ |
| 8744 | sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net); |
| 8745 | /* |
| 8746 | * do NOT clear the asconf |
| 8747 | * flag as it is used to do |
| 8748 | * appropriate source address |
| 8749 | * selection. |
| 8750 | */ |
| 8751 | if (*now_filled == 0) { |
| 8752 | (void)SCTP_GETTIME_TIMEVAL(now); |
| 8753 | *now_filled = 1; |
| 8754 | } |
| 8755 | net->last_sent_time = *now; |
| 8756 | hbflag = 0; |
| 8757 | if ((error = sctp_lowlevel_chunk_output(inp, stcb, net, |
| 8758 | (struct sockaddr *)&net->ro._l_addr, |
| 8759 | outchain, auth_offset, auth, |
| 8760 | stcb->asoc.authinfo.active_keyid, |
| 8761 | no_fragmentflg, 0, asconf, |
| 8762 | inp->sctp_lport, stcb->rport, |
| 8763 | htonl(stcb->asoc.peer_vtag), |
| 8764 | net->port, NULL, |
| 8765 | #if defined(__FreeBSD__) |
| 8766 | 0, 0, |
| 8767 | #endif |
| 8768 | so_locked))) { |
| 8769 | /* error, we could not output */ |
| 8770 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error); |
| 8771 | if (from_where == 0) { |
| 8772 | SCTP_STAT_INCR(sctps_lowlevelerrusr); |
| 8773 | } |
| 8774 | if (error == ENOBUFS) { |
| 8775 | asoc->ifp_had_enobuf = 1; |
| 8776 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 8777 | } |
| 8778 | /* error, could not output */ |
| 8779 | if (error == EHOSTUNREACH) { |
| 8780 | /* |
| 8781 | * Destination went |
| 8782 | * unreachable |
| 8783 | * during this send |
| 8784 | */ |
| 8785 | sctp_move_chunks_from_net(stcb, net); |
| 8786 | } |
| 8787 | *reason_code = 7; |
| 8788 | break; |
| 8789 | } else { |
| 8790 | asoc->ifp_had_enobuf = 0; |
| 8791 | } |
| 8792 | /* |
| 8793 | * increase the number we sent, if a |
| 8794 | * cookie is sent we don't tell them |
| 8795 | * any was sent out. |
| 8796 | */ |
| 8797 | outchain = endoutchain = NULL; |
| 8798 | auth = NULL; |
| 8799 | auth_offset = 0; |
| 8800 | if (!no_out_cnt) |
| 8801 | *num_out += ctl_cnt; |
| 8802 | /* recalc a clean slate and setup */ |
| 8803 | switch (net->ro._l_addr.sa.sa_family) { |
| 8804 | #ifdef INET |
| 8805 | case AF_INET: |
| 8806 | mtu = net->mtu - SCTP_MIN_V4_OVERHEAD; |
| 8807 | break; |
| 8808 | #endif |
| 8809 | #ifdef INET6 |
| 8810 | case AF_INET6: |
| 8811 | mtu = net->mtu - SCTP_MIN_OVERHEAD; |
| 8812 | break; |
| 8813 | #endif |
| 8814 | #if defined(__Userspace__) |
| 8815 | case AF_CONN: |
| 8816 | mtu = net->mtu - sizeof(struct sctphdr); |
| 8817 | break; |
| 8818 | #endif |
| 8819 | default: |
| 8820 | /* TSNH */ |
| 8821 | mtu = net->mtu; |
| 8822 | break; |
| 8823 | } |
| 8824 | to_out = 0; |
| 8825 | no_fragmentflg = 1; |
| 8826 | } |
| 8827 | } |
| 8828 | } |
| 8829 | if (error != 0) { |
| 8830 | /* try next net */ |
| 8831 | continue; |
| 8832 | } |
| 8833 | /************************/ |
| 8834 | /* Control transmission */ |
| 8835 | /************************/ |
| 8836 | /* Now first lets go through the control queue */ |
| 8837 | TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { |
| 8838 | if ((sack_goes_to) && |
| 8839 | (chk->rec.chunk_id.id == SCTP_ECN_ECHO) && |
| 8840 | (chk->whoTo != sack_goes_to)) { |
| 8841 | /* |
| 8842 | * if we have a sack in queue, and we are looking at an |
| 8843 | * ecn echo that is NOT queued to where the sack is going.. |
| 8844 | */ |
| 8845 | if (chk->whoTo == net) { |
| 8846 | /* Don't transmit it to where its going (current net) */ |
| 8847 | continue; |
| 8848 | } else if (sack_goes_to == net) { |
| 8849 | /* But do transmit it to this address */ |
| 8850 | goto skip_net_check; |
| 8851 | } |
| 8852 | } |
| 8853 | if (chk->whoTo == NULL) { |
| 8854 | if (asoc->alternate == NULL) { |
| 8855 | if (asoc->primary_destination != net) { |
| 8856 | continue; |
| 8857 | } |
| 8858 | } else { |
| 8859 | if (asoc->alternate != net) { |
| 8860 | continue; |
| 8861 | } |
| 8862 | } |
| 8863 | } else { |
| 8864 | if (chk->whoTo != net) { |
| 8865 | continue; |
| 8866 | } |
| 8867 | } |
| 8868 | skip_net_check: |
| 8869 | if (chk->data == NULL) { |
| 8870 | continue; |
| 8871 | } |
| 8872 | if (chk->sent != SCTP_DATAGRAM_UNSENT) { |
| 8873 | /* |
| 8874 | * It must be unsent. Cookies and ASCONF's |
| 8875 | * hang around but there timers will force |
| 8876 | * when marked for resend. |
| 8877 | */ |
| 8878 | continue; |
| 8879 | } |
| 8880 | /* |
| 8881 | * if no AUTH is yet included and this chunk |
| 8882 | * requires it, make sure to account for it. We |
| 8883 | * don't apply the size until the AUTH chunk is |
| 8884 | * actually added below in case there is no room for |
| 8885 | * this chunk. NOTE: we overload the use of "omtu" |
| 8886 | * here |
| 8887 | */ |
| 8888 | if ((auth == NULL) && |
| 8889 | sctp_auth_is_required_chunk(chk->rec.chunk_id.id, |
| 8890 | stcb->asoc.peer_auth_chunks)) { |
| 8891 | omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); |
| 8892 | } else |
| 8893 | omtu = 0; |
| 8894 | /* Here we do NOT factor the r_mtu */ |
| 8895 | if ((chk->send_size <= (int)(mtu - omtu)) || |
| 8896 | (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) { |
| 8897 | /* |
| 8898 | * We probably should glom the mbuf chain |
| 8899 | * from the chk->data for control but the |
| 8900 | * problem is it becomes yet one more level |
| 8901 | * of tracking to do if for some reason |
| 8902 | * output fails. Then I have got to |
| 8903 | * reconstruct the merged control chain.. el |
| 8904 | * yucko.. for now we take the easy way and |
| 8905 | * do the copy |
| 8906 | */ |
| 8907 | /* |
| 8908 | * Add an AUTH chunk, if chunk requires it |
| 8909 | * save the offset into the chain for AUTH |
| 8910 | */ |
| 8911 | if ((auth == NULL) && |
| 8912 | (sctp_auth_is_required_chunk(chk->rec.chunk_id.id, |
| 8913 | stcb->asoc.peer_auth_chunks))) { |
| 8914 | outchain = sctp_add_auth_chunk(outchain, |
| 8915 | &endoutchain, |
| 8916 | &auth, |
| 8917 | &auth_offset, |
| 8918 | stcb, |
| 8919 | chk->rec.chunk_id.id); |
| 8920 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 8921 | } |
| 8922 | outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, |
| 8923 | (int)chk->rec.chunk_id.can_take_data, |
| 8924 | chk->send_size, chk->copy_by_ref); |
| 8925 | if (outchain == NULL) { |
| 8926 | *reason_code = 8; |
| 8927 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 8928 | return (ENOMEM); |
| 8929 | } |
| 8930 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 8931 | /* update our MTU size */ |
| 8932 | if (mtu > (chk->send_size + omtu)) |
| 8933 | mtu -= (chk->send_size + omtu); |
| 8934 | else |
| 8935 | mtu = 0; |
| 8936 | to_out += (chk->send_size + omtu); |
| 8937 | /* Do clear IP_DF ? */ |
| 8938 | if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) { |
| 8939 | no_fragmentflg = 0; |
| 8940 | } |
| 8941 | if (chk->rec.chunk_id.can_take_data) |
| 8942 | chk->data = NULL; |
| 8943 | /* Mark things to be removed, if needed */ |
| 8944 | if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) || |
| 8945 | (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */ |
| 8946 | (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) || |
| 8947 | (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) || |
| 8948 | (chk->rec.chunk_id.id == SCTP_SHUTDOWN) || |
| 8949 | (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) || |
| 8950 | (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) || |
| 8951 | (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) || |
| 8952 | (chk->rec.chunk_id.id == SCTP_ECN_CWR) || |
| 8953 | (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) || |
| 8954 | (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) { |
| 8955 | if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) { |
| 8956 | hbflag = 1; |
| 8957 | } |
| 8958 | /* remove these chunks at the end */ |
| 8959 | if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) || |
| 8960 | (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) { |
| 8961 | /* turn off the timer */ |
| 8962 | if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) { |
| 8963 | sctp_timer_stop(SCTP_TIMER_TYPE_RECV, |
| 8964 | inp, stcb, net, |
| 8965 | SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1); |
| 8966 | } |
| 8967 | } |
| 8968 | ctl_cnt++; |
| 8969 | } else { |
| 8970 | /* |
| 8971 | * Other chunks, since they have |
| 8972 | * timers running (i.e. COOKIE) |
| 8973 | * we just "trust" that it |
| 8974 | * gets sent or retransmitted. |
| 8975 | */ |
| 8976 | ctl_cnt++; |
| 8977 | if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) { |
| 8978 | cookie = 1; |
| 8979 | no_out_cnt = 1; |
| 8980 | } else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) { |
| 8981 | /* |
| 8982 | * Increment ecne send count here |
| 8983 | * this means we may be over-zealous in |
| 8984 | * our counting if the send fails, but its |
| 8985 | * the best place to do it (we used to do |
| 8986 | * it in the queue of the chunk, but that did |
| 8987 | * not tell how many times it was sent. |
| 8988 | */ |
| 8989 | SCTP_STAT_INCR(sctps_sendecne); |
| 8990 | } |
| 8991 | chk->sent = SCTP_DATAGRAM_SENT; |
| 8992 | if (chk->whoTo == NULL) { |
| 8993 | chk->whoTo = net; |
| 8994 | atomic_add_int(&net->ref_count, 1); |
| 8995 | } |
| 8996 | chk->snd_count++; |
| 8997 | } |
| 8998 | if (mtu == 0) { |
| 8999 | /* |
| 9000 | * Ok we are out of room but we can |
| 9001 | * output without effecting the |
| 9002 | * flight size since this little guy |
| 9003 | * is a control only packet. |
| 9004 | */ |
| 9005 | if (asconf) { |
| 9006 | sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net); |
| 9007 | /* |
| 9008 | * do NOT clear the asconf |
| 9009 | * flag as it is used to do |
| 9010 | * appropriate source address |
| 9011 | * selection. |
| 9012 | */ |
| 9013 | } |
| 9014 | if (cookie) { |
| 9015 | sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net); |
| 9016 | cookie = 0; |
| 9017 | } |
| 9018 | /* Only HB or ASCONF advances time */ |
| 9019 | if (hbflag) { |
| 9020 | if (*now_filled == 0) { |
| 9021 | (void)SCTP_GETTIME_TIMEVAL(now); |
| 9022 | *now_filled = 1; |
| 9023 | } |
| 9024 | net->last_sent_time = *now; |
| 9025 | hbflag = 0; |
| 9026 | } |
| 9027 | if ((error = sctp_lowlevel_chunk_output(inp, stcb, net, |
| 9028 | (struct sockaddr *)&net->ro._l_addr, |
| 9029 | outchain, |
| 9030 | auth_offset, auth, |
| 9031 | stcb->asoc.authinfo.active_keyid, |
| 9032 | no_fragmentflg, 0, asconf, |
| 9033 | inp->sctp_lport, stcb->rport, |
| 9034 | htonl(stcb->asoc.peer_vtag), |
| 9035 | net->port, NULL, |
| 9036 | #if defined(__FreeBSD__) |
| 9037 | 0, 0, |
| 9038 | #endif |
| 9039 | so_locked))) { |
| 9040 | /* error, we could not output */ |
| 9041 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error); |
| 9042 | if (from_where == 0) { |
| 9043 | SCTP_STAT_INCR(sctps_lowlevelerrusr); |
| 9044 | } |
| 9045 | if (error == ENOBUFS) { |
| 9046 | asoc->ifp_had_enobuf = 1; |
| 9047 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 9048 | } |
| 9049 | if (error == EHOSTUNREACH) { |
| 9050 | /* |
| 9051 | * Destination went |
| 9052 | * unreachable |
| 9053 | * during this send |
| 9054 | */ |
| 9055 | sctp_move_chunks_from_net(stcb, net); |
| 9056 | } |
| 9057 | *reason_code = 7; |
| 9058 | break; |
| 9059 | } else { |
| 9060 | asoc->ifp_had_enobuf = 0; |
| 9061 | } |
| 9062 | /* |
| 9063 | * increase the number we sent, if a |
| 9064 | * cookie is sent we don't tell them |
| 9065 | * any was sent out. |
| 9066 | */ |
| 9067 | outchain = endoutchain = NULL; |
| 9068 | auth = NULL; |
| 9069 | auth_offset = 0; |
| 9070 | if (!no_out_cnt) |
| 9071 | *num_out += ctl_cnt; |
| 9072 | /* recalc a clean slate and setup */ |
| 9073 | switch (net->ro._l_addr.sa.sa_family) { |
| 9074 | #ifdef INET |
| 9075 | case AF_INET: |
| 9076 | mtu = net->mtu - SCTP_MIN_V4_OVERHEAD; |
| 9077 | break; |
| 9078 | #endif |
| 9079 | #ifdef INET6 |
| 9080 | case AF_INET6: |
| 9081 | mtu = net->mtu - SCTP_MIN_OVERHEAD; |
| 9082 | break; |
| 9083 | #endif |
| 9084 | #if defined(__Userspace__) |
| 9085 | case AF_CONN: |
| 9086 | mtu = net->mtu - sizeof(struct sctphdr); |
| 9087 | break; |
| 9088 | #endif |
| 9089 | default: |
| 9090 | /* TSNH */ |
| 9091 | mtu = net->mtu; |
| 9092 | break; |
| 9093 | } |
| 9094 | to_out = 0; |
| 9095 | no_fragmentflg = 1; |
| 9096 | } |
| 9097 | } |
| 9098 | } |
| 9099 | if (error != 0) { |
| 9100 | /* try next net */ |
| 9101 | continue; |
| 9102 | } |
| 9103 | /* JRI: if dest is in PF state, do not send data to it */ |
| 9104 | if ((asoc->sctp_cmt_on_off > 0) && |
| 9105 | (net != stcb->asoc.alternate) && |
| 9106 | (net->dest_state & SCTP_ADDR_PF)) { |
| 9107 | goto no_data_fill; |
| 9108 | } |
| 9109 | if (net->flight_size >= net->cwnd) { |
| 9110 | goto no_data_fill; |
| 9111 | } |
| 9112 | if ((asoc->sctp_cmt_on_off > 0) && |
| 9113 | (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) && |
| 9114 | (net->flight_size > max_rwnd_per_dest)) { |
| 9115 | goto no_data_fill; |
| 9116 | } |
| 9117 | /* |
| 9118 | * We need a specific accounting for the usage of the |
| 9119 | * send buffer. We also need to check the number of messages |
| 9120 | * per net. For now, this is better than nothing and it |
| 9121 | * disabled by default... |
| 9122 | */ |
| 9123 | if ((asoc->sctp_cmt_on_off > 0) && |
| 9124 | (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) && |
| 9125 | (max_send_per_dest > 0) && |
| 9126 | (net->flight_size > max_send_per_dest)) { |
| 9127 | goto no_data_fill; |
| 9128 | } |
| 9129 | /*********************/ |
| 9130 | /* Data transmission */ |
| 9131 | /*********************/ |
| 9132 | /* |
| 9133 | * if AUTH for DATA is required and no AUTH has been added |
| 9134 | * yet, account for this in the mtu now... if no data can be |
| 9135 | * bundled, this adjustment won't matter anyways since the |
| 9136 | * packet will be going out... |
| 9137 | */ |
| 9138 | data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, |
| 9139 | stcb->asoc.peer_auth_chunks); |
| 9140 | if (data_auth_reqd && (auth == NULL)) { |
| 9141 | mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); |
| 9142 | } |
| 9143 | /* now lets add any data within the MTU constraints */ |
| 9144 | switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) { |
| 9145 | #ifdef INET |
| 9146 | case AF_INET: |
| 9147 | if (net->mtu > SCTP_MIN_V4_OVERHEAD) |
| 9148 | omtu = net->mtu - SCTP_MIN_V4_OVERHEAD; |
| 9149 | else |
| 9150 | omtu = 0; |
| 9151 | break; |
| 9152 | #endif |
| 9153 | #ifdef INET6 |
| 9154 | case AF_INET6: |
| 9155 | if (net->mtu > SCTP_MIN_OVERHEAD) |
| 9156 | omtu = net->mtu - SCTP_MIN_OVERHEAD; |
| 9157 | else |
| 9158 | omtu = 0; |
| 9159 | break; |
| 9160 | #endif |
| 9161 | #if defined(__Userspace__) |
| 9162 | case AF_CONN: |
| 9163 | if (net->mtu > sizeof(struct sctphdr)) { |
| 9164 | omtu = net->mtu - sizeof(struct sctphdr); |
| 9165 | } else { |
| 9166 | omtu = 0; |
| 9167 | } |
| 9168 | break; |
| 9169 | #endif |
| 9170 | default: |
| 9171 | /* TSNH */ |
| 9172 | omtu = 0; |
| 9173 | break; |
| 9174 | } |
| 9175 | if ((((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) || |
| 9176 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) && |
| 9177 | (skip_data_for_this_net == 0)) || |
| 9178 | (cookie)) { |
| 9179 | TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { |
| 9180 | if (no_data_chunks) { |
| 9181 | /* let only control go out */ |
| 9182 | *reason_code = 1; |
| 9183 | break; |
| 9184 | } |
| 9185 | if (net->flight_size >= net->cwnd) { |
| 9186 | /* skip this net, no room for data */ |
| 9187 | *reason_code = 2; |
| 9188 | break; |
| 9189 | } |
| 9190 | if ((chk->whoTo != NULL) && |
| 9191 | (chk->whoTo != net)) { |
| 9192 | /* Don't send the chunk on this net */ |
| 9193 | continue; |
| 9194 | } |
| 9195 |
| 9196 | if (asoc->sctp_cmt_on_off == 0) { |
| 9197 | if ((asoc->alternate) && |
| 9198 | (asoc->alternate != net) && |
| 9199 | (chk->whoTo == NULL)) { |
| 9200 | continue; |
| 9201 | } else if ((net != asoc->primary_destination) && |
| 9202 | (asoc->alternate == NULL) && |
| 9203 | (chk->whoTo == NULL)) { |
| 9204 | continue; |
| 9205 | } |
| 9206 | } |
| 9207 | if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) { |
| 9208 | /*- |
| 9209 | * strange, we have a chunk that is |
| 9210 | * to big for its destination and |
| 9211 | * yet no fragment ok flag. |
| 9212 | * Something went wrong when the |
| 9213 | * PMTU changed...we did not mark |
| 9214 | * this chunk for some reason?? I |
| 9215 | * will fix it here by letting IP |
| 9216 | * fragment it for now and printing |
| 9217 | * a warning. This really should not |
| 9218 | * happen ... |
| 9219 | */ |
| 9220 | SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n", |
| 9221 | chk->send_size, mtu); |
| 9222 | chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; |
| 9223 | } |
| 9224 | if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && |
| 9225 | (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) { |
| 9226 | struct sctp_data_chunk *dchkh; |
| 9227 |
| 9228 | dchkh = mtod(chk->data, struct sctp_data_chunk *); |
| 9229 | dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY; |
| 9230 | } |
| 9231 | if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) || |
| 9232 | ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) { |
| 9233 | /* ok we will add this one */ |
| 9234 |
| 9235 | /* |
| 9236 | * Add an AUTH chunk, if chunk |
| 9237 | * requires it, save the offset into |
| 9238 | * the chain for AUTH |
| 9239 | */ |
| 9240 | if (data_auth_reqd) { |
| 9241 | if (auth == NULL) { |
| 9242 | outchain = sctp_add_auth_chunk(outchain, |
| 9243 | &endoutchain, |
| 9244 | &auth, |
| 9245 | &auth_offset, |
| 9246 | stcb, |
| 9247 | SCTP_DATA); |
| 9248 | auth_keyid = chk->auth_keyid; |
| 9249 | override_ok = 0; |
| 9250 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 9251 | } else if (override_ok) { |
| 9252 | /* use this data's keyid */ |
| 9253 | auth_keyid = chk->auth_keyid; |
| 9254 | override_ok = 0; |
| 9255 | } else if (auth_keyid != chk->auth_keyid) { |
| 9256 | /* different keyid, so done bundling */ |
| 9257 | break; |
| 9258 | } |
| 9259 | } |
| 9260 | outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0, |
| 9261 | chk->send_size, chk->copy_by_ref); |
| 9262 | if (outchain == NULL) { |
| 9263 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n"); |
| 9264 | if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { |
| 9265 | sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); |
| 9266 | } |
| 9267 | *reason_code = 3; |
| 9268 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 9269 | return (ENOMEM); |
| 9270 | } |
| 9271 | /* upate our MTU size */ |
| 9272 | /* Do clear IP_DF ? */ |
| 9273 | if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) { |
| 9274 | no_fragmentflg = 0; |
| 9275 | } |
| 9276 | /* unsigned subtraction of mtu */ |
| 9277 | if (mtu > chk->send_size) |
| 9278 | mtu -= chk->send_size; |
| 9279 | else |
| 9280 | mtu = 0; |
| 9281 | /* unsigned subtraction of r_mtu */ |
| 9282 | if (r_mtu > chk->send_size) |
| 9283 | r_mtu -= chk->send_size; |
| 9284 | else |
| 9285 | r_mtu = 0; |
| 9286 |
| 9287 | to_out += chk->send_size; |
| 9288 | if ((to_out > mx_mtu) && no_fragmentflg) { |
| 9289 | #ifdef INVARIANTS |
| 9290 | panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out); |
| 9291 | #else |
| 9292 | SCTP_PRINTF("Exceeding mtu of %d out size is %d\n", |
| 9293 | mx_mtu, to_out); |
| 9294 | #endif |
| 9295 | } |
| 9296 | chk->window_probe = 0; |
| 9297 | data_list[bundle_at++] = chk; |
| 9298 | if (bundle_at >= SCTP_MAX_DATA_BUNDLING) { |
| 9299 | break; |
| 9300 | } |
| 9301 | if (chk->sent == SCTP_DATAGRAM_UNSENT) { |
| 9302 | if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) { |
| 9303 | SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks); |
| 9304 | } else { |
| 9305 | SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks); |
| 9306 | } |
| 9307 | if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) && |
| 9308 | ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0)) |
| 9309 | /* Count number of user msg's that were fragmented |
| 9310 | * we do this by counting when we see a LAST fragment |
| 9311 | * only. |
| 9312 | */ |
| 9313 | SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs); |
| 9314 | } |
| 9315 | if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) { |
| 9316 | if ((one_chunk) && (stcb->asoc.total_flight == 0)) { |
| 9317 | data_list[0]->window_probe = 1; |
| 9318 | net->window_probe = 1; |
| 9319 | } |
| 9320 | break; |
| 9321 | } |
| 9322 | } else { |
| 9323 | /* |
| 9324 | * Must be sent in order of the |
| 9325 | * TSN's (on a network) |
| 9326 | */ |
| 9327 | break; |
| 9328 | } |
| 9329 | } /* for (chunk gather loop for this net) */ |
| 9330 | } /* if asoc.state OPEN */ |
| 9331 | no_data_fill: |
| 9332 | /* Is there something to send for this destination? */ |
| 9333 | if (outchain) { |
| 9334 | /* We may need to start a control timer or two */ |
| 9335 | if (asconf) { |
| 9336 | sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, |
| 9337 | stcb, net); |
| 9338 | /* |
| 9339 | * do NOT clear the asconf flag as it is used |
| 9340 | * to do appropriate source address selection. |
| 9341 | */ |
| 9342 | } |
| 9343 | if (cookie) { |
| 9344 | sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net); |
| 9345 | cookie = 0; |
| 9346 | } |
| 9347 | /* must start a send timer if data is being sent */ |
| 9348 | if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) { |
| 9349 | /* |
| 9350 | * no timer running on this destination |
| 9351 | * restart it. |
| 9352 | */ |
| 9353 | sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); |
| 9354 | } |
| 9355 | if (bundle_at || hbflag) { |
| 9356 | /* For data/asconf and hb set time */ |
| 9357 | if (*now_filled == 0) { |
| 9358 | (void)SCTP_GETTIME_TIMEVAL(now); |
| 9359 | *now_filled = 1; |
| 9360 | } |
| 9361 | net->last_sent_time = *now; |
| 9362 | } |
| 9363 | /* Now send it, if there is anything to send :> */ |
| 9364 | if ((error = sctp_lowlevel_chunk_output(inp, |
| 9365 | stcb, |
| 9366 | net, |
| 9367 | (struct sockaddr *)&net->ro._l_addr, |
| 9368 | outchain, |
| 9369 | auth_offset, |
| 9370 | auth, |
| 9371 | auth_keyid, |
| 9372 | no_fragmentflg, |
| 9373 | bundle_at, |
| 9374 | asconf, |
| 9375 | inp->sctp_lport, stcb->rport, |
| 9376 | htonl(stcb->asoc.peer_vtag), |
| 9377 | net->port, NULL, |
| 9378 | #if defined(__FreeBSD__) |
| 9379 | 0, 0, |
| 9380 | #endif |
| 9381 | so_locked))) { |
| 9382 | /* error, we could not output */ |
| 9383 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error); |
| 9384 | if (from_where == 0) { |
| 9385 | SCTP_STAT_INCR(sctps_lowlevelerrusr); |
| 9386 | } |
| 9387 | if (error == ENOBUFS) { |
| 9388 | asoc->ifp_had_enobuf = 1; |
| 9389 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 9390 | } |
| 9391 | if (error == EHOSTUNREACH) { |
| 9392 | /* |
| 9393 | * Destination went unreachable |
| 9394 | * during this send |
| 9395 | */ |
| 9396 | sctp_move_chunks_from_net(stcb, net); |
| 9397 | } |
| 9398 | *reason_code = 6; |
| 9399 | /*- |
| 9400 | * I add this line to be paranoid. As far as |
| 9401 | * I can tell the continue, takes us back to |
| 9402 | * the top of the for, but just to make sure |
| 9403 | * I will reset these again here. |
| 9404 | */ |
| 9405 | ctl_cnt = bundle_at = 0; |
| 9406 | continue; /* This takes us back to the for() for the nets. */ |
| 9407 | } else { |
| 9408 | asoc->ifp_had_enobuf = 0; |
| 9409 | } |
| 9410 | endoutchain = NULL; |
| 9411 | auth = NULL; |
| 9412 | auth_offset = 0; |
| 9413 | if (!no_out_cnt) { |
| 9414 | *num_out += (ctl_cnt + bundle_at); |
| 9415 | } |
| 9416 | if (bundle_at) { |
| 9417 | /* setup for a RTO measurement */ |
| 9418 | tsns_sent = data_list[0]->rec.data.tsn; |
| 9419 | /* fill time if not already filled */ |
| 9420 | if (*now_filled == 0) { |
| 9421 | (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent); |
| 9422 | *now_filled = 1; |
| 9423 | *now = asoc->time_last_sent; |
| 9424 | } else { |
| 9425 | asoc->time_last_sent = *now; |
| 9426 | } |
| 9427 | if (net->rto_needed) { |
| 9428 | data_list[0]->do_rtt = 1; |
| 9429 | net->rto_needed = 0; |
| 9430 | } |
| 9431 | SCTP_STAT_INCR_BY(sctps_senddata, bundle_at); |
| 9432 | sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net); |
| 9433 | } |
| 9434 | if (one_chunk) { |
| 9435 | break; |
| 9436 | } |
| 9437 | } |
| 9438 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 9439 | sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND); |
| 9440 | } |
| 9441 | } |
| 9442 | if (old_start_at == NULL) { |
| 9443 | old_start_at = start_at; |
| 9444 | start_at = TAILQ_FIRST(&asoc->nets); |
| 9445 | if (old_start_at) |
| 9446 | goto again_one_more_time; |
| 9447 | } |
| 9448 |
| 9449 | /* |
| 9450 | * At the end there should be no NON timed chunks hanging on this |
| 9451 | * queue. |
| 9452 | */ |
| 9453 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 9454 | sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND); |
| 9455 | } |
| 9456 | if ((*num_out == 0) && (*reason_code == 0)) { |
| 9457 | *reason_code = 4; |
| 9458 | } else { |
| 9459 | *reason_code = 5; |
| 9460 | } |
| 9461 | sctp_clean_up_ctl(stcb, asoc, so_locked); |
| 9462 | return (0); |
| 9463 | } |
| 9464 |
| 9465 | void |
| 9466 | sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err) |
| 9467 | { |
| 9468 | /*- |
| 9469 | * Prepend a OPERATIONAL_ERROR chunk header and put on the end of |
| 9470 | * the control chunk queue. |
| 9471 | */ |
| 9472 | struct sctp_chunkhdr *hdr; |
| 9473 | struct sctp_tmit_chunk *chk; |
| 9474 | struct mbuf *mat, *last_mbuf; |
| 9475 | uint32_t chunk_length; |
| 9476 | uint16_t padding_length; |
| 9477 |
| 9478 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 9479 | SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_NOWAIT); |
| 9480 | if (op_err == NULL) { |
| 9481 | return; |
| 9482 | } |
| 9483 | last_mbuf = NULL; |
| 9484 | chunk_length = 0; |
| 9485 | for (mat = op_err; mat != NULL; mat = SCTP_BUF_NEXT(mat)) { |
| 9486 | chunk_length += SCTP_BUF_LEN(mat); |
| 9487 | if (SCTP_BUF_NEXT(mat) == NULL) { |
| 9488 | last_mbuf = mat; |
| 9489 | } |
| 9490 | } |
| 9491 | if (chunk_length > SCTP_MAX_CHUNK_LENGTH) { |
| 9492 | sctp_m_freem(op_err); |
| 9493 | return; |
| 9494 | } |
| 9495 | padding_length = chunk_length % 4; |
| 9496 | if (padding_length != 0) { |
| 9497 | padding_length = 4 - padding_length; |
| 9498 | } |
| 9499 | if (padding_length != 0) { |
| 9500 | if (sctp_add_pad_tombuf(last_mbuf, padding_length) == NULL) { |
| 9501 | sctp_m_freem(op_err); |
| 9502 | return; |
| 9503 | } |
| 9504 | } |
| 9505 | sctp_alloc_a_chunk(stcb, chk); |
| 9506 | if (chk == NULL) { |
| 9507 | /* no memory */ |
| 9508 | sctp_m_freem(op_err); |
| 9509 | return; |
| 9510 | } |
| 9511 | chk->copy_by_ref = 0; |
| 9512 | chk->rec.chunk_id.id = SCTP_OPERATION_ERROR; |
| 9513 | chk->rec.chunk_id.can_take_data = 0; |
| 9514 | chk->flags = 0; |
| 9515 | chk->send_size = (uint16_t)chunk_length; |
| 9516 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 9517 | chk->snd_count = 0; |
| 9518 | chk->asoc = &stcb->asoc; |
| 9519 | chk->data = op_err; |
| 9520 | chk->whoTo = NULL; |
| 9521 | hdr = mtod(op_err, struct sctp_chunkhdr *); |
| 9522 | hdr->chunk_type = SCTP_OPERATION_ERROR; |
| 9523 | hdr->chunk_flags = 0; |
| 9524 | hdr->chunk_length = htons(chk->send_size); |
| 9525 | TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); |
| 9526 | chk->asoc->ctrl_queue_cnt++; |
| 9527 | } |
| 9528 |
| 9529 | int |
| 9530 | sctp_send_cookie_echo(struct mbuf *m, |
| 9531 | int offset, int limit, |
| 9532 | struct sctp_tcb *stcb, |
| 9533 | struct sctp_nets *net) |
| 9534 | { |
| 9535 | /*- |
| 9536 | * pull out the cookie and put it at the front of the control chunk |
| 9537 | * queue. |
| 9538 | */ |
| 9539 | int at; |
| 9540 | struct mbuf *cookie; |
| 9541 | struct sctp_paramhdr param, *phdr; |
| 9542 | struct sctp_chunkhdr *hdr; |
| 9543 | struct sctp_tmit_chunk *chk; |
| 9544 | uint16_t ptype, plen; |
| 9545 |
| 9546 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 9547 | /* First find the cookie in the param area */ |
| 9548 | cookie = NULL; |
| 9549 | at = offset + sizeof(struct sctp_init_chunk); |
| 9550 | for (;;) { |
| 9551 | phdr = sctp_get_next_param(m, at, ¶m, sizeof(param)); |
| 9552 | if (phdr == NULL) { |
| 9553 | return (-3); |
| 9554 | } |
| 9555 | ptype = ntohs(phdr->param_type); |
| 9556 | plen = ntohs(phdr->param_length); |
| 9557 | if (plen < sizeof(struct sctp_paramhdr)) { |
| 9558 | return (-6); |
| 9559 | } |
| 9560 | if (ptype == SCTP_STATE_COOKIE) { |
| 9561 | int pad; |
| 9562 |
| 9563 | /* found the cookie */ |
| 9564 | if (at + plen > limit) { |
| 9565 | return (-7); |
| 9566 | } |
| 9567 | cookie = SCTP_M_COPYM(m, at, plen, M_NOWAIT); |
| 9568 | if (cookie == NULL) { |
| 9569 | /* No memory */ |
| 9570 | return (-2); |
| 9571 | } |
| 9572 | if ((pad = (plen % 4)) > 0) { |
| 9573 | pad = 4 - pad; |
| 9574 | } |
| 9575 | if (pad > 0) { |
| 9576 | if (sctp_pad_lastmbuf(cookie, pad, NULL) == NULL) { |
| 9577 | return (-8); |
| 9578 | } |
| 9579 | } |
| 9580 | #ifdef SCTP_MBUF_LOGGING |
| 9581 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { |
| 9582 | sctp_log_mbc(cookie, SCTP_MBUF_ICOPY); |
| 9583 | } |
| 9584 | #endif |
| 9585 | break; |
| 9586 | } |
| 9587 | at += SCTP_SIZE32(plen); |
| 9588 | } |
| 9589 | /* ok, we got the cookie lets change it into a cookie echo chunk */ |
| 9590 | /* first the change from param to cookie */ |
| 9591 | hdr = mtod(cookie, struct sctp_chunkhdr *); |
| 9592 | hdr->chunk_type = SCTP_COOKIE_ECHO; |
| 9593 | hdr->chunk_flags = 0; |
| 9594 | /* get the chunk stuff now and place it in the FRONT of the queue */ |
| 9595 | sctp_alloc_a_chunk(stcb, chk); |
| 9596 | if (chk == NULL) { |
| 9597 | /* no memory */ |
| 9598 | sctp_m_freem(cookie); |
| 9599 | return (-5); |
| 9600 | } |
| 9601 | chk->copy_by_ref = 0; |
| 9602 | chk->rec.chunk_id.id = SCTP_COOKIE_ECHO; |
| 9603 | chk->rec.chunk_id.can_take_data = 0; |
| 9604 | chk->flags = CHUNK_FLAGS_FRAGMENT_OK; |
| 9605 | chk->send_size = SCTP_SIZE32(plen); |
| 9606 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 9607 | chk->snd_count = 0; |
| 9608 | chk->asoc = &stcb->asoc; |
| 9609 | chk->data = cookie; |
| 9610 | chk->whoTo = net; |
| 9611 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 9612 | TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next); |
| 9613 | chk->asoc->ctrl_queue_cnt++; |
| 9614 | return (0); |
| 9615 | } |
| 9616 |
| 9617 | void |
| 9618 | sctp_send_heartbeat_ack(struct sctp_tcb *stcb, |
| 9619 | struct mbuf *m, |
| 9620 | int offset, |
| 9621 | int chk_length, |
| 9622 | struct sctp_nets *net) |
| 9623 | { |
| 9624 | /* |
| 9625 | * take a HB request and make it into a HB ack and send it. |
| 9626 | */ |
| 9627 | struct mbuf *outchain; |
| 9628 | struct sctp_chunkhdr *chdr; |
| 9629 | struct sctp_tmit_chunk *chk; |
| 9630 |
| 9631 | if (net == NULL) |
| 9632 | /* must have a net pointer */ |
| 9633 | return; |
| 9634 |
| 9635 | outchain = SCTP_M_COPYM(m, offset, chk_length, M_NOWAIT); |
| 9636 | if (outchain == NULL) { |
| 9637 | /* gak out of memory */ |
| 9638 | return; |
| 9639 | } |
| 9640 | #ifdef SCTP_MBUF_LOGGING |
| 9641 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { |
| 9642 | sctp_log_mbc(outchain, SCTP_MBUF_ICOPY); |
| 9643 | } |
| 9644 | #endif |
| 9645 | chdr = mtod(outchain, struct sctp_chunkhdr *); |
| 9646 | chdr->chunk_type = SCTP_HEARTBEAT_ACK; |
| 9647 | chdr->chunk_flags = 0; |
| 9648 | if (chk_length % 4 != 0) { |
| 9649 | sctp_pad_lastmbuf(outchain, 4 - (chk_length % 4), NULL); |
| 9650 | } |
| 9651 | sctp_alloc_a_chunk(stcb, chk); |
| 9652 | if (chk == NULL) { |
| 9653 | /* no memory */ |
| 9654 | sctp_m_freem(outchain); |
| 9655 | return; |
| 9656 | } |
| 9657 | chk->copy_by_ref = 0; |
| 9658 | chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK; |
| 9659 | chk->rec.chunk_id.can_take_data = 1; |
| 9660 | chk->flags = 0; |
| 9661 | chk->send_size = chk_length; |
| 9662 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 9663 | chk->snd_count = 0; |
| 9664 | chk->asoc = &stcb->asoc; |
| 9665 | chk->data = outchain; |
| 9666 | chk->whoTo = net; |
| 9667 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 9668 | TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); |
| 9669 | chk->asoc->ctrl_queue_cnt++; |
| 9670 | } |
| 9671 |
| 9672 | void |
| 9673 | sctp_send_cookie_ack(struct sctp_tcb *stcb) |
| 9674 | { |
| 9675 | /* formulate and queue a cookie-ack back to sender */ |
| 9676 | struct mbuf *cookie_ack; |
| 9677 | struct sctp_chunkhdr *hdr; |
| 9678 | struct sctp_tmit_chunk *chk; |
| 9679 |
| 9680 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 9681 |
| 9682 | cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER); |
| 9683 | if (cookie_ack == NULL) { |
| 9684 | /* no mbuf's */ |
| 9685 | return; |
| 9686 | } |
| 9687 | SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD); |
| 9688 | sctp_alloc_a_chunk(stcb, chk); |
| 9689 | if (chk == NULL) { |
| 9690 | /* no memory */ |
| 9691 | sctp_m_freem(cookie_ack); |
| 9692 | return; |
| 9693 | } |
| 9694 | chk->copy_by_ref = 0; |
| 9695 | chk->rec.chunk_id.id = SCTP_COOKIE_ACK; |
| 9696 | chk->rec.chunk_id.can_take_data = 1; |
| 9697 | chk->flags = 0; |
| 9698 | chk->send_size = sizeof(struct sctp_chunkhdr); |
| 9699 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 9700 | chk->snd_count = 0; |
| 9701 | chk->asoc = &stcb->asoc; |
| 9702 | chk->data = cookie_ack; |
| 9703 | if (chk->asoc->last_control_chunk_from != NULL) { |
| 9704 | chk->whoTo = chk->asoc->last_control_chunk_from; |
| 9705 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 9706 | } else { |
| 9707 | chk->whoTo = NULL; |
| 9708 | } |
| 9709 | hdr = mtod(cookie_ack, struct sctp_chunkhdr *); |
| 9710 | hdr->chunk_type = SCTP_COOKIE_ACK; |
| 9711 | hdr->chunk_flags = 0; |
| 9712 | hdr->chunk_length = htons(chk->send_size); |
| 9713 | SCTP_BUF_LEN(cookie_ack) = chk->send_size; |
| 9714 | TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); |
| 9715 | chk->asoc->ctrl_queue_cnt++; |
| 9716 | return; |
| 9717 | } |
| 9718 |
| 9719 |
| 9720 | void |
| 9721 | sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net) |
| 9722 | { |
| 9723 | /* formulate and queue a SHUTDOWN-ACK back to the sender */ |
| 9724 | struct mbuf *m_shutdown_ack; |
| 9725 | struct sctp_shutdown_ack_chunk *ack_cp; |
| 9726 | struct sctp_tmit_chunk *chk; |
| 9727 |
| 9728 | m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_NOWAIT, 1, MT_HEADER); |
| 9729 | if (m_shutdown_ack == NULL) { |
| 9730 | /* no mbuf's */ |
| 9731 | return; |
| 9732 | } |
| 9733 | SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD); |
| 9734 | sctp_alloc_a_chunk(stcb, chk); |
| 9735 | if (chk == NULL) { |
| 9736 | /* no memory */ |
| 9737 | sctp_m_freem(m_shutdown_ack); |
| 9738 | return; |
| 9739 | } |
| 9740 | chk->copy_by_ref = 0; |
| 9741 | chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK; |
| 9742 | chk->rec.chunk_id.can_take_data = 1; |
| 9743 | chk->flags = 0; |
| 9744 | chk->send_size = sizeof(struct sctp_chunkhdr); |
| 9745 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 9746 | chk->snd_count = 0; |
| 9747 | chk->asoc = &stcb->asoc; |
| 9748 | chk->data = m_shutdown_ack; |
| 9749 | chk->whoTo = net; |
| 9750 | if (chk->whoTo) { |
| 9751 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 9752 | } |
| 9753 | ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *); |
| 9754 | ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK; |
| 9755 | ack_cp->ch.chunk_flags = 0; |
| 9756 | ack_cp->ch.chunk_length = htons(chk->send_size); |
| 9757 | SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size; |
| 9758 | TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); |
| 9759 | chk->asoc->ctrl_queue_cnt++; |
| 9760 | return; |
| 9761 | } |
| 9762 |
| 9763 | void |
| 9764 | sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net) |
| 9765 | { |
| 9766 | /* formulate and queue a SHUTDOWN to the sender */ |
| 9767 | struct mbuf *m_shutdown; |
| 9768 | struct sctp_shutdown_chunk *shutdown_cp; |
| 9769 | struct sctp_tmit_chunk *chk; |
| 9770 |
| 9771 | TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { |
| 9772 | if (chk->rec.chunk_id.id == SCTP_SHUTDOWN) { |
| 9773 | /* We already have a SHUTDOWN queued. Reuse it. */ |
| 9774 | if (chk->whoTo) { |
| 9775 | sctp_free_remote_addr(chk->whoTo); |
| 9776 | chk->whoTo = NULL; |
| 9777 | } |
| 9778 | break; |
| 9779 | } |
| 9780 | } |
| 9781 | if (chk == NULL) { |
| 9782 | m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_NOWAIT, 1, MT_HEADER); |
| 9783 | if (m_shutdown == NULL) { |
| 9784 | /* no mbuf's */ |
| 9785 | return; |
| 9786 | } |
| 9787 | SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD); |
| 9788 | sctp_alloc_a_chunk(stcb, chk); |
| 9789 | if (chk == NULL) { |
| 9790 | /* no memory */ |
| 9791 | sctp_m_freem(m_shutdown); |
| 9792 | return; |
| 9793 | } |
| 9794 | chk->copy_by_ref = 0; |
| 9795 | chk->rec.chunk_id.id = SCTP_SHUTDOWN; |
| 9796 | chk->rec.chunk_id.can_take_data = 1; |
| 9797 | chk->flags = 0; |
| 9798 | chk->send_size = sizeof(struct sctp_shutdown_chunk); |
| 9799 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 9800 | chk->snd_count = 0; |
| 9801 | chk->asoc = &stcb->asoc; |
| 9802 | chk->data = m_shutdown; |
| 9803 | chk->whoTo = net; |
| 9804 | if (chk->whoTo) { |
| 9805 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 9806 | } |
| 9807 | shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *); |
| 9808 | shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN; |
| 9809 | shutdown_cp->ch.chunk_flags = 0; |
| 9810 | shutdown_cp->ch.chunk_length = htons(chk->send_size); |
| 9811 | shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn); |
| 9812 | SCTP_BUF_LEN(m_shutdown) = chk->send_size; |
| 9813 | TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); |
| 9814 | chk->asoc->ctrl_queue_cnt++; |
| 9815 | } else { |
| 9816 | TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, sctp_next); |
| 9817 | chk->whoTo = net; |
| 9818 | if (chk->whoTo) { |
| 9819 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 9820 | } |
| 9821 | shutdown_cp = mtod(chk->data, struct sctp_shutdown_chunk *); |
| 9822 | shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn); |
| 9823 | TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next); |
| 9824 | } |
| 9825 | return; |
| 9826 | } |
| 9827 |
| 9828 | void |
| 9829 | sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked) |
| 9830 | { |
| 9831 | /* |
| 9832 | * formulate and queue an ASCONF to the peer. |
| 9833 | * ASCONF parameters should be queued on the assoc queue. |
| 9834 | */ |
| 9835 | struct sctp_tmit_chunk *chk; |
| 9836 | struct mbuf *m_asconf; |
| 9837 | int len; |
| 9838 |
| 9839 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 9840 |
| 9841 | if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) && |
| 9842 | (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) { |
| 9843 | /* can't send a new one if there is one in flight already */ |
| 9844 | return; |
| 9845 | } |
| 9846 |
| 9847 | /* compose an ASCONF chunk, maximum length is PMTU */ |
| 9848 | m_asconf = sctp_compose_asconf(stcb, &len, addr_locked); |
| 9849 | if (m_asconf == NULL) { |
| 9850 | return; |
| 9851 | } |
| 9852 |
| 9853 | sctp_alloc_a_chunk(stcb, chk); |
| 9854 | if (chk == NULL) { |
| 9855 | /* no memory */ |
| 9856 | sctp_m_freem(m_asconf); |
| 9857 | return; |
| 9858 | } |
| 9859 |
| 9860 | chk->copy_by_ref = 0; |
| 9861 | chk->rec.chunk_id.id = SCTP_ASCONF; |
| 9862 | chk->rec.chunk_id.can_take_data = 0; |
| 9863 | chk->flags = CHUNK_FLAGS_FRAGMENT_OK; |
| 9864 | chk->data = m_asconf; |
| 9865 | chk->send_size = len; |
| 9866 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 9867 | chk->snd_count = 0; |
| 9868 | chk->asoc = &stcb->asoc; |
| 9869 | chk->whoTo = net; |
| 9870 | if (chk->whoTo) { |
| 9871 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 9872 | } |
| 9873 | TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next); |
| 9874 | chk->asoc->ctrl_queue_cnt++; |
| 9875 | return; |
| 9876 | } |
| 9877 |
| 9878 | void |
| 9879 | sctp_send_asconf_ack(struct sctp_tcb *stcb) |
| 9880 | { |
| 9881 | /* |
| 9882 | * formulate and queue a asconf-ack back to sender. |
| 9883 | * the asconf-ack must be stored in the tcb. |
| 9884 | */ |
| 9885 | struct sctp_tmit_chunk *chk; |
| 9886 | struct sctp_asconf_ack *ack, *latest_ack; |
| 9887 | struct mbuf *m_ack; |
| 9888 | struct sctp_nets *net = NULL; |
| 9889 |
| 9890 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 9891 | /* Get the latest ASCONF-ACK */ |
| 9892 | latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead); |
| 9893 | if (latest_ack == NULL) { |
| 9894 | return; |
| 9895 | } |
| 9896 | if (latest_ack->last_sent_to != NULL && |
| 9897 | latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) { |
| 9898 | /* we're doing a retransmission */ |
| 9899 | net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0); |
| 9900 | if (net == NULL) { |
| 9901 | /* no alternate */ |
| 9902 | if (stcb->asoc.last_control_chunk_from == NULL) { |
| 9903 | if (stcb->asoc.alternate) { |
| 9904 | net = stcb->asoc.alternate; |
| 9905 | } else { |
| 9906 | net = stcb->asoc.primary_destination; |
| 9907 | } |
| 9908 | } else { |
| 9909 | net = stcb->asoc.last_control_chunk_from; |
| 9910 | } |
| 9911 | } |
| 9912 | } else { |
| 9913 | /* normal case */ |
| 9914 | if (stcb->asoc.last_control_chunk_from == NULL) { |
| 9915 | if (stcb->asoc.alternate) { |
| 9916 | net = stcb->asoc.alternate; |
| 9917 | } else { |
| 9918 | net = stcb->asoc.primary_destination; |
| 9919 | } |
| 9920 | } else { |
| 9921 | net = stcb->asoc.last_control_chunk_from; |
| 9922 | } |
| 9923 | } |
| 9924 | latest_ack->last_sent_to = net; |
| 9925 |
| 9926 | TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) { |
| 9927 | if (ack->data == NULL) { |
| 9928 | continue; |
| 9929 | } |
| 9930 |
| 9931 | /* copy the asconf_ack */ |
| 9932 | m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_NOWAIT); |
| 9933 | if (m_ack == NULL) { |
| 9934 | /* couldn't copy it */ |
| 9935 | return; |
| 9936 | } |
| 9937 | #ifdef SCTP_MBUF_LOGGING |
| 9938 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { |
| 9939 | sctp_log_mbc(m_ack, SCTP_MBUF_ICOPY); |
| 9940 | } |
| 9941 | #endif |
| 9942 |
| 9943 | sctp_alloc_a_chunk(stcb, chk); |
| 9944 | if (chk == NULL) { |
| 9945 | /* no memory */ |
| 9946 | if (m_ack) |
| 9947 | sctp_m_freem(m_ack); |
| 9948 | return; |
| 9949 | } |
| 9950 | chk->copy_by_ref = 0; |
| 9951 | chk->rec.chunk_id.id = SCTP_ASCONF_ACK; |
| 9952 | chk->rec.chunk_id.can_take_data = 1; |
| 9953 | chk->flags = CHUNK_FLAGS_FRAGMENT_OK; |
| 9954 | chk->whoTo = net; |
| 9955 | if (chk->whoTo) { |
| 9956 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 9957 | } |
| 9958 | chk->data = m_ack; |
| 9959 | chk->send_size = ack->len; |
| 9960 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 9961 | chk->snd_count = 0; |
| 9962 | chk->asoc = &stcb->asoc; |
| 9963 |
| 9964 | TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); |
| 9965 | chk->asoc->ctrl_queue_cnt++; |
| 9966 | } |
| 9967 | return; |
| 9968 | } |
| 9969 |
| 9970 |
| 9971 | static int |
| 9972 | sctp_chunk_retransmission(struct sctp_inpcb *inp, |
| 9973 | struct sctp_tcb *stcb, |
| 9974 | struct sctp_association *asoc, |
| 9975 | int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked |
| 9976 | #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) |
| 9977 | SCTP_UNUSED |
| 9978 | #endif |
| 9979 | ) |
| 9980 | { |
| 9981 | /*- |
| 9982 | * send out one MTU of retransmission. If fast_retransmit is |
| 9983 | * happening we ignore the cwnd. Otherwise we obey the cwnd and |
| 9984 | * rwnd. For a Cookie or Asconf in the control chunk queue we |
| 9985 | * retransmit them by themselves. |
| 9986 | * |
| 9987 | * For data chunks we will pick out the lowest TSN's in the sent_queue |
| 9988 | * marked for resend and bundle them all together (up to a MTU of |
| 9989 | * destination). The address to send to should have been |
| 9990 | * selected/changed where the retransmission was marked (i.e. in FR |
| 9991 | * or t3-timeout routines). |
| 9992 | */ |
| 9993 | struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING]; |
| 9994 | struct sctp_tmit_chunk *chk, *fwd; |
| 9995 | struct mbuf *m, *endofchain; |
| 9996 | struct sctp_nets *net = NULL; |
| 9997 | uint32_t tsns_sent = 0; |
| 9998 | int no_fragmentflg, bundle_at, cnt_thru; |
| 9999 | unsigned int mtu; |
| 10000 | int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started; |
| 10001 | struct sctp_auth_chunk *auth = NULL; |
| 10002 | uint32_t auth_offset = 0; |
| 10003 | uint16_t auth_keyid; |
| 10004 | int override_ok = 1; |
| 10005 | int data_auth_reqd = 0; |
| 10006 | uint32_t dmtu = 0; |
| 10007 |
| 10008 | #if defined(__APPLE__) |
| 10009 | if (so_locked) { |
| 10010 | sctp_lock_assert(SCTP_INP_SO(inp)); |
| 10011 | } else { |
| 10012 | sctp_unlock_assert(SCTP_INP_SO(inp)); |
| 10013 | } |
| 10014 | #endif |
| 10015 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 10016 | tmr_started = ctl_cnt = bundle_at = error = 0; |
| 10017 | no_fragmentflg = 1; |
| 10018 | fwd_tsn = 0; |
| 10019 | *cnt_out = 0; |
| 10020 | fwd = NULL; |
| 10021 | endofchain = m = NULL; |
| 10022 | auth_keyid = stcb->asoc.authinfo.active_keyid; |
| 10023 | #ifdef SCTP_AUDITING_ENABLED |
| 10024 | sctp_audit_log(0xC3, 1); |
| 10025 | #endif |
| 10026 | if ((TAILQ_EMPTY(&asoc->sent_queue)) && |
| 10027 | (TAILQ_EMPTY(&asoc->control_send_queue))) { |
| 10028 | SCTPDBG(SCTP_DEBUG_OUTPUT1,"SCTP hits empty queue with cnt set to %d?\n", |
| 10029 | asoc->sent_queue_retran_cnt); |
| 10030 | asoc->sent_queue_cnt = 0; |
| 10031 | asoc->sent_queue_cnt_removeable = 0; |
| 10032 | /* send back 0/0 so we enter normal transmission */ |
| 10033 | *cnt_out = 0; |
| 10034 | return (0); |
| 10035 | } |
| 10036 | TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { |
| 10037 | if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) || |
| 10038 | (chk->rec.chunk_id.id == SCTP_STREAM_RESET) || |
| 10039 | (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) { |
| 10040 | if (chk->sent != SCTP_DATAGRAM_RESEND) { |
| 10041 | continue; |
| 10042 | } |
| 10043 | if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) { |
| 10044 | if (chk != asoc->str_reset) { |
| 10045 | /* |
| 10046 | * not eligible for retran if its |
| 10047 | * not ours |
| 10048 | */ |
| 10049 | continue; |
| 10050 | } |
| 10051 | } |
| 10052 | ctl_cnt++; |
| 10053 | if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) { |
| 10054 | fwd_tsn = 1; |
| 10055 | } |
| 10056 | /* |
| 10057 | * Add an AUTH chunk, if chunk requires it save the |
| 10058 | * offset into the chain for AUTH |
| 10059 | */ |
| 10060 | if ((auth == NULL) && |
| 10061 | (sctp_auth_is_required_chunk(chk->rec.chunk_id.id, |
| 10062 | stcb->asoc.peer_auth_chunks))) { |
| 10063 | m = sctp_add_auth_chunk(m, &endofchain, |
| 10064 | &auth, &auth_offset, |
| 10065 | stcb, |
| 10066 | chk->rec.chunk_id.id); |
| 10067 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 10068 | } |
| 10069 | m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref); |
| 10070 | break; |
| 10071 | } |
| 10072 | } |
| 10073 | one_chunk = 0; |
| 10074 | cnt_thru = 0; |
| 10075 | /* do we have control chunks to retransmit? */ |
| 10076 | if (m != NULL) { |
| 10077 | /* Start a timer no matter if we succeed or fail */ |
| 10078 | if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) { |
| 10079 | sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo); |
| 10080 | } else if (chk->rec.chunk_id.id == SCTP_ASCONF) |
| 10081 | sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo); |
| 10082 | chk->snd_count++; /* update our count */ |
| 10083 | if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo, |
| 10084 | (struct sockaddr *)&chk->whoTo->ro._l_addr, m, |
| 10085 | auth_offset, auth, stcb->asoc.authinfo.active_keyid, |
| 10086 | no_fragmentflg, 0, 0, |
| 10087 | inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag), |
| 10088 | chk->whoTo->port, NULL, |
| 10089 | #if defined(__FreeBSD__) |
| 10090 | 0, 0, |
| 10091 | #endif |
| 10092 | so_locked))) { |
| 10093 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error); |
| 10094 | if (error == ENOBUFS) { |
| 10095 | asoc->ifp_had_enobuf = 1; |
| 10096 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 10097 | } |
| 10098 | return (error); |
| 10099 | } else { |
| 10100 | asoc->ifp_had_enobuf = 0; |
| 10101 | } |
| 10102 | endofchain = NULL; |
| 10103 | auth = NULL; |
| 10104 | auth_offset = 0; |
| 10105 | /* |
| 10106 | * We don't want to mark the net->sent time here since this |
| 10107 | * we use this for HB and retrans cannot measure RTT |
| 10108 | */ |
| 10109 | /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */ |
| 10110 | *cnt_out += 1; |
| 10111 | chk->sent = SCTP_DATAGRAM_SENT; |
| 10112 | sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt); |
| 10113 | if (fwd_tsn == 0) { |
| 10114 | return (0); |
| 10115 | } else { |
| 10116 | /* Clean up the fwd-tsn list */ |
| 10117 | sctp_clean_up_ctl(stcb, asoc, so_locked); |
| 10118 | return (0); |
| 10119 | } |
| 10120 | } |
| 10121 | /* |
| 10122 | * Ok, it is just data retransmission we need to do or that and a |
| 10123 | * fwd-tsn with it all. |
| 10124 | */ |
| 10125 | if (TAILQ_EMPTY(&asoc->sent_queue)) { |
| 10126 | return (SCTP_RETRAN_DONE); |
| 10127 | } |
| 10128 | if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) || |
| 10129 | (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT)) { |
| 10130 | /* not yet open, resend the cookie and that is it */ |
| 10131 | return (1); |
| 10132 | } |
| 10133 | #ifdef SCTP_AUDITING_ENABLED |
| 10134 | sctp_auditing(20, inp, stcb, NULL); |
| 10135 | #endif |
| 10136 | data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks); |
| 10137 | TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) { |
| 10138 | if (chk->sent != SCTP_DATAGRAM_RESEND) { |
| 10139 | /* No, not sent to this net or not ready for rtx */ |
| 10140 | continue; |
| 10141 | } |
| 10142 | if (chk->data == NULL) { |
| 10143 | SCTP_PRINTF("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n", |
| 10144 | chk->rec.data.tsn, chk->snd_count, chk->sent); |
| 10145 | continue; |
| 10146 | } |
| 10147 | if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) && |
| 10148 | (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) { |
| 10149 | struct mbuf *op_err; |
| 10150 | char msg[SCTP_DIAG_INFO_LEN]; |
| 10151 |
| 10152 | snprintf(msg, sizeof(msg), "TSN %8.8x retransmitted %d times, giving up", |
| 10153 | chk->rec.data.tsn, chk->snd_count); |
| 10154 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 10155 | msg); |
| 10156 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 10157 | sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, |
| 10158 | so_locked); |
| 10159 | SCTP_TCB_LOCK(stcb); |
| 10160 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
| 10161 | return (SCTP_RETRAN_EXIT); |
| 10162 | } |
| 10163 | /* pick up the net */ |
| 10164 | net = chk->whoTo; |
| 10165 | switch (net->ro._l_addr.sa.sa_family) { |
| 10166 | #ifdef INET |
| 10167 | case AF_INET: |
| 10168 | mtu = net->mtu - SCTP_MIN_V4_OVERHEAD; |
| 10169 | break; |
| 10170 | #endif |
| 10171 | #ifdef INET6 |
| 10172 | case AF_INET6: |
| 10173 | mtu = net->mtu - SCTP_MIN_OVERHEAD; |
| 10174 | break; |
| 10175 | #endif |
| 10176 | #if defined(__Userspace__) |
| 10177 | case AF_CONN: |
| 10178 | mtu = net->mtu - sizeof(struct sctphdr); |
| 10179 | break; |
| 10180 | #endif |
| 10181 | default: |
| 10182 | /* TSNH */ |
| 10183 | mtu = net->mtu; |
| 10184 | break; |
| 10185 | } |
| 10186 |
| 10187 | if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) { |
| 10188 | /* No room in peers rwnd */ |
| 10189 | uint32_t tsn; |
| 10190 |
| 10191 | tsn = asoc->last_acked_seq + 1; |
| 10192 | if (tsn == chk->rec.data.tsn) { |
| 10193 | /* |
| 10194 | * we make a special exception for this |
| 10195 | * case. The peer has no rwnd but is missing |
| 10196 | * the lowest chunk.. which is probably what |
| 10197 | * is holding up the rwnd. |
| 10198 | */ |
| 10199 | goto one_chunk_around; |
| 10200 | } |
| 10201 | return (1); |
| 10202 | } |
| 10203 | one_chunk_around: |
| 10204 | if (asoc->peers_rwnd < mtu) { |
| 10205 | one_chunk = 1; |
| 10206 | if ((asoc->peers_rwnd == 0) && |
| 10207 | (asoc->total_flight == 0)) { |
| 10208 | chk->window_probe = 1; |
| 10209 | chk->whoTo->window_probe = 1; |
| 10210 | } |
| 10211 | } |
| 10212 | #ifdef SCTP_AUDITING_ENABLED |
| 10213 | sctp_audit_log(0xC3, 2); |
| 10214 | #endif |
| 10215 | bundle_at = 0; |
| 10216 | m = NULL; |
| 10217 | net->fast_retran_ip = 0; |
| 10218 | if (chk->rec.data.doing_fast_retransmit == 0) { |
| 10219 | /* |
| 10220 | * if no FR in progress skip destination that have |
| 10221 | * flight_size > cwnd. |
| 10222 | */ |
| 10223 | if (net->flight_size >= net->cwnd) { |
| 10224 | continue; |
| 10225 | } |
| 10226 | } else { |
| 10227 | /* |
| 10228 | * Mark the destination net to have FR recovery |
| 10229 | * limits put on it. |
| 10230 | */ |
| 10231 | *fr_done = 1; |
| 10232 | net->fast_retran_ip = 1; |
| 10233 | } |
| 10234 |
| 10235 | /* |
| 10236 | * if no AUTH is yet included and this chunk requires it, |
| 10237 | * make sure to account for it. We don't apply the size |
| 10238 | * until the AUTH chunk is actually added below in case |
| 10239 | * there is no room for this chunk. |
| 10240 | */ |
| 10241 | if (data_auth_reqd && (auth == NULL)) { |
| 10242 | dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); |
| 10243 | } else |
| 10244 | dmtu = 0; |
| 10245 |
| 10246 | if ((chk->send_size <= (mtu - dmtu)) || |
| 10247 | (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) { |
| 10248 | /* ok we will add this one */ |
| 10249 | if (data_auth_reqd) { |
| 10250 | if (auth == NULL) { |
| 10251 | m = sctp_add_auth_chunk(m, |
| 10252 | &endofchain, |
| 10253 | &auth, |
| 10254 | &auth_offset, |
| 10255 | stcb, |
| 10256 | SCTP_DATA); |
| 10257 | auth_keyid = chk->auth_keyid; |
| 10258 | override_ok = 0; |
| 10259 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 10260 | } else if (override_ok) { |
| 10261 | auth_keyid = chk->auth_keyid; |
| 10262 | override_ok = 0; |
| 10263 | } else if (chk->auth_keyid != auth_keyid) { |
| 10264 | /* different keyid, so done bundling */ |
| 10265 | break; |
| 10266 | } |
| 10267 | } |
| 10268 | m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref); |
| 10269 | if (m == NULL) { |
| 10270 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 10271 | return (ENOMEM); |
| 10272 | } |
| 10273 | /* Do clear IP_DF ? */ |
| 10274 | if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) { |
| 10275 | no_fragmentflg = 0; |
| 10276 | } |
| 10277 | /* upate our MTU size */ |
| 10278 | if (mtu > (chk->send_size + dmtu)) |
| 10279 | mtu -= (chk->send_size + dmtu); |
| 10280 | else |
| 10281 | mtu = 0; |
| 10282 | data_list[bundle_at++] = chk; |
| 10283 | if (one_chunk && (asoc->total_flight <= 0)) { |
| 10284 | SCTP_STAT_INCR(sctps_windowprobed); |
| 10285 | } |
| 10286 | } |
| 10287 | if (one_chunk == 0) { |
| 10288 | /* |
| 10289 | * now are there anymore forward from chk to pick |
| 10290 | * up? |
| 10291 | */ |
| 10292 | for (fwd = TAILQ_NEXT(chk, sctp_next); fwd != NULL; fwd = TAILQ_NEXT(fwd, sctp_next)) { |
| 10293 | if (fwd->sent != SCTP_DATAGRAM_RESEND) { |
| 10294 | /* Nope, not for retran */ |
| 10295 | continue; |
| 10296 | } |
| 10297 | if (fwd->whoTo != net) { |
| 10298 | /* Nope, not the net in question */ |
| 10299 | continue; |
| 10300 | } |
| 10301 | if (data_auth_reqd && (auth == NULL)) { |
| 10302 | dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); |
| 10303 | } else |
| 10304 | dmtu = 0; |
| 10305 | if (fwd->send_size <= (mtu - dmtu)) { |
| 10306 | if (data_auth_reqd) { |
| 10307 | if (auth == NULL) { |
| 10308 | m = sctp_add_auth_chunk(m, |
| 10309 | &endofchain, |
| 10310 | &auth, |
| 10311 | &auth_offset, |
| 10312 | stcb, |
| 10313 | SCTP_DATA); |
| 10314 | auth_keyid = fwd->auth_keyid; |
| 10315 | override_ok = 0; |
| 10316 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 10317 | } else if (override_ok) { |
| 10318 | auth_keyid = fwd->auth_keyid; |
| 10319 | override_ok = 0; |
| 10320 | } else if (fwd->auth_keyid != auth_keyid) { |
| 10321 | /* different keyid, so done bundling */ |
| 10322 | break; |
| 10323 | } |
| 10324 | } |
| 10325 | m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref); |
| 10326 | if (m == NULL) { |
| 10327 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 10328 | return (ENOMEM); |
| 10329 | } |
| 10330 | /* Do clear IP_DF ? */ |
| 10331 | if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) { |
| 10332 | no_fragmentflg = 0; |
| 10333 | } |
| 10334 | /* upate our MTU size */ |
| 10335 | if (mtu > (fwd->send_size + dmtu)) |
| 10336 | mtu -= (fwd->send_size + dmtu); |
| 10337 | else |
| 10338 | mtu = 0; |
| 10339 | data_list[bundle_at++] = fwd; |
| 10340 | if (bundle_at >= SCTP_MAX_DATA_BUNDLING) { |
| 10341 | break; |
| 10342 | } |
| 10343 | } else { |
| 10344 | /* can't fit so we are done */ |
| 10345 | break; |
| 10346 | } |
| 10347 | } |
| 10348 | } |
| 10349 | /* Is there something to send for this destination? */ |
| 10350 | if (m) { |
| 10351 | /* |
| 10352 | * No matter if we fail/or succeed we should start a |
| 10353 | * timer. A failure is like a lost IP packet :-) |
| 10354 | */ |
| 10355 | if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { |
| 10356 | /* |
| 10357 | * no timer running on this destination |
| 10358 | * restart it. |
| 10359 | */ |
| 10360 | sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); |
| 10361 | tmr_started = 1; |
| 10362 | } |
| 10363 | /* Now lets send it, if there is anything to send :> */ |
| 10364 | if ((error = sctp_lowlevel_chunk_output(inp, stcb, net, |
| 10365 | (struct sockaddr *)&net->ro._l_addr, m, |
| 10366 | auth_offset, auth, auth_keyid, |
| 10367 | no_fragmentflg, 0, 0, |
| 10368 | inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag), |
| 10369 | net->port, NULL, |
| 10370 | #if defined(__FreeBSD__) |
| 10371 | 0, 0, |
| 10372 | #endif |
| 10373 | so_locked))) { |
| 10374 | /* error, we could not output */ |
| 10375 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error); |
| 10376 | if (error == ENOBUFS) { |
| 10377 | asoc->ifp_had_enobuf = 1; |
| 10378 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 10379 | } |
| 10380 | return (error); |
| 10381 | } else { |
| 10382 | asoc->ifp_had_enobuf = 0; |
| 10383 | } |
| 10384 | endofchain = NULL; |
| 10385 | auth = NULL; |
| 10386 | auth_offset = 0; |
| 10387 | /* For HB's */ |
| 10388 | /* |
| 10389 | * We don't want to mark the net->sent time here |
| 10390 | * since this we use this for HB and retrans cannot |
| 10391 | * measure RTT |
| 10392 | */ |
| 10393 | /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */ |
| 10394 |
| 10395 | /* For auto-close */ |
| 10396 | cnt_thru++; |
| 10397 | if (*now_filled == 0) { |
| 10398 | (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent); |
| 10399 | *now = asoc->time_last_sent; |
| 10400 | *now_filled = 1; |
| 10401 | } else { |
| 10402 | asoc->time_last_sent = *now; |
| 10403 | } |
| 10404 | *cnt_out += bundle_at; |
| 10405 | #ifdef SCTP_AUDITING_ENABLED |
| 10406 | sctp_audit_log(0xC4, bundle_at); |
| 10407 | #endif |
| 10408 | if (bundle_at) { |
| 10409 | tsns_sent = data_list[0]->rec.data.tsn; |
| 10410 | } |
| 10411 | for (i = 0; i < bundle_at; i++) { |
| 10412 | SCTP_STAT_INCR(sctps_sendretransdata); |
| 10413 | data_list[i]->sent = SCTP_DATAGRAM_SENT; |
| 10414 | /* |
| 10415 | * When we have a revoked data, and we |
| 10416 | * retransmit it, then we clear the revoked |
| 10417 | * flag since this flag dictates if we |
| 10418 | * subtracted from the fs |
| 10419 | */ |
| 10420 | if (data_list[i]->rec.data.chunk_was_revoked) { |
| 10421 | /* Deflate the cwnd */ |
| 10422 | data_list[i]->whoTo->cwnd -= data_list[i]->book_size; |
| 10423 | data_list[i]->rec.data.chunk_was_revoked = 0; |
| 10424 | } |
| 10425 | data_list[i]->snd_count++; |
| 10426 | sctp_ucount_decr(asoc->sent_queue_retran_cnt); |
| 10427 | /* record the time */ |
| 10428 | data_list[i]->sent_rcv_time = asoc->time_last_sent; |
| 10429 | if (data_list[i]->book_size_scale) { |
| 10430 | /* |
| 10431 | * need to double the book size on |
| 10432 | * this one |
| 10433 | */ |
| 10434 | data_list[i]->book_size_scale = 0; |
| 10435 | /* Since we double the booksize, we must |
| 10436 | * also double the output queue size, since this |
| 10437 | * get shrunk when we free by this amount. |
| 10438 | */ |
| 10439 | atomic_add_int(&((asoc)->total_output_queue_size),data_list[i]->book_size); |
| 10440 | data_list[i]->book_size *= 2; |
| 10441 |
| 10442 |
| 10443 | } else { |
| 10444 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) { |
| 10445 | sctp_log_rwnd(SCTP_DECREASE_PEER_RWND, |
| 10446 | asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)); |
| 10447 | } |
| 10448 | asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd, |
| 10449 | (uint32_t) (data_list[i]->send_size + |
| 10450 | SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))); |
| 10451 | } |
| 10452 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { |
| 10453 | sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND, |
| 10454 | data_list[i]->whoTo->flight_size, |
| 10455 | data_list[i]->book_size, |
| 10456 | (uint32_t)(uintptr_t)data_list[i]->whoTo, |
| 10457 | data_list[i]->rec.data.tsn); |
| 10458 | } |
| 10459 | sctp_flight_size_increase(data_list[i]); |
| 10460 | sctp_total_flight_increase(stcb, data_list[i]); |
| 10461 | if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) { |
| 10462 | /* SWS sender side engages */ |
| 10463 | asoc->peers_rwnd = 0; |
| 10464 | } |
| 10465 | if ((i == 0) && |
| 10466 | (data_list[i]->rec.data.doing_fast_retransmit)) { |
| 10467 | SCTP_STAT_INCR(sctps_sendfastretrans); |
| 10468 | if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) && |
| 10469 | (tmr_started == 0)) { |
| 10470 | /*- |
| 10471 | * ok we just fast-retrans'd |
| 10472 | * the lowest TSN, i.e the |
| 10473 | * first on the list. In |
| 10474 | * this case we want to give |
| 10475 | * some more time to get a |
| 10476 | * SACK back without a |
| 10477 | * t3-expiring. |
| 10478 | */ |
| 10479 | sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net, |
| 10480 | SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2); |
| 10481 | sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); |
| 10482 | } |
| 10483 | } |
| 10484 | } |
| 10485 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 10486 | sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND); |
| 10487 | } |
| 10488 | #ifdef SCTP_AUDITING_ENABLED |
| 10489 | sctp_auditing(21, inp, stcb, NULL); |
| 10490 | #endif |
| 10491 | } else { |
| 10492 | /* None will fit */ |
| 10493 | return (1); |
| 10494 | } |
| 10495 | if (asoc->sent_queue_retran_cnt <= 0) { |
| 10496 | /* all done we have no more to retran */ |
| 10497 | asoc->sent_queue_retran_cnt = 0; |
| 10498 | break; |
| 10499 | } |
| 10500 | if (one_chunk) { |
| 10501 | /* No more room in rwnd */ |
| 10502 | return (1); |
| 10503 | } |
| 10504 | /* stop the for loop here. we sent out a packet */ |
| 10505 | break; |
| 10506 | } |
| 10507 | return (0); |
| 10508 | } |
| 10509 |
| 10510 | static void |
| 10511 | sctp_timer_validation(struct sctp_inpcb *inp, |
| 10512 | struct sctp_tcb *stcb, |
| 10513 | struct sctp_association *asoc) |
| 10514 | { |
| 10515 | struct sctp_nets *net; |
| 10516 |
| 10517 | /* Validate that a timer is running somewhere */ |
| 10518 | TAILQ_FOREACH(net, &asoc->nets, sctp_next) { |
| 10519 | if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { |
| 10520 | /* Here is a timer */ |
| 10521 | return; |
| 10522 | } |
| 10523 | } |
| 10524 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 10525 | /* Gak, we did not have a timer somewhere */ |
| 10526 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n"); |
| 10527 | if (asoc->alternate) { |
| 10528 | sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->alternate); |
| 10529 | } else { |
| 10530 | sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination); |
| 10531 | } |
| 10532 | return; |
| 10533 | } |
| 10534 |
| 10535 | void |
| 10536 | sctp_chunk_output(struct sctp_inpcb *inp, |
| 10537 | struct sctp_tcb *stcb, |
| 10538 | int from_where, |
| 10539 | int so_locked |
| 10540 | #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) |
| 10541 | SCTP_UNUSED |
| 10542 | #endif |
| 10543 | ) |
| 10544 | { |
| 10545 | /*- |
| 10546 | * Ok this is the generic chunk service queue. we must do the |
| 10547 | * following: |
| 10548 | * - See if there are retransmits pending, if so we must |
| 10549 | * do these first. |
| 10550 | * - Service the stream queue that is next, moving any |
| 10551 | * message (note I must get a complete message i.e. |
| 10552 | * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning |
| 10553 | * TSN's |
| 10554 | * - Check to see if the cwnd/rwnd allows any output, if so we |
| 10555 | * go ahead and fomulate and send the low level chunks. Making sure |
| 10556 | * to combine any control in the control chunk queue also. |
| 10557 | */ |
| 10558 | struct sctp_association *asoc; |
| 10559 | struct sctp_nets *net; |
| 10560 | int error = 0, num_out, tot_out = 0, ret = 0, reason_code; |
| 10561 | unsigned int burst_cnt = 0; |
| 10562 | struct timeval now; |
| 10563 | int now_filled = 0; |
| 10564 | int nagle_on; |
| 10565 | int frag_point = sctp_get_frag_point(stcb, &stcb->asoc); |
| 10566 | int un_sent = 0; |
| 10567 | int fr_done; |
| 10568 | unsigned int tot_frs = 0; |
| 10569 |
| 10570 | #if defined(__APPLE__) |
| 10571 | if (so_locked) { |
| 10572 | sctp_lock_assert(SCTP_INP_SO(inp)); |
| 10573 | } else { |
| 10574 | sctp_unlock_assert(SCTP_INP_SO(inp)); |
| 10575 | } |
| 10576 | #endif |
| 10577 | asoc = &stcb->asoc; |
| 10578 | do_it_again: |
| 10579 | /* The Nagle algorithm is only applied when handling a send call. */ |
| 10580 | if (from_where == SCTP_OUTPUT_FROM_USR_SEND) { |
| 10581 | if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) { |
| 10582 | nagle_on = 0; |
| 10583 | } else { |
| 10584 | nagle_on = 1; |
| 10585 | } |
| 10586 | } else { |
| 10587 | nagle_on = 0; |
| 10588 | } |
| 10589 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 10590 |
| 10591 | un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); |
| 10592 |
| 10593 | if ((un_sent <= 0) && |
| 10594 | (TAILQ_EMPTY(&asoc->control_send_queue)) && |
| 10595 | (TAILQ_EMPTY(&asoc->asconf_send_queue)) && |
| 10596 | (asoc->sent_queue_retran_cnt == 0) && |
| 10597 | (asoc->trigger_reset == 0)) { |
| 10598 | /* Nothing to do unless there is something to be sent left */ |
| 10599 | return; |
| 10600 | } |
| 10601 | /* Do we have something to send, data or control AND |
| 10602 | * a sack timer running, if so piggy-back the sack. |
| 10603 | */ |
| 10604 | if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) { |
| 10605 | sctp_send_sack(stcb, so_locked); |
| 10606 | (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer); |
| 10607 | } |
| 10608 | while (asoc->sent_queue_retran_cnt) { |
| 10609 | /*- |
| 10610 | * Ok, it is retransmission time only, we send out only ONE |
| 10611 | * packet with a single call off to the retran code. |
| 10612 | */ |
| 10613 | if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) { |
| 10614 | /*- |
| 10615 | * Special hook for handling cookiess discarded |
| 10616 | * by peer that carried data. Send cookie-ack only |
| 10617 | * and then the next call with get the retran's. |
| 10618 | */ |
| 10619 | (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, |
| 10620 | from_where, |
| 10621 | &now, &now_filled, frag_point, so_locked); |
| 10622 | return; |
| 10623 | } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) { |
| 10624 | /* if its not from a HB then do it */ |
| 10625 | fr_done = 0; |
| 10626 | ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked); |
| 10627 | if (fr_done) { |
| 10628 | tot_frs++; |
| 10629 | } |
| 10630 | } else { |
| 10631 | /* |
| 10632 | * its from any other place, we don't allow retran |
| 10633 | * output (only control) |
| 10634 | */ |
| 10635 | ret = 1; |
| 10636 | } |
| 10637 | if (ret > 0) { |
| 10638 | /* Can't send anymore */ |
| 10639 | /*- |
| 10640 | * now lets push out control by calling med-level |
| 10641 | * output once. this assures that we WILL send HB's |
| 10642 | * if queued too. |
| 10643 | */ |
| 10644 | (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, |
| 10645 | from_where, |
| 10646 | &now, &now_filled, frag_point, so_locked); |
| 10647 | #ifdef SCTP_AUDITING_ENABLED |
| 10648 | sctp_auditing(8, inp, stcb, NULL); |
| 10649 | #endif |
| 10650 | sctp_timer_validation(inp, stcb, asoc); |
| 10651 | return; |
| 10652 | } |
| 10653 | if (ret < 0) { |
| 10654 | /*- |
| 10655 | * The count was off.. retran is not happening so do |
| 10656 | * the normal retransmission. |
| 10657 | */ |
| 10658 | #ifdef SCTP_AUDITING_ENABLED |
| 10659 | sctp_auditing(9, inp, stcb, NULL); |
| 10660 | #endif |
| 10661 | if (ret == SCTP_RETRAN_EXIT) { |
| 10662 | return; |
| 10663 | } |
| 10664 | break; |
| 10665 | } |
| 10666 | if (from_where == SCTP_OUTPUT_FROM_T3) { |
| 10667 | /* Only one transmission allowed out of a timeout */ |
| 10668 | #ifdef SCTP_AUDITING_ENABLED |
| 10669 | sctp_auditing(10, inp, stcb, NULL); |
| 10670 | #endif |
| 10671 | /* Push out any control */ |
| 10672 | (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where, |
| 10673 | &now, &now_filled, frag_point, so_locked); |
| 10674 | return; |
| 10675 | } |
| 10676 | if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) { |
| 10677 | /* Hit FR burst limit */ |
| 10678 | return; |
| 10679 | } |
| 10680 | if ((num_out == 0) && (ret == 0)) { |
| 10681 | /* No more retrans to send */ |
| 10682 | break; |
| 10683 | } |
| 10684 | } |
| 10685 | #ifdef SCTP_AUDITING_ENABLED |
| 10686 | sctp_auditing(12, inp, stcb, NULL); |
| 10687 | #endif |
| 10688 | /* Check for bad destinations, if they exist move chunks around. */ |
| 10689 | TAILQ_FOREACH(net, &asoc->nets, sctp_next) { |
| 10690 | if (!(net->dest_state & SCTP_ADDR_REACHABLE)) { |
| 10691 | /*- |
| 10692 | * if possible move things off of this address we |
| 10693 | * still may send below due to the dormant state but |
| 10694 | * we try to find an alternate address to send to |
| 10695 | * and if we have one we move all queued data on the |
| 10696 | * out wheel to this alternate address. |
| 10697 | */ |
| 10698 | if (net->ref_count > 1) |
| 10699 | sctp_move_chunks_from_net(stcb, net); |
| 10700 | } else { |
| 10701 | /*- |
| 10702 | * if ((asoc->sat_network) || (net->addr_is_local)) |
| 10703 | * { burst_limit = asoc->max_burst * |
| 10704 | * SCTP_SAT_NETWORK_BURST_INCR; } |
| 10705 | */ |
| 10706 | if (asoc->max_burst > 0) { |
| 10707 | if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) { |
| 10708 | if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) { |
| 10709 | /* JRS - Use the congestion control given in the congestion control module */ |
| 10710 | asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst); |
| 10711 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) { |
| 10712 | sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED); |
| 10713 | } |
| 10714 | SCTP_STAT_INCR(sctps_maxburstqueued); |
| 10715 | } |
| 10716 | net->fast_retran_ip = 0; |
| 10717 | } else { |
| 10718 | if (net->flight_size == 0) { |
| 10719 | /* Should be decaying the cwnd here */ |
| 10720 | ; |
| 10721 | } |
| 10722 | } |
| 10723 | } |
| 10724 | } |
| 10725 |
| 10726 | } |
| 10727 | burst_cnt = 0; |
| 10728 | do { |
| 10729 | error = sctp_med_chunk_output(inp, stcb, asoc, &num_out, |
| 10730 | &reason_code, 0, from_where, |
| 10731 | &now, &now_filled, frag_point, so_locked); |
| 10732 | if (error) { |
| 10733 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error); |
| 10734 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) { |
| 10735 | sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP); |
| 10736 | } |
| 10737 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 10738 | sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES); |
| 10739 | sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES); |
| 10740 | } |
| 10741 | break; |
| 10742 | } |
| 10743 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out); |
| 10744 |
| 10745 | tot_out += num_out; |
| 10746 | burst_cnt++; |
| 10747 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 10748 | sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES); |
| 10749 | if (num_out == 0) { |
| 10750 | sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES); |
| 10751 | } |
| 10752 | } |
| 10753 | if (nagle_on) { |
| 10754 | /* |
| 10755 | * When the Nagle algorithm is used, look at how much |
| 10756 | * is unsent, then if its smaller than an MTU and we |
| 10757 | * have data in flight we stop, except if we are |
| 10758 | * handling a fragmented user message. |
| 10759 | */ |
| 10760 | un_sent = stcb->asoc.total_output_queue_size - stcb->asoc.total_flight; |
| 10761 | if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) && |
| 10762 | (stcb->asoc.total_flight > 0)) { |
| 10763 | /* && sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {*/ |
| 10764 | break; |
| 10765 | } |
| 10766 | } |
| 10767 | if (TAILQ_EMPTY(&asoc->control_send_queue) && |
| 10768 | TAILQ_EMPTY(&asoc->send_queue) && |
| 10769 | sctp_is_there_unsent_data(stcb, so_locked) == 0) { |
| 10770 | /* Nothing left to send */ |
| 10771 | break; |
| 10772 | } |
| 10773 | if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) { |
| 10774 | /* Nothing left to send */ |
| 10775 | break; |
| 10776 | } |
| 10777 | } while (num_out && |
| 10778 | ((asoc->max_burst == 0) || |
| 10779 | SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) || |
| 10780 | (burst_cnt < asoc->max_burst))); |
| 10781 |
| 10782 | if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) { |
| 10783 | if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) { |
| 10784 | SCTP_STAT_INCR(sctps_maxburstqueued); |
| 10785 | asoc->burst_limit_applied = 1; |
| 10786 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) { |
| 10787 | sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED); |
| 10788 | } |
| 10789 | } else { |
| 10790 | asoc->burst_limit_applied = 0; |
| 10791 | } |
| 10792 | } |
| 10793 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 10794 | sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES); |
| 10795 | } |
| 10796 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n", |
| 10797 | tot_out); |
| 10798 |
| 10799 | /*- |
| 10800 | * Now we need to clean up the control chunk chain if a ECNE is on |
| 10801 | * it. It must be marked as UNSENT again so next call will continue |
| 10802 | * to send it until such time that we get a CWR, to remove it. |
| 10803 | */ |
| 10804 | if (stcb->asoc.ecn_echo_cnt_onq) |
| 10805 | sctp_fix_ecn_echo(asoc); |
| 10806 |
| 10807 | if (stcb->asoc.trigger_reset) { |
| 10808 | if (sctp_send_stream_reset_out_if_possible(stcb, so_locked) == 0) { |
| 10809 | goto do_it_again; |
| 10810 | } |
| 10811 | } |
| 10812 | return; |
| 10813 | } |
| 10814 |
| 10815 |
| 10816 | int |
| 10817 | sctp_output( |
| 10818 | struct sctp_inpcb *inp, |
| 10819 | #if defined(__Panda__) |
| 10820 | pakhandle_type m, |
| 10821 | #else |
| 10822 | struct mbuf *m, |
| 10823 | #endif |
| 10824 | struct sockaddr *addr, |
| 10825 | #if defined(__Panda__) |
| 10826 | pakhandle_type control, |
| 10827 | #else |
| 10828 | struct mbuf *control, |
| 10829 | #endif |
| 10830 | #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 |
| 10831 | struct thread *p, |
| 10832 | #elif defined(__Windows__) |
| 10833 | PKTHREAD p, |
| 10834 | #else |
| 10835 | #if defined(__APPLE__) |
| 10836 | struct proc *p SCTP_UNUSED, |
| 10837 | #else |
| 10838 | struct proc *p, |
| 10839 | #endif |
| 10840 | #endif |
| 10841 | int flags) |
| 10842 | { |
| 10843 | if (inp == NULL) { |
| 10844 | SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 10845 | return (EINVAL); |
| 10846 | } |
| 10847 |
| 10848 | if (inp->sctp_socket == NULL) { |
| 10849 | SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 10850 | return (EINVAL); |
| 10851 | } |
| 10852 | return (sctp_sosend(inp->sctp_socket, |
| 10853 | addr, |
| 10854 | (struct uio *)NULL, |
| 10855 | m, |
| 10856 | control, |
| 10857 | #if defined(__APPLE__) || defined(__Panda__) |
| 10858 | flags |
| 10859 | #else |
| 10860 | flags, p |
| 10861 | #endif |
| 10862 | )); |
| 10863 | } |
| 10864 |
| 10865 | void |
| 10866 | send_forward_tsn(struct sctp_tcb *stcb, |
| 10867 | struct sctp_association *asoc) |
| 10868 | { |
| 10869 | struct sctp_tmit_chunk *chk, *at, *tp1, *last; |
| 10870 | struct sctp_forward_tsn_chunk *fwdtsn; |
| 10871 | struct sctp_strseq *strseq; |
| 10872 | struct sctp_strseq_mid *strseq_m; |
| 10873 | uint32_t advance_peer_ack_point; |
| 10874 | unsigned int cnt_of_space, i, ovh; |
| 10875 | unsigned int space_needed; |
| 10876 | unsigned int cnt_of_skipped = 0; |
| 10877 |
| 10878 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 10879 | TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { |
| 10880 | if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) { |
| 10881 | /* mark it to unsent */ |
| 10882 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 10883 | chk->snd_count = 0; |
| 10884 | /* Do we correct its output location? */ |
| 10885 | if (chk->whoTo) { |
| 10886 | sctp_free_remote_addr(chk->whoTo); |
| 10887 | chk->whoTo = NULL; |
| 10888 | } |
| 10889 | goto sctp_fill_in_rest; |
| 10890 | } |
| 10891 | } |
| 10892 | /* Ok if we reach here we must build one */ |
| 10893 | sctp_alloc_a_chunk(stcb, chk); |
| 10894 | if (chk == NULL) { |
| 10895 | return; |
| 10896 | } |
| 10897 | asoc->fwd_tsn_cnt++; |
| 10898 | chk->copy_by_ref = 0; |
| 10899 | /* |
| 10900 | * We don't do the old thing here since |
| 10901 | * this is used not for on-wire but to |
| 10902 | * tell if we are sending a fwd-tsn by |
| 10903 | * the stack during output. And if its |
| 10904 | * a IFORWARD or a FORWARD it is a fwd-tsn. |
| 10905 | */ |
| 10906 | chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN; |
| 10907 | chk->rec.chunk_id.can_take_data = 0; |
| 10908 | chk->flags = 0; |
| 10909 | chk->asoc = asoc; |
| 10910 | chk->whoTo = NULL; |
| 10911 | chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); |
| 10912 | if (chk->data == NULL) { |
| 10913 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 10914 | return; |
| 10915 | } |
| 10916 | SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); |
| 10917 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 10918 | chk->snd_count = 0; |
| 10919 | TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next); |
| 10920 | asoc->ctrl_queue_cnt++; |
| 10921 | sctp_fill_in_rest: |
| 10922 | /*- |
| 10923 | * Here we go through and fill out the part that deals with |
| 10924 | * stream/seq of the ones we skip. |
| 10925 | */ |
| 10926 | SCTP_BUF_LEN(chk->data) = 0; |
| 10927 | TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) { |
| 10928 | if ((at->sent != SCTP_FORWARD_TSN_SKIP) && |
| 10929 | (at->sent != SCTP_DATAGRAM_NR_ACKED)) { |
| 10930 | /* no more to look at */ |
| 10931 | break; |
| 10932 | } |
| 10933 | if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) { |
| 10934 | /* We don't report these */ |
| 10935 | continue; |
| 10936 | } |
| 10937 | cnt_of_skipped++; |
| 10938 | } |
| 10939 | if (asoc->idata_supported) { |
| 10940 | space_needed = (sizeof(struct sctp_forward_tsn_chunk) + |
| 10941 | (cnt_of_skipped * sizeof(struct sctp_strseq_mid))); |
| 10942 | } else { |
| 10943 | space_needed = (sizeof(struct sctp_forward_tsn_chunk) + |
| 10944 | (cnt_of_skipped * sizeof(struct sctp_strseq))); |
| 10945 | } |
| 10946 | cnt_of_space = (unsigned int)M_TRAILINGSPACE(chk->data); |
| 10947 |
| 10948 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { |
| 10949 | ovh = SCTP_MIN_OVERHEAD; |
| 10950 | } else { |
| 10951 | ovh = SCTP_MIN_V4_OVERHEAD; |
| 10952 | } |
| 10953 | if (cnt_of_space > (asoc->smallest_mtu - ovh)) { |
| 10954 | /* trim to a mtu size */ |
| 10955 | cnt_of_space = asoc->smallest_mtu - ovh; |
| 10956 | } |
| 10957 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) { |
| 10958 | sctp_misc_ints(SCTP_FWD_TSN_CHECK, |
| 10959 | 0xff, 0, cnt_of_skipped, |
| 10960 | asoc->advanced_peer_ack_point); |
| 10961 | } |
| 10962 | advance_peer_ack_point = asoc->advanced_peer_ack_point; |
| 10963 | if (cnt_of_space < space_needed) { |
| 10964 | /*- |
| 10965 | * ok we must trim down the chunk by lowering the |
| 10966 | * advance peer ack point. |
| 10967 | */ |
| 10968 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) { |
| 10969 | sctp_misc_ints(SCTP_FWD_TSN_CHECK, |
| 10970 | 0xff, 0xff, cnt_of_space, |
| 10971 | space_needed); |
| 10972 | } |
| 10973 | cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk); |
| 10974 | if (asoc->idata_supported) { |
| 10975 | cnt_of_skipped /= sizeof(struct sctp_strseq_mid); |
| 10976 | } else { |
| 10977 | cnt_of_skipped /= sizeof(struct sctp_strseq); |
| 10978 | } |
| 10979 | /*- |
| 10980 | * Go through and find the TSN that will be the one |
| 10981 | * we report. |
| 10982 | */ |
| 10983 | at = TAILQ_FIRST(&asoc->sent_queue); |
| 10984 | if (at != NULL) { |
| 10985 | for (i = 0; i < cnt_of_skipped; i++) { |
| 10986 | tp1 = TAILQ_NEXT(at, sctp_next); |
| 10987 | if (tp1 == NULL) { |
| 10988 | break; |
| 10989 | } |
| 10990 | at = tp1; |
| 10991 | } |
| 10992 | } |
| 10993 | if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) { |
| 10994 | sctp_misc_ints(SCTP_FWD_TSN_CHECK, |
| 10995 | 0xff, cnt_of_skipped, at->rec.data.tsn, |
| 10996 | asoc->advanced_peer_ack_point); |
| 10997 | } |
| 10998 | last = at; |
| 10999 | /*- |
| 11000 | * last now points to last one I can report, update |
| 11001 | * peer ack point |
| 11002 | */ |
| 11003 | if (last) { |
| 11004 | advance_peer_ack_point = last->rec.data.tsn; |
| 11005 | } |
| 11006 | if (asoc->idata_supported) { |
| 11007 | space_needed = sizeof(struct sctp_forward_tsn_chunk) + |
| 11008 | cnt_of_skipped * sizeof(struct sctp_strseq_mid); |
| 11009 | } else { |
| 11010 | space_needed = sizeof(struct sctp_forward_tsn_chunk) + |
| 11011 | cnt_of_skipped * sizeof(struct sctp_strseq); |
| 11012 | } |
| 11013 | } |
| 11014 | chk->send_size = space_needed; |
| 11015 | /* Setup the chunk */ |
| 11016 | fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *); |
| 11017 | fwdtsn->ch.chunk_length = htons(chk->send_size); |
| 11018 | fwdtsn->ch.chunk_flags = 0; |
| 11019 | if (asoc->idata_supported) { |
| 11020 | fwdtsn->ch.chunk_type = SCTP_IFORWARD_CUM_TSN; |
| 11021 | } else { |
| 11022 | fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN; |
| 11023 | } |
| 11024 | fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point); |
| 11025 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 11026 | fwdtsn++; |
| 11027 | /*- |
| 11028 | * Move pointer to after the fwdtsn and transfer to the |
| 11029 | * strseq pointer. |
| 11030 | */ |
| 11031 | if (asoc->idata_supported) { |
| 11032 | strseq_m = (struct sctp_strseq_mid *)fwdtsn; |
| 11033 | strseq = NULL; |
| 11034 | } else { |
| 11035 | strseq = (struct sctp_strseq *)fwdtsn; |
| 11036 | strseq_m = NULL; |
| 11037 | } |
| 11038 | /*- |
| 11039 | * Now populate the strseq list. This is done blindly |
| 11040 | * without pulling out duplicate stream info. This is |
| 11041 | * inefficent but won't harm the process since the peer will |
| 11042 | * look at these in sequence and will thus release anything. |
| 11043 | * It could mean we exceed the PMTU and chop off some that |
| 11044 | * we could have included.. but this is unlikely (aka 1432/4 |
| 11045 | * would mean 300+ stream seq's would have to be reported in |
| 11046 | * one FWD-TSN. With a bit of work we can later FIX this to |
| 11047 | * optimize and pull out duplicates.. but it does add more |
| 11048 | * overhead. So for now... not! |
| 11049 | */ |
| 11050 | i = 0; |
| 11051 | TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) { |
| 11052 | if (i >= cnt_of_skipped) { |
| 11053 | break; |
| 11054 | } |
| 11055 | if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) { |
| 11056 | /* We don't report these */ |
| 11057 | continue; |
| 11058 | } |
| 11059 | if (at->rec.data.tsn == advance_peer_ack_point) { |
| 11060 | at->rec.data.fwd_tsn_cnt = 0; |
| 11061 | } |
| 11062 | if (asoc->idata_supported) { |
| 11063 | strseq_m->sid = htons(at->rec.data.sid); |
| 11064 | if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) { |
| 11065 | strseq_m->flags = htons(PR_SCTP_UNORDERED_FLAG); |
| 11066 | } else { |
| 11067 | strseq_m->flags = 0; |
| 11068 | } |
| 11069 | strseq_m->mid = htonl(at->rec.data.mid); |
| 11070 | strseq_m++; |
| 11071 | } else { |
| 11072 | strseq->sid = htons(at->rec.data.sid); |
| 11073 | strseq->ssn = htons((uint16_t)at->rec.data.mid); |
| 11074 | strseq++; |
| 11075 | } |
| 11076 | i++; |
| 11077 | } |
| 11078 | return; |
| 11079 | } |
| 11080 |
| 11081 | void |
| 11082 | sctp_send_sack(struct sctp_tcb *stcb, int so_locked |
| 11083 | #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) |
| 11084 | SCTP_UNUSED |
| 11085 | #endif |
| 11086 | ) |
| 11087 | { |
| 11088 | /*- |
| 11089 | * Queue up a SACK or NR-SACK in the control queue. |
| 11090 | * We must first check to see if a SACK or NR-SACK is |
| 11091 | * somehow on the control queue. |
| 11092 | * If so, we will take and and remove the old one. |
| 11093 | */ |
| 11094 | struct sctp_association *asoc; |
| 11095 | struct sctp_tmit_chunk *chk, *a_chk; |
| 11096 | struct sctp_sack_chunk *sack; |
| 11097 | struct sctp_nr_sack_chunk *nr_sack; |
| 11098 | struct sctp_gap_ack_block *gap_descriptor; |
| 11099 | const struct sack_track *selector; |
| 11100 | int mergeable = 0; |
| 11101 | int offset; |
| 11102 | caddr_t limit; |
| 11103 | uint32_t *dup; |
| 11104 | int limit_reached = 0; |
| 11105 | unsigned int i, siz, j; |
| 11106 | unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space; |
| 11107 | int num_dups = 0; |
| 11108 | int space_req; |
| 11109 | uint32_t highest_tsn; |
| 11110 | uint8_t flags; |
| 11111 | uint8_t type; |
| 11112 | uint8_t tsn_map; |
| 11113 |
| 11114 | if (stcb->asoc.nrsack_supported == 1) { |
| 11115 | type = SCTP_NR_SELECTIVE_ACK; |
| 11116 | } else { |
| 11117 | type = SCTP_SELECTIVE_ACK; |
| 11118 | } |
| 11119 | a_chk = NULL; |
| 11120 | asoc = &stcb->asoc; |
| 11121 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 11122 | if (asoc->last_data_chunk_from == NULL) { |
| 11123 | /* Hmm we never received anything */ |
| 11124 | return; |
| 11125 | } |
| 11126 | sctp_slide_mapping_arrays(stcb); |
| 11127 | sctp_set_rwnd(stcb, asoc); |
| 11128 | TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { |
| 11129 | if (chk->rec.chunk_id.id == type) { |
| 11130 | /* Hmm, found a sack already on queue, remove it */ |
| 11131 | TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); |
| 11132 | asoc->ctrl_queue_cnt--; |
| 11133 | a_chk = chk; |
| 11134 | if (a_chk->data) { |
| 11135 | sctp_m_freem(a_chk->data); |
| 11136 | a_chk->data = NULL; |
| 11137 | } |
| 11138 | if (a_chk->whoTo) { |
| 11139 | sctp_free_remote_addr(a_chk->whoTo); |
| 11140 | a_chk->whoTo = NULL; |
| 11141 | } |
| 11142 | break; |
| 11143 | } |
| 11144 | } |
| 11145 | if (a_chk == NULL) { |
| 11146 | sctp_alloc_a_chunk(stcb, a_chk); |
| 11147 | if (a_chk == NULL) { |
| 11148 | /* No memory so we drop the idea, and set a timer */ |
| 11149 | if (stcb->asoc.delayed_ack) { |
| 11150 | sctp_timer_stop(SCTP_TIMER_TYPE_RECV, |
| 11151 | stcb->sctp_ep, stcb, NULL, |
| 11152 | SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3); |
| 11153 | sctp_timer_start(SCTP_TIMER_TYPE_RECV, |
| 11154 | stcb->sctp_ep, stcb, NULL); |
| 11155 | } else { |
| 11156 | stcb->asoc.send_sack = 1; |
| 11157 | } |
| 11158 | return; |
| 11159 | } |
| 11160 | a_chk->copy_by_ref = 0; |
| 11161 | a_chk->rec.chunk_id.id = type; |
| 11162 | a_chk->rec.chunk_id.can_take_data = 1; |
| 11163 | } |
| 11164 | /* Clear our pkt counts */ |
| 11165 | asoc->data_pkts_seen = 0; |
| 11166 |
| 11167 | a_chk->flags = 0; |
| 11168 | a_chk->asoc = asoc; |
| 11169 | a_chk->snd_count = 0; |
| 11170 | a_chk->send_size = 0; /* fill in later */ |
| 11171 | a_chk->sent = SCTP_DATAGRAM_UNSENT; |
| 11172 | a_chk->whoTo = NULL; |
| 11173 |
| 11174 | if (!(asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE)) { |
| 11175 | /*- |
| 11176 | * Ok, the destination for the SACK is unreachable, lets see if |
| 11177 | * we can select an alternate to asoc->last_data_chunk_from |
| 11178 | */ |
| 11179 | a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0); |
| 11180 | if (a_chk->whoTo == NULL) { |
| 11181 | /* Nope, no alternate */ |
| 11182 | a_chk->whoTo = asoc->last_data_chunk_from; |
| 11183 | } |
| 11184 | } else { |
| 11185 | a_chk->whoTo = asoc->last_data_chunk_from; |
| 11186 | } |
| 11187 | if (a_chk->whoTo) { |
| 11188 | atomic_add_int(&a_chk->whoTo->ref_count, 1); |
| 11189 | } |
| 11190 | if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) { |
| 11191 | highest_tsn = asoc->highest_tsn_inside_map; |
| 11192 | } else { |
| 11193 | highest_tsn = asoc->highest_tsn_inside_nr_map; |
| 11194 | } |
| 11195 | if (highest_tsn == asoc->cumulative_tsn) { |
| 11196 | /* no gaps */ |
| 11197 | if (type == SCTP_SELECTIVE_ACK) { |
| 11198 | space_req = sizeof(struct sctp_sack_chunk); |
| 11199 | } else { |
| 11200 | space_req = sizeof(struct sctp_nr_sack_chunk); |
| 11201 | } |
| 11202 | } else { |
| 11203 | /* gaps get a cluster */ |
| 11204 | space_req = MCLBYTES; |
| 11205 | } |
| 11206 | /* Ok now lets formulate a MBUF with our sack */ |
| 11207 | a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_NOWAIT, 1, MT_DATA); |
| 11208 | if ((a_chk->data == NULL) || |
| 11209 | (a_chk->whoTo == NULL)) { |
| 11210 | /* rats, no mbuf memory */ |
| 11211 | if (a_chk->data) { |
| 11212 | /* was a problem with the destination */ |
| 11213 | sctp_m_freem(a_chk->data); |
| 11214 | a_chk->data = NULL; |
| 11215 | } |
| 11216 | sctp_free_a_chunk(stcb, a_chk, so_locked); |
| 11217 | /* sa_ignore NO_NULL_CHK */ |
| 11218 | if (stcb->asoc.delayed_ack) { |
| 11219 | sctp_timer_stop(SCTP_TIMER_TYPE_RECV, |
| 11220 | stcb->sctp_ep, stcb, NULL, |
| 11221 | SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4); |
| 11222 | sctp_timer_start(SCTP_TIMER_TYPE_RECV, |
| 11223 | stcb->sctp_ep, stcb, NULL); |
| 11224 | } else { |
| 11225 | stcb->asoc.send_sack = 1; |
| 11226 | } |
| 11227 | return; |
| 11228 | } |
| 11229 | /* ok, lets go through and fill it in */ |
| 11230 | SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD); |
| 11231 | space = (unsigned int)M_TRAILINGSPACE(a_chk->data); |
| 11232 | if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) { |
| 11233 | space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD); |
| 11234 | } |
| 11235 | limit = mtod(a_chk->data, caddr_t); |
| 11236 | limit += space; |
| 11237 |
| 11238 | flags = 0; |
| 11239 |
| 11240 | if ((asoc->sctp_cmt_on_off > 0) && |
| 11241 | SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) { |
| 11242 | /*- |
| 11243 | * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been |
| 11244 | * received, then set high bit to 1, else 0. Reset |
| 11245 | * pkts_rcvd. |
| 11246 | */ |
| 11247 | flags |= (asoc->cmt_dac_pkts_rcvd << 6); |
| 11248 | asoc->cmt_dac_pkts_rcvd = 0; |
| 11249 | } |
| 11250 | #ifdef SCTP_ASOCLOG_OF_TSNS |
| 11251 | stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn; |
| 11252 | stcb->asoc.cumack_log_atsnt++; |
| 11253 | if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) { |
| 11254 | stcb->asoc.cumack_log_atsnt = 0; |
| 11255 | } |
| 11256 | #endif |
| 11257 | /* reset the readers interpretation */ |
| 11258 | stcb->freed_by_sorcv_sincelast = 0; |
| 11259 |
| 11260 | if (type == SCTP_SELECTIVE_ACK) { |
| 11261 | sack = mtod(a_chk->data, struct sctp_sack_chunk *); |
| 11262 | nr_sack = NULL; |
| 11263 | gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk)); |
| 11264 | if (highest_tsn > asoc->mapping_array_base_tsn) { |
| 11265 | siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8; |
| 11266 | } else { |
| 11267 | siz = (((MAX_TSN - highest_tsn) + 1) + highest_tsn + 7) / 8; |
| 11268 | } |
| 11269 | } else { |
| 11270 | sack = NULL; |
| 11271 | nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *); |
| 11272 | gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk)); |
| 11273 | if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) { |
| 11274 | siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8; |
| 11275 | } else { |
| 11276 | siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8; |
| 11277 | } |
| 11278 | } |
| 11279 |
| 11280 | if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) { |
| 11281 | offset = 1; |
| 11282 | } else { |
| 11283 | offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn; |
| 11284 | } |
| 11285 | if (((type == SCTP_SELECTIVE_ACK) && |
| 11286 | SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) || |
| 11287 | ((type == SCTP_NR_SELECTIVE_ACK) && |
| 11288 | SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) { |
| 11289 | /* we have a gap .. maybe */ |
| 11290 | for (i = 0; i < siz; i++) { |
| 11291 | tsn_map = asoc->mapping_array[i]; |
| 11292 | if (type == SCTP_SELECTIVE_ACK) { |
| 11293 | tsn_map |= asoc->nr_mapping_array[i]; |
| 11294 | } |
| 11295 | if (i == 0) { |
| 11296 | /* |
| 11297 | * Clear all bits corresponding to TSNs |
| 11298 | * smaller or equal to the cumulative TSN. |
| 11299 | */ |
| 11300 | tsn_map &= (~0U << (1 - offset)); |
| 11301 | } |
| 11302 | selector = &sack_array[tsn_map]; |
| 11303 | if (mergeable && selector->right_edge) { |
| 11304 | /* |
| 11305 | * Backup, left and right edges were ok to |
| 11306 | * merge. |
| 11307 | */ |
| 11308 | num_gap_blocks--; |
| 11309 | gap_descriptor--; |
| 11310 | } |
| 11311 | if (selector->num_entries == 0) |
| 11312 | mergeable = 0; |
| 11313 | else { |
| 11314 | for (j = 0; j < selector->num_entries; j++) { |
| 11315 | if (mergeable && selector->right_edge) { |
| 11316 | /* |
| 11317 | * do a merge by NOT setting |
| 11318 | * the left side |
| 11319 | */ |
| 11320 | mergeable = 0; |
| 11321 | } else { |
| 11322 | /* |
| 11323 | * no merge, set the left |
| 11324 | * side |
| 11325 | */ |
| 11326 | mergeable = 0; |
| 11327 | gap_descriptor->start = htons((selector->gaps[j].start + offset)); |
| 11328 | } |
| 11329 | gap_descriptor->end = htons((selector->gaps[j].end + offset)); |
| 11330 | num_gap_blocks++; |
| 11331 | gap_descriptor++; |
| 11332 | if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) { |
| 11333 | /* no more room */ |
| 11334 | limit_reached = 1; |
| 11335 | break; |
| 11336 | } |
| 11337 | } |
| 11338 | if (selector->left_edge) { |
| 11339 | mergeable = 1; |
| 11340 | } |
| 11341 | } |
| 11342 | if (limit_reached) { |
| 11343 | /* Reached the limit stop */ |
| 11344 | break; |
| 11345 | } |
| 11346 | offset += 8; |
| 11347 | } |
| 11348 | } |
| 11349 | if ((type == SCTP_NR_SELECTIVE_ACK) && |
| 11350 | (limit_reached == 0)) { |
| 11351 |
| 11352 | mergeable = 0; |
| 11353 |
| 11354 | if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) { |
| 11355 | siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8; |
| 11356 | } else { |
| 11357 | siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8; |
| 11358 | } |
| 11359 |
| 11360 | if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) { |
| 11361 | offset = 1; |
| 11362 | } else { |
| 11363 | offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn; |
| 11364 | } |
| 11365 | if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) { |
| 11366 | /* we have a gap .. maybe */ |
| 11367 | for (i = 0; i < siz; i++) { |
| 11368 | tsn_map = asoc->nr_mapping_array[i]; |
| 11369 | if (i == 0) { |
| 11370 | /* |
| 11371 | * Clear all bits corresponding to TSNs |
| 11372 | * smaller or equal to the cumulative TSN. |
| 11373 | */ |
| 11374 | tsn_map &= (~0U << (1 - offset)); |
| 11375 | } |
| 11376 | selector = &sack_array[tsn_map]; |
| 11377 | if (mergeable && selector->right_edge) { |
| 11378 | /* |
| 11379 | * Backup, left and right edges were ok to |
| 11380 | * merge. |
| 11381 | */ |
| 11382 | num_nr_gap_blocks--; |
| 11383 | gap_descriptor--; |
| 11384 | } |
| 11385 | if (selector->num_entries == 0) |
| 11386 | mergeable = 0; |
| 11387 | else { |
| 11388 | for (j = 0; j < selector->num_entries; j++) { |
| 11389 | if (mergeable && selector->right_edge) { |
| 11390 | /* |
| 11391 | * do a merge by NOT setting |
| 11392 | * the left side |
| 11393 | */ |
| 11394 | mergeable = 0; |
| 11395 | } else { |
| 11396 | /* |
| 11397 | * no merge, set the left |
| 11398 | * side |
| 11399 | */ |
| 11400 | mergeable = 0; |
| 11401 | gap_descriptor->start = htons((selector->gaps[j].start + offset)); |
| 11402 | } |
| 11403 | gap_descriptor->end = htons((selector->gaps[j].end + offset)); |
| 11404 | num_nr_gap_blocks++; |
| 11405 | gap_descriptor++; |
| 11406 | if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) { |
| 11407 | /* no more room */ |
| 11408 | limit_reached = 1; |
| 11409 | break; |
| 11410 | } |
| 11411 | } |
| 11412 | if (selector->left_edge) { |
| 11413 | mergeable = 1; |
| 11414 | } |
| 11415 | } |
| 11416 | if (limit_reached) { |
| 11417 | /* Reached the limit stop */ |
| 11418 | break; |
| 11419 | } |
| 11420 | offset += 8; |
| 11421 | } |
| 11422 | } |
| 11423 | } |
| 11424 | /* now we must add any dups we are going to report. */ |
| 11425 | if ((limit_reached == 0) && (asoc->numduptsns)) { |
| 11426 | dup = (uint32_t *) gap_descriptor; |
| 11427 | for (i = 0; i < asoc->numduptsns; i++) { |
| 11428 | *dup = htonl(asoc->dup_tsns[i]); |
| 11429 | dup++; |
| 11430 | num_dups++; |
| 11431 | if (((caddr_t)dup + sizeof(uint32_t)) > limit) { |
| 11432 | /* no more room */ |
| 11433 | break; |
| 11434 | } |
| 11435 | } |
| 11436 | asoc->numduptsns = 0; |
| 11437 | } |
| 11438 | /* |
| 11439 | * now that the chunk is prepared queue it to the control chunk |
| 11440 | * queue. |
| 11441 | */ |
| 11442 | if (type == SCTP_SELECTIVE_ACK) { |
| 11443 | a_chk->send_size = (uint16_t)(sizeof(struct sctp_sack_chunk) + |
| 11444 | (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) + |
| 11445 | num_dups * sizeof(int32_t)); |
| 11446 | SCTP_BUF_LEN(a_chk->data) = a_chk->send_size; |
| 11447 | sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn); |
| 11448 | sack->sack.a_rwnd = htonl(asoc->my_rwnd); |
| 11449 | sack->sack.num_gap_ack_blks = htons(num_gap_blocks); |
| 11450 | sack->sack.num_dup_tsns = htons(num_dups); |
| 11451 | sack->ch.chunk_type = type; |
| 11452 | sack->ch.chunk_flags = flags; |
| 11453 | sack->ch.chunk_length = htons(a_chk->send_size); |
| 11454 | } else { |
| 11455 | a_chk->send_size = (uint16_t)(sizeof(struct sctp_nr_sack_chunk) + |
| 11456 | (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) + |
| 11457 | num_dups * sizeof(int32_t)); |
| 11458 | SCTP_BUF_LEN(a_chk->data) = a_chk->send_size; |
| 11459 | nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn); |
| 11460 | nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd); |
| 11461 | nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks); |
| 11462 | nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks); |
| 11463 | nr_sack->nr_sack.num_dup_tsns = htons(num_dups); |
| 11464 | nr_sack->nr_sack.reserved = 0; |
| 11465 | nr_sack->ch.chunk_type = type; |
| 11466 | nr_sack->ch.chunk_flags = flags; |
| 11467 | nr_sack->ch.chunk_length = htons(a_chk->send_size); |
| 11468 | } |
| 11469 | TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next); |
| 11470 | asoc->my_last_reported_rwnd = asoc->my_rwnd; |
| 11471 | asoc->ctrl_queue_cnt++; |
| 11472 | asoc->send_sack = 0; |
| 11473 | SCTP_STAT_INCR(sctps_sendsacks); |
| 11474 | return; |
| 11475 | } |
| 11476 |
| 11477 | void |
| 11478 | sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked |
| 11479 | #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) |
| 11480 | SCTP_UNUSED |
| 11481 | #endif |
| 11482 | ) |
| 11483 | { |
| 11484 | struct mbuf *m_abort, *m, *m_last; |
| 11485 | struct mbuf *m_out, *m_end = NULL; |
| 11486 | struct sctp_abort_chunk *abort; |
| 11487 | struct sctp_auth_chunk *auth = NULL; |
| 11488 | struct sctp_nets *net; |
| 11489 | uint32_t vtag; |
| 11490 | uint32_t auth_offset = 0; |
| 11491 | int error; |
| 11492 | uint16_t cause_len, chunk_len, padding_len; |
| 11493 |
| 11494 | #if defined(__APPLE__) |
| 11495 | if (so_locked) { |
| 11496 | sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep)); |
| 11497 | } else { |
| 11498 | sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep)); |
| 11499 | } |
| 11500 | #endif |
| 11501 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 11502 | /*- |
| 11503 | * Add an AUTH chunk, if chunk requires it and save the offset into |
| 11504 | * the chain for AUTH |
| 11505 | */ |
| 11506 | if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION, |
| 11507 | stcb->asoc.peer_auth_chunks)) { |
| 11508 | m_out = sctp_add_auth_chunk(NULL, &m_end, &auth, &auth_offset, |
| 11509 | stcb, SCTP_ABORT_ASSOCIATION); |
| 11510 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 11511 | } else { |
| 11512 | m_out = NULL; |
| 11513 | } |
| 11514 | m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_NOWAIT, 1, MT_HEADER); |
| 11515 | if (m_abort == NULL) { |
| 11516 | if (m_out) { |
| 11517 | sctp_m_freem(m_out); |
| 11518 | } |
| 11519 | if (operr) { |
| 11520 | sctp_m_freem(operr); |
| 11521 | } |
| 11522 | return; |
| 11523 | } |
| 11524 | /* link in any error */ |
| 11525 | SCTP_BUF_NEXT(m_abort) = operr; |
| 11526 | cause_len = 0; |
| 11527 | m_last = NULL; |
| 11528 | for (m = operr; m; m = SCTP_BUF_NEXT(m)) { |
| 11529 | cause_len += (uint16_t)SCTP_BUF_LEN(m); |
| 11530 | if (SCTP_BUF_NEXT(m) == NULL) { |
| 11531 | m_last = m; |
| 11532 | } |
| 11533 | } |
| 11534 | SCTP_BUF_LEN(m_abort) = sizeof(struct sctp_abort_chunk); |
| 11535 | chunk_len = (uint16_t)sizeof(struct sctp_abort_chunk) + cause_len; |
| 11536 | padding_len = SCTP_SIZE32(chunk_len) - chunk_len; |
| 11537 | if (m_out == NULL) { |
| 11538 | /* NO Auth chunk prepended, so reserve space in front */ |
| 11539 | SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD); |
| 11540 | m_out = m_abort; |
| 11541 | } else { |
| 11542 | /* Put AUTH chunk at the front of the chain */ |
| 11543 | SCTP_BUF_NEXT(m_end) = m_abort; |
| 11544 | } |
| 11545 | if (stcb->asoc.alternate) { |
| 11546 | net = stcb->asoc.alternate; |
| 11547 | } else { |
| 11548 | net = stcb->asoc.primary_destination; |
| 11549 | } |
| 11550 | /* Fill in the ABORT chunk header. */ |
| 11551 | abort = mtod(m_abort, struct sctp_abort_chunk *); |
| 11552 | abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION; |
| 11553 | if (stcb->asoc.peer_vtag == 0) { |
| 11554 | /* This happens iff the assoc is in COOKIE-WAIT state. */ |
| 11555 | vtag = stcb->asoc.my_vtag; |
| 11556 | abort->ch.chunk_flags = SCTP_HAD_NO_TCB; |
| 11557 | } else { |
| 11558 | vtag = stcb->asoc.peer_vtag; |
| 11559 | abort->ch.chunk_flags = 0; |
| 11560 | } |
| 11561 | abort->ch.chunk_length = htons(chunk_len); |
| 11562 | /* Add padding, if necessary. */ |
| 11563 | if (padding_len > 0) { |
| 11564 | if ((m_last == NULL) || |
| 11565 | (sctp_add_pad_tombuf(m_last, padding_len) == NULL)) { |
| 11566 | sctp_m_freem(m_out); |
| 11567 | return; |
| 11568 | } |
| 11569 | } |
| 11570 | if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net, |
| 11571 | (struct sockaddr *)&net->ro._l_addr, |
| 11572 | m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, 0, |
| 11573 | stcb->sctp_ep->sctp_lport, stcb->rport, htonl(vtag), |
| 11574 | stcb->asoc.primary_destination->port, NULL, |
| 11575 | #if defined(__FreeBSD__) |
| 11576 | 0, 0, |
| 11577 | #endif |
| 11578 | so_locked))) { |
| 11579 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error); |
| 11580 | if (error == ENOBUFS) { |
| 11581 | stcb->asoc.ifp_had_enobuf = 1; |
| 11582 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 11583 | } |
| 11584 | } else { |
| 11585 | stcb->asoc.ifp_had_enobuf = 0; |
| 11586 | } |
| 11587 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 11588 | } |
| 11589 |
| 11590 | void |
| 11591 | sctp_send_shutdown_complete(struct sctp_tcb *stcb, |
| 11592 | struct sctp_nets *net, |
| 11593 | int reflect_vtag) |
| 11594 | { |
| 11595 | /* formulate and SEND a SHUTDOWN-COMPLETE */ |
| 11596 | struct mbuf *m_shutdown_comp; |
| 11597 | struct sctp_shutdown_complete_chunk *shutdown_complete; |
| 11598 | uint32_t vtag; |
| 11599 | int error; |
| 11600 | uint8_t flags; |
| 11601 |
| 11602 | m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER); |
| 11603 | if (m_shutdown_comp == NULL) { |
| 11604 | /* no mbuf's */ |
| 11605 | return; |
| 11606 | } |
| 11607 | if (reflect_vtag) { |
| 11608 | flags = SCTP_HAD_NO_TCB; |
| 11609 | vtag = stcb->asoc.my_vtag; |
| 11610 | } else { |
| 11611 | flags = 0; |
| 11612 | vtag = stcb->asoc.peer_vtag; |
| 11613 | } |
| 11614 | shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *); |
| 11615 | shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE; |
| 11616 | shutdown_complete->ch.chunk_flags = flags; |
| 11617 | shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk)); |
| 11618 | SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk); |
| 11619 | if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net, |
| 11620 | (struct sockaddr *)&net->ro._l_addr, |
| 11621 | m_shutdown_comp, 0, NULL, 0, 1, 0, 0, |
| 11622 | stcb->sctp_ep->sctp_lport, stcb->rport, |
| 11623 | htonl(vtag), |
| 11624 | net->port, NULL, |
| 11625 | #if defined(__FreeBSD__) |
| 11626 | 0, 0, |
| 11627 | #endif |
| 11628 | SCTP_SO_NOT_LOCKED))) { |
| 11629 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error); |
| 11630 | if (error == ENOBUFS) { |
| 11631 | stcb->asoc.ifp_had_enobuf = 1; |
| 11632 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 11633 | } |
| 11634 | } else { |
| 11635 | stcb->asoc.ifp_had_enobuf = 0; |
| 11636 | } |
| 11637 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 11638 | return; |
| 11639 | } |
| 11640 |
| 11641 | #if defined(__FreeBSD__) |
| 11642 | static void |
| 11643 | sctp_send_resp_msg(struct sockaddr *src, struct sockaddr *dst, |
| 11644 | struct sctphdr *sh, uint32_t vtag, |
| 11645 | uint8_t type, struct mbuf *cause, |
| 11646 | uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum, |
| 11647 | uint32_t vrf_id, uint16_t port) |
| 11648 | #else |
| 11649 | static void |
| 11650 | sctp_send_resp_msg(struct sockaddr *src, struct sockaddr *dst, |
| 11651 | struct sctphdr *sh, uint32_t vtag, |
| 11652 | uint8_t type, struct mbuf *cause, |
| 11653 | uint32_t vrf_id SCTP_UNUSED, uint16_t port) |
| 11654 | #endif |
| 11655 | { |
| 11656 | #ifdef __Panda__ |
| 11657 | pakhandle_type o_pak; |
| 11658 | #else |
| 11659 | struct mbuf *o_pak; |
| 11660 | #endif |
| 11661 | struct mbuf *mout; |
| 11662 | struct sctphdr *shout; |
| 11663 | struct sctp_chunkhdr *ch; |
| 11664 | #if defined(INET) || defined(INET6) |
| 11665 | struct udphdr *udp; |
| 11666 | #endif |
| 11667 | int ret, len, cause_len, padding_len; |
| 11668 | #ifdef INET |
| 11669 | #if defined(__APPLE__) || defined(__Panda__) |
| 11670 | sctp_route_t ro; |
| 11671 | #endif |
| 11672 | struct sockaddr_in *src_sin, *dst_sin; |
| 11673 | struct ip *ip; |
| 11674 | #endif |
| 11675 | #ifdef INET6 |
| 11676 | struct sockaddr_in6 *src_sin6, *dst_sin6; |
| 11677 | struct ip6_hdr *ip6; |
| 11678 | #endif |
| 11679 |
| 11680 | /* Compute the length of the cause and add final padding. */ |
| 11681 | cause_len = 0; |
| 11682 | if (cause != NULL) { |
| 11683 | struct mbuf *m_at, *m_last = NULL; |
| 11684 |
| 11685 | for (m_at = cause; m_at; m_at = SCTP_BUF_NEXT(m_at)) { |
| 11686 | if (SCTP_BUF_NEXT(m_at) == NULL) |
| 11687 | m_last = m_at; |
| 11688 | cause_len += SCTP_BUF_LEN(m_at); |
| 11689 | } |
| 11690 | padding_len = cause_len % 4; |
| 11691 | if (padding_len != 0) { |
| 11692 | padding_len = 4 - padding_len; |
| 11693 | } |
| 11694 | if (padding_len != 0) { |
| 11695 | if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) { |
| 11696 | sctp_m_freem(cause); |
| 11697 | return; |
| 11698 | } |
| 11699 | } |
| 11700 | } else { |
| 11701 | padding_len = 0; |
| 11702 | } |
| 11703 | /* Get an mbuf for the header. */ |
| 11704 | len = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); |
| 11705 | switch (dst->sa_family) { |
| 11706 | #ifdef INET |
| 11707 | case AF_INET: |
| 11708 | len += sizeof(struct ip); |
| 11709 | break; |
| 11710 | #endif |
| 11711 | #ifdef INET6 |
| 11712 | case AF_INET6: |
| 11713 | len += sizeof(struct ip6_hdr); |
| 11714 | break; |
| 11715 | #endif |
| 11716 | default: |
| 11717 | break; |
| 11718 | } |
| 11719 | #if defined(INET) || defined(INET6) |
| 11720 | if (port) { |
| 11721 | len += sizeof(struct udphdr); |
| 11722 | } |
| 11723 | #endif |
| 11724 | #if defined(__APPLE__) |
| 11725 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
| 11726 | mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_NOWAIT, 1, MT_DATA); |
| 11727 | #else |
| 11728 | mout = sctp_get_mbuf_for_msg(len + SCTP_MAX_LINKHDR, 1, M_NOWAIT, 1, MT_DATA); |
| 11729 | #endif |
| 11730 | #else |
| 11731 | mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_NOWAIT, 1, MT_DATA); |
| 11732 | #endif |
| 11733 | if (mout == NULL) { |
| 11734 | if (cause) { |
| 11735 | sctp_m_freem(cause); |
| 11736 | } |
| 11737 | return; |
| 11738 | } |
| 11739 | #if defined(__APPLE__) |
| 11740 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
| 11741 | SCTP_BUF_RESV_UF(mout, max_linkhdr); |
| 11742 | #else |
| 11743 | SCTP_BUF_RESV_UF(mout, SCTP_MAX_LINKHDR); |
| 11744 | #endif |
| 11745 | #else |
| 11746 | SCTP_BUF_RESV_UF(mout, max_linkhdr); |
| 11747 | #endif |
| 11748 | SCTP_BUF_LEN(mout) = len; |
| 11749 | SCTP_BUF_NEXT(mout) = cause; |
| 11750 | #if defined(__FreeBSD__) |
| 11751 | M_SETFIB(mout, fibnum); |
| 11752 | mout->m_pkthdr.flowid = mflowid; |
| 11753 | M_HASHTYPE_SET(mout, mflowtype); |
| 11754 | #endif |
| 11755 | #ifdef INET |
| 11756 | ip = NULL; |
| 11757 | #endif |
| 11758 | #ifdef INET6 |
| 11759 | ip6 = NULL; |
| 11760 | #endif |
| 11761 | switch (dst->sa_family) { |
| 11762 | #ifdef INET |
| 11763 | case AF_INET: |
| 11764 | src_sin = (struct sockaddr_in *)src; |
| 11765 | dst_sin = (struct sockaddr_in *)dst; |
| 11766 | ip = mtod(mout, struct ip *); |
| 11767 | ip->ip_v = IPVERSION; |
| 11768 | ip->ip_hl = (sizeof(struct ip) >> 2); |
| 11769 | ip->ip_tos = 0; |
| 11770 | #if defined(__FreeBSD__) |
| 11771 | #if __FreeBSD_version >= 1000000 |
| 11772 | ip->ip_off = htons(IP_DF); |
| 11773 | #else |
| 11774 | ip->ip_off = IP_DF; |
| 11775 | #endif |
| 11776 | #elif defined(WITH_CONVERT_IP_OFF) || defined(__APPLE__) || defined(__Userspace_os_Darwin) |
| 11777 | ip->ip_off = IP_DF; |
| 11778 | #else |
| 11779 | ip->ip_off = htons(IP_DF); |
| 11780 | #endif |
| 11781 | #if defined(__FreeBSD__) |
| 11782 | ip_fillid(ip); |
| 11783 | #elif defined(__APPLE__) |
| 11784 | #if RANDOM_IP_ID |
| 11785 | ip->ip_id = ip_randomid(); |
| 11786 | #else |
| 11787 | ip->ip_id = htons(ip_id++); |
| 11788 | #endif |
| 11789 | #elif defined(__Userspace__) |
| 11790 | ip->ip_id = htons(ip_id++); |
| 11791 | #else |
| 11792 | ip->ip_id = ip_id++; |
| 11793 | #endif |
| 11794 | ip->ip_ttl = MODULE_GLOBAL(ip_defttl); |
| 11795 | if (port) { |
| 11796 | ip->ip_p = IPPROTO_UDP; |
| 11797 | } else { |
| 11798 | ip->ip_p = IPPROTO_SCTP; |
| 11799 | } |
| 11800 | ip->ip_src.s_addr = dst_sin->sin_addr.s_addr; |
| 11801 | ip->ip_dst.s_addr = src_sin->sin_addr.s_addr; |
| 11802 | ip->ip_sum = 0; |
| 11803 | len = sizeof(struct ip); |
| 11804 | shout = (struct sctphdr *)((caddr_t)ip + len); |
| 11805 | break; |
| 11806 | #endif |
| 11807 | #ifdef INET6 |
| 11808 | case AF_INET6: |
| 11809 | src_sin6 = (struct sockaddr_in6 *)src; |
| 11810 | dst_sin6 = (struct sockaddr_in6 *)dst; |
| 11811 | ip6 = mtod(mout, struct ip6_hdr *); |
| 11812 | ip6->ip6_flow = htonl(0x60000000); |
| 11813 | #if defined(__FreeBSD__) |
| 11814 | if (V_ip6_auto_flowlabel) { |
| 11815 | ip6->ip6_flow |= (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); |
| 11816 | } |
| 11817 | #endif |
| 11818 | #if defined(__Userspace__) |
| 11819 | ip6->ip6_hlim = IPv6_HOP_LIMIT; |
| 11820 | #else |
| 11821 | ip6->ip6_hlim = MODULE_GLOBAL(ip6_defhlim); |
| 11822 | #endif |
| 11823 | if (port) { |
| 11824 | ip6->ip6_nxt = IPPROTO_UDP; |
| 11825 | } else { |
| 11826 | ip6->ip6_nxt = IPPROTO_SCTP; |
| 11827 | } |
| 11828 | ip6->ip6_src = dst_sin6->sin6_addr; |
| 11829 | ip6->ip6_dst = src_sin6->sin6_addr; |
| 11830 | len = sizeof(struct ip6_hdr); |
| 11831 | shout = (struct sctphdr *)((caddr_t)ip6 + len); |
| 11832 | break; |
| 11833 | #endif |
| 11834 | default: |
| 11835 | len = 0; |
| 11836 | shout = mtod(mout, struct sctphdr *); |
| 11837 | break; |
| 11838 | } |
| 11839 | #if defined(INET) || defined(INET6) |
| 11840 | if (port) { |
| 11841 | if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) { |
| 11842 | sctp_m_freem(mout); |
| 11843 | return; |
| 11844 | } |
| 11845 | udp = (struct udphdr *)shout; |
| 11846 | udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)); |
| 11847 | udp->uh_dport = port; |
| 11848 | udp->uh_sum = 0; |
| 11849 | udp->uh_ulen = htons((uint16_t)(sizeof(struct udphdr) + |
| 11850 | sizeof(struct sctphdr) + |
| 11851 | sizeof(struct sctp_chunkhdr) + |
| 11852 | cause_len + padding_len)); |
| 11853 | len += sizeof(struct udphdr); |
| 11854 | shout = (struct sctphdr *)((caddr_t)shout + sizeof(struct udphdr)); |
| 11855 | } else { |
| 11856 | udp = NULL; |
| 11857 | } |
| 11858 | #endif |
| 11859 | shout->src_port = sh->dest_port; |
| 11860 | shout->dest_port = sh->src_port; |
| 11861 | shout->checksum = 0; |
| 11862 | if (vtag) { |
| 11863 | shout->v_tag = htonl(vtag); |
| 11864 | } else { |
| 11865 | shout->v_tag = sh->v_tag; |
| 11866 | } |
| 11867 | len += sizeof(struct sctphdr); |
| 11868 | ch = (struct sctp_chunkhdr *)((caddr_t)shout + sizeof(struct sctphdr)); |
| 11869 | ch->chunk_type = type; |
| 11870 | if (vtag) { |
| 11871 | ch->chunk_flags = 0; |
| 11872 | } else { |
| 11873 | ch->chunk_flags = SCTP_HAD_NO_TCB; |
| 11874 | } |
| 11875 | ch->chunk_length = htons((uint16_t)(sizeof(struct sctp_chunkhdr) + cause_len)); |
| 11876 | len += sizeof(struct sctp_chunkhdr); |
| 11877 | len += cause_len + padding_len; |
| 11878 |
| 11879 | if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) { |
| 11880 | sctp_m_freem(mout); |
| 11881 | return; |
| 11882 | } |
| 11883 | SCTP_ATTACH_CHAIN(o_pak, mout, len); |
| 11884 | switch (dst->sa_family) { |
| 11885 | #ifdef INET |
| 11886 | case AF_INET: |
| 11887 | #if defined(__APPLE__) || defined(__Panda__) |
| 11888 | /* zap the stack pointer to the route */ |
| 11889 | memset(&ro, 0, sizeof(sctp_route_t)); |
| 11890 | #if defined(__Panda__) |
| 11891 | ro._l_addr.sa.sa_family = AF_INET; |
| 11892 | #endif |
| 11893 | #endif |
| 11894 | if (port) { |
| 11895 | #if !defined(__Windows__) && !defined(__Userspace__) |
| 11896 | #if defined(__FreeBSD__) && ((__FreeBSD_version > 803000 && __FreeBSD_version < 900000) || __FreeBSD_version > 900000) |
| 11897 | if (V_udp_cksum) { |
| 11898 | udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP)); |
| 11899 | } else { |
| 11900 | udp->uh_sum = 0; |
| 11901 | } |
| 11902 | #else |
| 11903 | udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP)); |
| 11904 | #endif |
| 11905 | #else |
| 11906 | udp->uh_sum = 0; |
| 11907 | #endif |
| 11908 | } |
| 11909 | #if defined(__FreeBSD__) |
| 11910 | #if __FreeBSD_version >= 1000000 |
| 11911 | ip->ip_len = htons(len); |
| 11912 | #else |
| 11913 | ip->ip_len = len; |
| 11914 | #endif |
| 11915 | #elif defined(__APPLE__) || defined(__Userspace__) |
| 11916 | ip->ip_len = len; |
| 11917 | #else |
| 11918 | ip->ip_len = htons(len); |
| 11919 | #endif |
| 11920 | if (port) { |
| 11921 | shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip) + sizeof(struct udphdr)); |
| 11922 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 11923 | #if !defined(__Windows__) && !defined(__Userspace__) |
| 11924 | #if defined(__FreeBSD__) && ((__FreeBSD_version > 803000 && __FreeBSD_version < 900000) || __FreeBSD_version > 900000) |
| 11925 | if (V_udp_cksum) { |
| 11926 | SCTP_ENABLE_UDP_CSUM(o_pak); |
| 11927 | } |
| 11928 | #else |
| 11929 | SCTP_ENABLE_UDP_CSUM(o_pak); |
| 11930 | #endif |
| 11931 | #endif |
| 11932 | } else { |
| 11933 | #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 |
| 11934 | mout->m_pkthdr.csum_flags = CSUM_SCTP; |
| 11935 | mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum); |
| 11936 | SCTP_STAT_INCR(sctps_sendhwcrc); |
| 11937 | #else |
| 11938 | shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip)); |
| 11939 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 11940 | #endif |
| 11941 | } |
| 11942 | #ifdef SCTP_PACKET_LOGGING |
| 11943 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) { |
| 11944 | sctp_packet_log(o_pak); |
| 11945 | } |
| 11946 | #endif |
| 11947 | #if defined(__APPLE__) || defined(__Panda__) |
| 11948 | SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id); |
| 11949 | /* Free the route if we got one back */ |
| 11950 | if (ro.ro_rt) { |
| 11951 | RTFREE(ro.ro_rt); |
| 11952 | ro.ro_rt = NULL; |
| 11953 | } |
| 11954 | #else |
| 11955 | #if defined(__FreeBSD__) |
| 11956 | SCTP_PROBE5(send, NULL, NULL, ip, NULL, shout); |
| 11957 | #endif |
| 11958 | SCTP_IP_OUTPUT(ret, o_pak, NULL, NULL, vrf_id); |
| 11959 | #endif |
| 11960 | break; |
| 11961 | #endif |
| 11962 | #ifdef INET6 |
| 11963 | case AF_INET6: |
| 11964 | ip6->ip6_plen = htons((uint16_t)(len - sizeof(struct ip6_hdr))); |
| 11965 | if (port) { |
| 11966 | shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr)); |
| 11967 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 11968 | #if defined(__Windows__) |
| 11969 | udp->uh_sum = 0; |
| 11970 | #elif !defined(__Userspace__) |
| 11971 | if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) { |
| 11972 | udp->uh_sum = 0xffff; |
| 11973 | } |
| 11974 | #endif |
| 11975 | } else { |
| 11976 | #if defined(__FreeBSD__) && __FreeBSD_version >= 900000 |
| 11977 | #if __FreeBSD_version > 901000 |
| 11978 | mout->m_pkthdr.csum_flags = CSUM_SCTP_IPV6; |
| 11979 | #else |
| 11980 | mout->m_pkthdr.csum_flags = CSUM_SCTP; |
| 11981 | #endif |
| 11982 | mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum); |
| 11983 | SCTP_STAT_INCR(sctps_sendhwcrc); |
| 11984 | #else |
| 11985 | shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr)); |
| 11986 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 11987 | #endif |
| 11988 | } |
| 11989 | #ifdef SCTP_PACKET_LOGGING |
| 11990 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) { |
| 11991 | sctp_packet_log(o_pak); |
| 11992 | } |
| 11993 | #endif |
| 11994 | #if defined(__FreeBSD__) |
| 11995 | SCTP_PROBE5(send, NULL, NULL, ip6, NULL, shout); |
| 11996 | #endif |
| 11997 | SCTP_IP6_OUTPUT(ret, o_pak, NULL, NULL, NULL, vrf_id); |
| 11998 | break; |
| 11999 | #endif |
| 12000 | #if defined(__Userspace__) |
| 12001 | case AF_CONN: |
| 12002 | { |
| 12003 | char *buffer; |
| 12004 | struct sockaddr_conn *sconn; |
| 12005 |
| 12006 | sconn = (struct sockaddr_conn *)src; |
| 12007 | if (SCTP_BASE_VAR(crc32c_offloaded) == 0) { |
| 12008 | shout->checksum = sctp_calculate_cksum(mout, 0); |
| 12009 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 12010 | } else { |
| 12011 | SCTP_STAT_INCR(sctps_sendhwcrc); |
| 12012 | } |
| 12013 | #ifdef SCTP_PACKET_LOGGING |
| 12014 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) { |
| 12015 | sctp_packet_log(mout); |
| 12016 | } |
| 12017 | #endif |
| 12018 | /* Don't alloc/free for each packet */ |
| 12019 | if ((buffer = malloc(len)) != NULL) { |
| 12020 | m_copydata(mout, 0, len, buffer); |
| 12021 | ret = SCTP_BASE_VAR(conn_output)(sconn->sconn_addr, buffer, len, 0, 0); |
| 12022 | free(buffer); |
| 12023 | } else { |
| 12024 | ret = ENOMEM; |
| 12025 | } |
| 12026 | sctp_m_freem(mout); |
| 12027 | break; |
| 12028 | } |
| 12029 | #endif |
| 12030 | default: |
| 12031 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n", |
| 12032 | dst->sa_family); |
| 12033 | sctp_m_freem(mout); |
| 12034 | SCTP_LTRACE_ERR_RET_PKT(mout, NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT); |
| 12035 | return; |
| 12036 | } |
| 12037 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret); |
| 12038 | #if defined(__FreeBSD__) |
| 12039 | if (port) { |
| 12040 | UDPSTAT_INC(udps_opackets); |
| 12041 | } |
| 12042 | #endif |
| 12043 | SCTP_STAT_INCR(sctps_sendpackets); |
| 12044 | SCTP_STAT_INCR_COUNTER64(sctps_outpackets); |
| 12045 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 12046 | if (ret) { |
| 12047 | SCTP_STAT_INCR(sctps_senderrors); |
| 12048 | } |
| 12049 | return; |
| 12050 | } |
| 12051 |
| 12052 | void |
| 12053 | sctp_send_shutdown_complete2(struct sockaddr *src, struct sockaddr *dst, |
| 12054 | struct sctphdr *sh, |
| 12055 | #if defined(__FreeBSD__) |
| 12056 | uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum, |
| 12057 | #endif |
| 12058 | uint32_t vrf_id, uint16_t port) |
| 12059 | { |
| 12060 | sctp_send_resp_msg(src, dst, sh, 0, SCTP_SHUTDOWN_COMPLETE, NULL, |
| 12061 | #if defined(__FreeBSD__) |
| 12062 | mflowtype, mflowid, fibnum, |
| 12063 | #endif |
| 12064 | vrf_id, port); |
| 12065 | } |
| 12066 |
| 12067 | void |
| 12068 | sctp_send_hb(struct sctp_tcb *stcb, struct sctp_nets *net,int so_locked |
| 12069 | #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) |
| 12070 | SCTP_UNUSED |
| 12071 | #endif |
| 12072 | ) |
| 12073 | { |
| 12074 | struct sctp_tmit_chunk *chk; |
| 12075 | struct sctp_heartbeat_chunk *hb; |
| 12076 | struct timeval now; |
| 12077 |
| 12078 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 12079 | if (net == NULL) { |
| 12080 | return; |
| 12081 | } |
| 12082 | (void)SCTP_GETTIME_TIMEVAL(&now); |
| 12083 | switch (net->ro._l_addr.sa.sa_family) { |
| 12084 | #ifdef INET |
| 12085 | case AF_INET: |
| 12086 | break; |
| 12087 | #endif |
| 12088 | #ifdef INET6 |
| 12089 | case AF_INET6: |
| 12090 | break; |
| 12091 | #endif |
| 12092 | #if defined(__Userspace__) |
| 12093 | case AF_CONN: |
| 12094 | break; |
| 12095 | #endif |
| 12096 | default: |
| 12097 | return; |
| 12098 | } |
| 12099 | sctp_alloc_a_chunk(stcb, chk); |
| 12100 | if (chk == NULL) { |
| 12101 | SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n"); |
| 12102 | return; |
| 12103 | } |
| 12104 |
| 12105 | chk->copy_by_ref = 0; |
| 12106 | chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST; |
| 12107 | chk->rec.chunk_id.can_take_data = 1; |
| 12108 | chk->flags = 0; |
| 12109 | chk->asoc = &stcb->asoc; |
| 12110 | chk->send_size = sizeof(struct sctp_heartbeat_chunk); |
| 12111 |
| 12112 | chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER); |
| 12113 | if (chk->data == NULL) { |
| 12114 | sctp_free_a_chunk(stcb, chk, so_locked); |
| 12115 | return; |
| 12116 | } |
| 12117 | SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); |
| 12118 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12119 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 12120 | chk->snd_count = 0; |
| 12121 | chk->whoTo = net; |
| 12122 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 12123 | /* Now we have a mbuf that we can fill in with the details */ |
| 12124 | hb = mtod(chk->data, struct sctp_heartbeat_chunk *); |
| 12125 | memset(hb, 0, sizeof(struct sctp_heartbeat_chunk)); |
| 12126 | /* fill out chunk header */ |
| 12127 | hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST; |
| 12128 | hb->ch.chunk_flags = 0; |
| 12129 | hb->ch.chunk_length = htons(chk->send_size); |
| 12130 | /* Fill out hb parameter */ |
| 12131 | hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO); |
| 12132 | hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param)); |
| 12133 | hb->heartbeat.hb_info.time_value_1 = now.tv_sec; |
| 12134 | hb->heartbeat.hb_info.time_value_2 = now.tv_usec; |
| 12135 | /* Did our user request this one, put it in */ |
| 12136 | hb->heartbeat.hb_info.addr_family = (uint8_t)net->ro._l_addr.sa.sa_family; |
| 12137 | #ifdef HAVE_SA_LEN |
| 12138 | hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len; |
| 12139 | #else |
| 12140 | switch (net->ro._l_addr.sa.sa_family) { |
| 12141 | #ifdef INET |
| 12142 | case AF_INET: |
| 12143 | hb->heartbeat.hb_info.addr_len = sizeof(struct sockaddr_in); |
| 12144 | break; |
| 12145 | #endif |
| 12146 | #ifdef INET6 |
| 12147 | case AF_INET6: |
| 12148 | hb->heartbeat.hb_info.addr_len = sizeof(struct sockaddr_in6); |
| 12149 | break; |
| 12150 | #endif |
| 12151 | #if defined(__Userspace__) |
| 12152 | case AF_CONN: |
| 12153 | hb->heartbeat.hb_info.addr_len = sizeof(struct sockaddr_conn); |
| 12154 | break; |
| 12155 | #endif |
| 12156 | default: |
| 12157 | hb->heartbeat.hb_info.addr_len = 0; |
| 12158 | break; |
| 12159 | } |
| 12160 | #endif |
| 12161 | if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { |
| 12162 | /* |
| 12163 | * we only take from the entropy pool if the address is not |
| 12164 | * confirmed. |
| 12165 | */ |
| 12166 | net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep); |
| 12167 | net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep); |
| 12168 | } else { |
| 12169 | net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0; |
| 12170 | net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0; |
| 12171 | } |
| 12172 | switch (net->ro._l_addr.sa.sa_family) { |
| 12173 | #ifdef INET |
| 12174 | case AF_INET: |
| 12175 | memcpy(hb->heartbeat.hb_info.address, |
| 12176 | &net->ro._l_addr.sin.sin_addr, |
| 12177 | sizeof(net->ro._l_addr.sin.sin_addr)); |
| 12178 | break; |
| 12179 | #endif |
| 12180 | #ifdef INET6 |
| 12181 | case AF_INET6: |
| 12182 | memcpy(hb->heartbeat.hb_info.address, |
| 12183 | &net->ro._l_addr.sin6.sin6_addr, |
| 12184 | sizeof(net->ro._l_addr.sin6.sin6_addr)); |
| 12185 | break; |
| 12186 | #endif |
| 12187 | #if defined(__Userspace__) |
| 12188 | case AF_CONN: |
| 12189 | memcpy(hb->heartbeat.hb_info.address, |
| 12190 | &net->ro._l_addr.sconn.sconn_addr, |
| 12191 | sizeof(net->ro._l_addr.sconn.sconn_addr)); |
| 12192 | break; |
| 12193 | #endif |
| 12194 | default: |
| 12195 | if (chk->data) { |
| 12196 | sctp_m_freem(chk->data); |
| 12197 | chk->data = NULL; |
| 12198 | } |
| 12199 | sctp_free_a_chunk(stcb, chk, so_locked); |
| 12200 | return; |
| 12201 | break; |
| 12202 | } |
| 12203 | net->hb_responded = 0; |
| 12204 | TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next); |
| 12205 | stcb->asoc.ctrl_queue_cnt++; |
| 12206 | SCTP_STAT_INCR(sctps_sendheartbeat); |
| 12207 | return; |
| 12208 | } |
| 12209 |
| 12210 | void |
| 12211 | sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net, |
| 12212 | uint32_t high_tsn) |
| 12213 | { |
| 12214 | struct sctp_association *asoc; |
| 12215 | struct sctp_ecne_chunk *ecne; |
| 12216 | struct sctp_tmit_chunk *chk; |
| 12217 |
| 12218 | if (net == NULL) { |
| 12219 | return; |
| 12220 | } |
| 12221 | asoc = &stcb->asoc; |
| 12222 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 12223 | TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { |
| 12224 | if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) { |
| 12225 | /* found a previous ECN_ECHO update it if needed */ |
| 12226 | uint32_t cnt, ctsn; |
| 12227 | ecne = mtod(chk->data, struct sctp_ecne_chunk *); |
| 12228 | ctsn = ntohl(ecne->tsn); |
| 12229 | if (SCTP_TSN_GT(high_tsn, ctsn)) { |
| 12230 | ecne->tsn = htonl(high_tsn); |
| 12231 | SCTP_STAT_INCR(sctps_queue_upd_ecne); |
| 12232 | } |
| 12233 | cnt = ntohl(ecne->num_pkts_since_cwr); |
| 12234 | cnt++; |
| 12235 | ecne->num_pkts_since_cwr = htonl(cnt); |
| 12236 | return; |
| 12237 | } |
| 12238 | } |
| 12239 | /* nope could not find one to update so we must build one */ |
| 12240 | sctp_alloc_a_chunk(stcb, chk); |
| 12241 | if (chk == NULL) { |
| 12242 | return; |
| 12243 | } |
| 12244 | SCTP_STAT_INCR(sctps_queue_upd_ecne); |
| 12245 | chk->copy_by_ref = 0; |
| 12246 | chk->rec.chunk_id.id = SCTP_ECN_ECHO; |
| 12247 | chk->rec.chunk_id.can_take_data = 0; |
| 12248 | chk->flags = 0; |
| 12249 | chk->asoc = &stcb->asoc; |
| 12250 | chk->send_size = sizeof(struct sctp_ecne_chunk); |
| 12251 | chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER); |
| 12252 | if (chk->data == NULL) { |
| 12253 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 12254 | return; |
| 12255 | } |
| 12256 | SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); |
| 12257 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12258 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 12259 | chk->snd_count = 0; |
| 12260 | chk->whoTo = net; |
| 12261 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 12262 |
| 12263 | stcb->asoc.ecn_echo_cnt_onq++; |
| 12264 | ecne = mtod(chk->data, struct sctp_ecne_chunk *); |
| 12265 | ecne->ch.chunk_type = SCTP_ECN_ECHO; |
| 12266 | ecne->ch.chunk_flags = 0; |
| 12267 | ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk)); |
| 12268 | ecne->tsn = htonl(high_tsn); |
| 12269 | ecne->num_pkts_since_cwr = htonl(1); |
| 12270 | TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next); |
| 12271 | asoc->ctrl_queue_cnt++; |
| 12272 | } |
| 12273 |
| 12274 | void |
| 12275 | sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net, |
| 12276 | struct mbuf *m, int len, int iphlen, int bad_crc) |
| 12277 | { |
| 12278 | struct sctp_association *asoc; |
| 12279 | struct sctp_pktdrop_chunk *drp; |
| 12280 | struct sctp_tmit_chunk *chk; |
| 12281 | uint8_t *datap; |
| 12282 | int was_trunc = 0; |
| 12283 | int fullsz = 0; |
| 12284 | long spc; |
| 12285 | int offset; |
| 12286 | struct sctp_chunkhdr *ch, chunk_buf; |
| 12287 | unsigned int chk_length; |
| 12288 |
| 12289 | if (!stcb) { |
| 12290 | return; |
| 12291 | } |
| 12292 | asoc = &stcb->asoc; |
| 12293 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 12294 | if (asoc->pktdrop_supported == 0) { |
| 12295 | /*- |
| 12296 | * peer must declare support before I send one. |
| 12297 | */ |
| 12298 | return; |
| 12299 | } |
| 12300 | if (stcb->sctp_socket == NULL) { |
| 12301 | return; |
| 12302 | } |
| 12303 | sctp_alloc_a_chunk(stcb, chk); |
| 12304 | if (chk == NULL) { |
| 12305 | return; |
| 12306 | } |
| 12307 | chk->copy_by_ref = 0; |
| 12308 | chk->rec.chunk_id.id = SCTP_PACKET_DROPPED; |
| 12309 | chk->rec.chunk_id.can_take_data = 1; |
| 12310 | chk->flags = 0; |
| 12311 | len -= iphlen; |
| 12312 | chk->send_size = len; |
| 12313 | /* Validate that we do not have an ABORT in here. */ |
| 12314 | offset = iphlen + sizeof(struct sctphdr); |
| 12315 | ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, |
| 12316 | sizeof(*ch), (uint8_t *) & chunk_buf); |
| 12317 | while (ch != NULL) { |
| 12318 | chk_length = ntohs(ch->chunk_length); |
| 12319 | if (chk_length < sizeof(*ch)) { |
| 12320 | /* break to abort land */ |
| 12321 | break; |
| 12322 | } |
| 12323 | switch (ch->chunk_type) { |
| 12324 | case SCTP_PACKET_DROPPED: |
| 12325 | case SCTP_ABORT_ASSOCIATION: |
| 12326 | case SCTP_INITIATION_ACK: |
| 12327 | /** |
| 12328 | * We don't respond with an PKT-DROP to an ABORT |
| 12329 | * or PKT-DROP. We also do not respond to an |
| 12330 | * INIT-ACK, because we can't know if the initiation |
| 12331 | * tag is correct or not. |
| 12332 | */ |
| 12333 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 12334 | return; |
| 12335 | default: |
| 12336 | break; |
| 12337 | } |
| 12338 | offset += SCTP_SIZE32(chk_length); |
| 12339 | ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, |
| 12340 | sizeof(*ch), (uint8_t *) & chunk_buf); |
| 12341 | } |
| 12342 |
| 12343 | if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) > |
| 12344 | min(stcb->asoc.smallest_mtu, MCLBYTES)) { |
| 12345 | /* only send 1 mtu worth, trim off the |
| 12346 | * excess on the end. |
| 12347 | */ |
| 12348 | fullsz = len; |
| 12349 | len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD; |
| 12350 | was_trunc = 1; |
| 12351 | } |
| 12352 | chk->asoc = &stcb->asoc; |
| 12353 | chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); |
| 12354 | if (chk->data == NULL) { |
| 12355 | jump_out: |
| 12356 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 12357 | return; |
| 12358 | } |
| 12359 | SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); |
| 12360 | drp = mtod(chk->data, struct sctp_pktdrop_chunk *); |
| 12361 | if (drp == NULL) { |
| 12362 | sctp_m_freem(chk->data); |
| 12363 | chk->data = NULL; |
| 12364 | goto jump_out; |
| 12365 | } |
| 12366 | chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) + |
| 12367 | sizeof(struct sctphdr) + SCTP_MED_OVERHEAD)); |
| 12368 | chk->book_size_scale = 0; |
| 12369 | if (was_trunc) { |
| 12370 | drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED; |
| 12371 | drp->trunc_len = htons(fullsz); |
| 12372 | /* Len is already adjusted to size minus overhead above |
| 12373 | * take out the pkt_drop chunk itself from it. |
| 12374 | */ |
| 12375 | chk->send_size = (uint16_t)(len - sizeof(struct sctp_pktdrop_chunk)); |
| 12376 | len = chk->send_size; |
| 12377 | } else { |
| 12378 | /* no truncation needed */ |
| 12379 | drp->ch.chunk_flags = 0; |
| 12380 | drp->trunc_len = htons(0); |
| 12381 | } |
| 12382 | if (bad_crc) { |
| 12383 | drp->ch.chunk_flags |= SCTP_BADCRC; |
| 12384 | } |
| 12385 | chk->send_size += sizeof(struct sctp_pktdrop_chunk); |
| 12386 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12387 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 12388 | chk->snd_count = 0; |
| 12389 | if (net) { |
| 12390 | /* we should hit here */ |
| 12391 | chk->whoTo = net; |
| 12392 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 12393 | } else { |
| 12394 | chk->whoTo = NULL; |
| 12395 | } |
| 12396 | drp->ch.chunk_type = SCTP_PACKET_DROPPED; |
| 12397 | drp->ch.chunk_length = htons(chk->send_size); |
| 12398 | spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket); |
| 12399 | if (spc < 0) { |
| 12400 | spc = 0; |
| 12401 | } |
| 12402 | drp->bottle_bw = htonl(spc); |
| 12403 | if (asoc->my_rwnd) { |
| 12404 | drp->current_onq = htonl(asoc->size_on_reasm_queue + |
| 12405 | asoc->size_on_all_streams + |
| 12406 | asoc->my_rwnd_control_len + |
| 12407 | stcb->sctp_socket->so_rcv.sb_cc); |
| 12408 | } else { |
| 12409 | /*- |
| 12410 | * If my rwnd is 0, possibly from mbuf depletion as well as |
| 12411 | * space used, tell the peer there is NO space aka onq == bw |
| 12412 | */ |
| 12413 | drp->current_onq = htonl(spc); |
| 12414 | } |
| 12415 | drp->reserved = 0; |
| 12416 | datap = drp->data; |
| 12417 | m_copydata(m, iphlen, len, (caddr_t)datap); |
| 12418 | TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next); |
| 12419 | asoc->ctrl_queue_cnt++; |
| 12420 | } |
| 12421 |
| 12422 | void |
| 12423 | sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override) |
| 12424 | { |
| 12425 | struct sctp_association *asoc; |
| 12426 | struct sctp_cwr_chunk *cwr; |
| 12427 | struct sctp_tmit_chunk *chk; |
| 12428 |
| 12429 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 12430 | if (net == NULL) { |
| 12431 | return; |
| 12432 | } |
| 12433 | asoc = &stcb->asoc; |
| 12434 | TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { |
| 12435 | if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) { |
| 12436 | /* found a previous CWR queued to same destination update it if needed */ |
| 12437 | uint32_t ctsn; |
| 12438 | cwr = mtod(chk->data, struct sctp_cwr_chunk *); |
| 12439 | ctsn = ntohl(cwr->tsn); |
| 12440 | if (SCTP_TSN_GT(high_tsn, ctsn)) { |
| 12441 | cwr->tsn = htonl(high_tsn); |
| 12442 | } |
| 12443 | if (override & SCTP_CWR_REDUCE_OVERRIDE) { |
| 12444 | /* Make sure override is carried */ |
| 12445 | cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE; |
| 12446 | } |
| 12447 | return; |
| 12448 | } |
| 12449 | } |
| 12450 | sctp_alloc_a_chunk(stcb, chk); |
| 12451 | if (chk == NULL) { |
| 12452 | return; |
| 12453 | } |
| 12454 | chk->copy_by_ref = 0; |
| 12455 | chk->rec.chunk_id.id = SCTP_ECN_CWR; |
| 12456 | chk->rec.chunk_id.can_take_data = 1; |
| 12457 | chk->flags = 0; |
| 12458 | chk->asoc = &stcb->asoc; |
| 12459 | chk->send_size = sizeof(struct sctp_cwr_chunk); |
| 12460 | chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER); |
| 12461 | if (chk->data == NULL) { |
| 12462 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 12463 | return; |
| 12464 | } |
| 12465 | SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); |
| 12466 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12467 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 12468 | chk->snd_count = 0; |
| 12469 | chk->whoTo = net; |
| 12470 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 12471 | cwr = mtod(chk->data, struct sctp_cwr_chunk *); |
| 12472 | cwr->ch.chunk_type = SCTP_ECN_CWR; |
| 12473 | cwr->ch.chunk_flags = override; |
| 12474 | cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk)); |
| 12475 | cwr->tsn = htonl(high_tsn); |
| 12476 | TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next); |
| 12477 | asoc->ctrl_queue_cnt++; |
| 12478 | } |
| 12479 |
| 12480 | static int |
| 12481 | sctp_add_stream_reset_out(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, |
| 12482 | uint32_t seq, uint32_t resp_seq, uint32_t last_sent) |
| 12483 | { |
| 12484 | uint16_t len, old_len, i; |
| 12485 | struct sctp_stream_reset_out_request *req_out; |
| 12486 | struct sctp_chunkhdr *ch; |
| 12487 | int at; |
| 12488 | int number_entries=0; |
| 12489 |
| 12490 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12491 | old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); |
| 12492 | /* get to new offset for the param. */ |
| 12493 | req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len); |
| 12494 | /* now how long will this param be? */ |
| 12495 | for (i = 0; i < stcb->asoc.streamoutcnt; i++) { |
| 12496 | if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) && |
| 12497 | (stcb->asoc.strmout[i].chunks_on_queues == 0) && |
| 12498 | TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) { |
| 12499 | number_entries++; |
| 12500 | } |
| 12501 | } |
| 12502 | if (number_entries == 0) { |
| 12503 | return (0); |
| 12504 | } |
| 12505 | if (number_entries == stcb->asoc.streamoutcnt) { |
| 12506 | number_entries = 0; |
| 12507 | } |
| 12508 | if (number_entries > SCTP_MAX_STREAMS_AT_ONCE_RESET) { |
| 12509 | number_entries = SCTP_MAX_STREAMS_AT_ONCE_RESET; |
| 12510 | } |
| 12511 | len = (uint16_t)(sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries)); |
| 12512 | req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST); |
| 12513 | req_out->ph.param_length = htons(len); |
| 12514 | req_out->request_seq = htonl(seq); |
| 12515 | req_out->response_seq = htonl(resp_seq); |
| 12516 | req_out->send_reset_at_tsn = htonl(last_sent); |
| 12517 | at = 0; |
| 12518 | if (number_entries) { |
| 12519 | for (i = 0; i < stcb->asoc.streamoutcnt; i++) { |
| 12520 | if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) && |
| 12521 | (stcb->asoc.strmout[i].chunks_on_queues == 0) && |
| 12522 | TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) { |
| 12523 | req_out->list_of_streams[at] = htons(i); |
| 12524 | at++; |
| 12525 | stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT; |
| 12526 | if (at >= number_entries) { |
| 12527 | break; |
| 12528 | } |
| 12529 | } |
| 12530 | } |
| 12531 | } else { |
| 12532 | for (i = 0; i < stcb->asoc.streamoutcnt; i++) { |
| 12533 | stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT; |
| 12534 | } |
| 12535 | } |
| 12536 | if (SCTP_SIZE32(len) > len) { |
| 12537 | /*- |
| 12538 | * Need to worry about the pad we may end up adding to the |
| 12539 | * end. This is easy since the struct is either aligned to 4 |
| 12540 | * bytes or 2 bytes off. |
| 12541 | */ |
| 12542 | req_out->list_of_streams[number_entries] = 0; |
| 12543 | } |
| 12544 | /* now fix the chunk length */ |
| 12545 | ch->chunk_length = htons(len + old_len); |
| 12546 | chk->book_size = len + old_len; |
| 12547 | chk->book_size_scale = 0; |
| 12548 | chk->send_size = SCTP_SIZE32(chk->book_size); |
| 12549 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12550 | return (1); |
| 12551 | } |
| 12552 |
| 12553 | static void |
| 12554 | sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk, |
| 12555 | int number_entries, uint16_t *list, |
| 12556 | uint32_t seq) |
| 12557 | { |
| 12558 | uint16_t len, old_len, i; |
| 12559 | struct sctp_stream_reset_in_request *req_in; |
| 12560 | struct sctp_chunkhdr *ch; |
| 12561 |
| 12562 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12563 | old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); |
| 12564 |
| 12565 | /* get to new offset for the param. */ |
| 12566 | req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len); |
| 12567 | /* now how long will this param be? */ |
| 12568 | len = (uint16_t)(sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries)); |
| 12569 | req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST); |
| 12570 | req_in->ph.param_length = htons(len); |
| 12571 | req_in->request_seq = htonl(seq); |
| 12572 | if (number_entries) { |
| 12573 | for (i = 0; i < number_entries; i++) { |
| 12574 | req_in->list_of_streams[i] = htons(list[i]); |
| 12575 | } |
| 12576 | } |
| 12577 | if (SCTP_SIZE32(len) > len) { |
| 12578 | /*- |
| 12579 | * Need to worry about the pad we may end up adding to the |
| 12580 | * end. This is easy since the struct is either aligned to 4 |
| 12581 | * bytes or 2 bytes off. |
| 12582 | */ |
| 12583 | req_in->list_of_streams[number_entries] = 0; |
| 12584 | } |
| 12585 | /* now fix the chunk length */ |
| 12586 | ch->chunk_length = htons(len + old_len); |
| 12587 | chk->book_size = len + old_len; |
| 12588 | chk->book_size_scale = 0; |
| 12589 | chk->send_size = SCTP_SIZE32(chk->book_size); |
| 12590 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12591 | return; |
| 12592 | } |
| 12593 |
| 12594 | static void |
| 12595 | sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk, |
| 12596 | uint32_t seq) |
| 12597 | { |
| 12598 | uint16_t len, old_len; |
| 12599 | struct sctp_stream_reset_tsn_request *req_tsn; |
| 12600 | struct sctp_chunkhdr *ch; |
| 12601 |
| 12602 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12603 | old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); |
| 12604 |
| 12605 | /* get to new offset for the param. */ |
| 12606 | req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len); |
| 12607 | /* now how long will this param be? */ |
| 12608 | len = sizeof(struct sctp_stream_reset_tsn_request); |
| 12609 | req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST); |
| 12610 | req_tsn->ph.param_length = htons(len); |
| 12611 | req_tsn->request_seq = htonl(seq); |
| 12612 |
| 12613 | /* now fix the chunk length */ |
| 12614 | ch->chunk_length = htons(len + old_len); |
| 12615 | chk->send_size = len + old_len; |
| 12616 | chk->book_size = SCTP_SIZE32(chk->send_size); |
| 12617 | chk->book_size_scale = 0; |
| 12618 | SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); |
| 12619 | return; |
| 12620 | } |
| 12621 |
| 12622 | void |
| 12623 | sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk, |
| 12624 | uint32_t resp_seq, uint32_t result) |
| 12625 | { |
| 12626 | uint16_t len, old_len; |
| 12627 | struct sctp_stream_reset_response *resp; |
| 12628 | struct sctp_chunkhdr *ch; |
| 12629 |
| 12630 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12631 | old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); |
| 12632 |
| 12633 | /* get to new offset for the param. */ |
| 12634 | resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len); |
| 12635 | /* now how long will this param be? */ |
| 12636 | len = sizeof(struct sctp_stream_reset_response); |
| 12637 | resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE); |
| 12638 | resp->ph.param_length = htons(len); |
| 12639 | resp->response_seq = htonl(resp_seq); |
| 12640 | resp->result = ntohl(result); |
| 12641 |
| 12642 | /* now fix the chunk length */ |
| 12643 | ch->chunk_length = htons(len + old_len); |
| 12644 | chk->book_size = len + old_len; |
| 12645 | chk->book_size_scale = 0; |
| 12646 | chk->send_size = SCTP_SIZE32(chk->book_size); |
| 12647 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12648 | return; |
| 12649 | } |
| 12650 |
| 12651 | void |
| 12652 | sctp_send_deferred_reset_response(struct sctp_tcb *stcb, |
| 12653 | struct sctp_stream_reset_list *ent, |
| 12654 | int response) |
| 12655 | { |
| 12656 | struct sctp_association *asoc; |
| 12657 | struct sctp_tmit_chunk *chk; |
| 12658 | struct sctp_chunkhdr *ch; |
| 12659 |
| 12660 | asoc = &stcb->asoc; |
| 12661 |
| 12662 | /* |
| 12663 | * Reset our last reset action to the new one IP -> response |
| 12664 | * (PERFORMED probably). This assures that if we fail to send, a |
| 12665 | * retran from the peer will get the new response. |
| 12666 | */ |
| 12667 | asoc->last_reset_action[0] = response; |
| 12668 | if (asoc->stream_reset_outstanding) { |
| 12669 | return; |
| 12670 | } |
| 12671 | sctp_alloc_a_chunk(stcb, chk); |
| 12672 | if (chk == NULL) { |
| 12673 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 12674 | return; |
| 12675 | } |
| 12676 | chk->copy_by_ref = 0; |
| 12677 | chk->rec.chunk_id.id = SCTP_STREAM_RESET; |
| 12678 | chk->rec.chunk_id.can_take_data = 0; |
| 12679 | chk->flags = 0; |
| 12680 | chk->asoc = &stcb->asoc; |
| 12681 | chk->book_size = sizeof(struct sctp_chunkhdr); |
| 12682 | chk->send_size = SCTP_SIZE32(chk->book_size); |
| 12683 | chk->book_size_scale = 0; |
| 12684 | chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); |
| 12685 | if (chk->data == NULL) { |
| 12686 | sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED); |
| 12687 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 12688 | return; |
| 12689 | } |
| 12690 | SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); |
| 12691 | /* setup chunk parameters */ |
| 12692 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 12693 | chk->snd_count = 0; |
| 12694 | if (stcb->asoc.alternate) { |
| 12695 | chk->whoTo = stcb->asoc.alternate; |
| 12696 | } else { |
| 12697 | chk->whoTo = stcb->asoc.primary_destination; |
| 12698 | } |
| 12699 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12700 | ch->chunk_type = SCTP_STREAM_RESET; |
| 12701 | ch->chunk_flags = 0; |
| 12702 | ch->chunk_length = htons(chk->book_size); |
| 12703 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 12704 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12705 | sctp_add_stream_reset_result(chk, ent->seq, response); |
| 12706 | /* insert the chunk for sending */ |
| 12707 | TAILQ_INSERT_TAIL(&asoc->control_send_queue, |
| 12708 | chk, |
| 12709 | sctp_next); |
| 12710 | asoc->ctrl_queue_cnt++; |
| 12711 | } |
| 12712 |
| 12713 | void |
| 12714 | sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk, |
| 12715 | uint32_t resp_seq, uint32_t result, |
| 12716 | uint32_t send_una, uint32_t recv_next) |
| 12717 | { |
| 12718 | uint16_t len, old_len; |
| 12719 | struct sctp_stream_reset_response_tsn *resp; |
| 12720 | struct sctp_chunkhdr *ch; |
| 12721 |
| 12722 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12723 | old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); |
| 12724 |
| 12725 | /* get to new offset for the param. */ |
| 12726 | resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len); |
| 12727 | /* now how long will this param be? */ |
| 12728 | len = sizeof(struct sctp_stream_reset_response_tsn); |
| 12729 | resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE); |
| 12730 | resp->ph.param_length = htons(len); |
| 12731 | resp->response_seq = htonl(resp_seq); |
| 12732 | resp->result = htonl(result); |
| 12733 | resp->senders_next_tsn = htonl(send_una); |
| 12734 | resp->receivers_next_tsn = htonl(recv_next); |
| 12735 |
| 12736 | /* now fix the chunk length */ |
| 12737 | ch->chunk_length = htons(len + old_len); |
| 12738 | chk->book_size = len + old_len; |
| 12739 | chk->send_size = SCTP_SIZE32(chk->book_size); |
| 12740 | chk->book_size_scale = 0; |
| 12741 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12742 | return; |
| 12743 | } |
| 12744 |
| 12745 | static void |
| 12746 | sctp_add_an_out_stream(struct sctp_tmit_chunk *chk, |
| 12747 | uint32_t seq, |
| 12748 | uint16_t adding) |
| 12749 | { |
| 12750 | uint16_t len, old_len; |
| 12751 | struct sctp_chunkhdr *ch; |
| 12752 | struct sctp_stream_reset_add_strm *addstr; |
| 12753 |
| 12754 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12755 | old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); |
| 12756 |
| 12757 | /* get to new offset for the param. */ |
| 12758 | addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len); |
| 12759 | /* now how long will this param be? */ |
| 12760 | len = sizeof(struct sctp_stream_reset_add_strm); |
| 12761 |
| 12762 | /* Fill it out. */ |
| 12763 | addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_OUT_STREAMS); |
| 12764 | addstr->ph.param_length = htons(len); |
| 12765 | addstr->request_seq = htonl(seq); |
| 12766 | addstr->number_of_streams = htons(adding); |
| 12767 | addstr->reserved = 0; |
| 12768 |
| 12769 | /* now fix the chunk length */ |
| 12770 | ch->chunk_length = htons(len + old_len); |
| 12771 | chk->send_size = len + old_len; |
| 12772 | chk->book_size = SCTP_SIZE32(chk->send_size); |
| 12773 | chk->book_size_scale = 0; |
| 12774 | SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); |
| 12775 | return; |
| 12776 | } |
| 12777 |
| 12778 | static void |
| 12779 | sctp_add_an_in_stream(struct sctp_tmit_chunk *chk, |
| 12780 | uint32_t seq, |
| 12781 | uint16_t adding) |
| 12782 | { |
| 12783 | uint16_t len, old_len; |
| 12784 | struct sctp_chunkhdr *ch; |
| 12785 | struct sctp_stream_reset_add_strm *addstr; |
| 12786 |
| 12787 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12788 | old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); |
| 12789 |
| 12790 | /* get to new offset for the param. */ |
| 12791 | addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len); |
| 12792 | /* now how long will this param be? */ |
| 12793 | len = sizeof(struct sctp_stream_reset_add_strm); |
| 12794 | /* Fill it out. */ |
| 12795 | addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_IN_STREAMS); |
| 12796 | addstr->ph.param_length = htons(len); |
| 12797 | addstr->request_seq = htonl(seq); |
| 12798 | addstr->number_of_streams = htons(adding); |
| 12799 | addstr->reserved = 0; |
| 12800 |
| 12801 | /* now fix the chunk length */ |
| 12802 | ch->chunk_length = htons(len + old_len); |
| 12803 | chk->send_size = len + old_len; |
| 12804 | chk->book_size = SCTP_SIZE32(chk->send_size); |
| 12805 | chk->book_size_scale = 0; |
| 12806 | SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); |
| 12807 | return; |
| 12808 | } |
| 12809 |
| 12810 | int |
| 12811 | sctp_send_stream_reset_out_if_possible(struct sctp_tcb *stcb, int so_locked) |
| 12812 | { |
| 12813 | struct sctp_association *asoc; |
| 12814 | struct sctp_tmit_chunk *chk; |
| 12815 | struct sctp_chunkhdr *ch; |
| 12816 | uint32_t seq; |
| 12817 |
| 12818 | asoc = &stcb->asoc; |
| 12819 | asoc->trigger_reset = 0; |
| 12820 | if (asoc->stream_reset_outstanding) { |
| 12821 | return (EALREADY); |
| 12822 | } |
| 12823 | sctp_alloc_a_chunk(stcb, chk); |
| 12824 | if (chk == NULL) { |
| 12825 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 12826 | return (ENOMEM); |
| 12827 | } |
| 12828 | chk->copy_by_ref = 0; |
| 12829 | chk->rec.chunk_id.id = SCTP_STREAM_RESET; |
| 12830 | chk->rec.chunk_id.can_take_data = 0; |
| 12831 | chk->flags = 0; |
| 12832 | chk->asoc = &stcb->asoc; |
| 12833 | chk->book_size = sizeof(struct sctp_chunkhdr); |
| 12834 | chk->send_size = SCTP_SIZE32(chk->book_size); |
| 12835 | chk->book_size_scale = 0; |
| 12836 | chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); |
| 12837 | if (chk->data == NULL) { |
| 12838 | sctp_free_a_chunk(stcb, chk, so_locked); |
| 12839 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 12840 | return (ENOMEM); |
| 12841 | } |
| 12842 | SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); |
| 12843 |
| 12844 | /* setup chunk parameters */ |
| 12845 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 12846 | chk->snd_count = 0; |
| 12847 | if (stcb->asoc.alternate) { |
| 12848 | chk->whoTo = stcb->asoc.alternate; |
| 12849 | } else { |
| 12850 | chk->whoTo = stcb->asoc.primary_destination; |
| 12851 | } |
| 12852 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12853 | ch->chunk_type = SCTP_STREAM_RESET; |
| 12854 | ch->chunk_flags = 0; |
| 12855 | ch->chunk_length = htons(chk->book_size); |
| 12856 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 12857 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12858 | seq = stcb->asoc.str_reset_seq_out; |
| 12859 | if (sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1))) { |
| 12860 | seq++; |
| 12861 | asoc->stream_reset_outstanding++; |
| 12862 | } else { |
| 12863 | m_freem(chk->data); |
| 12864 | chk->data = NULL; |
| 12865 | sctp_free_a_chunk(stcb, chk, so_locked); |
| 12866 | return (ENOENT); |
| 12867 | } |
| 12868 | asoc->str_reset = chk; |
| 12869 | /* insert the chunk for sending */ |
| 12870 | TAILQ_INSERT_TAIL(&asoc->control_send_queue, |
| 12871 | chk, |
| 12872 | sctp_next); |
| 12873 | asoc->ctrl_queue_cnt++; |
| 12874 |
| 12875 | if (stcb->asoc.send_sack) { |
| 12876 | sctp_send_sack(stcb, so_locked); |
| 12877 | } |
| 12878 | sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); |
| 12879 | return (0); |
| 12880 | } |
| 12881 |
| 12882 | int |
| 12883 | sctp_send_str_reset_req(struct sctp_tcb *stcb, |
| 12884 | uint16_t number_entries, uint16_t *list, |
| 12885 | uint8_t send_in_req, |
| 12886 | uint8_t send_tsn_req, |
| 12887 | uint8_t add_stream, |
| 12888 | uint16_t adding_o, |
| 12889 | uint16_t adding_i, uint8_t peer_asked) |
| 12890 | { |
| 12891 | struct sctp_association *asoc; |
| 12892 | struct sctp_tmit_chunk *chk; |
| 12893 | struct sctp_chunkhdr *ch; |
| 12894 | int can_send_out_req=0; |
| 12895 | uint32_t seq; |
| 12896 |
| 12897 | asoc = &stcb->asoc; |
| 12898 | if (asoc->stream_reset_outstanding) { |
| 12899 | /*- |
| 12900 | * Already one pending, must get ACK back to clear the flag. |
| 12901 | */ |
| 12902 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY); |
| 12903 | return (EBUSY); |
| 12904 | } |
| 12905 | if ((send_in_req == 0) && (send_tsn_req == 0) && |
| 12906 | (add_stream == 0)) { |
| 12907 | /* nothing to do */ |
| 12908 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 12909 | return (EINVAL); |
| 12910 | } |
| 12911 | if (send_tsn_req && send_in_req) { |
| 12912 | /* error, can't do that */ |
| 12913 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 12914 | return (EINVAL); |
| 12915 | } else if (send_in_req) { |
| 12916 | can_send_out_req = 1; |
| 12917 | } |
| 12918 | if (number_entries > (MCLBYTES - |
| 12919 | SCTP_MIN_OVERHEAD - |
| 12920 | sizeof(struct sctp_chunkhdr) - |
| 12921 | sizeof(struct sctp_stream_reset_out_request)) / |
| 12922 | sizeof(uint16_t)) { |
| 12923 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 12924 | return (ENOMEM); |
| 12925 | } |
| 12926 | sctp_alloc_a_chunk(stcb, chk); |
| 12927 | if (chk == NULL) { |
| 12928 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 12929 | return (ENOMEM); |
| 12930 | } |
| 12931 | chk->copy_by_ref = 0; |
| 12932 | chk->rec.chunk_id.id = SCTP_STREAM_RESET; |
| 12933 | chk->rec.chunk_id.can_take_data = 0; |
| 12934 | chk->flags = 0; |
| 12935 | chk->asoc = &stcb->asoc; |
| 12936 | chk->book_size = sizeof(struct sctp_chunkhdr); |
| 12937 | chk->send_size = SCTP_SIZE32(chk->book_size); |
| 12938 | chk->book_size_scale = 0; |
| 12939 | chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); |
| 12940 | if (chk->data == NULL) { |
| 12941 | sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED); |
| 12942 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 12943 | return (ENOMEM); |
| 12944 | } |
| 12945 | SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); |
| 12946 |
| 12947 | /* setup chunk parameters */ |
| 12948 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 12949 | chk->snd_count = 0; |
| 12950 | if (stcb->asoc.alternate) { |
| 12951 | chk->whoTo = stcb->asoc.alternate; |
| 12952 | } else { |
| 12953 | chk->whoTo = stcb->asoc.primary_destination; |
| 12954 | } |
| 12955 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 12956 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12957 | ch->chunk_type = SCTP_STREAM_RESET; |
| 12958 | ch->chunk_flags = 0; |
| 12959 | ch->chunk_length = htons(chk->book_size); |
| 12960 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12961 |
| 12962 | seq = stcb->asoc.str_reset_seq_out; |
| 12963 | if (can_send_out_req) { |
| 12964 | int ret; |
| 12965 | ret = sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1)); |
| 12966 | if (ret) { |
| 12967 | seq++; |
| 12968 | asoc->stream_reset_outstanding++; |
| 12969 | } |
| 12970 | } |
| 12971 | if ((add_stream & 1) && |
| 12972 | ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < adding_o)) { |
| 12973 | /* Need to allocate more */ |
| 12974 | struct sctp_stream_out *oldstream; |
| 12975 | struct sctp_stream_queue_pending *sp, *nsp; |
| 12976 | int i; |
| 12977 | #if defined(SCTP_DETAILED_STR_STATS) |
| 12978 | int j; |
| 12979 | #endif |
| 12980 |
| 12981 | oldstream = stcb->asoc.strmout; |
| 12982 | /* get some more */ |
| 12983 | SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *, |
| 12984 | (stcb->asoc.streamoutcnt + adding_o) * sizeof(struct sctp_stream_out), |
| 12985 | SCTP_M_STRMO); |
| 12986 | if (stcb->asoc.strmout == NULL) { |
| 12987 | uint8_t x; |
| 12988 | stcb->asoc.strmout = oldstream; |
| 12989 | /* Turn off the bit */ |
| 12990 | x = add_stream & 0xfe; |
| 12991 | add_stream = x; |
| 12992 | goto skip_stuff; |
| 12993 | } |
| 12994 | /* Ok now we proceed with copying the old out stuff and |
| 12995 | * initializing the new stuff. |
| 12996 | */ |
| 12997 | SCTP_TCB_SEND_LOCK(stcb); |
| 12998 | stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 0, 1); |
| 12999 | for (i = 0; i < stcb->asoc.streamoutcnt; i++) { |
| 13000 | TAILQ_INIT(&stcb->asoc.strmout[i].outqueue); |
| 13001 | stcb->asoc.strmout[i].chunks_on_queues = oldstream[i].chunks_on_queues; |
| 13002 | stcb->asoc.strmout[i].next_mid_ordered = oldstream[i].next_mid_ordered; |
| 13003 | stcb->asoc.strmout[i].next_mid_unordered = oldstream[i].next_mid_unordered; |
| 13004 | stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete; |
| 13005 | stcb->asoc.strmout[i].sid = i; |
| 13006 | stcb->asoc.strmout[i].state = oldstream[i].state; |
| 13007 | /* FIX ME FIX ME */ |
| 13008 | /* This should be a SS_COPY operation FIX ME STREAM SCHEDULER EXPERT */ |
| 13009 | stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], &oldstream[i]); |
| 13010 | /* now anything on those queues? */ |
| 13011 | TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) { |
| 13012 | TAILQ_REMOVE(&oldstream[i].outqueue, sp, next); |
| 13013 | TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next); |
| 13014 | } |
| 13015 |
| 13016 | } |
| 13017 | /* now the new streams */ |
| 13018 | stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1); |
| 13019 | for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + adding_o); i++) { |
| 13020 | TAILQ_INIT(&stcb->asoc.strmout[i].outqueue); |
| 13021 | stcb->asoc.strmout[i].chunks_on_queues = 0; |
| 13022 | #if defined(SCTP_DETAILED_STR_STATS) |
| 13023 | for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) { |
| 13024 | stcb->asoc.strmout[i].abandoned_sent[j] = 0; |
| 13025 | stcb->asoc.strmout[i].abandoned_unsent[j] = 0; |
| 13026 | } |
| 13027 | #else |
| 13028 | stcb->asoc.strmout[i].abandoned_sent[0] = 0; |
| 13029 | stcb->asoc.strmout[i].abandoned_unsent[0] = 0; |
| 13030 | #endif |
| 13031 | stcb->asoc.strmout[i].next_mid_ordered = 0; |
| 13032 | stcb->asoc.strmout[i].next_mid_unordered = 0; |
| 13033 | stcb->asoc.strmout[i].sid = i; |
| 13034 | stcb->asoc.strmout[i].last_msg_incomplete = 0; |
| 13035 | stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL); |
| 13036 | stcb->asoc.strmout[i].state = SCTP_STREAM_CLOSED; |
| 13037 | } |
| 13038 | stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + adding_o; |
| 13039 | SCTP_FREE(oldstream, SCTP_M_STRMO); |
| 13040 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 13041 | } |
| 13042 | skip_stuff: |
| 13043 | if ((add_stream & 1) && (adding_o > 0)) { |
| 13044 | asoc->strm_pending_add_size = adding_o; |
| 13045 | asoc->peer_req_out = peer_asked; |
| 13046 | sctp_add_an_out_stream(chk, seq, adding_o); |
| 13047 | seq++; |
| 13048 | asoc->stream_reset_outstanding++; |
| 13049 | } |
| 13050 | if ((add_stream & 2) && (adding_i > 0)) { |
| 13051 | sctp_add_an_in_stream(chk, seq, adding_i); |
| 13052 | seq++; |
| 13053 | asoc->stream_reset_outstanding++; |
| 13054 | } |
| 13055 | if (send_in_req) { |
| 13056 | sctp_add_stream_reset_in(chk, number_entries, list, seq); |
| 13057 | seq++; |
| 13058 | asoc->stream_reset_outstanding++; |
| 13059 | } |
| 13060 | if (send_tsn_req) { |
| 13061 | sctp_add_stream_reset_tsn(chk, seq); |
| 13062 | asoc->stream_reset_outstanding++; |
| 13063 | } |
| 13064 | asoc->str_reset = chk; |
| 13065 | /* insert the chunk for sending */ |
| 13066 | TAILQ_INSERT_TAIL(&asoc->control_send_queue, |
| 13067 | chk, |
| 13068 | sctp_next); |
| 13069 | asoc->ctrl_queue_cnt++; |
| 13070 | if (stcb->asoc.send_sack) { |
| 13071 | sctp_send_sack(stcb, SCTP_SO_LOCKED); |
| 13072 | } |
| 13073 | sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); |
| 13074 | return (0); |
| 13075 | } |
| 13076 |
| 13077 | void |
| 13078 | sctp_send_abort(struct mbuf *m, int iphlen, struct sockaddr *src, struct sockaddr *dst, |
| 13079 | struct sctphdr *sh, uint32_t vtag, struct mbuf *cause, |
| 13080 | #if defined(__FreeBSD__) |
| 13081 | uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum, |
| 13082 | #endif |
| 13083 | uint32_t vrf_id, uint16_t port) |
| 13084 | { |
| 13085 | /* Don't respond to an ABORT with an ABORT. */ |
| 13086 | if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) { |
| 13087 | if (cause) |
| 13088 | sctp_m_freem(cause); |
| 13089 | return; |
| 13090 | } |
| 13091 | sctp_send_resp_msg(src, dst, sh, vtag, SCTP_ABORT_ASSOCIATION, cause, |
| 13092 | #if defined(__FreeBSD__) |
| 13093 | mflowtype, mflowid, fibnum, |
| 13094 | #endif |
| 13095 | vrf_id, port); |
| 13096 | return; |
| 13097 | } |
| 13098 |
| 13099 | void |
| 13100 | sctp_send_operr_to(struct sockaddr *src, struct sockaddr *dst, |
| 13101 | struct sctphdr *sh, uint32_t vtag, struct mbuf *cause, |
| 13102 | #if defined(__FreeBSD__) |
| 13103 | uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum, |
| 13104 | #endif |
| 13105 | uint32_t vrf_id, uint16_t port) |
| 13106 | { |
| 13107 | sctp_send_resp_msg(src, dst, sh, vtag, SCTP_OPERATION_ERROR, cause, |
| 13108 | #if defined(__FreeBSD__) |
| 13109 | mflowtype, mflowid, fibnum, |
| 13110 | #endif |
| 13111 | vrf_id, port); |
| 13112 | return; |
| 13113 | } |
| 13114 |
| 13115 | static struct mbuf * |
| 13116 | sctp_copy_resume(struct uio *uio, |
| 13117 | int max_send_len, |
| 13118 | #if (defined(__FreeBSD__) && __FreeBSD_version > 602000) || defined(__Userspace__) |
| 13119 | int user_marks_eor, |
| 13120 | #endif |
| 13121 | int *error, |
| 13122 | uint32_t *sndout, |
| 13123 | struct mbuf **new_tail) |
| 13124 | { |
| 13125 | #if defined(__Panda__) |
| 13126 | struct mbuf *m; |
| 13127 |
| 13128 | m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0, |
| 13129 | (user_marks_eor ? M_EOR : 0)); |
| 13130 | if (m == NULL) { |
| 13131 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS); |
| 13132 | *error = ENOBUFS; |
| 13133 | } else { |
| 13134 | *sndout = m_length(m, NULL); |
| 13135 | *new_tail = m_last(m); |
| 13136 | } |
| 13137 | return (m); |
| 13138 | #elif defined(__FreeBSD__) && __FreeBSD_version > 602000 || defined(__Userspace__) |
| 13139 | struct mbuf *m; |
| 13140 |
| 13141 | m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0, |
| 13142 | (M_PKTHDR | (user_marks_eor ? M_EOR : 0))); |
| 13143 | if (m == NULL) { |
| 13144 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS); |
| 13145 | *error = ENOBUFS; |
| 13146 | } else { |
| 13147 | *sndout = m_length(m, NULL); |
| 13148 | *new_tail = m_last(m); |
| 13149 | } |
| 13150 | return (m); |
| 13151 | #else |
| 13152 | int left, cancpy, willcpy; |
| 13153 | struct mbuf *m, *head; |
| 13154 |
| 13155 | #if defined(__APPLE__) |
| 13156 | #if defined(APPLE_LEOPARD) |
| 13157 | left = (int)min(uio->uio_resid, max_send_len); |
| 13158 | #else |
| 13159 | left = (int)min(uio_resid(uio), max_send_len); |
| 13160 | #endif |
| 13161 | #else |
| 13162 | left = (int)min(uio->uio_resid, max_send_len); |
| 13163 | #endif |
| 13164 | /* Always get a header just in case */ |
| 13165 | head = sctp_get_mbuf_for_msg(left, 0, M_WAITOK, 0, MT_DATA); |
| 13166 | if (head == NULL) { |
| 13167 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS); |
| 13168 | *error = ENOBUFS; |
| 13169 | return (NULL); |
| 13170 | } |
| 13171 | cancpy = (int)M_TRAILINGSPACE(head); |
| 13172 | willcpy = min(cancpy, left); |
| 13173 | *error = uiomove(mtod(head, caddr_t), willcpy, uio); |
| 13174 | if (*error) { |
| 13175 | sctp_m_freem(head); |
| 13176 | return (NULL); |
| 13177 | } |
| 13178 | *sndout += willcpy; |
| 13179 | left -= willcpy; |
| 13180 | SCTP_BUF_LEN(head) = willcpy; |
| 13181 | m = head; |
| 13182 | *new_tail = head; |
| 13183 | while (left > 0) { |
| 13184 | /* move in user data */ |
| 13185 | SCTP_BUF_NEXT(m) = sctp_get_mbuf_for_msg(left, 0, M_WAITOK, 0, MT_DATA); |
| 13186 | if (SCTP_BUF_NEXT(m) == NULL) { |
| 13187 | sctp_m_freem(head); |
| 13188 | *new_tail = NULL; |
| 13189 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS); |
| 13190 | *error = ENOBUFS; |
| 13191 | return (NULL); |
| 13192 | } |
| 13193 | m = SCTP_BUF_NEXT(m); |
| 13194 | cancpy = (int)M_TRAILINGSPACE(m); |
| 13195 | willcpy = min(cancpy, left); |
| 13196 | *error = uiomove(mtod(m, caddr_t), willcpy, uio); |
| 13197 | if (*error) { |
| 13198 | sctp_m_freem(head); |
| 13199 | *new_tail = NULL; |
| 13200 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT); |
| 13201 | *error = EFAULT; |
| 13202 | return (NULL); |
| 13203 | } |
| 13204 | SCTP_BUF_LEN(m) = willcpy; |
| 13205 | left -= willcpy; |
| 13206 | *sndout += willcpy; |
| 13207 | *new_tail = m; |
| 13208 | if (left == 0) { |
| 13209 | SCTP_BUF_NEXT(m) = NULL; |
| 13210 | } |
| 13211 | } |
| 13212 | return (head); |
| 13213 | #endif |
| 13214 | } |
| 13215 |
| 13216 | static int |
| 13217 | sctp_copy_one(struct sctp_stream_queue_pending *sp, |
| 13218 | struct uio *uio, |
| 13219 | int resv_upfront) |
| 13220 | { |
| 13221 | #if defined(__Panda__) |
| 13222 | sp->data = m_uiotombuf(uio, M_WAITOK, sp->length, |
| 13223 | resv_upfront, 0); |
| 13224 | if (sp->data == NULL) { |
| 13225 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS); |
| 13226 | return (ENOBUFS); |
| 13227 | } |
| 13228 |
| 13229 | sp->tail_mbuf = m_last(sp->data); |
| 13230 | return (0); |
| 13231 | #elif defined(__FreeBSD__) && __FreeBSD_version > 602000 || defined(__Userspace__) |
| 13232 | sp->data = m_uiotombuf(uio, M_WAITOK, sp->length, |
| 13233 | resv_upfront, 0); |
| 13234 | if (sp->data == NULL) { |
| 13235 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS); |
| 13236 | return (ENOBUFS); |
| 13237 | } |
| 13238 |
| 13239 | sp->tail_mbuf = m_last(sp->data); |
| 13240 | return (0); |
| 13241 | #else |
| 13242 | int left; |
| 13243 | int cancpy, willcpy, error; |
| 13244 | struct mbuf *m, *head; |
| 13245 | int cpsz = 0; |
| 13246 |
| 13247 | /* First one gets a header */ |
| 13248 | left = sp->length; |
| 13249 | head = m = sctp_get_mbuf_for_msg((left + resv_upfront), 0, M_WAITOK, 0, MT_DATA); |
| 13250 | if (m == NULL) { |
| 13251 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS); |
| 13252 | return (ENOBUFS); |
| 13253 | } |
| 13254 | /*- |
| 13255 | * Add this one for m in now, that way if the alloc fails we won't |
| 13256 | * have a bad cnt. |
| 13257 | */ |
| 13258 | SCTP_BUF_RESV_UF(m, resv_upfront); |
| 13259 | cancpy = (int)M_TRAILINGSPACE(m); |
| 13260 | willcpy = min(cancpy, left); |
| 13261 | while (left > 0) { |
| 13262 | /* move in user data */ |
| 13263 | error = uiomove(mtod(m, caddr_t), willcpy, uio); |
| 13264 | if (error) { |
| 13265 | sctp_m_freem(head); |
| 13266 | return (error); |
| 13267 | } |
| 13268 | SCTP_BUF_LEN(m) = willcpy; |
| 13269 | left -= willcpy; |
| 13270 | cpsz += willcpy; |
| 13271 | if (left > 0) { |
| 13272 | SCTP_BUF_NEXT(m) = sctp_get_mbuf_for_msg(left, 0, M_WAITOK, 0, MT_DATA); |
| 13273 | if (SCTP_BUF_NEXT(m) == NULL) { |
| 13274 | /* |
| 13275 | * the head goes back to caller, he can free |
| 13276 | * the rest |
| 13277 | */ |
| 13278 | sctp_m_freem(head); |
| 13279 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS); |
| 13280 | return (ENOBUFS); |
| 13281 | } |
| 13282 | m = SCTP_BUF_NEXT(m); |
| 13283 | cancpy = (int)M_TRAILINGSPACE(m); |
| 13284 | willcpy = min(cancpy, left); |
| 13285 | } else { |
| 13286 | sp->tail_mbuf = m; |
| 13287 | SCTP_BUF_NEXT(m) = NULL; |
| 13288 | } |
| 13289 | } |
| 13290 | sp->data = head; |
| 13291 | sp->length = cpsz; |
| 13292 | return (0); |
| 13293 | #endif |
| 13294 | } |
| 13295 |
| 13296 |
| 13297 |
| 13298 | static struct sctp_stream_queue_pending * |
| 13299 | sctp_copy_it_in(struct sctp_tcb *stcb, |
| 13300 | struct sctp_association *asoc, |
| 13301 | struct sctp_sndrcvinfo *srcv, |
| 13302 | struct uio *uio, |
| 13303 | struct sctp_nets *net, |
| 13304 | ssize_t max_send_len, |
| 13305 | int user_marks_eor, |
| 13306 | int *error) |
| 13307 |
| 13308 | { |
| 13309 | /*- |
| 13310 | * This routine must be very careful in its work. Protocol |
| 13311 | * processing is up and running so care must be taken to spl...() |
| 13312 | * when you need to do something that may effect the stcb/asoc. The |
| 13313 | * sb is locked however. When data is copied the protocol processing |
| 13314 | * should be enabled since this is a slower operation... |
| 13315 | */ |
| 13316 | struct sctp_stream_queue_pending *sp = NULL; |
| 13317 | int resv_in_first; |
| 13318 |
| 13319 | *error = 0; |
| 13320 | /* Now can we send this? */ |
| 13321 | if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) || |
| 13322 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) || |
| 13323 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) || |
| 13324 | (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) { |
| 13325 | /* got data while shutting down */ |
| 13326 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); |
| 13327 | *error = ECONNRESET; |
| 13328 | goto out_now; |
| 13329 | } |
| 13330 | sctp_alloc_a_strmoq(stcb, sp); |
| 13331 | if (sp == NULL) { |
| 13332 | SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 13333 | *error = ENOMEM; |
| 13334 | goto out_now; |
| 13335 | } |
| 13336 | sp->act_flags = 0; |
| 13337 | sp->sender_all_done = 0; |
| 13338 | sp->sinfo_flags = srcv->sinfo_flags; |
| 13339 | sp->timetolive = srcv->sinfo_timetolive; |
| 13340 | sp->ppid = srcv->sinfo_ppid; |
| 13341 | sp->context = srcv->sinfo_context; |
| 13342 | sp->fsn = 0; |
| 13343 | (void)SCTP_GETTIME_TIMEVAL(&sp->ts); |
| 13344 |
| 13345 | sp->sid = srcv->sinfo_stream; |
| 13346 | #if defined(__APPLE__) |
| 13347 | #if defined(APPLE_LEOPARD) |
| 13348 | sp->length = (uint32_t)min(uio->uio_resid, max_send_len); |
| 13349 | #else |
| 13350 | sp->length = (uint32_t)min(uio_resid(uio), max_send_len); |
| 13351 | #endif |
| 13352 | #else |
| 13353 | sp->length = (uint32_t)min(uio->uio_resid, max_send_len); |
| 13354 | #endif |
| 13355 | #if defined(__APPLE__) |
| 13356 | #if defined(APPLE_LEOPARD) |
| 13357 | if ((sp->length == (uint32_t)uio->uio_resid) && |
| 13358 | #else |
| 13359 | if ((sp->length == (uint32_t)uio_resid(uio)) && |
| 13360 | #endif |
| 13361 | #else |
| 13362 | if ((sp->length == (uint32_t)uio->uio_resid) && |
| 13363 | #endif |
| 13364 | ((user_marks_eor == 0) || |
| 13365 | (srcv->sinfo_flags & SCTP_EOF) || |
| 13366 | (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) { |
| 13367 | sp->msg_is_complete = 1; |
| 13368 | } else { |
| 13369 | sp->msg_is_complete = 0; |
| 13370 | } |
| 13371 | sp->sender_all_done = 0; |
| 13372 | sp->some_taken = 0; |
| 13373 | sp->put_last_out = 0; |
| 13374 | resv_in_first = SCTP_DATA_CHUNK_OVERHEAD(stcb); |
| 13375 | sp->data = sp->tail_mbuf = NULL; |
| 13376 | if (sp->length == 0) { |
| 13377 | goto skip_copy; |
| 13378 | } |
| 13379 | if (srcv->sinfo_keynumber_valid) { |
| 13380 | sp->auth_keyid = srcv->sinfo_keynumber; |
| 13381 | } else { |
| 13382 | sp->auth_keyid = stcb->asoc.authinfo.active_keyid; |
| 13383 | } |
| 13384 | if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) { |
| 13385 | sctp_auth_key_acquire(stcb, sp->auth_keyid); |
| 13386 | sp->holds_key_ref = 1; |
| 13387 | } |
| 13388 | #if defined(__APPLE__) |
| 13389 | SCTP_SOCKET_UNLOCK(SCTP_INP_SO(stcb->sctp_ep), 0); |
| 13390 | #endif |
| 13391 | *error = sctp_copy_one(sp, uio, resv_in_first); |
| 13392 | #if defined(__APPLE__) |
| 13393 | SCTP_SOCKET_LOCK(SCTP_INP_SO(stcb->sctp_ep), 0); |
| 13394 | #endif |
| 13395 | skip_copy: |
| 13396 | if (*error) { |
| 13397 | #if defined(__Userspace__) |
| 13398 | SCTP_TCB_LOCK(stcb); |
| 13399 | #endif |
| 13400 | sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED); |
| 13401 | #if defined(__Userspace__) |
| 13402 | SCTP_TCB_UNLOCK(stcb); |
| 13403 | #endif |
| 13404 | sp = NULL; |
| 13405 | } else { |
| 13406 | if (sp->sinfo_flags & SCTP_ADDR_OVER) { |
| 13407 | sp->net = net; |
| 13408 | atomic_add_int(&sp->net->ref_count, 1); |
| 13409 | } else { |
| 13410 | sp->net = NULL; |
| 13411 | } |
| 13412 | sctp_set_prsctp_policy(sp); |
| 13413 | } |
| 13414 | out_now: |
| 13415 | return (sp); |
| 13416 | } |
| 13417 |
| 13418 |
| 13419 | int |
| 13420 | sctp_sosend(struct socket *so, |
| 13421 | struct sockaddr *addr, |
| 13422 | struct uio *uio, |
| 13423 | #ifdef __Panda__ |
| 13424 | pakhandle_type top, |
| 13425 | pakhandle_type icontrol, |
| 13426 | #else |
| 13427 | struct mbuf *top, |
| 13428 | struct mbuf *control, |
| 13429 | #endif |
| 13430 | #if defined(__APPLE__) || defined(__Panda__) |
| 13431 | int flags |
| 13432 | #else |
| 13433 | int flags, |
| 13434 | #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 |
| 13435 | struct thread *p |
| 13436 | #elif defined(__Windows__) |
| 13437 | PKTHREAD p |
| 13438 | #else |
| 13439 | #if defined(__Userspace__) |
| 13440 | /* |
| 13441 | * proc is a dummy in __Userspace__ and will not be passed |
| 13442 | * to sctp_lower_sosend |
| 13443 | */ |
| 13444 | #endif |
| 13445 | struct proc *p |
| 13446 | #endif |
| 13447 | #endif |
| 13448 | ) |
| 13449 | { |
| 13450 | #ifdef __Panda__ |
| 13451 | struct mbuf *control = NULL; |
| 13452 | #endif |
| 13453 | #if defined(__APPLE__) |
| 13454 | struct proc *p = current_proc(); |
| 13455 | #endif |
| 13456 | int error, use_sndinfo = 0; |
| 13457 | struct sctp_sndrcvinfo sndrcvninfo; |
| 13458 | struct sockaddr *addr_to_use; |
| 13459 | #if defined(INET) && defined(INET6) |
| 13460 | struct sockaddr_in sin; |
| 13461 | #endif |
| 13462 |
| 13463 | #if defined(__APPLE__) |
| 13464 | SCTP_SOCKET_LOCK(so, 1); |
| 13465 | #endif |
| 13466 | #ifdef __Panda__ |
| 13467 | control = SCTP_HEADER_TO_CHAIN(icontrol); |
| 13468 | #endif |
| 13469 | if (control) { |
| 13470 | /* process cmsg snd/rcv info (maybe a assoc-id) */ |
| 13471 | if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&sndrcvninfo, control, |
| 13472 | sizeof(sndrcvninfo))) { |
| 13473 | /* got one */ |
| 13474 | use_sndinfo = 1; |
| 13475 | } |
| 13476 | } |
| 13477 | addr_to_use = addr; |
| 13478 | #if defined(INET) && defined(INET6) |
| 13479 | if ((addr) && (addr->sa_family == AF_INET6)) { |
| 13480 | struct sockaddr_in6 *sin6; |
| 13481 |
| 13482 | sin6 = (struct sockaddr_in6 *)addr; |
| 13483 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { |
| 13484 | in6_sin6_2_sin(&sin, sin6); |
| 13485 | addr_to_use = (struct sockaddr *)&sin; |
| 13486 | } |
| 13487 | } |
| 13488 | #endif |
| 13489 | error = sctp_lower_sosend(so, addr_to_use, uio, top, |
| 13490 | #ifdef __Panda__ |
| 13491 | icontrol, |
| 13492 | #else |
| 13493 | control, |
| 13494 | #endif |
| 13495 | flags, |
| 13496 | use_sndinfo ? &sndrcvninfo: NULL |
| 13497 | #if !(defined(__Panda__) || defined(__Userspace__)) |
| 13498 | , p |
| 13499 | #endif |
| 13500 | ); |
| 13501 | #if defined(__APPLE__) |
| 13502 | SCTP_SOCKET_UNLOCK(so, 1); |
| 13503 | #endif |
| 13504 | return (error); |
| 13505 | } |
| 13506 |
| 13507 |
| 13508 | int |
| 13509 | sctp_lower_sosend(struct socket *so, |
| 13510 | struct sockaddr *addr, |
| 13511 | struct uio *uio, |
| 13512 | #ifdef __Panda__ |
| 13513 | pakhandle_type i_pak, |
| 13514 | pakhandle_type i_control, |
| 13515 | #else |
| 13516 | struct mbuf *i_pak, |
| 13517 | struct mbuf *control, |
| 13518 | #endif |
| 13519 | int flags, |
| 13520 | struct sctp_sndrcvinfo *srcv |
| 13521 | #if !(defined( __Panda__) || defined(__Userspace__)) |
| 13522 | , |
| 13523 | #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 |
| 13524 | struct thread *p |
| 13525 | #elif defined(__Windows__) |
| 13526 | PKTHREAD p |
| 13527 | #else |
| 13528 | struct proc *p |
| 13529 | #endif |
| 13530 | #endif |
| 13531 | ) |
| 13532 | { |
| 13533 | ssize_t sndlen = 0, max_len, local_add_more; |
| 13534 | int error, len; |
| 13535 | struct mbuf *top = NULL; |
| 13536 | #ifdef __Panda__ |
| 13537 | struct mbuf *control = NULL; |
| 13538 | #endif |
| 13539 | int queue_only = 0, queue_only_for_init = 0; |
| 13540 | int free_cnt_applied = 0; |
| 13541 | int un_sent; |
| 13542 | int now_filled = 0; |
| 13543 | unsigned int inqueue_bytes = 0; |
| 13544 | struct sctp_block_entry be; |
| 13545 | struct sctp_inpcb *inp; |
| 13546 | struct sctp_tcb *stcb = NULL; |
| 13547 | struct timeval now; |
| 13548 | struct sctp_nets *net; |
| 13549 | struct sctp_association *asoc; |
| 13550 | struct sctp_inpcb *t_inp; |
| 13551 | int user_marks_eor; |
| 13552 | int create_lock_applied = 0; |
| 13553 | int nagle_applies = 0; |
| 13554 | int some_on_control = 0; |
| 13555 | int got_all_of_the_send = 0; |
| 13556 | int hold_tcblock = 0; |
| 13557 | int non_blocking = 0; |
| 13558 | ssize_t local_soresv = 0; |
| 13559 | uint16_t port; |
| 13560 | uint16_t sinfo_flags; |
| 13561 | sctp_assoc_t sinfo_assoc_id; |
| 13562 |
| 13563 | error = 0; |
| 13564 | net = NULL; |
| 13565 | stcb = NULL; |
| 13566 | asoc = NULL; |
| 13567 |
| 13568 | #if defined(__APPLE__) |
| 13569 | sctp_lock_assert(so); |
| 13570 | #endif |
| 13571 | t_inp = inp = (struct sctp_inpcb *)so->so_pcb; |
| 13572 | if (inp == NULL) { |
| 13573 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13574 | error = EINVAL; |
| 13575 | if (i_pak) { |
| 13576 | SCTP_RELEASE_PKT(i_pak); |
| 13577 | } |
| 13578 | return (error); |
| 13579 | } |
| 13580 | if ((uio == NULL) && (i_pak == NULL)) { |
| 13581 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13582 | return (EINVAL); |
| 13583 | } |
| 13584 | user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR); |
| 13585 | atomic_add_int(&inp->total_sends, 1); |
| 13586 | if (uio) { |
| 13587 | #if defined(__APPLE__) |
| 13588 | #if defined(APPLE_LEOPARD) |
| 13589 | if (uio->uio_resid < 0) { |
| 13590 | #else |
| 13591 | if (uio_resid(uio) < 0) { |
| 13592 | #endif |
| 13593 | #else |
| 13594 | if (uio->uio_resid < 0) { |
| 13595 | #endif |
| 13596 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13597 | return (EINVAL); |
| 13598 | } |
| 13599 | #if defined(__APPLE__) |
| 13600 | #if defined(APPLE_LEOPARD) |
| 13601 | sndlen = uio->uio_resid; |
| 13602 | #else |
| 13603 | sndlen = uio_resid(uio); |
| 13604 | #endif |
| 13605 | #else |
| 13606 | sndlen = uio->uio_resid; |
| 13607 | #endif |
| 13608 | } else { |
| 13609 | top = SCTP_HEADER_TO_CHAIN(i_pak); |
| 13610 | #ifdef __Panda__ |
| 13611 | /*- |
| 13612 | * app len indicates the datalen, dgsize for cases |
| 13613 | * of SCTP_EOF/ABORT will not have the right len |
| 13614 | */ |
| 13615 | sndlen = SCTP_APP_DATA_LEN(i_pak); |
| 13616 | /*- |
| 13617 | * Set the particle len also to zero to match |
| 13618 | * up with app len. We only have one particle |
| 13619 | * if app len is zero for Panda. This is ensured |
| 13620 | * in the socket lib |
| 13621 | */ |
| 13622 | if (sndlen == 0) { |
| 13623 | SCTP_BUF_LEN(top) = 0; |
| 13624 | } |
| 13625 | /*- |
| 13626 | * We delink the chain from header, but keep |
| 13627 | * the header around as we will need it in |
| 13628 | * EAGAIN case |
| 13629 | */ |
| 13630 | SCTP_DETACH_HEADER_FROM_CHAIN(i_pak); |
| 13631 | #else |
| 13632 | sndlen = SCTP_HEADER_LEN(i_pak); |
| 13633 | #endif |
| 13634 | } |
| 13635 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %zu\n", |
| 13636 | (void *)addr, |
| 13637 | sndlen); |
| 13638 | #ifdef __Panda__ |
| 13639 | if (i_control) { |
| 13640 | control = SCTP_HEADER_TO_CHAIN(i_control); |
| 13641 | } |
| 13642 | #endif |
| 13643 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && |
| 13644 | SCTP_IS_LISTENING(inp)) { |
| 13645 | /* The listener can NOT send */ |
| 13646 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN); |
| 13647 | error = ENOTCONN; |
| 13648 | goto out_unlocked; |
| 13649 | } |
| 13650 | /** |
| 13651 | * Pre-screen address, if one is given the sin-len |
| 13652 | * must be set correctly! |
| 13653 | */ |
| 13654 | if (addr) { |
| 13655 | union sctp_sockstore *raddr = (union sctp_sockstore *)addr; |
| 13656 | switch (raddr->sa.sa_family) { |
| 13657 | #ifdef INET |
| 13658 | case AF_INET: |
| 13659 | #ifdef HAVE_SIN_LEN |
| 13660 | if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) { |
| 13661 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13662 | error = EINVAL; |
| 13663 | goto out_unlocked; |
| 13664 | } |
| 13665 | #endif |
| 13666 | port = raddr->sin.sin_port; |
| 13667 | break; |
| 13668 | #endif |
| 13669 | #ifdef INET6 |
| 13670 | case AF_INET6: |
| 13671 | #ifdef HAVE_SIN6_LEN |
| 13672 | if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) { |
| 13673 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13674 | error = EINVAL; |
| 13675 | goto out_unlocked; |
| 13676 | } |
| 13677 | #endif |
| 13678 | port = raddr->sin6.sin6_port; |
| 13679 | break; |
| 13680 | #endif |
| 13681 | #if defined(__Userspace__) |
| 13682 | case AF_CONN: |
| 13683 | #ifdef HAVE_SCONN_LEN |
| 13684 | if (raddr->sconn.sconn_len != sizeof(struct sockaddr_conn)) { |
| 13685 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13686 | error = EINVAL; |
| 13687 | goto out_unlocked; |
| 13688 | } |
| 13689 | #endif |
| 13690 | port = raddr->sconn.sconn_port; |
| 13691 | break; |
| 13692 | #endif |
| 13693 | default: |
| 13694 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAFNOSUPPORT); |
| 13695 | error = EAFNOSUPPORT; |
| 13696 | goto out_unlocked; |
| 13697 | } |
| 13698 | } else |
| 13699 | port = 0; |
| 13700 |
| 13701 | if (srcv) { |
| 13702 | sinfo_flags = srcv->sinfo_flags; |
| 13703 | sinfo_assoc_id = srcv->sinfo_assoc_id; |
| 13704 | if (INVALID_SINFO_FLAG(sinfo_flags) || |
| 13705 | PR_SCTP_INVALID_POLICY(sinfo_flags)) { |
| 13706 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13707 | error = EINVAL; |
| 13708 | goto out_unlocked; |
| 13709 | } |
| 13710 | if (srcv->sinfo_flags) |
| 13711 | SCTP_STAT_INCR(sctps_sends_with_flags); |
| 13712 | } else { |
| 13713 | sinfo_flags = inp->def_send.sinfo_flags; |
| 13714 | sinfo_assoc_id = inp->def_send.sinfo_assoc_id; |
| 13715 | } |
| 13716 | #if defined(__FreeBSD__) |
| 13717 | if (flags & MSG_EOR) { |
| 13718 | sinfo_flags |= SCTP_EOR; |
| 13719 | } |
| 13720 | if (flags & MSG_EOF) { |
| 13721 | sinfo_flags |= SCTP_EOF; |
| 13722 | } |
| 13723 | #endif |
| 13724 | if (sinfo_flags & SCTP_SENDALL) { |
| 13725 | /* its a sendall */ |
| 13726 | error = sctp_sendall(inp, uio, top, srcv); |
| 13727 | top = NULL; |
| 13728 | goto out_unlocked; |
| 13729 | } |
| 13730 | if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) { |
| 13731 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13732 | error = EINVAL; |
| 13733 | goto out_unlocked; |
| 13734 | } |
| 13735 | /* now we must find the assoc */ |
| 13736 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) || |
| 13737 | (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { |
| 13738 | SCTP_INP_RLOCK(inp); |
| 13739 | stcb = LIST_FIRST(&inp->sctp_asoc_list); |
| 13740 | if (stcb) { |
| 13741 | SCTP_TCB_LOCK(stcb); |
| 13742 | hold_tcblock = 1; |
| 13743 | } |
| 13744 | SCTP_INP_RUNLOCK(inp); |
| 13745 | } else if (sinfo_assoc_id) { |
| 13746 | stcb = sctp_findassociation_ep_asocid(inp, sinfo_assoc_id, 1); |
| 13747 | if (stcb != NULL) { |
| 13748 | hold_tcblock = 1; |
| 13749 | } |
| 13750 | } else if (addr) { |
| 13751 | /*- |
| 13752 | * Since we did not use findep we must |
| 13753 | * increment it, and if we don't find a tcb |
| 13754 | * decrement it. |
| 13755 | */ |
| 13756 | SCTP_INP_WLOCK(inp); |
| 13757 | SCTP_INP_INCR_REF(inp); |
| 13758 | SCTP_INP_WUNLOCK(inp); |
| 13759 | stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL); |
| 13760 | if (stcb == NULL) { |
| 13761 | SCTP_INP_WLOCK(inp); |
| 13762 | SCTP_INP_DECR_REF(inp); |
| 13763 | SCTP_INP_WUNLOCK(inp); |
| 13764 | } else { |
| 13765 | hold_tcblock = 1; |
| 13766 | } |
| 13767 | } |
| 13768 | if ((stcb == NULL) && (addr)) { |
| 13769 | /* Possible implicit send? */ |
| 13770 | SCTP_ASOC_CREATE_LOCK(inp); |
| 13771 | create_lock_applied = 1; |
| 13772 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || |
| 13773 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { |
| 13774 | /* Should I really unlock ? */ |
| 13775 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13776 | error = EINVAL; |
| 13777 | goto out_unlocked; |
| 13778 |
| 13779 | } |
| 13780 | if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) && |
| 13781 | (addr->sa_family == AF_INET6)) { |
| 13782 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13783 | error = EINVAL; |
| 13784 | goto out_unlocked; |
| 13785 | } |
| 13786 | SCTP_INP_WLOCK(inp); |
| 13787 | SCTP_INP_INCR_REF(inp); |
| 13788 | SCTP_INP_WUNLOCK(inp); |
| 13789 | /* With the lock applied look again */ |
| 13790 | stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL); |
| 13791 | #if defined(INET) || defined(INET6) |
| 13792 | if ((stcb == NULL) && (control != NULL) && (port > 0)) { |
| 13793 | stcb = sctp_findassociation_cmsgs(&t_inp, port, control, &net, &error); |
| 13794 | } |
| 13795 | #endif |
| 13796 | if (stcb == NULL) { |
| 13797 | SCTP_INP_WLOCK(inp); |
| 13798 | SCTP_INP_DECR_REF(inp); |
| 13799 | SCTP_INP_WUNLOCK(inp); |
| 13800 | } else { |
| 13801 | hold_tcblock = 1; |
| 13802 | } |
| 13803 | if (error) { |
| 13804 | goto out_unlocked; |
| 13805 | } |
| 13806 | if (t_inp != inp) { |
| 13807 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN); |
| 13808 | error = ENOTCONN; |
| 13809 | goto out_unlocked; |
| 13810 | } |
| 13811 | } |
| 13812 | if (stcb == NULL) { |
| 13813 | if (addr == NULL) { |
| 13814 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT); |
| 13815 | error = ENOENT; |
| 13816 | goto out_unlocked; |
| 13817 | } else { |
| 13818 | /* We must go ahead and start the INIT process */ |
| 13819 | uint32_t vrf_id; |
| 13820 |
| 13821 | if ((sinfo_flags & SCTP_ABORT) || |
| 13822 | ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) { |
| 13823 | /*- |
| 13824 | * User asks to abort a non-existant assoc, |
| 13825 | * or EOF a non-existant assoc with no data |
| 13826 | */ |
| 13827 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT); |
| 13828 | error = ENOENT; |
| 13829 | goto out_unlocked; |
| 13830 | } |
| 13831 | /* get an asoc/stcb struct */ |
| 13832 | vrf_id = inp->def_vrf_id; |
| 13833 | #ifdef INVARIANTS |
| 13834 | if (create_lock_applied == 0) { |
| 13835 | panic("Error, should hold create lock and I don't?"); |
| 13836 | } |
| 13837 | #endif |
| 13838 | stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, |
| 13839 | inp->sctp_ep.pre_open_stream_count, |
| 13840 | inp->sctp_ep.port, |
| 13841 | #if !(defined( __Panda__) || defined(__Userspace__)) |
| 13842 | p, |
| 13843 | #else |
| 13844 | (struct proc *)NULL, |
| 13845 | #endif |
| 13846 | SCTP_INITIALIZE_AUTH_PARAMS); |
| 13847 | if (stcb == NULL) { |
| 13848 | /* Error is setup for us in the call */ |
| 13849 | goto out_unlocked; |
| 13850 | } |
| 13851 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { |
| 13852 | stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; |
| 13853 | /* Set the connected flag so we can queue data */ |
| 13854 | soisconnecting(so); |
| 13855 | } |
| 13856 | hold_tcblock = 1; |
| 13857 | if (create_lock_applied) { |
| 13858 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 13859 | create_lock_applied = 0; |
| 13860 | } else { |
| 13861 | SCTP_PRINTF("Huh-3? create lock should have been on??\n"); |
| 13862 | } |
| 13863 | /* Turn on queue only flag to prevent data from being sent */ |
| 13864 | queue_only = 1; |
| 13865 | asoc = &stcb->asoc; |
| 13866 | SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT); |
| 13867 | (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); |
| 13868 |
| 13869 | if (control) { |
| 13870 | if (sctp_process_cmsgs_for_init(stcb, control, &error)) { |
| 13871 | sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, |
| 13872 | SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5); |
| 13873 | hold_tcblock = 0; |
| 13874 | stcb = NULL; |
| 13875 | goto out_unlocked; |
| 13876 | } |
| 13877 | } |
| 13878 | /* out with the INIT */ |
| 13879 | queue_only_for_init = 1; |
| 13880 | /*- |
| 13881 | * we may want to dig in after this call and adjust the MTU |
| 13882 | * value. It defaulted to 1500 (constant) but the ro |
| 13883 | * structure may now have an update and thus we may need to |
| 13884 | * change it BEFORE we append the message. |
| 13885 | */ |
| 13886 | } |
| 13887 | } else |
| 13888 | asoc = &stcb->asoc; |
| 13889 | if (srcv == NULL) { |
| 13890 | srcv = (struct sctp_sndrcvinfo *)&asoc->def_send; |
| 13891 | sinfo_flags = srcv->sinfo_flags; |
| 13892 | #if defined(__FreeBSD__) |
| 13893 | if (flags & MSG_EOR) { |
| 13894 | sinfo_flags |= SCTP_EOR; |
| 13895 | } |
| 13896 | if (flags & MSG_EOF) { |
| 13897 | sinfo_flags |= SCTP_EOF; |
| 13898 | } |
| 13899 | #endif |
| 13900 | } |
| 13901 | if (sinfo_flags & SCTP_ADDR_OVER) { |
| 13902 | if (addr) |
| 13903 | net = sctp_findnet(stcb, addr); |
| 13904 | else |
| 13905 | net = NULL; |
| 13906 | if ((net == NULL) || |
| 13907 | ((port != 0) && (port != stcb->rport))) { |
| 13908 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13909 | error = EINVAL; |
| 13910 | goto out_unlocked; |
| 13911 | } |
| 13912 | } else { |
| 13913 | if (stcb->asoc.alternate) { |
| 13914 | net = stcb->asoc.alternate; |
| 13915 | } else { |
| 13916 | net = stcb->asoc.primary_destination; |
| 13917 | } |
| 13918 | } |
| 13919 | atomic_add_int(&stcb->total_sends, 1); |
| 13920 | /* Keep the stcb from being freed under our feet */ |
| 13921 | atomic_add_int(&asoc->refcnt, 1); |
| 13922 | free_cnt_applied = 1; |
| 13923 |
| 13924 | if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) { |
| 13925 | if (sndlen > (ssize_t)asoc->smallest_mtu) { |
| 13926 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE); |
| 13927 | error = EMSGSIZE; |
| 13928 | goto out_unlocked; |
| 13929 | } |
| 13930 | } |
| 13931 | #if defined(__Userspace__) |
| 13932 | if (inp->recv_callback) { |
| 13933 | non_blocking = 1; |
| 13934 | } |
| 13935 | #endif |
| 13936 | if (SCTP_SO_IS_NBIO(so) |
| 13937 | #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 |
| 13938 | || (flags & (MSG_NBIO | MSG_DONTWAIT)) != 0 |
| 13939 | #endif |
| 13940 | ) { |
| 13941 | non_blocking = 1; |
| 13942 | } |
| 13943 | /* would we block? */ |
| 13944 | if (non_blocking) { |
| 13945 | ssize_t amount; |
| 13946 |
| 13947 | if (hold_tcblock == 0) { |
| 13948 | SCTP_TCB_LOCK(stcb); |
| 13949 | hold_tcblock = 1; |
| 13950 | } |
| 13951 | inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 13952 | if (user_marks_eor == 0) { |
| 13953 | amount = sndlen; |
| 13954 | } else { |
| 13955 | amount = 1; |
| 13956 | } |
| 13957 | if ((SCTP_SB_LIMIT_SND(so) < (amount + inqueue_bytes + stcb->asoc.sb_send_resv)) || |
| 13958 | (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) { |
| 13959 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK); |
| 13960 | if (sndlen > (ssize_t)SCTP_SB_LIMIT_SND(so)) |
| 13961 | error = EMSGSIZE; |
| 13962 | else |
| 13963 | error = EWOULDBLOCK; |
| 13964 | goto out_unlocked; |
| 13965 | } |
| 13966 | stcb->asoc.sb_send_resv += (uint32_t)sndlen; |
| 13967 | SCTP_TCB_UNLOCK(stcb); |
| 13968 | hold_tcblock = 0; |
| 13969 | } else { |
| 13970 | atomic_add_int(&stcb->asoc.sb_send_resv, sndlen); |
| 13971 | } |
| 13972 | local_soresv = sndlen; |
| 13973 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 13974 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); |
| 13975 | error = ECONNRESET; |
| 13976 | goto out_unlocked; |
| 13977 | } |
| 13978 | if (create_lock_applied) { |
| 13979 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 13980 | create_lock_applied = 0; |
| 13981 | } |
| 13982 | /* Is the stream no. valid? */ |
| 13983 | if (srcv->sinfo_stream >= asoc->streamoutcnt) { |
| 13984 | /* Invalid stream number */ |
| 13985 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13986 | error = EINVAL; |
| 13987 | goto out_unlocked; |
| 13988 | } |
| 13989 | if ((asoc->strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPEN) && |
| 13990 | (asoc->strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPENING)) { |
| 13991 | /* |
| 13992 | * Can't queue any data while stream reset is underway. |
| 13993 | */ |
| 13994 | if (asoc->strmout[srcv->sinfo_stream].state > SCTP_STREAM_OPEN) { |
| 13995 | error = EAGAIN; |
| 13996 | } else { |
| 13997 | error = EINVAL; |
| 13998 | } |
| 13999 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, error); |
| 14000 | goto out_unlocked; |
| 14001 | } |
| 14002 | if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) || |
| 14003 | (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) { |
| 14004 | queue_only = 1; |
| 14005 | } |
| 14006 | /* we are now done with all control */ |
| 14007 | if (control) { |
| 14008 | sctp_m_freem(control); |
| 14009 | control = NULL; |
| 14010 | } |
| 14011 | if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) || |
| 14012 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) || |
| 14013 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) || |
| 14014 | (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) { |
| 14015 | if (sinfo_flags & SCTP_ABORT) { |
| 14016 | ; |
| 14017 | } else { |
| 14018 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); |
| 14019 | error = ECONNRESET; |
| 14020 | goto out_unlocked; |
| 14021 | } |
| 14022 | } |
| 14023 | /* Ok, we will attempt a msgsnd :> */ |
| 14024 | #if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__)) |
| 14025 | if (p) { |
| 14026 | #if defined(__FreeBSD__) && __FreeBSD_version >= 603000 |
| 14027 | p->td_ru.ru_msgsnd++; |
| 14028 | #elif defined(__FreeBSD__) && __FreeBSD_version >= 500000 |
| 14029 | p->td_proc->p_stats->p_ru.ru_msgsnd++; |
| 14030 | #else |
| 14031 | p->p_stats->p_ru.ru_msgsnd++; |
| 14032 | #endif |
| 14033 | } |
| 14034 | #endif |
| 14035 | /* Are we aborting? */ |
| 14036 | if (sinfo_flags & SCTP_ABORT) { |
| 14037 | struct mbuf *mm; |
| 14038 | ssize_t tot_demand, tot_out = 0, max_out; |
| 14039 |
| 14040 | SCTP_STAT_INCR(sctps_sends_with_abort); |
| 14041 | if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) || |
| 14042 | (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) { |
| 14043 | /* It has to be up before we abort */ |
| 14044 | /* how big is the user initiated abort? */ |
| 14045 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 14046 | error = EINVAL; |
| 14047 | goto out; |
| 14048 | } |
| 14049 | if (hold_tcblock) { |
| 14050 | SCTP_TCB_UNLOCK(stcb); |
| 14051 | hold_tcblock = 0; |
| 14052 | } |
| 14053 | if (top) { |
| 14054 | struct mbuf *cntm = NULL; |
| 14055 |
| 14056 | mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_WAITOK, 1, MT_DATA); |
| 14057 | if (sndlen != 0) { |
| 14058 | for (cntm = top; cntm; cntm = SCTP_BUF_NEXT(cntm)) { |
| 14059 | tot_out += SCTP_BUF_LEN(cntm); |
| 14060 | } |
| 14061 | } |
| 14062 | } else { |
| 14063 | /* Must fit in a MTU */ |
| 14064 | tot_out = sndlen; |
| 14065 | tot_demand = (tot_out + sizeof(struct sctp_paramhdr)); |
| 14066 | if (tot_demand > SCTP_DEFAULT_ADD_MORE) { |
| 14067 | /* To big */ |
| 14068 | SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE); |
| 14069 | error = EMSGSIZE; |
| 14070 | goto out; |
| 14071 | } |
| 14072 | mm = sctp_get_mbuf_for_msg((unsigned int)tot_demand, 0, M_WAITOK, 1, MT_DATA); |
| 14073 | } |
| 14074 | if (mm == NULL) { |
| 14075 | SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 14076 | error = ENOMEM; |
| 14077 | goto out; |
| 14078 | } |
| 14079 | max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr); |
| 14080 | max_out -= sizeof(struct sctp_abort_msg); |
| 14081 | if (tot_out > max_out) { |
| 14082 | tot_out = max_out; |
| 14083 | } |
| 14084 | if (mm) { |
| 14085 | struct sctp_paramhdr *ph; |
| 14086 |
| 14087 | /* now move forward the data pointer */ |
| 14088 | ph = mtod(mm, struct sctp_paramhdr *); |
| 14089 | ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); |
| 14090 | ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + tot_out)); |
| 14091 | ph++; |
| 14092 | SCTP_BUF_LEN(mm) = (int)(tot_out + sizeof(struct sctp_paramhdr)); |
| 14093 | if (top == NULL) { |
| 14094 | #if defined(__APPLE__) |
| 14095 | SCTP_SOCKET_UNLOCK(so, 0); |
| 14096 | #endif |
| 14097 | error = uiomove((caddr_t)ph, (int)tot_out, uio); |
| 14098 | #if defined(__APPLE__) |
| 14099 | SCTP_SOCKET_LOCK(so, 0); |
| 14100 | #endif |
| 14101 | if (error) { |
| 14102 | /*- |
| 14103 | * Here if we can't get his data we |
| 14104 | * still abort we just don't get to |
| 14105 | * send the users note :-0 |
| 14106 | */ |
| 14107 | sctp_m_freem(mm); |
| 14108 | mm = NULL; |
| 14109 | } |
| 14110 | } else { |
| 14111 | if (sndlen != 0) { |
| 14112 | SCTP_BUF_NEXT(mm) = top; |
| 14113 | } |
| 14114 | } |
| 14115 | } |
| 14116 | if (hold_tcblock == 0) { |
| 14117 | SCTP_TCB_LOCK(stcb); |
| 14118 | } |
| 14119 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 14120 | free_cnt_applied = 0; |
| 14121 | /* release this lock, otherwise we hang on ourselves */ |
| 14122 | sctp_abort_an_association(stcb->sctp_ep, stcb, mm, SCTP_SO_LOCKED); |
| 14123 | /* now relock the stcb so everything is sane */ |
| 14124 | hold_tcblock = 0; |
| 14125 | stcb = NULL; |
| 14126 | /* In this case top is already chained to mm |
| 14127 | * avoid double free, since we free it below if |
| 14128 | * top != NULL and driver would free it after sending |
| 14129 | * the packet out |
| 14130 | */ |
| 14131 | if (sndlen != 0) { |
| 14132 | top = NULL; |
| 14133 | } |
| 14134 | goto out_unlocked; |
| 14135 | } |
| 14136 | /* Calculate the maximum we can send */ |
| 14137 | inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 14138 | if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) { |
| 14139 | max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes; |
| 14140 | } else { |
| 14141 | max_len = 0; |
| 14142 | } |
| 14143 | if (hold_tcblock) { |
| 14144 | SCTP_TCB_UNLOCK(stcb); |
| 14145 | hold_tcblock = 0; |
| 14146 | } |
| 14147 | if (asoc->strmout == NULL) { |
| 14148 | /* huh? software error */ |
| 14149 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT); |
| 14150 | error = EFAULT; |
| 14151 | goto out_unlocked; |
| 14152 | } |
| 14153 |
| 14154 | /* Unless E_EOR mode is on, we must make a send FIT in one call. */ |
| 14155 | if ((user_marks_eor == 0) && |
| 14156 | (sndlen > (ssize_t)SCTP_SB_LIMIT_SND(stcb->sctp_socket))) { |
| 14157 | /* It will NEVER fit */ |
| 14158 | SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE); |
| 14159 | error = EMSGSIZE; |
| 14160 | goto out_unlocked; |
| 14161 | } |
| 14162 | if ((uio == NULL) && user_marks_eor) { |
| 14163 | /*- |
| 14164 | * We do not support eeor mode for |
| 14165 | * sending with mbuf chains (like sendfile). |
| 14166 | */ |
| 14167 | SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 14168 | error = EINVAL; |
| 14169 | goto out_unlocked; |
| 14170 | } |
| 14171 |
| 14172 | if (user_marks_eor) { |
| 14173 | local_add_more = (ssize_t)min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold)); |
| 14174 | } else { |
| 14175 | /*- |
| 14176 | * For non-eeor the whole message must fit in |
| 14177 | * the socket send buffer. |
| 14178 | */ |
| 14179 | local_add_more = sndlen; |
| 14180 | } |
| 14181 | len = 0; |
| 14182 | if (non_blocking) { |
| 14183 | goto skip_preblock; |
| 14184 | } |
| 14185 | if (((max_len <= local_add_more) && |
| 14186 | ((ssize_t)SCTP_SB_LIMIT_SND(so) >= local_add_more)) || |
| 14187 | (max_len == 0) || |
| 14188 | ((stcb->asoc.chunks_on_out_queue+stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) { |
| 14189 | /* No room right now ! */ |
| 14190 | SOCKBUF_LOCK(&so->so_snd); |
| 14191 | inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 14192 | while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) || |
| 14193 | ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) { |
| 14194 | SCTPDBG(SCTP_DEBUG_OUTPUT1,"pre_block limit:%u <(inq:%d + %zd) || (%d+%d > %d)\n", |
| 14195 | (unsigned int)SCTP_SB_LIMIT_SND(so), |
| 14196 | inqueue_bytes, |
| 14197 | local_add_more, |
| 14198 | stcb->asoc.stream_queue_cnt, |
| 14199 | stcb->asoc.chunks_on_out_queue, |
| 14200 | SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)); |
| 14201 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { |
| 14202 | sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, asoc, sndlen); |
| 14203 | } |
| 14204 | be.error = 0; |
| 14205 | #if !defined(__Panda__) && !defined(__Windows__) |
| 14206 | stcb->block_entry = &be; |
| 14207 | #endif |
| 14208 | error = sbwait(&so->so_snd); |
| 14209 | stcb->block_entry = NULL; |
| 14210 | if (error || so->so_error || be.error) { |
| 14211 | if (error == 0) { |
| 14212 | if (so->so_error) |
| 14213 | error = so->so_error; |
| 14214 | if (be.error) { |
| 14215 | error = be.error; |
| 14216 | } |
| 14217 | } |
| 14218 | SOCKBUF_UNLOCK(&so->so_snd); |
| 14219 | goto out_unlocked; |
| 14220 | } |
| 14221 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { |
| 14222 | sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK, |
| 14223 | asoc, stcb->asoc.total_output_queue_size); |
| 14224 | } |
| 14225 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 14226 | SOCKBUF_UNLOCK(&so->so_snd); |
| 14227 | goto out_unlocked; |
| 14228 | } |
| 14229 | inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 14230 | } |
| 14231 | if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) { |
| 14232 | max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes; |
| 14233 | } else { |
| 14234 | max_len = 0; |
| 14235 | } |
| 14236 | SOCKBUF_UNLOCK(&so->so_snd); |
| 14237 | } |
| 14238 |
| 14239 | skip_preblock: |
| 14240 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 14241 | goto out_unlocked; |
| 14242 | } |
| 14243 | #if defined(__APPLE__) |
| 14244 | error = sblock(&so->so_snd, SBLOCKWAIT(flags)); |
| 14245 | #endif |
| 14246 | /* sndlen covers for mbuf case |
| 14247 | * uio_resid covers for the non-mbuf case |
| 14248 | * NOTE: uio will be null when top/mbuf is passed |
| 14249 | */ |
| 14250 | if (sndlen == 0) { |
| 14251 | if (sinfo_flags & SCTP_EOF) { |
| 14252 | got_all_of_the_send = 1; |
| 14253 | goto dataless_eof; |
| 14254 | } else { |
| 14255 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 14256 | error = EINVAL; |
| 14257 | goto out; |
| 14258 | } |
| 14259 | } |
| 14260 | if (top == NULL) { |
| 14261 | struct sctp_stream_queue_pending *sp; |
| 14262 | struct sctp_stream_out *strm; |
| 14263 | uint32_t sndout; |
| 14264 |
| 14265 | SCTP_TCB_SEND_LOCK(stcb); |
| 14266 | if ((asoc->stream_locked) && |
| 14267 | (asoc->stream_locked_on != srcv->sinfo_stream)) { |
| 14268 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14269 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 14270 | error = EINVAL; |
| 14271 | goto out; |
| 14272 | } |
| 14273 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14274 |
| 14275 | strm = &stcb->asoc.strmout[srcv->sinfo_stream]; |
| 14276 | if (strm->last_msg_incomplete == 0) { |
| 14277 | do_a_copy_in: |
| 14278 | sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error); |
| 14279 | if (error) { |
| 14280 | goto out; |
| 14281 | } |
| 14282 | SCTP_TCB_SEND_LOCK(stcb); |
| 14283 | if (sp->msg_is_complete) { |
| 14284 | strm->last_msg_incomplete = 0; |
| 14285 | asoc->stream_locked = 0; |
| 14286 | } else { |
| 14287 | /* Just got locked to this guy in |
| 14288 | * case of an interrupt. |
| 14289 | */ |
| 14290 | strm->last_msg_incomplete = 1; |
| 14291 | if (stcb->asoc.idata_supported == 0) { |
| 14292 | asoc->stream_locked = 1; |
| 14293 | asoc->stream_locked_on = srcv->sinfo_stream; |
| 14294 | } |
| 14295 | sp->sender_all_done = 0; |
| 14296 | } |
| 14297 | sctp_snd_sb_alloc(stcb, sp->length); |
| 14298 | atomic_add_int(&asoc->stream_queue_cnt, 1); |
| 14299 | if (sinfo_flags & SCTP_UNORDERED) { |
| 14300 | SCTP_STAT_INCR(sctps_sends_with_unord); |
| 14301 | } |
| 14302 | TAILQ_INSERT_TAIL(&strm->outqueue, sp, next); |
| 14303 | stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp, 1); |
| 14304 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14305 | } else { |
| 14306 | SCTP_TCB_SEND_LOCK(stcb); |
| 14307 | sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead); |
| 14308 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14309 | if (sp == NULL) { |
| 14310 | /* ???? Huh ??? last msg is gone */ |
| 14311 | #ifdef INVARIANTS |
| 14312 | panic("Warning: Last msg marked incomplete, yet nothing left?"); |
| 14313 | #else |
| 14314 | SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n"); |
| 14315 | strm->last_msg_incomplete = 0; |
| 14316 | #endif |
| 14317 | goto do_a_copy_in; |
| 14318 |
| 14319 | } |
| 14320 | } |
| 14321 | #if defined(__APPLE__) |
| 14322 | #if defined(APPLE_LEOPARD) |
| 14323 | while (uio->uio_resid > 0) { |
| 14324 | #else |
| 14325 | while (uio_resid(uio) > 0) { |
| 14326 | #endif |
| 14327 | #else |
| 14328 | while (uio->uio_resid > 0) { |
| 14329 | #endif |
| 14330 | /* How much room do we have? */ |
| 14331 | struct mbuf *new_tail, *mm; |
| 14332 |
| 14333 | inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 14334 | if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) |
| 14335 | max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes; |
| 14336 | else |
| 14337 | max_len = 0; |
| 14338 |
| 14339 | if ((max_len > (ssize_t)SCTP_BASE_SYSCTL(sctp_add_more_threshold)) || |
| 14340 | (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) || |
| 14341 | #if defined(__APPLE__) |
| 14342 | #if defined(APPLE_LEOPARD) |
| 14343 | (uio->uio_resid && (uio->uio_resid <= max_len))) { |
| 14344 | #else |
| 14345 | (uio_resid(uio) && (uio_resid(uio) <= max_len))) { |
| 14346 | #endif |
| 14347 | #else |
| 14348 | (uio->uio_resid && (uio->uio_resid <= max_len))) { |
| 14349 | #endif |
| 14350 | sndout = 0; |
| 14351 | new_tail = NULL; |
| 14352 | if (hold_tcblock) { |
| 14353 | SCTP_TCB_UNLOCK(stcb); |
| 14354 | hold_tcblock = 0; |
| 14355 | } |
| 14356 | #if defined(__APPLE__) |
| 14357 | SCTP_SOCKET_UNLOCK(so, 0); |
| 14358 | #endif |
| 14359 | #if (defined(__FreeBSD__) && __FreeBSD_version > 602000) || defined(__Userspace__) |
| 14360 | mm = sctp_copy_resume(uio, (int)max_len, user_marks_eor, &error, &sndout, &new_tail); |
| 14361 | #else |
| 14362 | mm = sctp_copy_resume(uio, (int)max_len, &error, &sndout, &new_tail); |
| 14363 | #endif |
| 14364 | #if defined(__APPLE__) |
| 14365 | SCTP_SOCKET_LOCK(so, 0); |
| 14366 | #endif |
| 14367 | if ((mm == NULL) || error) { |
| 14368 | if (mm) { |
| 14369 | sctp_m_freem(mm); |
| 14370 | } |
| 14371 | goto out; |
| 14372 | } |
| 14373 | /* Update the mbuf and count */ |
| 14374 | SCTP_TCB_SEND_LOCK(stcb); |
| 14375 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 14376 | /* we need to get out. |
| 14377 | * Peer probably aborted. |
| 14378 | */ |
| 14379 | sctp_m_freem(mm); |
| 14380 | if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) { |
| 14381 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); |
| 14382 | error = ECONNRESET; |
| 14383 | } |
| 14384 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14385 | goto out; |
| 14386 | } |
| 14387 | if (sp->tail_mbuf) { |
| 14388 | /* tack it to the end */ |
| 14389 | SCTP_BUF_NEXT(sp->tail_mbuf) = mm; |
| 14390 | sp->tail_mbuf = new_tail; |
| 14391 | } else { |
| 14392 | /* A stolen mbuf */ |
| 14393 | sp->data = mm; |
| 14394 | sp->tail_mbuf = new_tail; |
| 14395 | } |
| 14396 | sctp_snd_sb_alloc(stcb, sndout); |
| 14397 | atomic_add_int(&sp->length, sndout); |
| 14398 | len += sndout; |
| 14399 | if (sinfo_flags & SCTP_SACK_IMMEDIATELY) { |
| 14400 | sp->sinfo_flags |= SCTP_SACK_IMMEDIATELY; |
| 14401 | } |
| 14402 |
| 14403 | /* Did we reach EOR? */ |
| 14404 | #if defined(__APPLE__) |
| 14405 | #if defined(APPLE_LEOPARD) |
| 14406 | if ((uio->uio_resid == 0) && |
| 14407 | #else |
| 14408 | if ((uio_resid(uio) == 0) && |
| 14409 | #endif |
| 14410 | #else |
| 14411 | if ((uio->uio_resid == 0) && |
| 14412 | #endif |
| 14413 | ((user_marks_eor == 0) || |
| 14414 | (sinfo_flags & SCTP_EOF) || |
| 14415 | (user_marks_eor && (sinfo_flags & SCTP_EOR)))) { |
| 14416 | sp->msg_is_complete = 1; |
| 14417 | } else { |
| 14418 | sp->msg_is_complete = 0; |
| 14419 | } |
| 14420 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14421 | } |
| 14422 | #if defined(__APPLE__) |
| 14423 | #if defined(APPLE_LEOPARD) |
| 14424 | if (uio->uio_resid == 0) { |
| 14425 | #else |
| 14426 | if (uio_resid(uio) == 0) { |
| 14427 | #endif |
| 14428 | #else |
| 14429 | if (uio->uio_resid == 0) { |
| 14430 | #endif |
| 14431 | /* got it all? */ |
| 14432 | continue; |
| 14433 | } |
| 14434 | /* PR-SCTP? */ |
| 14435 | if ((asoc->prsctp_supported) && (asoc->sent_queue_cnt_removeable > 0)) { |
| 14436 | /* This is ugly but we must assure locking order */ |
| 14437 | if (hold_tcblock == 0) { |
| 14438 | SCTP_TCB_LOCK(stcb); |
| 14439 | hold_tcblock = 1; |
| 14440 | } |
| 14441 | sctp_prune_prsctp(stcb, asoc, srcv, (int)sndlen); |
| 14442 | inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 14443 | if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) |
| 14444 | max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes; |
| 14445 | else |
| 14446 | max_len = 0; |
| 14447 | if (max_len > 0) { |
| 14448 | continue; |
| 14449 | } |
| 14450 | SCTP_TCB_UNLOCK(stcb); |
| 14451 | hold_tcblock = 0; |
| 14452 | } |
| 14453 | /* wait for space now */ |
| 14454 | if (non_blocking) { |
| 14455 | /* Non-blocking io in place out */ |
| 14456 | goto skip_out_eof; |
| 14457 | } |
| 14458 | /* What about the INIT, send it maybe */ |
| 14459 | if (queue_only_for_init) { |
| 14460 | if (hold_tcblock == 0) { |
| 14461 | SCTP_TCB_LOCK(stcb); |
| 14462 | hold_tcblock = 1; |
| 14463 | } |
| 14464 | if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) { |
| 14465 | /* a collision took us forward? */ |
| 14466 | queue_only = 0; |
| 14467 | } else { |
| 14468 | sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); |
| 14469 | SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT); |
| 14470 | queue_only = 1; |
| 14471 | } |
| 14472 | } |
| 14473 | if ((net->flight_size > net->cwnd) && |
| 14474 | (asoc->sctp_cmt_on_off == 0)) { |
| 14475 | SCTP_STAT_INCR(sctps_send_cwnd_avoid); |
| 14476 | queue_only = 1; |
| 14477 | } else if (asoc->ifp_had_enobuf) { |
| 14478 | SCTP_STAT_INCR(sctps_ifnomemqueued); |
| 14479 | if (net->flight_size > (2 * net->mtu)) { |
| 14480 | queue_only = 1; |
| 14481 | } |
| 14482 | asoc->ifp_had_enobuf = 0; |
| 14483 | } |
| 14484 | un_sent = stcb->asoc.total_output_queue_size - stcb->asoc.total_flight; |
| 14485 | if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) && |
| 14486 | (stcb->asoc.total_flight > 0) && |
| 14487 | (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) && |
| 14488 | (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) { |
| 14489 |
| 14490 | /*- |
| 14491 | * Ok, Nagle is set on and we have data outstanding. |
| 14492 | * Don't send anything and let SACKs drive out the |
| 14493 | * data unless we have a "full" segment to send. |
| 14494 | */ |
| 14495 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) { |
| 14496 | sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED); |
| 14497 | } |
| 14498 | SCTP_STAT_INCR(sctps_naglequeued); |
| 14499 | nagle_applies = 1; |
| 14500 | } else { |
| 14501 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) { |
| 14502 | if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) |
| 14503 | sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED); |
| 14504 | } |
| 14505 | SCTP_STAT_INCR(sctps_naglesent); |
| 14506 | nagle_applies = 0; |
| 14507 | } |
| 14508 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { |
| 14509 |
| 14510 | sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only, |
| 14511 | nagle_applies, un_sent); |
| 14512 | sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size, |
| 14513 | stcb->asoc.total_flight, |
| 14514 | stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count); |
| 14515 | } |
| 14516 | if (queue_only_for_init) |
| 14517 | queue_only_for_init = 0; |
| 14518 | if ((queue_only == 0) && (nagle_applies == 0)) { |
| 14519 | /*- |
| 14520 | * need to start chunk output |
| 14521 | * before blocking.. note that if |
| 14522 | * a lock is already applied, then |
| 14523 | * the input via the net is happening |
| 14524 | * and I don't need to start output :-D |
| 14525 | */ |
| 14526 | if (hold_tcblock == 0) { |
| 14527 | if (SCTP_TCB_TRYLOCK(stcb)) { |
| 14528 | hold_tcblock = 1; |
| 14529 | sctp_chunk_output(inp, |
| 14530 | stcb, |
| 14531 | SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); |
| 14532 | } |
| 14533 | } else { |
| 14534 | sctp_chunk_output(inp, |
| 14535 | stcb, |
| 14536 | SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); |
| 14537 | } |
| 14538 | } |
| 14539 | if (hold_tcblock == 1) { |
| 14540 | SCTP_TCB_UNLOCK(stcb); |
| 14541 | hold_tcblock = 0; |
| 14542 | } |
| 14543 | SOCKBUF_LOCK(&so->so_snd); |
| 14544 | /*- |
| 14545 | * This is a bit strange, but I think it will |
| 14546 | * work. The total_output_queue_size is locked and |
| 14547 | * protected by the TCB_LOCK, which we just released. |
| 14548 | * There is a race that can occur between releasing it |
| 14549 | * above, and me getting the socket lock, where sacks |
| 14550 | * come in but we have not put the SB_WAIT on the |
| 14551 | * so_snd buffer to get the wakeup. After the LOCK |
| 14552 | * is applied the sack_processing will also need to |
| 14553 | * LOCK the so->so_snd to do the actual sowwakeup(). So |
| 14554 | * once we have the socket buffer lock if we recheck the |
| 14555 | * size we KNOW we will get to sleep safely with the |
| 14556 | * wakeup flag in place. |
| 14557 | */ |
| 14558 | inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 14559 | if (SCTP_SB_LIMIT_SND(so) <= (inqueue_bytes + |
| 14560 | min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) { |
| 14561 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { |
| 14562 | #if defined(__APPLE__) |
| 14563 | #if defined(APPLE_LEOPARD) |
| 14564 | sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK, |
| 14565 | asoc, uio->uio_resid); |
| 14566 | #else |
| 14567 | sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK, |
| 14568 | asoc, uio_resid(uio)); |
| 14569 | #endif |
| 14570 | #else |
| 14571 | sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK, |
| 14572 | asoc, uio->uio_resid); |
| 14573 | #endif |
| 14574 | } |
| 14575 | be.error = 0; |
| 14576 | #if !defined(__Panda__) && !defined(__Windows__) |
| 14577 | stcb->block_entry = &be; |
| 14578 | #endif |
| 14579 | #if defined(__APPLE__) |
| 14580 | sbunlock(&so->so_snd, 1); |
| 14581 | #endif |
| 14582 | error = sbwait(&so->so_snd); |
| 14583 | stcb->block_entry = NULL; |
| 14584 |
| 14585 | if (error || so->so_error || be.error) { |
| 14586 | if (error == 0) { |
| 14587 | if (so->so_error) |
| 14588 | error = so->so_error; |
| 14589 | if (be.error) { |
| 14590 | error = be.error; |
| 14591 | } |
| 14592 | } |
| 14593 | SOCKBUF_UNLOCK(&so->so_snd); |
| 14594 | goto out_unlocked; |
| 14595 | } |
| 14596 |
| 14597 | #if defined(__APPLE__) |
| 14598 | error = sblock(&so->so_snd, SBLOCKWAIT(flags)); |
| 14599 | #endif |
| 14600 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { |
| 14601 | sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK, |
| 14602 | asoc, stcb->asoc.total_output_queue_size); |
| 14603 | } |
| 14604 | } |
| 14605 | SOCKBUF_UNLOCK(&so->so_snd); |
| 14606 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 14607 | goto out_unlocked; |
| 14608 | } |
| 14609 | } |
| 14610 | SCTP_TCB_SEND_LOCK(stcb); |
| 14611 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 14612 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14613 | goto out_unlocked; |
| 14614 | } |
| 14615 | if (sp) { |
| 14616 | if (sp->msg_is_complete == 0) { |
| 14617 | strm->last_msg_incomplete = 1; |
| 14618 | if (stcb->asoc.idata_supported == 0) { |
| 14619 | asoc->stream_locked = 1; |
| 14620 | asoc->stream_locked_on = srcv->sinfo_stream; |
| 14621 | } |
| 14622 | } else { |
| 14623 | sp->sender_all_done = 1; |
| 14624 | strm->last_msg_incomplete = 0; |
| 14625 | asoc->stream_locked = 0; |
| 14626 | } |
| 14627 | } else { |
| 14628 | SCTP_PRINTF("Huh no sp TSNH?\n"); |
| 14629 | strm->last_msg_incomplete = 0; |
| 14630 | asoc->stream_locked = 0; |
| 14631 | } |
| 14632 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14633 | #if defined(__APPLE__) |
| 14634 | #if defined(APPLE_LEOPARD) |
| 14635 | if (uio->uio_resid == 0) { |
| 14636 | #else |
| 14637 | if (uio_resid(uio) == 0) { |
| 14638 | #endif |
| 14639 | #else |
| 14640 | if (uio->uio_resid == 0) { |
| 14641 | #endif |
| 14642 | got_all_of_the_send = 1; |
| 14643 | } |
| 14644 | } else { |
| 14645 | /* We send in a 0, since we do NOT have any locks */ |
| 14646 | error = sctp_msg_append(stcb, net, top, srcv, 0); |
| 14647 | top = NULL; |
| 14648 | if (sinfo_flags & SCTP_EOF) { |
| 14649 | /* |
| 14650 | * This should only happen for Panda for the mbuf |
| 14651 | * send case, which does NOT yet support EEOR mode. |
| 14652 | * Thus, we can just set this flag to do the proper |
| 14653 | * EOF handling. |
| 14654 | */ |
| 14655 | got_all_of_the_send = 1; |
| 14656 | } |
| 14657 | } |
| 14658 | if (error) { |
| 14659 | goto out; |
| 14660 | } |
| 14661 | dataless_eof: |
| 14662 | /* EOF thing ? */ |
| 14663 | if ((sinfo_flags & SCTP_EOF) && |
| 14664 | (got_all_of_the_send == 1)) { |
| 14665 | SCTP_STAT_INCR(sctps_sends_with_eof); |
| 14666 | error = 0; |
| 14667 | if (hold_tcblock == 0) { |
| 14668 | SCTP_TCB_LOCK(stcb); |
| 14669 | hold_tcblock = 1; |
| 14670 | } |
| 14671 | if (TAILQ_EMPTY(&asoc->send_queue) && |
| 14672 | TAILQ_EMPTY(&asoc->sent_queue) && |
| 14673 | sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED) == 0) { |
| 14674 | if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) { |
| 14675 | goto abort_anyway; |
| 14676 | } |
| 14677 | /* there is nothing queued to send, so I'm done... */ |
| 14678 | if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) && |
| 14679 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) && |
| 14680 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { |
| 14681 | struct sctp_nets *netp; |
| 14682 |
| 14683 | /* only send SHUTDOWN the first time through */ |
| 14684 | if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) { |
| 14685 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
| 14686 | } |
| 14687 | SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT); |
| 14688 | sctp_stop_timers_for_shutdown(stcb); |
| 14689 | if (stcb->asoc.alternate) { |
| 14690 | netp = stcb->asoc.alternate; |
| 14691 | } else { |
| 14692 | netp = stcb->asoc.primary_destination; |
| 14693 | } |
| 14694 | sctp_send_shutdown(stcb, netp); |
| 14695 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, |
| 14696 | netp); |
| 14697 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, |
| 14698 | asoc->primary_destination); |
| 14699 | } |
| 14700 | } else { |
| 14701 | /*- |
| 14702 | * we still got (or just got) data to send, so set |
| 14703 | * SHUTDOWN_PENDING |
| 14704 | */ |
| 14705 | /*- |
| 14706 | * XXX sockets draft says that SCTP_EOF should be |
| 14707 | * sent with no data. currently, we will allow user |
| 14708 | * data to be sent first and move to |
| 14709 | * SHUTDOWN-PENDING |
| 14710 | */ |
| 14711 | if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) && |
| 14712 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) && |
| 14713 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { |
| 14714 | if (hold_tcblock == 0) { |
| 14715 | SCTP_TCB_LOCK(stcb); |
| 14716 | hold_tcblock = 1; |
| 14717 | } |
| 14718 | if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) { |
| 14719 | SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT); |
| 14720 | } |
| 14721 | SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING); |
| 14722 | if (TAILQ_EMPTY(&asoc->send_queue) && |
| 14723 | TAILQ_EMPTY(&asoc->sent_queue) && |
| 14724 | (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) { |
| 14725 | struct mbuf *op_err; |
| 14726 | char msg[SCTP_DIAG_INFO_LEN]; |
| 14727 |
| 14728 | abort_anyway: |
| 14729 | if (free_cnt_applied) { |
| 14730 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 14731 | free_cnt_applied = 0; |
| 14732 | } |
| 14733 | snprintf(msg, sizeof(msg), |
| 14734 | "%s:%d at %s", __FILE__, __LINE__, __func__); |
| 14735 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 14736 | msg); |
| 14737 | sctp_abort_an_association(stcb->sctp_ep, stcb, |
| 14738 | op_err, SCTP_SO_LOCKED); |
| 14739 | /* now relock the stcb so everything is sane */ |
| 14740 | hold_tcblock = 0; |
| 14741 | stcb = NULL; |
| 14742 | goto out; |
| 14743 | } |
| 14744 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, |
| 14745 | asoc->primary_destination); |
| 14746 | sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY); |
| 14747 | } |
| 14748 | } |
| 14749 | } |
| 14750 | skip_out_eof: |
| 14751 | if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { |
| 14752 | some_on_control = 1; |
| 14753 | } |
| 14754 | if (queue_only_for_init) { |
| 14755 | if (hold_tcblock == 0) { |
| 14756 | SCTP_TCB_LOCK(stcb); |
| 14757 | hold_tcblock = 1; |
| 14758 | } |
| 14759 | if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) { |
| 14760 | /* a collision took us forward? */ |
| 14761 | queue_only = 0; |
| 14762 | } else { |
| 14763 | sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); |
| 14764 | SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT); |
| 14765 | queue_only = 1; |
| 14766 | } |
| 14767 | } |
| 14768 | if ((net->flight_size > net->cwnd) && |
| 14769 | (stcb->asoc.sctp_cmt_on_off == 0)) { |
| 14770 | SCTP_STAT_INCR(sctps_send_cwnd_avoid); |
| 14771 | queue_only = 1; |
| 14772 | } else if (asoc->ifp_had_enobuf) { |
| 14773 | SCTP_STAT_INCR(sctps_ifnomemqueued); |
| 14774 | if (net->flight_size > (2 * net->mtu)) { |
| 14775 | queue_only = 1; |
| 14776 | } |
| 14777 | asoc->ifp_had_enobuf = 0; |
| 14778 | } |
| 14779 | un_sent = stcb->asoc.total_output_queue_size - stcb->asoc.total_flight; |
| 14780 | if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) && |
| 14781 | (stcb->asoc.total_flight > 0) && |
| 14782 | (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) && |
| 14783 | (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) { |
| 14784 | /*- |
| 14785 | * Ok, Nagle is set on and we have data outstanding. |
| 14786 | * Don't send anything and let SACKs drive out the |
| 14787 | * data unless wen have a "full" segment to send. |
| 14788 | */ |
| 14789 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) { |
| 14790 | sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED); |
| 14791 | } |
| 14792 | SCTP_STAT_INCR(sctps_naglequeued); |
| 14793 | nagle_applies = 1; |
| 14794 | } else { |
| 14795 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) { |
| 14796 | if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) |
| 14797 | sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED); |
| 14798 | } |
| 14799 | SCTP_STAT_INCR(sctps_naglesent); |
| 14800 | nagle_applies = 0; |
| 14801 | } |
| 14802 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { |
| 14803 | sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only, |
| 14804 | nagle_applies, un_sent); |
| 14805 | sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size, |
| 14806 | stcb->asoc.total_flight, |
| 14807 | stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count); |
| 14808 | } |
| 14809 | if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) { |
| 14810 | /* we can attempt to send too. */ |
| 14811 | if (hold_tcblock == 0) { |
| 14812 | /* If there is activity recv'ing sacks no need to send */ |
| 14813 | if (SCTP_TCB_TRYLOCK(stcb)) { |
| 14814 | sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); |
| 14815 | hold_tcblock = 1; |
| 14816 | } |
| 14817 | } else { |
| 14818 | sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); |
| 14819 | } |
| 14820 | } else if ((queue_only == 0) && |
| 14821 | (stcb->asoc.peers_rwnd == 0) && |
| 14822 | (stcb->asoc.total_flight == 0)) { |
| 14823 | /* We get to have a probe outstanding */ |
| 14824 | if (hold_tcblock == 0) { |
| 14825 | hold_tcblock = 1; |
| 14826 | SCTP_TCB_LOCK(stcb); |
| 14827 | } |
| 14828 | sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); |
| 14829 | } else if (some_on_control) { |
| 14830 | int num_out, reason, frag_point; |
| 14831 |
| 14832 | /* Here we do control only */ |
| 14833 | if (hold_tcblock == 0) { |
| 14834 | hold_tcblock = 1; |
| 14835 | SCTP_TCB_LOCK(stcb); |
| 14836 | } |
| 14837 | frag_point = sctp_get_frag_point(stcb, &stcb->asoc); |
| 14838 | (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out, |
| 14839 | &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED); |
| 14840 | } |
| 14841 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n", |
| 14842 | queue_only, stcb->asoc.peers_rwnd, un_sent, |
| 14843 | stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue, |
| 14844 | stcb->asoc.total_output_queue_size, error); |
| 14845 |
| 14846 | out: |
| 14847 | #if defined(__APPLE__) |
| 14848 | sbunlock(&so->so_snd, 1); |
| 14849 | #endif |
| 14850 | out_unlocked: |
| 14851 |
| 14852 | if (local_soresv && stcb) { |
| 14853 | atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen); |
| 14854 | } |
| 14855 | if (create_lock_applied) { |
| 14856 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 14857 | } |
| 14858 | if ((stcb) && hold_tcblock) { |
| 14859 | SCTP_TCB_UNLOCK(stcb); |
| 14860 | } |
| 14861 | if (stcb && free_cnt_applied) { |
| 14862 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 14863 | } |
| 14864 | #ifdef INVARIANTS |
| 14865 | #if defined(__FreeBSD__) |
| 14866 | if (stcb) { |
| 14867 | if (mtx_owned(&stcb->tcb_mtx)) { |
| 14868 | panic("Leaving with tcb mtx owned?"); |
| 14869 | } |
| 14870 | if (mtx_owned(&stcb->tcb_send_mtx)) { |
| 14871 | panic("Leaving with tcb send mtx owned?"); |
| 14872 | } |
| 14873 | } |
| 14874 | #endif |
| 14875 | #endif |
| 14876 | #ifdef __Panda__ |
| 14877 | /* |
| 14878 | * Handle the EAGAIN/ENOMEM cases to reattach the pak header |
| 14879 | * to particle when pak is passed in, so that caller |
| 14880 | * can try again with this pak |
| 14881 | * |
| 14882 | * NOTE: For other cases, including success case, |
| 14883 | * we simply want to return the header back to free |
| 14884 | * pool |
| 14885 | */ |
| 14886 | if (top) { |
| 14887 | if ((error == EAGAIN) || (error == ENOMEM)) { |
| 14888 | SCTP_ATTACH_CHAIN(i_pak, top, sndlen); |
| 14889 | top = NULL; |
| 14890 | } else { |
| 14891 | (void)SCTP_RELEASE_HEADER(i_pak); |
| 14892 | } |
| 14893 | } else { |
| 14894 | /* This is to handle cases when top has |
| 14895 | * been reset to NULL but pak might not |
| 14896 | * be freed |
| 14897 | */ |
| 14898 | if (i_pak) { |
| 14899 | (void)SCTP_RELEASE_HEADER(i_pak); |
| 14900 | } |
| 14901 | } |
| 14902 | #endif |
| 14903 | if (top) { |
| 14904 | sctp_m_freem(top); |
| 14905 | } |
| 14906 | if (control) { |
| 14907 | sctp_m_freem(control); |
| 14908 | } |
| 14909 | return (error); |
| 14910 | } |
| 14911 |
| 14912 |
| 14913 | /* |
| 14914 | * generate an AUTHentication chunk, if required |
| 14915 | */ |
| 14916 | struct mbuf * |
| 14917 | sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end, |
| 14918 | struct sctp_auth_chunk **auth_ret, uint32_t * offset, |
| 14919 | struct sctp_tcb *stcb, uint8_t chunk) |
| 14920 | { |
| 14921 | struct mbuf *m_auth; |
| 14922 | struct sctp_auth_chunk *auth; |
| 14923 | int chunk_len; |
| 14924 | struct mbuf *cn; |
| 14925 |
| 14926 | if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) || |
| 14927 | (stcb == NULL)) |
| 14928 | return (m); |
| 14929 |
| 14930 | if (stcb->asoc.auth_supported == 0) { |
| 14931 | return (m); |
| 14932 | } |
| 14933 | /* does the requested chunk require auth? */ |
| 14934 | if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) { |
| 14935 | return (m); |
| 14936 | } |
| 14937 | m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_NOWAIT, 1, MT_HEADER); |
| 14938 | if (m_auth == NULL) { |
| 14939 | /* no mbuf's */ |
| 14940 | return (m); |
| 14941 | } |
| 14942 | /* reserve some space if this will be the first mbuf */ |
| 14943 | if (m == NULL) |
| 14944 | SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD); |
| 14945 | /* fill in the AUTH chunk details */ |
| 14946 | auth = mtod(m_auth, struct sctp_auth_chunk *); |
| 14947 | memset(auth, 0, sizeof(*auth)); |
| 14948 | auth->ch.chunk_type = SCTP_AUTHENTICATION; |
| 14949 | auth->ch.chunk_flags = 0; |
| 14950 | chunk_len = sizeof(*auth) + |
| 14951 | sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id); |
| 14952 | auth->ch.chunk_length = htons(chunk_len); |
| 14953 | auth->hmac_id = htons(stcb->asoc.peer_hmac_id); |
| 14954 | /* key id and hmac digest will be computed and filled in upon send */ |
| 14955 |
| 14956 | /* save the offset where the auth was inserted into the chain */ |
| 14957 | *offset = 0; |
| 14958 | for (cn = m; cn; cn = SCTP_BUF_NEXT(cn)) { |
| 14959 | *offset += SCTP_BUF_LEN(cn); |
| 14960 | } |
| 14961 |
| 14962 | /* update length and return pointer to the auth chunk */ |
| 14963 | SCTP_BUF_LEN(m_auth) = chunk_len; |
| 14964 | m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0); |
| 14965 | if (auth_ret != NULL) |
| 14966 | *auth_ret = auth; |
| 14967 |
| 14968 | return (m); |
| 14969 | } |
| 14970 |
| 14971 | #if defined(__FreeBSD__) || defined(__APPLE__) |
| 14972 | #ifdef INET6 |
| 14973 | int |
| 14974 | sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t *ro) |
| 14975 | { |
| 14976 | struct nd_prefix *pfx = NULL; |
| 14977 | struct nd_pfxrouter *pfxrtr = NULL; |
| 14978 | struct sockaddr_in6 gw6; |
| 14979 |
| 14980 | if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6) |
| 14981 | return (0); |
| 14982 |
| 14983 | /* get prefix entry of address */ |
| 14984 | #if defined(__FreeBSD__) |
| 14985 | ND6_RLOCK(); |
| 14986 | #endif |
| 14987 | LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) { |
| 14988 | if (pfx->ndpr_stateflags & NDPRF_DETACHED) |
| 14989 | continue; |
| 14990 | if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr, |
| 14991 | &src6->sin6_addr, &pfx->ndpr_mask)) |
| 14992 | break; |
| 14993 | } |
| 14994 | /* no prefix entry in the prefix list */ |
| 14995 | if (pfx == NULL) { |
| 14996 | #if defined(__FreeBSD__) |
| 14997 | ND6_RUNLOCK(); |
| 14998 | #endif |
| 14999 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for "); |
| 15000 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6); |
| 15001 | return (0); |
| 15002 | } |
| 15003 |
| 15004 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is "); |
| 15005 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6); |
| 15006 |
| 15007 | /* search installed gateway from prefix entry */ |
| 15008 | LIST_FOREACH(pfxrtr, &pfx->ndpr_advrtrs, pfr_entry) { |
| 15009 | memset(&gw6, 0, sizeof(struct sockaddr_in6)); |
| 15010 | gw6.sin6_family = AF_INET6; |
| 15011 | #ifdef HAVE_SIN6_LEN |
| 15012 | gw6.sin6_len = sizeof(struct sockaddr_in6); |
| 15013 | #endif |
| 15014 | memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr, |
| 15015 | sizeof(struct in6_addr)); |
| 15016 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is "); |
| 15017 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6); |
| 15018 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is "); |
| 15019 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway); |
| 15020 | if (sctp_cmpaddr((struct sockaddr *)&gw6, ro->ro_rt->rt_gateway)) { |
| 15021 | #if defined(__FreeBSD__) |
| 15022 | ND6_RUNLOCK(); |
| 15023 | #endif |
| 15024 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n"); |
| 15025 | return (1); |
| 15026 | } |
| 15027 | } |
| 15028 | #if defined(__FreeBSD__) |
| 15029 | ND6_RUNLOCK(); |
| 15030 | #endif |
| 15031 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n"); |
| 15032 | return (0); |
| 15033 | } |
| 15034 | #endif |
| 15035 |
| 15036 | int |
| 15037 | sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t *ro) |
| 15038 | { |
| 15039 | #ifdef INET |
| 15040 | struct sockaddr_in *sin, *mask; |
| 15041 | struct ifaddr *ifa; |
| 15042 | struct in_addr srcnetaddr, gwnetaddr; |
| 15043 |
| 15044 | if (ro == NULL || ro->ro_rt == NULL || |
| 15045 | sifa->address.sa.sa_family != AF_INET) { |
| 15046 | return (0); |
| 15047 | } |
| 15048 | ifa = (struct ifaddr *)sifa->ifa; |
| 15049 | mask = (struct sockaddr_in *)(ifa->ifa_netmask); |
| 15050 | sin = &sifa->address.sin; |
| 15051 | srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr); |
| 15052 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is "); |
| 15053 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa); |
| 15054 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr); |
| 15055 |
| 15056 | sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway; |
| 15057 | gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr); |
| 15058 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is "); |
| 15059 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway); |
| 15060 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr); |
| 15061 | if (srcnetaddr.s_addr == gwnetaddr.s_addr) { |
| 15062 | return (1); |
| 15063 | } |
| 15064 | #endif |
| 15065 | return (0); |
| 15066 | } |
| 15067 | #elif defined(__Userspace__) |
| 15068 | /* TODO __Userspace__ versions of sctp_vXsrc_match_nexthop(). */ |
| 15069 | int |
| 15070 | sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t *ro) |
| 15071 | { |
| 15072 | return (0); |
| 15073 | } |
| 15074 | int |
| 15075 | sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t *ro) |
| 15076 | { |
| 15077 | return (0); |
| 15078 | } |
| 15079 |
| 15080 | #endif |
| 15081 |