| 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 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 36 | #include <sys/cdefs.h> |
| 37 | __FBSDID("$FreeBSD$"); |
| 38 | #endif |
| 39 |
| 40 | #include <netinet/sctp_os.h> |
| 41 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 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__) && !defined(__Userspace__) |
| 60 | #include <netinet/sctp_kdtrace.h> |
| 61 | #endif |
| 62 | #if defined(__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(_WIN32) |
| 67 | #include <netinet/udp.h> |
| 68 | #endif |
| 69 | #endif |
| 70 | #if !defined(__Userspace__) |
| 71 | #if defined(__APPLE__) |
| 72 | #include <netinet/in.h> |
| 73 | #endif |
| 74 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 75 | #include <netinet/udp_var.h> |
| 76 | #include <machine/in_cksum.h> |
| 77 | #endif |
| 78 | #endif |
| 79 | #if defined(__Userspace__) && defined(INET6) |
| 80 | #include <netinet6/sctp6_var.h> |
| 81 | #endif |
| 82 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 83 | #if !(defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)) |
| 84 | #define SCTP_MAX_LINKHDR 16 |
| 85 | #endif |
| 86 | #endif |
| 87 |
| 88 | #define SCTP_MAX_GAPS_INARRAY 4 |
| 89 | struct sack_track { |
| 90 | uint8_t right_edge; /* mergable on the right edge */ |
| 91 | uint8_t left_edge; /* mergable on the left edge */ |
| 92 | uint8_t num_entries; |
| 93 | uint8_t spare; |
| 94 | struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY]; |
| 95 | }; |
| 96 |
| 97 | const struct sack_track sack_array[256] = { |
| 98 | {0, 0, 0, 0, /* 0x00 */ |
| 99 | {{0, 0}, |
| 100 | {0, 0}, |
| 101 | {0, 0}, |
| 102 | {0, 0} |
| 103 | } |
| 104 | }, |
| 105 | {1, 0, 1, 0, /* 0x01 */ |
| 106 | {{0, 0}, |
| 107 | {0, 0}, |
| 108 | {0, 0}, |
| 109 | {0, 0} |
| 110 | } |
| 111 | }, |
| 112 | {0, 0, 1, 0, /* 0x02 */ |
| 113 | {{1, 1}, |
| 114 | {0, 0}, |
| 115 | {0, 0}, |
| 116 | {0, 0} |
| 117 | } |
| 118 | }, |
| 119 | {1, 0, 1, 0, /* 0x03 */ |
| 120 | {{0, 1}, |
| 121 | {0, 0}, |
| 122 | {0, 0}, |
| 123 | {0, 0} |
| 124 | } |
| 125 | }, |
| 126 | {0, 0, 1, 0, /* 0x04 */ |
| 127 | {{2, 2}, |
| 128 | {0, 0}, |
| 129 | {0, 0}, |
| 130 | {0, 0} |
| 131 | } |
| 132 | }, |
| 133 | {1, 0, 2, 0, /* 0x05 */ |
| 134 | {{0, 0}, |
| 135 | {2, 2}, |
| 136 | {0, 0}, |
| 137 | {0, 0} |
| 138 | } |
| 139 | }, |
| 140 | {0, 0, 1, 0, /* 0x06 */ |
| 141 | {{1, 2}, |
| 142 | {0, 0}, |
| 143 | {0, 0}, |
| 144 | {0, 0} |
| 145 | } |
| 146 | }, |
| 147 | {1, 0, 1, 0, /* 0x07 */ |
| 148 | {{0, 2}, |
| 149 | {0, 0}, |
| 150 | {0, 0}, |
| 151 | {0, 0} |
| 152 | } |
| 153 | }, |
| 154 | {0, 0, 1, 0, /* 0x08 */ |
| 155 | {{3, 3}, |
| 156 | {0, 0}, |
| 157 | {0, 0}, |
| 158 | {0, 0} |
| 159 | } |
| 160 | }, |
| 161 | {1, 0, 2, 0, /* 0x09 */ |
| 162 | {{0, 0}, |
| 163 | {3, 3}, |
| 164 | {0, 0}, |
| 165 | {0, 0} |
| 166 | } |
| 167 | }, |
| 168 | {0, 0, 2, 0, /* 0x0a */ |
| 169 | {{1, 1}, |
| 170 | {3, 3}, |
| 171 | {0, 0}, |
| 172 | {0, 0} |
| 173 | } |
| 174 | }, |
| 175 | {1, 0, 2, 0, /* 0x0b */ |
| 176 | {{0, 1}, |
| 177 | {3, 3}, |
| 178 | {0, 0}, |
| 179 | {0, 0} |
| 180 | } |
| 181 | }, |
| 182 | {0, 0, 1, 0, /* 0x0c */ |
| 183 | {{2, 3}, |
| 184 | {0, 0}, |
| 185 | {0, 0}, |
| 186 | {0, 0} |
| 187 | } |
| 188 | }, |
| 189 | {1, 0, 2, 0, /* 0x0d */ |
| 190 | {{0, 0}, |
| 191 | {2, 3}, |
| 192 | {0, 0}, |
| 193 | {0, 0} |
| 194 | } |
| 195 | }, |
| 196 | {0, 0, 1, 0, /* 0x0e */ |
| 197 | {{1, 3}, |
| 198 | {0, 0}, |
| 199 | {0, 0}, |
| 200 | {0, 0} |
| 201 | } |
| 202 | }, |
| 203 | {1, 0, 1, 0, /* 0x0f */ |
| 204 | {{0, 3}, |
| 205 | {0, 0}, |
| 206 | {0, 0}, |
| 207 | {0, 0} |
| 208 | } |
| 209 | }, |
| 210 | {0, 0, 1, 0, /* 0x10 */ |
| 211 | {{4, 4}, |
| 212 | {0, 0}, |
| 213 | {0, 0}, |
| 214 | {0, 0} |
| 215 | } |
| 216 | }, |
| 217 | {1, 0, 2, 0, /* 0x11 */ |
| 218 | {{0, 0}, |
| 219 | {4, 4}, |
| 220 | {0, 0}, |
| 221 | {0, 0} |
| 222 | } |
| 223 | }, |
| 224 | {0, 0, 2, 0, /* 0x12 */ |
| 225 | {{1, 1}, |
| 226 | {4, 4}, |
| 227 | {0, 0}, |
| 228 | {0, 0} |
| 229 | } |
| 230 | }, |
| 231 | {1, 0, 2, 0, /* 0x13 */ |
| 232 | {{0, 1}, |
| 233 | {4, 4}, |
| 234 | {0, 0}, |
| 235 | {0, 0} |
| 236 | } |
| 237 | }, |
| 238 | {0, 0, 2, 0, /* 0x14 */ |
| 239 | {{2, 2}, |
| 240 | {4, 4}, |
| 241 | {0, 0}, |
| 242 | {0, 0} |
| 243 | } |
| 244 | }, |
| 245 | {1, 0, 3, 0, /* 0x15 */ |
| 246 | {{0, 0}, |
| 247 | {2, 2}, |
| 248 | {4, 4}, |
| 249 | {0, 0} |
| 250 | } |
| 251 | }, |
| 252 | {0, 0, 2, 0, /* 0x16 */ |
| 253 | {{1, 2}, |
| 254 | {4, 4}, |
| 255 | {0, 0}, |
| 256 | {0, 0} |
| 257 | } |
| 258 | }, |
| 259 | {1, 0, 2, 0, /* 0x17 */ |
| 260 | {{0, 2}, |
| 261 | {4, 4}, |
| 262 | {0, 0}, |
| 263 | {0, 0} |
| 264 | } |
| 265 | }, |
| 266 | {0, 0, 1, 0, /* 0x18 */ |
| 267 | {{3, 4}, |
| 268 | {0, 0}, |
| 269 | {0, 0}, |
| 270 | {0, 0} |
| 271 | } |
| 272 | }, |
| 273 | {1, 0, 2, 0, /* 0x19 */ |
| 274 | {{0, 0}, |
| 275 | {3, 4}, |
| 276 | {0, 0}, |
| 277 | {0, 0} |
| 278 | } |
| 279 | }, |
| 280 | {0, 0, 2, 0, /* 0x1a */ |
| 281 | {{1, 1}, |
| 282 | {3, 4}, |
| 283 | {0, 0}, |
| 284 | {0, 0} |
| 285 | } |
| 286 | }, |
| 287 | {1, 0, 2, 0, /* 0x1b */ |
| 288 | {{0, 1}, |
| 289 | {3, 4}, |
| 290 | {0, 0}, |
| 291 | {0, 0} |
| 292 | } |
| 293 | }, |
| 294 | {0, 0, 1, 0, /* 0x1c */ |
| 295 | {{2, 4}, |
| 296 | {0, 0}, |
| 297 | {0, 0}, |
| 298 | {0, 0} |
| 299 | } |
| 300 | }, |
| 301 | {1, 0, 2, 0, /* 0x1d */ |
| 302 | {{0, 0}, |
| 303 | {2, 4}, |
| 304 | {0, 0}, |
| 305 | {0, 0} |
| 306 | } |
| 307 | }, |
| 308 | {0, 0, 1, 0, /* 0x1e */ |
| 309 | {{1, 4}, |
| 310 | {0, 0}, |
| 311 | {0, 0}, |
| 312 | {0, 0} |
| 313 | } |
| 314 | }, |
| 315 | {1, 0, 1, 0, /* 0x1f */ |
| 316 | {{0, 4}, |
| 317 | {0, 0}, |
| 318 | {0, 0}, |
| 319 | {0, 0} |
| 320 | } |
| 321 | }, |
| 322 | {0, 0, 1, 0, /* 0x20 */ |
| 323 | {{5, 5}, |
| 324 | {0, 0}, |
| 325 | {0, 0}, |
| 326 | {0, 0} |
| 327 | } |
| 328 | }, |
| 329 | {1, 0, 2, 0, /* 0x21 */ |
| 330 | {{0, 0}, |
| 331 | {5, 5}, |
| 332 | {0, 0}, |
| 333 | {0, 0} |
| 334 | } |
| 335 | }, |
| 336 | {0, 0, 2, 0, /* 0x22 */ |
| 337 | {{1, 1}, |
| 338 | {5, 5}, |
| 339 | {0, 0}, |
| 340 | {0, 0} |
| 341 | } |
| 342 | }, |
| 343 | {1, 0, 2, 0, /* 0x23 */ |
| 344 | {{0, 1}, |
| 345 | {5, 5}, |
| 346 | {0, 0}, |
| 347 | {0, 0} |
| 348 | } |
| 349 | }, |
| 350 | {0, 0, 2, 0, /* 0x24 */ |
| 351 | {{2, 2}, |
| 352 | {5, 5}, |
| 353 | {0, 0}, |
| 354 | {0, 0} |
| 355 | } |
| 356 | }, |
| 357 | {1, 0, 3, 0, /* 0x25 */ |
| 358 | {{0, 0}, |
| 359 | {2, 2}, |
| 360 | {5, 5}, |
| 361 | {0, 0} |
| 362 | } |
| 363 | }, |
| 364 | {0, 0, 2, 0, /* 0x26 */ |
| 365 | {{1, 2}, |
| 366 | {5, 5}, |
| 367 | {0, 0}, |
| 368 | {0, 0} |
| 369 | } |
| 370 | }, |
| 371 | {1, 0, 2, 0, /* 0x27 */ |
| 372 | {{0, 2}, |
| 373 | {5, 5}, |
| 374 | {0, 0}, |
| 375 | {0, 0} |
| 376 | } |
| 377 | }, |
| 378 | {0, 0, 2, 0, /* 0x28 */ |
| 379 | {{3, 3}, |
| 380 | {5, 5}, |
| 381 | {0, 0}, |
| 382 | {0, 0} |
| 383 | } |
| 384 | }, |
| 385 | {1, 0, 3, 0, /* 0x29 */ |
| 386 | {{0, 0}, |
| 387 | {3, 3}, |
| 388 | {5, 5}, |
| 389 | {0, 0} |
| 390 | } |
| 391 | }, |
| 392 | {0, 0, 3, 0, /* 0x2a */ |
| 393 | {{1, 1}, |
| 394 | {3, 3}, |
| 395 | {5, 5}, |
| 396 | {0, 0} |
| 397 | } |
| 398 | }, |
| 399 | {1, 0, 3, 0, /* 0x2b */ |
| 400 | {{0, 1}, |
| 401 | {3, 3}, |
| 402 | {5, 5}, |
| 403 | {0, 0} |
| 404 | } |
| 405 | }, |
| 406 | {0, 0, 2, 0, /* 0x2c */ |
| 407 | {{2, 3}, |
| 408 | {5, 5}, |
| 409 | {0, 0}, |
| 410 | {0, 0} |
| 411 | } |
| 412 | }, |
| 413 | {1, 0, 3, 0, /* 0x2d */ |
| 414 | {{0, 0}, |
| 415 | {2, 3}, |
| 416 | {5, 5}, |
| 417 | {0, 0} |
| 418 | } |
| 419 | }, |
| 420 | {0, 0, 2, 0, /* 0x2e */ |
| 421 | {{1, 3}, |
| 422 | {5, 5}, |
| 423 | {0, 0}, |
| 424 | {0, 0} |
| 425 | } |
| 426 | }, |
| 427 | {1, 0, 2, 0, /* 0x2f */ |
| 428 | {{0, 3}, |
| 429 | {5, 5}, |
| 430 | {0, 0}, |
| 431 | {0, 0} |
| 432 | } |
| 433 | }, |
| 434 | {0, 0, 1, 0, /* 0x30 */ |
| 435 | {{4, 5}, |
| 436 | {0, 0}, |
| 437 | {0, 0}, |
| 438 | {0, 0} |
| 439 | } |
| 440 | }, |
| 441 | {1, 0, 2, 0, /* 0x31 */ |
| 442 | {{0, 0}, |
| 443 | {4, 5}, |
| 444 | {0, 0}, |
| 445 | {0, 0} |
| 446 | } |
| 447 | }, |
| 448 | {0, 0, 2, 0, /* 0x32 */ |
| 449 | {{1, 1}, |
| 450 | {4, 5}, |
| 451 | {0, 0}, |
| 452 | {0, 0} |
| 453 | } |
| 454 | }, |
| 455 | {1, 0, 2, 0, /* 0x33 */ |
| 456 | {{0, 1}, |
| 457 | {4, 5}, |
| 458 | {0, 0}, |
| 459 | {0, 0} |
| 460 | } |
| 461 | }, |
| 462 | {0, 0, 2, 0, /* 0x34 */ |
| 463 | {{2, 2}, |
| 464 | {4, 5}, |
| 465 | {0, 0}, |
| 466 | {0, 0} |
| 467 | } |
| 468 | }, |
| 469 | {1, 0, 3, 0, /* 0x35 */ |
| 470 | {{0, 0}, |
| 471 | {2, 2}, |
| 472 | {4, 5}, |
| 473 | {0, 0} |
| 474 | } |
| 475 | }, |
| 476 | {0, 0, 2, 0, /* 0x36 */ |
| 477 | {{1, 2}, |
| 478 | {4, 5}, |
| 479 | {0, 0}, |
| 480 | {0, 0} |
| 481 | } |
| 482 | }, |
| 483 | {1, 0, 2, 0, /* 0x37 */ |
| 484 | {{0, 2}, |
| 485 | {4, 5}, |
| 486 | {0, 0}, |
| 487 | {0, 0} |
| 488 | } |
| 489 | }, |
| 490 | {0, 0, 1, 0, /* 0x38 */ |
| 491 | {{3, 5}, |
| 492 | {0, 0}, |
| 493 | {0, 0}, |
| 494 | {0, 0} |
| 495 | } |
| 496 | }, |
| 497 | {1, 0, 2, 0, /* 0x39 */ |
| 498 | {{0, 0}, |
| 499 | {3, 5}, |
| 500 | {0, 0}, |
| 501 | {0, 0} |
| 502 | } |
| 503 | }, |
| 504 | {0, 0, 2, 0, /* 0x3a */ |
| 505 | {{1, 1}, |
| 506 | {3, 5}, |
| 507 | {0, 0}, |
| 508 | {0, 0} |
| 509 | } |
| 510 | }, |
| 511 | {1, 0, 2, 0, /* 0x3b */ |
| 512 | {{0, 1}, |
| 513 | {3, 5}, |
| 514 | {0, 0}, |
| 515 | {0, 0} |
| 516 | } |
| 517 | }, |
| 518 | {0, 0, 1, 0, /* 0x3c */ |
| 519 | {{2, 5}, |
| 520 | {0, 0}, |
| 521 | {0, 0}, |
| 522 | {0, 0} |
| 523 | } |
| 524 | }, |
| 525 | {1, 0, 2, 0, /* 0x3d */ |
| 526 | {{0, 0}, |
| 527 | {2, 5}, |
| 528 | {0, 0}, |
| 529 | {0, 0} |
| 530 | } |
| 531 | }, |
| 532 | {0, 0, 1, 0, /* 0x3e */ |
| 533 | {{1, 5}, |
| 534 | {0, 0}, |
| 535 | {0, 0}, |
| 536 | {0, 0} |
| 537 | } |
| 538 | }, |
| 539 | {1, 0, 1, 0, /* 0x3f */ |
| 540 | {{0, 5}, |
| 541 | {0, 0}, |
| 542 | {0, 0}, |
| 543 | {0, 0} |
| 544 | } |
| 545 | }, |
| 546 | {0, 0, 1, 0, /* 0x40 */ |
| 547 | {{6, 6}, |
| 548 | {0, 0}, |
| 549 | {0, 0}, |
| 550 | {0, 0} |
| 551 | } |
| 552 | }, |
| 553 | {1, 0, 2, 0, /* 0x41 */ |
| 554 | {{0, 0}, |
| 555 | {6, 6}, |
| 556 | {0, 0}, |
| 557 | {0, 0} |
| 558 | } |
| 559 | }, |
| 560 | {0, 0, 2, 0, /* 0x42 */ |
| 561 | {{1, 1}, |
| 562 | {6, 6}, |
| 563 | {0, 0}, |
| 564 | {0, 0} |
| 565 | } |
| 566 | }, |
| 567 | {1, 0, 2, 0, /* 0x43 */ |
| 568 | {{0, 1}, |
| 569 | {6, 6}, |
| 570 | {0, 0}, |
| 571 | {0, 0} |
| 572 | } |
| 573 | }, |
| 574 | {0, 0, 2, 0, /* 0x44 */ |
| 575 | {{2, 2}, |
| 576 | {6, 6}, |
| 577 | {0, 0}, |
| 578 | {0, 0} |
| 579 | } |
| 580 | }, |
| 581 | {1, 0, 3, 0, /* 0x45 */ |
| 582 | {{0, 0}, |
| 583 | {2, 2}, |
| 584 | {6, 6}, |
| 585 | {0, 0} |
| 586 | } |
| 587 | }, |
| 588 | {0, 0, 2, 0, /* 0x46 */ |
| 589 | {{1, 2}, |
| 590 | {6, 6}, |
| 591 | {0, 0}, |
| 592 | {0, 0} |
| 593 | } |
| 594 | }, |
| 595 | {1, 0, 2, 0, /* 0x47 */ |
| 596 | {{0, 2}, |
| 597 | {6, 6}, |
| 598 | {0, 0}, |
| 599 | {0, 0} |
| 600 | } |
| 601 | }, |
| 602 | {0, 0, 2, 0, /* 0x48 */ |
| 603 | {{3, 3}, |
| 604 | {6, 6}, |
| 605 | {0, 0}, |
| 606 | {0, 0} |
| 607 | } |
| 608 | }, |
| 609 | {1, 0, 3, 0, /* 0x49 */ |
| 610 | {{0, 0}, |
| 611 | {3, 3}, |
| 612 | {6, 6}, |
| 613 | {0, 0} |
| 614 | } |
| 615 | }, |
| 616 | {0, 0, 3, 0, /* 0x4a */ |
| 617 | {{1, 1}, |
| 618 | {3, 3}, |
| 619 | {6, 6}, |
| 620 | {0, 0} |
| 621 | } |
| 622 | }, |
| 623 | {1, 0, 3, 0, /* 0x4b */ |
| 624 | {{0, 1}, |
| 625 | {3, 3}, |
| 626 | {6, 6}, |
| 627 | {0, 0} |
| 628 | } |
| 629 | }, |
| 630 | {0, 0, 2, 0, /* 0x4c */ |
| 631 | {{2, 3}, |
| 632 | {6, 6}, |
| 633 | {0, 0}, |
| 634 | {0, 0} |
| 635 | } |
| 636 | }, |
| 637 | {1, 0, 3, 0, /* 0x4d */ |
| 638 | {{0, 0}, |
| 639 | {2, 3}, |
| 640 | {6, 6}, |
| 641 | {0, 0} |
| 642 | } |
| 643 | }, |
| 644 | {0, 0, 2, 0, /* 0x4e */ |
| 645 | {{1, 3}, |
| 646 | {6, 6}, |
| 647 | {0, 0}, |
| 648 | {0, 0} |
| 649 | } |
| 650 | }, |
| 651 | {1, 0, 2, 0, /* 0x4f */ |
| 652 | {{0, 3}, |
| 653 | {6, 6}, |
| 654 | {0, 0}, |
| 655 | {0, 0} |
| 656 | } |
| 657 | }, |
| 658 | {0, 0, 2, 0, /* 0x50 */ |
| 659 | {{4, 4}, |
| 660 | {6, 6}, |
| 661 | {0, 0}, |
| 662 | {0, 0} |
| 663 | } |
| 664 | }, |
| 665 | {1, 0, 3, 0, /* 0x51 */ |
| 666 | {{0, 0}, |
| 667 | {4, 4}, |
| 668 | {6, 6}, |
| 669 | {0, 0} |
| 670 | } |
| 671 | }, |
| 672 | {0, 0, 3, 0, /* 0x52 */ |
| 673 | {{1, 1}, |
| 674 | {4, 4}, |
| 675 | {6, 6}, |
| 676 | {0, 0} |
| 677 | } |
| 678 | }, |
| 679 | {1, 0, 3, 0, /* 0x53 */ |
| 680 | {{0, 1}, |
| 681 | {4, 4}, |
| 682 | {6, 6}, |
| 683 | {0, 0} |
| 684 | } |
| 685 | }, |
| 686 | {0, 0, 3, 0, /* 0x54 */ |
| 687 | {{2, 2}, |
| 688 | {4, 4}, |
| 689 | {6, 6}, |
| 690 | {0, 0} |
| 691 | } |
| 692 | }, |
| 693 | {1, 0, 4, 0, /* 0x55 */ |
| 694 | {{0, 0}, |
| 695 | {2, 2}, |
| 696 | {4, 4}, |
| 697 | {6, 6} |
| 698 | } |
| 699 | }, |
| 700 | {0, 0, 3, 0, /* 0x56 */ |
| 701 | {{1, 2}, |
| 702 | {4, 4}, |
| 703 | {6, 6}, |
| 704 | {0, 0} |
| 705 | } |
| 706 | }, |
| 707 | {1, 0, 3, 0, /* 0x57 */ |
| 708 | {{0, 2}, |
| 709 | {4, 4}, |
| 710 | {6, 6}, |
| 711 | {0, 0} |
| 712 | } |
| 713 | }, |
| 714 | {0, 0, 2, 0, /* 0x58 */ |
| 715 | {{3, 4}, |
| 716 | {6, 6}, |
| 717 | {0, 0}, |
| 718 | {0, 0} |
| 719 | } |
| 720 | }, |
| 721 | {1, 0, 3, 0, /* 0x59 */ |
| 722 | {{0, 0}, |
| 723 | {3, 4}, |
| 724 | {6, 6}, |
| 725 | {0, 0} |
| 726 | } |
| 727 | }, |
| 728 | {0, 0, 3, 0, /* 0x5a */ |
| 729 | {{1, 1}, |
| 730 | {3, 4}, |
| 731 | {6, 6}, |
| 732 | {0, 0} |
| 733 | } |
| 734 | }, |
| 735 | {1, 0, 3, 0, /* 0x5b */ |
| 736 | {{0, 1}, |
| 737 | {3, 4}, |
| 738 | {6, 6}, |
| 739 | {0, 0} |
| 740 | } |
| 741 | }, |
| 742 | {0, 0, 2, 0, /* 0x5c */ |
| 743 | {{2, 4}, |
| 744 | {6, 6}, |
| 745 | {0, 0}, |
| 746 | {0, 0} |
| 747 | } |
| 748 | }, |
| 749 | {1, 0, 3, 0, /* 0x5d */ |
| 750 | {{0, 0}, |
| 751 | {2, 4}, |
| 752 | {6, 6}, |
| 753 | {0, 0} |
| 754 | } |
| 755 | }, |
| 756 | {0, 0, 2, 0, /* 0x5e */ |
| 757 | {{1, 4}, |
| 758 | {6, 6}, |
| 759 | {0, 0}, |
| 760 | {0, 0} |
| 761 | } |
| 762 | }, |
| 763 | {1, 0, 2, 0, /* 0x5f */ |
| 764 | {{0, 4}, |
| 765 | {6, 6}, |
| 766 | {0, 0}, |
| 767 | {0, 0} |
| 768 | } |
| 769 | }, |
| 770 | {0, 0, 1, 0, /* 0x60 */ |
| 771 | {{5, 6}, |
| 772 | {0, 0}, |
| 773 | {0, 0}, |
| 774 | {0, 0} |
| 775 | } |
| 776 | }, |
| 777 | {1, 0, 2, 0, /* 0x61 */ |
| 778 | {{0, 0}, |
| 779 | {5, 6}, |
| 780 | {0, 0}, |
| 781 | {0, 0} |
| 782 | } |
| 783 | }, |
| 784 | {0, 0, 2, 0, /* 0x62 */ |
| 785 | {{1, 1}, |
| 786 | {5, 6}, |
| 787 | {0, 0}, |
| 788 | {0, 0} |
| 789 | } |
| 790 | }, |
| 791 | {1, 0, 2, 0, /* 0x63 */ |
| 792 | {{0, 1}, |
| 793 | {5, 6}, |
| 794 | {0, 0}, |
| 795 | {0, 0} |
| 796 | } |
| 797 | }, |
| 798 | {0, 0, 2, 0, /* 0x64 */ |
| 799 | {{2, 2}, |
| 800 | {5, 6}, |
| 801 | {0, 0}, |
| 802 | {0, 0} |
| 803 | } |
| 804 | }, |
| 805 | {1, 0, 3, 0, /* 0x65 */ |
| 806 | {{0, 0}, |
| 807 | {2, 2}, |
| 808 | {5, 6}, |
| 809 | {0, 0} |
| 810 | } |
| 811 | }, |
| 812 | {0, 0, 2, 0, /* 0x66 */ |
| 813 | {{1, 2}, |
| 814 | {5, 6}, |
| 815 | {0, 0}, |
| 816 | {0, 0} |
| 817 | } |
| 818 | }, |
| 819 | {1, 0, 2, 0, /* 0x67 */ |
| 820 | {{0, 2}, |
| 821 | {5, 6}, |
| 822 | {0, 0}, |
| 823 | {0, 0} |
| 824 | } |
| 825 | }, |
| 826 | {0, 0, 2, 0, /* 0x68 */ |
| 827 | {{3, 3}, |
| 828 | {5, 6}, |
| 829 | {0, 0}, |
| 830 | {0, 0} |
| 831 | } |
| 832 | }, |
| 833 | {1, 0, 3, 0, /* 0x69 */ |
| 834 | {{0, 0}, |
| 835 | {3, 3}, |
| 836 | {5, 6}, |
| 837 | {0, 0} |
| 838 | } |
| 839 | }, |
| 840 | {0, 0, 3, 0, /* 0x6a */ |
| 841 | {{1, 1}, |
| 842 | {3, 3}, |
| 843 | {5, 6}, |
| 844 | {0, 0} |
| 845 | } |
| 846 | }, |
| 847 | {1, 0, 3, 0, /* 0x6b */ |
| 848 | {{0, 1}, |
| 849 | {3, 3}, |
| 850 | {5, 6}, |
| 851 | {0, 0} |
| 852 | } |
| 853 | }, |
| 854 | {0, 0, 2, 0, /* 0x6c */ |
| 855 | {{2, 3}, |
| 856 | {5, 6}, |
| 857 | {0, 0}, |
| 858 | {0, 0} |
| 859 | } |
| 860 | }, |
| 861 | {1, 0, 3, 0, /* 0x6d */ |
| 862 | {{0, 0}, |
| 863 | {2, 3}, |
| 864 | {5, 6}, |
| 865 | {0, 0} |
| 866 | } |
| 867 | }, |
| 868 | {0, 0, 2, 0, /* 0x6e */ |
| 869 | {{1, 3}, |
| 870 | {5, 6}, |
| 871 | {0, 0}, |
| 872 | {0, 0} |
| 873 | } |
| 874 | }, |
| 875 | {1, 0, 2, 0, /* 0x6f */ |
| 876 | {{0, 3}, |
| 877 | {5, 6}, |
| 878 | {0, 0}, |
| 879 | {0, 0} |
| 880 | } |
| 881 | }, |
| 882 | {0, 0, 1, 0, /* 0x70 */ |
| 883 | {{4, 6}, |
| 884 | {0, 0}, |
| 885 | {0, 0}, |
| 886 | {0, 0} |
| 887 | } |
| 888 | }, |
| 889 | {1, 0, 2, 0, /* 0x71 */ |
| 890 | {{0, 0}, |
| 891 | {4, 6}, |
| 892 | {0, 0}, |
| 893 | {0, 0} |
| 894 | } |
| 895 | }, |
| 896 | {0, 0, 2, 0, /* 0x72 */ |
| 897 | {{1, 1}, |
| 898 | {4, 6}, |
| 899 | {0, 0}, |
| 900 | {0, 0} |
| 901 | } |
| 902 | }, |
| 903 | {1, 0, 2, 0, /* 0x73 */ |
| 904 | {{0, 1}, |
| 905 | {4, 6}, |
| 906 | {0, 0}, |
| 907 | {0, 0} |
| 908 | } |
| 909 | }, |
| 910 | {0, 0, 2, 0, /* 0x74 */ |
| 911 | {{2, 2}, |
| 912 | {4, 6}, |
| 913 | {0, 0}, |
| 914 | {0, 0} |
| 915 | } |
| 916 | }, |
| 917 | {1, 0, 3, 0, /* 0x75 */ |
| 918 | {{0, 0}, |
| 919 | {2, 2}, |
| 920 | {4, 6}, |
| 921 | {0, 0} |
| 922 | } |
| 923 | }, |
| 924 | {0, 0, 2, 0, /* 0x76 */ |
| 925 | {{1, 2}, |
| 926 | {4, 6}, |
| 927 | {0, 0}, |
| 928 | {0, 0} |
| 929 | } |
| 930 | }, |
| 931 | {1, 0, 2, 0, /* 0x77 */ |
| 932 | {{0, 2}, |
| 933 | {4, 6}, |
| 934 | {0, 0}, |
| 935 | {0, 0} |
| 936 | } |
| 937 | }, |
| 938 | {0, 0, 1, 0, /* 0x78 */ |
| 939 | {{3, 6}, |
| 940 | {0, 0}, |
| 941 | {0, 0}, |
| 942 | {0, 0} |
| 943 | } |
| 944 | }, |
| 945 | {1, 0, 2, 0, /* 0x79 */ |
| 946 | {{0, 0}, |
| 947 | {3, 6}, |
| 948 | {0, 0}, |
| 949 | {0, 0} |
| 950 | } |
| 951 | }, |
| 952 | {0, 0, 2, 0, /* 0x7a */ |
| 953 | {{1, 1}, |
| 954 | {3, 6}, |
| 955 | {0, 0}, |
| 956 | {0, 0} |
| 957 | } |
| 958 | }, |
| 959 | {1, 0, 2, 0, /* 0x7b */ |
| 960 | {{0, 1}, |
| 961 | {3, 6}, |
| 962 | {0, 0}, |
| 963 | {0, 0} |
| 964 | } |
| 965 | }, |
| 966 | {0, 0, 1, 0, /* 0x7c */ |
| 967 | {{2, 6}, |
| 968 | {0, 0}, |
| 969 | {0, 0}, |
| 970 | {0, 0} |
| 971 | } |
| 972 | }, |
| 973 | {1, 0, 2, 0, /* 0x7d */ |
| 974 | {{0, 0}, |
| 975 | {2, 6}, |
| 976 | {0, 0}, |
| 977 | {0, 0} |
| 978 | } |
| 979 | }, |
| 980 | {0, 0, 1, 0, /* 0x7e */ |
| 981 | {{1, 6}, |
| 982 | {0, 0}, |
| 983 | {0, 0}, |
| 984 | {0, 0} |
| 985 | } |
| 986 | }, |
| 987 | {1, 0, 1, 0, /* 0x7f */ |
| 988 | {{0, 6}, |
| 989 | {0, 0}, |
| 990 | {0, 0}, |
| 991 | {0, 0} |
| 992 | } |
| 993 | }, |
| 994 | {0, 1, 1, 0, /* 0x80 */ |
| 995 | {{7, 7}, |
| 996 | {0, 0}, |
| 997 | {0, 0}, |
| 998 | {0, 0} |
| 999 | } |
| 1000 | }, |
| 1001 | {1, 1, 2, 0, /* 0x81 */ |
| 1002 | {{0, 0}, |
| 1003 | {7, 7}, |
| 1004 | {0, 0}, |
| 1005 | {0, 0} |
| 1006 | } |
| 1007 | }, |
| 1008 | {0, 1, 2, 0, /* 0x82 */ |
| 1009 | {{1, 1}, |
| 1010 | {7, 7}, |
| 1011 | {0, 0}, |
| 1012 | {0, 0} |
| 1013 | } |
| 1014 | }, |
| 1015 | {1, 1, 2, 0, /* 0x83 */ |
| 1016 | {{0, 1}, |
| 1017 | {7, 7}, |
| 1018 | {0, 0}, |
| 1019 | {0, 0} |
| 1020 | } |
| 1021 | }, |
| 1022 | {0, 1, 2, 0, /* 0x84 */ |
| 1023 | {{2, 2}, |
| 1024 | {7, 7}, |
| 1025 | {0, 0}, |
| 1026 | {0, 0} |
| 1027 | } |
| 1028 | }, |
| 1029 | {1, 1, 3, 0, /* 0x85 */ |
| 1030 | {{0, 0}, |
| 1031 | {2, 2}, |
| 1032 | {7, 7}, |
| 1033 | {0, 0} |
| 1034 | } |
| 1035 | }, |
| 1036 | {0, 1, 2, 0, /* 0x86 */ |
| 1037 | {{1, 2}, |
| 1038 | {7, 7}, |
| 1039 | {0, 0}, |
| 1040 | {0, 0} |
| 1041 | } |
| 1042 | }, |
| 1043 | {1, 1, 2, 0, /* 0x87 */ |
| 1044 | {{0, 2}, |
| 1045 | {7, 7}, |
| 1046 | {0, 0}, |
| 1047 | {0, 0} |
| 1048 | } |
| 1049 | }, |
| 1050 | {0, 1, 2, 0, /* 0x88 */ |
| 1051 | {{3, 3}, |
| 1052 | {7, 7}, |
| 1053 | {0, 0}, |
| 1054 | {0, 0} |
| 1055 | } |
| 1056 | }, |
| 1057 | {1, 1, 3, 0, /* 0x89 */ |
| 1058 | {{0, 0}, |
| 1059 | {3, 3}, |
| 1060 | {7, 7}, |
| 1061 | {0, 0} |
| 1062 | } |
| 1063 | }, |
| 1064 | {0, 1, 3, 0, /* 0x8a */ |
| 1065 | {{1, 1}, |
| 1066 | {3, 3}, |
| 1067 | {7, 7}, |
| 1068 | {0, 0} |
| 1069 | } |
| 1070 | }, |
| 1071 | {1, 1, 3, 0, /* 0x8b */ |
| 1072 | {{0, 1}, |
| 1073 | {3, 3}, |
| 1074 | {7, 7}, |
| 1075 | {0, 0} |
| 1076 | } |
| 1077 | }, |
| 1078 | {0, 1, 2, 0, /* 0x8c */ |
| 1079 | {{2, 3}, |
| 1080 | {7, 7}, |
| 1081 | {0, 0}, |
| 1082 | {0, 0} |
| 1083 | } |
| 1084 | }, |
| 1085 | {1, 1, 3, 0, /* 0x8d */ |
| 1086 | {{0, 0}, |
| 1087 | {2, 3}, |
| 1088 | {7, 7}, |
| 1089 | {0, 0} |
| 1090 | } |
| 1091 | }, |
| 1092 | {0, 1, 2, 0, /* 0x8e */ |
| 1093 | {{1, 3}, |
| 1094 | {7, 7}, |
| 1095 | {0, 0}, |
| 1096 | {0, 0} |
| 1097 | } |
| 1098 | }, |
| 1099 | {1, 1, 2, 0, /* 0x8f */ |
| 1100 | {{0, 3}, |
| 1101 | {7, 7}, |
| 1102 | {0, 0}, |
| 1103 | {0, 0} |
| 1104 | } |
| 1105 | }, |
| 1106 | {0, 1, 2, 0, /* 0x90 */ |
| 1107 | {{4, 4}, |
| 1108 | {7, 7}, |
| 1109 | {0, 0}, |
| 1110 | {0, 0} |
| 1111 | } |
| 1112 | }, |
| 1113 | {1, 1, 3, 0, /* 0x91 */ |
| 1114 | {{0, 0}, |
| 1115 | {4, 4}, |
| 1116 | {7, 7}, |
| 1117 | {0, 0} |
| 1118 | } |
| 1119 | }, |
| 1120 | {0, 1, 3, 0, /* 0x92 */ |
| 1121 | {{1, 1}, |
| 1122 | {4, 4}, |
| 1123 | {7, 7}, |
| 1124 | {0, 0} |
| 1125 | } |
| 1126 | }, |
| 1127 | {1, 1, 3, 0, /* 0x93 */ |
| 1128 | {{0, 1}, |
| 1129 | {4, 4}, |
| 1130 | {7, 7}, |
| 1131 | {0, 0} |
| 1132 | } |
| 1133 | }, |
| 1134 | {0, 1, 3, 0, /* 0x94 */ |
| 1135 | {{2, 2}, |
| 1136 | {4, 4}, |
| 1137 | {7, 7}, |
| 1138 | {0, 0} |
| 1139 | } |
| 1140 | }, |
| 1141 | {1, 1, 4, 0, /* 0x95 */ |
| 1142 | {{0, 0}, |
| 1143 | {2, 2}, |
| 1144 | {4, 4}, |
| 1145 | {7, 7} |
| 1146 | } |
| 1147 | }, |
| 1148 | {0, 1, 3, 0, /* 0x96 */ |
| 1149 | {{1, 2}, |
| 1150 | {4, 4}, |
| 1151 | {7, 7}, |
| 1152 | {0, 0} |
| 1153 | } |
| 1154 | }, |
| 1155 | {1, 1, 3, 0, /* 0x97 */ |
| 1156 | {{0, 2}, |
| 1157 | {4, 4}, |
| 1158 | {7, 7}, |
| 1159 | {0, 0} |
| 1160 | } |
| 1161 | }, |
| 1162 | {0, 1, 2, 0, /* 0x98 */ |
| 1163 | {{3, 4}, |
| 1164 | {7, 7}, |
| 1165 | {0, 0}, |
| 1166 | {0, 0} |
| 1167 | } |
| 1168 | }, |
| 1169 | {1, 1, 3, 0, /* 0x99 */ |
| 1170 | {{0, 0}, |
| 1171 | {3, 4}, |
| 1172 | {7, 7}, |
| 1173 | {0, 0} |
| 1174 | } |
| 1175 | }, |
| 1176 | {0, 1, 3, 0, /* 0x9a */ |
| 1177 | {{1, 1}, |
| 1178 | {3, 4}, |
| 1179 | {7, 7}, |
| 1180 | {0, 0} |
| 1181 | } |
| 1182 | }, |
| 1183 | {1, 1, 3, 0, /* 0x9b */ |
| 1184 | {{0, 1}, |
| 1185 | {3, 4}, |
| 1186 | {7, 7}, |
| 1187 | {0, 0} |
| 1188 | } |
| 1189 | }, |
| 1190 | {0, 1, 2, 0, /* 0x9c */ |
| 1191 | {{2, 4}, |
| 1192 | {7, 7}, |
| 1193 | {0, 0}, |
| 1194 | {0, 0} |
| 1195 | } |
| 1196 | }, |
| 1197 | {1, 1, 3, 0, /* 0x9d */ |
| 1198 | {{0, 0}, |
| 1199 | {2, 4}, |
| 1200 | {7, 7}, |
| 1201 | {0, 0} |
| 1202 | } |
| 1203 | }, |
| 1204 | {0, 1, 2, 0, /* 0x9e */ |
| 1205 | {{1, 4}, |
| 1206 | {7, 7}, |
| 1207 | {0, 0}, |
| 1208 | {0, 0} |
| 1209 | } |
| 1210 | }, |
| 1211 | {1, 1, 2, 0, /* 0x9f */ |
| 1212 | {{0, 4}, |
| 1213 | {7, 7}, |
| 1214 | {0, 0}, |
| 1215 | {0, 0} |
| 1216 | } |
| 1217 | }, |
| 1218 | {0, 1, 2, 0, /* 0xa0 */ |
| 1219 | {{5, 5}, |
| 1220 | {7, 7}, |
| 1221 | {0, 0}, |
| 1222 | {0, 0} |
| 1223 | } |
| 1224 | }, |
| 1225 | {1, 1, 3, 0, /* 0xa1 */ |
| 1226 | {{0, 0}, |
| 1227 | {5, 5}, |
| 1228 | {7, 7}, |
| 1229 | {0, 0} |
| 1230 | } |
| 1231 | }, |
| 1232 | {0, 1, 3, 0, /* 0xa2 */ |
| 1233 | {{1, 1}, |
| 1234 | {5, 5}, |
| 1235 | {7, 7}, |
| 1236 | {0, 0} |
| 1237 | } |
| 1238 | }, |
| 1239 | {1, 1, 3, 0, /* 0xa3 */ |
| 1240 | {{0, 1}, |
| 1241 | {5, 5}, |
| 1242 | {7, 7}, |
| 1243 | {0, 0} |
| 1244 | } |
| 1245 | }, |
| 1246 | {0, 1, 3, 0, /* 0xa4 */ |
| 1247 | {{2, 2}, |
| 1248 | {5, 5}, |
| 1249 | {7, 7}, |
| 1250 | {0, 0} |
| 1251 | } |
| 1252 | }, |
| 1253 | {1, 1, 4, 0, /* 0xa5 */ |
| 1254 | {{0, 0}, |
| 1255 | {2, 2}, |
| 1256 | {5, 5}, |
| 1257 | {7, 7} |
| 1258 | } |
| 1259 | }, |
| 1260 | {0, 1, 3, 0, /* 0xa6 */ |
| 1261 | {{1, 2}, |
| 1262 | {5, 5}, |
| 1263 | {7, 7}, |
| 1264 | {0, 0} |
| 1265 | } |
| 1266 | }, |
| 1267 | {1, 1, 3, 0, /* 0xa7 */ |
| 1268 | {{0, 2}, |
| 1269 | {5, 5}, |
| 1270 | {7, 7}, |
| 1271 | {0, 0} |
| 1272 | } |
| 1273 | }, |
| 1274 | {0, 1, 3, 0, /* 0xa8 */ |
| 1275 | {{3, 3}, |
| 1276 | {5, 5}, |
| 1277 | {7, 7}, |
| 1278 | {0, 0} |
| 1279 | } |
| 1280 | }, |
| 1281 | {1, 1, 4, 0, /* 0xa9 */ |
| 1282 | {{0, 0}, |
| 1283 | {3, 3}, |
| 1284 | {5, 5}, |
| 1285 | {7, 7} |
| 1286 | } |
| 1287 | }, |
| 1288 | {0, 1, 4, 0, /* 0xaa */ |
| 1289 | {{1, 1}, |
| 1290 | {3, 3}, |
| 1291 | {5, 5}, |
| 1292 | {7, 7} |
| 1293 | } |
| 1294 | }, |
| 1295 | {1, 1, 4, 0, /* 0xab */ |
| 1296 | {{0, 1}, |
| 1297 | {3, 3}, |
| 1298 | {5, 5}, |
| 1299 | {7, 7} |
| 1300 | } |
| 1301 | }, |
| 1302 | {0, 1, 3, 0, /* 0xac */ |
| 1303 | {{2, 3}, |
| 1304 | {5, 5}, |
| 1305 | {7, 7}, |
| 1306 | {0, 0} |
| 1307 | } |
| 1308 | }, |
| 1309 | {1, 1, 4, 0, /* 0xad */ |
| 1310 | {{0, 0}, |
| 1311 | {2, 3}, |
| 1312 | {5, 5}, |
| 1313 | {7, 7} |
| 1314 | } |
| 1315 | }, |
| 1316 | {0, 1, 3, 0, /* 0xae */ |
| 1317 | {{1, 3}, |
| 1318 | {5, 5}, |
| 1319 | {7, 7}, |
| 1320 | {0, 0} |
| 1321 | } |
| 1322 | }, |
| 1323 | {1, 1, 3, 0, /* 0xaf */ |
| 1324 | {{0, 3}, |
| 1325 | {5, 5}, |
| 1326 | {7, 7}, |
| 1327 | {0, 0} |
| 1328 | } |
| 1329 | }, |
| 1330 | {0, 1, 2, 0, /* 0xb0 */ |
| 1331 | {{4, 5}, |
| 1332 | {7, 7}, |
| 1333 | {0, 0}, |
| 1334 | {0, 0} |
| 1335 | } |
| 1336 | }, |
| 1337 | {1, 1, 3, 0, /* 0xb1 */ |
| 1338 | {{0, 0}, |
| 1339 | {4, 5}, |
| 1340 | {7, 7}, |
| 1341 | {0, 0} |
| 1342 | } |
| 1343 | }, |
| 1344 | {0, 1, 3, 0, /* 0xb2 */ |
| 1345 | {{1, 1}, |
| 1346 | {4, 5}, |
| 1347 | {7, 7}, |
| 1348 | {0, 0} |
| 1349 | } |
| 1350 | }, |
| 1351 | {1, 1, 3, 0, /* 0xb3 */ |
| 1352 | {{0, 1}, |
| 1353 | {4, 5}, |
| 1354 | {7, 7}, |
| 1355 | {0, 0} |
| 1356 | } |
| 1357 | }, |
| 1358 | {0, 1, 3, 0, /* 0xb4 */ |
| 1359 | {{2, 2}, |
| 1360 | {4, 5}, |
| 1361 | {7, 7}, |
| 1362 | {0, 0} |
| 1363 | } |
| 1364 | }, |
| 1365 | {1, 1, 4, 0, /* 0xb5 */ |
| 1366 | {{0, 0}, |
| 1367 | {2, 2}, |
| 1368 | {4, 5}, |
| 1369 | {7, 7} |
| 1370 | } |
| 1371 | }, |
| 1372 | {0, 1, 3, 0, /* 0xb6 */ |
| 1373 | {{1, 2}, |
| 1374 | {4, 5}, |
| 1375 | {7, 7}, |
| 1376 | {0, 0} |
| 1377 | } |
| 1378 | }, |
| 1379 | {1, 1, 3, 0, /* 0xb7 */ |
| 1380 | {{0, 2}, |
| 1381 | {4, 5}, |
| 1382 | {7, 7}, |
| 1383 | {0, 0} |
| 1384 | } |
| 1385 | }, |
| 1386 | {0, 1, 2, 0, /* 0xb8 */ |
| 1387 | {{3, 5}, |
| 1388 | {7, 7}, |
| 1389 | {0, 0}, |
| 1390 | {0, 0} |
| 1391 | } |
| 1392 | }, |
| 1393 | {1, 1, 3, 0, /* 0xb9 */ |
| 1394 | {{0, 0}, |
| 1395 | {3, 5}, |
| 1396 | {7, 7}, |
| 1397 | {0, 0} |
| 1398 | } |
| 1399 | }, |
| 1400 | {0, 1, 3, 0, /* 0xba */ |
| 1401 | {{1, 1}, |
| 1402 | {3, 5}, |
| 1403 | {7, 7}, |
| 1404 | {0, 0} |
| 1405 | } |
| 1406 | }, |
| 1407 | {1, 1, 3, 0, /* 0xbb */ |
| 1408 | {{0, 1}, |
| 1409 | {3, 5}, |
| 1410 | {7, 7}, |
| 1411 | {0, 0} |
| 1412 | } |
| 1413 | }, |
| 1414 | {0, 1, 2, 0, /* 0xbc */ |
| 1415 | {{2, 5}, |
| 1416 | {7, 7}, |
| 1417 | {0, 0}, |
| 1418 | {0, 0} |
| 1419 | } |
| 1420 | }, |
| 1421 | {1, 1, 3, 0, /* 0xbd */ |
| 1422 | {{0, 0}, |
| 1423 | {2, 5}, |
| 1424 | {7, 7}, |
| 1425 | {0, 0} |
| 1426 | } |
| 1427 | }, |
| 1428 | {0, 1, 2, 0, /* 0xbe */ |
| 1429 | {{1, 5}, |
| 1430 | {7, 7}, |
| 1431 | {0, 0}, |
| 1432 | {0, 0} |
| 1433 | } |
| 1434 | }, |
| 1435 | {1, 1, 2, 0, /* 0xbf */ |
| 1436 | {{0, 5}, |
| 1437 | {7, 7}, |
| 1438 | {0, 0}, |
| 1439 | {0, 0} |
| 1440 | } |
| 1441 | }, |
| 1442 | {0, 1, 1, 0, /* 0xc0 */ |
| 1443 | {{6, 7}, |
| 1444 | {0, 0}, |
| 1445 | {0, 0}, |
| 1446 | {0, 0} |
| 1447 | } |
| 1448 | }, |
| 1449 | {1, 1, 2, 0, /* 0xc1 */ |
| 1450 | {{0, 0}, |
| 1451 | {6, 7}, |
| 1452 | {0, 0}, |
| 1453 | {0, 0} |
| 1454 | } |
| 1455 | }, |
| 1456 | {0, 1, 2, 0, /* 0xc2 */ |
| 1457 | {{1, 1}, |
| 1458 | {6, 7}, |
| 1459 | {0, 0}, |
| 1460 | {0, 0} |
| 1461 | } |
| 1462 | }, |
| 1463 | {1, 1, 2, 0, /* 0xc3 */ |
| 1464 | {{0, 1}, |
| 1465 | {6, 7}, |
| 1466 | {0, 0}, |
| 1467 | {0, 0} |
| 1468 | } |
| 1469 | }, |
| 1470 | {0, 1, 2, 0, /* 0xc4 */ |
| 1471 | {{2, 2}, |
| 1472 | {6, 7}, |
| 1473 | {0, 0}, |
| 1474 | {0, 0} |
| 1475 | } |
| 1476 | }, |
| 1477 | {1, 1, 3, 0, /* 0xc5 */ |
| 1478 | {{0, 0}, |
| 1479 | {2, 2}, |
| 1480 | {6, 7}, |
| 1481 | {0, 0} |
| 1482 | } |
| 1483 | }, |
| 1484 | {0, 1, 2, 0, /* 0xc6 */ |
| 1485 | {{1, 2}, |
| 1486 | {6, 7}, |
| 1487 | {0, 0}, |
| 1488 | {0, 0} |
| 1489 | } |
| 1490 | }, |
| 1491 | {1, 1, 2, 0, /* 0xc7 */ |
| 1492 | {{0, 2}, |
| 1493 | {6, 7}, |
| 1494 | {0, 0}, |
| 1495 | {0, 0} |
| 1496 | } |
| 1497 | }, |
| 1498 | {0, 1, 2, 0, /* 0xc8 */ |
| 1499 | {{3, 3}, |
| 1500 | {6, 7}, |
| 1501 | {0, 0}, |
| 1502 | {0, 0} |
| 1503 | } |
| 1504 | }, |
| 1505 | {1, 1, 3, 0, /* 0xc9 */ |
| 1506 | {{0, 0}, |
| 1507 | {3, 3}, |
| 1508 | {6, 7}, |
| 1509 | {0, 0} |
| 1510 | } |
| 1511 | }, |
| 1512 | {0, 1, 3, 0, /* 0xca */ |
| 1513 | {{1, 1}, |
| 1514 | {3, 3}, |
| 1515 | {6, 7}, |
| 1516 | {0, 0} |
| 1517 | } |
| 1518 | }, |
| 1519 | {1, 1, 3, 0, /* 0xcb */ |
| 1520 | {{0, 1}, |
| 1521 | {3, 3}, |
| 1522 | {6, 7}, |
| 1523 | {0, 0} |
| 1524 | } |
| 1525 | }, |
| 1526 | {0, 1, 2, 0, /* 0xcc */ |
| 1527 | {{2, 3}, |
| 1528 | {6, 7}, |
| 1529 | {0, 0}, |
| 1530 | {0, 0} |
| 1531 | } |
| 1532 | }, |
| 1533 | {1, 1, 3, 0, /* 0xcd */ |
| 1534 | {{0, 0}, |
| 1535 | {2, 3}, |
| 1536 | {6, 7}, |
| 1537 | {0, 0} |
| 1538 | } |
| 1539 | }, |
| 1540 | {0, 1, 2, 0, /* 0xce */ |
| 1541 | {{1, 3}, |
| 1542 | {6, 7}, |
| 1543 | {0, 0}, |
| 1544 | {0, 0} |
| 1545 | } |
| 1546 | }, |
| 1547 | {1, 1, 2, 0, /* 0xcf */ |
| 1548 | {{0, 3}, |
| 1549 | {6, 7}, |
| 1550 | {0, 0}, |
| 1551 | {0, 0} |
| 1552 | } |
| 1553 | }, |
| 1554 | {0, 1, 2, 0, /* 0xd0 */ |
| 1555 | {{4, 4}, |
| 1556 | {6, 7}, |
| 1557 | {0, 0}, |
| 1558 | {0, 0} |
| 1559 | } |
| 1560 | }, |
| 1561 | {1, 1, 3, 0, /* 0xd1 */ |
| 1562 | {{0, 0}, |
| 1563 | {4, 4}, |
| 1564 | {6, 7}, |
| 1565 | {0, 0} |
| 1566 | } |
| 1567 | }, |
| 1568 | {0, 1, 3, 0, /* 0xd2 */ |
| 1569 | {{1, 1}, |
| 1570 | {4, 4}, |
| 1571 | {6, 7}, |
| 1572 | {0, 0} |
| 1573 | } |
| 1574 | }, |
| 1575 | {1, 1, 3, 0, /* 0xd3 */ |
| 1576 | {{0, 1}, |
| 1577 | {4, 4}, |
| 1578 | {6, 7}, |
| 1579 | {0, 0} |
| 1580 | } |
| 1581 | }, |
| 1582 | {0, 1, 3, 0, /* 0xd4 */ |
| 1583 | {{2, 2}, |
| 1584 | {4, 4}, |
| 1585 | {6, 7}, |
| 1586 | {0, 0} |
| 1587 | } |
| 1588 | }, |
| 1589 | {1, 1, 4, 0, /* 0xd5 */ |
| 1590 | {{0, 0}, |
| 1591 | {2, 2}, |
| 1592 | {4, 4}, |
| 1593 | {6, 7} |
| 1594 | } |
| 1595 | }, |
| 1596 | {0, 1, 3, 0, /* 0xd6 */ |
| 1597 | {{1, 2}, |
| 1598 | {4, 4}, |
| 1599 | {6, 7}, |
| 1600 | {0, 0} |
| 1601 | } |
| 1602 | }, |
| 1603 | {1, 1, 3, 0, /* 0xd7 */ |
| 1604 | {{0, 2}, |
| 1605 | {4, 4}, |
| 1606 | {6, 7}, |
| 1607 | {0, 0} |
| 1608 | } |
| 1609 | }, |
| 1610 | {0, 1, 2, 0, /* 0xd8 */ |
| 1611 | {{3, 4}, |
| 1612 | {6, 7}, |
| 1613 | {0, 0}, |
| 1614 | {0, 0} |
| 1615 | } |
| 1616 | }, |
| 1617 | {1, 1, 3, 0, /* 0xd9 */ |
| 1618 | {{0, 0}, |
| 1619 | {3, 4}, |
| 1620 | {6, 7}, |
| 1621 | {0, 0} |
| 1622 | } |
| 1623 | }, |
| 1624 | {0, 1, 3, 0, /* 0xda */ |
| 1625 | {{1, 1}, |
| 1626 | {3, 4}, |
| 1627 | {6, 7}, |
| 1628 | {0, 0} |
| 1629 | } |
| 1630 | }, |
| 1631 | {1, 1, 3, 0, /* 0xdb */ |
| 1632 | {{0, 1}, |
| 1633 | {3, 4}, |
| 1634 | {6, 7}, |
| 1635 | {0, 0} |
| 1636 | } |
| 1637 | }, |
| 1638 | {0, 1, 2, 0, /* 0xdc */ |
| 1639 | {{2, 4}, |
| 1640 | {6, 7}, |
| 1641 | {0, 0}, |
| 1642 | {0, 0} |
| 1643 | } |
| 1644 | }, |
| 1645 | {1, 1, 3, 0, /* 0xdd */ |
| 1646 | {{0, 0}, |
| 1647 | {2, 4}, |
| 1648 | {6, 7}, |
| 1649 | {0, 0} |
| 1650 | } |
| 1651 | }, |
| 1652 | {0, 1, 2, 0, /* 0xde */ |
| 1653 | {{1, 4}, |
| 1654 | {6, 7}, |
| 1655 | {0, 0}, |
| 1656 | {0, 0} |
| 1657 | } |
| 1658 | }, |
| 1659 | {1, 1, 2, 0, /* 0xdf */ |
| 1660 | {{0, 4}, |
| 1661 | {6, 7}, |
| 1662 | {0, 0}, |
| 1663 | {0, 0} |
| 1664 | } |
| 1665 | }, |
| 1666 | {0, 1, 1, 0, /* 0xe0 */ |
| 1667 | {{5, 7}, |
| 1668 | {0, 0}, |
| 1669 | {0, 0}, |
| 1670 | {0, 0} |
| 1671 | } |
| 1672 | }, |
| 1673 | {1, 1, 2, 0, /* 0xe1 */ |
| 1674 | {{0, 0}, |
| 1675 | {5, 7}, |
| 1676 | {0, 0}, |
| 1677 | {0, 0} |
| 1678 | } |
| 1679 | }, |
| 1680 | {0, 1, 2, 0, /* 0xe2 */ |
| 1681 | {{1, 1}, |
| 1682 | {5, 7}, |
| 1683 | {0, 0}, |
| 1684 | {0, 0} |
| 1685 | } |
| 1686 | }, |
| 1687 | {1, 1, 2, 0, /* 0xe3 */ |
| 1688 | {{0, 1}, |
| 1689 | {5, 7}, |
| 1690 | {0, 0}, |
| 1691 | {0, 0} |
| 1692 | } |
| 1693 | }, |
| 1694 | {0, 1, 2, 0, /* 0xe4 */ |
| 1695 | {{2, 2}, |
| 1696 | {5, 7}, |
| 1697 | {0, 0}, |
| 1698 | {0, 0} |
| 1699 | } |
| 1700 | }, |
| 1701 | {1, 1, 3, 0, /* 0xe5 */ |
| 1702 | {{0, 0}, |
| 1703 | {2, 2}, |
| 1704 | {5, 7}, |
| 1705 | {0, 0} |
| 1706 | } |
| 1707 | }, |
| 1708 | {0, 1, 2, 0, /* 0xe6 */ |
| 1709 | {{1, 2}, |
| 1710 | {5, 7}, |
| 1711 | {0, 0}, |
| 1712 | {0, 0} |
| 1713 | } |
| 1714 | }, |
| 1715 | {1, 1, 2, 0, /* 0xe7 */ |
| 1716 | {{0, 2}, |
| 1717 | {5, 7}, |
| 1718 | {0, 0}, |
| 1719 | {0, 0} |
| 1720 | } |
| 1721 | }, |
| 1722 | {0, 1, 2, 0, /* 0xe8 */ |
| 1723 | {{3, 3}, |
| 1724 | {5, 7}, |
| 1725 | {0, 0}, |
| 1726 | {0, 0} |
| 1727 | } |
| 1728 | }, |
| 1729 | {1, 1, 3, 0, /* 0xe9 */ |
| 1730 | {{0, 0}, |
| 1731 | {3, 3}, |
| 1732 | {5, 7}, |
| 1733 | {0, 0} |
| 1734 | } |
| 1735 | }, |
| 1736 | {0, 1, 3, 0, /* 0xea */ |
| 1737 | {{1, 1}, |
| 1738 | {3, 3}, |
| 1739 | {5, 7}, |
| 1740 | {0, 0} |
| 1741 | } |
| 1742 | }, |
| 1743 | {1, 1, 3, 0, /* 0xeb */ |
| 1744 | {{0, 1}, |
| 1745 | {3, 3}, |
| 1746 | {5, 7}, |
| 1747 | {0, 0} |
| 1748 | } |
| 1749 | }, |
| 1750 | {0, 1, 2, 0, /* 0xec */ |
| 1751 | {{2, 3}, |
| 1752 | {5, 7}, |
| 1753 | {0, 0}, |
| 1754 | {0, 0} |
| 1755 | } |
| 1756 | }, |
| 1757 | {1, 1, 3, 0, /* 0xed */ |
| 1758 | {{0, 0}, |
| 1759 | {2, 3}, |
| 1760 | {5, 7}, |
| 1761 | {0, 0} |
| 1762 | } |
| 1763 | }, |
| 1764 | {0, 1, 2, 0, /* 0xee */ |
| 1765 | {{1, 3}, |
| 1766 | {5, 7}, |
| 1767 | {0, 0}, |
| 1768 | {0, 0} |
| 1769 | } |
| 1770 | }, |
| 1771 | {1, 1, 2, 0, /* 0xef */ |
| 1772 | {{0, 3}, |
| 1773 | {5, 7}, |
| 1774 | {0, 0}, |
| 1775 | {0, 0} |
| 1776 | } |
| 1777 | }, |
| 1778 | {0, 1, 1, 0, /* 0xf0 */ |
| 1779 | {{4, 7}, |
| 1780 | {0, 0}, |
| 1781 | {0, 0}, |
| 1782 | {0, 0} |
| 1783 | } |
| 1784 | }, |
| 1785 | {1, 1, 2, 0, /* 0xf1 */ |
| 1786 | {{0, 0}, |
| 1787 | {4, 7}, |
| 1788 | {0, 0}, |
| 1789 | {0, 0} |
| 1790 | } |
| 1791 | }, |
| 1792 | {0, 1, 2, 0, /* 0xf2 */ |
| 1793 | {{1, 1}, |
| 1794 | {4, 7}, |
| 1795 | {0, 0}, |
| 1796 | {0, 0} |
| 1797 | } |
| 1798 | }, |
| 1799 | {1, 1, 2, 0, /* 0xf3 */ |
| 1800 | {{0, 1}, |
| 1801 | {4, 7}, |
| 1802 | {0, 0}, |
| 1803 | {0, 0} |
| 1804 | } |
| 1805 | }, |
| 1806 | {0, 1, 2, 0, /* 0xf4 */ |
| 1807 | {{2, 2}, |
| 1808 | {4, 7}, |
| 1809 | {0, 0}, |
| 1810 | {0, 0} |
| 1811 | } |
| 1812 | }, |
| 1813 | {1, 1, 3, 0, /* 0xf5 */ |
| 1814 | {{0, 0}, |
| 1815 | {2, 2}, |
| 1816 | {4, 7}, |
| 1817 | {0, 0} |
| 1818 | } |
| 1819 | }, |
| 1820 | {0, 1, 2, 0, /* 0xf6 */ |
| 1821 | {{1, 2}, |
| 1822 | {4, 7}, |
| 1823 | {0, 0}, |
| 1824 | {0, 0} |
| 1825 | } |
| 1826 | }, |
| 1827 | {1, 1, 2, 0, /* 0xf7 */ |
| 1828 | {{0, 2}, |
| 1829 | {4, 7}, |
| 1830 | {0, 0}, |
| 1831 | {0, 0} |
| 1832 | } |
| 1833 | }, |
| 1834 | {0, 1, 1, 0, /* 0xf8 */ |
| 1835 | {{3, 7}, |
| 1836 | {0, 0}, |
| 1837 | {0, 0}, |
| 1838 | {0, 0} |
| 1839 | } |
| 1840 | }, |
| 1841 | {1, 1, 2, 0, /* 0xf9 */ |
| 1842 | {{0, 0}, |
| 1843 | {3, 7}, |
| 1844 | {0, 0}, |
| 1845 | {0, 0} |
| 1846 | } |
| 1847 | }, |
| 1848 | {0, 1, 2, 0, /* 0xfa */ |
| 1849 | {{1, 1}, |
| 1850 | {3, 7}, |
| 1851 | {0, 0}, |
| 1852 | {0, 0} |
| 1853 | } |
| 1854 | }, |
| 1855 | {1, 1, 2, 0, /* 0xfb */ |
| 1856 | {{0, 1}, |
| 1857 | {3, 7}, |
| 1858 | {0, 0}, |
| 1859 | {0, 0} |
| 1860 | } |
| 1861 | }, |
| 1862 | {0, 1, 1, 0, /* 0xfc */ |
| 1863 | {{2, 7}, |
| 1864 | {0, 0}, |
| 1865 | {0, 0}, |
| 1866 | {0, 0} |
| 1867 | } |
| 1868 | }, |
| 1869 | {1, 1, 2, 0, /* 0xfd */ |
| 1870 | {{0, 0}, |
| 1871 | {2, 7}, |
| 1872 | {0, 0}, |
| 1873 | {0, 0} |
| 1874 | } |
| 1875 | }, |
| 1876 | {0, 1, 1, 0, /* 0xfe */ |
| 1877 | {{1, 7}, |
| 1878 | {0, 0}, |
| 1879 | {0, 0}, |
| 1880 | {0, 0} |
| 1881 | } |
| 1882 | }, |
| 1883 | {1, 1, 1, 0, /* 0xff */ |
| 1884 | {{0, 7}, |
| 1885 | {0, 0}, |
| 1886 | {0, 0}, |
| 1887 | {0, 0} |
| 1888 | } |
| 1889 | } |
| 1890 | }; |
| 1891 |
| 1892 | int |
| 1893 | sctp_is_address_in_scope(struct sctp_ifa *ifa, |
| 1894 | struct sctp_scoping *scope, |
| 1895 | int do_update) |
| 1896 | { |
| 1897 | if ((scope->loopback_scope == 0) && |
| 1898 | (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) { |
| 1899 | /* |
| 1900 | * skip loopback if not in scope * |
| 1901 | */ |
| 1902 | return (0); |
| 1903 | } |
| 1904 | switch (ifa->address.sa.sa_family) { |
| 1905 | #ifdef INET |
| 1906 | case AF_INET: |
| 1907 | if (scope->ipv4_addr_legal) { |
| 1908 | struct sockaddr_in *sin; |
| 1909 |
| 1910 | sin = &ifa->address.sin; |
| 1911 | if (sin->sin_addr.s_addr == 0) { |
| 1912 | /* not in scope , unspecified */ |
| 1913 | return (0); |
| 1914 | } |
| 1915 | if ((scope->ipv4_local_scope == 0) && |
| 1916 | (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) { |
| 1917 | /* private address not in scope */ |
| 1918 | return (0); |
| 1919 | } |
| 1920 | } else { |
| 1921 | return (0); |
| 1922 | } |
| 1923 | break; |
| 1924 | #endif |
| 1925 | #ifdef INET6 |
| 1926 | case AF_INET6: |
| 1927 | if (scope->ipv6_addr_legal) { |
| 1928 | struct sockaddr_in6 *sin6; |
| 1929 |
| 1930 | /* Must update the flags, bummer, which |
| 1931 | * means any IFA locks must now be applied HERE <-> |
| 1932 | */ |
| 1933 | if (do_update) { |
| 1934 | sctp_gather_internal_ifa_flags(ifa); |
| 1935 | } |
| 1936 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
| 1937 | return (0); |
| 1938 | } |
| 1939 | /* ok to use deprecated addresses? */ |
| 1940 | sin6 = &ifa->address.sin6; |
| 1941 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
| 1942 | /* skip unspecifed addresses */ |
| 1943 | return (0); |
| 1944 | } |
| 1945 | if ( /* (local_scope == 0) && */ |
| 1946 | (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) { |
| 1947 | return (0); |
| 1948 | } |
| 1949 | if ((scope->site_scope == 0) && |
| 1950 | (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) { |
| 1951 | return (0); |
| 1952 | } |
| 1953 | } else { |
| 1954 | return (0); |
| 1955 | } |
| 1956 | break; |
| 1957 | #endif |
| 1958 | #if defined(__Userspace__) |
| 1959 | case AF_CONN: |
| 1960 | if (!scope->conn_addr_legal) { |
| 1961 | return (0); |
| 1962 | } |
| 1963 | break; |
| 1964 | #endif |
| 1965 | default: |
| 1966 | return (0); |
| 1967 | } |
| 1968 | return (1); |
| 1969 | } |
| 1970 |
| 1971 | static struct mbuf * |
| 1972 | sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa, uint16_t *len) |
| 1973 | { |
| 1974 | #if defined(INET) || defined(INET6) |
| 1975 | struct sctp_paramhdr *paramh; |
| 1976 | struct mbuf *mret; |
| 1977 | uint16_t plen; |
| 1978 | #endif |
| 1979 |
| 1980 | switch (ifa->address.sa.sa_family) { |
| 1981 | #ifdef INET |
| 1982 | case AF_INET: |
| 1983 | plen = (uint16_t)sizeof(struct sctp_ipv4addr_param); |
| 1984 | break; |
| 1985 | #endif |
| 1986 | #ifdef INET6 |
| 1987 | case AF_INET6: |
| 1988 | plen = (uint16_t)sizeof(struct sctp_ipv6addr_param); |
| 1989 | break; |
| 1990 | #endif |
| 1991 | default: |
| 1992 | return (m); |
| 1993 | } |
| 1994 | #if defined(INET) || defined(INET6) |
| 1995 | if (M_TRAILINGSPACE(m) >= plen) { |
| 1996 | /* easy side we just drop it on the end */ |
| 1997 | paramh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m))); |
| 1998 | mret = m; |
| 1999 | } else { |
| 2000 | /* Need more space */ |
| 2001 | mret = m; |
| 2002 | while (SCTP_BUF_NEXT(mret) != NULL) { |
| 2003 | mret = SCTP_BUF_NEXT(mret); |
| 2004 | } |
| 2005 | SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(plen, 0, M_NOWAIT, 1, MT_DATA); |
| 2006 | if (SCTP_BUF_NEXT(mret) == NULL) { |
| 2007 | /* We are hosed, can't add more addresses */ |
| 2008 | return (m); |
| 2009 | } |
| 2010 | mret = SCTP_BUF_NEXT(mret); |
| 2011 | paramh = mtod(mret, struct sctp_paramhdr *); |
| 2012 | } |
| 2013 | /* now add the parameter */ |
| 2014 | switch (ifa->address.sa.sa_family) { |
| 2015 | #ifdef INET |
| 2016 | case AF_INET: |
| 2017 | { |
| 2018 | struct sctp_ipv4addr_param *ipv4p; |
| 2019 | struct sockaddr_in *sin; |
| 2020 |
| 2021 | sin = &ifa->address.sin; |
| 2022 | ipv4p = (struct sctp_ipv4addr_param *)paramh; |
| 2023 | paramh->param_type = htons(SCTP_IPV4_ADDRESS); |
| 2024 | paramh->param_length = htons(plen); |
| 2025 | ipv4p->addr = sin->sin_addr.s_addr; |
| 2026 | SCTP_BUF_LEN(mret) += plen; |
| 2027 | break; |
| 2028 | } |
| 2029 | #endif |
| 2030 | #ifdef INET6 |
| 2031 | case AF_INET6: |
| 2032 | { |
| 2033 | struct sctp_ipv6addr_param *ipv6p; |
| 2034 | struct sockaddr_in6 *sin6; |
| 2035 |
| 2036 | sin6 = &ifa->address.sin6; |
| 2037 | ipv6p = (struct sctp_ipv6addr_param *)paramh; |
| 2038 | paramh->param_type = htons(SCTP_IPV6_ADDRESS); |
| 2039 | paramh->param_length = htons(plen); |
| 2040 | memcpy(ipv6p->addr, &sin6->sin6_addr, |
| 2041 | sizeof(ipv6p->addr)); |
| 2042 | #if defined(SCTP_EMBEDDED_V6_SCOPE) |
| 2043 | /* clear embedded scope in the address */ |
| 2044 | in6_clearscope((struct in6_addr *)ipv6p->addr); |
| 2045 | #endif |
| 2046 | SCTP_BUF_LEN(mret) += plen; |
| 2047 | break; |
| 2048 | } |
| 2049 | #endif |
| 2050 | default: |
| 2051 | return (m); |
| 2052 | } |
| 2053 | if (len != NULL) { |
| 2054 | *len += plen; |
| 2055 | } |
| 2056 | return (mret); |
| 2057 | #endif |
| 2058 | } |
| 2059 |
| 2060 | struct mbuf * |
| 2061 | sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_tcb *stcb, |
| 2062 | struct sctp_scoping *scope, |
| 2063 | struct mbuf *m_at, int cnt_inits_to, |
| 2064 | uint16_t *padding_len, uint16_t *chunk_len) |
| 2065 | { |
| 2066 | struct sctp_vrf *vrf = NULL; |
| 2067 | int cnt, limit_out = 0, total_count; |
| 2068 | uint32_t vrf_id; |
| 2069 |
| 2070 | vrf_id = inp->def_vrf_id; |
| 2071 | SCTP_IPI_ADDR_RLOCK(); |
| 2072 | vrf = sctp_find_vrf(vrf_id); |
| 2073 | if (vrf == NULL) { |
| 2074 | SCTP_IPI_ADDR_RUNLOCK(); |
| 2075 | return (m_at); |
| 2076 | } |
| 2077 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
| 2078 | struct sctp_ifa *sctp_ifap; |
| 2079 | struct sctp_ifn *sctp_ifnp; |
| 2080 |
| 2081 | cnt = cnt_inits_to; |
| 2082 | if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) { |
| 2083 | limit_out = 1; |
| 2084 | cnt = SCTP_ADDRESS_LIMIT; |
| 2085 | goto skip_count; |
| 2086 | } |
| 2087 | LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) { |
| 2088 | if ((scope->loopback_scope == 0) && |
| 2089 | SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) { |
| 2090 | /* |
| 2091 | * Skip loopback devices if loopback_scope |
| 2092 | * not set |
| 2093 | */ |
| 2094 | continue; |
| 2095 | } |
| 2096 | LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) { |
| 2097 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 2098 | #ifdef INET |
| 2099 | if ((sctp_ifap->address.sa.sa_family == AF_INET) && |
| 2100 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 2101 | &sctp_ifap->address.sin.sin_addr) != 0)) { |
| 2102 | continue; |
| 2103 | } |
| 2104 | #endif |
| 2105 | #ifdef INET6 |
| 2106 | if ((sctp_ifap->address.sa.sa_family == AF_INET6) && |
| 2107 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 2108 | &sctp_ifap->address.sin6.sin6_addr) != 0)) { |
| 2109 | continue; |
| 2110 | } |
| 2111 | #endif |
| 2112 | #endif |
| 2113 | if (sctp_is_addr_restricted(stcb, sctp_ifap)) { |
| 2114 | continue; |
| 2115 | } |
| 2116 | #if defined(__Userspace__) |
| 2117 | if (sctp_ifap->address.sa.sa_family == AF_CONN) { |
| 2118 | continue; |
| 2119 | } |
| 2120 | #endif |
| 2121 | if (sctp_is_address_in_scope(sctp_ifap, scope, 1) == 0) { |
| 2122 | continue; |
| 2123 | } |
| 2124 | cnt++; |
| 2125 | if (cnt > SCTP_ADDRESS_LIMIT) { |
| 2126 | break; |
| 2127 | } |
| 2128 | } |
| 2129 | if (cnt > SCTP_ADDRESS_LIMIT) { |
| 2130 | break; |
| 2131 | } |
| 2132 | } |
| 2133 | skip_count: |
| 2134 | if (cnt > 1) { |
| 2135 | total_count = 0; |
| 2136 | LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) { |
| 2137 | cnt = 0; |
| 2138 | if ((scope->loopback_scope == 0) && |
| 2139 | SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) { |
| 2140 | /* |
| 2141 | * Skip loopback devices if |
| 2142 | * loopback_scope not set |
| 2143 | */ |
| 2144 | continue; |
| 2145 | } |
| 2146 | LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) { |
| 2147 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 2148 | #ifdef INET |
| 2149 | if ((sctp_ifap->address.sa.sa_family == AF_INET) && |
| 2150 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 2151 | &sctp_ifap->address.sin.sin_addr) != 0)) { |
| 2152 | continue; |
| 2153 | } |
| 2154 | #endif |
| 2155 | #ifdef INET6 |
| 2156 | if ((sctp_ifap->address.sa.sa_family == AF_INET6) && |
| 2157 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 2158 | &sctp_ifap->address.sin6.sin6_addr) != 0)) { |
| 2159 | continue; |
| 2160 | } |
| 2161 | #endif |
| 2162 | #endif |
| 2163 | if (sctp_is_addr_restricted(stcb, sctp_ifap)) { |
| 2164 | continue; |
| 2165 | } |
| 2166 | #if defined(__Userspace__) |
| 2167 | if (sctp_ifap->address.sa.sa_family == AF_CONN) { |
| 2168 | continue; |
| 2169 | } |
| 2170 | #endif |
| 2171 | if (sctp_is_address_in_scope(sctp_ifap, |
| 2172 | scope, 0) == 0) { |
| 2173 | continue; |
| 2174 | } |
| 2175 | if ((chunk_len != NULL) && |
| 2176 | (padding_len != NULL) && |
| 2177 | (*padding_len > 0)) { |
| 2178 | memset(mtod(m_at, caddr_t) + *chunk_len, 0, *padding_len); |
| 2179 | SCTP_BUF_LEN(m_at) += *padding_len; |
| 2180 | *chunk_len += *padding_len; |
| 2181 | *padding_len = 0; |
| 2182 | } |
| 2183 | m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap, chunk_len); |
| 2184 | if (limit_out) { |
| 2185 | cnt++; |
| 2186 | total_count++; |
| 2187 | if (cnt >= 2) { |
| 2188 | /* two from each address */ |
| 2189 | break; |
| 2190 | } |
| 2191 | if (total_count > SCTP_ADDRESS_LIMIT) { |
| 2192 | /* No more addresses */ |
| 2193 | break; |
| 2194 | } |
| 2195 | } |
| 2196 | } |
| 2197 | } |
| 2198 | } |
| 2199 | } else { |
| 2200 | struct sctp_laddr *laddr; |
| 2201 |
| 2202 | cnt = cnt_inits_to; |
| 2203 | /* First, how many ? */ |
| 2204 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 2205 | if (laddr->ifa == NULL) { |
| 2206 | continue; |
| 2207 | } |
| 2208 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) |
| 2209 | /* Address being deleted by the system, dont |
| 2210 | * list. |
| 2211 | */ |
| 2212 | continue; |
| 2213 | if (laddr->action == SCTP_DEL_IP_ADDRESS) { |
| 2214 | /* Address being deleted on this ep |
| 2215 | * don't list. |
| 2216 | */ |
| 2217 | continue; |
| 2218 | } |
| 2219 | #if defined(__Userspace__) |
| 2220 | if (laddr->ifa->address.sa.sa_family == AF_CONN) { |
| 2221 | continue; |
| 2222 | } |
| 2223 | #endif |
| 2224 | if (sctp_is_address_in_scope(laddr->ifa, |
| 2225 | scope, 1) == 0) { |
| 2226 | continue; |
| 2227 | } |
| 2228 | cnt++; |
| 2229 | } |
| 2230 | /* |
| 2231 | * To get through a NAT we only list addresses if we have |
| 2232 | * more than one. That way if you just bind a single address |
| 2233 | * we let the source of the init dictate our address. |
| 2234 | */ |
| 2235 | if (cnt > 1) { |
| 2236 | cnt = cnt_inits_to; |
| 2237 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 2238 | if (laddr->ifa == NULL) { |
| 2239 | continue; |
| 2240 | } |
| 2241 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
| 2242 | continue; |
| 2243 | } |
| 2244 | #if defined(__Userspace__) |
| 2245 | if (laddr->ifa->address.sa.sa_family == AF_CONN) { |
| 2246 | continue; |
| 2247 | } |
| 2248 | #endif |
| 2249 | if (sctp_is_address_in_scope(laddr->ifa, |
| 2250 | scope, 0) == 0) { |
| 2251 | continue; |
| 2252 | } |
| 2253 | if ((chunk_len != NULL) && |
| 2254 | (padding_len != NULL) && |
| 2255 | (*padding_len > 0)) { |
| 2256 | memset(mtod(m_at, caddr_t) + *chunk_len, 0, *padding_len); |
| 2257 | SCTP_BUF_LEN(m_at) += *padding_len; |
| 2258 | *chunk_len += *padding_len; |
| 2259 | *padding_len = 0; |
| 2260 | } |
| 2261 | m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa, chunk_len); |
| 2262 | cnt++; |
| 2263 | if (cnt >= SCTP_ADDRESS_LIMIT) { |
| 2264 | break; |
| 2265 | } |
| 2266 | } |
| 2267 | } |
| 2268 | } |
| 2269 | SCTP_IPI_ADDR_RUNLOCK(); |
| 2270 | return (m_at); |
| 2271 | } |
| 2272 |
| 2273 | static struct sctp_ifa * |
| 2274 | sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa, |
| 2275 | uint8_t dest_is_loop, |
| 2276 | uint8_t dest_is_priv, |
| 2277 | sa_family_t fam) |
| 2278 | { |
| 2279 | uint8_t dest_is_global = 0; |
| 2280 | /* dest_is_priv is true if destination is a private address */ |
| 2281 | /* dest_is_loop is true if destination is a loopback addresses */ |
| 2282 |
| 2283 | /** |
| 2284 | * Here we determine if its a preferred address. A preferred address |
| 2285 | * means it is the same scope or higher scope then the destination. |
| 2286 | * L = loopback, P = private, G = global |
| 2287 | * ----------------------------------------- |
| 2288 | * src | dest | result |
| 2289 | * ---------------------------------------- |
| 2290 | * L | L | yes |
| 2291 | * ----------------------------------------- |
| 2292 | * P | L | yes-v4 no-v6 |
| 2293 | * ----------------------------------------- |
| 2294 | * G | L | yes-v4 no-v6 |
| 2295 | * ----------------------------------------- |
| 2296 | * L | P | no |
| 2297 | * ----------------------------------------- |
| 2298 | * P | P | yes |
| 2299 | * ----------------------------------------- |
| 2300 | * G | P | no |
| 2301 | * ----------------------------------------- |
| 2302 | * L | G | no |
| 2303 | * ----------------------------------------- |
| 2304 | * P | G | no |
| 2305 | * ----------------------------------------- |
| 2306 | * G | G | yes |
| 2307 | * ----------------------------------------- |
| 2308 | */ |
| 2309 |
| 2310 | if (ifa->address.sa.sa_family != fam) { |
| 2311 | /* forget mis-matched family */ |
| 2312 | return (NULL); |
| 2313 | } |
| 2314 | if ((dest_is_priv == 0) && (dest_is_loop == 0)) { |
| 2315 | dest_is_global = 1; |
| 2316 | } |
| 2317 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:"); |
| 2318 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa); |
| 2319 | /* Ok the address may be ok */ |
| 2320 | #ifdef INET6 |
| 2321 | if (fam == AF_INET6) { |
| 2322 | /* ok to use deprecated addresses? no lets not! */ |
| 2323 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
| 2324 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n"); |
| 2325 | return (NULL); |
| 2326 | } |
| 2327 | if (ifa->src_is_priv && !ifa->src_is_loop) { |
| 2328 | if (dest_is_loop) { |
| 2329 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n"); |
| 2330 | return (NULL); |
| 2331 | } |
| 2332 | } |
| 2333 | if (ifa->src_is_glob) { |
| 2334 | if (dest_is_loop) { |
| 2335 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n"); |
| 2336 | return (NULL); |
| 2337 | } |
| 2338 | } |
| 2339 | } |
| 2340 | #endif |
| 2341 | /* Now that we know what is what, implement or table |
| 2342 | * this could in theory be done slicker (it used to be), but this |
| 2343 | * is straightforward and easier to validate :-) |
| 2344 | */ |
| 2345 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n", |
| 2346 | ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob); |
| 2347 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n", |
| 2348 | dest_is_loop, dest_is_priv, dest_is_global); |
| 2349 |
| 2350 | if ((ifa->src_is_loop) && (dest_is_priv)) { |
| 2351 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n"); |
| 2352 | return (NULL); |
| 2353 | } |
| 2354 | if ((ifa->src_is_glob) && (dest_is_priv)) { |
| 2355 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n"); |
| 2356 | return (NULL); |
| 2357 | } |
| 2358 | if ((ifa->src_is_loop) && (dest_is_global)) { |
| 2359 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n"); |
| 2360 | return (NULL); |
| 2361 | } |
| 2362 | if ((ifa->src_is_priv) && (dest_is_global)) { |
| 2363 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n"); |
| 2364 | return (NULL); |
| 2365 | } |
| 2366 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n"); |
| 2367 | /* its a preferred address */ |
| 2368 | return (ifa); |
| 2369 | } |
| 2370 |
| 2371 | static struct sctp_ifa * |
| 2372 | sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa, |
| 2373 | uint8_t dest_is_loop, |
| 2374 | uint8_t dest_is_priv, |
| 2375 | sa_family_t fam) |
| 2376 | { |
| 2377 | uint8_t dest_is_global = 0; |
| 2378 |
| 2379 | /** |
| 2380 | * Here we determine if its a acceptable address. A acceptable |
| 2381 | * address means it is the same scope or higher scope but we can |
| 2382 | * allow for NAT which means its ok to have a global dest and a |
| 2383 | * private src. |
| 2384 | * |
| 2385 | * L = loopback, P = private, G = global |
| 2386 | * ----------------------------------------- |
| 2387 | * src | dest | result |
| 2388 | * ----------------------------------------- |
| 2389 | * L | L | yes |
| 2390 | * ----------------------------------------- |
| 2391 | * P | L | yes-v4 no-v6 |
| 2392 | * ----------------------------------------- |
| 2393 | * G | L | yes |
| 2394 | * ----------------------------------------- |
| 2395 | * L | P | no |
| 2396 | * ----------------------------------------- |
| 2397 | * P | P | yes |
| 2398 | * ----------------------------------------- |
| 2399 | * G | P | yes - May not work |
| 2400 | * ----------------------------------------- |
| 2401 | * L | G | no |
| 2402 | * ----------------------------------------- |
| 2403 | * P | G | yes - May not work |
| 2404 | * ----------------------------------------- |
| 2405 | * G | G | yes |
| 2406 | * ----------------------------------------- |
| 2407 | */ |
| 2408 |
| 2409 | if (ifa->address.sa.sa_family != fam) { |
| 2410 | /* forget non matching family */ |
| 2411 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n", |
| 2412 | ifa->address.sa.sa_family, fam); |
| 2413 | return (NULL); |
| 2414 | } |
| 2415 | /* Ok the address may be ok */ |
| 2416 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa); |
| 2417 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n", |
| 2418 | dest_is_loop, dest_is_priv); |
| 2419 | if ((dest_is_loop == 0) && (dest_is_priv == 0)) { |
| 2420 | dest_is_global = 1; |
| 2421 | } |
| 2422 | #ifdef INET6 |
| 2423 | if (fam == AF_INET6) { |
| 2424 | /* ok to use deprecated addresses? */ |
| 2425 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
| 2426 | return (NULL); |
| 2427 | } |
| 2428 | if (ifa->src_is_priv) { |
| 2429 | /* Special case, linklocal to loop */ |
| 2430 | if (dest_is_loop) |
| 2431 | return (NULL); |
| 2432 | } |
| 2433 | } |
| 2434 | #endif |
| 2435 | /* |
| 2436 | * Now that we know what is what, implement our table. |
| 2437 | * This could in theory be done slicker (it used to be), but this |
| 2438 | * is straightforward and easier to validate :-) |
| 2439 | */ |
| 2440 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n", |
| 2441 | ifa->src_is_loop, |
| 2442 | dest_is_priv); |
| 2443 | if ((ifa->src_is_loop == 1) && (dest_is_priv)) { |
| 2444 | return (NULL); |
| 2445 | } |
| 2446 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n", |
| 2447 | ifa->src_is_loop, |
| 2448 | dest_is_global); |
| 2449 | if ((ifa->src_is_loop == 1) && (dest_is_global)) { |
| 2450 | return (NULL); |
| 2451 | } |
| 2452 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n"); |
| 2453 | /* its an acceptable address */ |
| 2454 | return (ifa); |
| 2455 | } |
| 2456 |
| 2457 | int |
| 2458 | sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) |
| 2459 | { |
| 2460 | struct sctp_laddr *laddr; |
| 2461 |
| 2462 | if (stcb == NULL) { |
| 2463 | /* There are no restrictions, no TCB :-) */ |
| 2464 | return (0); |
| 2465 | } |
| 2466 | LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) { |
| 2467 | if (laddr->ifa == NULL) { |
| 2468 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n", |
| 2469 | __func__); |
| 2470 | continue; |
| 2471 | } |
| 2472 | if (laddr->ifa == ifa) { |
| 2473 | /* Yes it is on the list */ |
| 2474 | return (1); |
| 2475 | } |
| 2476 | } |
| 2477 | return (0); |
| 2478 | } |
| 2479 |
| 2480 | int |
| 2481 | sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa) |
| 2482 | { |
| 2483 | struct sctp_laddr *laddr; |
| 2484 |
| 2485 | if (ifa == NULL) |
| 2486 | return (0); |
| 2487 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 2488 | if (laddr->ifa == NULL) { |
| 2489 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n", |
| 2490 | __func__); |
| 2491 | continue; |
| 2492 | } |
| 2493 | if ((laddr->ifa == ifa) && laddr->action == 0) |
| 2494 | /* same pointer */ |
| 2495 | return (1); |
| 2496 | } |
| 2497 | return (0); |
| 2498 | } |
| 2499 |
| 2500 | static struct sctp_ifa * |
| 2501 | sctp_choose_boundspecific_inp(struct sctp_inpcb *inp, |
| 2502 | sctp_route_t *ro, |
| 2503 | uint32_t vrf_id, |
| 2504 | int non_asoc_addr_ok, |
| 2505 | uint8_t dest_is_priv, |
| 2506 | uint8_t dest_is_loop, |
| 2507 | sa_family_t fam) |
| 2508 | { |
| 2509 | struct sctp_laddr *laddr, *starting_point; |
| 2510 | void *ifn; |
| 2511 | int resettotop = 0; |
| 2512 | struct sctp_ifn *sctp_ifn; |
| 2513 | struct sctp_ifa *sctp_ifa, *sifa; |
| 2514 | struct sctp_vrf *vrf; |
| 2515 | uint32_t ifn_index; |
| 2516 |
| 2517 | vrf = sctp_find_vrf(vrf_id); |
| 2518 | if (vrf == NULL) |
| 2519 | return (NULL); |
| 2520 |
| 2521 | ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro); |
| 2522 | ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro); |
| 2523 | sctp_ifn = sctp_find_ifn(ifn, ifn_index); |
| 2524 | /* |
| 2525 | * first question, is the ifn we will emit on in our list, if so, we |
| 2526 | * want such an address. Note that we first looked for a |
| 2527 | * preferred address. |
| 2528 | */ |
| 2529 | if (sctp_ifn) { |
| 2530 | /* is a preferred one on the interface we route out? */ |
| 2531 | LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { |
| 2532 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 2533 | #ifdef INET |
| 2534 | if ((sctp_ifa->address.sa.sa_family == AF_INET) && |
| 2535 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 2536 | &sctp_ifa->address.sin.sin_addr) != 0)) { |
| 2537 | continue; |
| 2538 | } |
| 2539 | #endif |
| 2540 | #ifdef INET6 |
| 2541 | if ((sctp_ifa->address.sa.sa_family == AF_INET6) && |
| 2542 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 2543 | &sctp_ifa->address.sin6.sin6_addr) != 0)) { |
| 2544 | continue; |
| 2545 | } |
| 2546 | #endif |
| 2547 | #endif |
| 2548 | if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && |
| 2549 | (non_asoc_addr_ok == 0)) |
| 2550 | continue; |
| 2551 | sifa = sctp_is_ifa_addr_preferred(sctp_ifa, |
| 2552 | dest_is_loop, |
| 2553 | dest_is_priv, fam); |
| 2554 | if (sifa == NULL) |
| 2555 | continue; |
| 2556 | if (sctp_is_addr_in_ep(inp, sifa)) { |
| 2557 | atomic_add_int(&sifa->refcount, 1); |
| 2558 | return (sifa); |
| 2559 | } |
| 2560 | } |
| 2561 | } |
| 2562 | /* |
| 2563 | * ok, now we now need to find one on the list of the addresses. |
| 2564 | * We can't get one on the emitting interface so let's find first |
| 2565 | * a preferred one. If not that an acceptable one otherwise... |
| 2566 | * we return NULL. |
| 2567 | */ |
| 2568 | starting_point = inp->next_addr_touse; |
| 2569 | once_again: |
| 2570 | if (inp->next_addr_touse == NULL) { |
| 2571 | inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list); |
| 2572 | resettotop = 1; |
| 2573 | } |
| 2574 | for (laddr = inp->next_addr_touse; laddr; |
| 2575 | laddr = LIST_NEXT(laddr, sctp_nxt_addr)) { |
| 2576 | if (laddr->ifa == NULL) { |
| 2577 | /* address has been removed */ |
| 2578 | continue; |
| 2579 | } |
| 2580 | if (laddr->action == SCTP_DEL_IP_ADDRESS) { |
| 2581 | /* address is being deleted */ |
| 2582 | continue; |
| 2583 | } |
| 2584 | sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, |
| 2585 | dest_is_priv, fam); |
| 2586 | if (sifa == NULL) |
| 2587 | continue; |
| 2588 | atomic_add_int(&sifa->refcount, 1); |
| 2589 | return (sifa); |
| 2590 | } |
| 2591 | if (resettotop == 0) { |
| 2592 | inp->next_addr_touse = NULL; |
| 2593 | goto once_again; |
| 2594 | } |
| 2595 |
| 2596 | inp->next_addr_touse = starting_point; |
| 2597 | resettotop = 0; |
| 2598 | once_again_too: |
| 2599 | if (inp->next_addr_touse == NULL) { |
| 2600 | inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list); |
| 2601 | resettotop = 1; |
| 2602 | } |
| 2603 |
| 2604 | /* ok, what about an acceptable address in the inp */ |
| 2605 | for (laddr = inp->next_addr_touse; laddr; |
| 2606 | laddr = LIST_NEXT(laddr, sctp_nxt_addr)) { |
| 2607 | if (laddr->ifa == NULL) { |
| 2608 | /* address has been removed */ |
| 2609 | continue; |
| 2610 | } |
| 2611 | if (laddr->action == SCTP_DEL_IP_ADDRESS) { |
| 2612 | /* address is being deleted */ |
| 2613 | continue; |
| 2614 | } |
| 2615 | sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop, |
| 2616 | dest_is_priv, fam); |
| 2617 | if (sifa == NULL) |
| 2618 | continue; |
| 2619 | atomic_add_int(&sifa->refcount, 1); |
| 2620 | return (sifa); |
| 2621 | } |
| 2622 | if (resettotop == 0) { |
| 2623 | inp->next_addr_touse = NULL; |
| 2624 | goto once_again_too; |
| 2625 | } |
| 2626 |
| 2627 | /* |
| 2628 | * no address bound can be a source for the destination we are in |
| 2629 | * trouble |
| 2630 | */ |
| 2631 | return (NULL); |
| 2632 | } |
| 2633 |
| 2634 | static struct sctp_ifa * |
| 2635 | sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp, |
| 2636 | struct sctp_tcb *stcb, |
| 2637 | sctp_route_t *ro, |
| 2638 | uint32_t vrf_id, |
| 2639 | uint8_t dest_is_priv, |
| 2640 | uint8_t dest_is_loop, |
| 2641 | int non_asoc_addr_ok, |
| 2642 | sa_family_t fam) |
| 2643 | { |
| 2644 | struct sctp_laddr *laddr, *starting_point; |
| 2645 | void *ifn; |
| 2646 | struct sctp_ifn *sctp_ifn; |
| 2647 | struct sctp_ifa *sctp_ifa, *sifa; |
| 2648 | uint8_t start_at_beginning = 0; |
| 2649 | struct sctp_vrf *vrf; |
| 2650 | uint32_t ifn_index; |
| 2651 |
| 2652 | /* |
| 2653 | * first question, is the ifn we will emit on in our list, if so, we |
| 2654 | * want that one. |
| 2655 | */ |
| 2656 | vrf = sctp_find_vrf(vrf_id); |
| 2657 | if (vrf == NULL) |
| 2658 | return (NULL); |
| 2659 |
| 2660 | ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro); |
| 2661 | ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro); |
| 2662 | sctp_ifn = sctp_find_ifn(ifn, ifn_index); |
| 2663 |
| 2664 | /* |
| 2665 | * first question, is the ifn we will emit on in our list? If so, |
| 2666 | * we want that one. First we look for a preferred. Second, we go |
| 2667 | * for an acceptable. |
| 2668 | */ |
| 2669 | if (sctp_ifn) { |
| 2670 | /* first try for a preferred address on the ep */ |
| 2671 | LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { |
| 2672 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 2673 | #ifdef INET |
| 2674 | if ((sctp_ifa->address.sa.sa_family == AF_INET) && |
| 2675 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 2676 | &sctp_ifa->address.sin.sin_addr) != 0)) { |
| 2677 | continue; |
| 2678 | } |
| 2679 | #endif |
| 2680 | #ifdef INET6 |
| 2681 | if ((sctp_ifa->address.sa.sa_family == AF_INET6) && |
| 2682 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 2683 | &sctp_ifa->address.sin6.sin6_addr) != 0)) { |
| 2684 | continue; |
| 2685 | } |
| 2686 | #endif |
| 2687 | #endif |
| 2688 | if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0)) |
| 2689 | continue; |
| 2690 | if (sctp_is_addr_in_ep(inp, sctp_ifa)) { |
| 2691 | sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam); |
| 2692 | if (sifa == NULL) |
| 2693 | continue; |
| 2694 | if (((non_asoc_addr_ok == 0) && |
| 2695 | (sctp_is_addr_restricted(stcb, sifa))) || |
| 2696 | (non_asoc_addr_ok && |
| 2697 | (sctp_is_addr_restricted(stcb, sifa)) && |
| 2698 | (!sctp_is_addr_pending(stcb, sifa)))) { |
| 2699 | /* on the no-no list */ |
| 2700 | continue; |
| 2701 | } |
| 2702 | atomic_add_int(&sifa->refcount, 1); |
| 2703 | return (sifa); |
| 2704 | } |
| 2705 | } |
| 2706 | /* next try for an acceptable address on the ep */ |
| 2707 | LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { |
| 2708 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 2709 | #ifdef INET |
| 2710 | if ((sctp_ifa->address.sa.sa_family == AF_INET) && |
| 2711 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 2712 | &sctp_ifa->address.sin.sin_addr) != 0)) { |
| 2713 | continue; |
| 2714 | } |
| 2715 | #endif |
| 2716 | #ifdef INET6 |
| 2717 | if ((sctp_ifa->address.sa.sa_family == AF_INET6) && |
| 2718 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 2719 | &sctp_ifa->address.sin6.sin6_addr) != 0)) { |
| 2720 | continue; |
| 2721 | } |
| 2722 | #endif |
| 2723 | #endif |
| 2724 | if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0)) |
| 2725 | continue; |
| 2726 | if (sctp_is_addr_in_ep(inp, sctp_ifa)) { |
| 2727 | sifa= sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv,fam); |
| 2728 | if (sifa == NULL) |
| 2729 | continue; |
| 2730 | if (((non_asoc_addr_ok == 0) && |
| 2731 | (sctp_is_addr_restricted(stcb, sifa))) || |
| 2732 | (non_asoc_addr_ok && |
| 2733 | (sctp_is_addr_restricted(stcb, sifa)) && |
| 2734 | (!sctp_is_addr_pending(stcb, sifa)))) { |
| 2735 | /* on the no-no list */ |
| 2736 | continue; |
| 2737 | } |
| 2738 | atomic_add_int(&sifa->refcount, 1); |
| 2739 | return (sifa); |
| 2740 | } |
| 2741 | } |
| 2742 | } |
| 2743 | /* |
| 2744 | * if we can't find one like that then we must look at all |
| 2745 | * addresses bound to pick one at first preferable then |
| 2746 | * secondly acceptable. |
| 2747 | */ |
| 2748 | starting_point = stcb->asoc.last_used_address; |
| 2749 | sctp_from_the_top: |
| 2750 | if (stcb->asoc.last_used_address == NULL) { |
| 2751 | start_at_beginning = 1; |
| 2752 | stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list); |
| 2753 | } |
| 2754 | /* search beginning with the last used address */ |
| 2755 | for (laddr = stcb->asoc.last_used_address; laddr; |
| 2756 | laddr = LIST_NEXT(laddr, sctp_nxt_addr)) { |
| 2757 | if (laddr->ifa == NULL) { |
| 2758 | /* address has been removed */ |
| 2759 | continue; |
| 2760 | } |
| 2761 | if (laddr->action == SCTP_DEL_IP_ADDRESS) { |
| 2762 | /* address is being deleted */ |
| 2763 | continue; |
| 2764 | } |
| 2765 | sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam); |
| 2766 | if (sifa == NULL) |
| 2767 | continue; |
| 2768 | if (((non_asoc_addr_ok == 0) && |
| 2769 | (sctp_is_addr_restricted(stcb, sifa))) || |
| 2770 | (non_asoc_addr_ok && |
| 2771 | (sctp_is_addr_restricted(stcb, sifa)) && |
| 2772 | (!sctp_is_addr_pending(stcb, sifa)))) { |
| 2773 | /* on the no-no list */ |
| 2774 | continue; |
| 2775 | } |
| 2776 | stcb->asoc.last_used_address = laddr; |
| 2777 | atomic_add_int(&sifa->refcount, 1); |
| 2778 | return (sifa); |
| 2779 | } |
| 2780 | if (start_at_beginning == 0) { |
| 2781 | stcb->asoc.last_used_address = NULL; |
| 2782 | goto sctp_from_the_top; |
| 2783 | } |
| 2784 | /* now try for any higher scope than the destination */ |
| 2785 | stcb->asoc.last_used_address = starting_point; |
| 2786 | start_at_beginning = 0; |
| 2787 | sctp_from_the_top2: |
| 2788 | if (stcb->asoc.last_used_address == NULL) { |
| 2789 | start_at_beginning = 1; |
| 2790 | stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list); |
| 2791 | } |
| 2792 | /* search beginning with the last used address */ |
| 2793 | for (laddr = stcb->asoc.last_used_address; laddr; |
| 2794 | laddr = LIST_NEXT(laddr, sctp_nxt_addr)) { |
| 2795 | if (laddr->ifa == NULL) { |
| 2796 | /* address has been removed */ |
| 2797 | continue; |
| 2798 | } |
| 2799 | if (laddr->action == SCTP_DEL_IP_ADDRESS) { |
| 2800 | /* address is being deleted */ |
| 2801 | continue; |
| 2802 | } |
| 2803 | sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop, |
| 2804 | dest_is_priv, fam); |
| 2805 | if (sifa == NULL) |
| 2806 | continue; |
| 2807 | if (((non_asoc_addr_ok == 0) && |
| 2808 | (sctp_is_addr_restricted(stcb, sifa))) || |
| 2809 | (non_asoc_addr_ok && |
| 2810 | (sctp_is_addr_restricted(stcb, sifa)) && |
| 2811 | (!sctp_is_addr_pending(stcb, sifa)))) { |
| 2812 | /* on the no-no list */ |
| 2813 | continue; |
| 2814 | } |
| 2815 | stcb->asoc.last_used_address = laddr; |
| 2816 | atomic_add_int(&sifa->refcount, 1); |
| 2817 | return (sifa); |
| 2818 | } |
| 2819 | if (start_at_beginning == 0) { |
| 2820 | stcb->asoc.last_used_address = NULL; |
| 2821 | goto sctp_from_the_top2; |
| 2822 | } |
| 2823 | return (NULL); |
| 2824 | } |
| 2825 |
| 2826 | static struct sctp_ifa * |
| 2827 | sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn, |
| 2828 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 2829 | struct sctp_inpcb *inp, |
| 2830 | #else |
| 2831 | struct sctp_inpcb *inp SCTP_UNUSED, |
| 2832 | #endif |
| 2833 | struct sctp_tcb *stcb, |
| 2834 | int non_asoc_addr_ok, |
| 2835 | uint8_t dest_is_loop, |
| 2836 | uint8_t dest_is_priv, |
| 2837 | int addr_wanted, |
| 2838 | sa_family_t fam, |
| 2839 | sctp_route_t *ro) |
| 2840 | { |
| 2841 | struct sctp_ifa *ifa, *sifa; |
| 2842 | int num_eligible_addr = 0; |
| 2843 | #ifdef INET6 |
| 2844 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 2845 | struct sockaddr_in6 sin6, lsa6; |
| 2846 |
| 2847 | if (fam == AF_INET6) { |
| 2848 | memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6)); |
| 2849 | #ifdef SCTP_KAME |
| 2850 | (void)sa6_recoverscope(&sin6); |
| 2851 | #else |
| 2852 | (void)in6_recoverscope(&sin6, &sin6.sin6_addr, NULL); |
| 2853 | #endif /* SCTP_KAME */ |
| 2854 | } |
| 2855 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 2856 | #endif /* INET6 */ |
| 2857 | LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) { |
| 2858 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 2859 | #ifdef INET |
| 2860 | if ((ifa->address.sa.sa_family == AF_INET) && |
| 2861 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 2862 | &ifa->address.sin.sin_addr) != 0)) { |
| 2863 | continue; |
| 2864 | } |
| 2865 | #endif |
| 2866 | #ifdef INET6 |
| 2867 | if ((ifa->address.sa.sa_family == AF_INET6) && |
| 2868 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 2869 | &ifa->address.sin6.sin6_addr) != 0)) { |
| 2870 | continue; |
| 2871 | } |
| 2872 | #endif |
| 2873 | #endif |
| 2874 | if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && |
| 2875 | (non_asoc_addr_ok == 0)) |
| 2876 | continue; |
| 2877 | sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop, |
| 2878 | dest_is_priv, fam); |
| 2879 | if (sifa == NULL) |
| 2880 | continue; |
| 2881 | #ifdef INET6 |
| 2882 | if (fam == AF_INET6 && |
| 2883 | dest_is_loop && |
| 2884 | sifa->src_is_loop && sifa->src_is_priv) { |
| 2885 | /* don't allow fe80::1 to be a src on loop ::1, we don't list it |
| 2886 | * to the peer so we will get an abort. |
| 2887 | */ |
| 2888 | continue; |
| 2889 | } |
| 2890 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 2891 | if (fam == AF_INET6 && |
| 2892 | IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) && |
| 2893 | IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) { |
| 2894 | /* link-local <-> link-local must belong to the same scope. */ |
| 2895 | memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6)); |
| 2896 | #ifdef SCTP_KAME |
| 2897 | (void)sa6_recoverscope(&lsa6); |
| 2898 | #else |
| 2899 | (void)in6_recoverscope(&lsa6, &lsa6.sin6_addr, NULL); |
| 2900 | #endif /* SCTP_KAME */ |
| 2901 | if (sin6.sin6_scope_id != lsa6.sin6_scope_id) { |
| 2902 | continue; |
| 2903 | } |
| 2904 | } |
| 2905 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 2906 | #endif /* INET6 */ |
| 2907 |
| 2908 | #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__) |
| 2909 | /* Check if the IPv6 address matches to next-hop. |
| 2910 | In the mobile case, old IPv6 address may be not deleted |
| 2911 | from the interface. Then, the interface has previous and |
| 2912 | new addresses. We should use one corresponding to the |
| 2913 | next-hop. (by micchie) |
| 2914 | */ |
| 2915 | #ifdef INET6 |
| 2916 | if (stcb && fam == AF_INET6 && |
| 2917 | sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) { |
| 2918 | if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro) |
| 2919 | == 0) { |
| 2920 | continue; |
| 2921 | } |
| 2922 | } |
| 2923 | #endif |
| 2924 | #ifdef INET |
| 2925 | /* Avoid topologically incorrect IPv4 address */ |
| 2926 | if (stcb && fam == AF_INET && |
| 2927 | sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) { |
| 2928 | if (sctp_v4src_match_nexthop(sifa, ro) == 0) { |
| 2929 | continue; |
| 2930 | } |
| 2931 | } |
| 2932 | #endif |
| 2933 | #endif |
| 2934 | if (stcb) { |
| 2935 | if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) { |
| 2936 | continue; |
| 2937 | } |
| 2938 | if (((non_asoc_addr_ok == 0) && |
| 2939 | (sctp_is_addr_restricted(stcb, sifa))) || |
| 2940 | (non_asoc_addr_ok && |
| 2941 | (sctp_is_addr_restricted(stcb, sifa)) && |
| 2942 | (!sctp_is_addr_pending(stcb, sifa)))) { |
| 2943 | /* |
| 2944 | * It is restricted for some reason.. |
| 2945 | * probably not yet added. |
| 2946 | */ |
| 2947 | continue; |
| 2948 | } |
| 2949 | } |
| 2950 | if (num_eligible_addr >= addr_wanted) { |
| 2951 | return (sifa); |
| 2952 | } |
| 2953 | num_eligible_addr++; |
| 2954 | } |
| 2955 | return (NULL); |
| 2956 | } |
| 2957 |
| 2958 | static int |
| 2959 | sctp_count_num_preferred_boundall(struct sctp_ifn *ifn, |
| 2960 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 2961 | struct sctp_inpcb *inp, |
| 2962 | #else |
| 2963 | struct sctp_inpcb *inp SCTP_UNUSED, |
| 2964 | #endif |
| 2965 | struct sctp_tcb *stcb, |
| 2966 | int non_asoc_addr_ok, |
| 2967 | uint8_t dest_is_loop, |
| 2968 | uint8_t dest_is_priv, |
| 2969 | sa_family_t fam) |
| 2970 | { |
| 2971 | struct sctp_ifa *ifa, *sifa; |
| 2972 | int num_eligible_addr = 0; |
| 2973 |
| 2974 | LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) { |
| 2975 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 2976 | #ifdef INET |
| 2977 | if ((ifa->address.sa.sa_family == AF_INET) && |
| 2978 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 2979 | &ifa->address.sin.sin_addr) != 0)) { |
| 2980 | continue; |
| 2981 | } |
| 2982 | #endif |
| 2983 | #ifdef INET6 |
| 2984 | if ((ifa->address.sa.sa_family == AF_INET6) && |
| 2985 | (stcb != NULL) && |
| 2986 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 2987 | &ifa->address.sin6.sin6_addr) != 0)) { |
| 2988 | continue; |
| 2989 | } |
| 2990 | #endif |
| 2991 | #endif |
| 2992 | if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && |
| 2993 | (non_asoc_addr_ok == 0)) { |
| 2994 | continue; |
| 2995 | } |
| 2996 | sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop, |
| 2997 | dest_is_priv, fam); |
| 2998 | if (sifa == NULL) { |
| 2999 | continue; |
| 3000 | } |
| 3001 | if (stcb) { |
| 3002 | if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) { |
| 3003 | continue; |
| 3004 | } |
| 3005 | if (((non_asoc_addr_ok == 0) && |
| 3006 | (sctp_is_addr_restricted(stcb, sifa))) || |
| 3007 | (non_asoc_addr_ok && |
| 3008 | (sctp_is_addr_restricted(stcb, sifa)) && |
| 3009 | (!sctp_is_addr_pending(stcb, sifa)))) { |
| 3010 | /* |
| 3011 | * It is restricted for some reason.. |
| 3012 | * probably not yet added. |
| 3013 | */ |
| 3014 | continue; |
| 3015 | } |
| 3016 | } |
| 3017 | num_eligible_addr++; |
| 3018 | } |
| 3019 | return (num_eligible_addr); |
| 3020 | } |
| 3021 |
| 3022 | static struct sctp_ifa * |
| 3023 | sctp_choose_boundall(struct sctp_inpcb *inp, |
| 3024 | struct sctp_tcb *stcb, |
| 3025 | struct sctp_nets *net, |
| 3026 | sctp_route_t *ro, |
| 3027 | uint32_t vrf_id, |
| 3028 | uint8_t dest_is_priv, |
| 3029 | uint8_t dest_is_loop, |
| 3030 | int non_asoc_addr_ok, |
| 3031 | sa_family_t fam) |
| 3032 | { |
| 3033 | int cur_addr_num = 0, num_preferred = 0; |
| 3034 | void *ifn; |
| 3035 | struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn; |
| 3036 | struct sctp_ifa *sctp_ifa, *sifa; |
| 3037 | uint32_t ifn_index; |
| 3038 | struct sctp_vrf *vrf; |
| 3039 | #ifdef INET |
| 3040 | int retried = 0; |
| 3041 | #endif |
| 3042 |
| 3043 | /*- |
| 3044 | * For boundall we can use any address in the association. |
| 3045 | * If non_asoc_addr_ok is set we can use any address (at least in |
| 3046 | * theory). So we look for preferred addresses first. If we find one, |
| 3047 | * we use it. Otherwise we next try to get an address on the |
| 3048 | * interface, which we should be able to do (unless non_asoc_addr_ok |
| 3049 | * is false and we are routed out that way). In these cases where we |
| 3050 | * can't use the address of the interface we go through all the |
| 3051 | * ifn's looking for an address we can use and fill that in. Punting |
| 3052 | * means we send back address 0, which will probably cause problems |
| 3053 | * actually since then IP will fill in the address of the route ifn, |
| 3054 | * which means we probably already rejected it.. i.e. here comes an |
| 3055 | * abort :-<. |
| 3056 | */ |
| 3057 | vrf = sctp_find_vrf(vrf_id); |
| 3058 | if (vrf == NULL) |
| 3059 | return (NULL); |
| 3060 |
| 3061 | ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro); |
| 3062 | ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro); |
| 3063 | SCTPDBG(SCTP_DEBUG_OUTPUT2,"ifn from route:%p ifn_index:%d\n", ifn, ifn_index); |
| 3064 | emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index); |
| 3065 | if (sctp_ifn == NULL) { |
| 3066 | /* ?? We don't have this guy ?? */ |
| 3067 | SCTPDBG(SCTP_DEBUG_OUTPUT2,"No ifn emit interface?\n"); |
| 3068 | goto bound_all_plan_b; |
| 3069 | } |
| 3070 | SCTPDBG(SCTP_DEBUG_OUTPUT2,"ifn_index:%d name:%s is emit interface\n", |
| 3071 | ifn_index, sctp_ifn->ifn_name); |
| 3072 |
| 3073 | if (net) { |
| 3074 | cur_addr_num = net->indx_of_eligible_next_to_use; |
| 3075 | } |
| 3076 | num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, |
| 3077 | inp, stcb, |
| 3078 | non_asoc_addr_ok, |
| 3079 | dest_is_loop, |
| 3080 | dest_is_priv, fam); |
| 3081 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n", |
| 3082 | num_preferred, sctp_ifn->ifn_name); |
| 3083 | if (num_preferred == 0) { |
| 3084 | /* |
| 3085 | * no eligible addresses, we must use some other interface |
| 3086 | * address if we can find one. |
| 3087 | */ |
| 3088 | goto bound_all_plan_b; |
| 3089 | } |
| 3090 | /* |
| 3091 | * Ok we have num_eligible_addr set with how many we can use, this |
| 3092 | * may vary from call to call due to addresses being deprecated |
| 3093 | * etc.. |
| 3094 | */ |
| 3095 | if (cur_addr_num >= num_preferred) { |
| 3096 | cur_addr_num = 0; |
| 3097 | } |
| 3098 | /* |
| 3099 | * select the nth address from the list (where cur_addr_num is the |
| 3100 | * nth) and 0 is the first one, 1 is the second one etc... |
| 3101 | */ |
| 3102 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num); |
| 3103 |
| 3104 | sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop, |
| 3105 | dest_is_priv, cur_addr_num, fam, ro); |
| 3106 |
| 3107 | /* if sctp_ifa is NULL something changed??, fall to plan b. */ |
| 3108 | if (sctp_ifa) { |
| 3109 | atomic_add_int(&sctp_ifa->refcount, 1); |
| 3110 | if (net) { |
| 3111 | /* save off where the next one we will want */ |
| 3112 | net->indx_of_eligible_next_to_use = cur_addr_num + 1; |
| 3113 | } |
| 3114 | return (sctp_ifa); |
| 3115 | } |
| 3116 | /* |
| 3117 | * plan_b: Look at all interfaces and find a preferred address. If |
| 3118 | * no preferred fall through to plan_c. |
| 3119 | */ |
| 3120 | bound_all_plan_b: |
| 3121 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n"); |
| 3122 | LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { |
| 3123 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n", |
| 3124 | sctp_ifn->ifn_name); |
| 3125 | if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { |
| 3126 | /* wrong base scope */ |
| 3127 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n"); |
| 3128 | continue; |
| 3129 | } |
| 3130 | if ((sctp_ifn == looked_at) && looked_at) { |
| 3131 | /* already looked at this guy */ |
| 3132 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n"); |
| 3133 | continue; |
| 3134 | } |
| 3135 | num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, |
| 3136 | dest_is_loop, dest_is_priv, fam); |
| 3137 | SCTPDBG(SCTP_DEBUG_OUTPUT2, |
| 3138 | "Found ifn:%p %d preferred source addresses\n", |
| 3139 | ifn, num_preferred); |
| 3140 | if (num_preferred == 0) { |
| 3141 | /* None on this interface. */ |
| 3142 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "No preferred -- skipping to next\n"); |
| 3143 | continue; |
| 3144 | } |
| 3145 | SCTPDBG(SCTP_DEBUG_OUTPUT2, |
| 3146 | "num preferred:%d on interface:%p cur_addr_num:%d\n", |
| 3147 | num_preferred, (void *)sctp_ifn, cur_addr_num); |
| 3148 |
| 3149 | /* |
| 3150 | * Ok we have num_eligible_addr set with how many we can |
| 3151 | * use, this may vary from call to call due to addresses |
| 3152 | * being deprecated etc.. |
| 3153 | */ |
| 3154 | if (cur_addr_num >= num_preferred) { |
| 3155 | cur_addr_num = 0; |
| 3156 | } |
| 3157 | sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop, |
| 3158 | dest_is_priv, cur_addr_num, fam, ro); |
| 3159 | if (sifa == NULL) |
| 3160 | continue; |
| 3161 | if (net) { |
| 3162 | net->indx_of_eligible_next_to_use = cur_addr_num + 1; |
| 3163 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n", |
| 3164 | cur_addr_num); |
| 3165 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:"); |
| 3166 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa); |
| 3167 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:"); |
| 3168 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa); |
| 3169 | } |
| 3170 | atomic_add_int(&sifa->refcount, 1); |
| 3171 | return (sifa); |
| 3172 | } |
| 3173 | #ifdef INET |
| 3174 | again_with_private_addresses_allowed: |
| 3175 | #endif |
| 3176 | /* plan_c: do we have an acceptable address on the emit interface */ |
| 3177 | sifa = NULL; |
| 3178 | SCTPDBG(SCTP_DEBUG_OUTPUT2,"Trying Plan C: find acceptable on interface\n"); |
| 3179 | if (emit_ifn == NULL) { |
| 3180 | SCTPDBG(SCTP_DEBUG_OUTPUT2,"Jump to Plan D - no emit_ifn\n"); |
| 3181 | goto plan_d; |
| 3182 | } |
| 3183 | LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) { |
| 3184 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", (void *)sctp_ifa); |
| 3185 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 3186 | #ifdef INET |
| 3187 | if ((sctp_ifa->address.sa.sa_family == AF_INET) && |
| 3188 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 3189 | &sctp_ifa->address.sin.sin_addr) != 0)) { |
| 3190 | SCTPDBG(SCTP_DEBUG_OUTPUT2,"Jailed\n"); |
| 3191 | continue; |
| 3192 | } |
| 3193 | #endif |
| 3194 | #ifdef INET6 |
| 3195 | if ((sctp_ifa->address.sa.sa_family == AF_INET6) && |
| 3196 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 3197 | &sctp_ifa->address.sin6.sin6_addr) != 0)) { |
| 3198 | SCTPDBG(SCTP_DEBUG_OUTPUT2,"Jailed\n"); |
| 3199 | continue; |
| 3200 | } |
| 3201 | #endif |
| 3202 | #endif |
| 3203 | if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && |
| 3204 | (non_asoc_addr_ok == 0)) { |
| 3205 | SCTPDBG(SCTP_DEBUG_OUTPUT2,"Defer\n"); |
| 3206 | continue; |
| 3207 | } |
| 3208 | sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, |
| 3209 | dest_is_priv, fam); |
| 3210 | if (sifa == NULL) { |
| 3211 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n"); |
| 3212 | continue; |
| 3213 | } |
| 3214 | if (stcb) { |
| 3215 | if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) { |
| 3216 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n"); |
| 3217 | sifa = NULL; |
| 3218 | continue; |
| 3219 | } |
| 3220 | if (((non_asoc_addr_ok == 0) && |
| 3221 | (sctp_is_addr_restricted(stcb, sifa))) || |
| 3222 | (non_asoc_addr_ok && |
| 3223 | (sctp_is_addr_restricted(stcb, sifa)) && |
| 3224 | (!sctp_is_addr_pending(stcb, sifa)))) { |
| 3225 | /* |
| 3226 | * It is restricted for some |
| 3227 | * reason.. probably not yet added. |
| 3228 | */ |
| 3229 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its restricted\n"); |
| 3230 | sifa = NULL; |
| 3231 | continue; |
| 3232 | } |
| 3233 | } |
| 3234 | atomic_add_int(&sifa->refcount, 1); |
| 3235 | goto out; |
| 3236 | } |
| 3237 | plan_d: |
| 3238 | /* |
| 3239 | * plan_d: We are in trouble. No preferred address on the emit |
| 3240 | * interface. And not even a preferred address on all interfaces. |
| 3241 | * Go out and see if we can find an acceptable address somewhere |
| 3242 | * amongst all interfaces. |
| 3243 | */ |
| 3244 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", (void *)looked_at); |
| 3245 | LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { |
| 3246 | if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { |
| 3247 | /* wrong base scope */ |
| 3248 | continue; |
| 3249 | } |
| 3250 | LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { |
| 3251 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 3252 | #ifdef INET |
| 3253 | if ((sctp_ifa->address.sa.sa_family == AF_INET) && |
| 3254 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 3255 | &sctp_ifa->address.sin.sin_addr) != 0)) { |
| 3256 | continue; |
| 3257 | } |
| 3258 | #endif |
| 3259 | #ifdef INET6 |
| 3260 | if ((sctp_ifa->address.sa.sa_family == AF_INET6) && |
| 3261 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 3262 | &sctp_ifa->address.sin6.sin6_addr) != 0)) { |
| 3263 | continue; |
| 3264 | } |
| 3265 | #endif |
| 3266 | #endif |
| 3267 | if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && |
| 3268 | (non_asoc_addr_ok == 0)) |
| 3269 | continue; |
| 3270 | sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, |
| 3271 | dest_is_loop, |
| 3272 | dest_is_priv, fam); |
| 3273 | if (sifa == NULL) |
| 3274 | continue; |
| 3275 | if (stcb) { |
| 3276 | if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) { |
| 3277 | sifa = NULL; |
| 3278 | continue; |
| 3279 | } |
| 3280 | if (((non_asoc_addr_ok == 0) && |
| 3281 | (sctp_is_addr_restricted(stcb, sifa))) || |
| 3282 | (non_asoc_addr_ok && |
| 3283 | (sctp_is_addr_restricted(stcb, sifa)) && |
| 3284 | (!sctp_is_addr_pending(stcb, sifa)))) { |
| 3285 | /* |
| 3286 | * It is restricted for some |
| 3287 | * reason.. probably not yet added. |
| 3288 | */ |
| 3289 | sifa = NULL; |
| 3290 | continue; |
| 3291 | } |
| 3292 | } |
| 3293 | goto out; |
| 3294 | } |
| 3295 | } |
| 3296 | #ifdef INET |
| 3297 | if (stcb) { |
| 3298 | if ((retried == 0) && (stcb->asoc.scope.ipv4_local_scope == 0)) { |
| 3299 | stcb->asoc.scope.ipv4_local_scope = 1; |
| 3300 | retried = 1; |
| 3301 | goto again_with_private_addresses_allowed; |
| 3302 | } else if (retried == 1) { |
| 3303 | stcb->asoc.scope.ipv4_local_scope = 0; |
| 3304 | } |
| 3305 | } |
| 3306 | #endif |
| 3307 | out: |
| 3308 | #ifdef INET |
| 3309 | if (sifa) { |
| 3310 | if (retried == 1) { |
| 3311 | LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { |
| 3312 | if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { |
| 3313 | /* wrong base scope */ |
| 3314 | continue; |
| 3315 | } |
| 3316 | LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { |
| 3317 | struct sctp_ifa *tmp_sifa; |
| 3318 |
| 3319 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 3320 | #ifdef INET |
| 3321 | if ((sctp_ifa->address.sa.sa_family == AF_INET) && |
| 3322 | (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 3323 | &sctp_ifa->address.sin.sin_addr) != 0)) { |
| 3324 | continue; |
| 3325 | } |
| 3326 | #endif |
| 3327 | #ifdef INET6 |
| 3328 | if ((sctp_ifa->address.sa.sa_family == AF_INET6) && |
| 3329 | (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 3330 | &sctp_ifa->address.sin6.sin6_addr) != 0)) { |
| 3331 | continue; |
| 3332 | } |
| 3333 | #endif |
| 3334 | #endif |
| 3335 | if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && |
| 3336 | (non_asoc_addr_ok == 0)) |
| 3337 | continue; |
| 3338 | tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, |
| 3339 | dest_is_loop, |
| 3340 | dest_is_priv, fam); |
| 3341 | if (tmp_sifa == NULL) { |
| 3342 | continue; |
| 3343 | } |
| 3344 | if (tmp_sifa == sifa) { |
| 3345 | continue; |
| 3346 | } |
| 3347 | if (stcb) { |
| 3348 | if (sctp_is_address_in_scope(tmp_sifa, |
| 3349 | &stcb->asoc.scope, 0) == 0) { |
| 3350 | continue; |
| 3351 | } |
| 3352 | if (((non_asoc_addr_ok == 0) && |
| 3353 | (sctp_is_addr_restricted(stcb, tmp_sifa))) || |
| 3354 | (non_asoc_addr_ok && |
| 3355 | (sctp_is_addr_restricted(stcb, tmp_sifa)) && |
| 3356 | (!sctp_is_addr_pending(stcb, tmp_sifa)))) { |
| 3357 | /* |
| 3358 | * It is restricted for some |
| 3359 | * reason.. probably not yet added. |
| 3360 | */ |
| 3361 | continue; |
| 3362 | } |
| 3363 | } |
| 3364 | if ((tmp_sifa->address.sin.sin_family == AF_INET) && |
| 3365 | (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) { |
| 3366 | sctp_add_local_addr_restricted(stcb, tmp_sifa); |
| 3367 | } |
| 3368 | } |
| 3369 | } |
| 3370 | } |
| 3371 | atomic_add_int(&sifa->refcount, 1); |
| 3372 | } |
| 3373 | #endif |
| 3374 | return (sifa); |
| 3375 | } |
| 3376 |
| 3377 | /* tcb may be NULL */ |
| 3378 | struct sctp_ifa * |
| 3379 | sctp_source_address_selection(struct sctp_inpcb *inp, |
| 3380 | struct sctp_tcb *stcb, |
| 3381 | sctp_route_t *ro, |
| 3382 | struct sctp_nets *net, |
| 3383 | int non_asoc_addr_ok, uint32_t vrf_id) |
| 3384 | { |
| 3385 | struct sctp_ifa *answer; |
| 3386 | uint8_t dest_is_priv, dest_is_loop; |
| 3387 | sa_family_t fam; |
| 3388 | #ifdef INET |
| 3389 | struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst; |
| 3390 | #endif |
| 3391 | #ifdef INET6 |
| 3392 | struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst; |
| 3393 | #endif |
| 3394 |
| 3395 | /** |
| 3396 | * Rules: |
| 3397 | * - Find the route if needed, cache if I can. |
| 3398 | * - Look at interface address in route, Is it in the bound list. If so we |
| 3399 | * have the best source. |
| 3400 | * - If not we must rotate amongst the addresses. |
| 3401 | * |
| 3402 | * Cavets and issues |
| 3403 | * |
| 3404 | * Do we need to pay attention to scope. We can have a private address |
| 3405 | * or a global address we are sourcing or sending to. So if we draw |
| 3406 | * it out |
| 3407 | * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz |
| 3408 | * For V4 |
| 3409 | * ------------------------------------------ |
| 3410 | * source * dest * result |
| 3411 | * ----------------------------------------- |
| 3412 | * <a> Private * Global * NAT |
| 3413 | * ----------------------------------------- |
| 3414 | * <b> Private * Private * No problem |
| 3415 | * ----------------------------------------- |
| 3416 | * <c> Global * Private * Huh, How will this work? |
| 3417 | * ----------------------------------------- |
| 3418 | * <d> Global * Global * No Problem |
| 3419 | *------------------------------------------ |
| 3420 | * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz |
| 3421 | * For V6 |
| 3422 | *------------------------------------------ |
| 3423 | * source * dest * result |
| 3424 | * ----------------------------------------- |
| 3425 | * <a> Linklocal * Global * |
| 3426 | * ----------------------------------------- |
| 3427 | * <b> Linklocal * Linklocal * No problem |
| 3428 | * ----------------------------------------- |
| 3429 | * <c> Global * Linklocal * Huh, How will this work? |
| 3430 | * ----------------------------------------- |
| 3431 | * <d> Global * Global * No Problem |
| 3432 | *------------------------------------------ |
| 3433 | * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz |
| 3434 | * |
| 3435 | * And then we add to that what happens if there are multiple addresses |
| 3436 | * assigned to an interface. Remember the ifa on a ifn is a linked |
| 3437 | * list of addresses. So one interface can have more than one IP |
| 3438 | * address. What happens if we have both a private and a global |
| 3439 | * address? Do we then use context of destination to sort out which |
| 3440 | * one is best? And what about NAT's sending P->G may get you a NAT |
| 3441 | * translation, or should you select the G thats on the interface in |
| 3442 | * preference. |
| 3443 | * |
| 3444 | * Decisions: |
| 3445 | * |
| 3446 | * - count the number of addresses on the interface. |
| 3447 | * - if it is one, no problem except case <c>. |
| 3448 | * For <a> we will assume a NAT out there. |
| 3449 | * - if there are more than one, then we need to worry about scope P |
| 3450 | * or G. We should prefer G -> G and P -> P if possible. |
| 3451 | * Then as a secondary fall back to mixed types G->P being a last |
| 3452 | * ditch one. |
| 3453 | * - The above all works for bound all, but bound specific we need to |
| 3454 | * use the same concept but instead only consider the bound |
| 3455 | * addresses. If the bound set is NOT assigned to the interface then |
| 3456 | * we must use rotation amongst the bound addresses.. |
| 3457 | */ |
| 3458 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 3459 | if (ro->ro_nh == NULL) { |
| 3460 | #else |
| 3461 | if (ro->ro_rt == NULL) { |
| 3462 | #endif |
| 3463 | /* |
| 3464 | * Need a route to cache. |
| 3465 | */ |
| 3466 | SCTP_RTALLOC(ro, vrf_id, inp->fibnum); |
| 3467 | } |
| 3468 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 3469 | if (ro->ro_nh == NULL) { |
| 3470 | #else |
| 3471 | if (ro->ro_rt == NULL) { |
| 3472 | #endif |
| 3473 | return (NULL); |
| 3474 | } |
| 3475 | #if defined(_WIN32) |
| 3476 | /* On Windows the sa_family is U_SHORT or ADDRESS_FAMILY */ |
| 3477 | fam = (sa_family_t)ro->ro_dst.sa_family; |
| 3478 | #else |
| 3479 | fam = ro->ro_dst.sa_family; |
| 3480 | #endif |
| 3481 | dest_is_priv = dest_is_loop = 0; |
| 3482 | /* Setup our scopes for the destination */ |
| 3483 | switch (fam) { |
| 3484 | #ifdef INET |
| 3485 | case AF_INET: |
| 3486 | /* Scope based on outbound address */ |
| 3487 | if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) { |
| 3488 | dest_is_loop = 1; |
| 3489 | if (net != NULL) { |
| 3490 | /* mark it as local */ |
| 3491 | net->addr_is_local = 1; |
| 3492 | } |
| 3493 | } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) { |
| 3494 | dest_is_priv = 1; |
| 3495 | } |
| 3496 | break; |
| 3497 | #endif |
| 3498 | #ifdef INET6 |
| 3499 | case AF_INET6: |
| 3500 | /* Scope based on outbound address */ |
| 3501 | #if defined(_WIN32) |
| 3502 | if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) { |
| 3503 | #else |
| 3504 | if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) || |
| 3505 | SCTP_ROUTE_IS_REAL_LOOP(ro)) { |
| 3506 | #endif |
| 3507 | /* |
| 3508 | * If the address is a loopback address, which |
| 3509 | * consists of "::1" OR "fe80::1%lo0", we are loopback |
| 3510 | * scope. But we don't use dest_is_priv (link local |
| 3511 | * addresses). |
| 3512 | */ |
| 3513 | dest_is_loop = 1; |
| 3514 | if (net != NULL) { |
| 3515 | /* mark it as local */ |
| 3516 | net->addr_is_local = 1; |
| 3517 | } |
| 3518 | } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) { |
| 3519 | dest_is_priv = 1; |
| 3520 | } |
| 3521 | break; |
| 3522 | #endif |
| 3523 | } |
| 3524 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:"); |
| 3525 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst); |
| 3526 | SCTP_IPI_ADDR_RLOCK(); |
| 3527 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
| 3528 | /* |
| 3529 | * Bound all case |
| 3530 | */ |
| 3531 | answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id, |
| 3532 | dest_is_priv, dest_is_loop, |
| 3533 | non_asoc_addr_ok, fam); |
| 3534 | SCTP_IPI_ADDR_RUNLOCK(); |
| 3535 | return (answer); |
| 3536 | } |
| 3537 | /* |
| 3538 | * Subset bound case |
| 3539 | */ |
| 3540 | if (stcb) { |
| 3541 | answer = sctp_choose_boundspecific_stcb(inp, stcb, ro, |
| 3542 | vrf_id, dest_is_priv, |
| 3543 | dest_is_loop, |
| 3544 | non_asoc_addr_ok, fam); |
| 3545 | } else { |
| 3546 | answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id, |
| 3547 | non_asoc_addr_ok, |
| 3548 | dest_is_priv, |
| 3549 | dest_is_loop, fam); |
| 3550 | } |
| 3551 | SCTP_IPI_ADDR_RUNLOCK(); |
| 3552 | return (answer); |
| 3553 | } |
| 3554 |
| 3555 | static int |
| 3556 | sctp_find_cmsg(int c_type, void *data, struct mbuf *control, size_t cpsize) |
| 3557 | { |
| 3558 | #if defined(_WIN32) |
| 3559 | WSACMSGHDR cmh; |
| 3560 | #else |
| 3561 | struct cmsghdr cmh; |
| 3562 | #endif |
| 3563 | struct sctp_sndinfo sndinfo; |
| 3564 | struct sctp_prinfo prinfo; |
| 3565 | struct sctp_authinfo authinfo; |
| 3566 | int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off; |
| 3567 | int found; |
| 3568 |
| 3569 | /* |
| 3570 | * Independent of how many mbufs, find the c_type inside the control |
| 3571 | * structure and copy out the data. |
| 3572 | */ |
| 3573 | found = 0; |
| 3574 | tot_len = SCTP_BUF_LEN(control); |
| 3575 | for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) { |
| 3576 | rem_len = tot_len - off; |
| 3577 | if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) { |
| 3578 | /* There is not enough room for one more. */ |
| 3579 | return (found); |
| 3580 | } |
| 3581 | m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh); |
| 3582 | if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) { |
| 3583 | /* We dont't have a complete CMSG header. */ |
| 3584 | return (found); |
| 3585 | } |
| 3586 | if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) { |
| 3587 | /* We don't have the complete CMSG. */ |
| 3588 | return (found); |
| 3589 | } |
| 3590 | cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh)); |
| 3591 | cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh)); |
| 3592 | if ((cmh.cmsg_level == IPPROTO_SCTP) && |
| 3593 | ((c_type == cmh.cmsg_type) || |
| 3594 | ((c_type == SCTP_SNDRCV) && |
| 3595 | ((cmh.cmsg_type == SCTP_SNDINFO) || |
| 3596 | (cmh.cmsg_type == SCTP_PRINFO) || |
| 3597 | (cmh.cmsg_type == SCTP_AUTHINFO))))) { |
| 3598 | if (c_type == cmh.cmsg_type) { |
| 3599 | if (cpsize > INT_MAX) { |
| 3600 | return (found); |
| 3601 | } |
| 3602 | if (cmsg_data_len < (int)cpsize) { |
| 3603 | return (found); |
| 3604 | } |
| 3605 | /* It is exactly what we want. Copy it out. */ |
| 3606 | m_copydata(control, cmsg_data_off, (int)cpsize, (caddr_t)data); |
| 3607 | return (1); |
| 3608 | } else { |
| 3609 | struct sctp_sndrcvinfo *sndrcvinfo; |
| 3610 |
| 3611 | sndrcvinfo = (struct sctp_sndrcvinfo *)data; |
| 3612 | if (found == 0) { |
| 3613 | if (cpsize < sizeof(struct sctp_sndrcvinfo)) { |
| 3614 | return (found); |
| 3615 | } |
| 3616 | memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo)); |
| 3617 | } |
| 3618 | switch (cmh.cmsg_type) { |
| 3619 | case SCTP_SNDINFO: |
| 3620 | if (cmsg_data_len < (int)sizeof(struct sctp_sndinfo)) { |
| 3621 | return (found); |
| 3622 | } |
| 3623 | m_copydata(control, cmsg_data_off, sizeof(struct sctp_sndinfo), (caddr_t)&sndinfo); |
| 3624 | sndrcvinfo->sinfo_stream = sndinfo.snd_sid; |
| 3625 | sndrcvinfo->sinfo_flags = sndinfo.snd_flags; |
| 3626 | sndrcvinfo->sinfo_ppid = sndinfo.snd_ppid; |
| 3627 | sndrcvinfo->sinfo_context = sndinfo.snd_context; |
| 3628 | sndrcvinfo->sinfo_assoc_id = sndinfo.snd_assoc_id; |
| 3629 | break; |
| 3630 | case SCTP_PRINFO: |
| 3631 | if (cmsg_data_len < (int)sizeof(struct sctp_prinfo)) { |
| 3632 | return (found); |
| 3633 | } |
| 3634 | m_copydata(control, cmsg_data_off, sizeof(struct sctp_prinfo), (caddr_t)&prinfo); |
| 3635 | if (prinfo.pr_policy != SCTP_PR_SCTP_NONE) { |
| 3636 | sndrcvinfo->sinfo_timetolive = prinfo.pr_value; |
| 3637 | } else { |
| 3638 | sndrcvinfo->sinfo_timetolive = 0; |
| 3639 | } |
| 3640 | sndrcvinfo->sinfo_flags |= prinfo.pr_policy; |
| 3641 | break; |
| 3642 | case SCTP_AUTHINFO: |
| 3643 | if (cmsg_data_len < (int)sizeof(struct sctp_authinfo)) { |
| 3644 | return (found); |
| 3645 | } |
| 3646 | m_copydata(control, cmsg_data_off, sizeof(struct sctp_authinfo), (caddr_t)&authinfo); |
| 3647 | sndrcvinfo->sinfo_keynumber_valid = 1; |
| 3648 | sndrcvinfo->sinfo_keynumber = authinfo.auth_keynumber; |
| 3649 | break; |
| 3650 | default: |
| 3651 | return (found); |
| 3652 | } |
| 3653 | found = 1; |
| 3654 | } |
| 3655 | } |
| 3656 | } |
| 3657 | return (found); |
| 3658 | } |
| 3659 |
| 3660 | static int |
| 3661 | sctp_process_cmsgs_for_init(struct sctp_tcb *stcb, struct mbuf *control, int *error) |
| 3662 | { |
| 3663 | #if defined(_WIN32) |
| 3664 | WSACMSGHDR cmh; |
| 3665 | #else |
| 3666 | struct cmsghdr cmh; |
| 3667 | #endif |
| 3668 | struct sctp_initmsg initmsg; |
| 3669 | #ifdef INET |
| 3670 | struct sockaddr_in sin; |
| 3671 | #endif |
| 3672 | #ifdef INET6 |
| 3673 | struct sockaddr_in6 sin6; |
| 3674 | #endif |
| 3675 | int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off; |
| 3676 |
| 3677 | tot_len = SCTP_BUF_LEN(control); |
| 3678 | for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) { |
| 3679 | rem_len = tot_len - off; |
| 3680 | if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) { |
| 3681 | /* There is not enough room for one more. */ |
| 3682 | *error = EINVAL; |
| 3683 | return (1); |
| 3684 | } |
| 3685 | m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh); |
| 3686 | if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) { |
| 3687 | /* We dont't have a complete CMSG header. */ |
| 3688 | *error = EINVAL; |
| 3689 | return (1); |
| 3690 | } |
| 3691 | if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) { |
| 3692 | /* We don't have the complete CMSG. */ |
| 3693 | *error = EINVAL; |
| 3694 | return (1); |
| 3695 | } |
| 3696 | cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh)); |
| 3697 | cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh)); |
| 3698 | if (cmh.cmsg_level == IPPROTO_SCTP) { |
| 3699 | switch (cmh.cmsg_type) { |
| 3700 | case SCTP_INIT: |
| 3701 | if (cmsg_data_len < (int)sizeof(struct sctp_initmsg)) { |
| 3702 | *error = EINVAL; |
| 3703 | return (1); |
| 3704 | } |
| 3705 | m_copydata(control, cmsg_data_off, sizeof(struct sctp_initmsg), (caddr_t)&initmsg); |
| 3706 | if (initmsg.sinit_max_attempts) |
| 3707 | stcb->asoc.max_init_times = initmsg.sinit_max_attempts; |
| 3708 | if (initmsg.sinit_num_ostreams) |
| 3709 | stcb->asoc.pre_open_streams = initmsg.sinit_num_ostreams; |
| 3710 | if (initmsg.sinit_max_instreams) |
| 3711 | stcb->asoc.max_inbound_streams = initmsg.sinit_max_instreams; |
| 3712 | if (initmsg.sinit_max_init_timeo) |
| 3713 | stcb->asoc.initial_init_rto_max = initmsg.sinit_max_init_timeo; |
| 3714 | if (stcb->asoc.streamoutcnt < stcb->asoc.pre_open_streams) { |
| 3715 | struct sctp_stream_out *tmp_str; |
| 3716 | unsigned int i; |
| 3717 | #if defined(SCTP_DETAILED_STR_STATS) |
| 3718 | int j; |
| 3719 | #endif |
| 3720 |
| 3721 | /* Default is NOT correct */ |
| 3722 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, default:%d pre_open:%d\n", |
| 3723 | stcb->asoc.streamoutcnt, stcb->asoc.pre_open_streams); |
| 3724 | SCTP_TCB_UNLOCK(stcb); |
| 3725 | SCTP_MALLOC(tmp_str, |
| 3726 | struct sctp_stream_out *, |
| 3727 | (stcb->asoc.pre_open_streams * sizeof(struct sctp_stream_out)), |
| 3728 | SCTP_M_STRMO); |
| 3729 | SCTP_TCB_LOCK(stcb); |
| 3730 | if (tmp_str != NULL) { |
| 3731 | SCTP_FREE(stcb->asoc.strmout, SCTP_M_STRMO); |
| 3732 | stcb->asoc.strmout = tmp_str; |
| 3733 | stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt = stcb->asoc.pre_open_streams; |
| 3734 | } else { |
| 3735 | stcb->asoc.pre_open_streams = stcb->asoc.streamoutcnt; |
| 3736 | } |
| 3737 | for (i = 0; i < stcb->asoc.streamoutcnt; i++) { |
| 3738 | TAILQ_INIT(&stcb->asoc.strmout[i].outqueue); |
| 3739 | stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL); |
| 3740 | stcb->asoc.strmout[i].chunks_on_queues = 0; |
| 3741 | #if defined(SCTP_DETAILED_STR_STATS) |
| 3742 | for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) { |
| 3743 | stcb->asoc.strmout[i].abandoned_sent[j] = 0; |
| 3744 | stcb->asoc.strmout[i].abandoned_unsent[j] = 0; |
| 3745 | } |
| 3746 | #else |
| 3747 | stcb->asoc.strmout[i].abandoned_sent[0] = 0; |
| 3748 | stcb->asoc.strmout[i].abandoned_unsent[0] = 0; |
| 3749 | #endif |
| 3750 | stcb->asoc.strmout[i].next_mid_ordered = 0; |
| 3751 | stcb->asoc.strmout[i].next_mid_unordered = 0; |
| 3752 | stcb->asoc.strmout[i].sid = i; |
| 3753 | stcb->asoc.strmout[i].last_msg_incomplete = 0; |
| 3754 | stcb->asoc.strmout[i].state = SCTP_STREAM_OPENING; |
| 3755 | } |
| 3756 | } |
| 3757 | break; |
| 3758 | #ifdef INET |
| 3759 | case SCTP_DSTADDRV4: |
| 3760 | if (cmsg_data_len < (int)sizeof(struct in_addr)) { |
| 3761 | *error = EINVAL; |
| 3762 | return (1); |
| 3763 | } |
| 3764 | memset(&sin, 0, sizeof(struct sockaddr_in)); |
| 3765 | sin.sin_family = AF_INET; |
| 3766 | #ifdef HAVE_SIN_LEN |
| 3767 | sin.sin_len = sizeof(struct sockaddr_in); |
| 3768 | #endif |
| 3769 | sin.sin_port = stcb->rport; |
| 3770 | m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr); |
| 3771 | if ((sin.sin_addr.s_addr == INADDR_ANY) || |
| 3772 | (sin.sin_addr.s_addr == INADDR_BROADCAST) || |
| 3773 | IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { |
| 3774 | *error = EINVAL; |
| 3775 | return (1); |
| 3776 | } |
| 3777 | if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port, |
| 3778 | SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { |
| 3779 | *error = ENOBUFS; |
| 3780 | return (1); |
| 3781 | } |
| 3782 | break; |
| 3783 | #endif |
| 3784 | #ifdef INET6 |
| 3785 | case SCTP_DSTADDRV6: |
| 3786 | if (cmsg_data_len < (int)sizeof(struct in6_addr)) { |
| 3787 | *error = EINVAL; |
| 3788 | return (1); |
| 3789 | } |
| 3790 | memset(&sin6, 0, sizeof(struct sockaddr_in6)); |
| 3791 | sin6.sin6_family = AF_INET6; |
| 3792 | #ifdef HAVE_SIN6_LEN |
| 3793 | sin6.sin6_len = sizeof(struct sockaddr_in6); |
| 3794 | #endif |
| 3795 | sin6.sin6_port = stcb->rport; |
| 3796 | m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr); |
| 3797 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) || |
| 3798 | IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) { |
| 3799 | *error = EINVAL; |
| 3800 | return (1); |
| 3801 | } |
| 3802 | #ifdef INET |
| 3803 | if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) { |
| 3804 | in6_sin6_2_sin(&sin, &sin6); |
| 3805 | if ((sin.sin_addr.s_addr == INADDR_ANY) || |
| 3806 | (sin.sin_addr.s_addr == INADDR_BROADCAST) || |
| 3807 | IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { |
| 3808 | *error = EINVAL; |
| 3809 | return (1); |
| 3810 | } |
| 3811 | if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port, |
| 3812 | SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { |
| 3813 | *error = ENOBUFS; |
| 3814 | return (1); |
| 3815 | } |
| 3816 | } else |
| 3817 | #endif |
| 3818 | if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin6, NULL, stcb->asoc.port, |
| 3819 | SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { |
| 3820 | *error = ENOBUFS; |
| 3821 | return (1); |
| 3822 | } |
| 3823 | break; |
| 3824 | #endif |
| 3825 | default: |
| 3826 | break; |
| 3827 | } |
| 3828 | } |
| 3829 | } |
| 3830 | return (0); |
| 3831 | } |
| 3832 |
| 3833 | #if defined(INET) || defined(INET6) |
| 3834 | static struct sctp_tcb * |
| 3835 | sctp_findassociation_cmsgs(struct sctp_inpcb **inp_p, |
| 3836 | uint16_t port, |
| 3837 | struct mbuf *control, |
| 3838 | struct sctp_nets **net_p, |
| 3839 | int *error) |
| 3840 | { |
| 3841 | #if defined(_WIN32) |
| 3842 | WSACMSGHDR cmh; |
| 3843 | #else |
| 3844 | struct cmsghdr cmh; |
| 3845 | #endif |
| 3846 | struct sctp_tcb *stcb; |
| 3847 | struct sockaddr *addr; |
| 3848 | #ifdef INET |
| 3849 | struct sockaddr_in sin; |
| 3850 | #endif |
| 3851 | #ifdef INET6 |
| 3852 | struct sockaddr_in6 sin6; |
| 3853 | #endif |
| 3854 | int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off; |
| 3855 |
| 3856 | tot_len = SCTP_BUF_LEN(control); |
| 3857 | for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) { |
| 3858 | rem_len = tot_len - off; |
| 3859 | if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) { |
| 3860 | /* There is not enough room for one more. */ |
| 3861 | *error = EINVAL; |
| 3862 | return (NULL); |
| 3863 | } |
| 3864 | m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh); |
| 3865 | if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) { |
| 3866 | /* We dont't have a complete CMSG header. */ |
| 3867 | *error = EINVAL; |
| 3868 | return (NULL); |
| 3869 | } |
| 3870 | if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) { |
| 3871 | /* We don't have the complete CMSG. */ |
| 3872 | *error = EINVAL; |
| 3873 | return (NULL); |
| 3874 | } |
| 3875 | cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh)); |
| 3876 | cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh)); |
| 3877 | if (cmh.cmsg_level == IPPROTO_SCTP) { |
| 3878 | switch (cmh.cmsg_type) { |
| 3879 | #ifdef INET |
| 3880 | case SCTP_DSTADDRV4: |
| 3881 | if (cmsg_data_len < (int)sizeof(struct in_addr)) { |
| 3882 | *error = EINVAL; |
| 3883 | return (NULL); |
| 3884 | } |
| 3885 | memset(&sin, 0, sizeof(struct sockaddr_in)); |
| 3886 | sin.sin_family = AF_INET; |
| 3887 | #ifdef HAVE_SIN_LEN |
| 3888 | sin.sin_len = sizeof(struct sockaddr_in); |
| 3889 | #endif |
| 3890 | sin.sin_port = port; |
| 3891 | m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr); |
| 3892 | addr = (struct sockaddr *)&sin; |
| 3893 | break; |
| 3894 | #endif |
| 3895 | #ifdef INET6 |
| 3896 | case SCTP_DSTADDRV6: |
| 3897 | if (cmsg_data_len < (int)sizeof(struct in6_addr)) { |
| 3898 | *error = EINVAL; |
| 3899 | return (NULL); |
| 3900 | } |
| 3901 | memset(&sin6, 0, sizeof(struct sockaddr_in6)); |
| 3902 | sin6.sin6_family = AF_INET6; |
| 3903 | #ifdef HAVE_SIN6_LEN |
| 3904 | sin6.sin6_len = sizeof(struct sockaddr_in6); |
| 3905 | #endif |
| 3906 | sin6.sin6_port = port; |
| 3907 | m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr); |
| 3908 | #ifdef INET |
| 3909 | if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) { |
| 3910 | in6_sin6_2_sin(&sin, &sin6); |
| 3911 | addr = (struct sockaddr *)&sin; |
| 3912 | } else |
| 3913 | #endif |
| 3914 | addr = (struct sockaddr *)&sin6; |
| 3915 | break; |
| 3916 | #endif |
| 3917 | default: |
| 3918 | addr = NULL; |
| 3919 | break; |
| 3920 | } |
| 3921 | if (addr) { |
| 3922 | stcb = sctp_findassociation_ep_addr(inp_p, addr, net_p, NULL, NULL); |
| 3923 | if (stcb != NULL) { |
| 3924 | return (stcb); |
| 3925 | } |
| 3926 | } |
| 3927 | } |
| 3928 | } |
| 3929 | return (NULL); |
| 3930 | } |
| 3931 | #endif |
| 3932 |
| 3933 | static struct mbuf * |
| 3934 | sctp_add_cookie(struct mbuf *init, int init_offset, |
| 3935 | struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t **signature) |
| 3936 | { |
| 3937 | struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret; |
| 3938 | struct sctp_state_cookie *stc; |
| 3939 | struct sctp_paramhdr *ph; |
| 3940 | uint16_t cookie_sz; |
| 3941 |
| 3942 | mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) + |
| 3943 | sizeof(struct sctp_paramhdr)), 0, |
| 3944 | M_NOWAIT, 1, MT_DATA); |
| 3945 | if (mret == NULL) { |
| 3946 | return (NULL); |
| 3947 | } |
| 3948 | copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_NOWAIT); |
| 3949 | if (copy_init == NULL) { |
| 3950 | sctp_m_freem(mret); |
| 3951 | return (NULL); |
| 3952 | } |
| 3953 | #ifdef SCTP_MBUF_LOGGING |
| 3954 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { |
| 3955 | sctp_log_mbc(copy_init, SCTP_MBUF_ICOPY); |
| 3956 | } |
| 3957 | #endif |
| 3958 | copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL, |
| 3959 | M_NOWAIT); |
| 3960 | if (copy_initack == NULL) { |
| 3961 | sctp_m_freem(mret); |
| 3962 | sctp_m_freem(copy_init); |
| 3963 | return (NULL); |
| 3964 | } |
| 3965 | #ifdef SCTP_MBUF_LOGGING |
| 3966 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { |
| 3967 | sctp_log_mbc(copy_initack, SCTP_MBUF_ICOPY); |
| 3968 | } |
| 3969 | #endif |
| 3970 | /* easy side we just drop it on the end */ |
| 3971 | ph = mtod(mret, struct sctp_paramhdr *); |
| 3972 | SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) + |
| 3973 | sizeof(struct sctp_paramhdr); |
| 3974 | stc = (struct sctp_state_cookie *)((caddr_t)ph + |
| 3975 | sizeof(struct sctp_paramhdr)); |
| 3976 | ph->param_type = htons(SCTP_STATE_COOKIE); |
| 3977 | ph->param_length = 0; /* fill in at the end */ |
| 3978 | /* Fill in the stc cookie data */ |
| 3979 | memcpy(stc, stc_in, sizeof(struct sctp_state_cookie)); |
| 3980 |
| 3981 | /* tack the INIT and then the INIT-ACK onto the chain */ |
| 3982 | cookie_sz = 0; |
| 3983 | for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) { |
| 3984 | cookie_sz += SCTP_BUF_LEN(m_at); |
| 3985 | if (SCTP_BUF_NEXT(m_at) == NULL) { |
| 3986 | SCTP_BUF_NEXT(m_at) = copy_init; |
| 3987 | break; |
| 3988 | } |
| 3989 | } |
| 3990 | for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) { |
| 3991 | cookie_sz += SCTP_BUF_LEN(m_at); |
| 3992 | if (SCTP_BUF_NEXT(m_at) == NULL) { |
| 3993 | SCTP_BUF_NEXT(m_at) = copy_initack; |
| 3994 | break; |
| 3995 | } |
| 3996 | } |
| 3997 | for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) { |
| 3998 | cookie_sz += SCTP_BUF_LEN(m_at); |
| 3999 | if (SCTP_BUF_NEXT(m_at) == NULL) { |
| 4000 | break; |
| 4001 | } |
| 4002 | } |
| 4003 | sig = sctp_get_mbuf_for_msg(SCTP_SIGNATURE_SIZE, 0, M_NOWAIT, 1, MT_DATA); |
| 4004 | if (sig == NULL) { |
| 4005 | /* no space, so free the entire chain */ |
| 4006 | sctp_m_freem(mret); |
| 4007 | return (NULL); |
| 4008 | } |
| 4009 | SCTP_BUF_NEXT(m_at) = sig; |
| 4010 | SCTP_BUF_LEN(sig) = SCTP_SIGNATURE_SIZE; |
| 4011 | cookie_sz += SCTP_SIGNATURE_SIZE; |
| 4012 | ph->param_length = htons(cookie_sz); |
| 4013 | *signature = (uint8_t *)mtod(sig, caddr_t); |
| 4014 | memset(*signature, 0, SCTP_SIGNATURE_SIZE); |
| 4015 | return (mret); |
| 4016 | } |
| 4017 |
| 4018 | static uint8_t |
| 4019 | sctp_get_ect(struct sctp_tcb *stcb) |
| 4020 | { |
| 4021 | if ((stcb != NULL) && (stcb->asoc.ecn_supported == 1)) { |
| 4022 | return (SCTP_ECT0_BIT); |
| 4023 | } else { |
| 4024 | return (0); |
| 4025 | } |
| 4026 | } |
| 4027 |
| 4028 | #if defined(INET) || defined(INET6) |
| 4029 | static void |
| 4030 | sctp_handle_no_route(struct sctp_tcb *stcb, |
| 4031 | struct sctp_nets *net, |
| 4032 | int so_locked) |
| 4033 | { |
| 4034 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n"); |
| 4035 |
| 4036 | if (net) { |
| 4037 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was "); |
| 4038 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa); |
| 4039 | if (net->dest_state & SCTP_ADDR_CONFIRMED) { |
| 4040 | if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) { |
| 4041 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", (void *)net); |
| 4042 | sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, |
| 4043 | stcb, 0, |
| 4044 | (void *)net, |
| 4045 | so_locked); |
| 4046 | net->dest_state &= ~SCTP_ADDR_REACHABLE; |
| 4047 | net->dest_state &= ~SCTP_ADDR_PF; |
| 4048 | } |
| 4049 | } |
| 4050 | if (stcb) { |
| 4051 | if (net == stcb->asoc.primary_destination) { |
| 4052 | /* need a new primary */ |
| 4053 | struct sctp_nets *alt; |
| 4054 |
| 4055 | alt = sctp_find_alternate_net(stcb, net, 0); |
| 4056 | if (alt != net) { |
| 4057 | if (stcb->asoc.alternate) { |
| 4058 | sctp_free_remote_addr(stcb->asoc.alternate); |
| 4059 | } |
| 4060 | stcb->asoc.alternate = alt; |
| 4061 | atomic_add_int(&stcb->asoc.alternate->ref_count, 1); |
| 4062 | if (net->ro._s_addr) { |
| 4063 | sctp_free_ifa(net->ro._s_addr); |
| 4064 | net->ro._s_addr = NULL; |
| 4065 | } |
| 4066 | net->src_addr_selected = 0; |
| 4067 | } |
| 4068 | } |
| 4069 | } |
| 4070 | } |
| 4071 | } |
| 4072 | #endif |
| 4073 |
| 4074 | static int |
| 4075 | sctp_lowlevel_chunk_output(struct sctp_inpcb *inp, |
| 4076 | struct sctp_tcb *stcb, /* may be NULL */ |
| 4077 | struct sctp_nets *net, |
| 4078 | struct sockaddr *to, |
| 4079 | struct mbuf *m, |
| 4080 | uint32_t auth_offset, |
| 4081 | struct sctp_auth_chunk *auth, |
| 4082 | uint16_t auth_keyid, |
| 4083 | int nofragment_flag, |
| 4084 | int ecn_ok, |
| 4085 | int out_of_asoc_ok, |
| 4086 | uint16_t src_port, |
| 4087 | uint16_t dest_port, |
| 4088 | uint32_t v_tag, |
| 4089 | uint16_t port, |
| 4090 | union sctp_sockstore *over_addr, |
| 4091 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4092 | uint8_t mflowtype, uint32_t mflowid, |
| 4093 | #endif |
| 4094 | int so_locked) |
| 4095 | /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */ |
| 4096 | { |
| 4097 | /** |
| 4098 | * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet header |
| 4099 | * WITH an SCTPHDR but no IP header, endpoint inp and sa structure: |
| 4100 | * - fill in the HMAC digest of any AUTH chunk in the packet. |
| 4101 | * - calculate and fill in the SCTP checksum. |
| 4102 | * - prepend an IP address header. |
| 4103 | * - if boundall use INADDR_ANY. |
| 4104 | * - if boundspecific do source address selection. |
| 4105 | * - set fragmentation option for ipV4. |
| 4106 | * - On return from IP output, check/adjust mtu size of output |
| 4107 | * interface and smallest_mtu size as well. |
| 4108 | */ |
| 4109 | /* Will need ifdefs around this */ |
| 4110 | struct mbuf *newm; |
| 4111 | struct sctphdr *sctphdr; |
| 4112 | int packet_length; |
| 4113 | int ret; |
| 4114 | #if defined(INET) || defined(INET6) |
| 4115 | uint32_t vrf_id; |
| 4116 | #endif |
| 4117 | #if defined(INET) || defined(INET6) |
| 4118 | struct mbuf *o_pak; |
| 4119 | sctp_route_t *ro = NULL; |
| 4120 | struct udphdr *udp = NULL; |
| 4121 | #endif |
| 4122 | uint8_t tos_value; |
| 4123 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 4124 | struct socket *so = NULL; |
| 4125 | #endif |
| 4126 |
| 4127 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 4128 | if (so_locked) { |
| 4129 | sctp_lock_assert(SCTP_INP_SO(inp)); |
| 4130 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 4131 | } else { |
| 4132 | sctp_unlock_assert(SCTP_INP_SO(inp)); |
| 4133 | } |
| 4134 | #endif |
| 4135 | if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) { |
| 4136 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT); |
| 4137 | sctp_m_freem(m); |
| 4138 | return (EFAULT); |
| 4139 | } |
| 4140 | #if defined(INET) || defined(INET6) |
| 4141 | if (stcb) { |
| 4142 | vrf_id = stcb->asoc.vrf_id; |
| 4143 | } else { |
| 4144 | vrf_id = inp->def_vrf_id; |
| 4145 | } |
| 4146 | #endif |
| 4147 | /* fill in the HMAC digest for any AUTH chunk in the packet */ |
| 4148 | if ((auth != NULL) && (stcb != NULL)) { |
| 4149 | sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid); |
| 4150 | } |
| 4151 |
| 4152 | if (net) { |
| 4153 | tos_value = net->dscp; |
| 4154 | } else if (stcb) { |
| 4155 | tos_value = stcb->asoc.default_dscp; |
| 4156 | } else { |
| 4157 | tos_value = inp->sctp_ep.default_dscp; |
| 4158 | } |
| 4159 |
| 4160 | switch (to->sa_family) { |
| 4161 | #ifdef INET |
| 4162 | case AF_INET: |
| 4163 | { |
| 4164 | struct ip *ip = NULL; |
| 4165 | sctp_route_t iproute; |
| 4166 | int len; |
| 4167 |
| 4168 | len = SCTP_MIN_V4_OVERHEAD; |
| 4169 | if (port) { |
| 4170 | len += sizeof(struct udphdr); |
| 4171 | } |
| 4172 | newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA); |
| 4173 | if (newm == NULL) { |
| 4174 | sctp_m_freem(m); |
| 4175 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 4176 | return (ENOMEM); |
| 4177 | } |
| 4178 | SCTP_ALIGN_TO_END(newm, len); |
| 4179 | SCTP_BUF_LEN(newm) = len; |
| 4180 | SCTP_BUF_NEXT(newm) = m; |
| 4181 | m = newm; |
| 4182 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4183 | if (net != NULL) { |
| 4184 | m->m_pkthdr.flowid = net->flowid; |
| 4185 | M_HASHTYPE_SET(m, net->flowtype); |
| 4186 | } else { |
| 4187 | m->m_pkthdr.flowid = mflowid; |
| 4188 | M_HASHTYPE_SET(m, mflowtype); |
| 4189 | } |
| 4190 | #endif |
| 4191 | packet_length = sctp_calculate_len(m); |
| 4192 | ip = mtod(m, struct ip *); |
| 4193 | ip->ip_v = IPVERSION; |
| 4194 | ip->ip_hl = (sizeof(struct ip) >> 2); |
| 4195 | if (tos_value == 0) { |
| 4196 | /* |
| 4197 | * This means especially, that it is not set at the |
| 4198 | * SCTP layer. So use the value from the IP layer. |
| 4199 | */ |
| 4200 | tos_value = inp->ip_inp.inp.inp_ip_tos; |
| 4201 | } |
| 4202 | tos_value &= 0xfc; |
| 4203 | if (ecn_ok) { |
| 4204 | tos_value |= sctp_get_ect(stcb); |
| 4205 | } |
| 4206 | if ((nofragment_flag) && (port == 0)) { |
| 4207 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4208 | ip->ip_off = htons(IP_DF); |
| 4209 | #elif defined(WITH_CONVERT_IP_OFF) || defined(__APPLE__) |
| 4210 | ip->ip_off = IP_DF; |
| 4211 | #else |
| 4212 | ip->ip_off = htons(IP_DF); |
| 4213 | #endif |
| 4214 | } else { |
| 4215 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4216 | ip->ip_off = htons(0); |
| 4217 | #else |
| 4218 | ip->ip_off = 0; |
| 4219 | #endif |
| 4220 | } |
| 4221 | #if defined(__Userspace__) |
| 4222 | ip->ip_id = htons(SCTP_IP_ID(inp)++); |
| 4223 | #elif defined(__FreeBSD__) |
| 4224 | /* FreeBSD has a function for ip_id's */ |
| 4225 | ip_fillid(ip); |
| 4226 | #elif defined(__APPLE__) |
| 4227 | #if RANDOM_IP_ID |
| 4228 | ip->ip_id = ip_randomid(); |
| 4229 | #else |
| 4230 | ip->ip_id = htons(ip_id++); |
| 4231 | #endif |
| 4232 | #else |
| 4233 | ip->ip_id = SCTP_IP_ID(inp)++; |
| 4234 | #endif |
| 4235 |
| 4236 | ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl; |
| 4237 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4238 | ip->ip_len = htons(packet_length); |
| 4239 | #else |
| 4240 | ip->ip_len = packet_length; |
| 4241 | #endif |
| 4242 | ip->ip_tos = tos_value; |
| 4243 | if (port) { |
| 4244 | ip->ip_p = IPPROTO_UDP; |
| 4245 | } else { |
| 4246 | ip->ip_p = IPPROTO_SCTP; |
| 4247 | } |
| 4248 | ip->ip_sum = 0; |
| 4249 | if (net == NULL) { |
| 4250 | ro = &iproute; |
| 4251 | memset(&iproute, 0, sizeof(iproute)); |
| 4252 | #ifdef HAVE_SA_LEN |
| 4253 | memcpy(&ro->ro_dst, to, to->sa_len); |
| 4254 | #else |
| 4255 | memcpy(&ro->ro_dst, to, sizeof(struct sockaddr_in)); |
| 4256 | #endif |
| 4257 | } else { |
| 4258 | ro = (sctp_route_t *)&net->ro; |
| 4259 | } |
| 4260 | /* Now the address selection part */ |
| 4261 | ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr; |
| 4262 |
| 4263 | /* call the routine to select the src address */ |
| 4264 | if (net && out_of_asoc_ok == 0) { |
| 4265 | if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED|SCTP_ADDR_IFA_UNUSEABLE))) { |
| 4266 | sctp_free_ifa(net->ro._s_addr); |
| 4267 | net->ro._s_addr = NULL; |
| 4268 | net->src_addr_selected = 0; |
| 4269 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4270 | RO_NHFREE(ro); |
| 4271 | #else |
| 4272 | if (ro->ro_rt) { |
| 4273 | RTFREE(ro->ro_rt); |
| 4274 | ro->ro_rt = NULL; |
| 4275 | } |
| 4276 | #endif |
| 4277 | } |
| 4278 | if (net->src_addr_selected == 0) { |
| 4279 | /* Cache the source address */ |
| 4280 | net->ro._s_addr = sctp_source_address_selection(inp,stcb, |
| 4281 | ro, net, 0, |
| 4282 | vrf_id); |
| 4283 | net->src_addr_selected = 1; |
| 4284 | } |
| 4285 | if (net->ro._s_addr == NULL) { |
| 4286 | /* No route to host */ |
| 4287 | net->src_addr_selected = 0; |
| 4288 | sctp_handle_no_route(stcb, net, so_locked); |
| 4289 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); |
| 4290 | sctp_m_freem(m); |
| 4291 | return (EHOSTUNREACH); |
| 4292 | } |
| 4293 | ip->ip_src = net->ro._s_addr->address.sin.sin_addr; |
| 4294 | } else { |
| 4295 | if (over_addr == NULL) { |
| 4296 | struct sctp_ifa *_lsrc; |
| 4297 |
| 4298 | _lsrc = sctp_source_address_selection(inp, stcb, ro, |
| 4299 | net, |
| 4300 | out_of_asoc_ok, |
| 4301 | vrf_id); |
| 4302 | if (_lsrc == NULL) { |
| 4303 | sctp_handle_no_route(stcb, net, so_locked); |
| 4304 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); |
| 4305 | sctp_m_freem(m); |
| 4306 | return (EHOSTUNREACH); |
| 4307 | } |
| 4308 | ip->ip_src = _lsrc->address.sin.sin_addr; |
| 4309 | sctp_free_ifa(_lsrc); |
| 4310 | } else { |
| 4311 | ip->ip_src = over_addr->sin.sin_addr; |
| 4312 | SCTP_RTALLOC(ro, vrf_id, inp->fibnum); |
| 4313 | } |
| 4314 | } |
| 4315 | if (port) { |
| 4316 | if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) { |
| 4317 | sctp_handle_no_route(stcb, net, so_locked); |
| 4318 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); |
| 4319 | sctp_m_freem(m); |
| 4320 | return (EHOSTUNREACH); |
| 4321 | } |
| 4322 | udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip)); |
| 4323 | udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)); |
| 4324 | udp->uh_dport = port; |
| 4325 | udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip))); |
| 4326 | #if !defined(__Userspace__) |
| 4327 | #if defined(__FreeBSD__) |
| 4328 | if (V_udp_cksum) { |
| 4329 | udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP)); |
| 4330 | } else { |
| 4331 | udp->uh_sum = 0; |
| 4332 | } |
| 4333 | #else |
| 4334 | udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP)); |
| 4335 | #endif |
| 4336 | #else |
| 4337 | udp->uh_sum = 0; |
| 4338 | #endif |
| 4339 | sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr)); |
| 4340 | } else { |
| 4341 | sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip)); |
| 4342 | } |
| 4343 |
| 4344 | sctphdr->src_port = src_port; |
| 4345 | sctphdr->dest_port = dest_port; |
| 4346 | sctphdr->v_tag = v_tag; |
| 4347 | sctphdr->checksum = 0; |
| 4348 |
| 4349 | /* |
| 4350 | * If source address selection fails and we find no route |
| 4351 | * then the ip_output should fail as well with a |
| 4352 | * NO_ROUTE_TO_HOST type error. We probably should catch |
| 4353 | * that somewhere and abort the association right away |
| 4354 | * (assuming this is an INIT being sent). |
| 4355 | */ |
| 4356 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4357 | if (ro->ro_nh == NULL) { |
| 4358 | #else |
| 4359 | if (ro->ro_rt == NULL) { |
| 4360 | #endif |
| 4361 | /* |
| 4362 | * src addr selection failed to find a route (or |
| 4363 | * valid source addr), so we can't get there from |
| 4364 | * here (yet)! |
| 4365 | */ |
| 4366 | sctp_handle_no_route(stcb, net, so_locked); |
| 4367 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); |
| 4368 | sctp_m_freem(m); |
| 4369 | return (EHOSTUNREACH); |
| 4370 | } |
| 4371 | if (ro != &iproute) { |
| 4372 | memcpy(&iproute, ro, sizeof(*ro)); |
| 4373 | } |
| 4374 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n", |
| 4375 | (uint32_t) (ntohl(ip->ip_src.s_addr))); |
| 4376 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n", |
| 4377 | (uint32_t)(ntohl(ip->ip_dst.s_addr))); |
| 4378 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4379 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n", |
| 4380 | (void *)ro->ro_nh); |
| 4381 | #else |
| 4382 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n", |
| 4383 | (void *)ro->ro_rt); |
| 4384 | #endif |
| 4385 |
| 4386 | if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) { |
| 4387 | /* failed to prepend data, give up */ |
| 4388 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 4389 | sctp_m_freem(m); |
| 4390 | return (ENOMEM); |
| 4391 | } |
| 4392 | SCTP_ATTACH_CHAIN(o_pak, m, packet_length); |
| 4393 | if (port) { |
| 4394 | sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr)); |
| 4395 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 4396 | #if !defined(__Userspace__) |
| 4397 | #if defined(__FreeBSD__) |
| 4398 | if (V_udp_cksum) { |
| 4399 | SCTP_ENABLE_UDP_CSUM(o_pak); |
| 4400 | } |
| 4401 | #else |
| 4402 | SCTP_ENABLE_UDP_CSUM(o_pak); |
| 4403 | #endif |
| 4404 | #endif |
| 4405 | } else { |
| 4406 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4407 | m->m_pkthdr.csum_flags = CSUM_SCTP; |
| 4408 | m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum); |
| 4409 | SCTP_STAT_INCR(sctps_sendhwcrc); |
| 4410 | #else |
| 4411 | if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) && |
| 4412 | (stcb) && (stcb->asoc.scope.loopback_scope))) { |
| 4413 | sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip)); |
| 4414 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 4415 | } else { |
| 4416 | SCTP_STAT_INCR(sctps_sendhwcrc); |
| 4417 | } |
| 4418 | #endif |
| 4419 | } |
| 4420 | #ifdef SCTP_PACKET_LOGGING |
| 4421 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) |
| 4422 | sctp_packet_log(o_pak); |
| 4423 | #endif |
| 4424 | /* send it out. table id is taken from stcb */ |
| 4425 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 4426 | if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) { |
| 4427 | so = SCTP_INP_SO(inp); |
| 4428 | SCTP_SOCKET_UNLOCK(so, 0); |
| 4429 | } |
| 4430 | #endif |
| 4431 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4432 | SCTP_PROBE5(send, NULL, stcb, ip, stcb, sctphdr); |
| 4433 | #endif |
| 4434 | SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id); |
| 4435 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 4436 | if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) { |
| 4437 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 4438 | SCTP_TCB_UNLOCK(stcb); |
| 4439 | SCTP_SOCKET_LOCK(so, 0); |
| 4440 | SCTP_TCB_LOCK(stcb); |
| 4441 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
| 4442 | } |
| 4443 | #endif |
| 4444 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4445 | if (port) { |
| 4446 | UDPSTAT_INC(udps_opackets); |
| 4447 | } |
| 4448 | #endif |
| 4449 | SCTP_STAT_INCR(sctps_sendpackets); |
| 4450 | SCTP_STAT_INCR_COUNTER64(sctps_outpackets); |
| 4451 | if (ret) |
| 4452 | SCTP_STAT_INCR(sctps_senderrors); |
| 4453 |
| 4454 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret); |
| 4455 | if (net == NULL) { |
| 4456 | /* free tempy routes */ |
| 4457 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4458 | RO_NHFREE(ro); |
| 4459 | #else |
| 4460 | if (ro->ro_rt) { |
| 4461 | RTFREE(ro->ro_rt); |
| 4462 | ro->ro_rt = NULL; |
| 4463 | } |
| 4464 | #endif |
| 4465 | } else { |
| 4466 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4467 | if ((ro->ro_nh != NULL) && (net->ro._s_addr) && |
| 4468 | #else |
| 4469 | if ((ro->ro_rt != NULL) && (net->ro._s_addr) && |
| 4470 | #endif |
| 4471 | ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) { |
| 4472 | uint32_t mtu; |
| 4473 |
| 4474 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4475 | mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_nh); |
| 4476 | #else |
| 4477 | mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt); |
| 4478 | #endif |
| 4479 | if (mtu > 0) { |
| 4480 | if (net->port) { |
| 4481 | mtu -= sizeof(struct udphdr); |
| 4482 | } |
| 4483 | if (mtu < net->mtu) { |
| 4484 | if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) { |
| 4485 | sctp_mtu_size_reset(inp, &stcb->asoc, mtu); |
| 4486 | } |
| 4487 | net->mtu = mtu; |
| 4488 | } |
| 4489 | } |
| 4490 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4491 | } else if (ro->ro_nh == NULL) { |
| 4492 | #else |
| 4493 | } else if (ro->ro_rt == NULL) { |
| 4494 | #endif |
| 4495 | /* route was freed */ |
| 4496 | if (net->ro._s_addr && |
| 4497 | net->src_addr_selected) { |
| 4498 | sctp_free_ifa(net->ro._s_addr); |
| 4499 | net->ro._s_addr = NULL; |
| 4500 | } |
| 4501 | net->src_addr_selected = 0; |
| 4502 | } |
| 4503 | } |
| 4504 | return (ret); |
| 4505 | } |
| 4506 | #endif |
| 4507 | #ifdef INET6 |
| 4508 | case AF_INET6: |
| 4509 | { |
| 4510 | uint32_t flowlabel, flowinfo; |
| 4511 | struct ip6_hdr *ip6h; |
| 4512 | struct route_in6 ip6route; |
| 4513 | #if !defined(__Userspace__) |
| 4514 | struct ifnet *ifp; |
| 4515 | #endif |
| 4516 | struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp; |
| 4517 | int prev_scope = 0; |
| 4518 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4519 | struct sockaddr_in6 lsa6_storage; |
| 4520 | int error; |
| 4521 | #endif |
| 4522 | u_short prev_port = 0; |
| 4523 | int len; |
| 4524 |
| 4525 | if (net) { |
| 4526 | flowlabel = net->flowlabel; |
| 4527 | } else if (stcb) { |
| 4528 | flowlabel = stcb->asoc.default_flowlabel; |
| 4529 | } else { |
| 4530 | flowlabel = inp->sctp_ep.default_flowlabel; |
| 4531 | } |
| 4532 | if (flowlabel == 0) { |
| 4533 | /* |
| 4534 | * This means especially, that it is not set at the |
| 4535 | * SCTP layer. So use the value from the IP layer. |
| 4536 | */ |
| 4537 | #if defined(__APPLE__) && !defined(__Userspace__) && (!defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)) |
| 4538 | flowlabel = ntohl(inp->ip_inp.inp.inp_flow); |
| 4539 | #else |
| 4540 | flowlabel = ntohl(((struct inpcb *)inp)->inp_flow); |
| 4541 | #endif |
| 4542 | } |
| 4543 | flowlabel &= 0x000fffff; |
| 4544 | len = SCTP_MIN_OVERHEAD; |
| 4545 | if (port) { |
| 4546 | len += sizeof(struct udphdr); |
| 4547 | } |
| 4548 | newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA); |
| 4549 | if (newm == NULL) { |
| 4550 | sctp_m_freem(m); |
| 4551 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 4552 | return (ENOMEM); |
| 4553 | } |
| 4554 | SCTP_ALIGN_TO_END(newm, len); |
| 4555 | SCTP_BUF_LEN(newm) = len; |
| 4556 | SCTP_BUF_NEXT(newm) = m; |
| 4557 | m = newm; |
| 4558 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4559 | if (net != NULL) { |
| 4560 | m->m_pkthdr.flowid = net->flowid; |
| 4561 | M_HASHTYPE_SET(m, net->flowtype); |
| 4562 | } else { |
| 4563 | m->m_pkthdr.flowid = mflowid; |
| 4564 | M_HASHTYPE_SET(m, mflowtype); |
| 4565 | } |
| 4566 | #endif |
| 4567 | packet_length = sctp_calculate_len(m); |
| 4568 |
| 4569 | ip6h = mtod(m, struct ip6_hdr *); |
| 4570 | /* protect *sin6 from overwrite */ |
| 4571 | sin6 = (struct sockaddr_in6 *)to; |
| 4572 | tmp = *sin6; |
| 4573 | sin6 = &tmp; |
| 4574 |
| 4575 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4576 | /* KAME hack: embed scopeid */ |
| 4577 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 4578 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
| 4579 | if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0) |
| 4580 | #else |
| 4581 | if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL) != 0) |
| 4582 | #endif |
| 4583 | #elif defined(SCTP_KAME) |
| 4584 | if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) |
| 4585 | #else |
| 4586 | if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) |
| 4587 | #endif |
| 4588 | { |
| 4589 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 4590 | sctp_m_freem(m); |
| 4591 | return (EINVAL); |
| 4592 | } |
| 4593 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4594 | if (net == NULL) { |
| 4595 | memset(&ip6route, 0, sizeof(ip6route)); |
| 4596 | ro = (sctp_route_t *)&ip6route; |
| 4597 | #ifdef HAVE_SIN6_LEN |
| 4598 | memcpy(&ro->ro_dst, sin6, sin6->sin6_len); |
| 4599 | #else |
| 4600 | memcpy(&ro->ro_dst, sin6, sizeof(struct sockaddr_in6)); |
| 4601 | #endif |
| 4602 | } else { |
| 4603 | ro = (sctp_route_t *)&net->ro; |
| 4604 | } |
| 4605 | /* |
| 4606 | * We assume here that inp_flow is in host byte order within |
| 4607 | * the TCB! |
| 4608 | */ |
| 4609 | if (tos_value == 0) { |
| 4610 | /* |
| 4611 | * This means especially, that it is not set at the |
| 4612 | * SCTP layer. So use the value from the IP layer. |
| 4613 | */ |
| 4614 | #if defined(__APPLE__) && !defined(__Userspace__) && (!defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)) |
| 4615 | tos_value = (ntohl(inp->ip_inp.inp.inp_flow) >> 20) & 0xff; |
| 4616 | #else |
| 4617 | tos_value = (ntohl(((struct inpcb *)inp)->inp_flow) >> 20) & 0xff; |
| 4618 | #endif |
| 4619 | } |
| 4620 | tos_value &= 0xfc; |
| 4621 | if (ecn_ok) { |
| 4622 | tos_value |= sctp_get_ect(stcb); |
| 4623 | } |
| 4624 | flowinfo = 0x06; |
| 4625 | flowinfo <<= 8; |
| 4626 | flowinfo |= tos_value; |
| 4627 | flowinfo <<= 20; |
| 4628 | flowinfo |= flowlabel; |
| 4629 | ip6h->ip6_flow = htonl(flowinfo); |
| 4630 | if (port) { |
| 4631 | ip6h->ip6_nxt = IPPROTO_UDP; |
| 4632 | } else { |
| 4633 | ip6h->ip6_nxt = IPPROTO_SCTP; |
| 4634 | } |
| 4635 | ip6h->ip6_plen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr))); |
| 4636 | ip6h->ip6_dst = sin6->sin6_addr; |
| 4637 |
| 4638 | /* |
| 4639 | * Add SRC address selection here: we can only reuse to a |
| 4640 | * limited degree the kame src-addr-sel, since we can try |
| 4641 | * their selection but it may not be bound. |
| 4642 | */ |
| 4643 | memset(&lsa6_tmp, 0, sizeof(lsa6_tmp)); |
| 4644 | lsa6_tmp.sin6_family = AF_INET6; |
| 4645 | #ifdef HAVE_SIN6_LEN |
| 4646 | lsa6_tmp.sin6_len = sizeof(lsa6_tmp); |
| 4647 | #endif |
| 4648 | lsa6 = &lsa6_tmp; |
| 4649 | if (net && out_of_asoc_ok == 0) { |
| 4650 | if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED|SCTP_ADDR_IFA_UNUSEABLE))) { |
| 4651 | sctp_free_ifa(net->ro._s_addr); |
| 4652 | net->ro._s_addr = NULL; |
| 4653 | net->src_addr_selected = 0; |
| 4654 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4655 | RO_NHFREE(ro); |
| 4656 | #else |
| 4657 | if (ro->ro_rt) { |
| 4658 | RTFREE(ro->ro_rt); |
| 4659 | ro->ro_rt = NULL; |
| 4660 | } |
| 4661 | #endif |
| 4662 | } |
| 4663 | if (net->src_addr_selected == 0) { |
| 4664 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4665 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
| 4666 | /* KAME hack: embed scopeid */ |
| 4667 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 4668 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
| 4669 | if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0) |
| 4670 | #else |
| 4671 | if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL) != 0) |
| 4672 | #endif |
| 4673 | #elif defined(SCTP_KAME) |
| 4674 | if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) |
| 4675 | #else |
| 4676 | if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) |
| 4677 | #endif |
| 4678 | { |
| 4679 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 4680 | sctp_m_freem(m); |
| 4681 | return (EINVAL); |
| 4682 | } |
| 4683 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4684 | /* Cache the source address */ |
| 4685 | net->ro._s_addr = sctp_source_address_selection(inp, |
| 4686 | stcb, |
| 4687 | ro, |
| 4688 | net, |
| 4689 | 0, |
| 4690 | vrf_id); |
| 4691 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4692 | #ifdef SCTP_KAME |
| 4693 | (void)sa6_recoverscope(sin6); |
| 4694 | #else |
| 4695 | (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL); |
| 4696 | #endif /* SCTP_KAME */ |
| 4697 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4698 | net->src_addr_selected = 1; |
| 4699 | } |
| 4700 | if (net->ro._s_addr == NULL) { |
| 4701 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n"); |
| 4702 | net->src_addr_selected = 0; |
| 4703 | sctp_handle_no_route(stcb, net, so_locked); |
| 4704 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); |
| 4705 | sctp_m_freem(m); |
| 4706 | return (EHOSTUNREACH); |
| 4707 | } |
| 4708 | lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr; |
| 4709 | } else { |
| 4710 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4711 | sin6 = (struct sockaddr_in6 *)&ro->ro_dst; |
| 4712 | /* KAME hack: embed scopeid */ |
| 4713 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 4714 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
| 4715 | if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0) |
| 4716 | #else |
| 4717 | if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL) != 0) |
| 4718 | #endif |
| 4719 | #elif defined(SCTP_KAME) |
| 4720 | if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) |
| 4721 | #else |
| 4722 | if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) |
| 4723 | #endif |
| 4724 | { |
| 4725 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 4726 | sctp_m_freem(m); |
| 4727 | return (EINVAL); |
| 4728 | } |
| 4729 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4730 | if (over_addr == NULL) { |
| 4731 | struct sctp_ifa *_lsrc; |
| 4732 |
| 4733 | _lsrc = sctp_source_address_selection(inp, stcb, ro, |
| 4734 | net, |
| 4735 | out_of_asoc_ok, |
| 4736 | vrf_id); |
| 4737 | if (_lsrc == NULL) { |
| 4738 | sctp_handle_no_route(stcb, net, so_locked); |
| 4739 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); |
| 4740 | sctp_m_freem(m); |
| 4741 | return (EHOSTUNREACH); |
| 4742 | } |
| 4743 | lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr; |
| 4744 | sctp_free_ifa(_lsrc); |
| 4745 | } else { |
| 4746 | lsa6->sin6_addr = over_addr->sin6.sin6_addr; |
| 4747 | SCTP_RTALLOC(ro, vrf_id, inp->fibnum); |
| 4748 | } |
| 4749 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4750 | #ifdef SCTP_KAME |
| 4751 | (void)sa6_recoverscope(sin6); |
| 4752 | #else |
| 4753 | (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL); |
| 4754 | #endif /* SCTP_KAME */ |
| 4755 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4756 | } |
| 4757 | lsa6->sin6_port = inp->sctp_lport; |
| 4758 |
| 4759 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4760 | if (ro->ro_nh == NULL) { |
| 4761 | #else |
| 4762 | if (ro->ro_rt == NULL) { |
| 4763 | #endif |
| 4764 | /* |
| 4765 | * src addr selection failed to find a route (or |
| 4766 | * valid source addr), so we can't get there from |
| 4767 | * here! |
| 4768 | */ |
| 4769 | sctp_handle_no_route(stcb, net, so_locked); |
| 4770 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); |
| 4771 | sctp_m_freem(m); |
| 4772 | return (EHOSTUNREACH); |
| 4773 | } |
| 4774 | #ifndef SCOPEDROUTING |
| 4775 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4776 | /* |
| 4777 | * XXX: sa6 may not have a valid sin6_scope_id in the |
| 4778 | * non-SCOPEDROUTING case. |
| 4779 | */ |
| 4780 | memset(&lsa6_storage, 0, sizeof(lsa6_storage)); |
| 4781 | lsa6_storage.sin6_family = AF_INET6; |
| 4782 | #ifdef HAVE_SIN6_LEN |
| 4783 | lsa6_storage.sin6_len = sizeof(lsa6_storage); |
| 4784 | #endif |
| 4785 | #ifdef SCTP_KAME |
| 4786 | lsa6_storage.sin6_addr = lsa6->sin6_addr; |
| 4787 | if ((error = sa6_recoverscope(&lsa6_storage)) != 0) { |
| 4788 | #else |
| 4789 | if ((error = in6_recoverscope(&lsa6_storage, &lsa6->sin6_addr, |
| 4790 | NULL)) != 0) { |
| 4791 | #endif /* SCTP_KAME */ |
| 4792 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error); |
| 4793 | sctp_m_freem(m); |
| 4794 | return (error); |
| 4795 | } |
| 4796 | /* XXX */ |
| 4797 | lsa6_storage.sin6_addr = lsa6->sin6_addr; |
| 4798 | lsa6_storage.sin6_port = inp->sctp_lport; |
| 4799 | lsa6 = &lsa6_storage; |
| 4800 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4801 | #endif /* SCOPEDROUTING */ |
| 4802 | ip6h->ip6_src = lsa6->sin6_addr; |
| 4803 |
| 4804 | if (port) { |
| 4805 | if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) { |
| 4806 | sctp_handle_no_route(stcb, net, so_locked); |
| 4807 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH); |
| 4808 | sctp_m_freem(m); |
| 4809 | return (EHOSTUNREACH); |
| 4810 | } |
| 4811 | udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr)); |
| 4812 | udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)); |
| 4813 | udp->uh_dport = port; |
| 4814 | udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr))); |
| 4815 | udp->uh_sum = 0; |
| 4816 | sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr)); |
| 4817 | } else { |
| 4818 | sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr)); |
| 4819 | } |
| 4820 |
| 4821 | sctphdr->src_port = src_port; |
| 4822 | sctphdr->dest_port = dest_port; |
| 4823 | sctphdr->v_tag = v_tag; |
| 4824 | sctphdr->checksum = 0; |
| 4825 |
| 4826 | /* |
| 4827 | * We set the hop limit now since there is a good chance |
| 4828 | * that our ro pointer is now filled |
| 4829 | */ |
| 4830 | ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro); |
| 4831 | #if !defined(__Userspace__) |
| 4832 | ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro); |
| 4833 | #endif |
| 4834 |
| 4835 | #ifdef SCTP_DEBUG |
| 4836 | /* Copy to be sure something bad is not happening */ |
| 4837 | sin6->sin6_addr = ip6h->ip6_dst; |
| 4838 | lsa6->sin6_addr = ip6h->ip6_src; |
| 4839 | #endif |
| 4840 |
| 4841 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n"); |
| 4842 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: "); |
| 4843 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6); |
| 4844 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: "); |
| 4845 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6); |
| 4846 | if (net) { |
| 4847 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
| 4848 | /* preserve the port and scope for link local send */ |
| 4849 | prev_scope = sin6->sin6_scope_id; |
| 4850 | prev_port = sin6->sin6_port; |
| 4851 | } |
| 4852 |
| 4853 | if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) { |
| 4854 | /* failed to prepend data, give up */ |
| 4855 | sctp_m_freem(m); |
| 4856 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 4857 | return (ENOMEM); |
| 4858 | } |
| 4859 | SCTP_ATTACH_CHAIN(o_pak, m, packet_length); |
| 4860 | if (port) { |
| 4861 | sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr)); |
| 4862 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 4863 | #if !defined(__Userspace__) |
| 4864 | #if defined(_WIN32) |
| 4865 | udp->uh_sum = 0; |
| 4866 | #else |
| 4867 | if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) { |
| 4868 | udp->uh_sum = 0xffff; |
| 4869 | } |
| 4870 | #endif |
| 4871 | #endif |
| 4872 | } else { |
| 4873 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4874 | m->m_pkthdr.csum_flags = CSUM_SCTP_IPV6; |
| 4875 | m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum); |
| 4876 | SCTP_STAT_INCR(sctps_sendhwcrc); |
| 4877 | #else |
| 4878 | if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) && |
| 4879 | (stcb) && (stcb->asoc.scope.loopback_scope))) { |
| 4880 | sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr)); |
| 4881 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 4882 | } else { |
| 4883 | SCTP_STAT_INCR(sctps_sendhwcrc); |
| 4884 | } |
| 4885 | #endif |
| 4886 | } |
| 4887 | /* send it out. table id is taken from stcb */ |
| 4888 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 4889 | if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) { |
| 4890 | so = SCTP_INP_SO(inp); |
| 4891 | SCTP_SOCKET_UNLOCK(so, 0); |
| 4892 | } |
| 4893 | #endif |
| 4894 | #ifdef SCTP_PACKET_LOGGING |
| 4895 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) |
| 4896 | sctp_packet_log(o_pak); |
| 4897 | #endif |
| 4898 | #if !defined(__Userspace__) |
| 4899 | #if defined(__FreeBSD__) |
| 4900 | SCTP_PROBE5(send, NULL, stcb, ip6h, stcb, sctphdr); |
| 4901 | #endif |
| 4902 | SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id); |
| 4903 | #else |
| 4904 | SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, NULL, stcb, vrf_id); |
| 4905 | #endif |
| 4906 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 4907 | if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) { |
| 4908 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 4909 | SCTP_TCB_UNLOCK(stcb); |
| 4910 | SCTP_SOCKET_LOCK(so, 0); |
| 4911 | SCTP_TCB_LOCK(stcb); |
| 4912 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
| 4913 | } |
| 4914 | #endif |
| 4915 | if (net) { |
| 4916 | /* for link local this must be done */ |
| 4917 | sin6->sin6_scope_id = prev_scope; |
| 4918 | sin6->sin6_port = prev_port; |
| 4919 | } |
| 4920 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret); |
| 4921 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4922 | if (port) { |
| 4923 | UDPSTAT_INC(udps_opackets); |
| 4924 | } |
| 4925 | #endif |
| 4926 | SCTP_STAT_INCR(sctps_sendpackets); |
| 4927 | SCTP_STAT_INCR_COUNTER64(sctps_outpackets); |
| 4928 | if (ret) { |
| 4929 | SCTP_STAT_INCR(sctps_senderrors); |
| 4930 | } |
| 4931 | if (net == NULL) { |
| 4932 | /* Now if we had a temp route free it */ |
| 4933 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4934 | RO_NHFREE(ro); |
| 4935 | #else |
| 4936 | if (ro->ro_rt) { |
| 4937 | RTFREE(ro->ro_rt); |
| 4938 | ro->ro_rt = NULL; |
| 4939 | } |
| 4940 | #endif |
| 4941 | } else { |
| 4942 | /* PMTU check versus smallest asoc MTU goes here */ |
| 4943 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4944 | if (ro->ro_nh == NULL) { |
| 4945 | #else |
| 4946 | if (ro->ro_rt == NULL) { |
| 4947 | #endif |
| 4948 | /* Route was freed */ |
| 4949 | if (net->ro._s_addr && |
| 4950 | net->src_addr_selected) { |
| 4951 | sctp_free_ifa(net->ro._s_addr); |
| 4952 | net->ro._s_addr = NULL; |
| 4953 | } |
| 4954 | net->src_addr_selected = 0; |
| 4955 | } |
| 4956 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4957 | if ((ro->ro_nh != NULL) && (net->ro._s_addr) && |
| 4958 | #else |
| 4959 | if ((ro->ro_rt != NULL) && (net->ro._s_addr) && |
| 4960 | #endif |
| 4961 | ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) { |
| 4962 | uint32_t mtu; |
| 4963 |
| 4964 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 4965 | mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_nh); |
| 4966 | #else |
| 4967 | mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt); |
| 4968 | #endif |
| 4969 | if (mtu > 0) { |
| 4970 | if (net->port) { |
| 4971 | mtu -= sizeof(struct udphdr); |
| 4972 | } |
| 4973 | if (mtu < net->mtu) { |
| 4974 | if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) { |
| 4975 | sctp_mtu_size_reset(inp, &stcb->asoc, mtu); |
| 4976 | } |
| 4977 | net->mtu = mtu; |
| 4978 | } |
| 4979 | } |
| 4980 | } |
| 4981 | #if !defined(__Userspace__) |
| 4982 | else if (ifp) { |
| 4983 | #if defined(_WIN32) |
| 4984 | #define ND_IFINFO(ifp) (ifp) |
| 4985 | #define linkmtu if_mtu |
| 4986 | #endif |
| 4987 | if (ND_IFINFO(ifp)->linkmtu && |
| 4988 | (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) { |
| 4989 | sctp_mtu_size_reset(inp, |
| 4990 | &stcb->asoc, |
| 4991 | ND_IFINFO(ifp)->linkmtu); |
| 4992 | } |
| 4993 | } |
| 4994 | #endif |
| 4995 | } |
| 4996 | return (ret); |
| 4997 | } |
| 4998 | #endif |
| 4999 | #if defined(__Userspace__) |
| 5000 | case AF_CONN: |
| 5001 | { |
| 5002 | char *buffer; |
| 5003 | struct sockaddr_conn *sconn; |
| 5004 | int len; |
| 5005 |
| 5006 | sconn = (struct sockaddr_conn *)to; |
| 5007 | len = sizeof(struct sctphdr); |
| 5008 | newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA); |
| 5009 | if (newm == NULL) { |
| 5010 | sctp_m_freem(m); |
| 5011 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 5012 | return (ENOMEM); |
| 5013 | } |
| 5014 | SCTP_ALIGN_TO_END(newm, len); |
| 5015 | SCTP_BUF_LEN(newm) = len; |
| 5016 | SCTP_BUF_NEXT(newm) = m; |
| 5017 | m = newm; |
| 5018 | packet_length = sctp_calculate_len(m); |
| 5019 | sctphdr = mtod(m, struct sctphdr *); |
| 5020 | sctphdr->src_port = src_port; |
| 5021 | sctphdr->dest_port = dest_port; |
| 5022 | sctphdr->v_tag = v_tag; |
| 5023 | sctphdr->checksum = 0; |
| 5024 | if (SCTP_BASE_VAR(crc32c_offloaded) == 0) { |
| 5025 | sctphdr->checksum = sctp_calculate_cksum(m, 0); |
| 5026 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 5027 | } else { |
| 5028 | SCTP_STAT_INCR(sctps_sendhwcrc); |
| 5029 | } |
| 5030 | if (tos_value == 0) { |
| 5031 | tos_value = inp->ip_inp.inp.inp_ip_tos; |
| 5032 | } |
| 5033 | tos_value &= 0xfc; |
| 5034 | if (ecn_ok) { |
| 5035 | tos_value |= sctp_get_ect(stcb); |
| 5036 | } |
| 5037 | /* Don't alloc/free for each packet */ |
| 5038 | if ((buffer = malloc(packet_length)) != NULL) { |
| 5039 | m_copydata(m, 0, packet_length, buffer); |
| 5040 | ret = SCTP_BASE_VAR(conn_output)(sconn->sconn_addr, buffer, packet_length, tos_value, nofragment_flag); |
| 5041 | free(buffer); |
| 5042 | } else { |
| 5043 | ret = ENOMEM; |
| 5044 | } |
| 5045 | sctp_m_freem(m); |
| 5046 | return (ret); |
| 5047 | } |
| 5048 | #endif |
| 5049 | default: |
| 5050 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n", |
| 5051 | ((struct sockaddr *)to)->sa_family); |
| 5052 | sctp_m_freem(m); |
| 5053 | SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT); |
| 5054 | return (EFAULT); |
| 5055 | } |
| 5056 | } |
| 5057 |
| 5058 | void |
| 5059 | sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked) |
| 5060 | { |
| 5061 | struct mbuf *m, *m_last; |
| 5062 | struct sctp_nets *net; |
| 5063 | struct sctp_init_chunk *init; |
| 5064 | struct sctp_supported_addr_param *sup_addr; |
| 5065 | struct sctp_adaptation_layer_indication *ali; |
| 5066 | struct sctp_supported_chunk_types_param *pr_supported; |
| 5067 | struct sctp_paramhdr *ph; |
| 5068 | int cnt_inits_to = 0; |
| 5069 | int error; |
| 5070 | uint16_t num_ext, chunk_len, padding_len, parameter_len; |
| 5071 |
| 5072 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 5073 | if (so_locked) { |
| 5074 | sctp_lock_assert(SCTP_INP_SO(inp)); |
| 5075 | } else { |
| 5076 | sctp_unlock_assert(SCTP_INP_SO(inp)); |
| 5077 | } |
| 5078 | #endif |
| 5079 | /* INIT's always go to the primary (and usually ONLY address) */ |
| 5080 | net = stcb->asoc.primary_destination; |
| 5081 | if (net == NULL) { |
| 5082 | net = TAILQ_FIRST(&stcb->asoc.nets); |
| 5083 | if (net == NULL) { |
| 5084 | /* TSNH */ |
| 5085 | return; |
| 5086 | } |
| 5087 | /* we confirm any address we send an INIT to */ |
| 5088 | net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; |
| 5089 | (void)sctp_set_primary_addr(stcb, NULL, net); |
| 5090 | } else { |
| 5091 | /* we confirm any address we send an INIT to */ |
| 5092 | net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; |
| 5093 | } |
| 5094 | SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n"); |
| 5095 | #ifdef INET6 |
| 5096 | if (net->ro._l_addr.sa.sa_family == AF_INET6) { |
| 5097 | /* |
| 5098 | * special hook, if we are sending to link local it will not |
| 5099 | * show up in our private address count. |
| 5100 | */ |
| 5101 | if (IN6_IS_ADDR_LINKLOCAL(&net->ro._l_addr.sin6.sin6_addr)) |
| 5102 | cnt_inits_to = 1; |
| 5103 | } |
| 5104 | #endif |
| 5105 | if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { |
| 5106 | /* This case should not happen */ |
| 5107 | SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n"); |
| 5108 | return; |
| 5109 | } |
| 5110 | /* start the INIT timer */ |
| 5111 | sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net); |
| 5112 |
| 5113 | m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_NOWAIT, 1, MT_DATA); |
| 5114 | if (m == NULL) { |
| 5115 | /* No memory, INIT timer will re-attempt. */ |
| 5116 | SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n"); |
| 5117 | return; |
| 5118 | } |
| 5119 | chunk_len = (uint16_t)sizeof(struct sctp_init_chunk); |
| 5120 | padding_len = 0; |
| 5121 | /* Now lets put the chunk header in place */ |
| 5122 | init = mtod(m, struct sctp_init_chunk *); |
| 5123 | /* now the chunk header */ |
| 5124 | init->ch.chunk_type = SCTP_INITIATION; |
| 5125 | init->ch.chunk_flags = 0; |
| 5126 | /* fill in later from mbuf we build */ |
| 5127 | init->ch.chunk_length = 0; |
| 5128 | /* place in my tag */ |
| 5129 | init->init.initiate_tag = htonl(stcb->asoc.my_vtag); |
| 5130 | /* set up some of the credits. */ |
| 5131 | init->init.a_rwnd = htonl(max(inp->sctp_socket?SCTP_SB_LIMIT_RCV(inp->sctp_socket):0, |
| 5132 | SCTP_MINIMAL_RWND)); |
| 5133 | init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams); |
| 5134 | init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams); |
| 5135 | init->init.initial_tsn = htonl(stcb->asoc.init_seq_number); |
| 5136 |
| 5137 | /* Adaptation layer indication parameter */ |
| 5138 | if (inp->sctp_ep.adaptation_layer_indicator_provided) { |
| 5139 | parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication); |
| 5140 | ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t) + chunk_len); |
| 5141 | ali->ph.param_type = htons(SCTP_ULP_ADAPTATION); |
| 5142 | ali->ph.param_length = htons(parameter_len); |
| 5143 | ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator); |
| 5144 | chunk_len += parameter_len; |
| 5145 | } |
| 5146 |
| 5147 | /* ECN parameter */ |
| 5148 | if (stcb->asoc.ecn_supported == 1) { |
| 5149 | parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); |
| 5150 | ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len); |
| 5151 | ph->param_type = htons(SCTP_ECN_CAPABLE); |
| 5152 | ph->param_length = htons(parameter_len); |
| 5153 | chunk_len += parameter_len; |
| 5154 | } |
| 5155 |
| 5156 | /* PR-SCTP supported parameter */ |
| 5157 | if (stcb->asoc.prsctp_supported == 1) { |
| 5158 | parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); |
| 5159 | ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len); |
| 5160 | ph->param_type = htons(SCTP_PRSCTP_SUPPORTED); |
| 5161 | ph->param_length = htons(parameter_len); |
| 5162 | chunk_len += parameter_len; |
| 5163 | } |
| 5164 |
| 5165 | /* Add NAT friendly parameter. */ |
| 5166 | if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) { |
| 5167 | parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); |
| 5168 | ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len); |
| 5169 | ph->param_type = htons(SCTP_HAS_NAT_SUPPORT); |
| 5170 | ph->param_length = htons(parameter_len); |
| 5171 | chunk_len += parameter_len; |
| 5172 | } |
| 5173 |
| 5174 | /* And now tell the peer which extensions we support */ |
| 5175 | num_ext = 0; |
| 5176 | pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t) + chunk_len); |
| 5177 | if (stcb->asoc.prsctp_supported == 1) { |
| 5178 | pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN; |
| 5179 | if (stcb->asoc.idata_supported) { |
| 5180 | pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN; |
| 5181 | } |
| 5182 | } |
| 5183 | if (stcb->asoc.auth_supported == 1) { |
| 5184 | pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION; |
| 5185 | } |
| 5186 | if (stcb->asoc.asconf_supported == 1) { |
| 5187 | pr_supported->chunk_types[num_ext++] = SCTP_ASCONF; |
| 5188 | pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK; |
| 5189 | } |
| 5190 | if (stcb->asoc.reconfig_supported == 1) { |
| 5191 | pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET; |
| 5192 | } |
| 5193 | if (stcb->asoc.idata_supported) { |
| 5194 | pr_supported->chunk_types[num_ext++] = SCTP_IDATA; |
| 5195 | } |
| 5196 | if (stcb->asoc.nrsack_supported == 1) { |
| 5197 | pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK; |
| 5198 | } |
| 5199 | if (stcb->asoc.pktdrop_supported == 1) { |
| 5200 | pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED; |
| 5201 | } |
| 5202 | if (num_ext > 0) { |
| 5203 | parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext; |
| 5204 | pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT); |
| 5205 | pr_supported->ph.param_length = htons(parameter_len); |
| 5206 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 5207 | chunk_len += parameter_len; |
| 5208 | } |
| 5209 | /* add authentication parameters */ |
| 5210 | if (stcb->asoc.auth_supported) { |
| 5211 | /* attach RANDOM parameter, if available */ |
| 5212 | if (stcb->asoc.authinfo.random != NULL) { |
| 5213 | struct sctp_auth_random *randp; |
| 5214 |
| 5215 | if (padding_len > 0) { |
| 5216 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 5217 | chunk_len += padding_len; |
| 5218 | padding_len = 0; |
| 5219 | } |
| 5220 | randp = (struct sctp_auth_random *)(mtod(m, caddr_t) + chunk_len); |
| 5221 | parameter_len = (uint16_t)sizeof(struct sctp_auth_random) + stcb->asoc.authinfo.random_len; |
| 5222 | /* random key already contains the header */ |
| 5223 | memcpy(randp, stcb->asoc.authinfo.random->key, parameter_len); |
| 5224 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 5225 | chunk_len += parameter_len; |
| 5226 | } |
| 5227 | /* add HMAC_ALGO parameter */ |
| 5228 | if (stcb->asoc.local_hmacs != NULL) { |
| 5229 | struct sctp_auth_hmac_algo *hmacs; |
| 5230 |
| 5231 | if (padding_len > 0) { |
| 5232 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 5233 | chunk_len += padding_len; |
| 5234 | padding_len = 0; |
| 5235 | } |
| 5236 | hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t) + chunk_len); |
| 5237 | parameter_len = (uint16_t)(sizeof(struct sctp_auth_hmac_algo) + |
| 5238 | stcb->asoc.local_hmacs->num_algo * sizeof(uint16_t)); |
| 5239 | hmacs->ph.param_type = htons(SCTP_HMAC_LIST); |
| 5240 | hmacs->ph.param_length = htons(parameter_len); |
| 5241 | sctp_serialize_hmaclist(stcb->asoc.local_hmacs, (uint8_t *)hmacs->hmac_ids); |
| 5242 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 5243 | chunk_len += parameter_len; |
| 5244 | } |
| 5245 | /* add CHUNKS parameter */ |
| 5246 | if (stcb->asoc.local_auth_chunks != NULL) { |
| 5247 | struct sctp_auth_chunk_list *chunks; |
| 5248 |
| 5249 | if (padding_len > 0) { |
| 5250 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 5251 | chunk_len += padding_len; |
| 5252 | padding_len = 0; |
| 5253 | } |
| 5254 | chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t) + chunk_len); |
| 5255 | parameter_len = (uint16_t)(sizeof(struct sctp_auth_chunk_list) + |
| 5256 | sctp_auth_get_chklist_size(stcb->asoc.local_auth_chunks)); |
| 5257 | chunks->ph.param_type = htons(SCTP_CHUNK_LIST); |
| 5258 | chunks->ph.param_length = htons(parameter_len); |
| 5259 | sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks, chunks->chunk_types); |
| 5260 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 5261 | chunk_len += parameter_len; |
| 5262 | } |
| 5263 | } |
| 5264 |
| 5265 | /* now any cookie time extensions */ |
| 5266 | if (stcb->asoc.cookie_preserve_req > 0) { |
| 5267 | struct sctp_cookie_perserve_param *cookie_preserve; |
| 5268 |
| 5269 | if (padding_len > 0) { |
| 5270 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 5271 | chunk_len += padding_len; |
| 5272 | padding_len = 0; |
| 5273 | } |
| 5274 | parameter_len = (uint16_t)sizeof(struct sctp_cookie_perserve_param); |
| 5275 | cookie_preserve = (struct sctp_cookie_perserve_param *)(mtod(m, caddr_t) + chunk_len); |
| 5276 | cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE); |
| 5277 | cookie_preserve->ph.param_length = htons(parameter_len); |
| 5278 | cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req); |
| 5279 | stcb->asoc.cookie_preserve_req = 0; |
| 5280 | chunk_len += parameter_len; |
| 5281 | } |
| 5282 |
| 5283 | if (stcb->asoc.scope.ipv4_addr_legal || stcb->asoc.scope.ipv6_addr_legal) { |
| 5284 | uint8_t i; |
| 5285 |
| 5286 | if (padding_len > 0) { |
| 5287 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 5288 | chunk_len += padding_len; |
| 5289 | padding_len = 0; |
| 5290 | } |
| 5291 | parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); |
| 5292 | if (stcb->asoc.scope.ipv4_addr_legal) { |
| 5293 | parameter_len += (uint16_t)sizeof(uint16_t); |
| 5294 | } |
| 5295 | if (stcb->asoc.scope.ipv6_addr_legal) { |
| 5296 | parameter_len += (uint16_t)sizeof(uint16_t); |
| 5297 | } |
| 5298 | sup_addr = (struct sctp_supported_addr_param *)(mtod(m, caddr_t) + chunk_len); |
| 5299 | sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE); |
| 5300 | sup_addr->ph.param_length = htons(parameter_len); |
| 5301 | i = 0; |
| 5302 | if (stcb->asoc.scope.ipv4_addr_legal) { |
| 5303 | sup_addr->addr_type[i++] = htons(SCTP_IPV4_ADDRESS); |
| 5304 | } |
| 5305 | if (stcb->asoc.scope.ipv6_addr_legal) { |
| 5306 | sup_addr->addr_type[i++] = htons(SCTP_IPV6_ADDRESS); |
| 5307 | } |
| 5308 | padding_len = 4 - 2 * i; |
| 5309 | chunk_len += parameter_len; |
| 5310 | } |
| 5311 |
| 5312 | SCTP_BUF_LEN(m) = chunk_len; |
| 5313 | /* now the addresses */ |
| 5314 | /* To optimize this we could put the scoping stuff |
| 5315 | * into a structure and remove the individual uint8's from |
| 5316 | * the assoc structure. Then we could just sifa in the |
| 5317 | * address within the stcb. But for now this is a quick |
| 5318 | * hack to get the address stuff teased apart. |
| 5319 | */ |
| 5320 | m_last = sctp_add_addresses_to_i_ia(inp, stcb, &stcb->asoc.scope, |
| 5321 | m, cnt_inits_to, |
| 5322 | &padding_len, &chunk_len); |
| 5323 |
| 5324 | init->ch.chunk_length = htons(chunk_len); |
| 5325 | if (padding_len > 0) { |
| 5326 | if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) { |
| 5327 | sctp_m_freem(m); |
| 5328 | return; |
| 5329 | } |
| 5330 | } |
| 5331 | SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n"); |
| 5332 | if ((error = sctp_lowlevel_chunk_output(inp, stcb, net, |
| 5333 | (struct sockaddr *)&net->ro._l_addr, |
| 5334 | m, 0, NULL, 0, 0, 0, 0, |
| 5335 | inp->sctp_lport, stcb->rport, htonl(0), |
| 5336 | net->port, NULL, |
| 5337 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 5338 | 0, 0, |
| 5339 | #endif |
| 5340 | so_locked))) { |
| 5341 | SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error); |
| 5342 | if (error == ENOBUFS) { |
| 5343 | stcb->asoc.ifp_had_enobuf = 1; |
| 5344 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 5345 | } |
| 5346 | } else { |
| 5347 | stcb->asoc.ifp_had_enobuf = 0; |
| 5348 | } |
| 5349 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 5350 | (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); |
| 5351 | } |
| 5352 |
| 5353 | struct mbuf * |
| 5354 | sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt, |
| 5355 | int param_offset, int *abort_processing, |
| 5356 | struct sctp_chunkhdr *cp, |
| 5357 | int *nat_friendly, |
| 5358 | int *cookie_found) |
| 5359 | { |
| 5360 | /* |
| 5361 | * Given a mbuf containing an INIT or INIT-ACK with the param_offset |
| 5362 | * being equal to the beginning of the params i.e. (iphlen + |
| 5363 | * sizeof(struct sctp_init_msg) parse through the parameters to the |
| 5364 | * end of the mbuf verifying that all parameters are known. |
| 5365 | * |
| 5366 | * For unknown parameters build and return a mbuf with |
| 5367 | * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop |
| 5368 | * processing this chunk stop, and set *abort_processing to 1. |
| 5369 | * |
| 5370 | * By having param_offset be pre-set to where parameters begin it is |
| 5371 | * hoped that this routine may be reused in the future by new |
| 5372 | * features. |
| 5373 | */ |
| 5374 | struct sctp_paramhdr *phdr, params; |
| 5375 |
| 5376 | struct mbuf *mat, *m_tmp, *op_err, *op_err_last; |
| 5377 | int at, limit, pad_needed; |
| 5378 | uint16_t ptype, plen, padded_size; |
| 5379 |
| 5380 | *abort_processing = 0; |
| 5381 | if (cookie_found != NULL) { |
| 5382 | *cookie_found = 0; |
| 5383 | } |
| 5384 | mat = in_initpkt; |
| 5385 | limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk); |
| 5386 | at = param_offset; |
| 5387 | op_err = NULL; |
| 5388 | op_err_last = NULL; |
| 5389 | pad_needed = 0; |
| 5390 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n"); |
| 5391 | phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params)); |
| 5392 | while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) { |
| 5393 | ptype = ntohs(phdr->param_type); |
| 5394 | plen = ntohs(phdr->param_length); |
| 5395 | if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) { |
| 5396 | /* wacked parameter */ |
| 5397 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen); |
| 5398 | goto invalid_size; |
| 5399 | } |
| 5400 | limit -= SCTP_SIZE32(plen); |
| 5401 | /*- |
| 5402 | * All parameters for all chunks that we know/understand are |
| 5403 | * listed here. We process them other places and make |
| 5404 | * appropriate stop actions per the upper bits. However this |
| 5405 | * is the generic routine processor's can call to get back |
| 5406 | * an operr.. to either incorporate (init-ack) or send. |
| 5407 | */ |
| 5408 | padded_size = SCTP_SIZE32(plen); |
| 5409 | switch (ptype) { |
| 5410 | /* Param's with variable size */ |
| 5411 | case SCTP_HEARTBEAT_INFO: |
| 5412 | case SCTP_UNRECOG_PARAM: |
| 5413 | case SCTP_ERROR_CAUSE_IND: |
| 5414 | /* ok skip fwd */ |
| 5415 | at += padded_size; |
| 5416 | break; |
| 5417 | case SCTP_STATE_COOKIE: |
| 5418 | if (cookie_found != NULL) { |
| 5419 | *cookie_found = 1; |
| 5420 | } |
| 5421 | at += padded_size; |
| 5422 | break; |
| 5423 | /* Param's with variable size within a range */ |
| 5424 | case SCTP_CHUNK_LIST: |
| 5425 | case SCTP_SUPPORTED_CHUNK_EXT: |
| 5426 | if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) { |
| 5427 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen); |
| 5428 | goto invalid_size; |
| 5429 | } |
| 5430 | at += padded_size; |
| 5431 | break; |
| 5432 | case SCTP_SUPPORTED_ADDRTYPE: |
| 5433 | if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) { |
| 5434 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen); |
| 5435 | goto invalid_size; |
| 5436 | } |
| 5437 | at += padded_size; |
| 5438 | break; |
| 5439 | case SCTP_RANDOM: |
| 5440 | if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) { |
| 5441 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen); |
| 5442 | goto invalid_size; |
| 5443 | } |
| 5444 | at += padded_size; |
| 5445 | break; |
| 5446 | case SCTP_SET_PRIM_ADDR: |
| 5447 | case SCTP_DEL_IP_ADDRESS: |
| 5448 | case SCTP_ADD_IP_ADDRESS: |
| 5449 | if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) && |
| 5450 | (padded_size != sizeof(struct sctp_asconf_addr_param))) { |
| 5451 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen); |
| 5452 | goto invalid_size; |
| 5453 | } |
| 5454 | at += padded_size; |
| 5455 | break; |
| 5456 | /* Param's with a fixed size */ |
| 5457 | case SCTP_IPV4_ADDRESS: |
| 5458 | if (padded_size != sizeof(struct sctp_ipv4addr_param)) { |
| 5459 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen); |
| 5460 | goto invalid_size; |
| 5461 | } |
| 5462 | at += padded_size; |
| 5463 | break; |
| 5464 | case SCTP_IPV6_ADDRESS: |
| 5465 | if (padded_size != sizeof(struct sctp_ipv6addr_param)) { |
| 5466 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen); |
| 5467 | goto invalid_size; |
| 5468 | } |
| 5469 | at += padded_size; |
| 5470 | break; |
| 5471 | case SCTP_COOKIE_PRESERVE: |
| 5472 | if (padded_size != sizeof(struct sctp_cookie_perserve_param)) { |
| 5473 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen); |
| 5474 | goto invalid_size; |
| 5475 | } |
| 5476 | at += padded_size; |
| 5477 | break; |
| 5478 | case SCTP_HAS_NAT_SUPPORT: |
| 5479 | *nat_friendly = 1; |
| 5480 | /* fall through */ |
| 5481 | case SCTP_PRSCTP_SUPPORTED: |
| 5482 | if (padded_size != sizeof(struct sctp_paramhdr)) { |
| 5483 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen); |
| 5484 | goto invalid_size; |
| 5485 | } |
| 5486 | at += padded_size; |
| 5487 | break; |
| 5488 | case SCTP_ECN_CAPABLE: |
| 5489 | if (padded_size != sizeof(struct sctp_paramhdr)) { |
| 5490 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen); |
| 5491 | goto invalid_size; |
| 5492 | } |
| 5493 | at += padded_size; |
| 5494 | break; |
| 5495 | case SCTP_ULP_ADAPTATION: |
| 5496 | if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) { |
| 5497 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen); |
| 5498 | goto invalid_size; |
| 5499 | } |
| 5500 | at += padded_size; |
| 5501 | break; |
| 5502 | case SCTP_SUCCESS_REPORT: |
| 5503 | if (padded_size != sizeof(struct sctp_asconf_paramhdr)) { |
| 5504 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen); |
| 5505 | goto invalid_size; |
| 5506 | } |
| 5507 | at += padded_size; |
| 5508 | break; |
| 5509 | case SCTP_HOSTNAME_ADDRESS: |
| 5510 | { |
| 5511 | /* Hostname parameters are deprecated. */ |
| 5512 | struct sctp_gen_error_cause *cause; |
| 5513 | int l_len; |
| 5514 |
| 5515 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n"); |
| 5516 | *abort_processing = 1; |
| 5517 | sctp_m_freem(op_err); |
| 5518 | op_err = NULL; |
| 5519 | op_err_last = NULL; |
| 5520 | #ifdef INET6 |
| 5521 | l_len = SCTP_MIN_OVERHEAD; |
| 5522 | #else |
| 5523 | l_len = SCTP_MIN_V4_OVERHEAD; |
| 5524 | #endif |
| 5525 | l_len += sizeof(struct sctp_chunkhdr); |
| 5526 | l_len += sizeof(struct sctp_gen_error_cause); |
| 5527 | op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA); |
| 5528 | if (op_err != NULL) { |
| 5529 | /* |
| 5530 | * Pre-reserve space for IP, SCTP, and |
| 5531 | * chunk header. |
| 5532 | */ |
| 5533 | #ifdef INET6 |
| 5534 | SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); |
| 5535 | #else |
| 5536 | SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); |
| 5537 | #endif |
| 5538 | SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); |
| 5539 | SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); |
| 5540 | SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause); |
| 5541 | cause = mtod(op_err, struct sctp_gen_error_cause *); |
| 5542 | cause->code = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR); |
| 5543 | cause->length = htons((uint16_t)(sizeof(struct sctp_gen_error_cause) + plen)); |
| 5544 | SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT); |
| 5545 | if (SCTP_BUF_NEXT(op_err) == NULL) { |
| 5546 | sctp_m_freem(op_err); |
| 5547 | op_err = NULL; |
| 5548 | op_err_last = NULL; |
| 5549 | } |
| 5550 | } |
| 5551 | return (op_err); |
| 5552 | } |
| 5553 | default: |
| 5554 | /* |
| 5555 | * we do not recognize the parameter figure out what |
| 5556 | * we do. |
| 5557 | */ |
| 5558 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype); |
| 5559 | if ((ptype & 0x4000) == 0x4000) { |
| 5560 | /* Report bit is set?? */ |
| 5561 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n"); |
| 5562 | if (op_err == NULL) { |
| 5563 | int l_len; |
| 5564 | /* Ok need to try to get an mbuf */ |
| 5565 | #ifdef INET6 |
| 5566 | l_len = SCTP_MIN_OVERHEAD; |
| 5567 | #else |
| 5568 | l_len = SCTP_MIN_V4_OVERHEAD; |
| 5569 | #endif |
| 5570 | l_len += sizeof(struct sctp_chunkhdr); |
| 5571 | l_len += sizeof(struct sctp_paramhdr); |
| 5572 | op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA); |
| 5573 | if (op_err) { |
| 5574 | SCTP_BUF_LEN(op_err) = 0; |
| 5575 | #ifdef INET6 |
| 5576 | SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); |
| 5577 | #else |
| 5578 | SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); |
| 5579 | #endif |
| 5580 | SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); |
| 5581 | SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); |
| 5582 | op_err_last = op_err; |
| 5583 | } |
| 5584 | } |
| 5585 | if (op_err != NULL) { |
| 5586 | /* If we have space */ |
| 5587 | struct sctp_paramhdr *param; |
| 5588 |
| 5589 | if (pad_needed > 0) { |
| 5590 | op_err_last = sctp_add_pad_tombuf(op_err_last, pad_needed); |
| 5591 | } |
| 5592 | if (op_err_last == NULL) { |
| 5593 | sctp_m_freem(op_err); |
| 5594 | op_err = NULL; |
| 5595 | op_err_last = NULL; |
| 5596 | goto more_processing; |
| 5597 | } |
| 5598 | if (M_TRAILINGSPACE(op_err_last) < (int)sizeof(struct sctp_paramhdr)) { |
| 5599 | m_tmp = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA); |
| 5600 | if (m_tmp == NULL) { |
| 5601 | sctp_m_freem(op_err); |
| 5602 | op_err = NULL; |
| 5603 | op_err_last = NULL; |
| 5604 | goto more_processing; |
| 5605 | } |
| 5606 | SCTP_BUF_LEN(m_tmp) = 0; |
| 5607 | SCTP_BUF_NEXT(m_tmp) = NULL; |
| 5608 | SCTP_BUF_NEXT(op_err_last) = m_tmp; |
| 5609 | op_err_last = m_tmp; |
| 5610 | } |
| 5611 | param = (struct sctp_paramhdr *)(mtod(op_err_last, caddr_t) + SCTP_BUF_LEN(op_err_last)); |
| 5612 | param->param_type = htons(SCTP_UNRECOG_PARAM); |
| 5613 | param->param_length = htons((uint16_t)sizeof(struct sctp_paramhdr) + plen); |
| 5614 | SCTP_BUF_LEN(op_err_last) += sizeof(struct sctp_paramhdr); |
| 5615 | SCTP_BUF_NEXT(op_err_last) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT); |
| 5616 | if (SCTP_BUF_NEXT(op_err_last) == NULL) { |
| 5617 | sctp_m_freem(op_err); |
| 5618 | op_err = NULL; |
| 5619 | op_err_last = NULL; |
| 5620 | goto more_processing; |
| 5621 | } else { |
| 5622 | while (SCTP_BUF_NEXT(op_err_last) != NULL) { |
| 5623 | op_err_last = SCTP_BUF_NEXT(op_err_last); |
| 5624 | } |
| 5625 | } |
| 5626 | if (plen % 4 != 0) { |
| 5627 | pad_needed = 4 - (plen % 4); |
| 5628 | } else { |
| 5629 | pad_needed = 0; |
| 5630 | } |
| 5631 | } |
| 5632 | } |
| 5633 | more_processing: |
| 5634 | if ((ptype & 0x8000) == 0x0000) { |
| 5635 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n"); |
| 5636 | return (op_err); |
| 5637 | } else { |
| 5638 | /* skip this chunk and continue processing */ |
| 5639 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n"); |
| 5640 | at += SCTP_SIZE32(plen); |
| 5641 | } |
| 5642 | break; |
| 5643 | } |
| 5644 | phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params)); |
| 5645 | } |
| 5646 | return (op_err); |
| 5647 | invalid_size: |
| 5648 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n"); |
| 5649 | *abort_processing = 1; |
| 5650 | sctp_m_freem(op_err); |
| 5651 | op_err = NULL; |
| 5652 | op_err_last = NULL; |
| 5653 | if (phdr != NULL) { |
| 5654 | struct sctp_paramhdr *param; |
| 5655 | int l_len; |
| 5656 | #ifdef INET6 |
| 5657 | l_len = SCTP_MIN_OVERHEAD; |
| 5658 | #else |
| 5659 | l_len = SCTP_MIN_V4_OVERHEAD; |
| 5660 | #endif |
| 5661 | l_len += sizeof(struct sctp_chunkhdr); |
| 5662 | l_len += (2 * sizeof(struct sctp_paramhdr)); |
| 5663 | op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA); |
| 5664 | if (op_err) { |
| 5665 | SCTP_BUF_LEN(op_err) = 0; |
| 5666 | #ifdef INET6 |
| 5667 | SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); |
| 5668 | #else |
| 5669 | SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); |
| 5670 | #endif |
| 5671 | SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); |
| 5672 | SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); |
| 5673 | SCTP_BUF_LEN(op_err) = 2 * sizeof(struct sctp_paramhdr); |
| 5674 | param = mtod(op_err, struct sctp_paramhdr *); |
| 5675 | param->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION); |
| 5676 | param->param_length = htons(2 * sizeof(struct sctp_paramhdr)); |
| 5677 | param++; |
| 5678 | param->param_type = htons(ptype); |
| 5679 | param->param_length = htons(plen); |
| 5680 | } |
| 5681 | } |
| 5682 | return (op_err); |
| 5683 | } |
| 5684 |
| 5685 | /* |
| 5686 | * Given a INIT chunk, look through the parameters to verify that there |
| 5687 | * are no new addresses. |
| 5688 | * Return true, if there is a new address or there is a problem parsing |
| 5689 | the parameters. Provide an optional error cause used when sending an ABORT. |
| 5690 | * Return false, if there are no new addresses and there is no problem in |
| 5691 | parameter processing. |
| 5692 | */ |
| 5693 | static bool |
| 5694 | sctp_are_there_new_addresses(struct sctp_association *asoc, |
| 5695 | struct mbuf *in_initpkt, int offset, int limit, struct sockaddr *src, |
| 5696 | struct mbuf **op_err) |
| 5697 | { |
| 5698 | struct sockaddr *sa_touse; |
| 5699 | struct sockaddr *sa; |
| 5700 | struct sctp_paramhdr *phdr, params; |
| 5701 | struct sctp_nets *net; |
| 5702 | #ifdef INET |
| 5703 | struct sockaddr_in sin4, *sa4; |
| 5704 | #endif |
| 5705 | #ifdef INET6 |
| 5706 | struct sockaddr_in6 sin6, *sa6; |
| 5707 | #endif |
| 5708 | #if defined(__Userspace__) |
| 5709 | struct sockaddr_conn *sac; |
| 5710 | #endif |
| 5711 | uint16_t ptype, plen; |
| 5712 | bool fnd, check_src; |
| 5713 |
| 5714 | *op_err = NULL; |
| 5715 | #ifdef INET |
| 5716 | memset(&sin4, 0, sizeof(sin4)); |
| 5717 | sin4.sin_family = AF_INET; |
| 5718 | #ifdef HAVE_SIN_LEN |
| 5719 | sin4.sin_len = sizeof(sin4); |
| 5720 | #endif |
| 5721 | #endif |
| 5722 | #ifdef INET6 |
| 5723 | memset(&sin6, 0, sizeof(sin6)); |
| 5724 | sin6.sin6_family = AF_INET6; |
| 5725 | #ifdef HAVE_SIN6_LEN |
| 5726 | sin6.sin6_len = sizeof(sin6); |
| 5727 | #endif |
| 5728 | #endif |
| 5729 | /* First what about the src address of the pkt ? */ |
| 5730 | check_src = false; |
| 5731 | switch (src->sa_family) { |
| 5732 | #ifdef INET |
| 5733 | case AF_INET: |
| 5734 | if (asoc->scope.ipv4_addr_legal) { |
| 5735 | check_src = true; |
| 5736 | } |
| 5737 | break; |
| 5738 | #endif |
| 5739 | #ifdef INET6 |
| 5740 | case AF_INET6: |
| 5741 | if (asoc->scope.ipv6_addr_legal) { |
| 5742 | check_src = true; |
| 5743 | } |
| 5744 | break; |
| 5745 | #endif |
| 5746 | #if defined(__Userspace__) |
| 5747 | case AF_CONN: |
| 5748 | if (asoc->scope.conn_addr_legal) { |
| 5749 | check_src = true; |
| 5750 | } |
| 5751 | break; |
| 5752 | #endif |
| 5753 | default: |
| 5754 | /* TSNH */ |
| 5755 | break; |
| 5756 | } |
| 5757 | if (check_src) { |
| 5758 | fnd = false; |
| 5759 | TAILQ_FOREACH(net, &asoc->nets, sctp_next) { |
| 5760 | sa = (struct sockaddr *)&net->ro._l_addr; |
| 5761 | if (sa->sa_family == src->sa_family) { |
| 5762 | #ifdef INET |
| 5763 | if (sa->sa_family == AF_INET) { |
| 5764 | struct sockaddr_in *src4; |
| 5765 |
| 5766 | sa4 = (struct sockaddr_in *)sa; |
| 5767 | src4 = (struct sockaddr_in *)src; |
| 5768 | if (sa4->sin_addr.s_addr == src4->sin_addr.s_addr) { |
| 5769 | fnd = true; |
| 5770 | break; |
| 5771 | } |
| 5772 | } |
| 5773 | #endif |
| 5774 | #ifdef INET6 |
| 5775 | if (sa->sa_family == AF_INET6) { |
| 5776 | struct sockaddr_in6 *src6; |
| 5777 |
| 5778 | sa6 = (struct sockaddr_in6 *)sa; |
| 5779 | src6 = (struct sockaddr_in6 *)src; |
| 5780 | if (SCTP6_ARE_ADDR_EQUAL(sa6, src6)) { |
| 5781 | fnd = true; |
| 5782 | break; |
| 5783 | } |
| 5784 | } |
| 5785 | #endif |
| 5786 | #if defined(__Userspace__) |
| 5787 | if (sa->sa_family == AF_CONN) { |
| 5788 | struct sockaddr_conn *srcc; |
| 5789 |
| 5790 | sac = (struct sockaddr_conn *)sa; |
| 5791 | srcc = (struct sockaddr_conn *)src; |
| 5792 | if (sac->sconn_addr == srcc->sconn_addr) { |
| 5793 | fnd = true; |
| 5794 | break; |
| 5795 | } |
| 5796 | } |
| 5797 | #endif |
| 5798 | } |
| 5799 | } |
| 5800 | if (!fnd) { |
| 5801 | /* |
| 5802 | * If sending an ABORT in case of an additional address, |
| 5803 | * don't use the new address error cause. |
| 5804 | * This looks no different than if no listener was |
| 5805 | * present. |
| 5806 | */ |
| 5807 | *op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), "Address added"); |
| 5808 | return (true); |
| 5809 | } |
| 5810 | } |
| 5811 | /* Ok so far lets munge through the rest of the packet */ |
| 5812 | offset += sizeof(struct sctp_init_chunk); |
| 5813 | phdr = sctp_get_next_param(in_initpkt, offset, ¶ms, sizeof(params)); |
| 5814 | while (phdr) { |
| 5815 | sa_touse = NULL; |
| 5816 | ptype = ntohs(phdr->param_type); |
| 5817 | plen = ntohs(phdr->param_length); |
| 5818 | if (offset + plen > limit) { |
| 5819 | *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Partial parameter"); |
| 5820 | return (true); |
| 5821 | } |
| 5822 | if (plen < sizeof(struct sctp_paramhdr)) { |
| 5823 | *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length too small"); |
| 5824 | return (true); |
| 5825 | } |
| 5826 | switch (ptype) { |
| 5827 | #ifdef INET |
| 5828 | case SCTP_IPV4_ADDRESS: |
| 5829 | { |
| 5830 | struct sctp_ipv4addr_param *p4, p4_buf; |
| 5831 |
| 5832 | if (plen != sizeof(struct sctp_ipv4addr_param)) { |
| 5833 | *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length illegal"); |
| 5834 | return (true); |
| 5835 | } |
| 5836 | phdr = sctp_get_next_param(in_initpkt, offset, |
| 5837 | (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf)); |
| 5838 | if (phdr == NULL) { |
| 5839 | *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, ""); |
| 5840 | return (true); |
| 5841 | } |
| 5842 | if (asoc->scope.ipv4_addr_legal) { |
| 5843 | p4 = (struct sctp_ipv4addr_param *)phdr; |
| 5844 | sin4.sin_addr.s_addr = p4->addr; |
| 5845 | sa_touse = (struct sockaddr *)&sin4; |
| 5846 | } |
| 5847 | break; |
| 5848 | } |
| 5849 | #endif |
| 5850 | #ifdef INET6 |
| 5851 | case SCTP_IPV6_ADDRESS: |
| 5852 | { |
| 5853 | struct sctp_ipv6addr_param *p6, p6_buf; |
| 5854 |
| 5855 | if (plen != sizeof(struct sctp_ipv6addr_param)) { |
| 5856 | *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length illegal"); |
| 5857 | return (true); |
| 5858 | } |
| 5859 | phdr = sctp_get_next_param(in_initpkt, offset, |
| 5860 | (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf)); |
| 5861 | if (phdr == NULL) { |
| 5862 | *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, ""); |
| 5863 | return (true); |
| 5864 | } |
| 5865 | if (asoc->scope.ipv6_addr_legal) { |
| 5866 | p6 = (struct sctp_ipv6addr_param *)phdr; |
| 5867 | memcpy((caddr_t)&sin6.sin6_addr, p6->addr, |
| 5868 | sizeof(p6->addr)); |
| 5869 | sa_touse = (struct sockaddr *)&sin6; |
| 5870 | } |
| 5871 | break; |
| 5872 | } |
| 5873 | #endif |
| 5874 | default: |
| 5875 | sa_touse = NULL; |
| 5876 | break; |
| 5877 | } |
| 5878 | if (sa_touse) { |
| 5879 | /* ok, sa_touse points to one to check */ |
| 5880 | fnd = false; |
| 5881 | TAILQ_FOREACH(net, &asoc->nets, sctp_next) { |
| 5882 | sa = (struct sockaddr *)&net->ro._l_addr; |
| 5883 | if (sa->sa_family != sa_touse->sa_family) { |
| 5884 | continue; |
| 5885 | } |
| 5886 | #ifdef INET |
| 5887 | if (sa->sa_family == AF_INET) { |
| 5888 | sa4 = (struct sockaddr_in *)sa; |
| 5889 | if (sa4->sin_addr.s_addr == |
| 5890 | sin4.sin_addr.s_addr) { |
| 5891 | fnd = true; |
| 5892 | break; |
| 5893 | } |
| 5894 | } |
| 5895 | #endif |
| 5896 | #ifdef INET6 |
| 5897 | if (sa->sa_family == AF_INET6) { |
| 5898 | sa6 = (struct sockaddr_in6 *)sa; |
| 5899 | if (SCTP6_ARE_ADDR_EQUAL( |
| 5900 | sa6, &sin6)) { |
| 5901 | fnd = true; |
| 5902 | break; |
| 5903 | } |
| 5904 | } |
| 5905 | #endif |
| 5906 | } |
| 5907 | if (!fnd) { |
| 5908 | /* |
| 5909 | * If sending an ABORT in case of an additional |
| 5910 | * address, don't use the new address error |
| 5911 | * cause. |
| 5912 | * This looks no different than if no listener |
| 5913 | * was present. |
| 5914 | */ |
| 5915 | *op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), "Address added"); |
| 5916 | return (true); |
| 5917 | } |
| 5918 | } |
| 5919 | offset += SCTP_SIZE32(plen); |
| 5920 | if (offset >= limit) { |
| 5921 | break; |
| 5922 | } |
| 5923 | phdr = sctp_get_next_param(in_initpkt, offset, ¶ms, sizeof(params)); |
| 5924 | } |
| 5925 | return (false); |
| 5926 | } |
| 5927 |
| 5928 | /* |
| 5929 | * Given a MBUF chain that was sent into us containing an INIT. Build a |
| 5930 | * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done |
| 5931 | * a pullup to include IPv6/4header, SCTP header and initial part of INIT |
| 5932 | * message (i.e. the struct sctp_init_msg). |
| 5933 | */ |
| 5934 | void |
| 5935 | sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb, |
| 5936 | struct sctp_nets *src_net, struct mbuf *init_pkt, |
| 5937 | int iphlen, int offset, |
| 5938 | struct sockaddr *src, struct sockaddr *dst, |
| 5939 | struct sctphdr *sh, struct sctp_init_chunk *init_chk, |
| 5940 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 5941 | uint8_t mflowtype, uint32_t mflowid, |
| 5942 | #endif |
| 5943 | uint32_t vrf_id, uint16_t port) |
| 5944 | { |
| 5945 | struct sctp_association *asoc; |
| 5946 | struct mbuf *m, *m_tmp, *m_last, *m_cookie, *op_err; |
| 5947 | struct sctp_init_ack_chunk *initack; |
| 5948 | struct sctp_adaptation_layer_indication *ali; |
| 5949 | struct sctp_supported_chunk_types_param *pr_supported; |
| 5950 | struct sctp_paramhdr *ph; |
| 5951 | union sctp_sockstore *over_addr; |
| 5952 | struct sctp_scoping scp; |
| 5953 | struct timeval now; |
| 5954 | #ifdef INET |
| 5955 | struct sockaddr_in *dst4 = (struct sockaddr_in *)dst; |
| 5956 | struct sockaddr_in *src4 = (struct sockaddr_in *)src; |
| 5957 | struct sockaddr_in *sin; |
| 5958 | #endif |
| 5959 | #ifdef INET6 |
| 5960 | struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst; |
| 5961 | struct sockaddr_in6 *src6 = (struct sockaddr_in6 *)src; |
| 5962 | struct sockaddr_in6 *sin6; |
| 5963 | #endif |
| 5964 | #if defined(__Userspace__) |
| 5965 | struct sockaddr_conn *dstconn = (struct sockaddr_conn *)dst; |
| 5966 | struct sockaddr_conn *srcconn = (struct sockaddr_conn *)src; |
| 5967 | struct sockaddr_conn *sconn; |
| 5968 | #endif |
| 5969 | struct sockaddr *to; |
| 5970 | struct sctp_state_cookie stc; |
| 5971 | struct sctp_nets *net = NULL; |
| 5972 | uint8_t *signature = NULL; |
| 5973 | int cnt_inits_to = 0; |
| 5974 | uint16_t his_limit, i_want; |
| 5975 | int abort_flag; |
| 5976 | int nat_friendly = 0; |
| 5977 | int error; |
| 5978 | struct socket *so; |
| 5979 | uint16_t num_ext, chunk_len, padding_len, parameter_len; |
| 5980 |
| 5981 | if (stcb) { |
| 5982 | asoc = &stcb->asoc; |
| 5983 | } else { |
| 5984 | asoc = NULL; |
| 5985 | } |
| 5986 | if ((asoc != NULL) && |
| 5987 | (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT)) { |
| 5988 | if (sctp_are_there_new_addresses(asoc, init_pkt, offset, offset + ntohs(init_chk->ch.chunk_length), src, &op_err)) { |
| 5989 | /* |
| 5990 | * new addresses, out of here in non-cookie-wait states |
| 5991 | */ |
| 5992 | sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err, |
| 5993 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 5994 | mflowtype, mflowid, inp->fibnum, |
| 5995 | #endif |
| 5996 | vrf_id, port); |
| 5997 | return; |
| 5998 | } |
| 5999 | if (src_net != NULL && (src_net->port != port)) { |
| 6000 | /* |
| 6001 | * change of remote encapsulation port, out of here in |
| 6002 | * non-cookie-wait states |
| 6003 | * |
| 6004 | * Send an ABORT, without an specific error cause. |
| 6005 | * This looks no different than if no listener |
| 6006 | * was present. |
| 6007 | */ |
| 6008 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 6009 | "Remote encapsulation port changed"); |
| 6010 | sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err, |
| 6011 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 6012 | mflowtype, mflowid, inp->fibnum, |
| 6013 | #endif |
| 6014 | vrf_id, port); |
| 6015 | return; |
| 6016 | } |
| 6017 | } |
| 6018 | abort_flag = 0; |
| 6019 | op_err = sctp_arethere_unrecognized_parameters(init_pkt, |
| 6020 | (offset + sizeof(struct sctp_init_chunk)), |
| 6021 | &abort_flag, |
| 6022 | (struct sctp_chunkhdr *)init_chk, |
| 6023 | &nat_friendly, NULL); |
| 6024 | if (abort_flag) { |
| 6025 | do_a_abort: |
| 6026 | if (op_err == NULL) { |
| 6027 | char msg[SCTP_DIAG_INFO_LEN]; |
| 6028 |
| 6029 | SCTP_SNPRINTF(msg, sizeof(msg), "%s:%d at %s", __FILE__, __LINE__, __func__); |
| 6030 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 6031 | msg); |
| 6032 | } |
| 6033 | sctp_send_abort(init_pkt, iphlen, src, dst, sh, |
| 6034 | init_chk->init.initiate_tag, op_err, |
| 6035 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 6036 | mflowtype, mflowid, inp->fibnum, |
| 6037 | #endif |
| 6038 | vrf_id, port); |
| 6039 | return; |
| 6040 | } |
| 6041 | m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); |
| 6042 | if (m == NULL) { |
| 6043 | /* No memory, INIT timer will re-attempt. */ |
| 6044 | sctp_m_freem(op_err); |
| 6045 | return; |
| 6046 | } |
| 6047 | chunk_len = (uint16_t)sizeof(struct sctp_init_ack_chunk); |
| 6048 | padding_len = 0; |
| 6049 |
| 6050 | /* |
| 6051 | * We might not overwrite the identification[] completely and on |
| 6052 | * some platforms time_entered will contain some padding. |
| 6053 | * Therefore zero out the cookie to avoid putting |
| 6054 | * uninitialized memory on the wire. |
| 6055 | */ |
| 6056 | memset(&stc, 0, sizeof(struct sctp_state_cookie)); |
| 6057 |
| 6058 | /* the time I built cookie */ |
| 6059 | (void)SCTP_GETTIME_TIMEVAL(&now); |
| 6060 | stc.time_entered.tv_sec = now.tv_sec; |
| 6061 | stc.time_entered.tv_usec = now.tv_usec; |
| 6062 |
| 6063 | /* populate any tie tags */ |
| 6064 | if (asoc != NULL) { |
| 6065 | /* unlock before tag selections */ |
| 6066 | stc.tie_tag_my_vtag = asoc->my_vtag_nonce; |
| 6067 | stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce; |
| 6068 | stc.cookie_life = asoc->cookie_life; |
| 6069 | net = asoc->primary_destination; |
| 6070 | } else { |
| 6071 | stc.tie_tag_my_vtag = 0; |
| 6072 | stc.tie_tag_peer_vtag = 0; |
| 6073 | /* life I will award this cookie */ |
| 6074 | stc.cookie_life = inp->sctp_ep.def_cookie_life; |
| 6075 | } |
| 6076 |
| 6077 | /* copy in the ports for later check */ |
| 6078 | stc.myport = sh->dest_port; |
| 6079 | stc.peerport = sh->src_port; |
| 6080 |
| 6081 | /* |
| 6082 | * If we wanted to honor cookie life extensions, we would add to |
| 6083 | * stc.cookie_life. For now we should NOT honor any extension |
| 6084 | */ |
| 6085 | stc.site_scope = stc.local_scope = stc.loopback_scope = 0; |
| 6086 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { |
| 6087 | stc.ipv6_addr_legal = 1; |
| 6088 | if (SCTP_IPV6_V6ONLY(inp)) { |
| 6089 | stc.ipv4_addr_legal = 0; |
| 6090 | } else { |
| 6091 | stc.ipv4_addr_legal = 1; |
| 6092 | } |
| 6093 | #if defined(__Userspace__) |
| 6094 | stc.conn_addr_legal = 0; |
| 6095 | #endif |
| 6096 | } else { |
| 6097 | stc.ipv6_addr_legal = 0; |
| 6098 | #if defined(__Userspace__) |
| 6099 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) { |
| 6100 | stc.conn_addr_legal = 1; |
| 6101 | stc.ipv4_addr_legal = 0; |
| 6102 | } else { |
| 6103 | stc.conn_addr_legal = 0; |
| 6104 | stc.ipv4_addr_legal = 1; |
| 6105 | } |
| 6106 | #else |
| 6107 | stc.ipv4_addr_legal = 1; |
| 6108 | #endif |
| 6109 | } |
| 6110 | stc.ipv4_scope = 0; |
| 6111 | if (net == NULL) { |
| 6112 | to = src; |
| 6113 | switch (dst->sa_family) { |
| 6114 | #ifdef INET |
| 6115 | case AF_INET: |
| 6116 | { |
| 6117 | /* lookup address */ |
| 6118 | stc.address[0] = src4->sin_addr.s_addr; |
| 6119 | stc.address[1] = 0; |
| 6120 | stc.address[2] = 0; |
| 6121 | stc.address[3] = 0; |
| 6122 | stc.addr_type = SCTP_IPV4_ADDRESS; |
| 6123 | /* local from address */ |
| 6124 | stc.laddress[0] = dst4->sin_addr.s_addr; |
| 6125 | stc.laddress[1] = 0; |
| 6126 | stc.laddress[2] = 0; |
| 6127 | stc.laddress[3] = 0; |
| 6128 | stc.laddr_type = SCTP_IPV4_ADDRESS; |
| 6129 | /* scope_id is only for v6 */ |
| 6130 | stc.scope_id = 0; |
| 6131 | if ((IN4_ISPRIVATE_ADDRESS(&src4->sin_addr)) || |
| 6132 | (IN4_ISPRIVATE_ADDRESS(&dst4->sin_addr))){ |
| 6133 | stc.ipv4_scope = 1; |
| 6134 | } |
| 6135 | /* Must use the address in this case */ |
| 6136 | if (sctp_is_address_on_local_host(src, vrf_id)) { |
| 6137 | stc.loopback_scope = 1; |
| 6138 | stc.ipv4_scope = 1; |
| 6139 | stc.site_scope = 1; |
| 6140 | stc.local_scope = 0; |
| 6141 | } |
| 6142 | break; |
| 6143 | } |
| 6144 | #endif |
| 6145 | #ifdef INET6 |
| 6146 | case AF_INET6: |
| 6147 | { |
| 6148 | stc.addr_type = SCTP_IPV6_ADDRESS; |
| 6149 | memcpy(&stc.address, &src6->sin6_addr, sizeof(struct in6_addr)); |
| 6150 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 6151 | stc.scope_id = ntohs(in6_getscope(&src6->sin6_addr)); |
| 6152 | #else |
| 6153 | stc.scope_id = 0; |
| 6154 | #endif |
| 6155 | if (sctp_is_address_on_local_host(src, vrf_id)) { |
| 6156 | stc.loopback_scope = 1; |
| 6157 | stc.local_scope = 0; |
| 6158 | stc.site_scope = 1; |
| 6159 | stc.ipv4_scope = 1; |
| 6160 | } else if (IN6_IS_ADDR_LINKLOCAL(&src6->sin6_addr) || |
| 6161 | IN6_IS_ADDR_LINKLOCAL(&dst6->sin6_addr)) { |
| 6162 | /* |
| 6163 | * If the new destination or source is a |
| 6164 | * LINK_LOCAL we must have common both site and |
| 6165 | * local scope. Don't set local scope though |
| 6166 | * since we must depend on the source to be |
| 6167 | * added implicitly. We cannot assure just |
| 6168 | * because we share one link that all links are |
| 6169 | * common. |
| 6170 | */ |
| 6171 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 6172 | /* Mac OS X currently doesn't have in6_getscope() */ |
| 6173 | stc.scope_id = src6->sin6_addr.s6_addr16[1]; |
| 6174 | #endif |
| 6175 | stc.local_scope = 0; |
| 6176 | stc.site_scope = 1; |
| 6177 | stc.ipv4_scope = 1; |
| 6178 | /* |
| 6179 | * we start counting for the private address |
| 6180 | * stuff at 1. since the link local we |
| 6181 | * source from won't show up in our scoped |
| 6182 | * count. |
| 6183 | */ |
| 6184 | cnt_inits_to = 1; |
| 6185 | /* pull out the scope_id from incoming pkt */ |
| 6186 | } else if (IN6_IS_ADDR_SITELOCAL(&src6->sin6_addr) || |
| 6187 | IN6_IS_ADDR_SITELOCAL(&dst6->sin6_addr)) { |
| 6188 | /* |
| 6189 | * If the new destination or source is |
| 6190 | * SITE_LOCAL then we must have site scope in |
| 6191 | * common. |
| 6192 | */ |
| 6193 | stc.site_scope = 1; |
| 6194 | } |
| 6195 | memcpy(&stc.laddress, &dst6->sin6_addr, sizeof(struct in6_addr)); |
| 6196 | stc.laddr_type = SCTP_IPV6_ADDRESS; |
| 6197 | break; |
| 6198 | } |
| 6199 | #endif |
| 6200 | #if defined(__Userspace__) |
| 6201 | case AF_CONN: |
| 6202 | { |
| 6203 | /* lookup address */ |
| 6204 | stc.address[0] = 0; |
| 6205 | stc.address[1] = 0; |
| 6206 | stc.address[2] = 0; |
| 6207 | stc.address[3] = 0; |
| 6208 | memcpy(&stc.address, &srcconn->sconn_addr, sizeof(void *)); |
| 6209 | stc.addr_type = SCTP_CONN_ADDRESS; |
| 6210 | /* local from address */ |
| 6211 | stc.laddress[0] = 0; |
| 6212 | stc.laddress[1] = 0; |
| 6213 | stc.laddress[2] = 0; |
| 6214 | stc.laddress[3] = 0; |
| 6215 | memcpy(&stc.laddress, &dstconn->sconn_addr, sizeof(void *)); |
| 6216 | stc.laddr_type = SCTP_CONN_ADDRESS; |
| 6217 | /* scope_id is only for v6 */ |
| 6218 | stc.scope_id = 0; |
| 6219 | break; |
| 6220 | } |
| 6221 | #endif |
| 6222 | default: |
| 6223 | /* TSNH */ |
| 6224 | goto do_a_abort; |
| 6225 | break; |
| 6226 | } |
| 6227 | } else { |
| 6228 | /* set the scope per the existing tcb */ |
| 6229 |
| 6230 | #ifdef INET6 |
| 6231 | struct sctp_nets *lnet; |
| 6232 | #endif |
| 6233 |
| 6234 | stc.loopback_scope = asoc->scope.loopback_scope; |
| 6235 | stc.ipv4_scope = asoc->scope.ipv4_local_scope; |
| 6236 | stc.site_scope = asoc->scope.site_scope; |
| 6237 | stc.local_scope = asoc->scope.local_scope; |
| 6238 | #ifdef INET6 |
| 6239 | /* Why do we not consider IPv4 LL addresses? */ |
| 6240 | TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { |
| 6241 | if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) { |
| 6242 | if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) { |
| 6243 | /* |
| 6244 | * if we have a LL address, start |
| 6245 | * counting at 1. |
| 6246 | */ |
| 6247 | cnt_inits_to = 1; |
| 6248 | } |
| 6249 | } |
| 6250 | } |
| 6251 | #endif |
| 6252 | /* use the net pointer */ |
| 6253 | to = (struct sockaddr *)&net->ro._l_addr; |
| 6254 | switch (to->sa_family) { |
| 6255 | #ifdef INET |
| 6256 | case AF_INET: |
| 6257 | sin = (struct sockaddr_in *)to; |
| 6258 | stc.address[0] = sin->sin_addr.s_addr; |
| 6259 | stc.address[1] = 0; |
| 6260 | stc.address[2] = 0; |
| 6261 | stc.address[3] = 0; |
| 6262 | stc.addr_type = SCTP_IPV4_ADDRESS; |
| 6263 | if (net->src_addr_selected == 0) { |
| 6264 | /* |
| 6265 | * strange case here, the INIT should have |
| 6266 | * did the selection. |
| 6267 | */ |
| 6268 | net->ro._s_addr = sctp_source_address_selection(inp, |
| 6269 | stcb, (sctp_route_t *)&net->ro, |
| 6270 | net, 0, vrf_id); |
| 6271 | if (net->ro._s_addr == NULL) { |
| 6272 | sctp_m_freem(op_err); |
| 6273 | sctp_m_freem(m); |
| 6274 | return; |
| 6275 | } |
| 6276 |
| 6277 | net->src_addr_selected = 1; |
| 6278 | } |
| 6279 | stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr; |
| 6280 | stc.laddress[1] = 0; |
| 6281 | stc.laddress[2] = 0; |
| 6282 | stc.laddress[3] = 0; |
| 6283 | stc.laddr_type = SCTP_IPV4_ADDRESS; |
| 6284 | /* scope_id is only for v6 */ |
| 6285 | stc.scope_id = 0; |
| 6286 | break; |
| 6287 | #endif |
| 6288 | #ifdef INET6 |
| 6289 | case AF_INET6: |
| 6290 | sin6 = (struct sockaddr_in6 *)to; |
| 6291 | memcpy(&stc.address, &sin6->sin6_addr, |
| 6292 | sizeof(struct in6_addr)); |
| 6293 | stc.addr_type = SCTP_IPV6_ADDRESS; |
| 6294 | stc.scope_id = sin6->sin6_scope_id; |
| 6295 | if (net->src_addr_selected == 0) { |
| 6296 | /* |
| 6297 | * strange case here, the INIT should have |
| 6298 | * done the selection. |
| 6299 | */ |
| 6300 | net->ro._s_addr = sctp_source_address_selection(inp, |
| 6301 | stcb, (sctp_route_t *)&net->ro, |
| 6302 | net, 0, vrf_id); |
| 6303 | if (net->ro._s_addr == NULL) { |
| 6304 | sctp_m_freem(op_err); |
| 6305 | sctp_m_freem(m); |
| 6306 | return; |
| 6307 | } |
| 6308 |
| 6309 | net->src_addr_selected = 1; |
| 6310 | } |
| 6311 | memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr, |
| 6312 | sizeof(struct in6_addr)); |
| 6313 | stc.laddr_type = SCTP_IPV6_ADDRESS; |
| 6314 | break; |
| 6315 | #endif |
| 6316 | #if defined(__Userspace__) |
| 6317 | case AF_CONN: |
| 6318 | sconn = (struct sockaddr_conn *)to; |
| 6319 | stc.address[0] = 0; |
| 6320 | stc.address[1] = 0; |
| 6321 | stc.address[2] = 0; |
| 6322 | stc.address[3] = 0; |
| 6323 | memcpy(&stc.address, &sconn->sconn_addr, sizeof(void *)); |
| 6324 | stc.addr_type = SCTP_CONN_ADDRESS; |
| 6325 | stc.laddress[0] = 0; |
| 6326 | stc.laddress[1] = 0; |
| 6327 | stc.laddress[2] = 0; |
| 6328 | stc.laddress[3] = 0; |
| 6329 | memcpy(&stc.laddress, &sconn->sconn_addr, sizeof(void *)); |
| 6330 | stc.laddr_type = SCTP_CONN_ADDRESS; |
| 6331 | stc.scope_id = 0; |
| 6332 | break; |
| 6333 | #endif |
| 6334 | } |
| 6335 | } |
| 6336 | /* Now lets put the SCTP header in place */ |
| 6337 | initack = mtod(m, struct sctp_init_ack_chunk *); |
| 6338 | /* Save it off for quick ref */ |
| 6339 | stc.peers_vtag = ntohl(init_chk->init.initiate_tag); |
| 6340 | /* who are we */ |
| 6341 | memcpy(stc.identification, SCTP_VERSION_STRING, |
| 6342 | min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification))); |
| 6343 | memset(stc.reserved, 0, SCTP_RESERVE_SPACE); |
| 6344 | /* now the chunk header */ |
| 6345 | initack->ch.chunk_type = SCTP_INITIATION_ACK; |
| 6346 | initack->ch.chunk_flags = 0; |
| 6347 | /* fill in later from mbuf we build */ |
| 6348 | initack->ch.chunk_length = 0; |
| 6349 | /* place in my tag */ |
| 6350 | if ((asoc != NULL) && |
| 6351 | ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) || |
| 6352 | (SCTP_GET_STATE(stcb) == SCTP_STATE_INUSE) || |
| 6353 | (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED))) { |
| 6354 | /* re-use the v-tags and init-seq here */ |
| 6355 | initack->init.initiate_tag = htonl(asoc->my_vtag); |
| 6356 | initack->init.initial_tsn = htonl(asoc->init_seq_number); |
| 6357 | } else { |
| 6358 | uint32_t vtag, itsn; |
| 6359 |
| 6360 | if (asoc) { |
| 6361 | atomic_add_int(&asoc->refcnt, 1); |
| 6362 | SCTP_TCB_UNLOCK(stcb); |
| 6363 | new_tag: |
| 6364 | SCTP_INP_INFO_RLOCK(); |
| 6365 | vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1); |
| 6366 | SCTP_INP_INFO_RUNLOCK(); |
| 6367 | if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) { |
| 6368 | /* Got a duplicate vtag on some guy behind a nat |
| 6369 | * make sure we don't use it. |
| 6370 | */ |
| 6371 | goto new_tag; |
| 6372 | } |
| 6373 | initack->init.initiate_tag = htonl(vtag); |
| 6374 | /* get a TSN to use too */ |
| 6375 | itsn = sctp_select_initial_TSN(&inp->sctp_ep); |
| 6376 | initack->init.initial_tsn = htonl(itsn); |
| 6377 | SCTP_TCB_LOCK(stcb); |
| 6378 | atomic_add_int(&asoc->refcnt, -1); |
| 6379 | } else { |
| 6380 | SCTP_INP_INCR_REF(inp); |
| 6381 | SCTP_INP_RUNLOCK(inp); |
| 6382 | SCTP_INP_INFO_RLOCK(); |
| 6383 | vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1); |
| 6384 | SCTP_INP_INFO_RUNLOCK(); |
| 6385 | initack->init.initiate_tag = htonl(vtag); |
| 6386 | /* get a TSN to use too */ |
| 6387 | initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep)); |
| 6388 | SCTP_INP_RLOCK(inp); |
| 6389 | SCTP_INP_DECR_REF(inp); |
| 6390 | } |
| 6391 | } |
| 6392 | /* save away my tag to */ |
| 6393 | stc.my_vtag = initack->init.initiate_tag; |
| 6394 |
| 6395 | /* set up some of the credits. */ |
| 6396 | so = inp->sctp_socket; |
| 6397 | if (so == NULL) { |
| 6398 | /* memory problem */ |
| 6399 | sctp_m_freem(op_err); |
| 6400 | sctp_m_freem(m); |
| 6401 | return; |
| 6402 | } else { |
| 6403 | initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND)); |
| 6404 | } |
| 6405 | /* set what I want */ |
| 6406 | his_limit = ntohs(init_chk->init.num_inbound_streams); |
| 6407 | /* choose what I want */ |
| 6408 | if (asoc != NULL) { |
| 6409 | if (asoc->streamoutcnt > asoc->pre_open_streams) { |
| 6410 | i_want = asoc->streamoutcnt; |
| 6411 | } else { |
| 6412 | i_want = asoc->pre_open_streams; |
| 6413 | } |
| 6414 | } else { |
| 6415 | i_want = inp->sctp_ep.pre_open_stream_count; |
| 6416 | } |
| 6417 | if (his_limit < i_want) { |
| 6418 | /* I Want more :< */ |
| 6419 | initack->init.num_outbound_streams = init_chk->init.num_inbound_streams; |
| 6420 | } else { |
| 6421 | /* I can have what I want :> */ |
| 6422 | initack->init.num_outbound_streams = htons(i_want); |
| 6423 | } |
| 6424 | /* tell him his limit. */ |
| 6425 | initack->init.num_inbound_streams = |
| 6426 | htons(inp->sctp_ep.max_open_streams_intome); |
| 6427 |
| 6428 | /* adaptation layer indication parameter */ |
| 6429 | if (inp->sctp_ep.adaptation_layer_indicator_provided) { |
| 6430 | parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication); |
| 6431 | ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t) + chunk_len); |
| 6432 | ali->ph.param_type = htons(SCTP_ULP_ADAPTATION); |
| 6433 | ali->ph.param_length = htons(parameter_len); |
| 6434 | ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator); |
| 6435 | chunk_len += parameter_len; |
| 6436 | } |
| 6437 |
| 6438 | /* ECN parameter */ |
| 6439 | if (((asoc != NULL) && (asoc->ecn_supported == 1)) || |
| 6440 | ((asoc == NULL) && (inp->ecn_supported == 1))) { |
| 6441 | parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); |
| 6442 | ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len); |
| 6443 | ph->param_type = htons(SCTP_ECN_CAPABLE); |
| 6444 | ph->param_length = htons(parameter_len); |
| 6445 | chunk_len += parameter_len; |
| 6446 | } |
| 6447 |
| 6448 | /* PR-SCTP supported parameter */ |
| 6449 | if (((asoc != NULL) && (asoc->prsctp_supported == 1)) || |
| 6450 | ((asoc == NULL) && (inp->prsctp_supported == 1))) { |
| 6451 | parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); |
| 6452 | ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len); |
| 6453 | ph->param_type = htons(SCTP_PRSCTP_SUPPORTED); |
| 6454 | ph->param_length = htons(parameter_len); |
| 6455 | chunk_len += parameter_len; |
| 6456 | } |
| 6457 |
| 6458 | /* Add NAT friendly parameter */ |
| 6459 | if (nat_friendly) { |
| 6460 | parameter_len = (uint16_t)sizeof(struct sctp_paramhdr); |
| 6461 | ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len); |
| 6462 | ph->param_type = htons(SCTP_HAS_NAT_SUPPORT); |
| 6463 | ph->param_length = htons(parameter_len); |
| 6464 | chunk_len += parameter_len; |
| 6465 | } |
| 6466 |
| 6467 | /* And now tell the peer which extensions we support */ |
| 6468 | num_ext = 0; |
| 6469 | pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t) + chunk_len); |
| 6470 | if (((asoc != NULL) && (asoc->prsctp_supported == 1)) || |
| 6471 | ((asoc == NULL) && (inp->prsctp_supported == 1))) { |
| 6472 | pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN; |
| 6473 | if (((asoc != NULL) && (asoc->idata_supported == 1)) || |
| 6474 | ((asoc == NULL) && (inp->idata_supported == 1))) { |
| 6475 | pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN; |
| 6476 | } |
| 6477 | } |
| 6478 | if (((asoc != NULL) && (asoc->auth_supported == 1)) || |
| 6479 | ((asoc == NULL) && (inp->auth_supported == 1))) { |
| 6480 | pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION; |
| 6481 | } |
| 6482 | if (((asoc != NULL) && (asoc->asconf_supported == 1)) || |
| 6483 | ((asoc == NULL) && (inp->asconf_supported == 1))) { |
| 6484 | pr_supported->chunk_types[num_ext++] = SCTP_ASCONF; |
| 6485 | pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK; |
| 6486 | } |
| 6487 | if (((asoc != NULL) && (asoc->reconfig_supported == 1)) || |
| 6488 | ((asoc == NULL) && (inp->reconfig_supported == 1))) { |
| 6489 | pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET; |
| 6490 | } |
| 6491 | if (((asoc != NULL) && (asoc->idata_supported == 1)) || |
| 6492 | ((asoc == NULL) && (inp->idata_supported == 1))) { |
| 6493 | pr_supported->chunk_types[num_ext++] = SCTP_IDATA; |
| 6494 | } |
| 6495 | if (((asoc != NULL) && (asoc->nrsack_supported == 1)) || |
| 6496 | ((asoc == NULL) && (inp->nrsack_supported == 1))) { |
| 6497 | pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK; |
| 6498 | } |
| 6499 | if (((asoc != NULL) && (asoc->pktdrop_supported == 1)) || |
| 6500 | ((asoc == NULL) && (inp->pktdrop_supported == 1))) { |
| 6501 | pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED; |
| 6502 | } |
| 6503 | if (num_ext > 0) { |
| 6504 | parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext; |
| 6505 | pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT); |
| 6506 | pr_supported->ph.param_length = htons(parameter_len); |
| 6507 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 6508 | chunk_len += parameter_len; |
| 6509 | } |
| 6510 |
| 6511 | /* add authentication parameters */ |
| 6512 | if (((asoc != NULL) && (asoc->auth_supported == 1)) || |
| 6513 | ((asoc == NULL) && (inp->auth_supported == 1))) { |
| 6514 | struct sctp_auth_random *randp; |
| 6515 | struct sctp_auth_hmac_algo *hmacs; |
| 6516 | struct sctp_auth_chunk_list *chunks; |
| 6517 |
| 6518 | if (padding_len > 0) { |
| 6519 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 6520 | chunk_len += padding_len; |
| 6521 | padding_len = 0; |
| 6522 | } |
| 6523 | /* generate and add RANDOM parameter */ |
| 6524 | randp = (struct sctp_auth_random *)(mtod(m, caddr_t) + chunk_len); |
| 6525 | parameter_len = (uint16_t)sizeof(struct sctp_auth_random) + |
| 6526 | SCTP_AUTH_RANDOM_SIZE_DEFAULT; |
| 6527 | randp->ph.param_type = htons(SCTP_RANDOM); |
| 6528 | randp->ph.param_length = htons(parameter_len); |
| 6529 | SCTP_READ_RANDOM(randp->random_data, SCTP_AUTH_RANDOM_SIZE_DEFAULT); |
| 6530 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 6531 | chunk_len += parameter_len; |
| 6532 |
| 6533 | if (padding_len > 0) { |
| 6534 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 6535 | chunk_len += padding_len; |
| 6536 | padding_len = 0; |
| 6537 | } |
| 6538 | /* add HMAC_ALGO parameter */ |
| 6539 | hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t) + chunk_len); |
| 6540 | parameter_len = (uint16_t)sizeof(struct sctp_auth_hmac_algo) + |
| 6541 | sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs, |
| 6542 | (uint8_t *)hmacs->hmac_ids); |
| 6543 | hmacs->ph.param_type = htons(SCTP_HMAC_LIST); |
| 6544 | hmacs->ph.param_length = htons(parameter_len); |
| 6545 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 6546 | chunk_len += parameter_len; |
| 6547 |
| 6548 | if (padding_len > 0) { |
| 6549 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 6550 | chunk_len += padding_len; |
| 6551 | padding_len = 0; |
| 6552 | } |
| 6553 | /* add CHUNKS parameter */ |
| 6554 | chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t) + chunk_len); |
| 6555 | parameter_len = (uint16_t)sizeof(struct sctp_auth_chunk_list) + |
| 6556 | sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks, |
| 6557 | chunks->chunk_types); |
| 6558 | chunks->ph.param_type = htons(SCTP_CHUNK_LIST); |
| 6559 | chunks->ph.param_length = htons(parameter_len); |
| 6560 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 6561 | chunk_len += parameter_len; |
| 6562 | } |
| 6563 | SCTP_BUF_LEN(m) = chunk_len; |
| 6564 | m_last = m; |
| 6565 | /* now the addresses */ |
| 6566 | /* To optimize this we could put the scoping stuff |
| 6567 | * into a structure and remove the individual uint8's from |
| 6568 | * the stc structure. Then we could just sifa in the |
| 6569 | * address within the stc.. but for now this is a quick |
| 6570 | * hack to get the address stuff teased apart. |
| 6571 | */ |
| 6572 | scp.ipv4_addr_legal = stc.ipv4_addr_legal; |
| 6573 | scp.ipv6_addr_legal = stc.ipv6_addr_legal; |
| 6574 | #if defined(__Userspace__) |
| 6575 | scp.conn_addr_legal = stc.conn_addr_legal; |
| 6576 | #endif |
| 6577 | scp.loopback_scope = stc.loopback_scope; |
| 6578 | scp.ipv4_local_scope = stc.ipv4_scope; |
| 6579 | scp.local_scope = stc.local_scope; |
| 6580 | scp.site_scope = stc.site_scope; |
| 6581 | m_last = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_last, |
| 6582 | cnt_inits_to, |
| 6583 | &padding_len, &chunk_len); |
| 6584 | /* padding_len can only be positive, if no addresses have been added */ |
| 6585 | if (padding_len > 0) { |
| 6586 | memset(mtod(m, caddr_t) + chunk_len, 0, padding_len); |
| 6587 | chunk_len += padding_len; |
| 6588 | SCTP_BUF_LEN(m) += padding_len; |
| 6589 | padding_len = 0; |
| 6590 | } |
| 6591 |
| 6592 | /* tack on the operational error if present */ |
| 6593 | if (op_err) { |
| 6594 | parameter_len = 0; |
| 6595 | for (m_tmp = op_err; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) { |
| 6596 | parameter_len += SCTP_BUF_LEN(m_tmp); |
| 6597 | } |
| 6598 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 6599 | SCTP_BUF_NEXT(m_last) = op_err; |
| 6600 | while (SCTP_BUF_NEXT(m_last) != NULL) { |
| 6601 | m_last = SCTP_BUF_NEXT(m_last); |
| 6602 | } |
| 6603 | chunk_len += parameter_len; |
| 6604 | } |
| 6605 | if (padding_len > 0) { |
| 6606 | m_last = sctp_add_pad_tombuf(m_last, padding_len); |
| 6607 | if (m_last == NULL) { |
| 6608 | /* Houston we have a problem, no space */ |
| 6609 | sctp_m_freem(m); |
| 6610 | return; |
| 6611 | } |
| 6612 | chunk_len += padding_len; |
| 6613 | padding_len = 0; |
| 6614 | } |
| 6615 | /* Now we must build a cookie */ |
| 6616 | m_cookie = sctp_add_cookie(init_pkt, offset, m, 0, &stc, &signature); |
| 6617 | if (m_cookie == NULL) { |
| 6618 | /* memory problem */ |
| 6619 | sctp_m_freem(m); |
| 6620 | return; |
| 6621 | } |
| 6622 | /* Now append the cookie to the end and update the space/size */ |
| 6623 | SCTP_BUF_NEXT(m_last) = m_cookie; |
| 6624 | parameter_len = 0; |
| 6625 | for (m_tmp = m_cookie; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) { |
| 6626 | parameter_len += SCTP_BUF_LEN(m_tmp); |
| 6627 | if (SCTP_BUF_NEXT(m_tmp) == NULL) { |
| 6628 | m_last = m_tmp; |
| 6629 | } |
| 6630 | } |
| 6631 | padding_len = SCTP_SIZE32(parameter_len) - parameter_len; |
| 6632 | chunk_len += parameter_len; |
| 6633 |
| 6634 | /* Place in the size, but we don't include |
| 6635 | * the last pad (if any) in the INIT-ACK. |
| 6636 | */ |
| 6637 | initack->ch.chunk_length = htons(chunk_len); |
| 6638 |
| 6639 | /* Time to sign the cookie, we don't sign over the cookie |
| 6640 | * signature though thus we set trailer. |
| 6641 | */ |
| 6642 | (void)sctp_hmac_m(SCTP_HMAC, |
| 6643 | (uint8_t *)inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)], |
| 6644 | SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr), |
| 6645 | (uint8_t *)signature, SCTP_SIGNATURE_SIZE); |
| 6646 | #if defined(__Userspace__) |
| 6647 | /* |
| 6648 | * Don't put AF_CONN addresses on the wire, in case this is critical |
| 6649 | * for the application. However, they are protected by the HMAC and |
| 6650 | * need to be reconstructed before checking the HMAC. |
| 6651 | * Clearing is only done in the mbuf chain, since the local stc is |
| 6652 | * not used anymore. |
| 6653 | */ |
| 6654 | if (stc.addr_type == SCTP_CONN_ADDRESS) { |
| 6655 | const void *p = NULL; |
| 6656 |
| 6657 | m_copyback(m_cookie, sizeof(struct sctp_paramhdr) + offsetof(struct sctp_state_cookie, address), |
| 6658 | (int)sizeof(void *), (caddr_t)&p); |
| 6659 | } |
| 6660 | if (stc.laddr_type == SCTP_CONN_ADDRESS) { |
| 6661 | const void *p = NULL; |
| 6662 |
| 6663 | m_copyback(m_cookie, sizeof(struct sctp_paramhdr) + offsetof(struct sctp_state_cookie, laddress), |
| 6664 | (int)sizeof(void *), (caddr_t)&p); |
| 6665 | } |
| 6666 | #endif |
| 6667 | /* |
| 6668 | * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return |
| 6669 | * here since the timer will drive a retranmission. |
| 6670 | */ |
| 6671 | if (padding_len > 0) { |
| 6672 | if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) { |
| 6673 | sctp_m_freem(m); |
| 6674 | return; |
| 6675 | } |
| 6676 | } |
| 6677 | if (stc.loopback_scope) { |
| 6678 | over_addr = (union sctp_sockstore *)dst; |
| 6679 | } else { |
| 6680 | over_addr = NULL; |
| 6681 | } |
| 6682 |
| 6683 | if ((error = sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0, |
| 6684 | 0, 0, |
| 6685 | inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag, |
| 6686 | port, over_addr, |
| 6687 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 6688 | mflowtype, mflowid, |
| 6689 | #endif |
| 6690 | SCTP_SO_NOT_LOCKED))) { |
| 6691 | SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error); |
| 6692 | if (error == ENOBUFS) { |
| 6693 | if (asoc != NULL) { |
| 6694 | asoc->ifp_had_enobuf = 1; |
| 6695 | } |
| 6696 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 6697 | } |
| 6698 | } else { |
| 6699 | if (asoc != NULL) { |
| 6700 | asoc->ifp_had_enobuf = 0; |
| 6701 | } |
| 6702 | } |
| 6703 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 6704 | } |
| 6705 |
| 6706 | static void |
| 6707 | sctp_prune_prsctp(struct sctp_tcb *stcb, |
| 6708 | struct sctp_association *asoc, |
| 6709 | struct sctp_sndrcvinfo *srcv, |
| 6710 | int dataout) |
| 6711 | { |
| 6712 | int freed_spc = 0; |
| 6713 | struct sctp_tmit_chunk *chk, *nchk; |
| 6714 |
| 6715 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 6716 | if ((asoc->prsctp_supported) && |
| 6717 | (asoc->sent_queue_cnt_removeable > 0)) { |
| 6718 | TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) { |
| 6719 | /* |
| 6720 | * Look for chunks marked with the PR_SCTP flag AND |
| 6721 | * the buffer space flag. If the one being sent is |
| 6722 | * equal or greater priority then purge the old one |
| 6723 | * and free some space. |
| 6724 | */ |
| 6725 | if (PR_SCTP_BUF_ENABLED(chk->flags)) { |
| 6726 | /* |
| 6727 | * This one is PR-SCTP AND buffer space |
| 6728 | * limited type |
| 6729 | */ |
| 6730 | if (chk->rec.data.timetodrop.tv_sec > (long)srcv->sinfo_timetolive) { |
| 6731 | /* |
| 6732 | * Lower numbers equates to higher |
| 6733 | * priority. So if the one we are |
| 6734 | * looking at has a larger priority, |
| 6735 | * we want to drop the data and NOT |
| 6736 | * retransmit it. |
| 6737 | */ |
| 6738 | if (chk->data) { |
| 6739 | /* |
| 6740 | * We release the book_size |
| 6741 | * if the mbuf is here |
| 6742 | */ |
| 6743 | int ret_spc; |
| 6744 | uint8_t sent; |
| 6745 |
| 6746 | if (chk->sent > SCTP_DATAGRAM_UNSENT) |
| 6747 | sent = 1; |
| 6748 | else |
| 6749 | sent = 0; |
| 6750 | ret_spc = sctp_release_pr_sctp_chunk(stcb, chk, |
| 6751 | sent, |
| 6752 | SCTP_SO_LOCKED); |
| 6753 | freed_spc += ret_spc; |
| 6754 | if (freed_spc >= dataout) { |
| 6755 | return; |
| 6756 | } |
| 6757 | } /* if chunk was present */ |
| 6758 | } /* if of sufficient priority */ |
| 6759 | } /* if chunk has enabled */ |
| 6760 | } /* tailqforeach */ |
| 6761 |
| 6762 | TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { |
| 6763 | /* Here we must move to the sent queue and mark */ |
| 6764 | if (PR_SCTP_BUF_ENABLED(chk->flags)) { |
| 6765 | if (chk->rec.data.timetodrop.tv_sec > (long)srcv->sinfo_timetolive) { |
| 6766 | if (chk->data) { |
| 6767 | /* |
| 6768 | * We release the book_size |
| 6769 | * if the mbuf is here |
| 6770 | */ |
| 6771 | int ret_spc; |
| 6772 |
| 6773 | ret_spc = sctp_release_pr_sctp_chunk(stcb, chk, |
| 6774 | 0, SCTP_SO_LOCKED); |
| 6775 |
| 6776 | freed_spc += ret_spc; |
| 6777 | if (freed_spc >= dataout) { |
| 6778 | return; |
| 6779 | } |
| 6780 | } /* end if chk->data */ |
| 6781 | } /* end if right class */ |
| 6782 | } /* end if chk pr-sctp */ |
| 6783 | } /* tailqforeachsafe (chk) */ |
| 6784 | } /* if enabled in asoc */ |
| 6785 | } |
| 6786 |
| 6787 | int |
| 6788 | sctp_get_frag_point(struct sctp_tcb *stcb, |
| 6789 | struct sctp_association *asoc) |
| 6790 | { |
| 6791 | int siz, ovh; |
| 6792 |
| 6793 | /* |
| 6794 | * For endpoints that have both v6 and v4 addresses we must reserve |
| 6795 | * room for the ipv6 header, for those that are only dealing with V4 |
| 6796 | * we use a larger frag point. |
| 6797 | */ |
| 6798 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { |
| 6799 | ovh = SCTP_MIN_OVERHEAD; |
| 6800 | } else { |
| 6801 | #if defined(__Userspace__) |
| 6802 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) { |
| 6803 | ovh = sizeof(struct sctphdr); |
| 6804 | } else { |
| 6805 | ovh = SCTP_MIN_V4_OVERHEAD; |
| 6806 | } |
| 6807 | #else |
| 6808 | ovh = SCTP_MIN_V4_OVERHEAD; |
| 6809 | #endif |
| 6810 | } |
| 6811 | ovh += SCTP_DATA_CHUNK_OVERHEAD(stcb); |
| 6812 | if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu) |
| 6813 | siz = asoc->smallest_mtu - ovh; |
| 6814 | else |
| 6815 | siz = (stcb->asoc.sctp_frag_point - ovh); |
| 6816 | /* |
| 6817 | * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) { |
| 6818 | */ |
| 6819 | /* A data chunk MUST fit in a cluster */ |
| 6820 | /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */ |
| 6821 | /* } */ |
| 6822 |
| 6823 | /* adjust for an AUTH chunk if DATA requires auth */ |
| 6824 | if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) |
| 6825 | siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); |
| 6826 |
| 6827 | if (siz % 4) { |
| 6828 | /* make it an even word boundary please */ |
| 6829 | siz -= (siz % 4); |
| 6830 | } |
| 6831 | return (siz); |
| 6832 | } |
| 6833 |
| 6834 | static void |
| 6835 | sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp) |
| 6836 | { |
| 6837 | /* |
| 6838 | * We assume that the user wants PR_SCTP_TTL if the user |
| 6839 | * provides a positive lifetime but does not specify any |
| 6840 | * PR_SCTP policy. |
| 6841 | */ |
| 6842 | if (PR_SCTP_ENABLED(sp->sinfo_flags)) { |
| 6843 | sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags); |
| 6844 | } else if (sp->timetolive > 0) { |
| 6845 | sp->sinfo_flags |= SCTP_PR_SCTP_TTL; |
| 6846 | sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags); |
| 6847 | } else { |
| 6848 | return; |
| 6849 | } |
| 6850 | switch (PR_SCTP_POLICY(sp->sinfo_flags)) { |
| 6851 | case CHUNK_FLAGS_PR_SCTP_BUF: |
| 6852 | /* |
| 6853 | * Time to live is a priority stored in tv_sec when |
| 6854 | * doing the buffer drop thing. |
| 6855 | */ |
| 6856 | sp->ts.tv_sec = sp->timetolive; |
| 6857 | sp->ts.tv_usec = 0; |
| 6858 | break; |
| 6859 | case CHUNK_FLAGS_PR_SCTP_TTL: |
| 6860 | { |
| 6861 | struct timeval tv; |
| 6862 | (void)SCTP_GETTIME_TIMEVAL(&sp->ts); |
| 6863 | tv.tv_sec = sp->timetolive / 1000; |
| 6864 | tv.tv_usec = (sp->timetolive * 1000) % 1000000; |
| 6865 | /* TODO sctp_constants.h needs alternative time macros when |
| 6866 | * _KERNEL is undefined. |
| 6867 | */ |
| 6868 | #if !(defined(__FreeBSD__) && !defined(__Userspace__)) |
| 6869 | timeradd(&sp->ts, &tv, &sp->ts); |
| 6870 | #else |
| 6871 | timevaladd(&sp->ts, &tv); |
| 6872 | #endif |
| 6873 | } |
| 6874 | break; |
| 6875 | case CHUNK_FLAGS_PR_SCTP_RTX: |
| 6876 | /* |
| 6877 | * Time to live is a the number or retransmissions |
| 6878 | * stored in tv_sec. |
| 6879 | */ |
| 6880 | sp->ts.tv_sec = sp->timetolive; |
| 6881 | sp->ts.tv_usec = 0; |
| 6882 | break; |
| 6883 | default: |
| 6884 | SCTPDBG(SCTP_DEBUG_USRREQ1, |
| 6885 | "Unknown PR_SCTP policy %u.\n", |
| 6886 | PR_SCTP_POLICY(sp->sinfo_flags)); |
| 6887 | break; |
| 6888 | } |
| 6889 | } |
| 6890 |
| 6891 | static int |
| 6892 | sctp_msg_append(struct sctp_tcb *stcb, |
| 6893 | struct sctp_nets *net, |
| 6894 | struct mbuf *m, |
| 6895 | struct sctp_sndrcvinfo *srcv, int hold_stcb_lock) |
| 6896 | { |
| 6897 | int error = 0; |
| 6898 | struct mbuf *at; |
| 6899 | struct sctp_stream_queue_pending *sp = NULL; |
| 6900 | struct sctp_stream_out *strm; |
| 6901 |
| 6902 | /* Given an mbuf chain, put it |
| 6903 | * into the association send queue and |
| 6904 | * place it on the wheel |
| 6905 | */ |
| 6906 | if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) { |
| 6907 | /* Invalid stream number */ |
| 6908 | SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 6909 | error = EINVAL; |
| 6910 | goto out_now; |
| 6911 | } |
| 6912 | if ((stcb->asoc.stream_locked) && |
| 6913 | (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) { |
| 6914 | SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 6915 | error = EINVAL; |
| 6916 | goto out_now; |
| 6917 | } |
| 6918 | strm = &stcb->asoc.strmout[srcv->sinfo_stream]; |
| 6919 | /* Now can we send this? */ |
| 6920 | if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) || |
| 6921 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) || |
| 6922 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) || |
| 6923 | (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) { |
| 6924 | /* got data while shutting down */ |
| 6925 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); |
| 6926 | error = ECONNRESET; |
| 6927 | goto out_now; |
| 6928 | } |
| 6929 | sctp_alloc_a_strmoq(stcb, sp); |
| 6930 | if (sp == NULL) { |
| 6931 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 6932 | error = ENOMEM; |
| 6933 | goto out_now; |
| 6934 | } |
| 6935 | sp->sinfo_flags = srcv->sinfo_flags; |
| 6936 | sp->timetolive = srcv->sinfo_timetolive; |
| 6937 | sp->ppid = srcv->sinfo_ppid; |
| 6938 | sp->context = srcv->sinfo_context; |
| 6939 | sp->fsn = 0; |
| 6940 | if (sp->sinfo_flags & SCTP_ADDR_OVER) { |
| 6941 | sp->net = net; |
| 6942 | atomic_add_int(&sp->net->ref_count, 1); |
| 6943 | } else { |
| 6944 | sp->net = NULL; |
| 6945 | } |
| 6946 | (void)SCTP_GETTIME_TIMEVAL(&sp->ts); |
| 6947 | sp->sid = srcv->sinfo_stream; |
| 6948 | sp->msg_is_complete = 1; |
| 6949 | sp->sender_all_done = 1; |
| 6950 | sp->some_taken = 0; |
| 6951 | sp->data = m; |
| 6952 | sp->tail_mbuf = NULL; |
| 6953 | sctp_set_prsctp_policy(sp); |
| 6954 | /* We could in theory (for sendall) sifa the length |
| 6955 | * in, but we would still have to hunt through the |
| 6956 | * chain since we need to setup the tail_mbuf |
| 6957 | */ |
| 6958 | sp->length = 0; |
| 6959 | for (at = m; at; at = SCTP_BUF_NEXT(at)) { |
| 6960 | if (SCTP_BUF_NEXT(at) == NULL) |
| 6961 | sp->tail_mbuf = at; |
| 6962 | sp->length += SCTP_BUF_LEN(at); |
| 6963 | } |
| 6964 | if (srcv->sinfo_keynumber_valid) { |
| 6965 | sp->auth_keyid = srcv->sinfo_keynumber; |
| 6966 | } else { |
| 6967 | sp->auth_keyid = stcb->asoc.authinfo.active_keyid; |
| 6968 | } |
| 6969 | if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) { |
| 6970 | sctp_auth_key_acquire(stcb, sp->auth_keyid); |
| 6971 | sp->holds_key_ref = 1; |
| 6972 | } |
| 6973 | if (hold_stcb_lock == 0) { |
| 6974 | SCTP_TCB_SEND_LOCK(stcb); |
| 6975 | } |
| 6976 | sctp_snd_sb_alloc(stcb, sp->length); |
| 6977 | atomic_add_int(&stcb->asoc.stream_queue_cnt, 1); |
| 6978 | TAILQ_INSERT_TAIL(&strm->outqueue, sp, next); |
| 6979 | stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp, 1); |
| 6980 | m = NULL; |
| 6981 | if (hold_stcb_lock == 0) { |
| 6982 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 6983 | } |
| 6984 | out_now: |
| 6985 | if (m) { |
| 6986 | sctp_m_freem(m); |
| 6987 | } |
| 6988 | return (error); |
| 6989 | } |
| 6990 |
| 6991 | static struct mbuf * |
| 6992 | sctp_copy_mbufchain(struct mbuf *clonechain, |
| 6993 | struct mbuf *outchain, |
| 6994 | struct mbuf **endofchain, |
| 6995 | int can_take_mbuf, |
| 6996 | int sizeofcpy, |
| 6997 | uint8_t copy_by_ref) |
| 6998 | { |
| 6999 | struct mbuf *m; |
| 7000 | struct mbuf *appendchain; |
| 7001 | caddr_t cp; |
| 7002 | int len; |
| 7003 |
| 7004 | if (endofchain == NULL) { |
| 7005 | /* error */ |
| 7006 | error_out: |
| 7007 | if (outchain) |
| 7008 | sctp_m_freem(outchain); |
| 7009 | return (NULL); |
| 7010 | } |
| 7011 | if (can_take_mbuf) { |
| 7012 | appendchain = clonechain; |
| 7013 | } else { |
| 7014 | if (!copy_by_ref && |
| 7015 | (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))) { |
| 7016 | /* Its not in a cluster */ |
| 7017 | if (*endofchain == NULL) { |
| 7018 | /* lets get a mbuf cluster */ |
| 7019 | if (outchain == NULL) { |
| 7020 | /* This is the general case */ |
| 7021 | new_mbuf: |
| 7022 | outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER); |
| 7023 | if (outchain == NULL) { |
| 7024 | goto error_out; |
| 7025 | } |
| 7026 | SCTP_BUF_LEN(outchain) = 0; |
| 7027 | *endofchain = outchain; |
| 7028 | /* get the prepend space */ |
| 7029 | SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV+4)); |
| 7030 | } else { |
| 7031 | /* We really should not get a NULL in endofchain */ |
| 7032 | /* find end */ |
| 7033 | m = outchain; |
| 7034 | while (m) { |
| 7035 | if (SCTP_BUF_NEXT(m) == NULL) { |
| 7036 | *endofchain = m; |
| 7037 | break; |
| 7038 | } |
| 7039 | m = SCTP_BUF_NEXT(m); |
| 7040 | } |
| 7041 | /* sanity */ |
| 7042 | if (*endofchain == NULL) { |
| 7043 | /* huh, TSNH XXX maybe we should panic */ |
| 7044 | sctp_m_freem(outchain); |
| 7045 | goto new_mbuf; |
| 7046 | } |
| 7047 | } |
| 7048 | /* get the new end of length */ |
| 7049 | len = (int)M_TRAILINGSPACE(*endofchain); |
| 7050 | } else { |
| 7051 | /* how much is left at the end? */ |
| 7052 | len = (int)M_TRAILINGSPACE(*endofchain); |
| 7053 | } |
| 7054 | /* Find the end of the data, for appending */ |
| 7055 | cp = (mtod((*endofchain), caddr_t) + SCTP_BUF_LEN((*endofchain))); |
| 7056 |
| 7057 | /* Now lets copy it out */ |
| 7058 | if (len >= sizeofcpy) { |
| 7059 | /* It all fits, copy it in */ |
| 7060 | m_copydata(clonechain, 0, sizeofcpy, cp); |
| 7061 | SCTP_BUF_LEN((*endofchain)) += sizeofcpy; |
| 7062 | } else { |
| 7063 | /* fill up the end of the chain */ |
| 7064 | if (len > 0) { |
| 7065 | m_copydata(clonechain, 0, len, cp); |
| 7066 | SCTP_BUF_LEN((*endofchain)) += len; |
| 7067 | /* now we need another one */ |
| 7068 | sizeofcpy -= len; |
| 7069 | } |
| 7070 | m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER); |
| 7071 | if (m == NULL) { |
| 7072 | /* We failed */ |
| 7073 | goto error_out; |
| 7074 | } |
| 7075 | SCTP_BUF_NEXT((*endofchain)) = m; |
| 7076 | *endofchain = m; |
| 7077 | cp = mtod((*endofchain), caddr_t); |
| 7078 | m_copydata(clonechain, len, sizeofcpy, cp); |
| 7079 | SCTP_BUF_LEN((*endofchain)) += sizeofcpy; |
| 7080 | } |
| 7081 | return (outchain); |
| 7082 | } else { |
| 7083 | /* copy the old fashion way */ |
| 7084 | appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_NOWAIT); |
| 7085 | #ifdef SCTP_MBUF_LOGGING |
| 7086 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { |
| 7087 | sctp_log_mbc(appendchain, SCTP_MBUF_ICOPY); |
| 7088 | } |
| 7089 | #endif |
| 7090 | } |
| 7091 | } |
| 7092 | if (appendchain == NULL) { |
| 7093 | /* error */ |
| 7094 | if (outchain) |
| 7095 | sctp_m_freem(outchain); |
| 7096 | return (NULL); |
| 7097 | } |
| 7098 | if (outchain) { |
| 7099 | /* tack on to the end */ |
| 7100 | if (*endofchain != NULL) { |
| 7101 | SCTP_BUF_NEXT(((*endofchain))) = appendchain; |
| 7102 | } else { |
| 7103 | m = outchain; |
| 7104 | while (m) { |
| 7105 | if (SCTP_BUF_NEXT(m) == NULL) { |
| 7106 | SCTP_BUF_NEXT(m) = appendchain; |
| 7107 | break; |
| 7108 | } |
| 7109 | m = SCTP_BUF_NEXT(m); |
| 7110 | } |
| 7111 | } |
| 7112 | /* |
| 7113 | * save off the end and update the end-chain |
| 7114 | * position |
| 7115 | */ |
| 7116 | m = appendchain; |
| 7117 | while (m) { |
| 7118 | if (SCTP_BUF_NEXT(m) == NULL) { |
| 7119 | *endofchain = m; |
| 7120 | break; |
| 7121 | } |
| 7122 | m = SCTP_BUF_NEXT(m); |
| 7123 | } |
| 7124 | return (outchain); |
| 7125 | } else { |
| 7126 | /* save off the end and update the end-chain position */ |
| 7127 | m = appendchain; |
| 7128 | while (m) { |
| 7129 | if (SCTP_BUF_NEXT(m) == NULL) { |
| 7130 | *endofchain = m; |
| 7131 | break; |
| 7132 | } |
| 7133 | m = SCTP_BUF_NEXT(m); |
| 7134 | } |
| 7135 | return (appendchain); |
| 7136 | } |
| 7137 | } |
| 7138 |
| 7139 | static int |
| 7140 | sctp_med_chunk_output(struct sctp_inpcb *inp, |
| 7141 | struct sctp_tcb *stcb, |
| 7142 | struct sctp_association *asoc, |
| 7143 | int *num_out, |
| 7144 | int *reason_code, |
| 7145 | int control_only, int from_where, |
| 7146 | struct timeval *now, int *now_filled, int frag_point, int so_locked); |
| 7147 |
| 7148 | static void |
| 7149 | sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr, |
| 7150 | uint32_t val SCTP_UNUSED) |
| 7151 | { |
| 7152 | struct sctp_copy_all *ca; |
| 7153 | struct mbuf *m; |
| 7154 | int ret = 0; |
| 7155 | int added_control = 0; |
| 7156 | int un_sent, do_chunk_output = 1; |
| 7157 | struct sctp_association *asoc; |
| 7158 | struct sctp_nets *net; |
| 7159 |
| 7160 | ca = (struct sctp_copy_all *)ptr; |
| 7161 | if (ca->m == NULL) { |
| 7162 | return; |
| 7163 | } |
| 7164 | if (ca->inp != inp) { |
| 7165 | /* TSNH */ |
| 7166 | return; |
| 7167 | } |
| 7168 | if (ca->sndlen > 0) { |
| 7169 | m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_NOWAIT); |
| 7170 | if (m == NULL) { |
| 7171 | /* can't copy so we are done */ |
| 7172 | ca->cnt_failed++; |
| 7173 | return; |
| 7174 | } |
| 7175 | #ifdef SCTP_MBUF_LOGGING |
| 7176 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { |
| 7177 | sctp_log_mbc(m, SCTP_MBUF_ICOPY); |
| 7178 | } |
| 7179 | #endif |
| 7180 | } else { |
| 7181 | m = NULL; |
| 7182 | } |
| 7183 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 7184 | if (stcb->asoc.alternate) { |
| 7185 | net = stcb->asoc.alternate; |
| 7186 | } else { |
| 7187 | net = stcb->asoc.primary_destination; |
| 7188 | } |
| 7189 | if (ca->sndrcv.sinfo_flags & SCTP_ABORT) { |
| 7190 | /* Abort this assoc with m as the user defined reason */ |
| 7191 | if (m != NULL) { |
| 7192 | SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_NOWAIT); |
| 7193 | } else { |
| 7194 | m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), |
| 7195 | 0, M_NOWAIT, 1, MT_DATA); |
| 7196 | SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr); |
| 7197 | } |
| 7198 | if (m != NULL) { |
| 7199 | struct sctp_paramhdr *ph; |
| 7200 |
| 7201 | ph = mtod(m, struct sctp_paramhdr *); |
| 7202 | ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); |
| 7203 | ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + ca->sndlen)); |
| 7204 | } |
| 7205 | /* We add one here to keep the assoc from |
| 7206 | * dis-appearing on us. |
| 7207 | */ |
| 7208 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 7209 | sctp_abort_an_association(inp, stcb, m, false, SCTP_SO_NOT_LOCKED); |
| 7210 | /* sctp_abort_an_association calls sctp_free_asoc() |
| 7211 | * free association will NOT free it since we |
| 7212 | * incremented the refcnt .. we do this to prevent |
| 7213 | * it being freed and things getting tricky since |
| 7214 | * we could end up (from free_asoc) calling inpcb_free |
| 7215 | * which would get a recursive lock call to the |
| 7216 | * iterator lock.. But as a consequence of that the |
| 7217 | * stcb will return to us un-locked.. since free_asoc |
| 7218 | * returns with either no TCB or the TCB unlocked, we |
| 7219 | * must relock.. to unlock in the iterator timer :-0 |
| 7220 | */ |
| 7221 | SCTP_TCB_LOCK(stcb); |
| 7222 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 7223 | goto no_chunk_output; |
| 7224 | } else { |
| 7225 | if (m) { |
| 7226 | ret = sctp_msg_append(stcb, net, m, |
| 7227 | &ca->sndrcv, 1); |
| 7228 | } |
| 7229 | asoc = &stcb->asoc; |
| 7230 | if (ca->sndrcv.sinfo_flags & SCTP_EOF) { |
| 7231 | /* shutdown this assoc */ |
| 7232 | if (TAILQ_EMPTY(&asoc->send_queue) && |
| 7233 | TAILQ_EMPTY(&asoc->sent_queue) && |
| 7234 | sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED) == 0) { |
| 7235 | if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) { |
| 7236 | goto abort_anyway; |
| 7237 | } |
| 7238 | /* there is nothing queued to send, so I'm done... */ |
| 7239 | if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) && |
| 7240 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) && |
| 7241 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { |
| 7242 | /* only send SHUTDOWN the first time through */ |
| 7243 | if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) { |
| 7244 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
| 7245 | } |
| 7246 | SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT); |
| 7247 | sctp_stop_timers_for_shutdown(stcb); |
| 7248 | sctp_send_shutdown(stcb, net); |
| 7249 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, |
| 7250 | net); |
| 7251 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, |
| 7252 | NULL); |
| 7253 | added_control = 1; |
| 7254 | do_chunk_output = 0; |
| 7255 | } |
| 7256 | } else { |
| 7257 | /* |
| 7258 | * we still got (or just got) data to send, so set |
| 7259 | * SHUTDOWN_PENDING |
| 7260 | */ |
| 7261 | /* |
| 7262 | * XXX sockets draft says that SCTP_EOF should be |
| 7263 | * sent with no data. currently, we will allow user |
| 7264 | * data to be sent first and move to |
| 7265 | * SHUTDOWN-PENDING |
| 7266 | */ |
| 7267 | if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) && |
| 7268 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) && |
| 7269 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { |
| 7270 | if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) { |
| 7271 | SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT); |
| 7272 | } |
| 7273 | SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING); |
| 7274 | if (TAILQ_EMPTY(&asoc->send_queue) && |
| 7275 | TAILQ_EMPTY(&asoc->sent_queue) && |
| 7276 | (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) { |
| 7277 | struct mbuf *op_err; |
| 7278 | char msg[SCTP_DIAG_INFO_LEN]; |
| 7279 |
| 7280 | abort_anyway: |
| 7281 | SCTP_SNPRINTF(msg, sizeof(msg), |
| 7282 | "%s:%d at %s", __FILE__, __LINE__, __func__); |
| 7283 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 7284 | msg); |
| 7285 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 7286 | sctp_abort_an_association(stcb->sctp_ep, stcb, |
| 7287 | op_err, false, SCTP_SO_NOT_LOCKED); |
| 7288 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 7289 | goto no_chunk_output; |
| 7290 | } |
| 7291 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, |
| 7292 | NULL); |
| 7293 | } |
| 7294 | } |
| 7295 | } |
| 7296 | } |
| 7297 | un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) + |
| 7298 | (stcb->asoc.stream_queue_cnt * SCTP_DATA_CHUNK_OVERHEAD(stcb))); |
| 7299 |
| 7300 | if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) && |
| 7301 | (stcb->asoc.total_flight > 0) && |
| 7302 | (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) { |
| 7303 | do_chunk_output = 0; |
| 7304 | } |
| 7305 | if (do_chunk_output) |
| 7306 | sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED); |
| 7307 | else if (added_control) { |
| 7308 | int num_out, reason, now_filled = 0; |
| 7309 | struct timeval now; |
| 7310 | int frag_point; |
| 7311 |
| 7312 | frag_point = sctp_get_frag_point(stcb, &stcb->asoc); |
| 7313 | (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out, |
| 7314 | &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED); |
| 7315 | } |
| 7316 | no_chunk_output: |
| 7317 | if (ret) { |
| 7318 | ca->cnt_failed++; |
| 7319 | } else { |
| 7320 | ca->cnt_sent++; |
| 7321 | } |
| 7322 | } |
| 7323 |
| 7324 | static void |
| 7325 | sctp_sendall_completes(void *ptr, uint32_t val SCTP_UNUSED) |
| 7326 | { |
| 7327 | struct sctp_copy_all *ca; |
| 7328 |
| 7329 | ca = (struct sctp_copy_all *)ptr; |
| 7330 | /* |
| 7331 | * Do a notify here? Kacheong suggests that the notify be done at |
| 7332 | * the send time.. so you would push up a notification if any send |
| 7333 | * failed. Don't know if this is feasible since the only failures we |
| 7334 | * have is "memory" related and if you cannot get an mbuf to send |
| 7335 | * the data you surely can't get an mbuf to send up to notify the |
| 7336 | * user you can't send the data :-> |
| 7337 | */ |
| 7338 |
| 7339 | /* now free everything */ |
| 7340 | if (ca->inp) { |
| 7341 | /* Lets clear the flag to allow others to run. */ |
| 7342 | SCTP_INP_WLOCK(ca->inp); |
| 7343 | ca->inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP; |
| 7344 | SCTP_INP_WUNLOCK(ca->inp); |
| 7345 | } |
| 7346 | sctp_m_freem(ca->m); |
| 7347 | SCTP_FREE(ca, SCTP_M_COPYAL); |
| 7348 | } |
| 7349 |
| 7350 | static struct mbuf * |
| 7351 | sctp_copy_out_all(struct uio *uio, ssize_t len) |
| 7352 | { |
| 7353 | struct mbuf *ret, *at; |
| 7354 | ssize_t left, willcpy, cancpy, error; |
| 7355 |
| 7356 | ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAITOK, 1, MT_DATA); |
| 7357 | if (ret == NULL) { |
| 7358 | /* TSNH */ |
| 7359 | return (NULL); |
| 7360 | } |
| 7361 | left = len; |
| 7362 | SCTP_BUF_LEN(ret) = 0; |
| 7363 | /* save space for the data chunk header */ |
| 7364 | cancpy = (int)M_TRAILINGSPACE(ret); |
| 7365 | willcpy = min(cancpy, left); |
| 7366 | at = ret; |
| 7367 | while (left > 0) { |
| 7368 | /* Align data to the end */ |
| 7369 | error = uiomove(mtod(at, caddr_t), (int)willcpy, uio); |
| 7370 | if (error) { |
| 7371 | err_out_now: |
| 7372 | sctp_m_freem(at); |
| 7373 | return (NULL); |
| 7374 | } |
| 7375 | SCTP_BUF_LEN(at) = (int)willcpy; |
| 7376 | SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0; |
| 7377 | left -= willcpy; |
| 7378 | if (left > 0) { |
| 7379 | SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg((unsigned int)left, 0, M_WAITOK, 1, MT_DATA); |
| 7380 | if (SCTP_BUF_NEXT(at) == NULL) { |
| 7381 | goto err_out_now; |
| 7382 | } |
| 7383 | at = SCTP_BUF_NEXT(at); |
| 7384 | SCTP_BUF_LEN(at) = 0; |
| 7385 | cancpy = (int)M_TRAILINGSPACE(at); |
| 7386 | willcpy = min(cancpy, left); |
| 7387 | } |
| 7388 | } |
| 7389 | return (ret); |
| 7390 | } |
| 7391 |
| 7392 | static int |
| 7393 | sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m, |
| 7394 | struct sctp_sndrcvinfo *srcv) |
| 7395 | { |
| 7396 | int ret; |
| 7397 | struct sctp_copy_all *ca; |
| 7398 |
| 7399 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 7400 | #if defined(APPLE_LEOPARD) |
| 7401 | if (uio->uio_resid > SCTP_BASE_SYSCTL(sctp_sendall_limit)) { |
| 7402 | #else |
| 7403 | if (uio_resid(uio) > SCTP_BASE_SYSCTL(sctp_sendall_limit)) { |
| 7404 | #endif |
| 7405 | #else |
| 7406 | if (uio->uio_resid > (ssize_t)SCTP_BASE_SYSCTL(sctp_sendall_limit)) { |
| 7407 | #endif |
| 7408 | /* You must not be larger than the limit! */ |
| 7409 | return (EMSGSIZE); |
| 7410 | } |
| 7411 | SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all), |
| 7412 | SCTP_M_COPYAL); |
| 7413 | if (ca == NULL) { |
| 7414 | sctp_m_freem(m); |
| 7415 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 7416 | return (ENOMEM); |
| 7417 | } |
| 7418 | memset(ca, 0, sizeof(struct sctp_copy_all)); |
| 7419 |
| 7420 | ca->inp = inp; |
| 7421 | if (srcv) { |
| 7422 | memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo)); |
| 7423 | } |
| 7424 |
| 7425 | /* Serialize. */ |
| 7426 | SCTP_INP_WLOCK(inp); |
| 7427 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_SND_ITERATOR_UP) != 0) { |
| 7428 | SCTP_INP_WUNLOCK(inp); |
| 7429 | sctp_m_freem(m); |
| 7430 | SCTP_FREE(ca, SCTP_M_COPYAL); |
| 7431 | return (EBUSY); |
| 7432 | } |
| 7433 | inp->sctp_flags |= SCTP_PCB_FLAGS_SND_ITERATOR_UP; |
| 7434 | SCTP_INP_WUNLOCK(inp); |
| 7435 |
| 7436 | /* |
| 7437 | * take off the sendall flag, it would be bad if we failed to do |
| 7438 | * this :-0 |
| 7439 | */ |
| 7440 | ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL; |
| 7441 | /* get length and mbuf chain */ |
| 7442 | if (uio) { |
| 7443 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 7444 | #if defined(APPLE_LEOPARD) |
| 7445 | ca->sndlen = uio->uio_resid; |
| 7446 | #else |
| 7447 | ca->sndlen = uio_resid(uio); |
| 7448 | #endif |
| 7449 | #else |
| 7450 | ca->sndlen = uio->uio_resid; |
| 7451 | #endif |
| 7452 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 7453 | SCTP_SOCKET_UNLOCK(SCTP_INP_SO(inp), 0); |
| 7454 | #endif |
| 7455 | ca->m = sctp_copy_out_all(uio, ca->sndlen); |
| 7456 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 7457 | SCTP_SOCKET_LOCK(SCTP_INP_SO(inp), 0); |
| 7458 | #endif |
| 7459 | if (ca->m == NULL) { |
| 7460 | SCTP_FREE(ca, SCTP_M_COPYAL); |
| 7461 | sctp_m_freem(m); |
| 7462 | SCTP_INP_WLOCK(inp); |
| 7463 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP; |
| 7464 | SCTP_INP_WUNLOCK(inp); |
| 7465 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 7466 | return (ENOMEM); |
| 7467 | } |
| 7468 | } else { |
| 7469 | /* Gather the length of the send */ |
| 7470 | struct mbuf *mat; |
| 7471 |
| 7472 | ca->sndlen = 0; |
| 7473 | for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) { |
| 7474 | ca->sndlen += SCTP_BUF_LEN(mat); |
| 7475 | } |
| 7476 | } |
| 7477 | ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL, |
| 7478 | SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES, |
| 7479 | SCTP_ASOC_ANY_STATE, |
| 7480 | (void *)ca, 0, |
| 7481 | sctp_sendall_completes, inp, 1); |
| 7482 | if (ret) { |
| 7483 | SCTP_INP_WLOCK(inp); |
| 7484 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP; |
| 7485 | SCTP_INP_WUNLOCK(inp); |
| 7486 | SCTP_FREE(ca, SCTP_M_COPYAL); |
| 7487 | SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT); |
| 7488 | return (EFAULT); |
| 7489 | } |
| 7490 | return (0); |
| 7491 | } |
| 7492 |
| 7493 | void |
| 7494 | sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc) |
| 7495 | { |
| 7496 | struct sctp_tmit_chunk *chk, *nchk; |
| 7497 |
| 7498 | TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { |
| 7499 | if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) { |
| 7500 | TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); |
| 7501 | asoc->ctrl_queue_cnt--; |
| 7502 | if (chk->data) { |
| 7503 | sctp_m_freem(chk->data); |
| 7504 | chk->data = NULL; |
| 7505 | } |
| 7506 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 7507 | } |
| 7508 | } |
| 7509 | } |
| 7510 |
| 7511 | void |
| 7512 | sctp_toss_old_asconf(struct sctp_tcb *stcb) |
| 7513 | { |
| 7514 | struct sctp_association *asoc; |
| 7515 | struct sctp_tmit_chunk *chk, *nchk; |
| 7516 | struct sctp_asconf_chunk *acp; |
| 7517 |
| 7518 | asoc = &stcb->asoc; |
| 7519 | TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) { |
| 7520 | /* find SCTP_ASCONF chunk in queue */ |
| 7521 | if (chk->rec.chunk_id.id == SCTP_ASCONF) { |
| 7522 | if (chk->data) { |
| 7523 | acp = mtod(chk->data, struct sctp_asconf_chunk *); |
| 7524 | if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) { |
| 7525 | /* Not Acked yet */ |
| 7526 | break; |
| 7527 | } |
| 7528 | } |
| 7529 | TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next); |
| 7530 | asoc->ctrl_queue_cnt--; |
| 7531 | if (chk->data) { |
| 7532 | sctp_m_freem(chk->data); |
| 7533 | chk->data = NULL; |
| 7534 | } |
| 7535 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 7536 | } |
| 7537 | } |
| 7538 | } |
| 7539 |
| 7540 | static void |
| 7541 | sctp_clean_up_datalist(struct sctp_tcb *stcb, |
| 7542 | struct sctp_association *asoc, |
| 7543 | struct sctp_tmit_chunk **data_list, |
| 7544 | int bundle_at, |
| 7545 | struct sctp_nets *net) |
| 7546 | { |
| 7547 | int i; |
| 7548 | struct sctp_tmit_chunk *tp1; |
| 7549 |
| 7550 | for (i = 0; i < bundle_at; i++) { |
| 7551 | /* off of the send queue */ |
| 7552 | TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next); |
| 7553 | asoc->send_queue_cnt--; |
| 7554 | if (i > 0) { |
| 7555 | /* |
| 7556 | * Any chunk NOT 0 you zap the time chunk 0 gets |
| 7557 | * zapped or set based on if a RTO measurment is |
| 7558 | * needed. |
| 7559 | */ |
| 7560 | data_list[i]->do_rtt = 0; |
| 7561 | } |
| 7562 | /* record time */ |
| 7563 | data_list[i]->sent_rcv_time = net->last_sent_time; |
| 7564 | data_list[i]->rec.data.cwnd_at_send = net->cwnd; |
| 7565 | data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.tsn; |
| 7566 | if (data_list[i]->whoTo == NULL) { |
| 7567 | data_list[i]->whoTo = net; |
| 7568 | atomic_add_int(&net->ref_count, 1); |
| 7569 | } |
| 7570 | /* on to the sent queue */ |
| 7571 | tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead); |
| 7572 | if ((tp1) && SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) { |
| 7573 | struct sctp_tmit_chunk *tpp; |
| 7574 |
| 7575 | /* need to move back */ |
| 7576 | back_up_more: |
| 7577 | tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next); |
| 7578 | if (tpp == NULL) { |
| 7579 | TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next); |
| 7580 | goto all_done; |
| 7581 | } |
| 7582 | tp1 = tpp; |
| 7583 | if (SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) { |
| 7584 | goto back_up_more; |
| 7585 | } |
| 7586 | TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next); |
| 7587 | } else { |
| 7588 | TAILQ_INSERT_TAIL(&asoc->sent_queue, |
| 7589 | data_list[i], |
| 7590 | sctp_next); |
| 7591 | } |
| 7592 | all_done: |
| 7593 | /* This does not lower until the cum-ack passes it */ |
| 7594 | asoc->sent_queue_cnt++; |
| 7595 | if ((asoc->peers_rwnd <= 0) && |
| 7596 | (asoc->total_flight == 0) && |
| 7597 | (bundle_at == 1)) { |
| 7598 | /* Mark the chunk as being a window probe */ |
| 7599 | SCTP_STAT_INCR(sctps_windowprobed); |
| 7600 | } |
| 7601 | #ifdef SCTP_AUDITING_ENABLED |
| 7602 | sctp_audit_log(0xC2, 3); |
| 7603 | #endif |
| 7604 | data_list[i]->sent = SCTP_DATAGRAM_SENT; |
| 7605 | data_list[i]->snd_count = 1; |
| 7606 | data_list[i]->rec.data.chunk_was_revoked = 0; |
| 7607 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { |
| 7608 | sctp_misc_ints(SCTP_FLIGHT_LOG_UP, |
| 7609 | data_list[i]->whoTo->flight_size, |
| 7610 | data_list[i]->book_size, |
| 7611 | (uint32_t)(uintptr_t)data_list[i]->whoTo, |
| 7612 | data_list[i]->rec.data.tsn); |
| 7613 | } |
| 7614 | sctp_flight_size_increase(data_list[i]); |
| 7615 | sctp_total_flight_increase(stcb, data_list[i]); |
| 7616 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) { |
| 7617 | sctp_log_rwnd(SCTP_DECREASE_PEER_RWND, |
| 7618 | asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)); |
| 7619 | } |
| 7620 | asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd, |
| 7621 | (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))); |
| 7622 | if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) { |
| 7623 | /* SWS sender side engages */ |
| 7624 | asoc->peers_rwnd = 0; |
| 7625 | } |
| 7626 | } |
| 7627 | if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) { |
| 7628 | (*asoc->cc_functions.sctp_cwnd_update_packet_transmitted)(stcb, net); |
| 7629 | } |
| 7630 | } |
| 7631 |
| 7632 | static void |
| 7633 | sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked) |
| 7634 | { |
| 7635 | struct sctp_tmit_chunk *chk, *nchk; |
| 7636 |
| 7637 | TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { |
| 7638 | if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) || |
| 7639 | (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */ |
| 7640 | (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) || |
| 7641 | (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) || |
| 7642 | (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) || |
| 7643 | (chk->rec.chunk_id.id == SCTP_SHUTDOWN) || |
| 7644 | (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) || |
| 7645 | (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) || |
| 7646 | (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) || |
| 7647 | (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) || |
| 7648 | (chk->rec.chunk_id.id == SCTP_ECN_CWR) || |
| 7649 | (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) { |
| 7650 | /* Stray chunks must be cleaned up */ |
| 7651 | clean_up_anyway: |
| 7652 | TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); |
| 7653 | asoc->ctrl_queue_cnt--; |
| 7654 | if (chk->data) { |
| 7655 | sctp_m_freem(chk->data); |
| 7656 | chk->data = NULL; |
| 7657 | } |
| 7658 | if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) { |
| 7659 | asoc->fwd_tsn_cnt--; |
| 7660 | } |
| 7661 | sctp_free_a_chunk(stcb, chk, so_locked); |
| 7662 | } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) { |
| 7663 | /* special handling, we must look into the param */ |
| 7664 | if (chk != asoc->str_reset) { |
| 7665 | goto clean_up_anyway; |
| 7666 | } |
| 7667 | } |
| 7668 | } |
| 7669 | } |
| 7670 |
| 7671 | static uint32_t |
| 7672 | sctp_can_we_split_this(struct sctp_tcb *stcb, uint32_t length, |
| 7673 | uint32_t space_left, uint32_t frag_point, int eeor_on) |
| 7674 | { |
| 7675 | /* Make a decision on if I should split a |
| 7676 | * msg into multiple parts. This is only asked of |
| 7677 | * incomplete messages. |
| 7678 | */ |
| 7679 | if (eeor_on) { |
| 7680 | /* If we are doing EEOR we need to always send |
| 7681 | * it if its the entire thing, since it might |
| 7682 | * be all the guy is putting in the hopper. |
| 7683 | */ |
| 7684 | if (space_left >= length) { |
| 7685 | /*- |
| 7686 | * If we have data outstanding, |
| 7687 | * we get another chance when the sack |
| 7688 | * arrives to transmit - wait for more data |
| 7689 | */ |
| 7690 | if (stcb->asoc.total_flight == 0) { |
| 7691 | /* If nothing is in flight, we zero |
| 7692 | * the packet counter. |
| 7693 | */ |
| 7694 | return (length); |
| 7695 | } |
| 7696 | return (0); |
| 7697 |
| 7698 | } else { |
| 7699 | /* You can fill the rest */ |
| 7700 | return (space_left); |
| 7701 | } |
| 7702 | } |
| 7703 | /*- |
| 7704 | * For those strange folk that make the send buffer |
| 7705 | * smaller than our fragmentation point, we can't |
| 7706 | * get a full msg in so we have to allow splitting. |
| 7707 | */ |
| 7708 | if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) { |
| 7709 | return (length); |
| 7710 | } |
| 7711 | if ((length <= space_left) || |
| 7712 | ((length - space_left) < SCTP_BASE_SYSCTL(sctp_min_residual))) { |
| 7713 | /* Sub-optimial residual don't split in non-eeor mode. */ |
| 7714 | return (0); |
| 7715 | } |
| 7716 | /* If we reach here length is larger |
| 7717 | * than the space_left. Do we wish to split |
| 7718 | * it for the sake of packet putting together? |
| 7719 | */ |
| 7720 | if (space_left >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) { |
| 7721 | /* Its ok to split it */ |
| 7722 | return (min(space_left, frag_point)); |
| 7723 | } |
| 7724 | /* Nope, can't split */ |
| 7725 | return (0); |
| 7726 | } |
| 7727 |
| 7728 | static uint32_t |
| 7729 | sctp_move_to_outqueue(struct sctp_tcb *stcb, |
| 7730 | struct sctp_stream_out *strq, |
| 7731 | uint32_t space_left, |
| 7732 | uint32_t frag_point, |
| 7733 | int *giveup, |
| 7734 | int eeor_mode, |
| 7735 | int *bail, |
| 7736 | int so_locked) |
| 7737 | { |
| 7738 | /* Move from the stream to the send_queue keeping track of the total */ |
| 7739 | struct sctp_association *asoc; |
| 7740 | struct sctp_stream_queue_pending *sp; |
| 7741 | struct sctp_tmit_chunk *chk; |
| 7742 | struct sctp_data_chunk *dchkh=NULL; |
| 7743 | struct sctp_idata_chunk *ndchkh=NULL; |
| 7744 | uint32_t to_move, length; |
| 7745 | int leading; |
| 7746 | uint8_t rcv_flags = 0; |
| 7747 | uint8_t some_taken; |
| 7748 | uint8_t send_lock_up = 0; |
| 7749 |
| 7750 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 7751 | asoc = &stcb->asoc; |
| 7752 | one_more_time: |
| 7753 | /*sa_ignore FREED_MEMORY*/ |
| 7754 | sp = TAILQ_FIRST(&strq->outqueue); |
| 7755 | if (sp == NULL) { |
| 7756 | if (send_lock_up == 0) { |
| 7757 | SCTP_TCB_SEND_LOCK(stcb); |
| 7758 | send_lock_up = 1; |
| 7759 | } |
| 7760 | sp = TAILQ_FIRST(&strq->outqueue); |
| 7761 | if (sp) { |
| 7762 | goto one_more_time; |
| 7763 | } |
| 7764 | if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_EXPLICIT_EOR) == 0) && |
| 7765 | (stcb->asoc.idata_supported == 0) && |
| 7766 | (strq->last_msg_incomplete)) { |
| 7767 | SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n", |
| 7768 | strq->sid, |
| 7769 | strq->last_msg_incomplete); |
| 7770 | strq->last_msg_incomplete = 0; |
| 7771 | } |
| 7772 | to_move = 0; |
| 7773 | if (send_lock_up) { |
| 7774 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 7775 | send_lock_up = 0; |
| 7776 | } |
| 7777 | goto out_of; |
| 7778 | } |
| 7779 | if ((sp->msg_is_complete) && (sp->length == 0)) { |
| 7780 | if (sp->sender_all_done) { |
| 7781 | /* We are doing deferred cleanup. Last |
| 7782 | * time through when we took all the data |
| 7783 | * the sender_all_done was not set. |
| 7784 | */ |
| 7785 | if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) { |
| 7786 | SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); |
| 7787 | SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n", |
| 7788 | sp->sender_all_done, |
| 7789 | sp->length, |
| 7790 | sp->msg_is_complete, |
| 7791 | sp->put_last_out, |
| 7792 | send_lock_up); |
| 7793 | } |
| 7794 | if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) { |
| 7795 | SCTP_TCB_SEND_LOCK(stcb); |
| 7796 | send_lock_up = 1; |
| 7797 | } |
| 7798 | atomic_subtract_int(&asoc->stream_queue_cnt, 1); |
| 7799 | TAILQ_REMOVE(&strq->outqueue, sp, next); |
| 7800 | stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up); |
| 7801 | if ((strq->state == SCTP_STREAM_RESET_PENDING) && |
| 7802 | (strq->chunks_on_queues == 0) && |
| 7803 | TAILQ_EMPTY(&strq->outqueue)) { |
| 7804 | stcb->asoc.trigger_reset = 1; |
| 7805 | } |
| 7806 | if (sp->net) { |
| 7807 | sctp_free_remote_addr(sp->net); |
| 7808 | sp->net = NULL; |
| 7809 | } |
| 7810 | if (sp->data) { |
| 7811 | sctp_m_freem(sp->data); |
| 7812 | sp->data = NULL; |
| 7813 | } |
| 7814 | sctp_free_a_strmoq(stcb, sp, so_locked); |
| 7815 | /* we can't be locked to it */ |
| 7816 | if (send_lock_up) { |
| 7817 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 7818 | send_lock_up = 0; |
| 7819 | } |
| 7820 | /* back to get the next msg */ |
| 7821 | goto one_more_time; |
| 7822 | } else { |
| 7823 | /* sender just finished this but |
| 7824 | * still holds a reference |
| 7825 | */ |
| 7826 | *giveup = 1; |
| 7827 | to_move = 0; |
| 7828 | goto out_of; |
| 7829 | } |
| 7830 | } else { |
| 7831 | /* is there some to get */ |
| 7832 | if (sp->length == 0) { |
| 7833 | /* no */ |
| 7834 | *giveup = 1; |
| 7835 | to_move = 0; |
| 7836 | goto out_of; |
| 7837 | } else if (sp->discard_rest) { |
| 7838 | if (send_lock_up == 0) { |
| 7839 | SCTP_TCB_SEND_LOCK(stcb); |
| 7840 | send_lock_up = 1; |
| 7841 | } |
| 7842 | /* Whack down the size */ |
| 7843 | atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length); |
| 7844 | if ((stcb->sctp_socket != NULL) && |
| 7845 | ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || |
| 7846 | (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { |
| 7847 | atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length); |
| 7848 | } |
| 7849 | if (sp->data) { |
| 7850 | sctp_m_freem(sp->data); |
| 7851 | sp->data = NULL; |
| 7852 | sp->tail_mbuf = NULL; |
| 7853 | } |
| 7854 | sp->length = 0; |
| 7855 | sp->some_taken = 1; |
| 7856 | *giveup = 1; |
| 7857 | to_move = 0; |
| 7858 | goto out_of; |
| 7859 | } |
| 7860 | } |
| 7861 | some_taken = sp->some_taken; |
| 7862 | re_look: |
| 7863 | length = sp->length; |
| 7864 | if (sp->msg_is_complete) { |
| 7865 | /* The message is complete */ |
| 7866 | to_move = min(length, frag_point); |
| 7867 | if (to_move == length) { |
| 7868 | /* All of it fits in the MTU */ |
| 7869 | if (sp->some_taken) { |
| 7870 | rcv_flags |= SCTP_DATA_LAST_FRAG; |
| 7871 | } else { |
| 7872 | rcv_flags |= SCTP_DATA_NOT_FRAG; |
| 7873 | } |
| 7874 | sp->put_last_out = 1; |
| 7875 | if (sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) { |
| 7876 | rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY; |
| 7877 | } |
| 7878 | } else { |
| 7879 | /* Not all of it fits, we fragment */ |
| 7880 | if (sp->some_taken == 0) { |
| 7881 | rcv_flags |= SCTP_DATA_FIRST_FRAG; |
| 7882 | } |
| 7883 | sp->some_taken = 1; |
| 7884 | } |
| 7885 | } else { |
| 7886 | to_move = sctp_can_we_split_this(stcb, length, space_left, frag_point, eeor_mode); |
| 7887 | if (to_move) { |
| 7888 | /*- |
| 7889 | * We use a snapshot of length in case it |
| 7890 | * is expanding during the compare. |
| 7891 | */ |
| 7892 | uint32_t llen; |
| 7893 |
| 7894 | llen = length; |
| 7895 | if (to_move >= llen) { |
| 7896 | to_move = llen; |
| 7897 | if (send_lock_up == 0) { |
| 7898 | /*- |
| 7899 | * We are taking all of an incomplete msg |
| 7900 | * thus we need a send lock. |
| 7901 | */ |
| 7902 | SCTP_TCB_SEND_LOCK(stcb); |
| 7903 | send_lock_up = 1; |
| 7904 | if (sp->msg_is_complete) { |
| 7905 | /* the sender finished the msg */ |
| 7906 | goto re_look; |
| 7907 | } |
| 7908 | } |
| 7909 | } |
| 7910 | if (sp->some_taken == 0) { |
| 7911 | rcv_flags |= SCTP_DATA_FIRST_FRAG; |
| 7912 | sp->some_taken = 1; |
| 7913 | } |
| 7914 | } else { |
| 7915 | /* Nothing to take. */ |
| 7916 | *giveup = 1; |
| 7917 | to_move = 0; |
| 7918 | goto out_of; |
| 7919 | } |
| 7920 | } |
| 7921 |
| 7922 | /* If we reach here, we can copy out a chunk */ |
| 7923 | sctp_alloc_a_chunk(stcb, chk); |
| 7924 | if (chk == NULL) { |
| 7925 | /* No chunk memory */ |
| 7926 | *giveup = 1; |
| 7927 | to_move = 0; |
| 7928 | goto out_of; |
| 7929 | } |
| 7930 | /* Setup for unordered if needed by looking |
| 7931 | * at the user sent info flags. |
| 7932 | */ |
| 7933 | if (sp->sinfo_flags & SCTP_UNORDERED) { |
| 7934 | rcv_flags |= SCTP_DATA_UNORDERED; |
| 7935 | } |
| 7936 | if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && |
| 7937 | (sp->sinfo_flags & SCTP_EOF) == SCTP_EOF) { |
| 7938 | rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY; |
| 7939 | } |
| 7940 | /* clear out the chunk before setting up */ |
| 7941 | memset(chk, 0, sizeof(*chk)); |
| 7942 | chk->rec.data.rcv_flags = rcv_flags; |
| 7943 |
| 7944 | if (to_move >= length) { |
| 7945 | /* we think we can steal the whole thing */ |
| 7946 | if ((sp->sender_all_done == 0) && (send_lock_up == 0)) { |
| 7947 | SCTP_TCB_SEND_LOCK(stcb); |
| 7948 | send_lock_up = 1; |
| 7949 | } |
| 7950 | if (to_move < sp->length) { |
| 7951 | /* bail, it changed */ |
| 7952 | goto dont_do_it; |
| 7953 | } |
| 7954 | chk->data = sp->data; |
| 7955 | chk->last_mbuf = sp->tail_mbuf; |
| 7956 | /* register the stealing */ |
| 7957 | sp->data = sp->tail_mbuf = NULL; |
| 7958 | } else { |
| 7959 | struct mbuf *m; |
| 7960 | dont_do_it: |
| 7961 | chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_NOWAIT); |
| 7962 | chk->last_mbuf = NULL; |
| 7963 | if (chk->data == NULL) { |
| 7964 | sp->some_taken = some_taken; |
| 7965 | sctp_free_a_chunk(stcb, chk, so_locked); |
| 7966 | *bail = 1; |
| 7967 | to_move = 0; |
| 7968 | goto out_of; |
| 7969 | } |
| 7970 | #ifdef SCTP_MBUF_LOGGING |
| 7971 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { |
| 7972 | sctp_log_mbc(chk->data, SCTP_MBUF_ICOPY); |
| 7973 | } |
| 7974 | #endif |
| 7975 | /* Pull off the data */ |
| 7976 | m_adj(sp->data, to_move); |
| 7977 | /* Now lets work our way down and compact it */ |
| 7978 | m = sp->data; |
| 7979 | while (m && (SCTP_BUF_LEN(m) == 0)) { |
| 7980 | sp->data = SCTP_BUF_NEXT(m); |
| 7981 | SCTP_BUF_NEXT(m) = NULL; |
| 7982 | if (sp->tail_mbuf == m) { |
| 7983 | /*- |
| 7984 | * Freeing tail? TSNH since |
| 7985 | * we supposedly were taking less |
| 7986 | * than the sp->length. |
| 7987 | */ |
| 7988 | #ifdef INVARIANTS |
| 7989 | panic("Huh, freing tail? - TSNH"); |
| 7990 | #else |
| 7991 | SCTP_PRINTF("Huh, freeing tail? - TSNH\n"); |
| 7992 | sp->tail_mbuf = sp->data = NULL; |
| 7993 | sp->length = 0; |
| 7994 | #endif |
| 7995 | } |
| 7996 | sctp_m_free(m); |
| 7997 | m = sp->data; |
| 7998 | } |
| 7999 | } |
| 8000 | if (SCTP_BUF_IS_EXTENDED(chk->data)) { |
| 8001 | chk->copy_by_ref = 1; |
| 8002 | } else { |
| 8003 | chk->copy_by_ref = 0; |
| 8004 | } |
| 8005 | /* get last_mbuf and counts of mb usage |
| 8006 | * This is ugly but hopefully its only one mbuf. |
| 8007 | */ |
| 8008 | if (chk->last_mbuf == NULL) { |
| 8009 | chk->last_mbuf = chk->data; |
| 8010 | while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) { |
| 8011 | chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf); |
| 8012 | } |
| 8013 | } |
| 8014 |
| 8015 | if (to_move > length) { |
| 8016 | /*- This should not happen either |
| 8017 | * since we always lower to_move to the size |
| 8018 | * of sp->length if its larger. |
| 8019 | */ |
| 8020 | #ifdef INVARIANTS |
| 8021 | panic("Huh, how can to_move be larger?"); |
| 8022 | #else |
| 8023 | SCTP_PRINTF("Huh, how can to_move be larger?\n"); |
| 8024 | sp->length = 0; |
| 8025 | #endif |
| 8026 | } else { |
| 8027 | atomic_subtract_int(&sp->length, to_move); |
| 8028 | } |
| 8029 | leading = SCTP_DATA_CHUNK_OVERHEAD(stcb); |
| 8030 | if (M_LEADINGSPACE(chk->data) < leading) { |
| 8031 | /* Not enough room for a chunk header, get some */ |
| 8032 | struct mbuf *m; |
| 8033 |
| 8034 | m = sctp_get_mbuf_for_msg(1, 0, M_NOWAIT, 1, MT_DATA); |
| 8035 | if (m == NULL) { |
| 8036 | /* |
| 8037 | * we're in trouble here. _PREPEND below will free |
| 8038 | * all the data if there is no leading space, so we |
| 8039 | * must put the data back and restore. |
| 8040 | */ |
| 8041 | if (send_lock_up == 0) { |
| 8042 | SCTP_TCB_SEND_LOCK(stcb); |
| 8043 | send_lock_up = 1; |
| 8044 | } |
| 8045 | if (sp->data == NULL) { |
| 8046 | /* unsteal the data */ |
| 8047 | sp->data = chk->data; |
| 8048 | sp->tail_mbuf = chk->last_mbuf; |
| 8049 | } else { |
| 8050 | struct mbuf *m_tmp; |
| 8051 | /* reassemble the data */ |
| 8052 | m_tmp = sp->data; |
| 8053 | sp->data = chk->data; |
| 8054 | SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp; |
| 8055 | } |
| 8056 | sp->some_taken = some_taken; |
| 8057 | atomic_add_int(&sp->length, to_move); |
| 8058 | chk->data = NULL; |
| 8059 | *bail = 1; |
| 8060 | sctp_free_a_chunk(stcb, chk, so_locked); |
| 8061 | to_move = 0; |
| 8062 | goto out_of; |
| 8063 | } else { |
| 8064 | SCTP_BUF_LEN(m) = 0; |
| 8065 | SCTP_BUF_NEXT(m) = chk->data; |
| 8066 | chk->data = m; |
| 8067 | M_ALIGN(chk->data, 4); |
| 8068 | } |
| 8069 | } |
| 8070 | SCTP_BUF_PREPEND(chk->data, SCTP_DATA_CHUNK_OVERHEAD(stcb), M_NOWAIT); |
| 8071 | if (chk->data == NULL) { |
| 8072 | /* HELP, TSNH since we assured it would not above? */ |
| 8073 | #ifdef INVARIANTS |
| 8074 | panic("prepend failes HELP?"); |
| 8075 | #else |
| 8076 | SCTP_PRINTF("prepend fails HELP?\n"); |
| 8077 | sctp_free_a_chunk(stcb, chk, so_locked); |
| 8078 | #endif |
| 8079 | *bail = 1; |
| 8080 | to_move = 0; |
| 8081 | goto out_of; |
| 8082 | } |
| 8083 | sctp_snd_sb_alloc(stcb, SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 8084 | chk->book_size = chk->send_size = (uint16_t)(to_move + SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 8085 | chk->book_size_scale = 0; |
| 8086 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 8087 |
| 8088 | chk->flags = 0; |
| 8089 | chk->asoc = &stcb->asoc; |
| 8090 | chk->pad_inplace = 0; |
| 8091 | chk->no_fr_allowed = 0; |
| 8092 | if (stcb->asoc.idata_supported == 0) { |
| 8093 | if (rcv_flags & SCTP_DATA_UNORDERED) { |
| 8094 | /* Just use 0. The receiver ignores the values. */ |
| 8095 | chk->rec.data.mid = 0; |
| 8096 | } else { |
| 8097 | chk->rec.data.mid = strq->next_mid_ordered; |
| 8098 | if (rcv_flags & SCTP_DATA_LAST_FRAG) { |
| 8099 | strq->next_mid_ordered++; |
| 8100 | } |
| 8101 | } |
| 8102 | } else { |
| 8103 | if (rcv_flags & SCTP_DATA_UNORDERED) { |
| 8104 | chk->rec.data.mid = strq->next_mid_unordered; |
| 8105 | if (rcv_flags & SCTP_DATA_LAST_FRAG) { |
| 8106 | strq->next_mid_unordered++; |
| 8107 | } |
| 8108 | } else { |
| 8109 | chk->rec.data.mid = strq->next_mid_ordered; |
| 8110 | if (rcv_flags & SCTP_DATA_LAST_FRAG) { |
| 8111 | strq->next_mid_ordered++; |
| 8112 | } |
| 8113 | } |
| 8114 | } |
| 8115 | chk->rec.data.sid = sp->sid; |
| 8116 | chk->rec.data.ppid = sp->ppid; |
| 8117 | chk->rec.data.context = sp->context; |
| 8118 | chk->rec.data.doing_fast_retransmit = 0; |
| 8119 |
| 8120 | chk->rec.data.timetodrop = sp->ts; |
| 8121 | chk->flags = sp->act_flags; |
| 8122 |
| 8123 | if (sp->net) { |
| 8124 | chk->whoTo = sp->net; |
| 8125 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 8126 | } else |
| 8127 | chk->whoTo = NULL; |
| 8128 |
| 8129 | if (sp->holds_key_ref) { |
| 8130 | chk->auth_keyid = sp->auth_keyid; |
| 8131 | sctp_auth_key_acquire(stcb, chk->auth_keyid); |
| 8132 | chk->holds_key_ref = 1; |
| 8133 | } |
| 8134 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 8135 | chk->rec.data.tsn = atomic_fetchadd_int(&asoc->sending_seq, 1); |
| 8136 | #else |
| 8137 | chk->rec.data.tsn = asoc->sending_seq++; |
| 8138 | #endif |
| 8139 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) { |
| 8140 | sctp_misc_ints(SCTP_STRMOUT_LOG_SEND, |
| 8141 | (uint32_t)(uintptr_t)stcb, sp->length, |
| 8142 | (uint32_t)((chk->rec.data.sid << 16) | (0x0000ffff & chk->rec.data.mid)), |
| 8143 | chk->rec.data.tsn); |
| 8144 | } |
| 8145 | if (stcb->asoc.idata_supported == 0) { |
| 8146 | dchkh = mtod(chk->data, struct sctp_data_chunk *); |
| 8147 | } else { |
| 8148 | ndchkh = mtod(chk->data, struct sctp_idata_chunk *); |
| 8149 | } |
| 8150 | /* |
| 8151 | * Put the rest of the things in place now. Size was done |
| 8152 | * earlier in previous loop prior to padding. |
| 8153 | */ |
| 8154 |
| 8155 | #ifdef SCTP_ASOCLOG_OF_TSNS |
| 8156 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 8157 | if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) { |
| 8158 | asoc->tsn_out_at = 0; |
| 8159 | asoc->tsn_out_wrapped = 1; |
| 8160 | } |
| 8161 | asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.tsn; |
| 8162 | asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.sid; |
| 8163 | asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.mid; |
| 8164 | asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size; |
| 8165 | asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags; |
| 8166 | asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb; |
| 8167 | asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at; |
| 8168 | asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2; |
| 8169 | asoc->tsn_out_at++; |
| 8170 | #endif |
| 8171 | if (stcb->asoc.idata_supported == 0) { |
| 8172 | dchkh->ch.chunk_type = SCTP_DATA; |
| 8173 | dchkh->ch.chunk_flags = chk->rec.data.rcv_flags; |
| 8174 | dchkh->dp.tsn = htonl(chk->rec.data.tsn); |
| 8175 | dchkh->dp.sid = htons(strq->sid); |
| 8176 | dchkh->dp.ssn = htons((uint16_t)chk->rec.data.mid); |
| 8177 | dchkh->dp.ppid = chk->rec.data.ppid; |
| 8178 | dchkh->ch.chunk_length = htons(chk->send_size); |
| 8179 | } else { |
| 8180 | ndchkh->ch.chunk_type = SCTP_IDATA; |
| 8181 | ndchkh->ch.chunk_flags = chk->rec.data.rcv_flags; |
| 8182 | ndchkh->dp.tsn = htonl(chk->rec.data.tsn); |
| 8183 | ndchkh->dp.sid = htons(strq->sid); |
| 8184 | ndchkh->dp.reserved = htons(0); |
| 8185 | ndchkh->dp.mid = htonl(chk->rec.data.mid); |
| 8186 | if (sp->fsn == 0) |
| 8187 | ndchkh->dp.ppid_fsn.ppid = chk->rec.data.ppid; |
| 8188 | else |
| 8189 | ndchkh->dp.ppid_fsn.fsn = htonl(sp->fsn); |
| 8190 | sp->fsn++; |
| 8191 | ndchkh->ch.chunk_length = htons(chk->send_size); |
| 8192 | } |
| 8193 | /* Now advance the chk->send_size by the actual pad needed. */ |
| 8194 | if (chk->send_size < SCTP_SIZE32(chk->book_size)) { |
| 8195 | /* need a pad */ |
| 8196 | struct mbuf *lm; |
| 8197 | int pads; |
| 8198 |
| 8199 | pads = SCTP_SIZE32(chk->book_size) - chk->send_size; |
| 8200 | lm = sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf); |
| 8201 | if (lm != NULL) { |
| 8202 | chk->last_mbuf = lm; |
| 8203 | chk->pad_inplace = 1; |
| 8204 | } |
| 8205 | chk->send_size += pads; |
| 8206 | } |
| 8207 | if (PR_SCTP_ENABLED(chk->flags)) { |
| 8208 | asoc->pr_sctp_cnt++; |
| 8209 | } |
| 8210 | if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) { |
| 8211 | /* All done pull and kill the message */ |
| 8212 | if (sp->put_last_out == 0) { |
| 8213 | SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n"); |
| 8214 | SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n", |
| 8215 | sp->sender_all_done, |
| 8216 | sp->length, |
| 8217 | sp->msg_is_complete, |
| 8218 | sp->put_last_out, |
| 8219 | send_lock_up); |
| 8220 | } |
| 8221 | if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) { |
| 8222 | SCTP_TCB_SEND_LOCK(stcb); |
| 8223 | send_lock_up = 1; |
| 8224 | } |
| 8225 | atomic_subtract_int(&asoc->stream_queue_cnt, 1); |
| 8226 | TAILQ_REMOVE(&strq->outqueue, sp, next); |
| 8227 | stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up); |
| 8228 | if ((strq->state == SCTP_STREAM_RESET_PENDING) && |
| 8229 | (strq->chunks_on_queues == 0) && |
| 8230 | TAILQ_EMPTY(&strq->outqueue)) { |
| 8231 | stcb->asoc.trigger_reset = 1; |
| 8232 | } |
| 8233 | if (sp->net) { |
| 8234 | sctp_free_remote_addr(sp->net); |
| 8235 | sp->net = NULL; |
| 8236 | } |
| 8237 | if (sp->data) { |
| 8238 | sctp_m_freem(sp->data); |
| 8239 | sp->data = NULL; |
| 8240 | } |
| 8241 | sctp_free_a_strmoq(stcb, sp, so_locked); |
| 8242 | } |
| 8243 | asoc->chunks_on_out_queue++; |
| 8244 | strq->chunks_on_queues++; |
| 8245 | TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next); |
| 8246 | asoc->send_queue_cnt++; |
| 8247 | out_of: |
| 8248 | if (send_lock_up) { |
| 8249 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 8250 | } |
| 8251 | return (to_move); |
| 8252 | } |
| 8253 |
| 8254 | static void |
| 8255 | sctp_fill_outqueue(struct sctp_tcb *stcb, |
| 8256 | struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now, int so_locked) |
| 8257 | { |
| 8258 | struct sctp_association *asoc; |
| 8259 | struct sctp_stream_out *strq; |
| 8260 | uint32_t space_left, moved, total_moved; |
| 8261 | int bail, giveup; |
| 8262 |
| 8263 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 8264 | asoc = &stcb->asoc; |
| 8265 | total_moved = 0; |
| 8266 | switch (net->ro._l_addr.sa.sa_family) { |
| 8267 | #ifdef INET |
| 8268 | case AF_INET: |
| 8269 | space_left = net->mtu - SCTP_MIN_V4_OVERHEAD; |
| 8270 | break; |
| 8271 | #endif |
| 8272 | #ifdef INET6 |
| 8273 | case AF_INET6: |
| 8274 | space_left = net->mtu - SCTP_MIN_OVERHEAD; |
| 8275 | break; |
| 8276 | #endif |
| 8277 | #if defined(__Userspace__) |
| 8278 | case AF_CONN: |
| 8279 | space_left = net->mtu - sizeof(struct sctphdr); |
| 8280 | break; |
| 8281 | #endif |
| 8282 | default: |
| 8283 | /* TSNH */ |
| 8284 | space_left = net->mtu; |
| 8285 | break; |
| 8286 | } |
| 8287 | /* Need an allowance for the data chunk header too */ |
| 8288 | space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb); |
| 8289 |
| 8290 | /* must make even word boundary */ |
| 8291 | space_left &= 0xfffffffc; |
| 8292 | strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc); |
| 8293 | giveup = 0; |
| 8294 | bail = 0; |
| 8295 | while ((space_left > 0) && (strq != NULL)) { |
| 8296 | moved = sctp_move_to_outqueue(stcb, strq, space_left, frag_point, |
| 8297 | &giveup, eeor_mode, &bail, so_locked); |
| 8298 | stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, moved); |
| 8299 | if ((giveup != 0) || (bail != 0)) { |
| 8300 | break; |
| 8301 | } |
| 8302 | strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc); |
| 8303 | total_moved += moved; |
| 8304 | if (space_left >= moved) { |
| 8305 | space_left -= moved; |
| 8306 | } else { |
| 8307 | space_left = 0; |
| 8308 | } |
| 8309 | if (space_left >= SCTP_DATA_CHUNK_OVERHEAD(stcb)) { |
| 8310 | space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb); |
| 8311 | } else { |
| 8312 | space_left = 0; |
| 8313 | } |
| 8314 | space_left &= 0xfffffffc; |
| 8315 | } |
| 8316 | if (bail != 0) |
| 8317 | *quit_now = 1; |
| 8318 |
| 8319 | stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc); |
| 8320 |
| 8321 | if (total_moved == 0) { |
| 8322 | if ((stcb->asoc.sctp_cmt_on_off == 0) && |
| 8323 | (net == stcb->asoc.primary_destination)) { |
| 8324 | /* ran dry for primary network net */ |
| 8325 | SCTP_STAT_INCR(sctps_primary_randry); |
| 8326 | } else if (stcb->asoc.sctp_cmt_on_off > 0) { |
| 8327 | /* ran dry with CMT on */ |
| 8328 | SCTP_STAT_INCR(sctps_cmt_randry); |
| 8329 | } |
| 8330 | } |
| 8331 | } |
| 8332 |
| 8333 | void |
| 8334 | sctp_fix_ecn_echo(struct sctp_association *asoc) |
| 8335 | { |
| 8336 | struct sctp_tmit_chunk *chk; |
| 8337 |
| 8338 | TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { |
| 8339 | if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) { |
| 8340 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 8341 | } |
| 8342 | } |
| 8343 | } |
| 8344 |
| 8345 | void |
| 8346 | sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net) |
| 8347 | { |
| 8348 | struct sctp_association *asoc; |
| 8349 | struct sctp_tmit_chunk *chk; |
| 8350 | struct sctp_stream_queue_pending *sp; |
| 8351 | unsigned int i; |
| 8352 |
| 8353 | if (net == NULL) { |
| 8354 | return; |
| 8355 | } |
| 8356 | asoc = &stcb->asoc; |
| 8357 | for (i = 0; i < stcb->asoc.streamoutcnt; i++) { |
| 8358 | TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) { |
| 8359 | if (sp->net == net) { |
| 8360 | sctp_free_remote_addr(sp->net); |
| 8361 | sp->net = NULL; |
| 8362 | } |
| 8363 | } |
| 8364 | } |
| 8365 | TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) { |
| 8366 | if (chk->whoTo == net) { |
| 8367 | sctp_free_remote_addr(chk->whoTo); |
| 8368 | chk->whoTo = NULL; |
| 8369 | } |
| 8370 | } |
| 8371 | } |
| 8372 |
| 8373 | int |
| 8374 | sctp_med_chunk_output(struct sctp_inpcb *inp, |
| 8375 | struct sctp_tcb *stcb, |
| 8376 | struct sctp_association *asoc, |
| 8377 | int *num_out, |
| 8378 | int *reason_code, |
| 8379 | int control_only, int from_where, |
| 8380 | struct timeval *now, int *now_filled, int frag_point, int so_locked) |
| 8381 | { |
| 8382 | /** |
| 8383 | * Ok this is the generic chunk service queue. we must do the |
| 8384 | * following: |
| 8385 | * - Service the stream queue that is next, moving any |
| 8386 | * message (note I must get a complete message i.e. FIRST/MIDDLE and |
| 8387 | * LAST to the out queue in one pass) and assigning TSN's. This |
| 8388 | * only applys though if the peer does not support NDATA. For NDATA |
| 8389 | * chunks its ok to not send the entire message ;-) |
| 8390 | * - Check to see if the cwnd/rwnd allows any output, if so we go ahead and |
| 8391 | * fomulate and send the low level chunks. Making sure to combine |
| 8392 | * any control in the control chunk queue also. |
| 8393 | */ |
| 8394 | struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL; |
| 8395 | struct mbuf *outchain, *endoutchain; |
| 8396 | struct sctp_tmit_chunk *chk, *nchk; |
| 8397 |
| 8398 | /* temp arrays for unlinking */ |
| 8399 | struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING]; |
| 8400 | int no_fragmentflg, error; |
| 8401 | unsigned int max_rwnd_per_dest, max_send_per_dest; |
| 8402 | int one_chunk, hbflag, skip_data_for_this_net; |
| 8403 | int asconf, cookie, no_out_cnt; |
| 8404 | int bundle_at, ctl_cnt, no_data_chunks, eeor_mode; |
| 8405 | unsigned int mtu, r_mtu, omtu, mx_mtu, to_out; |
| 8406 | int tsns_sent = 0; |
| 8407 | uint32_t auth_offset; |
| 8408 | struct sctp_auth_chunk *auth; |
| 8409 | uint16_t auth_keyid; |
| 8410 | int override_ok = 1; |
| 8411 | int skip_fill_up = 0; |
| 8412 | int data_auth_reqd = 0; |
| 8413 | /* JRS 5/14/07 - Add flag for whether a heartbeat is sent to |
| 8414 | the destination. */ |
| 8415 | int quit_now = 0; |
| 8416 |
| 8417 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 8418 | if (so_locked) { |
| 8419 | sctp_lock_assert(SCTP_INP_SO(inp)); |
| 8420 | } else { |
| 8421 | sctp_unlock_assert(SCTP_INP_SO(inp)); |
| 8422 | } |
| 8423 | #endif |
| 8424 | *num_out = 0; |
| 8425 | *reason_code = 0; |
| 8426 | auth_keyid = stcb->asoc.authinfo.active_keyid; |
| 8427 | if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) || |
| 8428 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) || |
| 8429 | (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) { |
| 8430 | eeor_mode = 1; |
| 8431 | } else { |
| 8432 | eeor_mode = 0; |
| 8433 | } |
| 8434 | ctl_cnt = no_out_cnt = asconf = cookie = 0; |
| 8435 | /* |
| 8436 | * First lets prime the pump. For each destination, if there is room |
| 8437 | * in the flight size, attempt to pull an MTU's worth out of the |
| 8438 | * stream queues into the general send_queue |
| 8439 | */ |
| 8440 | #ifdef SCTP_AUDITING_ENABLED |
| 8441 | sctp_audit_log(0xC2, 2); |
| 8442 | #endif |
| 8443 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 8444 | hbflag = 0; |
| 8445 | if (control_only) |
| 8446 | no_data_chunks = 1; |
| 8447 | else |
| 8448 | no_data_chunks = 0; |
| 8449 |
| 8450 | /* Nothing to possible to send? */ |
| 8451 | if ((TAILQ_EMPTY(&asoc->control_send_queue) || |
| 8452 | (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) && |
| 8453 | TAILQ_EMPTY(&asoc->asconf_send_queue) && |
| 8454 | TAILQ_EMPTY(&asoc->send_queue) && |
| 8455 | sctp_is_there_unsent_data(stcb, so_locked) == 0) { |
| 8456 | nothing_to_send: |
| 8457 | *reason_code = 9; |
| 8458 | return (0); |
| 8459 | } |
| 8460 | if (asoc->peers_rwnd == 0) { |
| 8461 | /* No room in peers rwnd */ |
| 8462 | *reason_code = 1; |
| 8463 | if (asoc->total_flight > 0) { |
| 8464 | /* we are allowed one chunk in flight */ |
| 8465 | no_data_chunks = 1; |
| 8466 | } |
| 8467 | } |
| 8468 | if (stcb->asoc.ecn_echo_cnt_onq) { |
| 8469 | /* Record where a sack goes, if any */ |
| 8470 | if (no_data_chunks && |
| 8471 | (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) { |
| 8472 | /* Nothing but ECNe to send - we don't do that */ |
| 8473 | goto nothing_to_send; |
| 8474 | } |
| 8475 | TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { |
| 8476 | if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) || |
| 8477 | (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) { |
| 8478 | sack_goes_to = chk->whoTo; |
| 8479 | break; |
| 8480 | } |
| 8481 | } |
| 8482 | } |
| 8483 | max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets); |
| 8484 | if (stcb->sctp_socket) |
| 8485 | max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets; |
| 8486 | else |
| 8487 | max_send_per_dest = 0; |
| 8488 | if (no_data_chunks == 0) { |
| 8489 | /* How many non-directed chunks are there? */ |
| 8490 | TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) { |
| 8491 | if (chk->whoTo == NULL) { |
| 8492 | /* We already have non-directed |
| 8493 | * chunks on the queue, no need |
| 8494 | * to do a fill-up. |
| 8495 | */ |
| 8496 | skip_fill_up = 1; |
| 8497 | break; |
| 8498 | } |
| 8499 | } |
| 8500 | } |
| 8501 | if ((no_data_chunks == 0) && |
| 8502 | (skip_fill_up == 0) && |
| 8503 | (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) { |
| 8504 | TAILQ_FOREACH(net, &asoc->nets, sctp_next) { |
| 8505 | /* |
| 8506 | * This for loop we are in takes in |
| 8507 | * each net, if its's got space in cwnd and |
| 8508 | * has data sent to it (when CMT is off) then it |
| 8509 | * calls sctp_fill_outqueue for the net. This gets |
| 8510 | * data on the send queue for that network. |
| 8511 | * |
| 8512 | * In sctp_fill_outqueue TSN's are assigned and |
| 8513 | * data is copied out of the stream buffers. Note |
| 8514 | * mostly copy by reference (we hope). |
| 8515 | */ |
| 8516 | net->window_probe = 0; |
| 8517 | if ((net != stcb->asoc.alternate) && |
| 8518 | ((net->dest_state & SCTP_ADDR_PF) || |
| 8519 | (!(net->dest_state & SCTP_ADDR_REACHABLE)) || |
| 8520 | (net->dest_state & SCTP_ADDR_UNCONFIRMED))) { |
| 8521 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 8522 | sctp_log_cwnd(stcb, net, 1, |
| 8523 | SCTP_CWND_LOG_FILL_OUTQ_CALLED); |
| 8524 | } |
| 8525 | continue; |
| 8526 | } |
| 8527 | if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) && |
| 8528 | (net->flight_size == 0)) { |
| 8529 | (*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins)(stcb, net); |
| 8530 | } |
| 8531 | if (net->flight_size >= net->cwnd) { |
| 8532 | /* skip this network, no room - can't fill */ |
| 8533 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 8534 | sctp_log_cwnd(stcb, net, 3, |
| 8535 | SCTP_CWND_LOG_FILL_OUTQ_CALLED); |
| 8536 | } |
| 8537 | continue; |
| 8538 | } |
| 8539 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 8540 | sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED); |
| 8541 | } |
| 8542 | sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked); |
| 8543 | if (quit_now) { |
| 8544 | /* memory alloc failure */ |
| 8545 | no_data_chunks = 1; |
| 8546 | break; |
| 8547 | } |
| 8548 | } |
| 8549 | } |
| 8550 | /* now service each destination and send out what we can for it */ |
| 8551 | /* Nothing to send? */ |
| 8552 | if (TAILQ_EMPTY(&asoc->control_send_queue) && |
| 8553 | TAILQ_EMPTY(&asoc->asconf_send_queue) && |
| 8554 | TAILQ_EMPTY(&asoc->send_queue)) { |
| 8555 | *reason_code = 8; |
| 8556 | return (0); |
| 8557 | } |
| 8558 |
| 8559 | if (asoc->sctp_cmt_on_off > 0) { |
| 8560 | /* get the last start point */ |
| 8561 | start_at = asoc->last_net_cmt_send_started; |
| 8562 | if (start_at == NULL) { |
| 8563 | /* null so to beginning */ |
| 8564 | start_at = TAILQ_FIRST(&asoc->nets); |
| 8565 | } else { |
| 8566 | start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next); |
| 8567 | if (start_at == NULL) { |
| 8568 | start_at = TAILQ_FIRST(&asoc->nets); |
| 8569 | } |
| 8570 | } |
| 8571 | asoc->last_net_cmt_send_started = start_at; |
| 8572 | } else { |
| 8573 | start_at = TAILQ_FIRST(&asoc->nets); |
| 8574 | } |
| 8575 | TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { |
| 8576 | if (chk->whoTo == NULL) { |
| 8577 | if (asoc->alternate) { |
| 8578 | chk->whoTo = asoc->alternate; |
| 8579 | } else { |
| 8580 | chk->whoTo = asoc->primary_destination; |
| 8581 | } |
| 8582 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 8583 | } |
| 8584 | } |
| 8585 | old_start_at = NULL; |
| 8586 | again_one_more_time: |
| 8587 | for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) { |
| 8588 | /* how much can we send? */ |
| 8589 | /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */ |
| 8590 | if (old_start_at && (old_start_at == net)) { |
| 8591 | /* through list ocmpletely. */ |
| 8592 | break; |
| 8593 | } |
| 8594 | tsns_sent = 0xa; |
| 8595 | if (TAILQ_EMPTY(&asoc->control_send_queue) && |
| 8596 | TAILQ_EMPTY(&asoc->asconf_send_queue) && |
| 8597 | (net->flight_size >= net->cwnd)) { |
| 8598 | /* Nothing on control or asconf and flight is full, we can skip |
| 8599 | * even in the CMT case. |
| 8600 | */ |
| 8601 | continue; |
| 8602 | } |
| 8603 | bundle_at = 0; |
| 8604 | endoutchain = outchain = NULL; |
| 8605 | auth = NULL; |
| 8606 | auth_offset = 0; |
| 8607 | no_fragmentflg = 1; |
| 8608 | one_chunk = 0; |
| 8609 | if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { |
| 8610 | skip_data_for_this_net = 1; |
| 8611 | } else { |
| 8612 | skip_data_for_this_net = 0; |
| 8613 | } |
| 8614 | switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) { |
| 8615 | #ifdef INET |
| 8616 | case AF_INET: |
| 8617 | mtu = net->mtu - SCTP_MIN_V4_OVERHEAD; |
| 8618 | break; |
| 8619 | #endif |
| 8620 | #ifdef INET6 |
| 8621 | case AF_INET6: |
| 8622 | mtu = net->mtu - SCTP_MIN_OVERHEAD; |
| 8623 | break; |
| 8624 | #endif |
| 8625 | #if defined(__Userspace__) |
| 8626 | case AF_CONN: |
| 8627 | mtu = net->mtu - sizeof(struct sctphdr); |
| 8628 | break; |
| 8629 | #endif |
| 8630 | default: |
| 8631 | /* TSNH */ |
| 8632 | mtu = net->mtu; |
| 8633 | break; |
| 8634 | } |
| 8635 | mx_mtu = mtu; |
| 8636 | to_out = 0; |
| 8637 | if (mtu > asoc->peers_rwnd) { |
| 8638 | if (asoc->total_flight > 0) { |
| 8639 | /* We have a packet in flight somewhere */ |
| 8640 | r_mtu = asoc->peers_rwnd; |
| 8641 | } else { |
| 8642 | /* We are always allowed to send one MTU out */ |
| 8643 | one_chunk = 1; |
| 8644 | r_mtu = mtu; |
| 8645 | } |
| 8646 | } else { |
| 8647 | r_mtu = mtu; |
| 8648 | } |
| 8649 | error = 0; |
| 8650 | /************************/ |
| 8651 | /* ASCONF transmission */ |
| 8652 | /************************/ |
| 8653 | /* Now first lets go through the asconf queue */ |
| 8654 | TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) { |
| 8655 | if (chk->rec.chunk_id.id != SCTP_ASCONF) { |
| 8656 | continue; |
| 8657 | } |
| 8658 | if (chk->whoTo == NULL) { |
| 8659 | if (asoc->alternate == NULL) { |
| 8660 | if (asoc->primary_destination != net) { |
| 8661 | break; |
| 8662 | } |
| 8663 | } else { |
| 8664 | if (asoc->alternate != net) { |
| 8665 | break; |
| 8666 | } |
| 8667 | } |
| 8668 | } else { |
| 8669 | if (chk->whoTo != net) { |
| 8670 | break; |
| 8671 | } |
| 8672 | } |
| 8673 | if (chk->data == NULL) { |
| 8674 | break; |
| 8675 | } |
| 8676 | if (chk->sent != SCTP_DATAGRAM_UNSENT && |
| 8677 | chk->sent != SCTP_DATAGRAM_RESEND) { |
| 8678 | break; |
| 8679 | } |
| 8680 | /* |
| 8681 | * if no AUTH is yet included and this chunk |
| 8682 | * requires it, make sure to account for it. We |
| 8683 | * don't apply the size until the AUTH chunk is |
| 8684 | * actually added below in case there is no room for |
| 8685 | * this chunk. NOTE: we overload the use of "omtu" |
| 8686 | * here |
| 8687 | */ |
| 8688 | if ((auth == NULL) && |
| 8689 | sctp_auth_is_required_chunk(chk->rec.chunk_id.id, |
| 8690 | stcb->asoc.peer_auth_chunks)) { |
| 8691 | omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); |
| 8692 | } else |
| 8693 | omtu = 0; |
| 8694 | /* Here we do NOT factor the r_mtu */ |
| 8695 | if ((chk->send_size < (int)(mtu - omtu)) || |
| 8696 | (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) { |
| 8697 | /* |
| 8698 | * We probably should glom the mbuf chain |
| 8699 | * from the chk->data for control but the |
| 8700 | * problem is it becomes yet one more level |
| 8701 | * of tracking to do if for some reason |
| 8702 | * output fails. Then I have got to |
| 8703 | * reconstruct the merged control chain.. el |
| 8704 | * yucko.. for now we take the easy way and |
| 8705 | * do the copy |
| 8706 | */ |
| 8707 | /* |
| 8708 | * Add an AUTH chunk, if chunk requires it |
| 8709 | * save the offset into the chain for AUTH |
| 8710 | */ |
| 8711 | if ((auth == NULL) && |
| 8712 | (sctp_auth_is_required_chunk(chk->rec.chunk_id.id, |
| 8713 | stcb->asoc.peer_auth_chunks))) { |
| 8714 | outchain = sctp_add_auth_chunk(outchain, |
| 8715 | &endoutchain, |
| 8716 | &auth, |
| 8717 | &auth_offset, |
| 8718 | stcb, |
| 8719 | chk->rec.chunk_id.id); |
| 8720 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 8721 | } |
| 8722 | outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, |
| 8723 | (int)chk->rec.chunk_id.can_take_data, |
| 8724 | chk->send_size, chk->copy_by_ref); |
| 8725 | if (outchain == NULL) { |
| 8726 | *reason_code = 8; |
| 8727 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 8728 | return (ENOMEM); |
| 8729 | } |
| 8730 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 8731 | /* update our MTU size */ |
| 8732 | if (mtu > (chk->send_size + omtu)) |
| 8733 | mtu -= (chk->send_size + omtu); |
| 8734 | else |
| 8735 | mtu = 0; |
| 8736 | to_out += (chk->send_size + omtu); |
| 8737 | /* Do clear IP_DF ? */ |
| 8738 | if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) { |
| 8739 | no_fragmentflg = 0; |
| 8740 | } |
| 8741 | if (chk->rec.chunk_id.can_take_data) |
| 8742 | chk->data = NULL; |
| 8743 | /* |
| 8744 | * set hb flag since we can |
| 8745 | * use these for RTO |
| 8746 | */ |
| 8747 | hbflag = 1; |
| 8748 | asconf = 1; |
| 8749 | /* |
| 8750 | * should sysctl this: don't |
| 8751 | * bundle data with ASCONF |
| 8752 | * since it requires AUTH |
| 8753 | */ |
| 8754 | no_data_chunks = 1; |
| 8755 | chk->sent = SCTP_DATAGRAM_SENT; |
| 8756 | if (chk->whoTo == NULL) { |
| 8757 | chk->whoTo = net; |
| 8758 | atomic_add_int(&net->ref_count, 1); |
| 8759 | } |
| 8760 | chk->snd_count++; |
| 8761 | if (mtu == 0) { |
| 8762 | /* |
| 8763 | * Ok we are out of room but we can |
| 8764 | * output without effecting the |
| 8765 | * flight size since this little guy |
| 8766 | * is a control only packet. |
| 8767 | */ |
| 8768 | sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net); |
| 8769 | /* |
| 8770 | * do NOT clear the asconf |
| 8771 | * flag as it is used to do |
| 8772 | * appropriate source address |
| 8773 | * selection. |
| 8774 | */ |
| 8775 | if (*now_filled == 0) { |
| 8776 | (void)SCTP_GETTIME_TIMEVAL(now); |
| 8777 | *now_filled = 1; |
| 8778 | } |
| 8779 | net->last_sent_time = *now; |
| 8780 | hbflag = 0; |
| 8781 | if ((error = sctp_lowlevel_chunk_output(inp, stcb, net, |
| 8782 | (struct sockaddr *)&net->ro._l_addr, |
| 8783 | outchain, auth_offset, auth, |
| 8784 | stcb->asoc.authinfo.active_keyid, |
| 8785 | no_fragmentflg, 0, asconf, |
| 8786 | inp->sctp_lport, stcb->rport, |
| 8787 | htonl(stcb->asoc.peer_vtag), |
| 8788 | net->port, NULL, |
| 8789 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 8790 | 0, 0, |
| 8791 | #endif |
| 8792 | so_locked))) { |
| 8793 | /* error, we could not output */ |
| 8794 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error); |
| 8795 | if (from_where == 0) { |
| 8796 | SCTP_STAT_INCR(sctps_lowlevelerrusr); |
| 8797 | } |
| 8798 | if (error == ENOBUFS) { |
| 8799 | asoc->ifp_had_enobuf = 1; |
| 8800 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 8801 | } |
| 8802 | /* error, could not output */ |
| 8803 | if (error == EHOSTUNREACH) { |
| 8804 | /* |
| 8805 | * Destination went |
| 8806 | * unreachable |
| 8807 | * during this send |
| 8808 | */ |
| 8809 | sctp_move_chunks_from_net(stcb, net); |
| 8810 | } |
| 8811 | *reason_code = 7; |
| 8812 | break; |
| 8813 | } else { |
| 8814 | asoc->ifp_had_enobuf = 0; |
| 8815 | } |
| 8816 | /* |
| 8817 | * increase the number we sent, if a |
| 8818 | * cookie is sent we don't tell them |
| 8819 | * any was sent out. |
| 8820 | */ |
| 8821 | outchain = endoutchain = NULL; |
| 8822 | auth = NULL; |
| 8823 | auth_offset = 0; |
| 8824 | if (!no_out_cnt) |
| 8825 | *num_out += ctl_cnt; |
| 8826 | /* recalc a clean slate and setup */ |
| 8827 | switch (net->ro._l_addr.sa.sa_family) { |
| 8828 | #ifdef INET |
| 8829 | case AF_INET: |
| 8830 | mtu = net->mtu - SCTP_MIN_V4_OVERHEAD; |
| 8831 | break; |
| 8832 | #endif |
| 8833 | #ifdef INET6 |
| 8834 | case AF_INET6: |
| 8835 | mtu = net->mtu - SCTP_MIN_OVERHEAD; |
| 8836 | break; |
| 8837 | #endif |
| 8838 | #if defined(__Userspace__) |
| 8839 | case AF_CONN: |
| 8840 | mtu = net->mtu - sizeof(struct sctphdr); |
| 8841 | break; |
| 8842 | #endif |
| 8843 | default: |
| 8844 | /* TSNH */ |
| 8845 | mtu = net->mtu; |
| 8846 | break; |
| 8847 | } |
| 8848 | to_out = 0; |
| 8849 | no_fragmentflg = 1; |
| 8850 | } |
| 8851 | } |
| 8852 | } |
| 8853 | if (error != 0) { |
| 8854 | /* try next net */ |
| 8855 | continue; |
| 8856 | } |
| 8857 | /************************/ |
| 8858 | /* Control transmission */ |
| 8859 | /************************/ |
| 8860 | /* Now first lets go through the control queue */ |
| 8861 | TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { |
| 8862 | if ((sack_goes_to) && |
| 8863 | (chk->rec.chunk_id.id == SCTP_ECN_ECHO) && |
| 8864 | (chk->whoTo != sack_goes_to)) { |
| 8865 | /* |
| 8866 | * if we have a sack in queue, and we are looking at an |
| 8867 | * ecn echo that is NOT queued to where the sack is going.. |
| 8868 | */ |
| 8869 | if (chk->whoTo == net) { |
| 8870 | /* Don't transmit it to where its going (current net) */ |
| 8871 | continue; |
| 8872 | } else if (sack_goes_to == net) { |
| 8873 | /* But do transmit it to this address */ |
| 8874 | goto skip_net_check; |
| 8875 | } |
| 8876 | } |
| 8877 | if (chk->whoTo == NULL) { |
| 8878 | if (asoc->alternate == NULL) { |
| 8879 | if (asoc->primary_destination != net) { |
| 8880 | continue; |
| 8881 | } |
| 8882 | } else { |
| 8883 | if (asoc->alternate != net) { |
| 8884 | continue; |
| 8885 | } |
| 8886 | } |
| 8887 | } else { |
| 8888 | if (chk->whoTo != net) { |
| 8889 | continue; |
| 8890 | } |
| 8891 | } |
| 8892 | skip_net_check: |
| 8893 | if (chk->data == NULL) { |
| 8894 | continue; |
| 8895 | } |
| 8896 | if (chk->sent != SCTP_DATAGRAM_UNSENT) { |
| 8897 | /* |
| 8898 | * It must be unsent. Cookies and ASCONF's |
| 8899 | * hang around but there timers will force |
| 8900 | * when marked for resend. |
| 8901 | */ |
| 8902 | continue; |
| 8903 | } |
| 8904 | /* |
| 8905 | * if no AUTH is yet included and this chunk |
| 8906 | * requires it, make sure to account for it. We |
| 8907 | * don't apply the size until the AUTH chunk is |
| 8908 | * actually added below in case there is no room for |
| 8909 | * this chunk. NOTE: we overload the use of "omtu" |
| 8910 | * here |
| 8911 | */ |
| 8912 | if ((auth == NULL) && |
| 8913 | sctp_auth_is_required_chunk(chk->rec.chunk_id.id, |
| 8914 | stcb->asoc.peer_auth_chunks)) { |
| 8915 | omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); |
| 8916 | } else |
| 8917 | omtu = 0; |
| 8918 | /* Here we do NOT factor the r_mtu */ |
| 8919 | if ((chk->send_size <= (int)(mtu - omtu)) || |
| 8920 | (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) { |
| 8921 | /* |
| 8922 | * We probably should glom the mbuf chain |
| 8923 | * from the chk->data for control but the |
| 8924 | * problem is it becomes yet one more level |
| 8925 | * of tracking to do if for some reason |
| 8926 | * output fails. Then I have got to |
| 8927 | * reconstruct the merged control chain.. el |
| 8928 | * yucko.. for now we take the easy way and |
| 8929 | * do the copy |
| 8930 | */ |
| 8931 | /* |
| 8932 | * Add an AUTH chunk, if chunk requires it |
| 8933 | * save the offset into the chain for AUTH |
| 8934 | */ |
| 8935 | if ((auth == NULL) && |
| 8936 | (sctp_auth_is_required_chunk(chk->rec.chunk_id.id, |
| 8937 | stcb->asoc.peer_auth_chunks))) { |
| 8938 | outchain = sctp_add_auth_chunk(outchain, |
| 8939 | &endoutchain, |
| 8940 | &auth, |
| 8941 | &auth_offset, |
| 8942 | stcb, |
| 8943 | chk->rec.chunk_id.id); |
| 8944 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 8945 | } |
| 8946 | outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, |
| 8947 | (int)chk->rec.chunk_id.can_take_data, |
| 8948 | chk->send_size, chk->copy_by_ref); |
| 8949 | if (outchain == NULL) { |
| 8950 | *reason_code = 8; |
| 8951 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 8952 | return (ENOMEM); |
| 8953 | } |
| 8954 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 8955 | /* update our MTU size */ |
| 8956 | if (mtu > (chk->send_size + omtu)) |
| 8957 | mtu -= (chk->send_size + omtu); |
| 8958 | else |
| 8959 | mtu = 0; |
| 8960 | to_out += (chk->send_size + omtu); |
| 8961 | /* Do clear IP_DF ? */ |
| 8962 | if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) { |
| 8963 | no_fragmentflg = 0; |
| 8964 | } |
| 8965 | if (chk->rec.chunk_id.can_take_data) |
| 8966 | chk->data = NULL; |
| 8967 | /* Mark things to be removed, if needed */ |
| 8968 | if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) || |
| 8969 | (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */ |
| 8970 | (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) || |
| 8971 | (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) || |
| 8972 | (chk->rec.chunk_id.id == SCTP_SHUTDOWN) || |
| 8973 | (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) || |
| 8974 | (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) || |
| 8975 | (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) || |
| 8976 | (chk->rec.chunk_id.id == SCTP_ECN_CWR) || |
| 8977 | (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) || |
| 8978 | (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) { |
| 8979 | if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) { |
| 8980 | hbflag = 1; |
| 8981 | } |
| 8982 | /* remove these chunks at the end */ |
| 8983 | if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) || |
| 8984 | (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) { |
| 8985 | /* turn off the timer */ |
| 8986 | if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) { |
| 8987 | sctp_timer_stop(SCTP_TIMER_TYPE_RECV, |
| 8988 | inp, stcb, NULL, |
| 8989 | SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1); |
| 8990 | } |
| 8991 | } |
| 8992 | ctl_cnt++; |
| 8993 | } else { |
| 8994 | /* |
| 8995 | * Other chunks, since they have |
| 8996 | * timers running (i.e. COOKIE) |
| 8997 | * we just "trust" that it |
| 8998 | * gets sent or retransmitted. |
| 8999 | */ |
| 9000 | ctl_cnt++; |
| 9001 | if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) { |
| 9002 | cookie = 1; |
| 9003 | no_out_cnt = 1; |
| 9004 | } else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) { |
| 9005 | /* |
| 9006 | * Increment ecne send count here |
| 9007 | * this means we may be over-zealous in |
| 9008 | * our counting if the send fails, but its |
| 9009 | * the best place to do it (we used to do |
| 9010 | * it in the queue of the chunk, but that did |
| 9011 | * not tell how many times it was sent. |
| 9012 | */ |
| 9013 | SCTP_STAT_INCR(sctps_sendecne); |
| 9014 | } |
| 9015 | chk->sent = SCTP_DATAGRAM_SENT; |
| 9016 | if (chk->whoTo == NULL) { |
| 9017 | chk->whoTo = net; |
| 9018 | atomic_add_int(&net->ref_count, 1); |
| 9019 | } |
| 9020 | chk->snd_count++; |
| 9021 | } |
| 9022 | if (mtu == 0) { |
| 9023 | /* |
| 9024 | * Ok we are out of room but we can |
| 9025 | * output without effecting the |
| 9026 | * flight size since this little guy |
| 9027 | * is a control only packet. |
| 9028 | */ |
| 9029 | if (asconf) { |
| 9030 | sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net); |
| 9031 | /* |
| 9032 | * do NOT clear the asconf |
| 9033 | * flag as it is used to do |
| 9034 | * appropriate source address |
| 9035 | * selection. |
| 9036 | */ |
| 9037 | } |
| 9038 | if (cookie) { |
| 9039 | sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net); |
| 9040 | cookie = 0; |
| 9041 | } |
| 9042 | /* Only HB or ASCONF advances time */ |
| 9043 | if (hbflag) { |
| 9044 | if (*now_filled == 0) { |
| 9045 | (void)SCTP_GETTIME_TIMEVAL(now); |
| 9046 | *now_filled = 1; |
| 9047 | } |
| 9048 | net->last_sent_time = *now; |
| 9049 | hbflag = 0; |
| 9050 | } |
| 9051 | if ((error = sctp_lowlevel_chunk_output(inp, stcb, net, |
| 9052 | (struct sockaddr *)&net->ro._l_addr, |
| 9053 | outchain, |
| 9054 | auth_offset, auth, |
| 9055 | stcb->asoc.authinfo.active_keyid, |
| 9056 | no_fragmentflg, 0, asconf, |
| 9057 | inp->sctp_lport, stcb->rport, |
| 9058 | htonl(stcb->asoc.peer_vtag), |
| 9059 | net->port, NULL, |
| 9060 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 9061 | 0, 0, |
| 9062 | #endif |
| 9063 | so_locked))) { |
| 9064 | /* error, we could not output */ |
| 9065 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error); |
| 9066 | if (from_where == 0) { |
| 9067 | SCTP_STAT_INCR(sctps_lowlevelerrusr); |
| 9068 | } |
| 9069 | if (error == ENOBUFS) { |
| 9070 | asoc->ifp_had_enobuf = 1; |
| 9071 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 9072 | } |
| 9073 | if (error == EHOSTUNREACH) { |
| 9074 | /* |
| 9075 | * Destination went |
| 9076 | * unreachable |
| 9077 | * during this send |
| 9078 | */ |
| 9079 | sctp_move_chunks_from_net(stcb, net); |
| 9080 | } |
| 9081 | *reason_code = 7; |
| 9082 | break; |
| 9083 | } else { |
| 9084 | asoc->ifp_had_enobuf = 0; |
| 9085 | } |
| 9086 | /* |
| 9087 | * increase the number we sent, if a |
| 9088 | * cookie is sent we don't tell them |
| 9089 | * any was sent out. |
| 9090 | */ |
| 9091 | outchain = endoutchain = NULL; |
| 9092 | auth = NULL; |
| 9093 | auth_offset = 0; |
| 9094 | if (!no_out_cnt) |
| 9095 | *num_out += ctl_cnt; |
| 9096 | /* recalc a clean slate and setup */ |
| 9097 | switch (net->ro._l_addr.sa.sa_family) { |
| 9098 | #ifdef INET |
| 9099 | case AF_INET: |
| 9100 | mtu = net->mtu - SCTP_MIN_V4_OVERHEAD; |
| 9101 | break; |
| 9102 | #endif |
| 9103 | #ifdef INET6 |
| 9104 | case AF_INET6: |
| 9105 | mtu = net->mtu - SCTP_MIN_OVERHEAD; |
| 9106 | break; |
| 9107 | #endif |
| 9108 | #if defined(__Userspace__) |
| 9109 | case AF_CONN: |
| 9110 | mtu = net->mtu - sizeof(struct sctphdr); |
| 9111 | break; |
| 9112 | #endif |
| 9113 | default: |
| 9114 | /* TSNH */ |
| 9115 | mtu = net->mtu; |
| 9116 | break; |
| 9117 | } |
| 9118 | to_out = 0; |
| 9119 | no_fragmentflg = 1; |
| 9120 | } |
| 9121 | } |
| 9122 | } |
| 9123 | if (error != 0) { |
| 9124 | /* try next net */ |
| 9125 | continue; |
| 9126 | } |
| 9127 | /* JRI: if dest is in PF state, do not send data to it */ |
| 9128 | if ((asoc->sctp_cmt_on_off > 0) && |
| 9129 | (net != stcb->asoc.alternate) && |
| 9130 | (net->dest_state & SCTP_ADDR_PF)) { |
| 9131 | goto no_data_fill; |
| 9132 | } |
| 9133 | if (net->flight_size >= net->cwnd) { |
| 9134 | goto no_data_fill; |
| 9135 | } |
| 9136 | if ((asoc->sctp_cmt_on_off > 0) && |
| 9137 | (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) && |
| 9138 | (net->flight_size > max_rwnd_per_dest)) { |
| 9139 | goto no_data_fill; |
| 9140 | } |
| 9141 | /* |
| 9142 | * We need a specific accounting for the usage of the |
| 9143 | * send buffer. We also need to check the number of messages |
| 9144 | * per net. For now, this is better than nothing and it |
| 9145 | * disabled by default... |
| 9146 | */ |
| 9147 | if ((asoc->sctp_cmt_on_off > 0) && |
| 9148 | (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) && |
| 9149 | (max_send_per_dest > 0) && |
| 9150 | (net->flight_size > max_send_per_dest)) { |
| 9151 | goto no_data_fill; |
| 9152 | } |
| 9153 | /*********************/ |
| 9154 | /* Data transmission */ |
| 9155 | /*********************/ |
| 9156 | /* |
| 9157 | * if AUTH for DATA is required and no AUTH has been added |
| 9158 | * yet, account for this in the mtu now... if no data can be |
| 9159 | * bundled, this adjustment won't matter anyways since the |
| 9160 | * packet will be going out... |
| 9161 | */ |
| 9162 | data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, |
| 9163 | stcb->asoc.peer_auth_chunks); |
| 9164 | if (data_auth_reqd && (auth == NULL)) { |
| 9165 | mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); |
| 9166 | } |
| 9167 | /* now lets add any data within the MTU constraints */ |
| 9168 | switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) { |
| 9169 | #ifdef INET |
| 9170 | case AF_INET: |
| 9171 | if (net->mtu > SCTP_MIN_V4_OVERHEAD) |
| 9172 | omtu = net->mtu - SCTP_MIN_V4_OVERHEAD; |
| 9173 | else |
| 9174 | omtu = 0; |
| 9175 | break; |
| 9176 | #endif |
| 9177 | #ifdef INET6 |
| 9178 | case AF_INET6: |
| 9179 | if (net->mtu > SCTP_MIN_OVERHEAD) |
| 9180 | omtu = net->mtu - SCTP_MIN_OVERHEAD; |
| 9181 | else |
| 9182 | omtu = 0; |
| 9183 | break; |
| 9184 | #endif |
| 9185 | #if defined(__Userspace__) |
| 9186 | case AF_CONN: |
| 9187 | if (net->mtu > sizeof(struct sctphdr)) { |
| 9188 | omtu = net->mtu - sizeof(struct sctphdr); |
| 9189 | } else { |
| 9190 | omtu = 0; |
| 9191 | } |
| 9192 | break; |
| 9193 | #endif |
| 9194 | default: |
| 9195 | /* TSNH */ |
| 9196 | omtu = 0; |
| 9197 | break; |
| 9198 | } |
| 9199 | if ((((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) || |
| 9200 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) && |
| 9201 | (skip_data_for_this_net == 0)) || |
| 9202 | (cookie)) { |
| 9203 | TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { |
| 9204 | if (no_data_chunks) { |
| 9205 | /* let only control go out */ |
| 9206 | *reason_code = 1; |
| 9207 | break; |
| 9208 | } |
| 9209 | if (net->flight_size >= net->cwnd) { |
| 9210 | /* skip this net, no room for data */ |
| 9211 | *reason_code = 2; |
| 9212 | break; |
| 9213 | } |
| 9214 | if ((chk->whoTo != NULL) && |
| 9215 | (chk->whoTo != net)) { |
| 9216 | /* Don't send the chunk on this net */ |
| 9217 | continue; |
| 9218 | } |
| 9219 |
| 9220 | if (asoc->sctp_cmt_on_off == 0) { |
| 9221 | if ((asoc->alternate) && |
| 9222 | (asoc->alternate != net) && |
| 9223 | (chk->whoTo == NULL)) { |
| 9224 | continue; |
| 9225 | } else if ((net != asoc->primary_destination) && |
| 9226 | (asoc->alternate == NULL) && |
| 9227 | (chk->whoTo == NULL)) { |
| 9228 | continue; |
| 9229 | } |
| 9230 | } |
| 9231 | if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) { |
| 9232 | /*- |
| 9233 | * strange, we have a chunk that is |
| 9234 | * to big for its destination and |
| 9235 | * yet no fragment ok flag. |
| 9236 | * Something went wrong when the |
| 9237 | * PMTU changed...we did not mark |
| 9238 | * this chunk for some reason?? I |
| 9239 | * will fix it here by letting IP |
| 9240 | * fragment it for now and printing |
| 9241 | * a warning. This really should not |
| 9242 | * happen ... |
| 9243 | */ |
| 9244 | SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n", |
| 9245 | chk->send_size, mtu); |
| 9246 | chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; |
| 9247 | } |
| 9248 | if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && |
| 9249 | (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) { |
| 9250 | struct sctp_data_chunk *dchkh; |
| 9251 |
| 9252 | dchkh = mtod(chk->data, struct sctp_data_chunk *); |
| 9253 | dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY; |
| 9254 | } |
| 9255 | if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) || |
| 9256 | ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) { |
| 9257 | /* ok we will add this one */ |
| 9258 |
| 9259 | /* |
| 9260 | * Add an AUTH chunk, if chunk |
| 9261 | * requires it, save the offset into |
| 9262 | * the chain for AUTH |
| 9263 | */ |
| 9264 | if (data_auth_reqd) { |
| 9265 | if (auth == NULL) { |
| 9266 | outchain = sctp_add_auth_chunk(outchain, |
| 9267 | &endoutchain, |
| 9268 | &auth, |
| 9269 | &auth_offset, |
| 9270 | stcb, |
| 9271 | SCTP_DATA); |
| 9272 | auth_keyid = chk->auth_keyid; |
| 9273 | override_ok = 0; |
| 9274 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 9275 | } else if (override_ok) { |
| 9276 | /* use this data's keyid */ |
| 9277 | auth_keyid = chk->auth_keyid; |
| 9278 | override_ok = 0; |
| 9279 | } else if (auth_keyid != chk->auth_keyid) { |
| 9280 | /* different keyid, so done bundling */ |
| 9281 | break; |
| 9282 | } |
| 9283 | } |
| 9284 | outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0, |
| 9285 | chk->send_size, chk->copy_by_ref); |
| 9286 | if (outchain == NULL) { |
| 9287 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n"); |
| 9288 | if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { |
| 9289 | sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); |
| 9290 | } |
| 9291 | *reason_code = 3; |
| 9292 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 9293 | return (ENOMEM); |
| 9294 | } |
| 9295 | /* upate our MTU size */ |
| 9296 | /* Do clear IP_DF ? */ |
| 9297 | if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) { |
| 9298 | no_fragmentflg = 0; |
| 9299 | } |
| 9300 | /* unsigned subtraction of mtu */ |
| 9301 | if (mtu > chk->send_size) |
| 9302 | mtu -= chk->send_size; |
| 9303 | else |
| 9304 | mtu = 0; |
| 9305 | /* unsigned subtraction of r_mtu */ |
| 9306 | if (r_mtu > chk->send_size) |
| 9307 | r_mtu -= chk->send_size; |
| 9308 | else |
| 9309 | r_mtu = 0; |
| 9310 |
| 9311 | to_out += chk->send_size; |
| 9312 | if ((to_out > mx_mtu) && no_fragmentflg) { |
| 9313 | #ifdef INVARIANTS |
| 9314 | panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out); |
| 9315 | #else |
| 9316 | SCTP_PRINTF("Exceeding mtu of %d out size is %d\n", |
| 9317 | mx_mtu, to_out); |
| 9318 | #endif |
| 9319 | } |
| 9320 | chk->window_probe = 0; |
| 9321 | data_list[bundle_at++] = chk; |
| 9322 | if (bundle_at >= SCTP_MAX_DATA_BUNDLING) { |
| 9323 | break; |
| 9324 | } |
| 9325 | if (chk->sent == SCTP_DATAGRAM_UNSENT) { |
| 9326 | if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) { |
| 9327 | SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks); |
| 9328 | } else { |
| 9329 | SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks); |
| 9330 | } |
| 9331 | if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) && |
| 9332 | ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0)) |
| 9333 | /* Count number of user msg's that were fragmented |
| 9334 | * we do this by counting when we see a LAST fragment |
| 9335 | * only. |
| 9336 | */ |
| 9337 | SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs); |
| 9338 | } |
| 9339 | if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) { |
| 9340 | if ((one_chunk) && (stcb->asoc.total_flight == 0)) { |
| 9341 | data_list[0]->window_probe = 1; |
| 9342 | net->window_probe = 1; |
| 9343 | } |
| 9344 | break; |
| 9345 | } |
| 9346 | } else { |
| 9347 | /* |
| 9348 | * Must be sent in order of the |
| 9349 | * TSN's (on a network) |
| 9350 | */ |
| 9351 | break; |
| 9352 | } |
| 9353 | } /* for (chunk gather loop for this net) */ |
| 9354 | } /* if asoc.state OPEN */ |
| 9355 | no_data_fill: |
| 9356 | /* Is there something to send for this destination? */ |
| 9357 | if (outchain) { |
| 9358 | /* We may need to start a control timer or two */ |
| 9359 | if (asconf) { |
| 9360 | sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, |
| 9361 | stcb, net); |
| 9362 | /* |
| 9363 | * do NOT clear the asconf flag as it is used |
| 9364 | * to do appropriate source address selection. |
| 9365 | */ |
| 9366 | } |
| 9367 | if (cookie) { |
| 9368 | sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net); |
| 9369 | cookie = 0; |
| 9370 | } |
| 9371 | /* must start a send timer if data is being sent */ |
| 9372 | if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) { |
| 9373 | /* |
| 9374 | * no timer running on this destination |
| 9375 | * restart it. |
| 9376 | */ |
| 9377 | sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); |
| 9378 | } |
| 9379 | if (bundle_at || hbflag) { |
| 9380 | /* For data/asconf and hb set time */ |
| 9381 | if (*now_filled == 0) { |
| 9382 | (void)SCTP_GETTIME_TIMEVAL(now); |
| 9383 | *now_filled = 1; |
| 9384 | } |
| 9385 | net->last_sent_time = *now; |
| 9386 | } |
| 9387 | /* Now send it, if there is anything to send :> */ |
| 9388 | if ((error = sctp_lowlevel_chunk_output(inp, |
| 9389 | stcb, |
| 9390 | net, |
| 9391 | (struct sockaddr *)&net->ro._l_addr, |
| 9392 | outchain, |
| 9393 | auth_offset, |
| 9394 | auth, |
| 9395 | auth_keyid, |
| 9396 | no_fragmentflg, |
| 9397 | bundle_at, |
| 9398 | asconf, |
| 9399 | inp->sctp_lport, stcb->rport, |
| 9400 | htonl(stcb->asoc.peer_vtag), |
| 9401 | net->port, NULL, |
| 9402 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 9403 | 0, 0, |
| 9404 | #endif |
| 9405 | so_locked))) { |
| 9406 | /* error, we could not output */ |
| 9407 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error); |
| 9408 | if (from_where == 0) { |
| 9409 | SCTP_STAT_INCR(sctps_lowlevelerrusr); |
| 9410 | } |
| 9411 | if (error == ENOBUFS) { |
| 9412 | asoc->ifp_had_enobuf = 1; |
| 9413 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 9414 | } |
| 9415 | if (error == EHOSTUNREACH) { |
| 9416 | /* |
| 9417 | * Destination went unreachable |
| 9418 | * during this send |
| 9419 | */ |
| 9420 | sctp_move_chunks_from_net(stcb, net); |
| 9421 | } |
| 9422 | *reason_code = 6; |
| 9423 | /*- |
| 9424 | * I add this line to be paranoid. As far as |
| 9425 | * I can tell the continue, takes us back to |
| 9426 | * the top of the for, but just to make sure |
| 9427 | * I will reset these again here. |
| 9428 | */ |
| 9429 | ctl_cnt = 0; |
| 9430 | continue; /* This takes us back to the for() for the nets. */ |
| 9431 | } else { |
| 9432 | asoc->ifp_had_enobuf = 0; |
| 9433 | } |
| 9434 | endoutchain = NULL; |
| 9435 | auth = NULL; |
| 9436 | auth_offset = 0; |
| 9437 | if (!no_out_cnt) { |
| 9438 | *num_out += (ctl_cnt + bundle_at); |
| 9439 | } |
| 9440 | if (bundle_at) { |
| 9441 | /* setup for a RTO measurement */ |
| 9442 | tsns_sent = data_list[0]->rec.data.tsn; |
| 9443 | /* fill time if not already filled */ |
| 9444 | if (*now_filled == 0) { |
| 9445 | (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent); |
| 9446 | *now_filled = 1; |
| 9447 | *now = asoc->time_last_sent; |
| 9448 | } else { |
| 9449 | asoc->time_last_sent = *now; |
| 9450 | } |
| 9451 | if (net->rto_needed) { |
| 9452 | data_list[0]->do_rtt = 1; |
| 9453 | net->rto_needed = 0; |
| 9454 | } |
| 9455 | SCTP_STAT_INCR_BY(sctps_senddata, bundle_at); |
| 9456 | sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net); |
| 9457 | } |
| 9458 | if (one_chunk) { |
| 9459 | break; |
| 9460 | } |
| 9461 | } |
| 9462 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 9463 | sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND); |
| 9464 | } |
| 9465 | } |
| 9466 | if (old_start_at == NULL) { |
| 9467 | old_start_at = start_at; |
| 9468 | start_at = TAILQ_FIRST(&asoc->nets); |
| 9469 | if (old_start_at) |
| 9470 | goto again_one_more_time; |
| 9471 | } |
| 9472 |
| 9473 | /* |
| 9474 | * At the end there should be no NON timed chunks hanging on this |
| 9475 | * queue. |
| 9476 | */ |
| 9477 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 9478 | sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND); |
| 9479 | } |
| 9480 | if ((*num_out == 0) && (*reason_code == 0)) { |
| 9481 | *reason_code = 4; |
| 9482 | } else { |
| 9483 | *reason_code = 5; |
| 9484 | } |
| 9485 | sctp_clean_up_ctl(stcb, asoc, so_locked); |
| 9486 | return (0); |
| 9487 | } |
| 9488 |
| 9489 | void |
| 9490 | sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err) |
| 9491 | { |
| 9492 | /*- |
| 9493 | * Prepend a OPERATIONAL_ERROR chunk header and put on the end of |
| 9494 | * the control chunk queue. |
| 9495 | */ |
| 9496 | struct sctp_chunkhdr *hdr; |
| 9497 | struct sctp_tmit_chunk *chk; |
| 9498 | struct mbuf *mat, *last_mbuf; |
| 9499 | uint32_t chunk_length; |
| 9500 | uint16_t padding_length; |
| 9501 |
| 9502 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 9503 | SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_NOWAIT); |
| 9504 | if (op_err == NULL) { |
| 9505 | return; |
| 9506 | } |
| 9507 | last_mbuf = NULL; |
| 9508 | chunk_length = 0; |
| 9509 | for (mat = op_err; mat != NULL; mat = SCTP_BUF_NEXT(mat)) { |
| 9510 | chunk_length += SCTP_BUF_LEN(mat); |
| 9511 | if (SCTP_BUF_NEXT(mat) == NULL) { |
| 9512 | last_mbuf = mat; |
| 9513 | } |
| 9514 | } |
| 9515 | if (chunk_length > SCTP_MAX_CHUNK_LENGTH) { |
| 9516 | sctp_m_freem(op_err); |
| 9517 | return; |
| 9518 | } |
| 9519 | padding_length = chunk_length % 4; |
| 9520 | if (padding_length != 0) { |
| 9521 | padding_length = 4 - padding_length; |
| 9522 | } |
| 9523 | if (padding_length != 0) { |
| 9524 | if (sctp_add_pad_tombuf(last_mbuf, padding_length) == NULL) { |
| 9525 | sctp_m_freem(op_err); |
| 9526 | return; |
| 9527 | } |
| 9528 | } |
| 9529 | sctp_alloc_a_chunk(stcb, chk); |
| 9530 | if (chk == NULL) { |
| 9531 | /* no memory */ |
| 9532 | sctp_m_freem(op_err); |
| 9533 | return; |
| 9534 | } |
| 9535 | chk->copy_by_ref = 0; |
| 9536 | chk->rec.chunk_id.id = SCTP_OPERATION_ERROR; |
| 9537 | chk->rec.chunk_id.can_take_data = 0; |
| 9538 | chk->flags = 0; |
| 9539 | chk->send_size = (uint16_t)chunk_length; |
| 9540 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 9541 | chk->snd_count = 0; |
| 9542 | chk->asoc = &stcb->asoc; |
| 9543 | chk->data = op_err; |
| 9544 | chk->whoTo = NULL; |
| 9545 | hdr = mtod(op_err, struct sctp_chunkhdr *); |
| 9546 | hdr->chunk_type = SCTP_OPERATION_ERROR; |
| 9547 | hdr->chunk_flags = 0; |
| 9548 | hdr->chunk_length = htons(chk->send_size); |
| 9549 | TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); |
| 9550 | chk->asoc->ctrl_queue_cnt++; |
| 9551 | } |
| 9552 |
| 9553 | int |
| 9554 | sctp_send_cookie_echo(struct mbuf *m, |
| 9555 | int offset, int limit, |
| 9556 | struct sctp_tcb *stcb, |
| 9557 | struct sctp_nets *net) |
| 9558 | { |
| 9559 | /*- |
| 9560 | * pull out the cookie and put it at the front of the control chunk |
| 9561 | * queue. |
| 9562 | */ |
| 9563 | int at; |
| 9564 | struct mbuf *cookie; |
| 9565 | struct sctp_paramhdr param, *phdr; |
| 9566 | struct sctp_chunkhdr *hdr; |
| 9567 | struct sctp_tmit_chunk *chk; |
| 9568 | uint16_t ptype, plen; |
| 9569 |
| 9570 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 9571 | /* First find the cookie in the param area */ |
| 9572 | cookie = NULL; |
| 9573 | at = offset + sizeof(struct sctp_init_chunk); |
| 9574 | for (;;) { |
| 9575 | phdr = sctp_get_next_param(m, at, ¶m, sizeof(param)); |
| 9576 | if (phdr == NULL) { |
| 9577 | return (-3); |
| 9578 | } |
| 9579 | ptype = ntohs(phdr->param_type); |
| 9580 | plen = ntohs(phdr->param_length); |
| 9581 | if (plen < sizeof(struct sctp_paramhdr)) { |
| 9582 | return (-6); |
| 9583 | } |
| 9584 | if (ptype == SCTP_STATE_COOKIE) { |
| 9585 | int pad; |
| 9586 |
| 9587 | /* found the cookie */ |
| 9588 | if (at + plen > limit) { |
| 9589 | return (-7); |
| 9590 | } |
| 9591 | cookie = SCTP_M_COPYM(m, at, plen, M_NOWAIT); |
| 9592 | if (cookie == NULL) { |
| 9593 | /* No memory */ |
| 9594 | return (-2); |
| 9595 | } |
| 9596 | if ((pad = (plen % 4)) > 0) { |
| 9597 | pad = 4 - pad; |
| 9598 | } |
| 9599 | if (pad > 0) { |
| 9600 | if (sctp_pad_lastmbuf(cookie, pad, NULL) == NULL) { |
| 9601 | return (-8); |
| 9602 | } |
| 9603 | } |
| 9604 | #ifdef SCTP_MBUF_LOGGING |
| 9605 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { |
| 9606 | sctp_log_mbc(cookie, SCTP_MBUF_ICOPY); |
| 9607 | } |
| 9608 | #endif |
| 9609 | break; |
| 9610 | } |
| 9611 | at += SCTP_SIZE32(plen); |
| 9612 | } |
| 9613 | /* ok, we got the cookie lets change it into a cookie echo chunk */ |
| 9614 | /* first the change from param to cookie */ |
| 9615 | hdr = mtod(cookie, struct sctp_chunkhdr *); |
| 9616 | hdr->chunk_type = SCTP_COOKIE_ECHO; |
| 9617 | hdr->chunk_flags = 0; |
| 9618 | /* get the chunk stuff now and place it in the FRONT of the queue */ |
| 9619 | sctp_alloc_a_chunk(stcb, chk); |
| 9620 | if (chk == NULL) { |
| 9621 | /* no memory */ |
| 9622 | sctp_m_freem(cookie); |
| 9623 | return (-5); |
| 9624 | } |
| 9625 | chk->copy_by_ref = 0; |
| 9626 | chk->rec.chunk_id.id = SCTP_COOKIE_ECHO; |
| 9627 | chk->rec.chunk_id.can_take_data = 0; |
| 9628 | chk->flags = CHUNK_FLAGS_FRAGMENT_OK; |
| 9629 | chk->send_size = SCTP_SIZE32(plen); |
| 9630 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 9631 | chk->snd_count = 0; |
| 9632 | chk->asoc = &stcb->asoc; |
| 9633 | chk->data = cookie; |
| 9634 | chk->whoTo = net; |
| 9635 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 9636 | TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next); |
| 9637 | chk->asoc->ctrl_queue_cnt++; |
| 9638 | return (0); |
| 9639 | } |
| 9640 |
| 9641 | void |
| 9642 | sctp_send_heartbeat_ack(struct sctp_tcb *stcb, |
| 9643 | struct mbuf *m, |
| 9644 | int offset, |
| 9645 | int chk_length, |
| 9646 | struct sctp_nets *net) |
| 9647 | { |
| 9648 | /* |
| 9649 | * take a HB request and make it into a HB ack and send it. |
| 9650 | */ |
| 9651 | struct mbuf *outchain; |
| 9652 | struct sctp_chunkhdr *chdr; |
| 9653 | struct sctp_tmit_chunk *chk; |
| 9654 |
| 9655 | if (net == NULL) |
| 9656 | /* must have a net pointer */ |
| 9657 | return; |
| 9658 |
| 9659 | outchain = SCTP_M_COPYM(m, offset, chk_length, M_NOWAIT); |
| 9660 | if (outchain == NULL) { |
| 9661 | /* gak out of memory */ |
| 9662 | return; |
| 9663 | } |
| 9664 | #ifdef SCTP_MBUF_LOGGING |
| 9665 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { |
| 9666 | sctp_log_mbc(outchain, SCTP_MBUF_ICOPY); |
| 9667 | } |
| 9668 | #endif |
| 9669 | chdr = mtod(outchain, struct sctp_chunkhdr *); |
| 9670 | chdr->chunk_type = SCTP_HEARTBEAT_ACK; |
| 9671 | chdr->chunk_flags = 0; |
| 9672 | if (chk_length % 4 != 0) { |
| 9673 | sctp_pad_lastmbuf(outchain, 4 - (chk_length % 4), NULL); |
| 9674 | } |
| 9675 | sctp_alloc_a_chunk(stcb, chk); |
| 9676 | if (chk == NULL) { |
| 9677 | /* no memory */ |
| 9678 | sctp_m_freem(outchain); |
| 9679 | return; |
| 9680 | } |
| 9681 | chk->copy_by_ref = 0; |
| 9682 | chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK; |
| 9683 | chk->rec.chunk_id.can_take_data = 1; |
| 9684 | chk->flags = 0; |
| 9685 | chk->send_size = chk_length; |
| 9686 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 9687 | chk->snd_count = 0; |
| 9688 | chk->asoc = &stcb->asoc; |
| 9689 | chk->data = outchain; |
| 9690 | chk->whoTo = net; |
| 9691 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 9692 | TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); |
| 9693 | chk->asoc->ctrl_queue_cnt++; |
| 9694 | } |
| 9695 |
| 9696 | void |
| 9697 | sctp_send_cookie_ack(struct sctp_tcb *stcb) |
| 9698 | { |
| 9699 | /* formulate and queue a cookie-ack back to sender */ |
| 9700 | struct mbuf *cookie_ack; |
| 9701 | struct sctp_chunkhdr *hdr; |
| 9702 | struct sctp_tmit_chunk *chk; |
| 9703 |
| 9704 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 9705 |
| 9706 | cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER); |
| 9707 | if (cookie_ack == NULL) { |
| 9708 | /* no mbuf's */ |
| 9709 | return; |
| 9710 | } |
| 9711 | SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD); |
| 9712 | sctp_alloc_a_chunk(stcb, chk); |
| 9713 | if (chk == NULL) { |
| 9714 | /* no memory */ |
| 9715 | sctp_m_freem(cookie_ack); |
| 9716 | return; |
| 9717 | } |
| 9718 | chk->copy_by_ref = 0; |
| 9719 | chk->rec.chunk_id.id = SCTP_COOKIE_ACK; |
| 9720 | chk->rec.chunk_id.can_take_data = 1; |
| 9721 | chk->flags = 0; |
| 9722 | chk->send_size = sizeof(struct sctp_chunkhdr); |
| 9723 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 9724 | chk->snd_count = 0; |
| 9725 | chk->asoc = &stcb->asoc; |
| 9726 | chk->data = cookie_ack; |
| 9727 | if (chk->asoc->last_control_chunk_from != NULL) { |
| 9728 | chk->whoTo = chk->asoc->last_control_chunk_from; |
| 9729 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 9730 | } else { |
| 9731 | chk->whoTo = NULL; |
| 9732 | } |
| 9733 | hdr = mtod(cookie_ack, struct sctp_chunkhdr *); |
| 9734 | hdr->chunk_type = SCTP_COOKIE_ACK; |
| 9735 | hdr->chunk_flags = 0; |
| 9736 | hdr->chunk_length = htons(chk->send_size); |
| 9737 | SCTP_BUF_LEN(cookie_ack) = chk->send_size; |
| 9738 | TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); |
| 9739 | chk->asoc->ctrl_queue_cnt++; |
| 9740 | return; |
| 9741 | } |
| 9742 |
| 9743 | void |
| 9744 | sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net) |
| 9745 | { |
| 9746 | /* formulate and queue a SHUTDOWN-ACK back to the sender */ |
| 9747 | struct mbuf *m_shutdown_ack; |
| 9748 | struct sctp_shutdown_ack_chunk *ack_cp; |
| 9749 | struct sctp_tmit_chunk *chk; |
| 9750 |
| 9751 | m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_NOWAIT, 1, MT_HEADER); |
| 9752 | if (m_shutdown_ack == NULL) { |
| 9753 | /* no mbuf's */ |
| 9754 | return; |
| 9755 | } |
| 9756 | SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD); |
| 9757 | sctp_alloc_a_chunk(stcb, chk); |
| 9758 | if (chk == NULL) { |
| 9759 | /* no memory */ |
| 9760 | sctp_m_freem(m_shutdown_ack); |
| 9761 | return; |
| 9762 | } |
| 9763 | chk->copy_by_ref = 0; |
| 9764 | chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK; |
| 9765 | chk->rec.chunk_id.can_take_data = 1; |
| 9766 | chk->flags = 0; |
| 9767 | chk->send_size = sizeof(struct sctp_chunkhdr); |
| 9768 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 9769 | chk->snd_count = 0; |
| 9770 | chk->asoc = &stcb->asoc; |
| 9771 | chk->data = m_shutdown_ack; |
| 9772 | chk->whoTo = net; |
| 9773 | if (chk->whoTo) { |
| 9774 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 9775 | } |
| 9776 | ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *); |
| 9777 | ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK; |
| 9778 | ack_cp->ch.chunk_flags = 0; |
| 9779 | ack_cp->ch.chunk_length = htons(chk->send_size); |
| 9780 | SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size; |
| 9781 | TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); |
| 9782 | chk->asoc->ctrl_queue_cnt++; |
| 9783 | return; |
| 9784 | } |
| 9785 |
| 9786 | void |
| 9787 | sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net) |
| 9788 | { |
| 9789 | /* formulate and queue a SHUTDOWN to the sender */ |
| 9790 | struct mbuf *m_shutdown; |
| 9791 | struct sctp_shutdown_chunk *shutdown_cp; |
| 9792 | struct sctp_tmit_chunk *chk; |
| 9793 |
| 9794 | TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { |
| 9795 | if (chk->rec.chunk_id.id == SCTP_SHUTDOWN) { |
| 9796 | /* We already have a SHUTDOWN queued. Reuse it. */ |
| 9797 | if (chk->whoTo) { |
| 9798 | sctp_free_remote_addr(chk->whoTo); |
| 9799 | chk->whoTo = NULL; |
| 9800 | } |
| 9801 | break; |
| 9802 | } |
| 9803 | } |
| 9804 | if (chk == NULL) { |
| 9805 | m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_NOWAIT, 1, MT_HEADER); |
| 9806 | if (m_shutdown == NULL) { |
| 9807 | /* no mbuf's */ |
| 9808 | return; |
| 9809 | } |
| 9810 | SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD); |
| 9811 | sctp_alloc_a_chunk(stcb, chk); |
| 9812 | if (chk == NULL) { |
| 9813 | /* no memory */ |
| 9814 | sctp_m_freem(m_shutdown); |
| 9815 | return; |
| 9816 | } |
| 9817 | chk->copy_by_ref = 0; |
| 9818 | chk->rec.chunk_id.id = SCTP_SHUTDOWN; |
| 9819 | chk->rec.chunk_id.can_take_data = 1; |
| 9820 | chk->flags = 0; |
| 9821 | chk->send_size = sizeof(struct sctp_shutdown_chunk); |
| 9822 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 9823 | chk->snd_count = 0; |
| 9824 | chk->asoc = &stcb->asoc; |
| 9825 | chk->data = m_shutdown; |
| 9826 | chk->whoTo = net; |
| 9827 | if (chk->whoTo) { |
| 9828 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 9829 | } |
| 9830 | shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *); |
| 9831 | shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN; |
| 9832 | shutdown_cp->ch.chunk_flags = 0; |
| 9833 | shutdown_cp->ch.chunk_length = htons(chk->send_size); |
| 9834 | shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn); |
| 9835 | SCTP_BUF_LEN(m_shutdown) = chk->send_size; |
| 9836 | TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); |
| 9837 | chk->asoc->ctrl_queue_cnt++; |
| 9838 | } else { |
| 9839 | TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, sctp_next); |
| 9840 | chk->whoTo = net; |
| 9841 | if (chk->whoTo) { |
| 9842 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 9843 | } |
| 9844 | shutdown_cp = mtod(chk->data, struct sctp_shutdown_chunk *); |
| 9845 | shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn); |
| 9846 | TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next); |
| 9847 | } |
| 9848 | return; |
| 9849 | } |
| 9850 |
| 9851 | void |
| 9852 | sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked) |
| 9853 | { |
| 9854 | /* |
| 9855 | * formulate and queue an ASCONF to the peer. |
| 9856 | * ASCONF parameters should be queued on the assoc queue. |
| 9857 | */ |
| 9858 | struct sctp_tmit_chunk *chk; |
| 9859 | struct mbuf *m_asconf; |
| 9860 | int len; |
| 9861 |
| 9862 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 9863 |
| 9864 | if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) && |
| 9865 | (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) { |
| 9866 | /* can't send a new one if there is one in flight already */ |
| 9867 | return; |
| 9868 | } |
| 9869 |
| 9870 | /* compose an ASCONF chunk, maximum length is PMTU */ |
| 9871 | m_asconf = sctp_compose_asconf(stcb, &len, addr_locked); |
| 9872 | if (m_asconf == NULL) { |
| 9873 | return; |
| 9874 | } |
| 9875 |
| 9876 | sctp_alloc_a_chunk(stcb, chk); |
| 9877 | if (chk == NULL) { |
| 9878 | /* no memory */ |
| 9879 | sctp_m_freem(m_asconf); |
| 9880 | return; |
| 9881 | } |
| 9882 |
| 9883 | chk->copy_by_ref = 0; |
| 9884 | chk->rec.chunk_id.id = SCTP_ASCONF; |
| 9885 | chk->rec.chunk_id.can_take_data = 0; |
| 9886 | chk->flags = CHUNK_FLAGS_FRAGMENT_OK; |
| 9887 | chk->data = m_asconf; |
| 9888 | chk->send_size = len; |
| 9889 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 9890 | chk->snd_count = 0; |
| 9891 | chk->asoc = &stcb->asoc; |
| 9892 | chk->whoTo = net; |
| 9893 | if (chk->whoTo) { |
| 9894 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 9895 | } |
| 9896 | TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next); |
| 9897 | chk->asoc->ctrl_queue_cnt++; |
| 9898 | return; |
| 9899 | } |
| 9900 |
| 9901 | void |
| 9902 | sctp_send_asconf_ack(struct sctp_tcb *stcb) |
| 9903 | { |
| 9904 | /* |
| 9905 | * formulate and queue a asconf-ack back to sender. |
| 9906 | * the asconf-ack must be stored in the tcb. |
| 9907 | */ |
| 9908 | struct sctp_tmit_chunk *chk; |
| 9909 | struct sctp_asconf_ack *ack, *latest_ack; |
| 9910 | struct mbuf *m_ack; |
| 9911 | struct sctp_nets *net = NULL; |
| 9912 |
| 9913 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 9914 | /* Get the latest ASCONF-ACK */ |
| 9915 | latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead); |
| 9916 | if (latest_ack == NULL) { |
| 9917 | return; |
| 9918 | } |
| 9919 | if (latest_ack->last_sent_to != NULL && |
| 9920 | latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) { |
| 9921 | /* we're doing a retransmission */ |
| 9922 | net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0); |
| 9923 | if (net == NULL) { |
| 9924 | /* no alternate */ |
| 9925 | if (stcb->asoc.last_control_chunk_from == NULL) { |
| 9926 | if (stcb->asoc.alternate) { |
| 9927 | net = stcb->asoc.alternate; |
| 9928 | } else { |
| 9929 | net = stcb->asoc.primary_destination; |
| 9930 | } |
| 9931 | } else { |
| 9932 | net = stcb->asoc.last_control_chunk_from; |
| 9933 | } |
| 9934 | } |
| 9935 | } else { |
| 9936 | /* normal case */ |
| 9937 | if (stcb->asoc.last_control_chunk_from == NULL) { |
| 9938 | if (stcb->asoc.alternate) { |
| 9939 | net = stcb->asoc.alternate; |
| 9940 | } else { |
| 9941 | net = stcb->asoc.primary_destination; |
| 9942 | } |
| 9943 | } else { |
| 9944 | net = stcb->asoc.last_control_chunk_from; |
| 9945 | } |
| 9946 | } |
| 9947 | latest_ack->last_sent_to = net; |
| 9948 |
| 9949 | TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) { |
| 9950 | if (ack->data == NULL) { |
| 9951 | continue; |
| 9952 | } |
| 9953 |
| 9954 | /* copy the asconf_ack */ |
| 9955 | m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_NOWAIT); |
| 9956 | if (m_ack == NULL) { |
| 9957 | /* couldn't copy it */ |
| 9958 | return; |
| 9959 | } |
| 9960 | #ifdef SCTP_MBUF_LOGGING |
| 9961 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { |
| 9962 | sctp_log_mbc(m_ack, SCTP_MBUF_ICOPY); |
| 9963 | } |
| 9964 | #endif |
| 9965 |
| 9966 | sctp_alloc_a_chunk(stcb, chk); |
| 9967 | if (chk == NULL) { |
| 9968 | /* no memory */ |
| 9969 | if (m_ack) |
| 9970 | sctp_m_freem(m_ack); |
| 9971 | return; |
| 9972 | } |
| 9973 | chk->copy_by_ref = 0; |
| 9974 | chk->rec.chunk_id.id = SCTP_ASCONF_ACK; |
| 9975 | chk->rec.chunk_id.can_take_data = 1; |
| 9976 | chk->flags = CHUNK_FLAGS_FRAGMENT_OK; |
| 9977 | chk->whoTo = net; |
| 9978 | if (chk->whoTo) { |
| 9979 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 9980 | } |
| 9981 | chk->data = m_ack; |
| 9982 | chk->send_size = ack->len; |
| 9983 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 9984 | chk->snd_count = 0; |
| 9985 | chk->asoc = &stcb->asoc; |
| 9986 |
| 9987 | TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next); |
| 9988 | chk->asoc->ctrl_queue_cnt++; |
| 9989 | } |
| 9990 | return; |
| 9991 | } |
| 9992 |
| 9993 | static int |
| 9994 | sctp_chunk_retransmission(struct sctp_inpcb *inp, |
| 9995 | struct sctp_tcb *stcb, |
| 9996 | struct sctp_association *asoc, |
| 9997 | int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked) |
| 9998 | { |
| 9999 | /*- |
| 10000 | * send out one MTU of retransmission. If fast_retransmit is |
| 10001 | * happening we ignore the cwnd. Otherwise we obey the cwnd and |
| 10002 | * rwnd. For a Cookie or Asconf in the control chunk queue we |
| 10003 | * retransmit them by themselves. |
| 10004 | * |
| 10005 | * For data chunks we will pick out the lowest TSN's in the sent_queue |
| 10006 | * marked for resend and bundle them all together (up to a MTU of |
| 10007 | * destination). The address to send to should have been |
| 10008 | * selected/changed where the retransmission was marked (i.e. in FR |
| 10009 | * or t3-timeout routines). |
| 10010 | */ |
| 10011 | struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING]; |
| 10012 | struct sctp_tmit_chunk *chk, *fwd; |
| 10013 | struct mbuf *m, *endofchain; |
| 10014 | struct sctp_nets *net = NULL; |
| 10015 | uint32_t tsns_sent = 0; |
| 10016 | int no_fragmentflg, bundle_at, cnt_thru; |
| 10017 | unsigned int mtu; |
| 10018 | int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started; |
| 10019 | struct sctp_auth_chunk *auth = NULL; |
| 10020 | uint32_t auth_offset = 0; |
| 10021 | uint16_t auth_keyid; |
| 10022 | int override_ok = 1; |
| 10023 | int data_auth_reqd = 0; |
| 10024 | uint32_t dmtu = 0; |
| 10025 |
| 10026 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 10027 | if (so_locked) { |
| 10028 | sctp_lock_assert(SCTP_INP_SO(inp)); |
| 10029 | } else { |
| 10030 | sctp_unlock_assert(SCTP_INP_SO(inp)); |
| 10031 | } |
| 10032 | #endif |
| 10033 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 10034 | tmr_started = ctl_cnt = 0; |
| 10035 | no_fragmentflg = 1; |
| 10036 | fwd_tsn = 0; |
| 10037 | *cnt_out = 0; |
| 10038 | fwd = NULL; |
| 10039 | endofchain = m = NULL; |
| 10040 | auth_keyid = stcb->asoc.authinfo.active_keyid; |
| 10041 | #ifdef SCTP_AUDITING_ENABLED |
| 10042 | sctp_audit_log(0xC3, 1); |
| 10043 | #endif |
| 10044 | if ((TAILQ_EMPTY(&asoc->sent_queue)) && |
| 10045 | (TAILQ_EMPTY(&asoc->control_send_queue))) { |
| 10046 | SCTPDBG(SCTP_DEBUG_OUTPUT1,"SCTP hits empty queue with cnt set to %d?\n", |
| 10047 | asoc->sent_queue_retran_cnt); |
| 10048 | asoc->sent_queue_cnt = 0; |
| 10049 | asoc->sent_queue_cnt_removeable = 0; |
| 10050 | /* send back 0/0 so we enter normal transmission */ |
| 10051 | *cnt_out = 0; |
| 10052 | return (0); |
| 10053 | } |
| 10054 | TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { |
| 10055 | if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) || |
| 10056 | (chk->rec.chunk_id.id == SCTP_STREAM_RESET) || |
| 10057 | (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) { |
| 10058 | if (chk->sent != SCTP_DATAGRAM_RESEND) { |
| 10059 | continue; |
| 10060 | } |
| 10061 | if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) { |
| 10062 | if (chk != asoc->str_reset) { |
| 10063 | /* |
| 10064 | * not eligible for retran if its |
| 10065 | * not ours |
| 10066 | */ |
| 10067 | continue; |
| 10068 | } |
| 10069 | } |
| 10070 | ctl_cnt++; |
| 10071 | if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) { |
| 10072 | fwd_tsn = 1; |
| 10073 | } |
| 10074 | /* |
| 10075 | * Add an AUTH chunk, if chunk requires it save the |
| 10076 | * offset into the chain for AUTH |
| 10077 | */ |
| 10078 | if ((auth == NULL) && |
| 10079 | (sctp_auth_is_required_chunk(chk->rec.chunk_id.id, |
| 10080 | stcb->asoc.peer_auth_chunks))) { |
| 10081 | m = sctp_add_auth_chunk(m, &endofchain, |
| 10082 | &auth, &auth_offset, |
| 10083 | stcb, |
| 10084 | chk->rec.chunk_id.id); |
| 10085 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 10086 | } |
| 10087 | m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref); |
| 10088 | break; |
| 10089 | } |
| 10090 | } |
| 10091 | one_chunk = 0; |
| 10092 | cnt_thru = 0; |
| 10093 | /* do we have control chunks to retransmit? */ |
| 10094 | if (m != NULL) { |
| 10095 | /* Start a timer no matter if we succeed or fail */ |
| 10096 | if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) { |
| 10097 | sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo); |
| 10098 | } else if (chk->rec.chunk_id.id == SCTP_ASCONF) |
| 10099 | sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo); |
| 10100 | chk->snd_count++; /* update our count */ |
| 10101 | if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo, |
| 10102 | (struct sockaddr *)&chk->whoTo->ro._l_addr, m, |
| 10103 | auth_offset, auth, stcb->asoc.authinfo.active_keyid, |
| 10104 | no_fragmentflg, 0, 0, |
| 10105 | inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag), |
| 10106 | chk->whoTo->port, NULL, |
| 10107 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 10108 | 0, 0, |
| 10109 | #endif |
| 10110 | so_locked))) { |
| 10111 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error); |
| 10112 | if (error == ENOBUFS) { |
| 10113 | asoc->ifp_had_enobuf = 1; |
| 10114 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 10115 | } |
| 10116 | return (error); |
| 10117 | } else { |
| 10118 | asoc->ifp_had_enobuf = 0; |
| 10119 | } |
| 10120 | endofchain = NULL; |
| 10121 | auth = NULL; |
| 10122 | auth_offset = 0; |
| 10123 | /* |
| 10124 | * We don't want to mark the net->sent time here since this |
| 10125 | * we use this for HB and retrans cannot measure RTT |
| 10126 | */ |
| 10127 | /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */ |
| 10128 | *cnt_out += 1; |
| 10129 | chk->sent = SCTP_DATAGRAM_SENT; |
| 10130 | sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt); |
| 10131 | if (fwd_tsn == 0) { |
| 10132 | return (0); |
| 10133 | } else { |
| 10134 | /* Clean up the fwd-tsn list */ |
| 10135 | sctp_clean_up_ctl(stcb, asoc, so_locked); |
| 10136 | return (0); |
| 10137 | } |
| 10138 | } |
| 10139 | /* |
| 10140 | * Ok, it is just data retransmission we need to do or that and a |
| 10141 | * fwd-tsn with it all. |
| 10142 | */ |
| 10143 | if (TAILQ_EMPTY(&asoc->sent_queue)) { |
| 10144 | return (SCTP_RETRAN_DONE); |
| 10145 | } |
| 10146 | if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) || |
| 10147 | (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT)) { |
| 10148 | /* not yet open, resend the cookie and that is it */ |
| 10149 | return (1); |
| 10150 | } |
| 10151 | #ifdef SCTP_AUDITING_ENABLED |
| 10152 | sctp_auditing(20, inp, stcb, NULL); |
| 10153 | #endif |
| 10154 | data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks); |
| 10155 | TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) { |
| 10156 | if (chk->sent != SCTP_DATAGRAM_RESEND) { |
| 10157 | /* No, not sent to this net or not ready for rtx */ |
| 10158 | continue; |
| 10159 | } |
| 10160 | if (chk->data == NULL) { |
| 10161 | SCTP_PRINTF("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n", |
| 10162 | chk->rec.data.tsn, chk->snd_count, chk->sent); |
| 10163 | continue; |
| 10164 | } |
| 10165 | if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) && |
| 10166 | (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) { |
| 10167 | struct mbuf *op_err; |
| 10168 | char msg[SCTP_DIAG_INFO_LEN]; |
| 10169 |
| 10170 | SCTP_SNPRINTF(msg, sizeof(msg), "TSN %8.8x retransmitted %d times, giving up", |
| 10171 | chk->rec.data.tsn, chk->snd_count); |
| 10172 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 10173 | msg); |
| 10174 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 10175 | sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, |
| 10176 | false, so_locked); |
| 10177 | SCTP_TCB_LOCK(stcb); |
| 10178 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
| 10179 | return (SCTP_RETRAN_EXIT); |
| 10180 | } |
| 10181 | /* pick up the net */ |
| 10182 | net = chk->whoTo; |
| 10183 | switch (net->ro._l_addr.sa.sa_family) { |
| 10184 | #ifdef INET |
| 10185 | case AF_INET: |
| 10186 | mtu = net->mtu - SCTP_MIN_V4_OVERHEAD; |
| 10187 | break; |
| 10188 | #endif |
| 10189 | #ifdef INET6 |
| 10190 | case AF_INET6: |
| 10191 | mtu = net->mtu - SCTP_MIN_OVERHEAD; |
| 10192 | break; |
| 10193 | #endif |
| 10194 | #if defined(__Userspace__) |
| 10195 | case AF_CONN: |
| 10196 | mtu = net->mtu - sizeof(struct sctphdr); |
| 10197 | break; |
| 10198 | #endif |
| 10199 | default: |
| 10200 | /* TSNH */ |
| 10201 | mtu = net->mtu; |
| 10202 | break; |
| 10203 | } |
| 10204 |
| 10205 | if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) { |
| 10206 | /* No room in peers rwnd */ |
| 10207 | uint32_t tsn; |
| 10208 |
| 10209 | tsn = asoc->last_acked_seq + 1; |
| 10210 | if (tsn == chk->rec.data.tsn) { |
| 10211 | /* |
| 10212 | * we make a special exception for this |
| 10213 | * case. The peer has no rwnd but is missing |
| 10214 | * the lowest chunk.. which is probably what |
| 10215 | * is holding up the rwnd. |
| 10216 | */ |
| 10217 | goto one_chunk_around; |
| 10218 | } |
| 10219 | return (1); |
| 10220 | } |
| 10221 | one_chunk_around: |
| 10222 | if (asoc->peers_rwnd < mtu) { |
| 10223 | one_chunk = 1; |
| 10224 | if ((asoc->peers_rwnd == 0) && |
| 10225 | (asoc->total_flight == 0)) { |
| 10226 | chk->window_probe = 1; |
| 10227 | chk->whoTo->window_probe = 1; |
| 10228 | } |
| 10229 | } |
| 10230 | #ifdef SCTP_AUDITING_ENABLED |
| 10231 | sctp_audit_log(0xC3, 2); |
| 10232 | #endif |
| 10233 | bundle_at = 0; |
| 10234 | m = NULL; |
| 10235 | net->fast_retran_ip = 0; |
| 10236 | if (chk->rec.data.doing_fast_retransmit == 0) { |
| 10237 | /* |
| 10238 | * if no FR in progress skip destination that have |
| 10239 | * flight_size > cwnd. |
| 10240 | */ |
| 10241 | if (net->flight_size >= net->cwnd) { |
| 10242 | continue; |
| 10243 | } |
| 10244 | } else { |
| 10245 | /* |
| 10246 | * Mark the destination net to have FR recovery |
| 10247 | * limits put on it. |
| 10248 | */ |
| 10249 | *fr_done = 1; |
| 10250 | net->fast_retran_ip = 1; |
| 10251 | } |
| 10252 |
| 10253 | /* |
| 10254 | * if no AUTH is yet included and this chunk requires it, |
| 10255 | * make sure to account for it. We don't apply the size |
| 10256 | * until the AUTH chunk is actually added below in case |
| 10257 | * there is no room for this chunk. |
| 10258 | */ |
| 10259 | if (data_auth_reqd && (auth == NULL)) { |
| 10260 | dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); |
| 10261 | } else |
| 10262 | dmtu = 0; |
| 10263 |
| 10264 | if ((chk->send_size <= (mtu - dmtu)) || |
| 10265 | (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) { |
| 10266 | /* ok we will add this one */ |
| 10267 | if (data_auth_reqd) { |
| 10268 | if (auth == NULL) { |
| 10269 | m = sctp_add_auth_chunk(m, |
| 10270 | &endofchain, |
| 10271 | &auth, |
| 10272 | &auth_offset, |
| 10273 | stcb, |
| 10274 | SCTP_DATA); |
| 10275 | auth_keyid = chk->auth_keyid; |
| 10276 | override_ok = 0; |
| 10277 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 10278 | } else if (override_ok) { |
| 10279 | auth_keyid = chk->auth_keyid; |
| 10280 | override_ok = 0; |
| 10281 | } else if (chk->auth_keyid != auth_keyid) { |
| 10282 | /* different keyid, so done bundling */ |
| 10283 | break; |
| 10284 | } |
| 10285 | } |
| 10286 | m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref); |
| 10287 | if (m == NULL) { |
| 10288 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 10289 | return (ENOMEM); |
| 10290 | } |
| 10291 | /* Do clear IP_DF ? */ |
| 10292 | if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) { |
| 10293 | no_fragmentflg = 0; |
| 10294 | } |
| 10295 | /* upate our MTU size */ |
| 10296 | if (mtu > (chk->send_size + dmtu)) |
| 10297 | mtu -= (chk->send_size + dmtu); |
| 10298 | else |
| 10299 | mtu = 0; |
| 10300 | data_list[bundle_at++] = chk; |
| 10301 | if (one_chunk && (asoc->total_flight <= 0)) { |
| 10302 | SCTP_STAT_INCR(sctps_windowprobed); |
| 10303 | } |
| 10304 | } |
| 10305 | if (one_chunk == 0) { |
| 10306 | /* |
| 10307 | * now are there anymore forward from chk to pick |
| 10308 | * up? |
| 10309 | */ |
| 10310 | for (fwd = TAILQ_NEXT(chk, sctp_next); fwd != NULL; fwd = TAILQ_NEXT(fwd, sctp_next)) { |
| 10311 | if (fwd->sent != SCTP_DATAGRAM_RESEND) { |
| 10312 | /* Nope, not for retran */ |
| 10313 | continue; |
| 10314 | } |
| 10315 | if (fwd->whoTo != net) { |
| 10316 | /* Nope, not the net in question */ |
| 10317 | continue; |
| 10318 | } |
| 10319 | if (data_auth_reqd && (auth == NULL)) { |
| 10320 | dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); |
| 10321 | } else |
| 10322 | dmtu = 0; |
| 10323 | if (fwd->send_size <= (mtu - dmtu)) { |
| 10324 | if (data_auth_reqd) { |
| 10325 | if (auth == NULL) { |
| 10326 | m = sctp_add_auth_chunk(m, |
| 10327 | &endofchain, |
| 10328 | &auth, |
| 10329 | &auth_offset, |
| 10330 | stcb, |
| 10331 | SCTP_DATA); |
| 10332 | auth_keyid = fwd->auth_keyid; |
| 10333 | override_ok = 0; |
| 10334 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 10335 | } else if (override_ok) { |
| 10336 | auth_keyid = fwd->auth_keyid; |
| 10337 | override_ok = 0; |
| 10338 | } else if (fwd->auth_keyid != auth_keyid) { |
| 10339 | /* different keyid, so done bundling */ |
| 10340 | break; |
| 10341 | } |
| 10342 | } |
| 10343 | m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref); |
| 10344 | if (m == NULL) { |
| 10345 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 10346 | return (ENOMEM); |
| 10347 | } |
| 10348 | /* Do clear IP_DF ? */ |
| 10349 | if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) { |
| 10350 | no_fragmentflg = 0; |
| 10351 | } |
| 10352 | /* upate our MTU size */ |
| 10353 | if (mtu > (fwd->send_size + dmtu)) |
| 10354 | mtu -= (fwd->send_size + dmtu); |
| 10355 | else |
| 10356 | mtu = 0; |
| 10357 | data_list[bundle_at++] = fwd; |
| 10358 | if (bundle_at >= SCTP_MAX_DATA_BUNDLING) { |
| 10359 | break; |
| 10360 | } |
| 10361 | } else { |
| 10362 | /* can't fit so we are done */ |
| 10363 | break; |
| 10364 | } |
| 10365 | } |
| 10366 | } |
| 10367 | /* Is there something to send for this destination? */ |
| 10368 | if (m) { |
| 10369 | /* |
| 10370 | * No matter if we fail/or succeed we should start a |
| 10371 | * timer. A failure is like a lost IP packet :-) |
| 10372 | */ |
| 10373 | if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { |
| 10374 | /* |
| 10375 | * no timer running on this destination |
| 10376 | * restart it. |
| 10377 | */ |
| 10378 | sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); |
| 10379 | tmr_started = 1; |
| 10380 | } |
| 10381 | /* Now lets send it, if there is anything to send :> */ |
| 10382 | if ((error = sctp_lowlevel_chunk_output(inp, stcb, net, |
| 10383 | (struct sockaddr *)&net->ro._l_addr, m, |
| 10384 | auth_offset, auth, auth_keyid, |
| 10385 | no_fragmentflg, 0, 0, |
| 10386 | inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag), |
| 10387 | net->port, NULL, |
| 10388 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 10389 | 0, 0, |
| 10390 | #endif |
| 10391 | so_locked))) { |
| 10392 | /* error, we could not output */ |
| 10393 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error); |
| 10394 | if (error == ENOBUFS) { |
| 10395 | asoc->ifp_had_enobuf = 1; |
| 10396 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 10397 | } |
| 10398 | return (error); |
| 10399 | } else { |
| 10400 | asoc->ifp_had_enobuf = 0; |
| 10401 | } |
| 10402 | endofchain = NULL; |
| 10403 | auth = NULL; |
| 10404 | auth_offset = 0; |
| 10405 | /* For HB's */ |
| 10406 | /* |
| 10407 | * We don't want to mark the net->sent time here |
| 10408 | * since this we use this for HB and retrans cannot |
| 10409 | * measure RTT |
| 10410 | */ |
| 10411 | /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */ |
| 10412 |
| 10413 | /* For auto-close */ |
| 10414 | cnt_thru++; |
| 10415 | if (*now_filled == 0) { |
| 10416 | (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent); |
| 10417 | *now = asoc->time_last_sent; |
| 10418 | *now_filled = 1; |
| 10419 | } else { |
| 10420 | asoc->time_last_sent = *now; |
| 10421 | } |
| 10422 | *cnt_out += bundle_at; |
| 10423 | #ifdef SCTP_AUDITING_ENABLED |
| 10424 | sctp_audit_log(0xC4, bundle_at); |
| 10425 | #endif |
| 10426 | if (bundle_at) { |
| 10427 | tsns_sent = data_list[0]->rec.data.tsn; |
| 10428 | } |
| 10429 | for (i = 0; i < bundle_at; i++) { |
| 10430 | SCTP_STAT_INCR(sctps_sendretransdata); |
| 10431 | data_list[i]->sent = SCTP_DATAGRAM_SENT; |
| 10432 | /* |
| 10433 | * When we have a revoked data, and we |
| 10434 | * retransmit it, then we clear the revoked |
| 10435 | * flag since this flag dictates if we |
| 10436 | * subtracted from the fs |
| 10437 | */ |
| 10438 | if (data_list[i]->rec.data.chunk_was_revoked) { |
| 10439 | /* Deflate the cwnd */ |
| 10440 | data_list[i]->whoTo->cwnd -= data_list[i]->book_size; |
| 10441 | data_list[i]->rec.data.chunk_was_revoked = 0; |
| 10442 | } |
| 10443 | data_list[i]->snd_count++; |
| 10444 | sctp_ucount_decr(asoc->sent_queue_retran_cnt); |
| 10445 | /* record the time */ |
| 10446 | data_list[i]->sent_rcv_time = asoc->time_last_sent; |
| 10447 | if (data_list[i]->book_size_scale) { |
| 10448 | /* |
| 10449 | * need to double the book size on |
| 10450 | * this one |
| 10451 | */ |
| 10452 | data_list[i]->book_size_scale = 0; |
| 10453 | /* Since we double the booksize, we must |
| 10454 | * also double the output queue size, since this |
| 10455 | * get shrunk when we free by this amount. |
| 10456 | */ |
| 10457 | atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size); |
| 10458 | data_list[i]->book_size *= 2; |
| 10459 | } else { |
| 10460 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) { |
| 10461 | sctp_log_rwnd(SCTP_DECREASE_PEER_RWND, |
| 10462 | asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)); |
| 10463 | } |
| 10464 | asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd, |
| 10465 | (uint32_t) (data_list[i]->send_size + |
| 10466 | SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))); |
| 10467 | } |
| 10468 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { |
| 10469 | sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND, |
| 10470 | data_list[i]->whoTo->flight_size, |
| 10471 | data_list[i]->book_size, |
| 10472 | (uint32_t)(uintptr_t)data_list[i]->whoTo, |
| 10473 | data_list[i]->rec.data.tsn); |
| 10474 | } |
| 10475 | sctp_flight_size_increase(data_list[i]); |
| 10476 | sctp_total_flight_increase(stcb, data_list[i]); |
| 10477 | if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) { |
| 10478 | /* SWS sender side engages */ |
| 10479 | asoc->peers_rwnd = 0; |
| 10480 | } |
| 10481 | if ((i == 0) && |
| 10482 | (data_list[i]->rec.data.doing_fast_retransmit)) { |
| 10483 | SCTP_STAT_INCR(sctps_sendfastretrans); |
| 10484 | if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) && |
| 10485 | (tmr_started == 0)) { |
| 10486 | /*- |
| 10487 | * ok we just fast-retrans'd |
| 10488 | * the lowest TSN, i.e the |
| 10489 | * first on the list. In |
| 10490 | * this case we want to give |
| 10491 | * some more time to get a |
| 10492 | * SACK back without a |
| 10493 | * t3-expiring. |
| 10494 | */ |
| 10495 | sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net, |
| 10496 | SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2); |
| 10497 | sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net); |
| 10498 | } |
| 10499 | } |
| 10500 | } |
| 10501 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 10502 | sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND); |
| 10503 | } |
| 10504 | #ifdef SCTP_AUDITING_ENABLED |
| 10505 | sctp_auditing(21, inp, stcb, NULL); |
| 10506 | #endif |
| 10507 | } else { |
| 10508 | /* None will fit */ |
| 10509 | return (1); |
| 10510 | } |
| 10511 | if (asoc->sent_queue_retran_cnt <= 0) { |
| 10512 | /* all done we have no more to retran */ |
| 10513 | asoc->sent_queue_retran_cnt = 0; |
| 10514 | break; |
| 10515 | } |
| 10516 | if (one_chunk) { |
| 10517 | /* No more room in rwnd */ |
| 10518 | return (1); |
| 10519 | } |
| 10520 | /* stop the for loop here. we sent out a packet */ |
| 10521 | break; |
| 10522 | } |
| 10523 | return (0); |
| 10524 | } |
| 10525 |
| 10526 | static void |
| 10527 | sctp_timer_validation(struct sctp_inpcb *inp, |
| 10528 | struct sctp_tcb *stcb, |
| 10529 | struct sctp_association *asoc) |
| 10530 | { |
| 10531 | struct sctp_nets *net; |
| 10532 |
| 10533 | /* Validate that a timer is running somewhere */ |
| 10534 | TAILQ_FOREACH(net, &asoc->nets, sctp_next) { |
| 10535 | if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { |
| 10536 | /* Here is a timer */ |
| 10537 | return; |
| 10538 | } |
| 10539 | } |
| 10540 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 10541 | /* Gak, we did not have a timer somewhere */ |
| 10542 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n"); |
| 10543 | if (asoc->alternate) { |
| 10544 | sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->alternate); |
| 10545 | } else { |
| 10546 | sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination); |
| 10547 | } |
| 10548 | return; |
| 10549 | } |
| 10550 |
| 10551 | void |
| 10552 | sctp_chunk_output(struct sctp_inpcb *inp, |
| 10553 | struct sctp_tcb *stcb, |
| 10554 | int from_where, |
| 10555 | int so_locked) |
| 10556 | { |
| 10557 | /*- |
| 10558 | * Ok this is the generic chunk service queue. we must do the |
| 10559 | * following: |
| 10560 | * - See if there are retransmits pending, if so we must |
| 10561 | * do these first. |
| 10562 | * - Service the stream queue that is next, moving any |
| 10563 | * message (note I must get a complete message i.e. |
| 10564 | * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning |
| 10565 | * TSN's |
| 10566 | * - Check to see if the cwnd/rwnd allows any output, if so we |
| 10567 | * go ahead and fomulate and send the low level chunks. Making sure |
| 10568 | * to combine any control in the control chunk queue also. |
| 10569 | */ |
| 10570 | struct sctp_association *asoc; |
| 10571 | struct sctp_nets *net; |
| 10572 | int error = 0, num_out, tot_out = 0, ret = 0, reason_code; |
| 10573 | unsigned int burst_cnt = 0; |
| 10574 | struct timeval now; |
| 10575 | int now_filled = 0; |
| 10576 | int nagle_on; |
| 10577 | int frag_point = sctp_get_frag_point(stcb, &stcb->asoc); |
| 10578 | int un_sent = 0; |
| 10579 | int fr_done; |
| 10580 | unsigned int tot_frs = 0; |
| 10581 |
| 10582 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 10583 | if (so_locked) { |
| 10584 | sctp_lock_assert(SCTP_INP_SO(inp)); |
| 10585 | } else { |
| 10586 | sctp_unlock_assert(SCTP_INP_SO(inp)); |
| 10587 | } |
| 10588 | #endif |
| 10589 | asoc = &stcb->asoc; |
| 10590 | do_it_again: |
| 10591 | /* The Nagle algorithm is only applied when handling a send call. */ |
| 10592 | if (from_where == SCTP_OUTPUT_FROM_USR_SEND) { |
| 10593 | if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) { |
| 10594 | nagle_on = 0; |
| 10595 | } else { |
| 10596 | nagle_on = 1; |
| 10597 | } |
| 10598 | } else { |
| 10599 | nagle_on = 0; |
| 10600 | } |
| 10601 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 10602 |
| 10603 | un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); |
| 10604 |
| 10605 | if ((un_sent <= 0) && |
| 10606 | (TAILQ_EMPTY(&asoc->control_send_queue)) && |
| 10607 | (TAILQ_EMPTY(&asoc->asconf_send_queue)) && |
| 10608 | (asoc->sent_queue_retran_cnt == 0) && |
| 10609 | (asoc->trigger_reset == 0)) { |
| 10610 | /* Nothing to do unless there is something to be sent left */ |
| 10611 | return; |
| 10612 | } |
| 10613 | /* Do we have something to send, data or control AND |
| 10614 | * a sack timer running, if so piggy-back the sack. |
| 10615 | */ |
| 10616 | if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) { |
| 10617 | sctp_send_sack(stcb, so_locked); |
| 10618 | sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, |
| 10619 | SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3); |
| 10620 | } |
| 10621 | while (asoc->sent_queue_retran_cnt) { |
| 10622 | /*- |
| 10623 | * Ok, it is retransmission time only, we send out only ONE |
| 10624 | * packet with a single call off to the retran code. |
| 10625 | */ |
| 10626 | if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) { |
| 10627 | /*- |
| 10628 | * Special hook for handling cookiess discarded |
| 10629 | * by peer that carried data. Send cookie-ack only |
| 10630 | * and then the next call with get the retran's. |
| 10631 | */ |
| 10632 | (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, |
| 10633 | from_where, |
| 10634 | &now, &now_filled, frag_point, so_locked); |
| 10635 | return; |
| 10636 | } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) { |
| 10637 | /* if its not from a HB then do it */ |
| 10638 | fr_done = 0; |
| 10639 | ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked); |
| 10640 | if (fr_done) { |
| 10641 | tot_frs++; |
| 10642 | } |
| 10643 | } else { |
| 10644 | /* |
| 10645 | * its from any other place, we don't allow retran |
| 10646 | * output (only control) |
| 10647 | */ |
| 10648 | ret = 1; |
| 10649 | } |
| 10650 | if (ret > 0) { |
| 10651 | /* Can't send anymore */ |
| 10652 | /*- |
| 10653 | * now lets push out control by calling med-level |
| 10654 | * output once. this assures that we WILL send HB's |
| 10655 | * if queued too. |
| 10656 | */ |
| 10657 | (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, |
| 10658 | from_where, |
| 10659 | &now, &now_filled, frag_point, so_locked); |
| 10660 | #ifdef SCTP_AUDITING_ENABLED |
| 10661 | sctp_auditing(8, inp, stcb, NULL); |
| 10662 | #endif |
| 10663 | sctp_timer_validation(inp, stcb, asoc); |
| 10664 | return; |
| 10665 | } |
| 10666 | if (ret < 0) { |
| 10667 | /*- |
| 10668 | * The count was off.. retran is not happening so do |
| 10669 | * the normal retransmission. |
| 10670 | */ |
| 10671 | #ifdef SCTP_AUDITING_ENABLED |
| 10672 | sctp_auditing(9, inp, stcb, NULL); |
| 10673 | #endif |
| 10674 | if (ret == SCTP_RETRAN_EXIT) { |
| 10675 | return; |
| 10676 | } |
| 10677 | break; |
| 10678 | } |
| 10679 | if (from_where == SCTP_OUTPUT_FROM_T3) { |
| 10680 | /* Only one transmission allowed out of a timeout */ |
| 10681 | #ifdef SCTP_AUDITING_ENABLED |
| 10682 | sctp_auditing(10, inp, stcb, NULL); |
| 10683 | #endif |
| 10684 | /* Push out any control */ |
| 10685 | (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where, |
| 10686 | &now, &now_filled, frag_point, so_locked); |
| 10687 | return; |
| 10688 | } |
| 10689 | if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) { |
| 10690 | /* Hit FR burst limit */ |
| 10691 | return; |
| 10692 | } |
| 10693 | if ((num_out == 0) && (ret == 0)) { |
| 10694 | /* No more retrans to send */ |
| 10695 | break; |
| 10696 | } |
| 10697 | } |
| 10698 | #ifdef SCTP_AUDITING_ENABLED |
| 10699 | sctp_auditing(12, inp, stcb, NULL); |
| 10700 | #endif |
| 10701 | /* Check for bad destinations, if they exist move chunks around. */ |
| 10702 | TAILQ_FOREACH(net, &asoc->nets, sctp_next) { |
| 10703 | if (!(net->dest_state & SCTP_ADDR_REACHABLE)) { |
| 10704 | /*- |
| 10705 | * if possible move things off of this address we |
| 10706 | * still may send below due to the dormant state but |
| 10707 | * we try to find an alternate address to send to |
| 10708 | * and if we have one we move all queued data on the |
| 10709 | * out wheel to this alternate address. |
| 10710 | */ |
| 10711 | if (net->ref_count > 1) |
| 10712 | sctp_move_chunks_from_net(stcb, net); |
| 10713 | } else { |
| 10714 | /*- |
| 10715 | * if ((asoc->sat_network) || (net->addr_is_local)) |
| 10716 | * { burst_limit = asoc->max_burst * |
| 10717 | * SCTP_SAT_NETWORK_BURST_INCR; } |
| 10718 | */ |
| 10719 | if (asoc->max_burst > 0) { |
| 10720 | if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) { |
| 10721 | if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) { |
| 10722 | /* JRS - Use the congestion control given in the congestion control module */ |
| 10723 | asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst); |
| 10724 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) { |
| 10725 | sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED); |
| 10726 | } |
| 10727 | SCTP_STAT_INCR(sctps_maxburstqueued); |
| 10728 | } |
| 10729 | net->fast_retran_ip = 0; |
| 10730 | } else { |
| 10731 | if (net->flight_size == 0) { |
| 10732 | /* Should be decaying the cwnd here */ |
| 10733 | ; |
| 10734 | } |
| 10735 | } |
| 10736 | } |
| 10737 | } |
| 10738 | } |
| 10739 | burst_cnt = 0; |
| 10740 | do { |
| 10741 | error = sctp_med_chunk_output(inp, stcb, asoc, &num_out, |
| 10742 | &reason_code, 0, from_where, |
| 10743 | &now, &now_filled, frag_point, so_locked); |
| 10744 | if (error) { |
| 10745 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error); |
| 10746 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) { |
| 10747 | sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP); |
| 10748 | } |
| 10749 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 10750 | sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES); |
| 10751 | sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES); |
| 10752 | } |
| 10753 | break; |
| 10754 | } |
| 10755 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out); |
| 10756 |
| 10757 | tot_out += num_out; |
| 10758 | burst_cnt++; |
| 10759 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 10760 | sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES); |
| 10761 | if (num_out == 0) { |
| 10762 | sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES); |
| 10763 | } |
| 10764 | } |
| 10765 | if (nagle_on) { |
| 10766 | /* |
| 10767 | * When the Nagle algorithm is used, look at how much |
| 10768 | * is unsent, then if its smaller than an MTU and we |
| 10769 | * have data in flight we stop, except if we are |
| 10770 | * handling a fragmented user message. |
| 10771 | */ |
| 10772 | un_sent = stcb->asoc.total_output_queue_size - stcb->asoc.total_flight; |
| 10773 | if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) && |
| 10774 | (stcb->asoc.total_flight > 0)) { |
| 10775 | /* && sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {*/ |
| 10776 | break; |
| 10777 | } |
| 10778 | } |
| 10779 | if (TAILQ_EMPTY(&asoc->control_send_queue) && |
| 10780 | TAILQ_EMPTY(&asoc->send_queue) && |
| 10781 | sctp_is_there_unsent_data(stcb, so_locked) == 0) { |
| 10782 | /* Nothing left to send */ |
| 10783 | break; |
| 10784 | } |
| 10785 | if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) { |
| 10786 | /* Nothing left to send */ |
| 10787 | break; |
| 10788 | } |
| 10789 | } while (num_out && |
| 10790 | ((asoc->max_burst == 0) || |
| 10791 | SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) || |
| 10792 | (burst_cnt < asoc->max_burst))); |
| 10793 |
| 10794 | if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) { |
| 10795 | if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) { |
| 10796 | SCTP_STAT_INCR(sctps_maxburstqueued); |
| 10797 | asoc->burst_limit_applied = 1; |
| 10798 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) { |
| 10799 | sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED); |
| 10800 | } |
| 10801 | } else { |
| 10802 | asoc->burst_limit_applied = 0; |
| 10803 | } |
| 10804 | } |
| 10805 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { |
| 10806 | sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES); |
| 10807 | } |
| 10808 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n", |
| 10809 | tot_out); |
| 10810 |
| 10811 | /*- |
| 10812 | * Now we need to clean up the control chunk chain if a ECNE is on |
| 10813 | * it. It must be marked as UNSENT again so next call will continue |
| 10814 | * to send it until such time that we get a CWR, to remove it. |
| 10815 | */ |
| 10816 | if (stcb->asoc.ecn_echo_cnt_onq) |
| 10817 | sctp_fix_ecn_echo(asoc); |
| 10818 |
| 10819 | if (stcb->asoc.trigger_reset) { |
| 10820 | if (sctp_send_stream_reset_out_if_possible(stcb, so_locked) == 0) { |
| 10821 | goto do_it_again; |
| 10822 | } |
| 10823 | } |
| 10824 | return; |
| 10825 | } |
| 10826 |
| 10827 | int |
| 10828 | sctp_output( |
| 10829 | struct sctp_inpcb *inp, |
| 10830 | struct mbuf *m, |
| 10831 | struct sockaddr *addr, |
| 10832 | struct mbuf *control, |
| 10833 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 10834 | struct thread *p, |
| 10835 | #elif defined(_WIN32) && !defined(__Userspace__) |
| 10836 | PKTHREAD p, |
| 10837 | #else |
| 10838 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 10839 | struct proc *p SCTP_UNUSED, |
| 10840 | #else |
| 10841 | struct proc *p, |
| 10842 | #endif |
| 10843 | #endif |
| 10844 | int flags) |
| 10845 | { |
| 10846 | if (inp == NULL) { |
| 10847 | SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 10848 | return (EINVAL); |
| 10849 | } |
| 10850 |
| 10851 | if (inp->sctp_socket == NULL) { |
| 10852 | SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 10853 | return (EINVAL); |
| 10854 | } |
| 10855 | return (sctp_sosend(inp->sctp_socket, |
| 10856 | addr, |
| 10857 | (struct uio *)NULL, |
| 10858 | m, |
| 10859 | control, |
| 10860 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 10861 | flags |
| 10862 | #else |
| 10863 | flags, p |
| 10864 | #endif |
| 10865 | )); |
| 10866 | } |
| 10867 |
| 10868 | void |
| 10869 | send_forward_tsn(struct sctp_tcb *stcb, |
| 10870 | struct sctp_association *asoc) |
| 10871 | { |
| 10872 | struct sctp_tmit_chunk *chk, *at, *tp1, *last; |
| 10873 | struct sctp_forward_tsn_chunk *fwdtsn; |
| 10874 | struct sctp_strseq *strseq; |
| 10875 | struct sctp_strseq_mid *strseq_m; |
| 10876 | uint32_t advance_peer_ack_point; |
| 10877 | unsigned int cnt_of_space, i, ovh; |
| 10878 | unsigned int space_needed; |
| 10879 | unsigned int cnt_of_skipped = 0; |
| 10880 |
| 10881 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 10882 | TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { |
| 10883 | if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) { |
| 10884 | /* mark it to unsent */ |
| 10885 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 10886 | chk->snd_count = 0; |
| 10887 | /* Do we correct its output location? */ |
| 10888 | if (chk->whoTo) { |
| 10889 | sctp_free_remote_addr(chk->whoTo); |
| 10890 | chk->whoTo = NULL; |
| 10891 | } |
| 10892 | goto sctp_fill_in_rest; |
| 10893 | } |
| 10894 | } |
| 10895 | /* Ok if we reach here we must build one */ |
| 10896 | sctp_alloc_a_chunk(stcb, chk); |
| 10897 | if (chk == NULL) { |
| 10898 | return; |
| 10899 | } |
| 10900 | asoc->fwd_tsn_cnt++; |
| 10901 | chk->copy_by_ref = 0; |
| 10902 | /* |
| 10903 | * We don't do the old thing here since |
| 10904 | * this is used not for on-wire but to |
| 10905 | * tell if we are sending a fwd-tsn by |
| 10906 | * the stack during output. And if its |
| 10907 | * a IFORWARD or a FORWARD it is a fwd-tsn. |
| 10908 | */ |
| 10909 | chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN; |
| 10910 | chk->rec.chunk_id.can_take_data = 0; |
| 10911 | chk->flags = 0; |
| 10912 | chk->asoc = asoc; |
| 10913 | chk->whoTo = NULL; |
| 10914 | chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); |
| 10915 | if (chk->data == NULL) { |
| 10916 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 10917 | return; |
| 10918 | } |
| 10919 | SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); |
| 10920 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 10921 | chk->snd_count = 0; |
| 10922 | TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next); |
| 10923 | asoc->ctrl_queue_cnt++; |
| 10924 | sctp_fill_in_rest: |
| 10925 | /*- |
| 10926 | * Here we go through and fill out the part that deals with |
| 10927 | * stream/seq of the ones we skip. |
| 10928 | */ |
| 10929 | SCTP_BUF_LEN(chk->data) = 0; |
| 10930 | TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) { |
| 10931 | if ((at->sent != SCTP_FORWARD_TSN_SKIP) && |
| 10932 | (at->sent != SCTP_DATAGRAM_NR_ACKED)) { |
| 10933 | /* no more to look at */ |
| 10934 | break; |
| 10935 | } |
| 10936 | if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) { |
| 10937 | /* We don't report these */ |
| 10938 | continue; |
| 10939 | } |
| 10940 | cnt_of_skipped++; |
| 10941 | } |
| 10942 | if (asoc->idata_supported) { |
| 10943 | space_needed = (sizeof(struct sctp_forward_tsn_chunk) + |
| 10944 | (cnt_of_skipped * sizeof(struct sctp_strseq_mid))); |
| 10945 | } else { |
| 10946 | space_needed = (sizeof(struct sctp_forward_tsn_chunk) + |
| 10947 | (cnt_of_skipped * sizeof(struct sctp_strseq))); |
| 10948 | } |
| 10949 | cnt_of_space = (unsigned int)M_TRAILINGSPACE(chk->data); |
| 10950 |
| 10951 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { |
| 10952 | ovh = SCTP_MIN_OVERHEAD; |
| 10953 | } else { |
| 10954 | ovh = SCTP_MIN_V4_OVERHEAD; |
| 10955 | } |
| 10956 | if (cnt_of_space > (asoc->smallest_mtu - ovh)) { |
| 10957 | /* trim to a mtu size */ |
| 10958 | cnt_of_space = asoc->smallest_mtu - ovh; |
| 10959 | } |
| 10960 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) { |
| 10961 | sctp_misc_ints(SCTP_FWD_TSN_CHECK, |
| 10962 | 0xff, 0, cnt_of_skipped, |
| 10963 | asoc->advanced_peer_ack_point); |
| 10964 | } |
| 10965 | advance_peer_ack_point = asoc->advanced_peer_ack_point; |
| 10966 | if (cnt_of_space < space_needed) { |
| 10967 | /*- |
| 10968 | * ok we must trim down the chunk by lowering the |
| 10969 | * advance peer ack point. |
| 10970 | */ |
| 10971 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) { |
| 10972 | sctp_misc_ints(SCTP_FWD_TSN_CHECK, |
| 10973 | 0xff, 0xff, cnt_of_space, |
| 10974 | space_needed); |
| 10975 | } |
| 10976 | cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk); |
| 10977 | if (asoc->idata_supported) { |
| 10978 | cnt_of_skipped /= sizeof(struct sctp_strseq_mid); |
| 10979 | } else { |
| 10980 | cnt_of_skipped /= sizeof(struct sctp_strseq); |
| 10981 | } |
| 10982 | /*- |
| 10983 | * Go through and find the TSN that will be the one |
| 10984 | * we report. |
| 10985 | */ |
| 10986 | at = TAILQ_FIRST(&asoc->sent_queue); |
| 10987 | if (at != NULL) { |
| 10988 | for (i = 0; i < cnt_of_skipped; i++) { |
| 10989 | tp1 = TAILQ_NEXT(at, sctp_next); |
| 10990 | if (tp1 == NULL) { |
| 10991 | break; |
| 10992 | } |
| 10993 | at = tp1; |
| 10994 | } |
| 10995 | } |
| 10996 | if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) { |
| 10997 | sctp_misc_ints(SCTP_FWD_TSN_CHECK, |
| 10998 | 0xff, cnt_of_skipped, at->rec.data.tsn, |
| 10999 | asoc->advanced_peer_ack_point); |
| 11000 | } |
| 11001 | last = at; |
| 11002 | /*- |
| 11003 | * last now points to last one I can report, update |
| 11004 | * peer ack point |
| 11005 | */ |
| 11006 | if (last) { |
| 11007 | advance_peer_ack_point = last->rec.data.tsn; |
| 11008 | } |
| 11009 | if (asoc->idata_supported) { |
| 11010 | space_needed = sizeof(struct sctp_forward_tsn_chunk) + |
| 11011 | cnt_of_skipped * sizeof(struct sctp_strseq_mid); |
| 11012 | } else { |
| 11013 | space_needed = sizeof(struct sctp_forward_tsn_chunk) + |
| 11014 | cnt_of_skipped * sizeof(struct sctp_strseq); |
| 11015 | } |
| 11016 | } |
| 11017 | chk->send_size = space_needed; |
| 11018 | /* Setup the chunk */ |
| 11019 | fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *); |
| 11020 | fwdtsn->ch.chunk_length = htons(chk->send_size); |
| 11021 | fwdtsn->ch.chunk_flags = 0; |
| 11022 | if (asoc->idata_supported) { |
| 11023 | fwdtsn->ch.chunk_type = SCTP_IFORWARD_CUM_TSN; |
| 11024 | } else { |
| 11025 | fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN; |
| 11026 | } |
| 11027 | fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point); |
| 11028 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 11029 | fwdtsn++; |
| 11030 | /*- |
| 11031 | * Move pointer to after the fwdtsn and transfer to the |
| 11032 | * strseq pointer. |
| 11033 | */ |
| 11034 | if (asoc->idata_supported) { |
| 11035 | strseq_m = (struct sctp_strseq_mid *)fwdtsn; |
| 11036 | strseq = NULL; |
| 11037 | } else { |
| 11038 | strseq = (struct sctp_strseq *)fwdtsn; |
| 11039 | strseq_m = NULL; |
| 11040 | } |
| 11041 | /*- |
| 11042 | * Now populate the strseq list. This is done blindly |
| 11043 | * without pulling out duplicate stream info. This is |
| 11044 | * inefficent but won't harm the process since the peer will |
| 11045 | * look at these in sequence and will thus release anything. |
| 11046 | * It could mean we exceed the PMTU and chop off some that |
| 11047 | * we could have included.. but this is unlikely (aka 1432/4 |
| 11048 | * would mean 300+ stream seq's would have to be reported in |
| 11049 | * one FWD-TSN. With a bit of work we can later FIX this to |
| 11050 | * optimize and pull out duplicates.. but it does add more |
| 11051 | * overhead. So for now... not! |
| 11052 | */ |
| 11053 | i = 0; |
| 11054 | TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) { |
| 11055 | if (i >= cnt_of_skipped) { |
| 11056 | break; |
| 11057 | } |
| 11058 | if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) { |
| 11059 | /* We don't report these */ |
| 11060 | continue; |
| 11061 | } |
| 11062 | if (at->rec.data.tsn == advance_peer_ack_point) { |
| 11063 | at->rec.data.fwd_tsn_cnt = 0; |
| 11064 | } |
| 11065 | if (asoc->idata_supported) { |
| 11066 | strseq_m->sid = htons(at->rec.data.sid); |
| 11067 | if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) { |
| 11068 | strseq_m->flags = htons(PR_SCTP_UNORDERED_FLAG); |
| 11069 | } else { |
| 11070 | strseq_m->flags = 0; |
| 11071 | } |
| 11072 | strseq_m->mid = htonl(at->rec.data.mid); |
| 11073 | strseq_m++; |
| 11074 | } else { |
| 11075 | strseq->sid = htons(at->rec.data.sid); |
| 11076 | strseq->ssn = htons((uint16_t)at->rec.data.mid); |
| 11077 | strseq++; |
| 11078 | } |
| 11079 | i++; |
| 11080 | } |
| 11081 | return; |
| 11082 | } |
| 11083 |
| 11084 | void |
| 11085 | sctp_send_sack(struct sctp_tcb *stcb, int so_locked) |
| 11086 | { |
| 11087 | /*- |
| 11088 | * Queue up a SACK or NR-SACK in the control queue. |
| 11089 | * We must first check to see if a SACK or NR-SACK is |
| 11090 | * somehow on the control queue. |
| 11091 | * If so, we will take and and remove the old one. |
| 11092 | */ |
| 11093 | struct sctp_association *asoc; |
| 11094 | struct sctp_tmit_chunk *chk, *a_chk; |
| 11095 | struct sctp_sack_chunk *sack; |
| 11096 | struct sctp_nr_sack_chunk *nr_sack; |
| 11097 | struct sctp_gap_ack_block *gap_descriptor; |
| 11098 | const struct sack_track *selector; |
| 11099 | int mergeable = 0; |
| 11100 | int offset; |
| 11101 | caddr_t limit; |
| 11102 | uint32_t *dup; |
| 11103 | int limit_reached = 0; |
| 11104 | unsigned int i, siz, j; |
| 11105 | unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space; |
| 11106 | int num_dups = 0; |
| 11107 | int space_req; |
| 11108 | uint32_t highest_tsn; |
| 11109 | uint8_t flags; |
| 11110 | uint8_t type; |
| 11111 | uint8_t tsn_map; |
| 11112 |
| 11113 | if (stcb->asoc.nrsack_supported == 1) { |
| 11114 | type = SCTP_NR_SELECTIVE_ACK; |
| 11115 | } else { |
| 11116 | type = SCTP_SELECTIVE_ACK; |
| 11117 | } |
| 11118 | a_chk = NULL; |
| 11119 | asoc = &stcb->asoc; |
| 11120 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 11121 | if (asoc->last_data_chunk_from == NULL) { |
| 11122 | /* Hmm we never received anything */ |
| 11123 | return; |
| 11124 | } |
| 11125 | sctp_slide_mapping_arrays(stcb); |
| 11126 | sctp_set_rwnd(stcb, asoc); |
| 11127 | TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { |
| 11128 | if (chk->rec.chunk_id.id == type) { |
| 11129 | /* Hmm, found a sack already on queue, remove it */ |
| 11130 | TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); |
| 11131 | asoc->ctrl_queue_cnt--; |
| 11132 | a_chk = chk; |
| 11133 | if (a_chk->data) { |
| 11134 | sctp_m_freem(a_chk->data); |
| 11135 | a_chk->data = NULL; |
| 11136 | } |
| 11137 | if (a_chk->whoTo) { |
| 11138 | sctp_free_remote_addr(a_chk->whoTo); |
| 11139 | a_chk->whoTo = NULL; |
| 11140 | } |
| 11141 | break; |
| 11142 | } |
| 11143 | } |
| 11144 | if (a_chk == NULL) { |
| 11145 | sctp_alloc_a_chunk(stcb, a_chk); |
| 11146 | if (a_chk == NULL) { |
| 11147 | /* No memory so we drop the idea, and set a timer */ |
| 11148 | if (stcb->asoc.delayed_ack) { |
| 11149 | sctp_timer_stop(SCTP_TIMER_TYPE_RECV, |
| 11150 | stcb->sctp_ep, stcb, NULL, |
| 11151 | SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4); |
| 11152 | sctp_timer_start(SCTP_TIMER_TYPE_RECV, |
| 11153 | stcb->sctp_ep, stcb, NULL); |
| 11154 | } else { |
| 11155 | stcb->asoc.send_sack = 1; |
| 11156 | } |
| 11157 | return; |
| 11158 | } |
| 11159 | a_chk->copy_by_ref = 0; |
| 11160 | a_chk->rec.chunk_id.id = type; |
| 11161 | a_chk->rec.chunk_id.can_take_data = 1; |
| 11162 | } |
| 11163 | /* Clear our pkt counts */ |
| 11164 | asoc->data_pkts_seen = 0; |
| 11165 |
| 11166 | a_chk->flags = 0; |
| 11167 | a_chk->asoc = asoc; |
| 11168 | a_chk->snd_count = 0; |
| 11169 | a_chk->send_size = 0; /* fill in later */ |
| 11170 | a_chk->sent = SCTP_DATAGRAM_UNSENT; |
| 11171 | a_chk->whoTo = NULL; |
| 11172 |
| 11173 | if (!(asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE)) { |
| 11174 | /*- |
| 11175 | * Ok, the destination for the SACK is unreachable, lets see if |
| 11176 | * we can select an alternate to asoc->last_data_chunk_from |
| 11177 | */ |
| 11178 | a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0); |
| 11179 | if (a_chk->whoTo == NULL) { |
| 11180 | /* Nope, no alternate */ |
| 11181 | a_chk->whoTo = asoc->last_data_chunk_from; |
| 11182 | } |
| 11183 | } else { |
| 11184 | a_chk->whoTo = asoc->last_data_chunk_from; |
| 11185 | } |
| 11186 | if (a_chk->whoTo) { |
| 11187 | atomic_add_int(&a_chk->whoTo->ref_count, 1); |
| 11188 | } |
| 11189 | if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) { |
| 11190 | highest_tsn = asoc->highest_tsn_inside_map; |
| 11191 | } else { |
| 11192 | highest_tsn = asoc->highest_tsn_inside_nr_map; |
| 11193 | } |
| 11194 | if (highest_tsn == asoc->cumulative_tsn) { |
| 11195 | /* no gaps */ |
| 11196 | if (type == SCTP_SELECTIVE_ACK) { |
| 11197 | space_req = sizeof(struct sctp_sack_chunk); |
| 11198 | } else { |
| 11199 | space_req = sizeof(struct sctp_nr_sack_chunk); |
| 11200 | } |
| 11201 | } else { |
| 11202 | /* gaps get a cluster */ |
| 11203 | space_req = MCLBYTES; |
| 11204 | } |
| 11205 | /* Ok now lets formulate a MBUF with our sack */ |
| 11206 | a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_NOWAIT, 1, MT_DATA); |
| 11207 | if ((a_chk->data == NULL) || |
| 11208 | (a_chk->whoTo == NULL)) { |
| 11209 | /* rats, no mbuf memory */ |
| 11210 | if (a_chk->data) { |
| 11211 | /* was a problem with the destination */ |
| 11212 | sctp_m_freem(a_chk->data); |
| 11213 | a_chk->data = NULL; |
| 11214 | } |
| 11215 | sctp_free_a_chunk(stcb, a_chk, so_locked); |
| 11216 | /* sa_ignore NO_NULL_CHK */ |
| 11217 | if (stcb->asoc.delayed_ack) { |
| 11218 | sctp_timer_stop(SCTP_TIMER_TYPE_RECV, |
| 11219 | stcb->sctp_ep, stcb, NULL, |
| 11220 | SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5); |
| 11221 | sctp_timer_start(SCTP_TIMER_TYPE_RECV, |
| 11222 | stcb->sctp_ep, stcb, NULL); |
| 11223 | } else { |
| 11224 | stcb->asoc.send_sack = 1; |
| 11225 | } |
| 11226 | return; |
| 11227 | } |
| 11228 | /* ok, lets go through and fill it in */ |
| 11229 | SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD); |
| 11230 | space = (unsigned int)M_TRAILINGSPACE(a_chk->data); |
| 11231 | if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) { |
| 11232 | space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD); |
| 11233 | } |
| 11234 | limit = mtod(a_chk->data, caddr_t); |
| 11235 | limit += space; |
| 11236 |
| 11237 | flags = 0; |
| 11238 |
| 11239 | if ((asoc->sctp_cmt_on_off > 0) && |
| 11240 | SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) { |
| 11241 | /*- |
| 11242 | * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been |
| 11243 | * received, then set high bit to 1, else 0. Reset |
| 11244 | * pkts_rcvd. |
| 11245 | */ |
| 11246 | flags |= (asoc->cmt_dac_pkts_rcvd << 6); |
| 11247 | asoc->cmt_dac_pkts_rcvd = 0; |
| 11248 | } |
| 11249 | #ifdef SCTP_ASOCLOG_OF_TSNS |
| 11250 | stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn; |
| 11251 | stcb->asoc.cumack_log_atsnt++; |
| 11252 | if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) { |
| 11253 | stcb->asoc.cumack_log_atsnt = 0; |
| 11254 | } |
| 11255 | #endif |
| 11256 | /* reset the readers interpretation */ |
| 11257 | stcb->freed_by_sorcv_sincelast = 0; |
| 11258 |
| 11259 | if (type == SCTP_SELECTIVE_ACK) { |
| 11260 | sack = mtod(a_chk->data, struct sctp_sack_chunk *); |
| 11261 | nr_sack = NULL; |
| 11262 | gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk)); |
| 11263 | if (highest_tsn > asoc->mapping_array_base_tsn) { |
| 11264 | siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8; |
| 11265 | } else { |
| 11266 | siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + highest_tsn + 7) / 8; |
| 11267 | } |
| 11268 | } else { |
| 11269 | sack = NULL; |
| 11270 | nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *); |
| 11271 | gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk)); |
| 11272 | if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) { |
| 11273 | siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8; |
| 11274 | } else { |
| 11275 | siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8; |
| 11276 | } |
| 11277 | } |
| 11278 |
| 11279 | if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) { |
| 11280 | offset = 1; |
| 11281 | } else { |
| 11282 | offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn; |
| 11283 | } |
| 11284 | if (((type == SCTP_SELECTIVE_ACK) && |
| 11285 | SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) || |
| 11286 | ((type == SCTP_NR_SELECTIVE_ACK) && |
| 11287 | SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) { |
| 11288 | /* we have a gap .. maybe */ |
| 11289 | for (i = 0; i < siz; i++) { |
| 11290 | tsn_map = asoc->mapping_array[i]; |
| 11291 | if (type == SCTP_SELECTIVE_ACK) { |
| 11292 | tsn_map |= asoc->nr_mapping_array[i]; |
| 11293 | } |
| 11294 | if (i == 0) { |
| 11295 | /* |
| 11296 | * Clear all bits corresponding to TSNs |
| 11297 | * smaller or equal to the cumulative TSN. |
| 11298 | */ |
| 11299 | tsn_map &= (~0U << (1 - offset)); |
| 11300 | } |
| 11301 | selector = &sack_array[tsn_map]; |
| 11302 | if (mergeable && selector->right_edge) { |
| 11303 | /* |
| 11304 | * Backup, left and right edges were ok to |
| 11305 | * merge. |
| 11306 | */ |
| 11307 | num_gap_blocks--; |
| 11308 | gap_descriptor--; |
| 11309 | } |
| 11310 | if (selector->num_entries == 0) |
| 11311 | mergeable = 0; |
| 11312 | else { |
| 11313 | for (j = 0; j < selector->num_entries; j++) { |
| 11314 | if (mergeable && selector->right_edge) { |
| 11315 | /* |
| 11316 | * do a merge by NOT setting |
| 11317 | * the left side |
| 11318 | */ |
| 11319 | mergeable = 0; |
| 11320 | } else { |
| 11321 | /* |
| 11322 | * no merge, set the left |
| 11323 | * side |
| 11324 | */ |
| 11325 | mergeable = 0; |
| 11326 | gap_descriptor->start = htons((selector->gaps[j].start + offset)); |
| 11327 | } |
| 11328 | gap_descriptor->end = htons((selector->gaps[j].end + offset)); |
| 11329 | num_gap_blocks++; |
| 11330 | gap_descriptor++; |
| 11331 | if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) { |
| 11332 | /* no more room */ |
| 11333 | limit_reached = 1; |
| 11334 | break; |
| 11335 | } |
| 11336 | } |
| 11337 | if (selector->left_edge) { |
| 11338 | mergeable = 1; |
| 11339 | } |
| 11340 | } |
| 11341 | if (limit_reached) { |
| 11342 | /* Reached the limit stop */ |
| 11343 | break; |
| 11344 | } |
| 11345 | offset += 8; |
| 11346 | } |
| 11347 | } |
| 11348 | if ((type == SCTP_NR_SELECTIVE_ACK) && |
| 11349 | (limit_reached == 0)) { |
| 11350 | mergeable = 0; |
| 11351 |
| 11352 | if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) { |
| 11353 | siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8; |
| 11354 | } else { |
| 11355 | siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8; |
| 11356 | } |
| 11357 |
| 11358 | if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) { |
| 11359 | offset = 1; |
| 11360 | } else { |
| 11361 | offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn; |
| 11362 | } |
| 11363 | if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) { |
| 11364 | /* we have a gap .. maybe */ |
| 11365 | for (i = 0; i < siz; i++) { |
| 11366 | tsn_map = asoc->nr_mapping_array[i]; |
| 11367 | if (i == 0) { |
| 11368 | /* |
| 11369 | * Clear all bits corresponding to TSNs |
| 11370 | * smaller or equal to the cumulative TSN. |
| 11371 | */ |
| 11372 | tsn_map &= (~0U << (1 - offset)); |
| 11373 | } |
| 11374 | selector = &sack_array[tsn_map]; |
| 11375 | if (mergeable && selector->right_edge) { |
| 11376 | /* |
| 11377 | * Backup, left and right edges were ok to |
| 11378 | * merge. |
| 11379 | */ |
| 11380 | num_nr_gap_blocks--; |
| 11381 | gap_descriptor--; |
| 11382 | } |
| 11383 | if (selector->num_entries == 0) |
| 11384 | mergeable = 0; |
| 11385 | else { |
| 11386 | for (j = 0; j < selector->num_entries; j++) { |
| 11387 | if (mergeable && selector->right_edge) { |
| 11388 | /* |
| 11389 | * do a merge by NOT setting |
| 11390 | * the left side |
| 11391 | */ |
| 11392 | mergeable = 0; |
| 11393 | } else { |
| 11394 | /* |
| 11395 | * no merge, set the left |
| 11396 | * side |
| 11397 | */ |
| 11398 | mergeable = 0; |
| 11399 | gap_descriptor->start = htons((selector->gaps[j].start + offset)); |
| 11400 | } |
| 11401 | gap_descriptor->end = htons((selector->gaps[j].end + offset)); |
| 11402 | num_nr_gap_blocks++; |
| 11403 | gap_descriptor++; |
| 11404 | if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) { |
| 11405 | /* no more room */ |
| 11406 | limit_reached = 1; |
| 11407 | break; |
| 11408 | } |
| 11409 | } |
| 11410 | if (selector->left_edge) { |
| 11411 | mergeable = 1; |
| 11412 | } |
| 11413 | } |
| 11414 | if (limit_reached) { |
| 11415 | /* Reached the limit stop */ |
| 11416 | break; |
| 11417 | } |
| 11418 | offset += 8; |
| 11419 | } |
| 11420 | } |
| 11421 | } |
| 11422 | /* now we must add any dups we are going to report. */ |
| 11423 | if ((limit_reached == 0) && (asoc->numduptsns)) { |
| 11424 | dup = (uint32_t *) gap_descriptor; |
| 11425 | for (i = 0; i < asoc->numduptsns; i++) { |
| 11426 | *dup = htonl(asoc->dup_tsns[i]); |
| 11427 | dup++; |
| 11428 | num_dups++; |
| 11429 | if (((caddr_t)dup + sizeof(uint32_t)) > limit) { |
| 11430 | /* no more room */ |
| 11431 | break; |
| 11432 | } |
| 11433 | } |
| 11434 | asoc->numduptsns = 0; |
| 11435 | } |
| 11436 | /* |
| 11437 | * now that the chunk is prepared queue it to the control chunk |
| 11438 | * queue. |
| 11439 | */ |
| 11440 | if (type == SCTP_SELECTIVE_ACK) { |
| 11441 | a_chk->send_size = (uint16_t)(sizeof(struct sctp_sack_chunk) + |
| 11442 | (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) + |
| 11443 | num_dups * sizeof(int32_t)); |
| 11444 | SCTP_BUF_LEN(a_chk->data) = a_chk->send_size; |
| 11445 | sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn); |
| 11446 | sack->sack.a_rwnd = htonl(asoc->my_rwnd); |
| 11447 | sack->sack.num_gap_ack_blks = htons(num_gap_blocks); |
| 11448 | sack->sack.num_dup_tsns = htons(num_dups); |
| 11449 | sack->ch.chunk_type = type; |
| 11450 | sack->ch.chunk_flags = flags; |
| 11451 | sack->ch.chunk_length = htons(a_chk->send_size); |
| 11452 | } else { |
| 11453 | a_chk->send_size = (uint16_t)(sizeof(struct sctp_nr_sack_chunk) + |
| 11454 | (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) + |
| 11455 | num_dups * sizeof(int32_t)); |
| 11456 | SCTP_BUF_LEN(a_chk->data) = a_chk->send_size; |
| 11457 | nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn); |
| 11458 | nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd); |
| 11459 | nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks); |
| 11460 | nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks); |
| 11461 | nr_sack->nr_sack.num_dup_tsns = htons(num_dups); |
| 11462 | nr_sack->nr_sack.reserved = 0; |
| 11463 | nr_sack->ch.chunk_type = type; |
| 11464 | nr_sack->ch.chunk_flags = flags; |
| 11465 | nr_sack->ch.chunk_length = htons(a_chk->send_size); |
| 11466 | } |
| 11467 | TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next); |
| 11468 | asoc->my_last_reported_rwnd = asoc->my_rwnd; |
| 11469 | asoc->ctrl_queue_cnt++; |
| 11470 | asoc->send_sack = 0; |
| 11471 | SCTP_STAT_INCR(sctps_sendsacks); |
| 11472 | return; |
| 11473 | } |
| 11474 |
| 11475 | void |
| 11476 | sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked) |
| 11477 | { |
| 11478 | struct mbuf *m_abort, *m, *m_last; |
| 11479 | struct mbuf *m_out, *m_end = NULL; |
| 11480 | struct sctp_abort_chunk *abort; |
| 11481 | struct sctp_auth_chunk *auth = NULL; |
| 11482 | struct sctp_nets *net; |
| 11483 | uint32_t vtag; |
| 11484 | uint32_t auth_offset = 0; |
| 11485 | int error; |
| 11486 | uint16_t cause_len, chunk_len, padding_len; |
| 11487 |
| 11488 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 11489 | if (so_locked) { |
| 11490 | sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep)); |
| 11491 | } else { |
| 11492 | sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep)); |
| 11493 | } |
| 11494 | #endif |
| 11495 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 11496 | /*- |
| 11497 | * Add an AUTH chunk, if chunk requires it and save the offset into |
| 11498 | * the chain for AUTH |
| 11499 | */ |
| 11500 | if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION, |
| 11501 | stcb->asoc.peer_auth_chunks)) { |
| 11502 | m_out = sctp_add_auth_chunk(NULL, &m_end, &auth, &auth_offset, |
| 11503 | stcb, SCTP_ABORT_ASSOCIATION); |
| 11504 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 11505 | } else { |
| 11506 | m_out = NULL; |
| 11507 | } |
| 11508 | m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_NOWAIT, 1, MT_HEADER); |
| 11509 | if (m_abort == NULL) { |
| 11510 | if (m_out) { |
| 11511 | sctp_m_freem(m_out); |
| 11512 | } |
| 11513 | if (operr) { |
| 11514 | sctp_m_freem(operr); |
| 11515 | } |
| 11516 | return; |
| 11517 | } |
| 11518 | /* link in any error */ |
| 11519 | SCTP_BUF_NEXT(m_abort) = operr; |
| 11520 | cause_len = 0; |
| 11521 | m_last = NULL; |
| 11522 | for (m = operr; m; m = SCTP_BUF_NEXT(m)) { |
| 11523 | cause_len += (uint16_t)SCTP_BUF_LEN(m); |
| 11524 | if (SCTP_BUF_NEXT(m) == NULL) { |
| 11525 | m_last = m; |
| 11526 | } |
| 11527 | } |
| 11528 | SCTP_BUF_LEN(m_abort) = sizeof(struct sctp_abort_chunk); |
| 11529 | chunk_len = (uint16_t)sizeof(struct sctp_abort_chunk) + cause_len; |
| 11530 | padding_len = SCTP_SIZE32(chunk_len) - chunk_len; |
| 11531 | if (m_out == NULL) { |
| 11532 | /* NO Auth chunk prepended, so reserve space in front */ |
| 11533 | SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD); |
| 11534 | m_out = m_abort; |
| 11535 | } else { |
| 11536 | /* Put AUTH chunk at the front of the chain */ |
| 11537 | SCTP_BUF_NEXT(m_end) = m_abort; |
| 11538 | } |
| 11539 | if (stcb->asoc.alternate) { |
| 11540 | net = stcb->asoc.alternate; |
| 11541 | } else { |
| 11542 | net = stcb->asoc.primary_destination; |
| 11543 | } |
| 11544 | /* Fill in the ABORT chunk header. */ |
| 11545 | abort = mtod(m_abort, struct sctp_abort_chunk *); |
| 11546 | abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION; |
| 11547 | if (stcb->asoc.peer_vtag == 0) { |
| 11548 | /* This happens iff the assoc is in COOKIE-WAIT state. */ |
| 11549 | vtag = stcb->asoc.my_vtag; |
| 11550 | abort->ch.chunk_flags = SCTP_HAD_NO_TCB; |
| 11551 | } else { |
| 11552 | vtag = stcb->asoc.peer_vtag; |
| 11553 | abort->ch.chunk_flags = 0; |
| 11554 | } |
| 11555 | abort->ch.chunk_length = htons(chunk_len); |
| 11556 | /* Add padding, if necessary. */ |
| 11557 | if (padding_len > 0) { |
| 11558 | if ((m_last == NULL) || |
| 11559 | (sctp_add_pad_tombuf(m_last, padding_len) == NULL)) { |
| 11560 | sctp_m_freem(m_out); |
| 11561 | return; |
| 11562 | } |
| 11563 | } |
| 11564 | if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net, |
| 11565 | (struct sockaddr *)&net->ro._l_addr, |
| 11566 | m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, 0, |
| 11567 | stcb->sctp_ep->sctp_lport, stcb->rport, htonl(vtag), |
| 11568 | stcb->asoc.primary_destination->port, NULL, |
| 11569 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 11570 | 0, 0, |
| 11571 | #endif |
| 11572 | so_locked))) { |
| 11573 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error); |
| 11574 | if (error == ENOBUFS) { |
| 11575 | stcb->asoc.ifp_had_enobuf = 1; |
| 11576 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 11577 | } |
| 11578 | } else { |
| 11579 | stcb->asoc.ifp_had_enobuf = 0; |
| 11580 | } |
| 11581 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 11582 | } |
| 11583 |
| 11584 | void |
| 11585 | sctp_send_shutdown_complete(struct sctp_tcb *stcb, |
| 11586 | struct sctp_nets *net, |
| 11587 | int reflect_vtag) |
| 11588 | { |
| 11589 | /* formulate and SEND a SHUTDOWN-COMPLETE */ |
| 11590 | struct mbuf *m_shutdown_comp; |
| 11591 | struct sctp_shutdown_complete_chunk *shutdown_complete; |
| 11592 | uint32_t vtag; |
| 11593 | int error; |
| 11594 | uint8_t flags; |
| 11595 |
| 11596 | m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER); |
| 11597 | if (m_shutdown_comp == NULL) { |
| 11598 | /* no mbuf's */ |
| 11599 | return; |
| 11600 | } |
| 11601 | if (reflect_vtag) { |
| 11602 | flags = SCTP_HAD_NO_TCB; |
| 11603 | vtag = stcb->asoc.my_vtag; |
| 11604 | } else { |
| 11605 | flags = 0; |
| 11606 | vtag = stcb->asoc.peer_vtag; |
| 11607 | } |
| 11608 | shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *); |
| 11609 | shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE; |
| 11610 | shutdown_complete->ch.chunk_flags = flags; |
| 11611 | shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk)); |
| 11612 | SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk); |
| 11613 | if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net, |
| 11614 | (struct sockaddr *)&net->ro._l_addr, |
| 11615 | m_shutdown_comp, 0, NULL, 0, 1, 0, 0, |
| 11616 | stcb->sctp_ep->sctp_lport, stcb->rport, |
| 11617 | htonl(vtag), |
| 11618 | net->port, NULL, |
| 11619 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 11620 | 0, 0, |
| 11621 | #endif |
| 11622 | SCTP_SO_NOT_LOCKED))) { |
| 11623 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error); |
| 11624 | if (error == ENOBUFS) { |
| 11625 | stcb->asoc.ifp_had_enobuf = 1; |
| 11626 | SCTP_STAT_INCR(sctps_lowlevelerr); |
| 11627 | } |
| 11628 | } else { |
| 11629 | stcb->asoc.ifp_had_enobuf = 0; |
| 11630 | } |
| 11631 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 11632 | return; |
| 11633 | } |
| 11634 |
| 11635 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 11636 | static void |
| 11637 | sctp_send_resp_msg(struct sockaddr *src, struct sockaddr *dst, |
| 11638 | struct sctphdr *sh, uint32_t vtag, |
| 11639 | uint8_t type, struct mbuf *cause, |
| 11640 | uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum, |
| 11641 | uint32_t vrf_id, uint16_t port) |
| 11642 | #else |
| 11643 | static void |
| 11644 | sctp_send_resp_msg(struct sockaddr *src, struct sockaddr *dst, |
| 11645 | struct sctphdr *sh, uint32_t vtag, |
| 11646 | uint8_t type, struct mbuf *cause, |
| 11647 | uint32_t vrf_id SCTP_UNUSED, uint16_t port) |
| 11648 | #endif |
| 11649 | { |
| 11650 | struct mbuf *o_pak; |
| 11651 | struct mbuf *mout; |
| 11652 | struct sctphdr *shout; |
| 11653 | struct sctp_chunkhdr *ch; |
| 11654 | #if defined(INET) || defined(INET6) |
| 11655 | struct udphdr *udp; |
| 11656 | #endif |
| 11657 | int ret, len, cause_len, padding_len; |
| 11658 | #ifdef INET |
| 11659 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 11660 | sctp_route_t ro; |
| 11661 | #endif |
| 11662 | struct sockaddr_in *src_sin, *dst_sin; |
| 11663 | struct ip *ip; |
| 11664 | #endif |
| 11665 | #ifdef INET6 |
| 11666 | struct sockaddr_in6 *src_sin6, *dst_sin6; |
| 11667 | struct ip6_hdr *ip6; |
| 11668 | #endif |
| 11669 |
| 11670 | /* Compute the length of the cause and add final padding. */ |
| 11671 | cause_len = 0; |
| 11672 | if (cause != NULL) { |
| 11673 | struct mbuf *m_at, *m_last = NULL; |
| 11674 |
| 11675 | for (m_at = cause; m_at; m_at = SCTP_BUF_NEXT(m_at)) { |
| 11676 | if (SCTP_BUF_NEXT(m_at) == NULL) |
| 11677 | m_last = m_at; |
| 11678 | cause_len += SCTP_BUF_LEN(m_at); |
| 11679 | } |
| 11680 | padding_len = cause_len % 4; |
| 11681 | if (padding_len != 0) { |
| 11682 | padding_len = 4 - padding_len; |
| 11683 | } |
| 11684 | if (padding_len != 0) { |
| 11685 | if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) { |
| 11686 | sctp_m_freem(cause); |
| 11687 | return; |
| 11688 | } |
| 11689 | } |
| 11690 | } else { |
| 11691 | padding_len = 0; |
| 11692 | } |
| 11693 | /* Get an mbuf for the header. */ |
| 11694 | len = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); |
| 11695 | switch (dst->sa_family) { |
| 11696 | #ifdef INET |
| 11697 | case AF_INET: |
| 11698 | len += sizeof(struct ip); |
| 11699 | break; |
| 11700 | #endif |
| 11701 | #ifdef INET6 |
| 11702 | case AF_INET6: |
| 11703 | len += sizeof(struct ip6_hdr); |
| 11704 | break; |
| 11705 | #endif |
| 11706 | default: |
| 11707 | break; |
| 11708 | } |
| 11709 | #if defined(INET) || defined(INET6) |
| 11710 | if (port) { |
| 11711 | len += sizeof(struct udphdr); |
| 11712 | } |
| 11713 | #endif |
| 11714 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 11715 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
| 11716 | mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_NOWAIT, 1, MT_DATA); |
| 11717 | #else |
| 11718 | mout = sctp_get_mbuf_for_msg(len + SCTP_MAX_LINKHDR, 1, M_NOWAIT, 1, MT_DATA); |
| 11719 | #endif |
| 11720 | #else |
| 11721 | mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_NOWAIT, 1, MT_DATA); |
| 11722 | #endif |
| 11723 | if (mout == NULL) { |
| 11724 | if (cause) { |
| 11725 | sctp_m_freem(cause); |
| 11726 | } |
| 11727 | return; |
| 11728 | } |
| 11729 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 11730 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
| 11731 | SCTP_BUF_RESV_UF(mout, max_linkhdr); |
| 11732 | #else |
| 11733 | SCTP_BUF_RESV_UF(mout, SCTP_MAX_LINKHDR); |
| 11734 | #endif |
| 11735 | #else |
| 11736 | SCTP_BUF_RESV_UF(mout, max_linkhdr); |
| 11737 | #endif |
| 11738 | SCTP_BUF_LEN(mout) = len; |
| 11739 | SCTP_BUF_NEXT(mout) = cause; |
| 11740 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 11741 | M_SETFIB(mout, fibnum); |
| 11742 | mout->m_pkthdr.flowid = mflowid; |
| 11743 | M_HASHTYPE_SET(mout, mflowtype); |
| 11744 | #endif |
| 11745 | #ifdef INET |
| 11746 | ip = NULL; |
| 11747 | #endif |
| 11748 | #ifdef INET6 |
| 11749 | ip6 = NULL; |
| 11750 | #endif |
| 11751 | switch (dst->sa_family) { |
| 11752 | #ifdef INET |
| 11753 | case AF_INET: |
| 11754 | src_sin = (struct sockaddr_in *)src; |
| 11755 | dst_sin = (struct sockaddr_in *)dst; |
| 11756 | ip = mtod(mout, struct ip *); |
| 11757 | ip->ip_v = IPVERSION; |
| 11758 | ip->ip_hl = (sizeof(struct ip) >> 2); |
| 11759 | ip->ip_tos = 0; |
| 11760 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 11761 | ip->ip_off = htons(IP_DF); |
| 11762 | #elif defined(WITH_CONVERT_IP_OFF) || defined(__APPLE__) |
| 11763 | ip->ip_off = IP_DF; |
| 11764 | #else |
| 11765 | ip->ip_off = htons(IP_DF); |
| 11766 | #endif |
| 11767 | #if defined(__Userspace__) |
| 11768 | ip->ip_id = htons(ip_id++); |
| 11769 | #elif defined(__FreeBSD__) |
| 11770 | ip_fillid(ip); |
| 11771 | #elif defined(__APPLE__) |
| 11772 | #if RANDOM_IP_ID |
| 11773 | ip->ip_id = ip_randomid(); |
| 11774 | #else |
| 11775 | ip->ip_id = htons(ip_id++); |
| 11776 | #endif |
| 11777 | #else |
| 11778 | ip->ip_id = ip_id++; |
| 11779 | #endif |
| 11780 | ip->ip_ttl = MODULE_GLOBAL(ip_defttl); |
| 11781 | if (port) { |
| 11782 | ip->ip_p = IPPROTO_UDP; |
| 11783 | } else { |
| 11784 | ip->ip_p = IPPROTO_SCTP; |
| 11785 | } |
| 11786 | ip->ip_src.s_addr = dst_sin->sin_addr.s_addr; |
| 11787 | ip->ip_dst.s_addr = src_sin->sin_addr.s_addr; |
| 11788 | ip->ip_sum = 0; |
| 11789 | len = sizeof(struct ip); |
| 11790 | shout = (struct sctphdr *)((caddr_t)ip + len); |
| 11791 | break; |
| 11792 | #endif |
| 11793 | #ifdef INET6 |
| 11794 | case AF_INET6: |
| 11795 | src_sin6 = (struct sockaddr_in6 *)src; |
| 11796 | dst_sin6 = (struct sockaddr_in6 *)dst; |
| 11797 | ip6 = mtod(mout, struct ip6_hdr *); |
| 11798 | ip6->ip6_flow = htonl(0x60000000); |
| 11799 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 11800 | if (V_ip6_auto_flowlabel) { |
| 11801 | ip6->ip6_flow |= (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); |
| 11802 | } |
| 11803 | #endif |
| 11804 | #if defined(__Userspace__) |
| 11805 | ip6->ip6_hlim = IPv6_HOP_LIMIT; |
| 11806 | #else |
| 11807 | ip6->ip6_hlim = MODULE_GLOBAL(ip6_defhlim); |
| 11808 | #endif |
| 11809 | if (port) { |
| 11810 | ip6->ip6_nxt = IPPROTO_UDP; |
| 11811 | } else { |
| 11812 | ip6->ip6_nxt = IPPROTO_SCTP; |
| 11813 | } |
| 11814 | ip6->ip6_src = dst_sin6->sin6_addr; |
| 11815 | ip6->ip6_dst = src_sin6->sin6_addr; |
| 11816 | len = sizeof(struct ip6_hdr); |
| 11817 | shout = (struct sctphdr *)((caddr_t)ip6 + len); |
| 11818 | break; |
| 11819 | #endif |
| 11820 | default: |
| 11821 | len = 0; |
| 11822 | shout = mtod(mout, struct sctphdr *); |
| 11823 | break; |
| 11824 | } |
| 11825 | #if defined(INET) || defined(INET6) |
| 11826 | if (port) { |
| 11827 | if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) { |
| 11828 | sctp_m_freem(mout); |
| 11829 | return; |
| 11830 | } |
| 11831 | udp = (struct udphdr *)shout; |
| 11832 | udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)); |
| 11833 | udp->uh_dport = port; |
| 11834 | udp->uh_sum = 0; |
| 11835 | udp->uh_ulen = htons((uint16_t)(sizeof(struct udphdr) + |
| 11836 | sizeof(struct sctphdr) + |
| 11837 | sizeof(struct sctp_chunkhdr) + |
| 11838 | cause_len + padding_len)); |
| 11839 | len += sizeof(struct udphdr); |
| 11840 | shout = (struct sctphdr *)((caddr_t)shout + sizeof(struct udphdr)); |
| 11841 | } else { |
| 11842 | udp = NULL; |
| 11843 | } |
| 11844 | #endif |
| 11845 | shout->src_port = sh->dest_port; |
| 11846 | shout->dest_port = sh->src_port; |
| 11847 | shout->checksum = 0; |
| 11848 | if (vtag) { |
| 11849 | shout->v_tag = htonl(vtag); |
| 11850 | } else { |
| 11851 | shout->v_tag = sh->v_tag; |
| 11852 | } |
| 11853 | len += sizeof(struct sctphdr); |
| 11854 | ch = (struct sctp_chunkhdr *)((caddr_t)shout + sizeof(struct sctphdr)); |
| 11855 | ch->chunk_type = type; |
| 11856 | if (vtag) { |
| 11857 | ch->chunk_flags = 0; |
| 11858 | } else { |
| 11859 | ch->chunk_flags = SCTP_HAD_NO_TCB; |
| 11860 | } |
| 11861 | ch->chunk_length = htons((uint16_t)(sizeof(struct sctp_chunkhdr) + cause_len)); |
| 11862 | len += sizeof(struct sctp_chunkhdr); |
| 11863 | len += cause_len + padding_len; |
| 11864 |
| 11865 | if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) { |
| 11866 | sctp_m_freem(mout); |
| 11867 | return; |
| 11868 | } |
| 11869 | SCTP_ATTACH_CHAIN(o_pak, mout, len); |
| 11870 | switch (dst->sa_family) { |
| 11871 | #ifdef INET |
| 11872 | case AF_INET: |
| 11873 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 11874 | /* zap the stack pointer to the route */ |
| 11875 | memset(&ro, 0, sizeof(sctp_route_t)); |
| 11876 | #endif |
| 11877 | if (port) { |
| 11878 | #if !defined(_WIN32) && !defined(__Userspace__) |
| 11879 | #if defined(__FreeBSD__) |
| 11880 | if (V_udp_cksum) { |
| 11881 | udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP)); |
| 11882 | } else { |
| 11883 | udp->uh_sum = 0; |
| 11884 | } |
| 11885 | #else |
| 11886 | udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP)); |
| 11887 | #endif |
| 11888 | #else |
| 11889 | udp->uh_sum = 0; |
| 11890 | #endif |
| 11891 | } |
| 11892 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 11893 | ip->ip_len = htons(len); |
| 11894 | #elif defined(__APPLE__) || defined(__Userspace__) |
| 11895 | ip->ip_len = len; |
| 11896 | #else |
| 11897 | ip->ip_len = htons(len); |
| 11898 | #endif |
| 11899 | if (port) { |
| 11900 | shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip) + sizeof(struct udphdr)); |
| 11901 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 11902 | #if !defined(_WIN32) && !defined(__Userspace__) |
| 11903 | #if defined(__FreeBSD__) |
| 11904 | if (V_udp_cksum) { |
| 11905 | SCTP_ENABLE_UDP_CSUM(o_pak); |
| 11906 | } |
| 11907 | #else |
| 11908 | SCTP_ENABLE_UDP_CSUM(o_pak); |
| 11909 | #endif |
| 11910 | #endif |
| 11911 | } else { |
| 11912 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 11913 | mout->m_pkthdr.csum_flags = CSUM_SCTP; |
| 11914 | mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum); |
| 11915 | SCTP_STAT_INCR(sctps_sendhwcrc); |
| 11916 | #else |
| 11917 | shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip)); |
| 11918 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 11919 | #endif |
| 11920 | } |
| 11921 | #ifdef SCTP_PACKET_LOGGING |
| 11922 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) { |
| 11923 | sctp_packet_log(o_pak); |
| 11924 | } |
| 11925 | #endif |
| 11926 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 11927 | SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id); |
| 11928 | /* Free the route if we got one back */ |
| 11929 | if (ro.ro_rt) { |
| 11930 | RTFREE(ro.ro_rt); |
| 11931 | ro.ro_rt = NULL; |
| 11932 | } |
| 11933 | #else |
| 11934 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 11935 | SCTP_PROBE5(send, NULL, NULL, ip, NULL, shout); |
| 11936 | #endif |
| 11937 | SCTP_IP_OUTPUT(ret, o_pak, NULL, NULL, vrf_id); |
| 11938 | #endif |
| 11939 | break; |
| 11940 | #endif |
| 11941 | #ifdef INET6 |
| 11942 | case AF_INET6: |
| 11943 | ip6->ip6_plen = htons((uint16_t)(len - sizeof(struct ip6_hdr))); |
| 11944 | if (port) { |
| 11945 | shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr)); |
| 11946 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 11947 | #if !defined(__Userspace__) |
| 11948 | #if defined(_WIN32) |
| 11949 | udp->uh_sum = 0; |
| 11950 | #else |
| 11951 | if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) { |
| 11952 | udp->uh_sum = 0xffff; |
| 11953 | } |
| 11954 | #endif |
| 11955 | #endif |
| 11956 | } else { |
| 11957 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 11958 | mout->m_pkthdr.csum_flags = CSUM_SCTP_IPV6; |
| 11959 | mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum); |
| 11960 | SCTP_STAT_INCR(sctps_sendhwcrc); |
| 11961 | #else |
| 11962 | shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr)); |
| 11963 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 11964 | #endif |
| 11965 | } |
| 11966 | #ifdef SCTP_PACKET_LOGGING |
| 11967 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) { |
| 11968 | sctp_packet_log(o_pak); |
| 11969 | } |
| 11970 | #endif |
| 11971 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 11972 | SCTP_PROBE5(send, NULL, NULL, ip6, NULL, shout); |
| 11973 | #endif |
| 11974 | SCTP_IP6_OUTPUT(ret, o_pak, NULL, NULL, NULL, vrf_id); |
| 11975 | break; |
| 11976 | #endif |
| 11977 | #if defined(__Userspace__) |
| 11978 | case AF_CONN: |
| 11979 | { |
| 11980 | char *buffer; |
| 11981 | struct sockaddr_conn *sconn; |
| 11982 |
| 11983 | sconn = (struct sockaddr_conn *)src; |
| 11984 | if (SCTP_BASE_VAR(crc32c_offloaded) == 0) { |
| 11985 | shout->checksum = sctp_calculate_cksum(mout, 0); |
| 11986 | SCTP_STAT_INCR(sctps_sendswcrc); |
| 11987 | } else { |
| 11988 | SCTP_STAT_INCR(sctps_sendhwcrc); |
| 11989 | } |
| 11990 | #ifdef SCTP_PACKET_LOGGING |
| 11991 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) { |
| 11992 | sctp_packet_log(mout); |
| 11993 | } |
| 11994 | #endif |
| 11995 | /* Don't alloc/free for each packet */ |
| 11996 | if ((buffer = malloc(len)) != NULL) { |
| 11997 | m_copydata(mout, 0, len, buffer); |
| 11998 | ret = SCTP_BASE_VAR(conn_output)(sconn->sconn_addr, buffer, len, 0, 0); |
| 11999 | free(buffer); |
| 12000 | } else { |
| 12001 | ret = ENOMEM; |
| 12002 | } |
| 12003 | sctp_m_freem(mout); |
| 12004 | break; |
| 12005 | } |
| 12006 | #endif |
| 12007 | default: |
| 12008 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n", |
| 12009 | dst->sa_family); |
| 12010 | sctp_m_freem(mout); |
| 12011 | SCTP_LTRACE_ERR_RET_PKT(mout, NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT); |
| 12012 | return; |
| 12013 | } |
| 12014 | SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret); |
| 12015 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 12016 | if (port) { |
| 12017 | UDPSTAT_INC(udps_opackets); |
| 12018 | } |
| 12019 | #endif |
| 12020 | SCTP_STAT_INCR(sctps_sendpackets); |
| 12021 | SCTP_STAT_INCR_COUNTER64(sctps_outpackets); |
| 12022 | SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks); |
| 12023 | if (ret) { |
| 12024 | SCTP_STAT_INCR(sctps_senderrors); |
| 12025 | } |
| 12026 | return; |
| 12027 | } |
| 12028 |
| 12029 | void |
| 12030 | sctp_send_shutdown_complete2(struct sockaddr *src, struct sockaddr *dst, |
| 12031 | struct sctphdr *sh, |
| 12032 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 12033 | uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum, |
| 12034 | #endif |
| 12035 | uint32_t vrf_id, uint16_t port) |
| 12036 | { |
| 12037 | sctp_send_resp_msg(src, dst, sh, 0, SCTP_SHUTDOWN_COMPLETE, NULL, |
| 12038 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 12039 | mflowtype, mflowid, fibnum, |
| 12040 | #endif |
| 12041 | vrf_id, port); |
| 12042 | } |
| 12043 |
| 12044 | void |
| 12045 | sctp_send_hb(struct sctp_tcb *stcb, struct sctp_nets *net,int so_locked) |
| 12046 | { |
| 12047 | struct sctp_tmit_chunk *chk; |
| 12048 | struct sctp_heartbeat_chunk *hb; |
| 12049 | struct timeval now; |
| 12050 |
| 12051 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 12052 | if (net == NULL) { |
| 12053 | return; |
| 12054 | } |
| 12055 | (void)SCTP_GETTIME_TIMEVAL(&now); |
| 12056 | switch (net->ro._l_addr.sa.sa_family) { |
| 12057 | #ifdef INET |
| 12058 | case AF_INET: |
| 12059 | break; |
| 12060 | #endif |
| 12061 | #ifdef INET6 |
| 12062 | case AF_INET6: |
| 12063 | break; |
| 12064 | #endif |
| 12065 | #if defined(__Userspace__) |
| 12066 | case AF_CONN: |
| 12067 | break; |
| 12068 | #endif |
| 12069 | default: |
| 12070 | return; |
| 12071 | } |
| 12072 | sctp_alloc_a_chunk(stcb, chk); |
| 12073 | if (chk == NULL) { |
| 12074 | SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n"); |
| 12075 | return; |
| 12076 | } |
| 12077 |
| 12078 | chk->copy_by_ref = 0; |
| 12079 | chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST; |
| 12080 | chk->rec.chunk_id.can_take_data = 1; |
| 12081 | chk->flags = 0; |
| 12082 | chk->asoc = &stcb->asoc; |
| 12083 | chk->send_size = sizeof(struct sctp_heartbeat_chunk); |
| 12084 |
| 12085 | chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER); |
| 12086 | if (chk->data == NULL) { |
| 12087 | sctp_free_a_chunk(stcb, chk, so_locked); |
| 12088 | return; |
| 12089 | } |
| 12090 | SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); |
| 12091 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12092 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 12093 | chk->snd_count = 0; |
| 12094 | chk->whoTo = net; |
| 12095 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 12096 | /* Now we have a mbuf that we can fill in with the details */ |
| 12097 | hb = mtod(chk->data, struct sctp_heartbeat_chunk *); |
| 12098 | memset(hb, 0, sizeof(struct sctp_heartbeat_chunk)); |
| 12099 | /* fill out chunk header */ |
| 12100 | hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST; |
| 12101 | hb->ch.chunk_flags = 0; |
| 12102 | hb->ch.chunk_length = htons(chk->send_size); |
| 12103 | /* Fill out hb parameter */ |
| 12104 | hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO); |
| 12105 | hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param)); |
| 12106 | hb->heartbeat.hb_info.time_value_1 = (uint32_t)now.tv_sec; |
| 12107 | hb->heartbeat.hb_info.time_value_2 = now.tv_usec; |
| 12108 | /* Did our user request this one, put it in */ |
| 12109 | hb->heartbeat.hb_info.addr_family = (uint8_t)net->ro._l_addr.sa.sa_family; |
| 12110 | #ifdef HAVE_SA_LEN |
| 12111 | hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len; |
| 12112 | #else |
| 12113 | switch (net->ro._l_addr.sa.sa_family) { |
| 12114 | #ifdef INET |
| 12115 | case AF_INET: |
| 12116 | hb->heartbeat.hb_info.addr_len = sizeof(struct sockaddr_in); |
| 12117 | break; |
| 12118 | #endif |
| 12119 | #ifdef INET6 |
| 12120 | case AF_INET6: |
| 12121 | hb->heartbeat.hb_info.addr_len = sizeof(struct sockaddr_in6); |
| 12122 | break; |
| 12123 | #endif |
| 12124 | #if defined(__Userspace__) |
| 12125 | case AF_CONN: |
| 12126 | hb->heartbeat.hb_info.addr_len = sizeof(struct sockaddr_conn); |
| 12127 | break; |
| 12128 | #endif |
| 12129 | default: |
| 12130 | hb->heartbeat.hb_info.addr_len = 0; |
| 12131 | break; |
| 12132 | } |
| 12133 | #endif |
| 12134 | if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { |
| 12135 | /* |
| 12136 | * we only take from the entropy pool if the address is not |
| 12137 | * confirmed. |
| 12138 | */ |
| 12139 | net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep); |
| 12140 | net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep); |
| 12141 | } else { |
| 12142 | net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0; |
| 12143 | net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0; |
| 12144 | } |
| 12145 | switch (net->ro._l_addr.sa.sa_family) { |
| 12146 | #ifdef INET |
| 12147 | case AF_INET: |
| 12148 | memcpy(hb->heartbeat.hb_info.address, |
| 12149 | &net->ro._l_addr.sin.sin_addr, |
| 12150 | sizeof(net->ro._l_addr.sin.sin_addr)); |
| 12151 | break; |
| 12152 | #endif |
| 12153 | #ifdef INET6 |
| 12154 | case AF_INET6: |
| 12155 | memcpy(hb->heartbeat.hb_info.address, |
| 12156 | &net->ro._l_addr.sin6.sin6_addr, |
| 12157 | sizeof(net->ro._l_addr.sin6.sin6_addr)); |
| 12158 | break; |
| 12159 | #endif |
| 12160 | #if defined(__Userspace__) |
| 12161 | case AF_CONN: |
| 12162 | memcpy(hb->heartbeat.hb_info.address, |
| 12163 | &net->ro._l_addr.sconn.sconn_addr, |
| 12164 | sizeof(net->ro._l_addr.sconn.sconn_addr)); |
| 12165 | break; |
| 12166 | #endif |
| 12167 | default: |
| 12168 | if (chk->data) { |
| 12169 | sctp_m_freem(chk->data); |
| 12170 | chk->data = NULL; |
| 12171 | } |
| 12172 | sctp_free_a_chunk(stcb, chk, so_locked); |
| 12173 | return; |
| 12174 | break; |
| 12175 | } |
| 12176 | net->hb_responded = 0; |
| 12177 | TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next); |
| 12178 | stcb->asoc.ctrl_queue_cnt++; |
| 12179 | SCTP_STAT_INCR(sctps_sendheartbeat); |
| 12180 | return; |
| 12181 | } |
| 12182 |
| 12183 | void |
| 12184 | sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net, |
| 12185 | uint32_t high_tsn) |
| 12186 | { |
| 12187 | struct sctp_association *asoc; |
| 12188 | struct sctp_ecne_chunk *ecne; |
| 12189 | struct sctp_tmit_chunk *chk; |
| 12190 |
| 12191 | if (net == NULL) { |
| 12192 | return; |
| 12193 | } |
| 12194 | asoc = &stcb->asoc; |
| 12195 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 12196 | TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { |
| 12197 | if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) { |
| 12198 | /* found a previous ECN_ECHO update it if needed */ |
| 12199 | uint32_t cnt, ctsn; |
| 12200 | ecne = mtod(chk->data, struct sctp_ecne_chunk *); |
| 12201 | ctsn = ntohl(ecne->tsn); |
| 12202 | if (SCTP_TSN_GT(high_tsn, ctsn)) { |
| 12203 | ecne->tsn = htonl(high_tsn); |
| 12204 | SCTP_STAT_INCR(sctps_queue_upd_ecne); |
| 12205 | } |
| 12206 | cnt = ntohl(ecne->num_pkts_since_cwr); |
| 12207 | cnt++; |
| 12208 | ecne->num_pkts_since_cwr = htonl(cnt); |
| 12209 | return; |
| 12210 | } |
| 12211 | } |
| 12212 | /* nope could not find one to update so we must build one */ |
| 12213 | sctp_alloc_a_chunk(stcb, chk); |
| 12214 | if (chk == NULL) { |
| 12215 | return; |
| 12216 | } |
| 12217 | SCTP_STAT_INCR(sctps_queue_upd_ecne); |
| 12218 | chk->copy_by_ref = 0; |
| 12219 | chk->rec.chunk_id.id = SCTP_ECN_ECHO; |
| 12220 | chk->rec.chunk_id.can_take_data = 0; |
| 12221 | chk->flags = 0; |
| 12222 | chk->asoc = &stcb->asoc; |
| 12223 | chk->send_size = sizeof(struct sctp_ecne_chunk); |
| 12224 | chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER); |
| 12225 | if (chk->data == NULL) { |
| 12226 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 12227 | return; |
| 12228 | } |
| 12229 | SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); |
| 12230 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12231 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 12232 | chk->snd_count = 0; |
| 12233 | chk->whoTo = net; |
| 12234 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 12235 |
| 12236 | stcb->asoc.ecn_echo_cnt_onq++; |
| 12237 | ecne = mtod(chk->data, struct sctp_ecne_chunk *); |
| 12238 | ecne->ch.chunk_type = SCTP_ECN_ECHO; |
| 12239 | ecne->ch.chunk_flags = 0; |
| 12240 | ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk)); |
| 12241 | ecne->tsn = htonl(high_tsn); |
| 12242 | ecne->num_pkts_since_cwr = htonl(1); |
| 12243 | TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next); |
| 12244 | asoc->ctrl_queue_cnt++; |
| 12245 | } |
| 12246 |
| 12247 | void |
| 12248 | sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net, |
| 12249 | struct mbuf *m, int len, int iphlen, int bad_crc) |
| 12250 | { |
| 12251 | struct sctp_association *asoc; |
| 12252 | struct sctp_pktdrop_chunk *drp; |
| 12253 | struct sctp_tmit_chunk *chk; |
| 12254 | uint8_t *datap; |
| 12255 | int was_trunc = 0; |
| 12256 | int fullsz = 0; |
| 12257 | long spc; |
| 12258 | int offset; |
| 12259 | struct sctp_chunkhdr *ch, chunk_buf; |
| 12260 | unsigned int chk_length; |
| 12261 |
| 12262 | if (!stcb) { |
| 12263 | return; |
| 12264 | } |
| 12265 | asoc = &stcb->asoc; |
| 12266 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 12267 | if (asoc->pktdrop_supported == 0) { |
| 12268 | /*- |
| 12269 | * peer must declare support before I send one. |
| 12270 | */ |
| 12271 | return; |
| 12272 | } |
| 12273 | if (stcb->sctp_socket == NULL) { |
| 12274 | return; |
| 12275 | } |
| 12276 | sctp_alloc_a_chunk(stcb, chk); |
| 12277 | if (chk == NULL) { |
| 12278 | return; |
| 12279 | } |
| 12280 | chk->copy_by_ref = 0; |
| 12281 | chk->rec.chunk_id.id = SCTP_PACKET_DROPPED; |
| 12282 | chk->rec.chunk_id.can_take_data = 1; |
| 12283 | chk->flags = 0; |
| 12284 | len -= iphlen; |
| 12285 | chk->send_size = len; |
| 12286 | /* Validate that we do not have an ABORT in here. */ |
| 12287 | offset = iphlen + sizeof(struct sctphdr); |
| 12288 | ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, |
| 12289 | sizeof(*ch), (uint8_t *) & chunk_buf); |
| 12290 | while (ch != NULL) { |
| 12291 | chk_length = ntohs(ch->chunk_length); |
| 12292 | if (chk_length < sizeof(*ch)) { |
| 12293 | /* break to abort land */ |
| 12294 | break; |
| 12295 | } |
| 12296 | switch (ch->chunk_type) { |
| 12297 | case SCTP_PACKET_DROPPED: |
| 12298 | case SCTP_ABORT_ASSOCIATION: |
| 12299 | case SCTP_INITIATION_ACK: |
| 12300 | /** |
| 12301 | * We don't respond with an PKT-DROP to an ABORT |
| 12302 | * or PKT-DROP. We also do not respond to an |
| 12303 | * INIT-ACK, because we can't know if the initiation |
| 12304 | * tag is correct or not. |
| 12305 | */ |
| 12306 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 12307 | return; |
| 12308 | default: |
| 12309 | break; |
| 12310 | } |
| 12311 | offset += SCTP_SIZE32(chk_length); |
| 12312 | ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, |
| 12313 | sizeof(*ch), (uint8_t *) & chunk_buf); |
| 12314 | } |
| 12315 |
| 12316 | if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) > |
| 12317 | min(stcb->asoc.smallest_mtu, MCLBYTES)) { |
| 12318 | /* only send 1 mtu worth, trim off the |
| 12319 | * excess on the end. |
| 12320 | */ |
| 12321 | fullsz = len; |
| 12322 | len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD; |
| 12323 | was_trunc = 1; |
| 12324 | } |
| 12325 | chk->asoc = &stcb->asoc; |
| 12326 | chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); |
| 12327 | if (chk->data == NULL) { |
| 12328 | jump_out: |
| 12329 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 12330 | return; |
| 12331 | } |
| 12332 | SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); |
| 12333 | drp = mtod(chk->data, struct sctp_pktdrop_chunk *); |
| 12334 | if (drp == NULL) { |
| 12335 | sctp_m_freem(chk->data); |
| 12336 | chk->data = NULL; |
| 12337 | goto jump_out; |
| 12338 | } |
| 12339 | chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) + |
| 12340 | sizeof(struct sctphdr) + SCTP_MED_OVERHEAD)); |
| 12341 | chk->book_size_scale = 0; |
| 12342 | if (was_trunc) { |
| 12343 | drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED; |
| 12344 | drp->trunc_len = htons(fullsz); |
| 12345 | /* Len is already adjusted to size minus overhead above |
| 12346 | * take out the pkt_drop chunk itself from it. |
| 12347 | */ |
| 12348 | chk->send_size = (uint16_t)(len - sizeof(struct sctp_pktdrop_chunk)); |
| 12349 | len = chk->send_size; |
| 12350 | } else { |
| 12351 | /* no truncation needed */ |
| 12352 | drp->ch.chunk_flags = 0; |
| 12353 | drp->trunc_len = htons(0); |
| 12354 | } |
| 12355 | if (bad_crc) { |
| 12356 | drp->ch.chunk_flags |= SCTP_BADCRC; |
| 12357 | } |
| 12358 | chk->send_size += sizeof(struct sctp_pktdrop_chunk); |
| 12359 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12360 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 12361 | chk->snd_count = 0; |
| 12362 | if (net) { |
| 12363 | /* we should hit here */ |
| 12364 | chk->whoTo = net; |
| 12365 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 12366 | } else { |
| 12367 | chk->whoTo = NULL; |
| 12368 | } |
| 12369 | drp->ch.chunk_type = SCTP_PACKET_DROPPED; |
| 12370 | drp->ch.chunk_length = htons(chk->send_size); |
| 12371 | spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket); |
| 12372 | if (spc < 0) { |
| 12373 | spc = 0; |
| 12374 | } |
| 12375 | drp->bottle_bw = htonl(spc); |
| 12376 | if (asoc->my_rwnd) { |
| 12377 | drp->current_onq = htonl(asoc->size_on_reasm_queue + |
| 12378 | asoc->size_on_all_streams + |
| 12379 | asoc->my_rwnd_control_len + |
| 12380 | stcb->sctp_socket->so_rcv.sb_cc); |
| 12381 | } else { |
| 12382 | /*- |
| 12383 | * If my rwnd is 0, possibly from mbuf depletion as well as |
| 12384 | * space used, tell the peer there is NO space aka onq == bw |
| 12385 | */ |
| 12386 | drp->current_onq = htonl(spc); |
| 12387 | } |
| 12388 | drp->reserved = 0; |
| 12389 | datap = drp->data; |
| 12390 | m_copydata(m, iphlen, len, (caddr_t)datap); |
| 12391 | TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next); |
| 12392 | asoc->ctrl_queue_cnt++; |
| 12393 | } |
| 12394 |
| 12395 | void |
| 12396 | sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override) |
| 12397 | { |
| 12398 | struct sctp_association *asoc; |
| 12399 | struct sctp_cwr_chunk *cwr; |
| 12400 | struct sctp_tmit_chunk *chk; |
| 12401 |
| 12402 | SCTP_TCB_LOCK_ASSERT(stcb); |
| 12403 | if (net == NULL) { |
| 12404 | return; |
| 12405 | } |
| 12406 | asoc = &stcb->asoc; |
| 12407 | TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) { |
| 12408 | if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) { |
| 12409 | /* found a previous CWR queued to same destination update it if needed */ |
| 12410 | uint32_t ctsn; |
| 12411 | cwr = mtod(chk->data, struct sctp_cwr_chunk *); |
| 12412 | ctsn = ntohl(cwr->tsn); |
| 12413 | if (SCTP_TSN_GT(high_tsn, ctsn)) { |
| 12414 | cwr->tsn = htonl(high_tsn); |
| 12415 | } |
| 12416 | if (override & SCTP_CWR_REDUCE_OVERRIDE) { |
| 12417 | /* Make sure override is carried */ |
| 12418 | cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE; |
| 12419 | } |
| 12420 | return; |
| 12421 | } |
| 12422 | } |
| 12423 | sctp_alloc_a_chunk(stcb, chk); |
| 12424 | if (chk == NULL) { |
| 12425 | return; |
| 12426 | } |
| 12427 | chk->copy_by_ref = 0; |
| 12428 | chk->rec.chunk_id.id = SCTP_ECN_CWR; |
| 12429 | chk->rec.chunk_id.can_take_data = 1; |
| 12430 | chk->flags = 0; |
| 12431 | chk->asoc = &stcb->asoc; |
| 12432 | chk->send_size = sizeof(struct sctp_cwr_chunk); |
| 12433 | chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER); |
| 12434 | if (chk->data == NULL) { |
| 12435 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 12436 | return; |
| 12437 | } |
| 12438 | SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); |
| 12439 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12440 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 12441 | chk->snd_count = 0; |
| 12442 | chk->whoTo = net; |
| 12443 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 12444 | cwr = mtod(chk->data, struct sctp_cwr_chunk *); |
| 12445 | cwr->ch.chunk_type = SCTP_ECN_CWR; |
| 12446 | cwr->ch.chunk_flags = override; |
| 12447 | cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk)); |
| 12448 | cwr->tsn = htonl(high_tsn); |
| 12449 | TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next); |
| 12450 | asoc->ctrl_queue_cnt++; |
| 12451 | } |
| 12452 |
| 12453 | static int |
| 12454 | sctp_add_stream_reset_out(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, |
| 12455 | uint32_t seq, uint32_t resp_seq, uint32_t last_sent) |
| 12456 | { |
| 12457 | uint16_t len, old_len, i; |
| 12458 | struct sctp_stream_reset_out_request *req_out; |
| 12459 | struct sctp_chunkhdr *ch; |
| 12460 | int at; |
| 12461 | int number_entries=0; |
| 12462 |
| 12463 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12464 | old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); |
| 12465 | /* get to new offset for the param. */ |
| 12466 | req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len); |
| 12467 | /* now how long will this param be? */ |
| 12468 | for (i = 0; i < stcb->asoc.streamoutcnt; i++) { |
| 12469 | if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) && |
| 12470 | (stcb->asoc.strmout[i].chunks_on_queues == 0) && |
| 12471 | TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) { |
| 12472 | number_entries++; |
| 12473 | } |
| 12474 | } |
| 12475 | if (number_entries == 0) { |
| 12476 | return (0); |
| 12477 | } |
| 12478 | if (number_entries == stcb->asoc.streamoutcnt) { |
| 12479 | number_entries = 0; |
| 12480 | } |
| 12481 | if (number_entries > SCTP_MAX_STREAMS_AT_ONCE_RESET) { |
| 12482 | number_entries = SCTP_MAX_STREAMS_AT_ONCE_RESET; |
| 12483 | } |
| 12484 | len = (uint16_t)(sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries)); |
| 12485 | req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST); |
| 12486 | req_out->ph.param_length = htons(len); |
| 12487 | req_out->request_seq = htonl(seq); |
| 12488 | req_out->response_seq = htonl(resp_seq); |
| 12489 | req_out->send_reset_at_tsn = htonl(last_sent); |
| 12490 | at = 0; |
| 12491 | if (number_entries) { |
| 12492 | for (i = 0; i < stcb->asoc.streamoutcnt; i++) { |
| 12493 | if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) && |
| 12494 | (stcb->asoc.strmout[i].chunks_on_queues == 0) && |
| 12495 | TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) { |
| 12496 | req_out->list_of_streams[at] = htons(i); |
| 12497 | at++; |
| 12498 | stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT; |
| 12499 | if (at >= number_entries) { |
| 12500 | break; |
| 12501 | } |
| 12502 | } |
| 12503 | } |
| 12504 | } else { |
| 12505 | for (i = 0; i < stcb->asoc.streamoutcnt; i++) { |
| 12506 | stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT; |
| 12507 | } |
| 12508 | } |
| 12509 | if (SCTP_SIZE32(len) > len) { |
| 12510 | /*- |
| 12511 | * Need to worry about the pad we may end up adding to the |
| 12512 | * end. This is easy since the struct is either aligned to 4 |
| 12513 | * bytes or 2 bytes off. |
| 12514 | */ |
| 12515 | req_out->list_of_streams[number_entries] = 0; |
| 12516 | } |
| 12517 | /* now fix the chunk length */ |
| 12518 | ch->chunk_length = htons(len + old_len); |
| 12519 | chk->book_size = len + old_len; |
| 12520 | chk->book_size_scale = 0; |
| 12521 | chk->send_size = SCTP_SIZE32(chk->book_size); |
| 12522 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12523 | return (1); |
| 12524 | } |
| 12525 |
| 12526 | static void |
| 12527 | sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk, |
| 12528 | int number_entries, uint16_t *list, |
| 12529 | uint32_t seq) |
| 12530 | { |
| 12531 | uint16_t len, old_len, i; |
| 12532 | struct sctp_stream_reset_in_request *req_in; |
| 12533 | struct sctp_chunkhdr *ch; |
| 12534 |
| 12535 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12536 | old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); |
| 12537 |
| 12538 | /* get to new offset for the param. */ |
| 12539 | req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len); |
| 12540 | /* now how long will this param be? */ |
| 12541 | len = (uint16_t)(sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries)); |
| 12542 | req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST); |
| 12543 | req_in->ph.param_length = htons(len); |
| 12544 | req_in->request_seq = htonl(seq); |
| 12545 | if (number_entries) { |
| 12546 | for (i = 0; i < number_entries; i++) { |
| 12547 | req_in->list_of_streams[i] = htons(list[i]); |
| 12548 | } |
| 12549 | } |
| 12550 | if (SCTP_SIZE32(len) > len) { |
| 12551 | /*- |
| 12552 | * Need to worry about the pad we may end up adding to the |
| 12553 | * end. This is easy since the struct is either aligned to 4 |
| 12554 | * bytes or 2 bytes off. |
| 12555 | */ |
| 12556 | req_in->list_of_streams[number_entries] = 0; |
| 12557 | } |
| 12558 | /* now fix the chunk length */ |
| 12559 | ch->chunk_length = htons(len + old_len); |
| 12560 | chk->book_size = len + old_len; |
| 12561 | chk->book_size_scale = 0; |
| 12562 | chk->send_size = SCTP_SIZE32(chk->book_size); |
| 12563 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12564 | return; |
| 12565 | } |
| 12566 |
| 12567 | static void |
| 12568 | sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk, |
| 12569 | uint32_t seq) |
| 12570 | { |
| 12571 | uint16_t len, old_len; |
| 12572 | struct sctp_stream_reset_tsn_request *req_tsn; |
| 12573 | struct sctp_chunkhdr *ch; |
| 12574 |
| 12575 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12576 | old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); |
| 12577 |
| 12578 | /* get to new offset for the param. */ |
| 12579 | req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len); |
| 12580 | /* now how long will this param be? */ |
| 12581 | len = sizeof(struct sctp_stream_reset_tsn_request); |
| 12582 | req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST); |
| 12583 | req_tsn->ph.param_length = htons(len); |
| 12584 | req_tsn->request_seq = htonl(seq); |
| 12585 |
| 12586 | /* now fix the chunk length */ |
| 12587 | ch->chunk_length = htons(len + old_len); |
| 12588 | chk->send_size = len + old_len; |
| 12589 | chk->book_size = SCTP_SIZE32(chk->send_size); |
| 12590 | chk->book_size_scale = 0; |
| 12591 | SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); |
| 12592 | return; |
| 12593 | } |
| 12594 |
| 12595 | void |
| 12596 | sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk, |
| 12597 | uint32_t resp_seq, uint32_t result) |
| 12598 | { |
| 12599 | uint16_t len, old_len; |
| 12600 | struct sctp_stream_reset_response *resp; |
| 12601 | struct sctp_chunkhdr *ch; |
| 12602 |
| 12603 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12604 | old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); |
| 12605 |
| 12606 | /* get to new offset for the param. */ |
| 12607 | resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len); |
| 12608 | /* now how long will this param be? */ |
| 12609 | len = sizeof(struct sctp_stream_reset_response); |
| 12610 | resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE); |
| 12611 | resp->ph.param_length = htons(len); |
| 12612 | resp->response_seq = htonl(resp_seq); |
| 12613 | resp->result = ntohl(result); |
| 12614 |
| 12615 | /* now fix the chunk length */ |
| 12616 | ch->chunk_length = htons(len + old_len); |
| 12617 | chk->book_size = len + old_len; |
| 12618 | chk->book_size_scale = 0; |
| 12619 | chk->send_size = SCTP_SIZE32(chk->book_size); |
| 12620 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12621 | return; |
| 12622 | } |
| 12623 |
| 12624 | void |
| 12625 | sctp_send_deferred_reset_response(struct sctp_tcb *stcb, |
| 12626 | struct sctp_stream_reset_list *ent, |
| 12627 | int response) |
| 12628 | { |
| 12629 | struct sctp_association *asoc; |
| 12630 | struct sctp_tmit_chunk *chk; |
| 12631 | struct sctp_chunkhdr *ch; |
| 12632 |
| 12633 | asoc = &stcb->asoc; |
| 12634 |
| 12635 | /* |
| 12636 | * Reset our last reset action to the new one IP -> response |
| 12637 | * (PERFORMED probably). This assures that if we fail to send, a |
| 12638 | * retran from the peer will get the new response. |
| 12639 | */ |
| 12640 | asoc->last_reset_action[0] = response; |
| 12641 | if (asoc->stream_reset_outstanding) { |
| 12642 | return; |
| 12643 | } |
| 12644 | sctp_alloc_a_chunk(stcb, chk); |
| 12645 | if (chk == NULL) { |
| 12646 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 12647 | return; |
| 12648 | } |
| 12649 | chk->copy_by_ref = 0; |
| 12650 | chk->rec.chunk_id.id = SCTP_STREAM_RESET; |
| 12651 | chk->rec.chunk_id.can_take_data = 0; |
| 12652 | chk->flags = 0; |
| 12653 | chk->asoc = &stcb->asoc; |
| 12654 | chk->book_size = sizeof(struct sctp_chunkhdr); |
| 12655 | chk->send_size = SCTP_SIZE32(chk->book_size); |
| 12656 | chk->book_size_scale = 0; |
| 12657 | chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); |
| 12658 | if (chk->data == NULL) { |
| 12659 | sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED); |
| 12660 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 12661 | return; |
| 12662 | } |
| 12663 | SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); |
| 12664 | /* setup chunk parameters */ |
| 12665 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 12666 | chk->snd_count = 0; |
| 12667 | if (stcb->asoc.alternate) { |
| 12668 | chk->whoTo = stcb->asoc.alternate; |
| 12669 | } else { |
| 12670 | chk->whoTo = stcb->asoc.primary_destination; |
| 12671 | } |
| 12672 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12673 | ch->chunk_type = SCTP_STREAM_RESET; |
| 12674 | ch->chunk_flags = 0; |
| 12675 | ch->chunk_length = htons(chk->book_size); |
| 12676 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 12677 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12678 | sctp_add_stream_reset_result(chk, ent->seq, response); |
| 12679 | /* insert the chunk for sending */ |
| 12680 | TAILQ_INSERT_TAIL(&asoc->control_send_queue, |
| 12681 | chk, |
| 12682 | sctp_next); |
| 12683 | asoc->ctrl_queue_cnt++; |
| 12684 | } |
| 12685 |
| 12686 | void |
| 12687 | sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk, |
| 12688 | uint32_t resp_seq, uint32_t result, |
| 12689 | uint32_t send_una, uint32_t recv_next) |
| 12690 | { |
| 12691 | uint16_t len, old_len; |
| 12692 | struct sctp_stream_reset_response_tsn *resp; |
| 12693 | struct sctp_chunkhdr *ch; |
| 12694 |
| 12695 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12696 | old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); |
| 12697 |
| 12698 | /* get to new offset for the param. */ |
| 12699 | resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len); |
| 12700 | /* now how long will this param be? */ |
| 12701 | len = sizeof(struct sctp_stream_reset_response_tsn); |
| 12702 | resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE); |
| 12703 | resp->ph.param_length = htons(len); |
| 12704 | resp->response_seq = htonl(resp_seq); |
| 12705 | resp->result = htonl(result); |
| 12706 | resp->senders_next_tsn = htonl(send_una); |
| 12707 | resp->receivers_next_tsn = htonl(recv_next); |
| 12708 |
| 12709 | /* now fix the chunk length */ |
| 12710 | ch->chunk_length = htons(len + old_len); |
| 12711 | chk->book_size = len + old_len; |
| 12712 | chk->send_size = SCTP_SIZE32(chk->book_size); |
| 12713 | chk->book_size_scale = 0; |
| 12714 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12715 | return; |
| 12716 | } |
| 12717 |
| 12718 | static void |
| 12719 | sctp_add_an_out_stream(struct sctp_tmit_chunk *chk, |
| 12720 | uint32_t seq, |
| 12721 | uint16_t adding) |
| 12722 | { |
| 12723 | uint16_t len, old_len; |
| 12724 | struct sctp_chunkhdr *ch; |
| 12725 | struct sctp_stream_reset_add_strm *addstr; |
| 12726 |
| 12727 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12728 | old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); |
| 12729 |
| 12730 | /* get to new offset for the param. */ |
| 12731 | addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len); |
| 12732 | /* now how long will this param be? */ |
| 12733 | len = sizeof(struct sctp_stream_reset_add_strm); |
| 12734 |
| 12735 | /* Fill it out. */ |
| 12736 | addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_OUT_STREAMS); |
| 12737 | addstr->ph.param_length = htons(len); |
| 12738 | addstr->request_seq = htonl(seq); |
| 12739 | addstr->number_of_streams = htons(adding); |
| 12740 | addstr->reserved = 0; |
| 12741 |
| 12742 | /* now fix the chunk length */ |
| 12743 | ch->chunk_length = htons(len + old_len); |
| 12744 | chk->send_size = len + old_len; |
| 12745 | chk->book_size = SCTP_SIZE32(chk->send_size); |
| 12746 | chk->book_size_scale = 0; |
| 12747 | SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); |
| 12748 | return; |
| 12749 | } |
| 12750 |
| 12751 | static void |
| 12752 | sctp_add_an_in_stream(struct sctp_tmit_chunk *chk, |
| 12753 | uint32_t seq, |
| 12754 | uint16_t adding) |
| 12755 | { |
| 12756 | uint16_t len, old_len; |
| 12757 | struct sctp_chunkhdr *ch; |
| 12758 | struct sctp_stream_reset_add_strm *addstr; |
| 12759 |
| 12760 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12761 | old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length)); |
| 12762 |
| 12763 | /* get to new offset for the param. */ |
| 12764 | addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len); |
| 12765 | /* now how long will this param be? */ |
| 12766 | len = sizeof(struct sctp_stream_reset_add_strm); |
| 12767 | /* Fill it out. */ |
| 12768 | addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_IN_STREAMS); |
| 12769 | addstr->ph.param_length = htons(len); |
| 12770 | addstr->request_seq = htonl(seq); |
| 12771 | addstr->number_of_streams = htons(adding); |
| 12772 | addstr->reserved = 0; |
| 12773 |
| 12774 | /* now fix the chunk length */ |
| 12775 | ch->chunk_length = htons(len + old_len); |
| 12776 | chk->send_size = len + old_len; |
| 12777 | chk->book_size = SCTP_SIZE32(chk->send_size); |
| 12778 | chk->book_size_scale = 0; |
| 12779 | SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); |
| 12780 | return; |
| 12781 | } |
| 12782 |
| 12783 | int |
| 12784 | sctp_send_stream_reset_out_if_possible(struct sctp_tcb *stcb, int so_locked) |
| 12785 | { |
| 12786 | struct sctp_association *asoc; |
| 12787 | struct sctp_tmit_chunk *chk; |
| 12788 | struct sctp_chunkhdr *ch; |
| 12789 | uint32_t seq; |
| 12790 |
| 12791 | asoc = &stcb->asoc; |
| 12792 | asoc->trigger_reset = 0; |
| 12793 | if (asoc->stream_reset_outstanding) { |
| 12794 | return (EALREADY); |
| 12795 | } |
| 12796 | sctp_alloc_a_chunk(stcb, chk); |
| 12797 | if (chk == NULL) { |
| 12798 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 12799 | return (ENOMEM); |
| 12800 | } |
| 12801 | chk->copy_by_ref = 0; |
| 12802 | chk->rec.chunk_id.id = SCTP_STREAM_RESET; |
| 12803 | chk->rec.chunk_id.can_take_data = 0; |
| 12804 | chk->flags = 0; |
| 12805 | chk->asoc = &stcb->asoc; |
| 12806 | chk->book_size = sizeof(struct sctp_chunkhdr); |
| 12807 | chk->send_size = SCTP_SIZE32(chk->book_size); |
| 12808 | chk->book_size_scale = 0; |
| 12809 | chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); |
| 12810 | if (chk->data == NULL) { |
| 12811 | sctp_free_a_chunk(stcb, chk, so_locked); |
| 12812 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 12813 | return (ENOMEM); |
| 12814 | } |
| 12815 | SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); |
| 12816 |
| 12817 | /* setup chunk parameters */ |
| 12818 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 12819 | chk->snd_count = 0; |
| 12820 | if (stcb->asoc.alternate) { |
| 12821 | chk->whoTo = stcb->asoc.alternate; |
| 12822 | } else { |
| 12823 | chk->whoTo = stcb->asoc.primary_destination; |
| 12824 | } |
| 12825 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12826 | ch->chunk_type = SCTP_STREAM_RESET; |
| 12827 | ch->chunk_flags = 0; |
| 12828 | ch->chunk_length = htons(chk->book_size); |
| 12829 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 12830 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12831 | seq = stcb->asoc.str_reset_seq_out; |
| 12832 | if (sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1))) { |
| 12833 | seq++; |
| 12834 | asoc->stream_reset_outstanding++; |
| 12835 | } else { |
| 12836 | m_freem(chk->data); |
| 12837 | chk->data = NULL; |
| 12838 | sctp_free_a_chunk(stcb, chk, so_locked); |
| 12839 | return (ENOENT); |
| 12840 | } |
| 12841 | asoc->str_reset = chk; |
| 12842 | /* insert the chunk for sending */ |
| 12843 | TAILQ_INSERT_TAIL(&asoc->control_send_queue, |
| 12844 | chk, |
| 12845 | sctp_next); |
| 12846 | asoc->ctrl_queue_cnt++; |
| 12847 |
| 12848 | if (stcb->asoc.send_sack) { |
| 12849 | sctp_send_sack(stcb, so_locked); |
| 12850 | } |
| 12851 | sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); |
| 12852 | return (0); |
| 12853 | } |
| 12854 |
| 12855 | int |
| 12856 | sctp_send_str_reset_req(struct sctp_tcb *stcb, |
| 12857 | uint16_t number_entries, uint16_t *list, |
| 12858 | uint8_t send_in_req, |
| 12859 | uint8_t send_tsn_req, |
| 12860 | uint8_t add_stream, |
| 12861 | uint16_t adding_o, |
| 12862 | uint16_t adding_i, uint8_t peer_asked) |
| 12863 | { |
| 12864 | struct sctp_association *asoc; |
| 12865 | struct sctp_tmit_chunk *chk; |
| 12866 | struct sctp_chunkhdr *ch; |
| 12867 | int can_send_out_req=0; |
| 12868 | uint32_t seq; |
| 12869 |
| 12870 | asoc = &stcb->asoc; |
| 12871 | if (asoc->stream_reset_outstanding) { |
| 12872 | /*- |
| 12873 | * Already one pending, must get ACK back to clear the flag. |
| 12874 | */ |
| 12875 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY); |
| 12876 | return (EBUSY); |
| 12877 | } |
| 12878 | if ((send_in_req == 0) && (send_tsn_req == 0) && |
| 12879 | (add_stream == 0)) { |
| 12880 | /* nothing to do */ |
| 12881 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 12882 | return (EINVAL); |
| 12883 | } |
| 12884 | if (send_tsn_req && send_in_req) { |
| 12885 | /* error, can't do that */ |
| 12886 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 12887 | return (EINVAL); |
| 12888 | } else if (send_in_req) { |
| 12889 | can_send_out_req = 1; |
| 12890 | } |
| 12891 | if (number_entries > (MCLBYTES - |
| 12892 | SCTP_MIN_OVERHEAD - |
| 12893 | sizeof(struct sctp_chunkhdr) - |
| 12894 | sizeof(struct sctp_stream_reset_out_request)) / |
| 12895 | sizeof(uint16_t)) { |
| 12896 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 12897 | return (ENOMEM); |
| 12898 | } |
| 12899 | sctp_alloc_a_chunk(stcb, chk); |
| 12900 | if (chk == NULL) { |
| 12901 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 12902 | return (ENOMEM); |
| 12903 | } |
| 12904 | chk->copy_by_ref = 0; |
| 12905 | chk->rec.chunk_id.id = SCTP_STREAM_RESET; |
| 12906 | chk->rec.chunk_id.can_take_data = 0; |
| 12907 | chk->flags = 0; |
| 12908 | chk->asoc = &stcb->asoc; |
| 12909 | chk->book_size = sizeof(struct sctp_chunkhdr); |
| 12910 | chk->send_size = SCTP_SIZE32(chk->book_size); |
| 12911 | chk->book_size_scale = 0; |
| 12912 | chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); |
| 12913 | if (chk->data == NULL) { |
| 12914 | sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED); |
| 12915 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 12916 | return (ENOMEM); |
| 12917 | } |
| 12918 | SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); |
| 12919 |
| 12920 | /* setup chunk parameters */ |
| 12921 | chk->sent = SCTP_DATAGRAM_UNSENT; |
| 12922 | chk->snd_count = 0; |
| 12923 | if (stcb->asoc.alternate) { |
| 12924 | chk->whoTo = stcb->asoc.alternate; |
| 12925 | } else { |
| 12926 | chk->whoTo = stcb->asoc.primary_destination; |
| 12927 | } |
| 12928 | atomic_add_int(&chk->whoTo->ref_count, 1); |
| 12929 | ch = mtod(chk->data, struct sctp_chunkhdr *); |
| 12930 | ch->chunk_type = SCTP_STREAM_RESET; |
| 12931 | ch->chunk_flags = 0; |
| 12932 | ch->chunk_length = htons(chk->book_size); |
| 12933 | SCTP_BUF_LEN(chk->data) = chk->send_size; |
| 12934 |
| 12935 | seq = stcb->asoc.str_reset_seq_out; |
| 12936 | if (can_send_out_req) { |
| 12937 | int ret; |
| 12938 | ret = sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1)); |
| 12939 | if (ret) { |
| 12940 | seq++; |
| 12941 | asoc->stream_reset_outstanding++; |
| 12942 | } |
| 12943 | } |
| 12944 | if ((add_stream & 1) && |
| 12945 | ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < adding_o)) { |
| 12946 | /* Need to allocate more */ |
| 12947 | struct sctp_stream_out *oldstream; |
| 12948 | struct sctp_stream_queue_pending *sp, *nsp; |
| 12949 | int i; |
| 12950 | #if defined(SCTP_DETAILED_STR_STATS) |
| 12951 | int j; |
| 12952 | #endif |
| 12953 |
| 12954 | oldstream = stcb->asoc.strmout; |
| 12955 | /* get some more */ |
| 12956 | SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *, |
| 12957 | (stcb->asoc.streamoutcnt + adding_o) * sizeof(struct sctp_stream_out), |
| 12958 | SCTP_M_STRMO); |
| 12959 | if (stcb->asoc.strmout == NULL) { |
| 12960 | uint8_t x; |
| 12961 | stcb->asoc.strmout = oldstream; |
| 12962 | /* Turn off the bit */ |
| 12963 | x = add_stream & 0xfe; |
| 12964 | add_stream = x; |
| 12965 | goto skip_stuff; |
| 12966 | } |
| 12967 | /* Ok now we proceed with copying the old out stuff and |
| 12968 | * initializing the new stuff. |
| 12969 | */ |
| 12970 | SCTP_TCB_SEND_LOCK(stcb); |
| 12971 | stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 0, 1); |
| 12972 | for (i = 0; i < stcb->asoc.streamoutcnt; i++) { |
| 12973 | TAILQ_INIT(&stcb->asoc.strmout[i].outqueue); |
| 12974 | /* FIX ME FIX ME */ |
| 12975 | /* This should be a SS_COPY operation FIX ME STREAM SCHEDULER EXPERT */ |
| 12976 | stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], &oldstream[i]); |
| 12977 | stcb->asoc.strmout[i].chunks_on_queues = oldstream[i].chunks_on_queues; |
| 12978 | #if defined(SCTP_DETAILED_STR_STATS) |
| 12979 | for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) { |
| 12980 | stcb->asoc.strmout[i].abandoned_sent[j] = oldstream[i].abandoned_sent[j]; |
| 12981 | stcb->asoc.strmout[i].abandoned_unsent[j] = oldstream[i].abandoned_unsent[j]; |
| 12982 | } |
| 12983 | #else |
| 12984 | stcb->asoc.strmout[i].abandoned_sent[0] = oldstream[i].abandoned_sent[0]; |
| 12985 | stcb->asoc.strmout[i].abandoned_unsent[0] = oldstream[i].abandoned_unsent[0]; |
| 12986 | #endif |
| 12987 | stcb->asoc.strmout[i].next_mid_ordered = oldstream[i].next_mid_ordered; |
| 12988 | stcb->asoc.strmout[i].next_mid_unordered = oldstream[i].next_mid_unordered; |
| 12989 | stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete; |
| 12990 | stcb->asoc.strmout[i].sid = i; |
| 12991 | stcb->asoc.strmout[i].state = oldstream[i].state; |
| 12992 | /* now anything on those queues? */ |
| 12993 | TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) { |
| 12994 | TAILQ_REMOVE(&oldstream[i].outqueue, sp, next); |
| 12995 | TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next); |
| 12996 | } |
| 12997 | } |
| 12998 | /* now the new streams */ |
| 12999 | stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1); |
| 13000 | for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + adding_o); i++) { |
| 13001 | TAILQ_INIT(&stcb->asoc.strmout[i].outqueue); |
| 13002 | stcb->asoc.strmout[i].chunks_on_queues = 0; |
| 13003 | #if defined(SCTP_DETAILED_STR_STATS) |
| 13004 | for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) { |
| 13005 | stcb->asoc.strmout[i].abandoned_sent[j] = 0; |
| 13006 | stcb->asoc.strmout[i].abandoned_unsent[j] = 0; |
| 13007 | } |
| 13008 | #else |
| 13009 | stcb->asoc.strmout[i].abandoned_sent[0] = 0; |
| 13010 | stcb->asoc.strmout[i].abandoned_unsent[0] = 0; |
| 13011 | #endif |
| 13012 | stcb->asoc.strmout[i].next_mid_ordered = 0; |
| 13013 | stcb->asoc.strmout[i].next_mid_unordered = 0; |
| 13014 | stcb->asoc.strmout[i].sid = i; |
| 13015 | stcb->asoc.strmout[i].last_msg_incomplete = 0; |
| 13016 | stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL); |
| 13017 | stcb->asoc.strmout[i].state = SCTP_STREAM_CLOSED; |
| 13018 | } |
| 13019 | stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + adding_o; |
| 13020 | SCTP_FREE(oldstream, SCTP_M_STRMO); |
| 13021 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 13022 | } |
| 13023 | skip_stuff: |
| 13024 | if ((add_stream & 1) && (adding_o > 0)) { |
| 13025 | asoc->strm_pending_add_size = adding_o; |
| 13026 | asoc->peer_req_out = peer_asked; |
| 13027 | sctp_add_an_out_stream(chk, seq, adding_o); |
| 13028 | seq++; |
| 13029 | asoc->stream_reset_outstanding++; |
| 13030 | } |
| 13031 | if ((add_stream & 2) && (adding_i > 0)) { |
| 13032 | sctp_add_an_in_stream(chk, seq, adding_i); |
| 13033 | seq++; |
| 13034 | asoc->stream_reset_outstanding++; |
| 13035 | } |
| 13036 | if (send_in_req) { |
| 13037 | sctp_add_stream_reset_in(chk, number_entries, list, seq); |
| 13038 | seq++; |
| 13039 | asoc->stream_reset_outstanding++; |
| 13040 | } |
| 13041 | if (send_tsn_req) { |
| 13042 | sctp_add_stream_reset_tsn(chk, seq); |
| 13043 | asoc->stream_reset_outstanding++; |
| 13044 | } |
| 13045 | asoc->str_reset = chk; |
| 13046 | /* insert the chunk for sending */ |
| 13047 | TAILQ_INSERT_TAIL(&asoc->control_send_queue, |
| 13048 | chk, |
| 13049 | sctp_next); |
| 13050 | asoc->ctrl_queue_cnt++; |
| 13051 | if (stcb->asoc.send_sack) { |
| 13052 | sctp_send_sack(stcb, SCTP_SO_LOCKED); |
| 13053 | } |
| 13054 | sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); |
| 13055 | return (0); |
| 13056 | } |
| 13057 |
| 13058 | void |
| 13059 | sctp_send_abort(struct mbuf *m, int iphlen, struct sockaddr *src, struct sockaddr *dst, |
| 13060 | struct sctphdr *sh, uint32_t vtag, struct mbuf *cause, |
| 13061 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 13062 | uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum, |
| 13063 | #endif |
| 13064 | uint32_t vrf_id, uint16_t port) |
| 13065 | { |
| 13066 | /* Don't respond to an ABORT with an ABORT. */ |
| 13067 | if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) { |
| 13068 | if (cause) |
| 13069 | sctp_m_freem(cause); |
| 13070 | return; |
| 13071 | } |
| 13072 | sctp_send_resp_msg(src, dst, sh, vtag, SCTP_ABORT_ASSOCIATION, cause, |
| 13073 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 13074 | mflowtype, mflowid, fibnum, |
| 13075 | #endif |
| 13076 | vrf_id, port); |
| 13077 | return; |
| 13078 | } |
| 13079 |
| 13080 | void |
| 13081 | sctp_send_operr_to(struct sockaddr *src, struct sockaddr *dst, |
| 13082 | struct sctphdr *sh, uint32_t vtag, struct mbuf *cause, |
| 13083 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 13084 | uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum, |
| 13085 | #endif |
| 13086 | uint32_t vrf_id, uint16_t port) |
| 13087 | { |
| 13088 | sctp_send_resp_msg(src, dst, sh, vtag, SCTP_OPERATION_ERROR, cause, |
| 13089 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 13090 | mflowtype, mflowid, fibnum, |
| 13091 | #endif |
| 13092 | vrf_id, port); |
| 13093 | return; |
| 13094 | } |
| 13095 |
| 13096 | static struct mbuf * |
| 13097 | sctp_copy_resume(struct uio *uio, |
| 13098 | int max_send_len, |
| 13099 | #if defined(__FreeBSD__) || defined(__Userspace__) |
| 13100 | int user_marks_eor, |
| 13101 | #endif |
| 13102 | int *error, |
| 13103 | uint32_t *sndout, |
| 13104 | struct mbuf **new_tail) |
| 13105 | { |
| 13106 | #if defined(__FreeBSD__) || defined(__Userspace__) |
| 13107 | struct mbuf *m; |
| 13108 |
| 13109 | m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0, |
| 13110 | (M_PKTHDR | (user_marks_eor ? M_EOR : 0))); |
| 13111 | if (m == NULL) { |
| 13112 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS); |
| 13113 | *error = ENOBUFS; |
| 13114 | } else { |
| 13115 | *sndout = m_length(m, NULL); |
| 13116 | *new_tail = m_last(m); |
| 13117 | } |
| 13118 | return (m); |
| 13119 | #else |
| 13120 | int left, cancpy, willcpy; |
| 13121 | struct mbuf *m, *head; |
| 13122 |
| 13123 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 13124 | #if defined(APPLE_LEOPARD) |
| 13125 | left = (int)min(uio->uio_resid, max_send_len); |
| 13126 | #else |
| 13127 | left = (int)min(uio_resid(uio), max_send_len); |
| 13128 | #endif |
| 13129 | #else |
| 13130 | left = (int)min(uio->uio_resid, max_send_len); |
| 13131 | #endif |
| 13132 | /* Always get a header just in case */ |
| 13133 | head = sctp_get_mbuf_for_msg(left, 0, M_WAITOK, 0, MT_DATA); |
| 13134 | if (head == NULL) { |
| 13135 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS); |
| 13136 | *error = ENOBUFS; |
| 13137 | return (NULL); |
| 13138 | } |
| 13139 | cancpy = (int)M_TRAILINGSPACE(head); |
| 13140 | willcpy = min(cancpy, left); |
| 13141 | *error = uiomove(mtod(head, caddr_t), willcpy, uio); |
| 13142 | if (*error) { |
| 13143 | sctp_m_freem(head); |
| 13144 | return (NULL); |
| 13145 | } |
| 13146 | *sndout += willcpy; |
| 13147 | left -= willcpy; |
| 13148 | SCTP_BUF_LEN(head) = willcpy; |
| 13149 | m = head; |
| 13150 | *new_tail = head; |
| 13151 | while (left > 0) { |
| 13152 | /* move in user data */ |
| 13153 | SCTP_BUF_NEXT(m) = sctp_get_mbuf_for_msg(left, 0, M_WAITOK, 0, MT_DATA); |
| 13154 | if (SCTP_BUF_NEXT(m) == NULL) { |
| 13155 | sctp_m_freem(head); |
| 13156 | *new_tail = NULL; |
| 13157 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS); |
| 13158 | *error = ENOBUFS; |
| 13159 | return (NULL); |
| 13160 | } |
| 13161 | m = SCTP_BUF_NEXT(m); |
| 13162 | cancpy = (int)M_TRAILINGSPACE(m); |
| 13163 | willcpy = min(cancpy, left); |
| 13164 | *error = uiomove(mtod(m, caddr_t), willcpy, uio); |
| 13165 | if (*error) { |
| 13166 | sctp_m_freem(head); |
| 13167 | *new_tail = NULL; |
| 13168 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT); |
| 13169 | *error = EFAULT; |
| 13170 | return (NULL); |
| 13171 | } |
| 13172 | SCTP_BUF_LEN(m) = willcpy; |
| 13173 | left -= willcpy; |
| 13174 | *sndout += willcpy; |
| 13175 | *new_tail = m; |
| 13176 | if (left == 0) { |
| 13177 | SCTP_BUF_NEXT(m) = NULL; |
| 13178 | } |
| 13179 | } |
| 13180 | return (head); |
| 13181 | #endif |
| 13182 | } |
| 13183 |
| 13184 | static int |
| 13185 | sctp_copy_one(struct sctp_stream_queue_pending *sp, |
| 13186 | struct uio *uio, |
| 13187 | int resv_upfront) |
| 13188 | { |
| 13189 | #if defined(__FreeBSD__) || defined(__Userspace__) |
| 13190 | sp->data = m_uiotombuf(uio, M_WAITOK, sp->length, |
| 13191 | resv_upfront, 0); |
| 13192 | if (sp->data == NULL) { |
| 13193 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS); |
| 13194 | return (ENOBUFS); |
| 13195 | } |
| 13196 |
| 13197 | sp->tail_mbuf = m_last(sp->data); |
| 13198 | return (0); |
| 13199 | #else |
| 13200 | int left; |
| 13201 | int cancpy, willcpy, error; |
| 13202 | struct mbuf *m, *head; |
| 13203 | int cpsz = 0; |
| 13204 |
| 13205 | /* First one gets a header */ |
| 13206 | left = sp->length; |
| 13207 | head = m = sctp_get_mbuf_for_msg((left + resv_upfront), 0, M_WAITOK, 0, MT_DATA); |
| 13208 | if (m == NULL) { |
| 13209 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS); |
| 13210 | return (ENOBUFS); |
| 13211 | } |
| 13212 | /*- |
| 13213 | * Add this one for m in now, that way if the alloc fails we won't |
| 13214 | * have a bad cnt. |
| 13215 | */ |
| 13216 | SCTP_BUF_RESV_UF(m, resv_upfront); |
| 13217 | cancpy = (int)M_TRAILINGSPACE(m); |
| 13218 | willcpy = min(cancpy, left); |
| 13219 | while (left > 0) { |
| 13220 | /* move in user data */ |
| 13221 | error = uiomove(mtod(m, caddr_t), willcpy, uio); |
| 13222 | if (error) { |
| 13223 | sctp_m_freem(head); |
| 13224 | return (error); |
| 13225 | } |
| 13226 | SCTP_BUF_LEN(m) = willcpy; |
| 13227 | left -= willcpy; |
| 13228 | cpsz += willcpy; |
| 13229 | if (left > 0) { |
| 13230 | SCTP_BUF_NEXT(m) = sctp_get_mbuf_for_msg(left, 0, M_WAITOK, 0, MT_DATA); |
| 13231 | if (SCTP_BUF_NEXT(m) == NULL) { |
| 13232 | /* |
| 13233 | * the head goes back to caller, he can free |
| 13234 | * the rest |
| 13235 | */ |
| 13236 | sctp_m_freem(head); |
| 13237 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS); |
| 13238 | return (ENOBUFS); |
| 13239 | } |
| 13240 | m = SCTP_BUF_NEXT(m); |
| 13241 | cancpy = (int)M_TRAILINGSPACE(m); |
| 13242 | willcpy = min(cancpy, left); |
| 13243 | } else { |
| 13244 | sp->tail_mbuf = m; |
| 13245 | SCTP_BUF_NEXT(m) = NULL; |
| 13246 | } |
| 13247 | } |
| 13248 | sp->data = head; |
| 13249 | sp->length = cpsz; |
| 13250 | return (0); |
| 13251 | #endif |
| 13252 | } |
| 13253 |
| 13254 | static struct sctp_stream_queue_pending * |
| 13255 | sctp_copy_it_in(struct sctp_tcb *stcb, |
| 13256 | struct sctp_association *asoc, |
| 13257 | struct sctp_sndrcvinfo *srcv, |
| 13258 | struct uio *uio, |
| 13259 | struct sctp_nets *net, |
| 13260 | ssize_t max_send_len, |
| 13261 | int user_marks_eor, |
| 13262 | int *error) |
| 13263 |
| 13264 | { |
| 13265 | /*- |
| 13266 | * This routine must be very careful in its work. Protocol |
| 13267 | * processing is up and running so care must be taken to spl...() |
| 13268 | * when you need to do something that may effect the stcb/asoc. The |
| 13269 | * sb is locked however. When data is copied the protocol processing |
| 13270 | * should be enabled since this is a slower operation... |
| 13271 | */ |
| 13272 | struct sctp_stream_queue_pending *sp = NULL; |
| 13273 | int resv_in_first; |
| 13274 |
| 13275 | *error = 0; |
| 13276 | /* Now can we send this? */ |
| 13277 | if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) || |
| 13278 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) || |
| 13279 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) || |
| 13280 | (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) { |
| 13281 | /* got data while shutting down */ |
| 13282 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); |
| 13283 | *error = ECONNRESET; |
| 13284 | goto out_now; |
| 13285 | } |
| 13286 | sctp_alloc_a_strmoq(stcb, sp); |
| 13287 | if (sp == NULL) { |
| 13288 | SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 13289 | *error = ENOMEM; |
| 13290 | goto out_now; |
| 13291 | } |
| 13292 | sp->act_flags = 0; |
| 13293 | sp->sender_all_done = 0; |
| 13294 | sp->sinfo_flags = srcv->sinfo_flags; |
| 13295 | sp->timetolive = srcv->sinfo_timetolive; |
| 13296 | sp->ppid = srcv->sinfo_ppid; |
| 13297 | sp->context = srcv->sinfo_context; |
| 13298 | sp->fsn = 0; |
| 13299 | (void)SCTP_GETTIME_TIMEVAL(&sp->ts); |
| 13300 |
| 13301 | sp->sid = srcv->sinfo_stream; |
| 13302 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 13303 | #if defined(APPLE_LEOPARD) |
| 13304 | sp->length = (uint32_t)min(uio->uio_resid, max_send_len); |
| 13305 | #else |
| 13306 | sp->length = (uint32_t)min(uio_resid(uio), max_send_len); |
| 13307 | #endif |
| 13308 | #else |
| 13309 | sp->length = (uint32_t)min(uio->uio_resid, max_send_len); |
| 13310 | #endif |
| 13311 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 13312 | #if defined(APPLE_LEOPARD) |
| 13313 | if ((sp->length == (uint32_t)uio->uio_resid) && |
| 13314 | #else |
| 13315 | if ((sp->length == (uint32_t)uio_resid(uio)) && |
| 13316 | #endif |
| 13317 | #else |
| 13318 | if ((sp->length == (uint32_t)uio->uio_resid) && |
| 13319 | #endif |
| 13320 | ((user_marks_eor == 0) || |
| 13321 | (srcv->sinfo_flags & SCTP_EOF) || |
| 13322 | (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) { |
| 13323 | sp->msg_is_complete = 1; |
| 13324 | } else { |
| 13325 | sp->msg_is_complete = 0; |
| 13326 | } |
| 13327 | sp->sender_all_done = 0; |
| 13328 | sp->some_taken = 0; |
| 13329 | sp->put_last_out = 0; |
| 13330 | resv_in_first = SCTP_DATA_CHUNK_OVERHEAD(stcb); |
| 13331 | sp->data = sp->tail_mbuf = NULL; |
| 13332 | if (sp->length == 0) { |
| 13333 | goto skip_copy; |
| 13334 | } |
| 13335 | if (srcv->sinfo_keynumber_valid) { |
| 13336 | sp->auth_keyid = srcv->sinfo_keynumber; |
| 13337 | } else { |
| 13338 | sp->auth_keyid = stcb->asoc.authinfo.active_keyid; |
| 13339 | } |
| 13340 | if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) { |
| 13341 | sctp_auth_key_acquire(stcb, sp->auth_keyid); |
| 13342 | sp->holds_key_ref = 1; |
| 13343 | } |
| 13344 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 13345 | SCTP_SOCKET_UNLOCK(SCTP_INP_SO(stcb->sctp_ep), 0); |
| 13346 | #endif |
| 13347 | *error = sctp_copy_one(sp, uio, resv_in_first); |
| 13348 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 13349 | SCTP_SOCKET_LOCK(SCTP_INP_SO(stcb->sctp_ep), 0); |
| 13350 | #endif |
| 13351 | skip_copy: |
| 13352 | if (*error) { |
| 13353 | #if defined(__Userspace__) |
| 13354 | SCTP_TCB_LOCK(stcb); |
| 13355 | #endif |
| 13356 | sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED); |
| 13357 | #if defined(__Userspace__) |
| 13358 | SCTP_TCB_UNLOCK(stcb); |
| 13359 | #endif |
| 13360 | sp = NULL; |
| 13361 | } else { |
| 13362 | if (sp->sinfo_flags & SCTP_ADDR_OVER) { |
| 13363 | sp->net = net; |
| 13364 | atomic_add_int(&sp->net->ref_count, 1); |
| 13365 | } else { |
| 13366 | sp->net = NULL; |
| 13367 | } |
| 13368 | sctp_set_prsctp_policy(sp); |
| 13369 | } |
| 13370 | out_now: |
| 13371 | return (sp); |
| 13372 | } |
| 13373 |
| 13374 | int |
| 13375 | sctp_sosend(struct socket *so, |
| 13376 | struct sockaddr *addr, |
| 13377 | struct uio *uio, |
| 13378 | struct mbuf *top, |
| 13379 | struct mbuf *control, |
| 13380 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 13381 | int flags |
| 13382 | #else |
| 13383 | int flags, |
| 13384 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 13385 | struct thread *p |
| 13386 | #elif defined(_WIN32) && !defined(__Userspace__) |
| 13387 | PKTHREAD p |
| 13388 | #else |
| 13389 | #if defined(__Userspace__) |
| 13390 | /* |
| 13391 | * proc is a dummy in __Userspace__ and will not be passed |
| 13392 | * to sctp_lower_sosend |
| 13393 | */ |
| 13394 | #endif |
| 13395 | struct proc *p |
| 13396 | #endif |
| 13397 | #endif |
| 13398 | ) |
| 13399 | { |
| 13400 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 13401 | struct proc *p = current_proc(); |
| 13402 | #endif |
| 13403 | int error, use_sndinfo = 0; |
| 13404 | struct sctp_sndrcvinfo sndrcvninfo; |
| 13405 | struct sockaddr *addr_to_use; |
| 13406 | #if defined(INET) && defined(INET6) |
| 13407 | struct sockaddr_in sin; |
| 13408 | #endif |
| 13409 |
| 13410 | if (control) { |
| 13411 | /* process cmsg snd/rcv info (maybe a assoc-id) */ |
| 13412 | if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&sndrcvninfo, control, |
| 13413 | sizeof(sndrcvninfo))) { |
| 13414 | /* got one */ |
| 13415 | use_sndinfo = 1; |
| 13416 | } |
| 13417 | } |
| 13418 | addr_to_use = addr; |
| 13419 | #if defined(INET) && defined(INET6) |
| 13420 | if ((addr != NULL) && (addr->sa_family == AF_INET6)) { |
| 13421 | struct sockaddr_in6 *sin6; |
| 13422 |
| 13423 | #ifdef HAVE_SA_LEN |
| 13424 | if (addr->sa_len != sizeof(struct sockaddr_in6)) { |
| 13425 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13426 | return (EINVAL); |
| 13427 | } |
| 13428 | #endif |
| 13429 | sin6 = (struct sockaddr_in6 *)addr; |
| 13430 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { |
| 13431 | in6_sin6_2_sin(&sin, sin6); |
| 13432 | addr_to_use = (struct sockaddr *)&sin; |
| 13433 | } |
| 13434 | } |
| 13435 | #endif |
| 13436 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 13437 | SCTP_SOCKET_LOCK(so, 1); |
| 13438 | #endif |
| 13439 | error = sctp_lower_sosend(so, addr_to_use, uio, top, |
| 13440 | control, |
| 13441 | flags, |
| 13442 | use_sndinfo ? &sndrcvninfo: NULL |
| 13443 | #if !defined(__Userspace__) |
| 13444 | , p |
| 13445 | #endif |
| 13446 | ); |
| 13447 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 13448 | SCTP_SOCKET_UNLOCK(so, 1); |
| 13449 | #endif |
| 13450 | return (error); |
| 13451 | } |
| 13452 |
| 13453 | int |
| 13454 | sctp_lower_sosend(struct socket *so, |
| 13455 | struct sockaddr *addr, |
| 13456 | struct uio *uio, |
| 13457 | struct mbuf *i_pak, |
| 13458 | struct mbuf *control, |
| 13459 | int flags, |
| 13460 | struct sctp_sndrcvinfo *srcv |
| 13461 | #if !defined(__Userspace__) |
| 13462 | , |
| 13463 | #if defined(__FreeBSD__) |
| 13464 | struct thread *p |
| 13465 | #elif defined(_WIN32) |
| 13466 | PKTHREAD p |
| 13467 | #else |
| 13468 | struct proc *p |
| 13469 | #endif |
| 13470 | #endif |
| 13471 | ) |
| 13472 | { |
| 13473 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 13474 | struct epoch_tracker et; |
| 13475 | #endif |
| 13476 | ssize_t sndlen = 0, max_len, local_add_more; |
| 13477 | int error; |
| 13478 | struct mbuf *top = NULL; |
| 13479 | int queue_only = 0, queue_only_for_init = 0; |
| 13480 | int free_cnt_applied = 0; |
| 13481 | int un_sent; |
| 13482 | int now_filled = 0; |
| 13483 | unsigned int inqueue_bytes = 0; |
| 13484 | struct sctp_block_entry be; |
| 13485 | struct sctp_inpcb *inp; |
| 13486 | struct sctp_tcb *stcb = NULL; |
| 13487 | struct timeval now; |
| 13488 | struct sctp_nets *net; |
| 13489 | struct sctp_association *asoc; |
| 13490 | struct sctp_inpcb *t_inp; |
| 13491 | int user_marks_eor; |
| 13492 | int create_lock_applied = 0; |
| 13493 | int nagle_applies = 0; |
| 13494 | int some_on_control = 0; |
| 13495 | int got_all_of_the_send = 0; |
| 13496 | int hold_tcblock = 0; |
| 13497 | int non_blocking = 0; |
| 13498 | ssize_t local_soresv = 0; |
| 13499 | uint16_t port; |
| 13500 | uint16_t sinfo_flags; |
| 13501 | sctp_assoc_t sinfo_assoc_id; |
| 13502 |
| 13503 | error = 0; |
| 13504 | net = NULL; |
| 13505 | stcb = NULL; |
| 13506 | asoc = NULL; |
| 13507 |
| 13508 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 13509 | sctp_lock_assert(so); |
| 13510 | #endif |
| 13511 | t_inp = inp = (struct sctp_inpcb *)so->so_pcb; |
| 13512 | if (inp == NULL) { |
| 13513 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13514 | error = EINVAL; |
| 13515 | if (i_pak) { |
| 13516 | SCTP_RELEASE_PKT(i_pak); |
| 13517 | } |
| 13518 | return (error); |
| 13519 | } |
| 13520 | if ((uio == NULL) && (i_pak == NULL)) { |
| 13521 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13522 | return (EINVAL); |
| 13523 | } |
| 13524 | user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR); |
| 13525 | atomic_add_int(&inp->total_sends, 1); |
| 13526 | if (uio) { |
| 13527 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 13528 | #if defined(APPLE_LEOPARD) |
| 13529 | if (uio->uio_resid < 0) { |
| 13530 | #else |
| 13531 | if (uio_resid(uio) < 0) { |
| 13532 | #endif |
| 13533 | #else |
| 13534 | if (uio->uio_resid < 0) { |
| 13535 | #endif |
| 13536 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13537 | return (EINVAL); |
| 13538 | } |
| 13539 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 13540 | #if defined(APPLE_LEOPARD) |
| 13541 | sndlen = uio->uio_resid; |
| 13542 | #else |
| 13543 | sndlen = uio_resid(uio); |
| 13544 | #endif |
| 13545 | #else |
| 13546 | sndlen = uio->uio_resid; |
| 13547 | #endif |
| 13548 | } else { |
| 13549 | top = SCTP_HEADER_TO_CHAIN(i_pak); |
| 13550 | sndlen = SCTP_HEADER_LEN(i_pak); |
| 13551 | } |
| 13552 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %zd\n", |
| 13553 | (void *)addr, |
| 13554 | sndlen); |
| 13555 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && |
| 13556 | SCTP_IS_LISTENING(inp)) { |
| 13557 | /* The listener can NOT send */ |
| 13558 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN); |
| 13559 | error = ENOTCONN; |
| 13560 | goto out_unlocked; |
| 13561 | } |
| 13562 | /** |
| 13563 | * Pre-screen address, if one is given the sin-len |
| 13564 | * must be set correctly! |
| 13565 | */ |
| 13566 | if (addr) { |
| 13567 | union sctp_sockstore *raddr = (union sctp_sockstore *)addr; |
| 13568 | switch (raddr->sa.sa_family) { |
| 13569 | #ifdef INET |
| 13570 | case AF_INET: |
| 13571 | #ifdef HAVE_SIN_LEN |
| 13572 | if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) { |
| 13573 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13574 | error = EINVAL; |
| 13575 | goto out_unlocked; |
| 13576 | } |
| 13577 | #endif |
| 13578 | port = raddr->sin.sin_port; |
| 13579 | break; |
| 13580 | #endif |
| 13581 | #ifdef INET6 |
| 13582 | case AF_INET6: |
| 13583 | #ifdef HAVE_SIN6_LEN |
| 13584 | if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) { |
| 13585 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13586 | error = EINVAL; |
| 13587 | goto out_unlocked; |
| 13588 | } |
| 13589 | #endif |
| 13590 | port = raddr->sin6.sin6_port; |
| 13591 | break; |
| 13592 | #endif |
| 13593 | #if defined(__Userspace__) |
| 13594 | case AF_CONN: |
| 13595 | #ifdef HAVE_SCONN_LEN |
| 13596 | if (raddr->sconn.sconn_len != sizeof(struct sockaddr_conn)) { |
| 13597 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13598 | error = EINVAL; |
| 13599 | goto out_unlocked; |
| 13600 | } |
| 13601 | #endif |
| 13602 | port = raddr->sconn.sconn_port; |
| 13603 | break; |
| 13604 | #endif |
| 13605 | default: |
| 13606 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAFNOSUPPORT); |
| 13607 | error = EAFNOSUPPORT; |
| 13608 | goto out_unlocked; |
| 13609 | } |
| 13610 | } else |
| 13611 | port = 0; |
| 13612 |
| 13613 | if (srcv) { |
| 13614 | sinfo_flags = srcv->sinfo_flags; |
| 13615 | sinfo_assoc_id = srcv->sinfo_assoc_id; |
| 13616 | if (INVALID_SINFO_FLAG(sinfo_flags) || |
| 13617 | PR_SCTP_INVALID_POLICY(sinfo_flags)) { |
| 13618 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13619 | error = EINVAL; |
| 13620 | goto out_unlocked; |
| 13621 | } |
| 13622 | if (srcv->sinfo_flags) |
| 13623 | SCTP_STAT_INCR(sctps_sends_with_flags); |
| 13624 | } else { |
| 13625 | sinfo_flags = inp->def_send.sinfo_flags; |
| 13626 | sinfo_assoc_id = inp->def_send.sinfo_assoc_id; |
| 13627 | } |
| 13628 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 13629 | if (flags & MSG_EOR) { |
| 13630 | sinfo_flags |= SCTP_EOR; |
| 13631 | } |
| 13632 | if (flags & MSG_EOF) { |
| 13633 | sinfo_flags |= SCTP_EOF; |
| 13634 | } |
| 13635 | #endif |
| 13636 | if (sinfo_flags & SCTP_SENDALL) { |
| 13637 | /* its a sendall */ |
| 13638 | error = sctp_sendall(inp, uio, top, srcv); |
| 13639 | top = NULL; |
| 13640 | goto out_unlocked; |
| 13641 | } |
| 13642 | if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) { |
| 13643 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13644 | error = EINVAL; |
| 13645 | goto out_unlocked; |
| 13646 | } |
| 13647 | /* now we must find the assoc */ |
| 13648 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) || |
| 13649 | (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { |
| 13650 | SCTP_INP_RLOCK(inp); |
| 13651 | stcb = LIST_FIRST(&inp->sctp_asoc_list); |
| 13652 | if (stcb) { |
| 13653 | SCTP_TCB_LOCK(stcb); |
| 13654 | hold_tcblock = 1; |
| 13655 | } |
| 13656 | SCTP_INP_RUNLOCK(inp); |
| 13657 | } else if (sinfo_assoc_id) { |
| 13658 | stcb = sctp_findassociation_ep_asocid(inp, sinfo_assoc_id, 1); |
| 13659 | if (stcb != NULL) { |
| 13660 | hold_tcblock = 1; |
| 13661 | } |
| 13662 | } else if (addr) { |
| 13663 | /*- |
| 13664 | * Since we did not use findep we must |
| 13665 | * increment it, and if we don't find a tcb |
| 13666 | * decrement it. |
| 13667 | */ |
| 13668 | SCTP_INP_WLOCK(inp); |
| 13669 | SCTP_INP_INCR_REF(inp); |
| 13670 | SCTP_INP_WUNLOCK(inp); |
| 13671 | stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL); |
| 13672 | if (stcb == NULL) { |
| 13673 | SCTP_INP_WLOCK(inp); |
| 13674 | SCTP_INP_DECR_REF(inp); |
| 13675 | SCTP_INP_WUNLOCK(inp); |
| 13676 | } else { |
| 13677 | hold_tcblock = 1; |
| 13678 | } |
| 13679 | } |
| 13680 | if ((stcb == NULL) && (addr)) { |
| 13681 | /* Possible implicit send? */ |
| 13682 | SCTP_ASOC_CREATE_LOCK(inp); |
| 13683 | create_lock_applied = 1; |
| 13684 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || |
| 13685 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { |
| 13686 | /* Should I really unlock ? */ |
| 13687 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13688 | error = EINVAL; |
| 13689 | goto out_unlocked; |
| 13690 | } |
| 13691 | if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) && |
| 13692 | (addr->sa_family == AF_INET6)) { |
| 13693 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13694 | error = EINVAL; |
| 13695 | goto out_unlocked; |
| 13696 | } |
| 13697 | SCTP_INP_WLOCK(inp); |
| 13698 | SCTP_INP_INCR_REF(inp); |
| 13699 | SCTP_INP_WUNLOCK(inp); |
| 13700 | /* With the lock applied look again */ |
| 13701 | stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL); |
| 13702 | #if defined(INET) || defined(INET6) |
| 13703 | if ((stcb == NULL) && (control != NULL) && (port > 0)) { |
| 13704 | stcb = sctp_findassociation_cmsgs(&t_inp, port, control, &net, &error); |
| 13705 | } |
| 13706 | #endif |
| 13707 | if (stcb == NULL) { |
| 13708 | SCTP_INP_WLOCK(inp); |
| 13709 | SCTP_INP_DECR_REF(inp); |
| 13710 | SCTP_INP_WUNLOCK(inp); |
| 13711 | } else { |
| 13712 | hold_tcblock = 1; |
| 13713 | } |
| 13714 | if (error) { |
| 13715 | goto out_unlocked; |
| 13716 | } |
| 13717 | if (t_inp != inp) { |
| 13718 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN); |
| 13719 | error = ENOTCONN; |
| 13720 | goto out_unlocked; |
| 13721 | } |
| 13722 | } |
| 13723 | if (stcb == NULL) { |
| 13724 | if (addr == NULL) { |
| 13725 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT); |
| 13726 | error = ENOENT; |
| 13727 | goto out_unlocked; |
| 13728 | } else { |
| 13729 | /* We must go ahead and start the INIT process */ |
| 13730 | uint32_t vrf_id; |
| 13731 |
| 13732 | if ((sinfo_flags & SCTP_ABORT) || |
| 13733 | ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) { |
| 13734 | /*- |
| 13735 | * User asks to abort a non-existent assoc, |
| 13736 | * or EOF a non-existent assoc with no data |
| 13737 | */ |
| 13738 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT); |
| 13739 | error = ENOENT; |
| 13740 | goto out_unlocked; |
| 13741 | } |
| 13742 | /* get an asoc/stcb struct */ |
| 13743 | vrf_id = inp->def_vrf_id; |
| 13744 | #ifdef INVARIANTS |
| 13745 | if (create_lock_applied == 0) { |
| 13746 | panic("Error, should hold create lock and I don't?"); |
| 13747 | } |
| 13748 | #endif |
| 13749 | stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id, |
| 13750 | inp->sctp_ep.pre_open_stream_count, |
| 13751 | inp->sctp_ep.port, |
| 13752 | #if !defined(__Userspace__) |
| 13753 | p, |
| 13754 | #else |
| 13755 | (struct proc *)NULL, |
| 13756 | #endif |
| 13757 | SCTP_INITIALIZE_AUTH_PARAMS); |
| 13758 | if (stcb == NULL) { |
| 13759 | /* Error is setup for us in the call */ |
| 13760 | goto out_unlocked; |
| 13761 | } |
| 13762 | hold_tcblock = 1; |
| 13763 | if (create_lock_applied) { |
| 13764 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 13765 | create_lock_applied = 0; |
| 13766 | } else { |
| 13767 | SCTP_PRINTF("Huh-3? create lock should have been on??\n"); |
| 13768 | } |
| 13769 | /* Turn on queue only flag to prevent data from being sent */ |
| 13770 | queue_only = 1; |
| 13771 | asoc = &stcb->asoc; |
| 13772 | SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT); |
| 13773 | (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); |
| 13774 |
| 13775 | if (control) { |
| 13776 | if (sctp_process_cmsgs_for_init(stcb, control, &error)) { |
| 13777 | sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, |
| 13778 | SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6); |
| 13779 | hold_tcblock = 0; |
| 13780 | stcb = NULL; |
| 13781 | goto out_unlocked; |
| 13782 | } |
| 13783 | } |
| 13784 | /* out with the INIT */ |
| 13785 | queue_only_for_init = 1; |
| 13786 | /*- |
| 13787 | * we may want to dig in after this call and adjust the MTU |
| 13788 | * value. It defaulted to 1500 (constant) but the ro |
| 13789 | * structure may now have an update and thus we may need to |
| 13790 | * change it BEFORE we append the message. |
| 13791 | */ |
| 13792 | } |
| 13793 | } else |
| 13794 | asoc = &stcb->asoc; |
| 13795 | if (srcv == NULL) { |
| 13796 | srcv = (struct sctp_sndrcvinfo *)&asoc->def_send; |
| 13797 | sinfo_flags = srcv->sinfo_flags; |
| 13798 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 13799 | if (flags & MSG_EOR) { |
| 13800 | sinfo_flags |= SCTP_EOR; |
| 13801 | } |
| 13802 | if (flags & MSG_EOF) { |
| 13803 | sinfo_flags |= SCTP_EOF; |
| 13804 | } |
| 13805 | #endif |
| 13806 | } |
| 13807 | if (sinfo_flags & SCTP_ADDR_OVER) { |
| 13808 | if (addr) |
| 13809 | net = sctp_findnet(stcb, addr); |
| 13810 | else |
| 13811 | net = NULL; |
| 13812 | if ((net == NULL) || |
| 13813 | ((port != 0) && (port != stcb->rport))) { |
| 13814 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13815 | error = EINVAL; |
| 13816 | goto out_unlocked; |
| 13817 | } |
| 13818 | } else { |
| 13819 | if (stcb->asoc.alternate) { |
| 13820 | net = stcb->asoc.alternate; |
| 13821 | } else { |
| 13822 | net = stcb->asoc.primary_destination; |
| 13823 | } |
| 13824 | } |
| 13825 | atomic_add_int(&stcb->total_sends, 1); |
| 13826 | /* Keep the stcb from being freed under our feet */ |
| 13827 | atomic_add_int(&asoc->refcnt, 1); |
| 13828 | free_cnt_applied = 1; |
| 13829 |
| 13830 | if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) { |
| 13831 | if (sndlen > (ssize_t)asoc->smallest_mtu) { |
| 13832 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE); |
| 13833 | error = EMSGSIZE; |
| 13834 | goto out_unlocked; |
| 13835 | } |
| 13836 | } |
| 13837 | #if defined(__Userspace__) |
| 13838 | if (inp->recv_callback) { |
| 13839 | non_blocking = 1; |
| 13840 | } |
| 13841 | #endif |
| 13842 | if (SCTP_SO_IS_NBIO(so) |
| 13843 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 13844 | || (flags & (MSG_NBIO | MSG_DONTWAIT)) != 0 |
| 13845 | #endif |
| 13846 | ) { |
| 13847 | non_blocking = 1; |
| 13848 | } |
| 13849 | /* would we block? */ |
| 13850 | if (non_blocking) { |
| 13851 | ssize_t amount; |
| 13852 |
| 13853 | if (hold_tcblock == 0) { |
| 13854 | SCTP_TCB_LOCK(stcb); |
| 13855 | hold_tcblock = 1; |
| 13856 | } |
| 13857 | inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 13858 | if (user_marks_eor == 0) { |
| 13859 | amount = sndlen; |
| 13860 | } else { |
| 13861 | amount = 1; |
| 13862 | } |
| 13863 | if ((SCTP_SB_LIMIT_SND(so) < (amount + inqueue_bytes + stcb->asoc.sb_send_resv)) || |
| 13864 | (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) { |
| 13865 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK); |
| 13866 | if (sndlen > (ssize_t)SCTP_SB_LIMIT_SND(so)) |
| 13867 | error = EMSGSIZE; |
| 13868 | else |
| 13869 | error = EWOULDBLOCK; |
| 13870 | goto out_unlocked; |
| 13871 | } |
| 13872 | stcb->asoc.sb_send_resv += (uint32_t)sndlen; |
| 13873 | SCTP_TCB_UNLOCK(stcb); |
| 13874 | hold_tcblock = 0; |
| 13875 | } else { |
| 13876 | atomic_add_int(&stcb->asoc.sb_send_resv, (int)sndlen); |
| 13877 | } |
| 13878 | local_soresv = sndlen; |
| 13879 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 13880 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); |
| 13881 | error = ECONNRESET; |
| 13882 | goto out_unlocked; |
| 13883 | } |
| 13884 | if (create_lock_applied) { |
| 13885 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 13886 | create_lock_applied = 0; |
| 13887 | } |
| 13888 | /* Is the stream no. valid? */ |
| 13889 | if (srcv->sinfo_stream >= asoc->streamoutcnt) { |
| 13890 | /* Invalid stream number */ |
| 13891 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13892 | error = EINVAL; |
| 13893 | goto out_unlocked; |
| 13894 | } |
| 13895 | if ((asoc->strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPEN) && |
| 13896 | (asoc->strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPENING)) { |
| 13897 | /* |
| 13898 | * Can't queue any data while stream reset is underway. |
| 13899 | */ |
| 13900 | if (asoc->strmout[srcv->sinfo_stream].state > SCTP_STREAM_OPEN) { |
| 13901 | error = EAGAIN; |
| 13902 | } else { |
| 13903 | error = EINVAL; |
| 13904 | } |
| 13905 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, error); |
| 13906 | goto out_unlocked; |
| 13907 | } |
| 13908 | if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) || |
| 13909 | (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) { |
| 13910 | queue_only = 1; |
| 13911 | } |
| 13912 | /* we are now done with all control */ |
| 13913 | if (control) { |
| 13914 | sctp_m_freem(control); |
| 13915 | control = NULL; |
| 13916 | } |
| 13917 | if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) || |
| 13918 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) || |
| 13919 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) || |
| 13920 | (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) { |
| 13921 | if (sinfo_flags & SCTP_ABORT) { |
| 13922 | ; |
| 13923 | } else { |
| 13924 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); |
| 13925 | error = ECONNRESET; |
| 13926 | goto out_unlocked; |
| 13927 | } |
| 13928 | } |
| 13929 | /* Ok, we will attempt a msgsnd :> */ |
| 13930 | #if !(defined(_WIN32) || defined(__Userspace__)) |
| 13931 | if (p) { |
| 13932 | #if defined(__FreeBSD__) |
| 13933 | p->td_ru.ru_msgsnd++; |
| 13934 | #else |
| 13935 | p->p_stats->p_ru.ru_msgsnd++; |
| 13936 | #endif |
| 13937 | } |
| 13938 | #endif |
| 13939 | /* Are we aborting? */ |
| 13940 | if (sinfo_flags & SCTP_ABORT) { |
| 13941 | struct mbuf *mm; |
| 13942 | ssize_t tot_demand, tot_out = 0, max_out; |
| 13943 |
| 13944 | SCTP_STAT_INCR(sctps_sends_with_abort); |
| 13945 | if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) || |
| 13946 | (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) { |
| 13947 | /* It has to be up before we abort */ |
| 13948 | /* how big is the user initiated abort? */ |
| 13949 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 13950 | error = EINVAL; |
| 13951 | goto out; |
| 13952 | } |
| 13953 | if (hold_tcblock) { |
| 13954 | SCTP_TCB_UNLOCK(stcb); |
| 13955 | hold_tcblock = 0; |
| 13956 | } |
| 13957 | if (top) { |
| 13958 | struct mbuf *cntm = NULL; |
| 13959 |
| 13960 | mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_WAITOK, 1, MT_DATA); |
| 13961 | if (sndlen != 0) { |
| 13962 | for (cntm = top; cntm; cntm = SCTP_BUF_NEXT(cntm)) { |
| 13963 | tot_out += SCTP_BUF_LEN(cntm); |
| 13964 | } |
| 13965 | } |
| 13966 | } else { |
| 13967 | /* Must fit in a MTU */ |
| 13968 | tot_out = sndlen; |
| 13969 | tot_demand = (tot_out + sizeof(struct sctp_paramhdr)); |
| 13970 | if (tot_demand > SCTP_DEFAULT_ADD_MORE) { |
| 13971 | /* To big */ |
| 13972 | SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE); |
| 13973 | error = EMSGSIZE; |
| 13974 | goto out; |
| 13975 | } |
| 13976 | mm = sctp_get_mbuf_for_msg((unsigned int)tot_demand, 0, M_WAITOK, 1, MT_DATA); |
| 13977 | } |
| 13978 | if (mm == NULL) { |
| 13979 | SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM); |
| 13980 | error = ENOMEM; |
| 13981 | goto out; |
| 13982 | } |
| 13983 | max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr); |
| 13984 | max_out -= sizeof(struct sctp_abort_msg); |
| 13985 | if (tot_out > max_out) { |
| 13986 | tot_out = max_out; |
| 13987 | } |
| 13988 | if (mm) { |
| 13989 | struct sctp_paramhdr *ph; |
| 13990 |
| 13991 | /* now move forward the data pointer */ |
| 13992 | ph = mtod(mm, struct sctp_paramhdr *); |
| 13993 | ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); |
| 13994 | ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + tot_out)); |
| 13995 | ph++; |
| 13996 | SCTP_BUF_LEN(mm) = (int)(tot_out + sizeof(struct sctp_paramhdr)); |
| 13997 | if (top == NULL) { |
| 13998 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 13999 | SCTP_SOCKET_UNLOCK(so, 0); |
| 14000 | #endif |
| 14001 | error = uiomove((caddr_t)ph, (int)tot_out, uio); |
| 14002 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 14003 | SCTP_SOCKET_LOCK(so, 0); |
| 14004 | #endif |
| 14005 | if (error) { |
| 14006 | /*- |
| 14007 | * Here if we can't get his data we |
| 14008 | * still abort we just don't get to |
| 14009 | * send the users note :-0 |
| 14010 | */ |
| 14011 | sctp_m_freem(mm); |
| 14012 | mm = NULL; |
| 14013 | } |
| 14014 | } else { |
| 14015 | if (sndlen != 0) { |
| 14016 | SCTP_BUF_NEXT(mm) = top; |
| 14017 | } |
| 14018 | } |
| 14019 | } |
| 14020 | if (hold_tcblock == 0) { |
| 14021 | SCTP_TCB_LOCK(stcb); |
| 14022 | } |
| 14023 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 14024 | free_cnt_applied = 0; |
| 14025 | /* release this lock, otherwise we hang on ourselves */ |
| 14026 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 14027 | NET_EPOCH_ENTER(et); |
| 14028 | #endif |
| 14029 | sctp_abort_an_association(stcb->sctp_ep, stcb, mm, false, SCTP_SO_LOCKED); |
| 14030 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 14031 | NET_EPOCH_EXIT(et); |
| 14032 | #endif |
| 14033 | /* now relock the stcb so everything is sane */ |
| 14034 | hold_tcblock = 0; |
| 14035 | stcb = NULL; |
| 14036 | /* In this case top is already chained to mm |
| 14037 | * avoid double free, since we free it below if |
| 14038 | * top != NULL and driver would free it after sending |
| 14039 | * the packet out |
| 14040 | */ |
| 14041 | if (sndlen != 0) { |
| 14042 | top = NULL; |
| 14043 | } |
| 14044 | goto out_unlocked; |
| 14045 | } |
| 14046 | /* Calculate the maximum we can send */ |
| 14047 | inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 14048 | if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) { |
| 14049 | max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes; |
| 14050 | } else { |
| 14051 | max_len = 0; |
| 14052 | } |
| 14053 | if (hold_tcblock) { |
| 14054 | SCTP_TCB_UNLOCK(stcb); |
| 14055 | hold_tcblock = 0; |
| 14056 | } |
| 14057 | if (asoc->strmout == NULL) { |
| 14058 | /* huh? software error */ |
| 14059 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT); |
| 14060 | error = EFAULT; |
| 14061 | goto out_unlocked; |
| 14062 | } |
| 14063 |
| 14064 | /* Unless E_EOR mode is on, we must make a send FIT in one call. */ |
| 14065 | if ((user_marks_eor == 0) && |
| 14066 | (sndlen > (ssize_t)SCTP_SB_LIMIT_SND(stcb->sctp_socket))) { |
| 14067 | /* It will NEVER fit */ |
| 14068 | SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE); |
| 14069 | error = EMSGSIZE; |
| 14070 | goto out_unlocked; |
| 14071 | } |
| 14072 | if ((uio == NULL) && user_marks_eor) { |
| 14073 | /*- |
| 14074 | * We do not support eeor mode for |
| 14075 | * sending with mbuf chains (like sendfile). |
| 14076 | */ |
| 14077 | SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 14078 | error = EINVAL; |
| 14079 | goto out_unlocked; |
| 14080 | } |
| 14081 |
| 14082 | if (user_marks_eor) { |
| 14083 | local_add_more = (ssize_t)min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold)); |
| 14084 | } else { |
| 14085 | /*- |
| 14086 | * For non-eeor the whole message must fit in |
| 14087 | * the socket send buffer. |
| 14088 | */ |
| 14089 | local_add_more = sndlen; |
| 14090 | } |
| 14091 | if (non_blocking) { |
| 14092 | goto skip_preblock; |
| 14093 | } |
| 14094 | if (((max_len <= local_add_more) && |
| 14095 | ((ssize_t)SCTP_SB_LIMIT_SND(so) >= local_add_more)) || |
| 14096 | (max_len == 0) || |
| 14097 | ((stcb->asoc.chunks_on_out_queue+stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) { |
| 14098 | /* No room right now ! */ |
| 14099 | SOCKBUF_LOCK(&so->so_snd); |
| 14100 | inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 14101 | while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) || |
| 14102 | ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) { |
| 14103 | SCTPDBG(SCTP_DEBUG_OUTPUT1,"pre_block limit:%u <(inq:%d + %zd) || (%d+%d > %d)\n", |
| 14104 | (unsigned int)SCTP_SB_LIMIT_SND(so), |
| 14105 | inqueue_bytes, |
| 14106 | local_add_more, |
| 14107 | stcb->asoc.stream_queue_cnt, |
| 14108 | stcb->asoc.chunks_on_out_queue, |
| 14109 | SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)); |
| 14110 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { |
| 14111 | sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, asoc, sndlen); |
| 14112 | } |
| 14113 | be.error = 0; |
| 14114 | #if !(defined(_WIN32) && !defined(__Userspace__)) |
| 14115 | stcb->block_entry = &be; |
| 14116 | #endif |
| 14117 | error = sbwait(&so->so_snd); |
| 14118 | stcb->block_entry = NULL; |
| 14119 | if (error || so->so_error || be.error) { |
| 14120 | if (error == 0) { |
| 14121 | if (so->so_error) |
| 14122 | error = so->so_error; |
| 14123 | if (be.error) { |
| 14124 | error = be.error; |
| 14125 | } |
| 14126 | } |
| 14127 | SOCKBUF_UNLOCK(&so->so_snd); |
| 14128 | goto out_unlocked; |
| 14129 | } |
| 14130 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { |
| 14131 | sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK, |
| 14132 | asoc, stcb->asoc.total_output_queue_size); |
| 14133 | } |
| 14134 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 14135 | SOCKBUF_UNLOCK(&so->so_snd); |
| 14136 | goto out_unlocked; |
| 14137 | } |
| 14138 | inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 14139 | } |
| 14140 | if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) { |
| 14141 | max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes; |
| 14142 | } else { |
| 14143 | max_len = 0; |
| 14144 | } |
| 14145 | SOCKBUF_UNLOCK(&so->so_snd); |
| 14146 | } |
| 14147 |
| 14148 | skip_preblock: |
| 14149 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 14150 | goto out_unlocked; |
| 14151 | } |
| 14152 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 14153 | error = sblock(&so->so_snd, SBLOCKWAIT(flags)); |
| 14154 | #endif |
| 14155 | /* sndlen covers for mbuf case |
| 14156 | * uio_resid covers for the non-mbuf case |
| 14157 | * NOTE: uio will be null when top/mbuf is passed |
| 14158 | */ |
| 14159 | if (sndlen == 0) { |
| 14160 | if (sinfo_flags & SCTP_EOF) { |
| 14161 | got_all_of_the_send = 1; |
| 14162 | goto dataless_eof; |
| 14163 | } else { |
| 14164 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 14165 | error = EINVAL; |
| 14166 | goto out; |
| 14167 | } |
| 14168 | } |
| 14169 | if (top == NULL) { |
| 14170 | struct sctp_stream_queue_pending *sp; |
| 14171 | struct sctp_stream_out *strm; |
| 14172 | uint32_t sndout; |
| 14173 |
| 14174 | SCTP_TCB_SEND_LOCK(stcb); |
| 14175 | if ((asoc->stream_locked) && |
| 14176 | (asoc->stream_locked_on != srcv->sinfo_stream)) { |
| 14177 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14178 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 14179 | error = EINVAL; |
| 14180 | goto out; |
| 14181 | } |
| 14182 | strm = &stcb->asoc.strmout[srcv->sinfo_stream]; |
| 14183 | if (strm->last_msg_incomplete == 0) { |
| 14184 | do_a_copy_in: |
| 14185 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14186 | sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error); |
| 14187 | if (error) { |
| 14188 | goto out; |
| 14189 | } |
| 14190 | SCTP_TCB_SEND_LOCK(stcb); |
| 14191 | if (sp->msg_is_complete) { |
| 14192 | strm->last_msg_incomplete = 0; |
| 14193 | asoc->stream_locked = 0; |
| 14194 | } else { |
| 14195 | /* Just got locked to this guy in |
| 14196 | * case of an interrupt. |
| 14197 | */ |
| 14198 | strm->last_msg_incomplete = 1; |
| 14199 | if (stcb->asoc.idata_supported == 0) { |
| 14200 | asoc->stream_locked = 1; |
| 14201 | asoc->stream_locked_on = srcv->sinfo_stream; |
| 14202 | } |
| 14203 | sp->sender_all_done = 0; |
| 14204 | } |
| 14205 | sctp_snd_sb_alloc(stcb, sp->length); |
| 14206 | atomic_add_int(&asoc->stream_queue_cnt, 1); |
| 14207 | if (sinfo_flags & SCTP_UNORDERED) { |
| 14208 | SCTP_STAT_INCR(sctps_sends_with_unord); |
| 14209 | } |
| 14210 | sp->processing = 1; |
| 14211 | TAILQ_INSERT_TAIL(&strm->outqueue, sp, next); |
| 14212 | stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp, 1); |
| 14213 | } else { |
| 14214 | sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead); |
| 14215 | if (sp == NULL) { |
| 14216 | /* ???? Huh ??? last msg is gone */ |
| 14217 | #ifdef INVARIANTS |
| 14218 | panic("Warning: Last msg marked incomplete, yet nothing left?"); |
| 14219 | #else |
| 14220 | SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n"); |
| 14221 | strm->last_msg_incomplete = 0; |
| 14222 | #endif |
| 14223 | goto do_a_copy_in; |
| 14224 | } |
| 14225 | if (sp->processing) { |
| 14226 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14227 | SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL); |
| 14228 | error = EINVAL; |
| 14229 | goto out; |
| 14230 | } else { |
| 14231 | sp->processing = 1; |
| 14232 | } |
| 14233 | } |
| 14234 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14235 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 14236 | #if defined(APPLE_LEOPARD) |
| 14237 | while (uio->uio_resid > 0) { |
| 14238 | #else |
| 14239 | while (uio_resid(uio) > 0) { |
| 14240 | #endif |
| 14241 | #else |
| 14242 | while (uio->uio_resid > 0) { |
| 14243 | #endif |
| 14244 | /* How much room do we have? */ |
| 14245 | struct mbuf *new_tail, *mm; |
| 14246 |
| 14247 | inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 14248 | if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) |
| 14249 | max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes; |
| 14250 | else |
| 14251 | max_len = 0; |
| 14252 |
| 14253 | if ((max_len > (ssize_t)SCTP_BASE_SYSCTL(sctp_add_more_threshold)) || |
| 14254 | (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) || |
| 14255 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 14256 | #if defined(APPLE_LEOPARD) |
| 14257 | (uio->uio_resid && (uio->uio_resid <= max_len))) { |
| 14258 | #else |
| 14259 | (uio_resid(uio) && (uio_resid(uio) <= max_len))) { |
| 14260 | #endif |
| 14261 | #else |
| 14262 | (uio->uio_resid && (uio->uio_resid <= max_len))) { |
| 14263 | #endif |
| 14264 | sndout = 0; |
| 14265 | new_tail = NULL; |
| 14266 | if (hold_tcblock) { |
| 14267 | SCTP_TCB_UNLOCK(stcb); |
| 14268 | hold_tcblock = 0; |
| 14269 | } |
| 14270 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 14271 | SCTP_SOCKET_UNLOCK(so, 0); |
| 14272 | #endif |
| 14273 | #if defined(__FreeBSD__) || defined(__Userspace__) |
| 14274 | mm = sctp_copy_resume(uio, (int)max_len, user_marks_eor, &error, &sndout, &new_tail); |
| 14275 | #else |
| 14276 | mm = sctp_copy_resume(uio, (int)max_len, &error, &sndout, &new_tail); |
| 14277 | #endif |
| 14278 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 14279 | SCTP_SOCKET_LOCK(so, 0); |
| 14280 | #endif |
| 14281 | if ((mm == NULL) || error) { |
| 14282 | if (mm) { |
| 14283 | sctp_m_freem(mm); |
| 14284 | } |
| 14285 | SCTP_TCB_SEND_LOCK(stcb); |
| 14286 | if (((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) && |
| 14287 | ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) == 0) && |
| 14288 | (sp != NULL)) { |
| 14289 | sp->processing = 0; |
| 14290 | } |
| 14291 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14292 | goto out; |
| 14293 | } |
| 14294 | /* Update the mbuf and count */ |
| 14295 | SCTP_TCB_SEND_LOCK(stcb); |
| 14296 | if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) || |
| 14297 | (stcb->asoc.state & SCTP_STATE_WAS_ABORTED)) { |
| 14298 | /* we need to get out. |
| 14299 | * Peer probably aborted. |
| 14300 | */ |
| 14301 | sctp_m_freem(mm); |
| 14302 | if (stcb->asoc.state & SCTP_STATE_WAS_ABORTED) { |
| 14303 | SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET); |
| 14304 | error = ECONNRESET; |
| 14305 | } |
| 14306 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14307 | goto out; |
| 14308 | } |
| 14309 | if (sp->tail_mbuf) { |
| 14310 | /* tack it to the end */ |
| 14311 | SCTP_BUF_NEXT(sp->tail_mbuf) = mm; |
| 14312 | sp->tail_mbuf = new_tail; |
| 14313 | } else { |
| 14314 | /* A stolen mbuf */ |
| 14315 | sp->data = mm; |
| 14316 | sp->tail_mbuf = new_tail; |
| 14317 | } |
| 14318 | sctp_snd_sb_alloc(stcb, sndout); |
| 14319 | atomic_add_int(&sp->length, sndout); |
| 14320 | if (sinfo_flags & SCTP_SACK_IMMEDIATELY) { |
| 14321 | sp->sinfo_flags |= SCTP_SACK_IMMEDIATELY; |
| 14322 | } |
| 14323 |
| 14324 | /* Did we reach EOR? */ |
| 14325 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 14326 | #if defined(APPLE_LEOPARD) |
| 14327 | if ((uio->uio_resid == 0) && |
| 14328 | #else |
| 14329 | if ((uio_resid(uio) == 0) && |
| 14330 | #endif |
| 14331 | #else |
| 14332 | if ((uio->uio_resid == 0) && |
| 14333 | #endif |
| 14334 | ((user_marks_eor == 0) || |
| 14335 | (sinfo_flags & SCTP_EOF) || |
| 14336 | (user_marks_eor && (sinfo_flags & SCTP_EOR)))) { |
| 14337 | sp->msg_is_complete = 1; |
| 14338 | } else { |
| 14339 | sp->msg_is_complete = 0; |
| 14340 | } |
| 14341 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14342 | } |
| 14343 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 14344 | #if defined(APPLE_LEOPARD) |
| 14345 | if (uio->uio_resid == 0) { |
| 14346 | #else |
| 14347 | if (uio_resid(uio) == 0) { |
| 14348 | #endif |
| 14349 | #else |
| 14350 | if (uio->uio_resid == 0) { |
| 14351 | #endif |
| 14352 | /* got it all? */ |
| 14353 | continue; |
| 14354 | } |
| 14355 | /* PR-SCTP? */ |
| 14356 | if ((asoc->prsctp_supported) && (asoc->sent_queue_cnt_removeable > 0)) { |
| 14357 | /* This is ugly but we must assure locking order */ |
| 14358 | if (hold_tcblock == 0) { |
| 14359 | SCTP_TCB_LOCK(stcb); |
| 14360 | hold_tcblock = 1; |
| 14361 | } |
| 14362 | sctp_prune_prsctp(stcb, asoc, srcv, (int)sndlen); |
| 14363 | inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 14364 | if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) |
| 14365 | max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes; |
| 14366 | else |
| 14367 | max_len = 0; |
| 14368 | if (max_len > 0) { |
| 14369 | continue; |
| 14370 | } |
| 14371 | SCTP_TCB_UNLOCK(stcb); |
| 14372 | hold_tcblock = 0; |
| 14373 | } |
| 14374 | /* wait for space now */ |
| 14375 | if (non_blocking) { |
| 14376 | /* Non-blocking io in place out */ |
| 14377 | SCTP_TCB_SEND_LOCK(stcb); |
| 14378 | if (sp != NULL) { |
| 14379 | sp->processing = 0; |
| 14380 | } |
| 14381 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14382 | goto skip_out_eof; |
| 14383 | } |
| 14384 | /* What about the INIT, send it maybe */ |
| 14385 | if (queue_only_for_init) { |
| 14386 | if (hold_tcblock == 0) { |
| 14387 | SCTP_TCB_LOCK(stcb); |
| 14388 | hold_tcblock = 1; |
| 14389 | } |
| 14390 | if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) { |
| 14391 | /* a collision took us forward? */ |
| 14392 | queue_only = 0; |
| 14393 | } else { |
| 14394 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 14395 | NET_EPOCH_ENTER(et); |
| 14396 | #endif |
| 14397 | sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); |
| 14398 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 14399 | NET_EPOCH_EXIT(et); |
| 14400 | #endif |
| 14401 | SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT); |
| 14402 | queue_only = 1; |
| 14403 | } |
| 14404 | } |
| 14405 | if ((net->flight_size > net->cwnd) && |
| 14406 | (asoc->sctp_cmt_on_off == 0)) { |
| 14407 | SCTP_STAT_INCR(sctps_send_cwnd_avoid); |
| 14408 | queue_only = 1; |
| 14409 | } else if (asoc->ifp_had_enobuf) { |
| 14410 | SCTP_STAT_INCR(sctps_ifnomemqueued); |
| 14411 | if (net->flight_size > (2 * net->mtu)) { |
| 14412 | queue_only = 1; |
| 14413 | } |
| 14414 | asoc->ifp_had_enobuf = 0; |
| 14415 | } |
| 14416 | un_sent = stcb->asoc.total_output_queue_size - stcb->asoc.total_flight; |
| 14417 | if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) && |
| 14418 | (stcb->asoc.total_flight > 0) && |
| 14419 | (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) && |
| 14420 | (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) { |
| 14421 | /*- |
| 14422 | * Ok, Nagle is set on and we have data outstanding. |
| 14423 | * Don't send anything and let SACKs drive out the |
| 14424 | * data unless we have a "full" segment to send. |
| 14425 | */ |
| 14426 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) { |
| 14427 | sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED); |
| 14428 | } |
| 14429 | SCTP_STAT_INCR(sctps_naglequeued); |
| 14430 | nagle_applies = 1; |
| 14431 | } else { |
| 14432 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) { |
| 14433 | if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) |
| 14434 | sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED); |
| 14435 | } |
| 14436 | SCTP_STAT_INCR(sctps_naglesent); |
| 14437 | nagle_applies = 0; |
| 14438 | } |
| 14439 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { |
| 14440 | sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only, |
| 14441 | nagle_applies, un_sent); |
| 14442 | sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size, |
| 14443 | stcb->asoc.total_flight, |
| 14444 | stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count); |
| 14445 | } |
| 14446 | if (queue_only_for_init) |
| 14447 | queue_only_for_init = 0; |
| 14448 | if ((queue_only == 0) && (nagle_applies == 0)) { |
| 14449 | /*- |
| 14450 | * need to start chunk output |
| 14451 | * before blocking.. note that if |
| 14452 | * a lock is already applied, then |
| 14453 | * the input via the net is happening |
| 14454 | * and I don't need to start output :-D |
| 14455 | */ |
| 14456 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 14457 | NET_EPOCH_ENTER(et); |
| 14458 | #endif |
| 14459 | if (hold_tcblock == 0) { |
| 14460 | if (SCTP_TCB_TRYLOCK(stcb)) { |
| 14461 | hold_tcblock = 1; |
| 14462 | sctp_chunk_output(inp, |
| 14463 | stcb, |
| 14464 | SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); |
| 14465 | } |
| 14466 | } else { |
| 14467 | sctp_chunk_output(inp, |
| 14468 | stcb, |
| 14469 | SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); |
| 14470 | } |
| 14471 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 14472 | NET_EPOCH_EXIT(et); |
| 14473 | #endif |
| 14474 | } |
| 14475 | if (hold_tcblock == 1) { |
| 14476 | SCTP_TCB_UNLOCK(stcb); |
| 14477 | hold_tcblock = 0; |
| 14478 | } |
| 14479 | SOCKBUF_LOCK(&so->so_snd); |
| 14480 | /*- |
| 14481 | * This is a bit strange, but I think it will |
| 14482 | * work. The total_output_queue_size is locked and |
| 14483 | * protected by the TCB_LOCK, which we just released. |
| 14484 | * There is a race that can occur between releasing it |
| 14485 | * above, and me getting the socket lock, where sacks |
| 14486 | * come in but we have not put the SB_WAIT on the |
| 14487 | * so_snd buffer to get the wakeup. After the LOCK |
| 14488 | * is applied the sack_processing will also need to |
| 14489 | * LOCK the so->so_snd to do the actual sowwakeup(). So |
| 14490 | * once we have the socket buffer lock if we recheck the |
| 14491 | * size we KNOW we will get to sleep safely with the |
| 14492 | * wakeup flag in place. |
| 14493 | */ |
| 14494 | inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb)); |
| 14495 | if (SCTP_SB_LIMIT_SND(so) <= (inqueue_bytes + |
| 14496 | min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) { |
| 14497 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { |
| 14498 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 14499 | #if defined(APPLE_LEOPARD) |
| 14500 | sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK, |
| 14501 | asoc, uio->uio_resid); |
| 14502 | #else |
| 14503 | sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK, |
| 14504 | asoc, uio_resid(uio)); |
| 14505 | #endif |
| 14506 | #else |
| 14507 | sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK, |
| 14508 | asoc, uio->uio_resid); |
| 14509 | #endif |
| 14510 | } |
| 14511 | be.error = 0; |
| 14512 | #if !(defined(_WIN32) && !defined(__Userspace__)) |
| 14513 | stcb->block_entry = &be; |
| 14514 | #endif |
| 14515 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 14516 | sbunlock(&so->so_snd, 1); |
| 14517 | #endif |
| 14518 | error = sbwait(&so->so_snd); |
| 14519 | stcb->block_entry = NULL; |
| 14520 |
| 14521 | if (error || so->so_error || be.error) { |
| 14522 | if (error == 0) { |
| 14523 | if (so->so_error) |
| 14524 | error = so->so_error; |
| 14525 | if (be.error) { |
| 14526 | error = be.error; |
| 14527 | } |
| 14528 | } |
| 14529 | SOCKBUF_UNLOCK(&so->so_snd); |
| 14530 | SCTP_TCB_SEND_LOCK(stcb); |
| 14531 | if (((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) && |
| 14532 | ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) == 0) && |
| 14533 | (sp != NULL)) { |
| 14534 | sp->processing = 0; |
| 14535 | } |
| 14536 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14537 | goto out_unlocked; |
| 14538 | } |
| 14539 |
| 14540 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 14541 | error = sblock(&so->so_snd, SBLOCKWAIT(flags)); |
| 14542 | #endif |
| 14543 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { |
| 14544 | sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK, |
| 14545 | asoc, stcb->asoc.total_output_queue_size); |
| 14546 | } |
| 14547 | } |
| 14548 | SOCKBUF_UNLOCK(&so->so_snd); |
| 14549 | SCTP_TCB_SEND_LOCK(stcb); |
| 14550 | if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) || |
| 14551 | (stcb->asoc.state & SCTP_STATE_WAS_ABORTED)) { |
| 14552 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14553 | goto out_unlocked; |
| 14554 | } |
| 14555 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14556 | } |
| 14557 | SCTP_TCB_SEND_LOCK(stcb); |
| 14558 | if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) || |
| 14559 | (stcb->asoc.state & SCTP_STATE_WAS_ABORTED)) { |
| 14560 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14561 | goto out_unlocked; |
| 14562 | } |
| 14563 | if (sp) { |
| 14564 | if (sp->msg_is_complete == 0) { |
| 14565 | strm->last_msg_incomplete = 1; |
| 14566 | if (stcb->asoc.idata_supported == 0) { |
| 14567 | asoc->stream_locked = 1; |
| 14568 | asoc->stream_locked_on = srcv->sinfo_stream; |
| 14569 | } |
| 14570 | } else { |
| 14571 | sp->sender_all_done = 1; |
| 14572 | strm->last_msg_incomplete = 0; |
| 14573 | asoc->stream_locked = 0; |
| 14574 | } |
| 14575 | sp->processing = 0; |
| 14576 | } else { |
| 14577 | SCTP_PRINTF("Huh no sp TSNH?\n"); |
| 14578 | strm->last_msg_incomplete = 0; |
| 14579 | asoc->stream_locked = 0; |
| 14580 | } |
| 14581 | SCTP_TCB_SEND_UNLOCK(stcb); |
| 14582 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 14583 | #if defined(APPLE_LEOPARD) |
| 14584 | if (uio->uio_resid == 0) { |
| 14585 | #else |
| 14586 | if (uio_resid(uio) == 0) { |
| 14587 | #endif |
| 14588 | #else |
| 14589 | if (uio->uio_resid == 0) { |
| 14590 | #endif |
| 14591 | got_all_of_the_send = 1; |
| 14592 | } |
| 14593 | } else { |
| 14594 | /* We send in a 0, since we do NOT have any locks */ |
| 14595 | error = sctp_msg_append(stcb, net, top, srcv, 0); |
| 14596 | top = NULL; |
| 14597 | if (sinfo_flags & SCTP_EOF) { |
| 14598 | got_all_of_the_send = 1; |
| 14599 | } |
| 14600 | } |
| 14601 | if (error) { |
| 14602 | goto out; |
| 14603 | } |
| 14604 | dataless_eof: |
| 14605 | /* EOF thing ? */ |
| 14606 | if ((sinfo_flags & SCTP_EOF) && |
| 14607 | (got_all_of_the_send == 1)) { |
| 14608 | SCTP_STAT_INCR(sctps_sends_with_eof); |
| 14609 | error = 0; |
| 14610 | if (hold_tcblock == 0) { |
| 14611 | SCTP_TCB_LOCK(stcb); |
| 14612 | hold_tcblock = 1; |
| 14613 | } |
| 14614 | if (TAILQ_EMPTY(&asoc->send_queue) && |
| 14615 | TAILQ_EMPTY(&asoc->sent_queue) && |
| 14616 | sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED) == 0) { |
| 14617 | if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) { |
| 14618 | goto abort_anyway; |
| 14619 | } |
| 14620 | /* there is nothing queued to send, so I'm done... */ |
| 14621 | if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) && |
| 14622 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) && |
| 14623 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { |
| 14624 | struct sctp_nets *netp; |
| 14625 |
| 14626 | /* only send SHUTDOWN the first time through */ |
| 14627 | if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) { |
| 14628 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
| 14629 | } |
| 14630 | SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT); |
| 14631 | sctp_stop_timers_for_shutdown(stcb); |
| 14632 | if (stcb->asoc.alternate) { |
| 14633 | netp = stcb->asoc.alternate; |
| 14634 | } else { |
| 14635 | netp = stcb->asoc.primary_destination; |
| 14636 | } |
| 14637 | sctp_send_shutdown(stcb, netp); |
| 14638 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, |
| 14639 | netp); |
| 14640 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, |
| 14641 | NULL); |
| 14642 | } |
| 14643 | } else { |
| 14644 | /*- |
| 14645 | * we still got (or just got) data to send, so set |
| 14646 | * SHUTDOWN_PENDING |
| 14647 | */ |
| 14648 | /*- |
| 14649 | * XXX sockets draft says that SCTP_EOF should be |
| 14650 | * sent with no data. currently, we will allow user |
| 14651 | * data to be sent first and move to |
| 14652 | * SHUTDOWN-PENDING |
| 14653 | */ |
| 14654 | if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) && |
| 14655 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) && |
| 14656 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { |
| 14657 | if (hold_tcblock == 0) { |
| 14658 | SCTP_TCB_LOCK(stcb); |
| 14659 | hold_tcblock = 1; |
| 14660 | } |
| 14661 | if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) { |
| 14662 | SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT); |
| 14663 | } |
| 14664 | SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING); |
| 14665 | if (TAILQ_EMPTY(&asoc->send_queue) && |
| 14666 | TAILQ_EMPTY(&asoc->sent_queue) && |
| 14667 | (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) { |
| 14668 | struct mbuf *op_err; |
| 14669 | char msg[SCTP_DIAG_INFO_LEN]; |
| 14670 |
| 14671 | abort_anyway: |
| 14672 | if (free_cnt_applied) { |
| 14673 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 14674 | free_cnt_applied = 0; |
| 14675 | } |
| 14676 | SCTP_SNPRINTF(msg, sizeof(msg), |
| 14677 | "%s:%d at %s", __FILE__, __LINE__, __func__); |
| 14678 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 14679 | msg); |
| 14680 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 14681 | NET_EPOCH_ENTER(et); |
| 14682 | #endif |
| 14683 | sctp_abort_an_association(stcb->sctp_ep, stcb, |
| 14684 | op_err, false, SCTP_SO_LOCKED); |
| 14685 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 14686 | NET_EPOCH_EXIT(et); |
| 14687 | #endif |
| 14688 | /* now relock the stcb so everything is sane */ |
| 14689 | hold_tcblock = 0; |
| 14690 | stcb = NULL; |
| 14691 | goto out; |
| 14692 | } |
| 14693 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, |
| 14694 | NULL); |
| 14695 | sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY); |
| 14696 | } |
| 14697 | } |
| 14698 | } |
| 14699 | skip_out_eof: |
| 14700 | if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { |
| 14701 | some_on_control = 1; |
| 14702 | } |
| 14703 | if (queue_only_for_init) { |
| 14704 | if (hold_tcblock == 0) { |
| 14705 | SCTP_TCB_LOCK(stcb); |
| 14706 | hold_tcblock = 1; |
| 14707 | } |
| 14708 | if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) { |
| 14709 | /* a collision took us forward? */ |
| 14710 | queue_only = 0; |
| 14711 | } else { |
| 14712 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 14713 | NET_EPOCH_ENTER(et); |
| 14714 | #endif |
| 14715 | sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); |
| 14716 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 14717 | NET_EPOCH_EXIT(et); |
| 14718 | #endif |
| 14719 | SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT); |
| 14720 | queue_only = 1; |
| 14721 | } |
| 14722 | } |
| 14723 | if ((net->flight_size > net->cwnd) && |
| 14724 | (stcb->asoc.sctp_cmt_on_off == 0)) { |
| 14725 | SCTP_STAT_INCR(sctps_send_cwnd_avoid); |
| 14726 | queue_only = 1; |
| 14727 | } else if (asoc->ifp_had_enobuf) { |
| 14728 | SCTP_STAT_INCR(sctps_ifnomemqueued); |
| 14729 | if (net->flight_size > (2 * net->mtu)) { |
| 14730 | queue_only = 1; |
| 14731 | } |
| 14732 | asoc->ifp_had_enobuf = 0; |
| 14733 | } |
| 14734 | un_sent = stcb->asoc.total_output_queue_size - stcb->asoc.total_flight; |
| 14735 | if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) && |
| 14736 | (stcb->asoc.total_flight > 0) && |
| 14737 | (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) && |
| 14738 | (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) { |
| 14739 | /*- |
| 14740 | * Ok, Nagle is set on and we have data outstanding. |
| 14741 | * Don't send anything and let SACKs drive out the |
| 14742 | * data unless wen have a "full" segment to send. |
| 14743 | */ |
| 14744 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) { |
| 14745 | sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED); |
| 14746 | } |
| 14747 | SCTP_STAT_INCR(sctps_naglequeued); |
| 14748 | nagle_applies = 1; |
| 14749 | } else { |
| 14750 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) { |
| 14751 | if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) |
| 14752 | sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED); |
| 14753 | } |
| 14754 | SCTP_STAT_INCR(sctps_naglesent); |
| 14755 | nagle_applies = 0; |
| 14756 | } |
| 14757 | if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) { |
| 14758 | sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only, |
| 14759 | nagle_applies, un_sent); |
| 14760 | sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size, |
| 14761 | stcb->asoc.total_flight, |
| 14762 | stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count); |
| 14763 | } |
| 14764 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 14765 | NET_EPOCH_ENTER(et); |
| 14766 | #endif |
| 14767 | if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) { |
| 14768 | /* we can attempt to send too. */ |
| 14769 | if (hold_tcblock == 0) { |
| 14770 | /* If there is activity recv'ing sacks no need to send */ |
| 14771 | if (SCTP_TCB_TRYLOCK(stcb)) { |
| 14772 | sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); |
| 14773 | hold_tcblock = 1; |
| 14774 | } |
| 14775 | } else { |
| 14776 | sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); |
| 14777 | } |
| 14778 | } else if ((queue_only == 0) && |
| 14779 | (stcb->asoc.peers_rwnd == 0) && |
| 14780 | (stcb->asoc.total_flight == 0)) { |
| 14781 | /* We get to have a probe outstanding */ |
| 14782 | if (hold_tcblock == 0) { |
| 14783 | hold_tcblock = 1; |
| 14784 | SCTP_TCB_LOCK(stcb); |
| 14785 | } |
| 14786 | sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED); |
| 14787 | } else if (some_on_control) { |
| 14788 | int num_out, reason, frag_point; |
| 14789 |
| 14790 | /* Here we do control only */ |
| 14791 | if (hold_tcblock == 0) { |
| 14792 | hold_tcblock = 1; |
| 14793 | SCTP_TCB_LOCK(stcb); |
| 14794 | } |
| 14795 | frag_point = sctp_get_frag_point(stcb, &stcb->asoc); |
| 14796 | (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out, |
| 14797 | &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED); |
| 14798 | } |
| 14799 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 14800 | NET_EPOCH_EXIT(et); |
| 14801 | #endif |
| 14802 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n", |
| 14803 | queue_only, stcb->asoc.peers_rwnd, un_sent, |
| 14804 | stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue, |
| 14805 | stcb->asoc.total_output_queue_size, error); |
| 14806 |
| 14807 | out: |
| 14808 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 14809 | sbunlock(&so->so_snd, 1); |
| 14810 | #endif |
| 14811 | out_unlocked: |
| 14812 |
| 14813 | if (local_soresv && stcb) { |
| 14814 | atomic_subtract_int(&stcb->asoc.sb_send_resv, (int)sndlen); |
| 14815 | } |
| 14816 | if (create_lock_applied) { |
| 14817 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 14818 | } |
| 14819 | if ((stcb) && hold_tcblock) { |
| 14820 | SCTP_TCB_UNLOCK(stcb); |
| 14821 | } |
| 14822 | if (stcb && free_cnt_applied) { |
| 14823 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 14824 | } |
| 14825 | #ifdef INVARIANTS |
| 14826 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 14827 | if (stcb) { |
| 14828 | if (mtx_owned(&stcb->tcb_mtx)) { |
| 14829 | panic("Leaving with tcb mtx owned?"); |
| 14830 | } |
| 14831 | if (mtx_owned(&stcb->tcb_send_mtx)) { |
| 14832 | panic("Leaving with tcb send mtx owned?"); |
| 14833 | } |
| 14834 | } |
| 14835 | #endif |
| 14836 | #endif |
| 14837 | if (top) { |
| 14838 | sctp_m_freem(top); |
| 14839 | } |
| 14840 | if (control) { |
| 14841 | sctp_m_freem(control); |
| 14842 | } |
| 14843 | return (error); |
| 14844 | } |
| 14845 |
| 14846 | /* |
| 14847 | * generate an AUTHentication chunk, if required |
| 14848 | */ |
| 14849 | struct mbuf * |
| 14850 | sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end, |
| 14851 | struct sctp_auth_chunk **auth_ret, uint32_t * offset, |
| 14852 | struct sctp_tcb *stcb, uint8_t chunk) |
| 14853 | { |
| 14854 | struct mbuf *m_auth; |
| 14855 | struct sctp_auth_chunk *auth; |
| 14856 | int chunk_len; |
| 14857 | struct mbuf *cn; |
| 14858 |
| 14859 | if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) || |
| 14860 | (stcb == NULL)) |
| 14861 | return (m); |
| 14862 |
| 14863 | if (stcb->asoc.auth_supported == 0) { |
| 14864 | return (m); |
| 14865 | } |
| 14866 | /* does the requested chunk require auth? */ |
| 14867 | if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) { |
| 14868 | return (m); |
| 14869 | } |
| 14870 | m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_NOWAIT, 1, MT_HEADER); |
| 14871 | if (m_auth == NULL) { |
| 14872 | /* no mbuf's */ |
| 14873 | return (m); |
| 14874 | } |
| 14875 | /* reserve some space if this will be the first mbuf */ |
| 14876 | if (m == NULL) |
| 14877 | SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD); |
| 14878 | /* fill in the AUTH chunk details */ |
| 14879 | auth = mtod(m_auth, struct sctp_auth_chunk *); |
| 14880 | memset(auth, 0, sizeof(*auth)); |
| 14881 | auth->ch.chunk_type = SCTP_AUTHENTICATION; |
| 14882 | auth->ch.chunk_flags = 0; |
| 14883 | chunk_len = sizeof(*auth) + |
| 14884 | sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id); |
| 14885 | auth->ch.chunk_length = htons(chunk_len); |
| 14886 | auth->hmac_id = htons(stcb->asoc.peer_hmac_id); |
| 14887 | /* key id and hmac digest will be computed and filled in upon send */ |
| 14888 |
| 14889 | /* save the offset where the auth was inserted into the chain */ |
| 14890 | *offset = 0; |
| 14891 | for (cn = m; cn; cn = SCTP_BUF_NEXT(cn)) { |
| 14892 | *offset += SCTP_BUF_LEN(cn); |
| 14893 | } |
| 14894 |
| 14895 | /* update length and return pointer to the auth chunk */ |
| 14896 | SCTP_BUF_LEN(m_auth) = chunk_len; |
| 14897 | m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0); |
| 14898 | if (auth_ret != NULL) |
| 14899 | *auth_ret = auth; |
| 14900 |
| 14901 | return (m); |
| 14902 | } |
| 14903 |
| 14904 | #if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__) |
| 14905 | #ifdef INET6 |
| 14906 | int |
| 14907 | sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t *ro) |
| 14908 | { |
| 14909 | struct nd_prefix *pfx = NULL; |
| 14910 | struct nd_pfxrouter *pfxrtr = NULL; |
| 14911 | struct sockaddr_in6 gw6; |
| 14912 |
| 14913 | #if defined(__FreeBSD__) |
| 14914 | if (ro == NULL || ro->ro_nh == NULL || src6->sin6_family != AF_INET6) |
| 14915 | #else |
| 14916 | if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6) |
| 14917 | #endif |
| 14918 | return (0); |
| 14919 |
| 14920 | /* get prefix entry of address */ |
| 14921 | #if defined(__FreeBSD__) |
| 14922 | ND6_RLOCK(); |
| 14923 | #endif |
| 14924 | LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) { |
| 14925 | if (pfx->ndpr_stateflags & NDPRF_DETACHED) |
| 14926 | continue; |
| 14927 | if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr, |
| 14928 | &src6->sin6_addr, &pfx->ndpr_mask)) |
| 14929 | break; |
| 14930 | } |
| 14931 | /* no prefix entry in the prefix list */ |
| 14932 | if (pfx == NULL) { |
| 14933 | #if defined(__FreeBSD__) |
| 14934 | ND6_RUNLOCK(); |
| 14935 | #endif |
| 14936 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for "); |
| 14937 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6); |
| 14938 | return (0); |
| 14939 | } |
| 14940 |
| 14941 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is "); |
| 14942 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6); |
| 14943 |
| 14944 | /* search installed gateway from prefix entry */ |
| 14945 | LIST_FOREACH(pfxrtr, &pfx->ndpr_advrtrs, pfr_entry) { |
| 14946 | memset(&gw6, 0, sizeof(struct sockaddr_in6)); |
| 14947 | gw6.sin6_family = AF_INET6; |
| 14948 | #ifdef HAVE_SIN6_LEN |
| 14949 | gw6.sin6_len = sizeof(struct sockaddr_in6); |
| 14950 | #endif |
| 14951 | memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr, |
| 14952 | sizeof(struct in6_addr)); |
| 14953 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is "); |
| 14954 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6); |
| 14955 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is "); |
| 14956 | #if defined(__FreeBSD__) |
| 14957 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ro->ro_nh->gw_sa); |
| 14958 | #else |
| 14959 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway); |
| 14960 | #endif |
| 14961 | #if defined(__FreeBSD__) |
| 14962 | if (sctp_cmpaddr((struct sockaddr *)&gw6, &ro->ro_nh->gw_sa)) { |
| 14963 | ND6_RUNLOCK(); |
| 14964 | #else |
| 14965 | if (sctp_cmpaddr((struct sockaddr *)&gw6, ro->ro_rt->rt_gateway)) { |
| 14966 | #endif |
| 14967 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n"); |
| 14968 | return (1); |
| 14969 | } |
| 14970 | } |
| 14971 | #if defined(__FreeBSD__) |
| 14972 | ND6_RUNLOCK(); |
| 14973 | #endif |
| 14974 | SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n"); |
| 14975 | return (0); |
| 14976 | } |
| 14977 | #endif |
| 14978 |
| 14979 | int |
| 14980 | sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t *ro) |
| 14981 | { |
| 14982 | #ifdef INET |
| 14983 | struct sockaddr_in *sin, *mask; |
| 14984 | struct ifaddr *ifa; |
| 14985 | struct in_addr srcnetaddr, gwnetaddr; |
| 14986 |
| 14987 | #if defined(__FreeBSD__) |
| 14988 | if (ro == NULL || ro->ro_nh == NULL || |
| 14989 | #else |
| 14990 | if (ro == NULL || ro->ro_rt == NULL || |
| 14991 | #endif |
| 14992 | sifa->address.sa.sa_family != AF_INET) { |
| 14993 | return (0); |
| 14994 | } |
| 14995 | ifa = (struct ifaddr *)sifa->ifa; |
| 14996 | mask = (struct sockaddr_in *)(ifa->ifa_netmask); |
| 14997 | sin = &sifa->address.sin; |
| 14998 | srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr); |
| 14999 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is "); |
| 15000 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa); |
| 15001 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr); |
| 15002 |
| 15003 | #if defined(__FreeBSD__) |
| 15004 | sin = &ro->ro_nh->gw4_sa; |
| 15005 | #else |
| 15006 | sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway; |
| 15007 | #endif |
| 15008 | gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr); |
| 15009 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is "); |
| 15010 | #if defined(__FreeBSD__) |
| 15011 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ro->ro_nh->gw_sa); |
| 15012 | #else |
| 15013 | SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway); |
| 15014 | #endif |
| 15015 | SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr); |
| 15016 | if (srcnetaddr.s_addr == gwnetaddr.s_addr) { |
| 15017 | return (1); |
| 15018 | } |
| 15019 | #endif |
| 15020 | return (0); |
| 15021 | } |
| 15022 | #elif defined(__Userspace__) |
| 15023 | /* TODO __Userspace__ versions of sctp_vXsrc_match_nexthop(). */ |
| 15024 | int |
| 15025 | sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t *ro) |
| 15026 | { |
| 15027 | return (0); |
| 15028 | } |
| 15029 | int |
| 15030 | sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t *ro) |
| 15031 | { |
| 15032 | return (0); |
| 15033 | } |
| 15034 |
| 15035 | #endif |
| 15036 |