1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved. 5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. 6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. 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 #include <netinet/sctp_os.h> 36 #include <sys/proc.h> 37 #include <netinet/sctp_var.h> 38 #include <netinet/sctp_sysctl.h> 39 #include <netinet/sctp_pcb.h> 40 #include <netinet/sctputil.h> 41 #include <netinet/sctp.h> 42 #include <netinet/sctp_header.h> 43 #include <netinet/sctp_asconf.h> 44 #include <netinet/sctp_output.h> 45 #include <netinet/sctp_timer.h> 46 #include <netinet/sctp_bsd_addr.h> 47 #if defined(INET) || defined(INET6) 48 #include <netinet/udp.h> 49 #endif 50 #ifdef INET6 51 #include <netinet6/ip6_var.h> 52 #endif 53 #include <sys/sched.h> 54 #include <sys/smp.h> 55 #include <sys/unistd.h> 56 57 /* FIX: we don't handle multiple link local scopes */ 58 /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */ 59 #ifdef INET6 60 int 61 SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b) 62 { 63 struct sockaddr_in6 tmp_a, tmp_b; 64 65 memcpy(&tmp_a, a, sizeof(struct sockaddr_in6)); 66 if (sa6_embedscope(&tmp_a, MODULE_GLOBAL(ip6_use_defzone)) != 0) { 67 return (0); 68 } 69 memcpy(&tmp_b, b, sizeof(struct sockaddr_in6)); 70 if (sa6_embedscope(&tmp_b, MODULE_GLOBAL(ip6_use_defzone)) != 0) { 71 return (0); 72 } 73 return (IN6_ARE_ADDR_EQUAL(&tmp_a.sin6_addr, &tmp_b.sin6_addr)); 74 } 75 #endif 76 77 void 78 sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb) 79 { 80 /* 81 * We really don't need to lock this, but I will just because it 82 * does not hurt. 83 */ 84 SCTP_INP_INFO_RLOCK(); 85 spcb->ep_count = SCTP_BASE_INFO(ipi_count_ep); 86 spcb->asoc_count = SCTP_BASE_INFO(ipi_count_asoc); 87 spcb->laddr_count = SCTP_BASE_INFO(ipi_count_laddr); 88 spcb->raddr_count = SCTP_BASE_INFO(ipi_count_raddr); 89 spcb->chk_count = SCTP_BASE_INFO(ipi_count_chunk); 90 spcb->readq_count = SCTP_BASE_INFO(ipi_count_readq); 91 spcb->stream_oque = SCTP_BASE_INFO(ipi_count_strmoq); 92 spcb->free_chunks = SCTP_BASE_INFO(ipi_free_chunks); 93 SCTP_INP_INFO_RUNLOCK(); 94 } 95 96 /*- 97 * Addresses are added to VRF's (Virtual Router's). For BSD we 98 * have only the default VRF 0. We maintain a hash list of 99 * VRF's. Each VRF has its own list of sctp_ifn's. Each of 100 * these has a list of addresses. When we add a new address 101 * to a VRF we lookup the ifn/ifn_index, if the ifn does 102 * not exist we create it and add it to the list of IFN's 103 * within the VRF. Once we have the sctp_ifn, we add the 104 * address to the list. So we look something like: 105 * 106 * hash-vrf-table 107 * vrf-> ifn-> ifn -> ifn 108 * vrf | 109 * ... +--ifa-> ifa -> ifa 110 * vrf 111 * 112 * We keep these separate lists since the SCTP subsystem will 113 * point to these from its source address selection nets structure. 114 * When an address is deleted it does not happen right away on 115 * the SCTP side, it gets scheduled. What we do when a 116 * delete happens is immediately remove the address from 117 * the master list and decrement the refcount. As our 118 * addip iterator works through and frees the src address 119 * selection pointing to the sctp_ifa, eventually the refcount 120 * will reach 0 and we will delete it. Note that it is assumed 121 * that any locking on system level ifn/ifa is done at the 122 * caller of these functions and these routines will only 123 * lock the SCTP structures as they add or delete things. 124 * 125 * Other notes on VRF concepts. 126 * - An endpoint can be in multiple VRF's 127 * - An association lives within a VRF and only one VRF. 128 * - Any incoming packet we can deduce the VRF for by 129 * looking at the mbuf/pak inbound (for BSD its VRF=0 :D) 130 * - Any downward send call or connect call must supply the 131 * VRF via ancillary data or via some sort of set default 132 * VRF socket option call (again for BSD no brainer since 133 * the VRF is always 0). 134 * - An endpoint may add multiple VRF's to it. 135 * - Listening sockets can accept associations in any 136 * of the VRF's they are in but the assoc will end up 137 * in only one VRF (gotten from the packet or connect/send). 138 * 139 */ 140 141 struct sctp_vrf * 142 sctp_allocate_vrf(int vrf_id) 143 { 144 struct sctp_vrf *vrf = NULL; 145 struct sctp_vrflist *bucket; 146 147 /* First allocate the VRF structure */ 148 vrf = sctp_find_vrf(vrf_id); 149 if (vrf) { 150 /* Already allocated */ 151 return (vrf); 152 } 153 SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf), 154 SCTP_M_VRF); 155 if (vrf == NULL) { 156 /* No memory */ 157 #ifdef INVARIANTS 158 panic("No memory for VRF:%d", vrf_id); 159 #endif 160 return (NULL); 161 } 162 /* setup the VRF */ 163 memset(vrf, 0, sizeof(struct sctp_vrf)); 164 vrf->vrf_id = vrf_id; 165 LIST_INIT(&vrf->ifnlist); 166 vrf->total_ifa_count = 0; 167 vrf->refcount = 0; 168 /* now also setup table ids */ 169 SCTP_INIT_VRF_TABLEID(vrf); 170 /* Init the HASH of addresses */ 171 vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE, 172 &vrf->vrf_addr_hashmark); 173 if (vrf->vrf_addr_hash == NULL) { 174 /* No memory */ 175 #ifdef INVARIANTS 176 panic("No memory for VRF:%d", vrf_id); 177 #endif 178 SCTP_FREE(vrf, SCTP_M_VRF); 179 return (NULL); 180 } 181 182 /* Add it to the hash table */ 183 bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))]; 184 LIST_INSERT_HEAD(bucket, vrf, next_vrf); 185 atomic_add_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1); 186 return (vrf); 187 } 188 189 struct sctp_ifn * 190 sctp_find_ifn(void *ifn, uint32_t ifn_index) 191 { 192 struct sctp_ifn *sctp_ifnp; 193 struct sctp_ifnlist *hash_ifn_head; 194 195 SCTP_IPI_ADDR_LOCK_ASSERT(); 196 hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))]; 197 LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) { 198 if (sctp_ifnp->ifn_index == ifn_index) { 199 break; 200 } 201 if (ifn != NULL && sctp_ifnp->ifn_p == ifn) { 202 break; 203 } 204 } 205 return (sctp_ifnp); 206 } 207 208 struct sctp_vrf * 209 sctp_find_vrf(uint32_t vrf_id) 210 { 211 struct sctp_vrflist *bucket; 212 struct sctp_vrf *liste; 213 214 bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))]; 215 LIST_FOREACH(liste, bucket, next_vrf) { 216 if (vrf_id == liste->vrf_id) { 217 return (liste); 218 } 219 } 220 return (NULL); 221 } 222 223 void 224 sctp_free_vrf(struct sctp_vrf *vrf) 225 { 226 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&vrf->refcount)) { 227 if (vrf->vrf_addr_hash) { 228 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark); 229 vrf->vrf_addr_hash = NULL; 230 } 231 /* We zero'd the count */ 232 LIST_REMOVE(vrf, next_vrf); 233 SCTP_FREE(vrf, SCTP_M_VRF); 234 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1); 235 } 236 } 237 238 static void 239 sctp_free_ifn(struct sctp_ifn *sctp_ifnp) 240 { 241 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifnp->refcount)) { 242 /* We zero'd the count */ 243 if (sctp_ifnp->vrf) { 244 sctp_free_vrf(sctp_ifnp->vrf); 245 } 246 SCTP_FREE(sctp_ifnp, SCTP_M_IFN); 247 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifns), 1); 248 } 249 } 250 251 void 252 sctp_free_ifa(struct sctp_ifa *sctp_ifap) 253 { 254 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifap->refcount)) { 255 /* We zero'd the count */ 256 if (sctp_ifap->ifn_p) { 257 sctp_free_ifn(sctp_ifap->ifn_p); 258 } 259 SCTP_FREE(sctp_ifap, SCTP_M_IFA); 260 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifas), 1); 261 } 262 } 263 264 static void 265 sctp_delete_ifn(struct sctp_ifn *sctp_ifnp) 266 { 267 268 SCTP_IPI_ADDR_WLOCK_ASSERT(); 269 if (sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index) == NULL) { 270 /* Not in the list.. sorry */ 271 return; 272 } 273 LIST_REMOVE(sctp_ifnp, next_bucket); 274 LIST_REMOVE(sctp_ifnp, next_ifn); 275 /* Take away the reference, and possibly free it */ 276 sctp_free_ifn(sctp_ifnp); 277 } 278 279 /*- 280 * Add an ifa to an ifn. 281 * Register the interface as necessary. 282 */ 283 static void 284 sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap) 285 { 286 int ifa_af; 287 288 SCTP_IPI_ADDR_WLOCK_ASSERT(); 289 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa); 290 sctp_ifap->ifn_p = sctp_ifnp; 291 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1); 292 /* update address counts */ 293 sctp_ifnp->ifa_count++; 294 ifa_af = sctp_ifap->address.sa.sa_family; 295 switch (ifa_af) { 296 #ifdef INET 297 case AF_INET: 298 sctp_ifnp->num_v4++; 299 break; 300 #endif 301 #ifdef INET6 302 case AF_INET6: 303 sctp_ifnp->num_v6++; 304 break; 305 #endif 306 default: 307 break; 308 } 309 if (sctp_ifnp->ifa_count == 1) { 310 /* register the new interface */ 311 sctp_ifnp->registered_af = ifa_af; 312 } 313 } 314 315 /*- 316 * Remove an ifa from its ifn. 317 * If no more addresses exist, remove the ifn too. Otherwise, re-register 318 * the interface based on the remaining address families left. 319 */ 320 static void 321 sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap) 322 { 323 SCTP_IPI_ADDR_WLOCK_ASSERT(); 324 LIST_REMOVE(sctp_ifap, next_ifa); 325 if (sctp_ifap->ifn_p) { 326 /* update address counts */ 327 sctp_ifap->ifn_p->ifa_count--; 328 switch (sctp_ifap->address.sa.sa_family) { 329 #ifdef INET 330 case AF_INET: 331 sctp_ifap->ifn_p->num_v4--; 332 break; 333 #endif 334 #ifdef INET6 335 case AF_INET6: 336 sctp_ifap->ifn_p->num_v6--; 337 break; 338 #endif 339 default: 340 break; 341 } 342 343 if (LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) { 344 /* remove the ifn, possibly freeing it */ 345 sctp_delete_ifn(sctp_ifap->ifn_p); 346 } else { 347 /* re-register address family type, if needed */ 348 if ((sctp_ifap->ifn_p->num_v6 == 0) && 349 (sctp_ifap->ifn_p->registered_af == AF_INET6)) { 350 sctp_ifap->ifn_p->registered_af = AF_INET; 351 } else if ((sctp_ifap->ifn_p->num_v4 == 0) && 352 (sctp_ifap->ifn_p->registered_af == AF_INET)) { 353 sctp_ifap->ifn_p->registered_af = AF_INET6; 354 } 355 /* free the ifn refcount */ 356 sctp_free_ifn(sctp_ifap->ifn_p); 357 } 358 sctp_ifap->ifn_p = NULL; 359 } 360 } 361 362 struct sctp_ifa * 363 sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index, 364 uint32_t ifn_type, const char *if_name, void *ifa, 365 struct sockaddr *addr, uint32_t ifa_flags, 366 int dynamic_add) 367 { 368 struct sctp_vrf *vrf; 369 struct sctp_ifn *sctp_ifnp, *new_sctp_ifnp; 370 struct sctp_ifa *sctp_ifap, *new_sctp_ifap; 371 struct sctp_ifalist *hash_addr_head; 372 struct sctp_ifnlist *hash_ifn_head; 373 uint32_t hash_of_addr; 374 375 #ifdef SCTP_DEBUG 376 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id); 377 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr); 378 #endif 379 SCTP_MALLOC(new_sctp_ifnp, struct sctp_ifn *, 380 sizeof(struct sctp_ifn), SCTP_M_IFN); 381 if (new_sctp_ifnp == NULL) { 382 #ifdef INVARIANTS 383 panic("No memory for IFN"); 384 #endif 385 return (NULL); 386 } 387 SCTP_MALLOC(new_sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA); 388 if (new_sctp_ifap == NULL) { 389 #ifdef INVARIANTS 390 panic("No memory for IFA"); 391 #endif 392 SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN); 393 return (NULL); 394 } 395 396 SCTP_IPI_ADDR_WLOCK(); 397 sctp_ifnp = sctp_find_ifn(ifn, ifn_index); 398 if (sctp_ifnp) { 399 vrf = sctp_ifnp->vrf; 400 } else { 401 vrf = sctp_find_vrf(vrf_id); 402 if (vrf == NULL) { 403 vrf = sctp_allocate_vrf(vrf_id); 404 if (vrf == NULL) { 405 SCTP_IPI_ADDR_WUNLOCK(); 406 SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN); 407 SCTP_FREE(new_sctp_ifap, SCTP_M_IFA); 408 return (NULL); 409 } 410 } 411 } 412 if (sctp_ifnp == NULL) { 413 /* 414 * build one and add it, can't hold lock until after malloc 415 * done though. 416 */ 417 sctp_ifnp = new_sctp_ifnp; 418 new_sctp_ifnp = NULL; 419 memset(sctp_ifnp, 0, sizeof(struct sctp_ifn)); 420 sctp_ifnp->ifn_index = ifn_index; 421 sctp_ifnp->ifn_p = ifn; 422 sctp_ifnp->ifn_type = ifn_type; 423 sctp_ifnp->refcount = 0; 424 sctp_ifnp->vrf = vrf; 425 atomic_add_int(&vrf->refcount, 1); 426 sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index); 427 if (if_name != NULL) { 428 SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", if_name); 429 } else { 430 SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", "unknown"); 431 } 432 hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))]; 433 LIST_INIT(&sctp_ifnp->ifalist); 434 LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket); 435 LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn); 436 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifns), 1); 437 } 438 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); 439 if (sctp_ifap != NULL) { 440 /* The address being added is already or still known. */ 441 if (sctp_ifap->ifn_p != NULL) { 442 if (sctp_ifap->ifn_p->ifn_index == ifn_index) { 443 SCTPDBG(SCTP_DEBUG_PCB4, 444 "Using existing ifn %s (0x%x) for ifa %p\n", 445 sctp_ifap->ifn_p->ifn_name, ifn_index, 446 (void *)sctp_ifap); 447 if (new_sctp_ifnp == NULL) { 448 /* Remove the created one not used. */ 449 sctp_delete_ifn(sctp_ifnp); 450 } 451 if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) { 452 /* Switch back to active. */ 453 SCTPDBG(SCTP_DEBUG_PCB4, 454 "Clearing deleted ifa flag\n"); 455 sctp_ifap->localifa_flags = SCTP_ADDR_VALID; 456 sctp_ifap->ifn_p = sctp_ifnp; 457 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1); 458 } 459 } else { 460 /* 461 * The last IFN gets the address, remove the 462 * old one. 463 */ 464 SCTPDBG(SCTP_DEBUG_PCB4, 465 "Moving ifa %p from %s (0x%x) to %s (0x%x)\n", 466 (void *)sctp_ifap, 467 sctp_ifap->ifn_p->ifn_name, 468 sctp_ifap->ifn_p->ifn_index, if_name, 469 ifn_index); 470 /* remove the address from the old ifn */ 471 sctp_remove_ifa_from_ifn(sctp_ifap); 472 /* move the address over to the new ifn */ 473 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap); 474 } 475 } else { 476 /* Repair ifn_p, which was NULL... */ 477 sctp_ifap->localifa_flags = SCTP_ADDR_VALID; 478 SCTPDBG(SCTP_DEBUG_PCB4, 479 "Repairing ifn %p for ifa %p\n", 480 (void *)sctp_ifnp, (void *)sctp_ifap); 481 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap); 482 } 483 SCTP_IPI_ADDR_WUNLOCK(); 484 if (new_sctp_ifnp != NULL) { 485 SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN); 486 } 487 SCTP_FREE(new_sctp_ifap, SCTP_M_IFA); 488 return (sctp_ifap); 489 } 490 KASSERT(sctp_ifnp != NULL, 491 ("sctp_add_addr_to_vrf: sctp_ifnp == NULL")); 492 KASSERT(sctp_ifap == NULL, 493 ("sctp_add_addr_to_vrf: sctp_ifap (%p) != NULL", sctp_ifap)); 494 sctp_ifap = new_sctp_ifap; 495 memset(sctp_ifap, 0, sizeof(struct sctp_ifa)); 496 sctp_ifap->ifn_p = sctp_ifnp; 497 atomic_add_int(&sctp_ifnp->refcount, 1); 498 sctp_ifap->vrf_id = vrf_id; 499 sctp_ifap->ifa = ifa; 500 memcpy(&sctp_ifap->address, addr, addr->sa_len); 501 sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE; 502 sctp_ifap->flags = ifa_flags; 503 /* Set scope */ 504 switch (sctp_ifap->address.sa.sa_family) { 505 #ifdef INET 506 case AF_INET: 507 { 508 struct sockaddr_in *sin; 509 510 sin = &sctp_ifap->address.sin; 511 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) || 512 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) { 513 sctp_ifap->src_is_loop = 1; 514 } 515 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) { 516 sctp_ifap->src_is_priv = 1; 517 } 518 sctp_ifnp->num_v4++; 519 if (new_sctp_ifnp == NULL) 520 sctp_ifnp->registered_af = AF_INET; 521 break; 522 } 523 #endif 524 #ifdef INET6 525 case AF_INET6: 526 { 527 /* ok to use deprecated addresses? */ 528 struct sockaddr_in6 *sin6; 529 530 sin6 = &sctp_ifap->address.sin6; 531 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) || 532 (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) { 533 sctp_ifap->src_is_loop = 1; 534 } 535 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 536 sctp_ifap->src_is_priv = 1; 537 } 538 sctp_ifnp->num_v6++; 539 if (new_sctp_ifnp == NULL) 540 sctp_ifnp->registered_af = AF_INET6; 541 break; 542 } 543 #endif 544 default: 545 break; 546 } 547 hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa); 548 549 if ((sctp_ifap->src_is_priv == 0) && 550 (sctp_ifap->src_is_loop == 0)) { 551 sctp_ifap->src_is_glob = 1; 552 } 553 hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)]; 554 LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket); 555 sctp_ifap->refcount = 1; 556 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa); 557 sctp_ifnp->ifa_count++; 558 vrf->total_ifa_count++; 559 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifas), 1); 560 SCTP_IPI_ADDR_WUNLOCK(); 561 if (new_sctp_ifnp != NULL) { 562 SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN); 563 } 564 565 if (dynamic_add) { 566 /* 567 * Bump up the refcount so that when the timer completes it 568 * will drop back down. 569 */ 570 struct sctp_laddr *wi; 571 572 atomic_add_int(&sctp_ifap->refcount, 1); 573 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); 574 if (wi == NULL) { 575 /* 576 * Gak, what can we do? We have lost an address 577 * change can you say HOSED? 578 */ 579 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n"); 580 /* Opps, must decrement the count */ 581 sctp_del_addr_from_vrf(vrf_id, addr, ifn_index); 582 return (NULL); 583 } 584 SCTP_INCR_LADDR_COUNT(); 585 memset(wi, 0, sizeof(*wi)); 586 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time); 587 wi->ifa = sctp_ifap; 588 wi->action = SCTP_ADD_IP_ADDRESS; 589 590 SCTP_WQ_ADDR_LOCK(); 591 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr); 592 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ, 593 (struct sctp_inpcb *)NULL, 594 (struct sctp_tcb *)NULL, 595 (struct sctp_nets *)NULL); 596 SCTP_WQ_ADDR_UNLOCK(); 597 } else { 598 /* it's ready for use */ 599 sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE; 600 } 601 return (sctp_ifap); 602 } 603 604 void 605 sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr, 606 uint32_t ifn_index) 607 { 608 struct sctp_vrf *vrf; 609 struct sctp_ifa *sctp_ifap; 610 611 SCTP_IPI_ADDR_WLOCK(); 612 vrf = sctp_find_vrf(vrf_id); 613 if (vrf == NULL) { 614 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id); 615 SCTP_IPI_ADDR_WUNLOCK(); 616 return; 617 } 618 619 #ifdef SCTP_DEBUG 620 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id); 621 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr); 622 #endif 623 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); 624 if (sctp_ifap != NULL) { 625 /* Validate the delete */ 626 if (sctp_ifap->ifn_p) { 627 if (ifn_index != sctp_ifap->ifn_p->ifn_index) { 628 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n", 629 sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name); 630 SCTP_IPI_ADDR_WUNLOCK(); 631 return; 632 } 633 } 634 SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", (void *)sctp_ifap); 635 sctp_ifap->localifa_flags &= SCTP_ADDR_VALID; 636 /* 637 * We don't set the flag. This means that the structure will 638 * hang around in EP's that have bound specific to it until 639 * they close. This gives us TCP like behavior if someone 640 * removes an address (or for that matter adds it right 641 * back). 642 */ 643 /* sctp_ifap->localifa_flags |= SCTP_BEING_DELETED; */ 644 vrf->total_ifa_count--; 645 LIST_REMOVE(sctp_ifap, next_bucket); 646 sctp_remove_ifa_from_ifn(sctp_ifap); 647 } 648 #ifdef SCTP_DEBUG 649 else { 650 SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:", 651 ifn_index); 652 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr); 653 } 654 #endif 655 656 SCTP_IPI_ADDR_WUNLOCK(); 657 if (sctp_ifap != NULL) { 658 struct sctp_laddr *wi; 659 660 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); 661 if (wi == NULL) { 662 /* 663 * Gak, what can we do? We have lost an address 664 * change can you say HOSED? 665 */ 666 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n"); 667 668 /* Oops, must decrement the count */ 669 sctp_free_ifa(sctp_ifap); 670 return; 671 } 672 SCTP_INCR_LADDR_COUNT(); 673 memset(wi, 0, sizeof(*wi)); 674 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time); 675 wi->ifa = sctp_ifap; 676 wi->action = SCTP_DEL_IP_ADDRESS; 677 SCTP_WQ_ADDR_LOCK(); 678 /* 679 * Should this really be a tailq? As it is we will process 680 * the newest first :-0 681 */ 682 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr); 683 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ, 684 (struct sctp_inpcb *)NULL, 685 (struct sctp_tcb *)NULL, 686 (struct sctp_nets *)NULL); 687 SCTP_WQ_ADDR_UNLOCK(); 688 } 689 return; 690 } 691 692 static int 693 sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to) 694 { 695 int loopback_scope; 696 #if defined(INET) 697 int ipv4_local_scope, ipv4_addr_legal; 698 #endif 699 #if defined(INET6) 700 int local_scope, site_scope, ipv6_addr_legal; 701 #endif 702 struct sctp_vrf *vrf; 703 struct sctp_ifn *sctp_ifn; 704 struct sctp_ifa *sctp_ifa; 705 706 loopback_scope = stcb->asoc.scope.loopback_scope; 707 #if defined(INET) 708 ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope; 709 ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal; 710 #endif 711 #if defined(INET6) 712 local_scope = stcb->asoc.scope.local_scope; 713 site_scope = stcb->asoc.scope.site_scope; 714 ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal; 715 #endif 716 717 SCTP_IPI_ADDR_RLOCK(); 718 vrf = sctp_find_vrf(stcb->asoc.vrf_id); 719 if (vrf == NULL) { 720 /* no vrf, no addresses */ 721 SCTP_IPI_ADDR_RUNLOCK(); 722 return (0); 723 } 724 725 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 726 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { 727 if ((loopback_scope == 0) && 728 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { 729 continue; 730 } 731 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { 732 if (sctp_is_addr_restricted(stcb, sctp_ifa) && 733 (!sctp_is_addr_pending(stcb, sctp_ifa))) { 734 /* 735 * We allow pending addresses, where 736 * we have sent an asconf-add to be 737 * considered valid. 738 */ 739 continue; 740 } 741 if (sctp_ifa->address.sa.sa_family != to->sa_family) { 742 continue; 743 } 744 switch (sctp_ifa->address.sa.sa_family) { 745 #ifdef INET 746 case AF_INET: 747 if (ipv4_addr_legal) { 748 struct sockaddr_in *sin, 749 *rsin; 750 751 sin = &sctp_ifa->address.sin; 752 rsin = (struct sockaddr_in *)to; 753 if ((ipv4_local_scope == 0) && 754 IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) { 755 continue; 756 } 757 if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred, 758 &sin->sin_addr) != 0) { 759 continue; 760 } 761 if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) { 762 SCTP_IPI_ADDR_RUNLOCK(); 763 return (1); 764 } 765 } 766 break; 767 #endif 768 #ifdef INET6 769 case AF_INET6: 770 if (ipv6_addr_legal) { 771 struct sockaddr_in6 *sin6, 772 *rsin6; 773 774 sin6 = &sctp_ifa->address.sin6; 775 rsin6 = (struct sockaddr_in6 *)to; 776 if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred, 777 &sin6->sin6_addr) != 0) { 778 continue; 779 } 780 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 781 if (local_scope == 0) 782 continue; 783 if (sin6->sin6_scope_id == 0) { 784 if (sa6_recoverscope(sin6) != 0) 785 continue; 786 } 787 } 788 if ((site_scope == 0) && 789 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) { 790 continue; 791 } 792 if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) { 793 SCTP_IPI_ADDR_RUNLOCK(); 794 return (1); 795 } 796 } 797 break; 798 #endif 799 default: 800 /* TSNH */ 801 break; 802 } 803 } 804 } 805 } else { 806 struct sctp_laddr *laddr; 807 808 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) { 809 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { 810 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n"); 811 continue; 812 } 813 if (sctp_is_addr_restricted(stcb, laddr->ifa) && 814 (!sctp_is_addr_pending(stcb, laddr->ifa))) { 815 /* 816 * We allow pending addresses, where we have 817 * sent an asconf-add to be considered 818 * valid. 819 */ 820 continue; 821 } 822 if (laddr->ifa->address.sa.sa_family != to->sa_family) { 823 continue; 824 } 825 switch (to->sa_family) { 826 #ifdef INET 827 case AF_INET: 828 { 829 struct sockaddr_in *sin, *rsin; 830 831 sin = &laddr->ifa->address.sin; 832 rsin = (struct sockaddr_in *)to; 833 if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) { 834 SCTP_IPI_ADDR_RUNLOCK(); 835 return (1); 836 } 837 break; 838 } 839 #endif 840 #ifdef INET6 841 case AF_INET6: 842 { 843 struct sockaddr_in6 *sin6, *rsin6; 844 845 sin6 = &laddr->ifa->address.sin6; 846 rsin6 = (struct sockaddr_in6 *)to; 847 if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) { 848 SCTP_IPI_ADDR_RUNLOCK(); 849 return (1); 850 } 851 break; 852 } 853 854 #endif 855 default: 856 /* TSNH */ 857 break; 858 } 859 } 860 } 861 SCTP_IPI_ADDR_RUNLOCK(); 862 return (0); 863 } 864 865 static struct sctp_tcb * 866 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from, 867 struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id) 868 { 869 /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */ 870 /* 871 * If we support the TCP model, then we must now dig through to see 872 * if we can find our endpoint in the list of tcp ep's. 873 */ 874 uint16_t lport, rport; 875 struct sctppcbhead *ephead; 876 struct sctp_inpcb *inp; 877 struct sctp_laddr *laddr; 878 struct sctp_tcb *stcb; 879 struct sctp_nets *net; 880 881 if ((to == NULL) || (from == NULL)) { 882 return (NULL); 883 } 884 885 switch (to->sa_family) { 886 #ifdef INET 887 case AF_INET: 888 if (from->sa_family == AF_INET) { 889 lport = ((struct sockaddr_in *)to)->sin_port; 890 rport = ((struct sockaddr_in *)from)->sin_port; 891 } else { 892 return (NULL); 893 } 894 break; 895 #endif 896 #ifdef INET6 897 case AF_INET6: 898 if (from->sa_family == AF_INET6) { 899 lport = ((struct sockaddr_in6 *)to)->sin6_port; 900 rport = ((struct sockaddr_in6 *)from)->sin6_port; 901 } else { 902 return (NULL); 903 } 904 break; 905 #endif 906 default: 907 return (NULL); 908 } 909 ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))]; 910 /* 911 * Ok now for each of the guys in this bucket we must look and see: 912 * - Does the remote port match. - Does there single association's 913 * addresses match this address (to). If so we update p_ep to point 914 * to this ep and return the tcb from it. 915 */ 916 LIST_FOREACH(inp, ephead, sctp_hash) { 917 SCTP_INP_RLOCK(inp); 918 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 919 SCTP_INP_RUNLOCK(inp); 920 continue; 921 } 922 if (lport != inp->sctp_lport) { 923 SCTP_INP_RUNLOCK(inp); 924 continue; 925 } 926 switch (to->sa_family) { 927 #ifdef INET 928 case AF_INET: 929 { 930 struct sockaddr_in *sin; 931 932 sin = (struct sockaddr_in *)to; 933 if (prison_check_ip4(inp->ip_inp.inp.inp_cred, 934 &sin->sin_addr) != 0) { 935 SCTP_INP_RUNLOCK(inp); 936 continue; 937 } 938 break; 939 } 940 #endif 941 #ifdef INET6 942 case AF_INET6: 943 { 944 struct sockaddr_in6 *sin6; 945 946 sin6 = (struct sockaddr_in6 *)to; 947 if (prison_check_ip6(inp->ip_inp.inp.inp_cred, 948 &sin6->sin6_addr) != 0) { 949 SCTP_INP_RUNLOCK(inp); 950 continue; 951 } 952 break; 953 } 954 #endif 955 default: 956 SCTP_INP_RUNLOCK(inp); 957 continue; 958 } 959 if (inp->def_vrf_id != vrf_id) { 960 SCTP_INP_RUNLOCK(inp); 961 continue; 962 } 963 /* check to see if the ep has one of the addresses */ 964 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { 965 /* We are NOT bound all, so look further */ 966 int match = 0; 967 968 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 969 if (laddr->ifa == NULL) { 970 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __func__); 971 continue; 972 } 973 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { 974 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n"); 975 continue; 976 } 977 if (laddr->ifa->address.sa.sa_family == 978 to->sa_family) { 979 /* see if it matches */ 980 #ifdef INET 981 if (from->sa_family == AF_INET) { 982 struct sockaddr_in *intf_addr, 983 *sin; 984 985 intf_addr = &laddr->ifa->address.sin; 986 sin = (struct sockaddr_in *)to; 987 if (sin->sin_addr.s_addr == 988 intf_addr->sin_addr.s_addr) { 989 match = 1; 990 break; 991 } 992 } 993 #endif 994 #ifdef INET6 995 if (from->sa_family == AF_INET6) { 996 struct sockaddr_in6 *intf_addr6; 997 struct sockaddr_in6 *sin6; 998 999 sin6 = (struct sockaddr_in6 *) 1000 to; 1001 intf_addr6 = &laddr->ifa->address.sin6; 1002 1003 if (SCTP6_ARE_ADDR_EQUAL(sin6, 1004 intf_addr6)) { 1005 match = 1; 1006 break; 1007 } 1008 } 1009 #endif 1010 } 1011 } 1012 if (match == 0) { 1013 /* This endpoint does not have this address */ 1014 SCTP_INP_RUNLOCK(inp); 1015 continue; 1016 } 1017 } 1018 /* 1019 * Ok if we hit here the ep has the address, does it hold 1020 * the tcb? 1021 */ 1022 /* XXX: Why don't we TAILQ_FOREACH through sctp_asoc_list? */ 1023 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1024 if (stcb == NULL) { 1025 SCTP_INP_RUNLOCK(inp); 1026 continue; 1027 } 1028 SCTP_TCB_LOCK(stcb); 1029 if (!sctp_does_stcb_own_this_addr(stcb, to)) { 1030 SCTP_TCB_UNLOCK(stcb); 1031 SCTP_INP_RUNLOCK(inp); 1032 continue; 1033 } 1034 if (stcb->rport != rport) { 1035 /* remote port does not match. */ 1036 SCTP_TCB_UNLOCK(stcb); 1037 SCTP_INP_RUNLOCK(inp); 1038 continue; 1039 } 1040 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 1041 SCTP_TCB_UNLOCK(stcb); 1042 SCTP_INP_RUNLOCK(inp); 1043 continue; 1044 } 1045 if (!sctp_does_stcb_own_this_addr(stcb, to)) { 1046 SCTP_TCB_UNLOCK(stcb); 1047 SCTP_INP_RUNLOCK(inp); 1048 continue; 1049 } 1050 /* Does this TCB have a matching address? */ 1051 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1052 if (net->ro._l_addr.sa.sa_family != from->sa_family) { 1053 /* not the same family, can't be a match */ 1054 continue; 1055 } 1056 switch (from->sa_family) { 1057 #ifdef INET 1058 case AF_INET: 1059 { 1060 struct sockaddr_in *sin, *rsin; 1061 1062 sin = (struct sockaddr_in *)&net->ro._l_addr; 1063 rsin = (struct sockaddr_in *)from; 1064 if (sin->sin_addr.s_addr == 1065 rsin->sin_addr.s_addr) { 1066 /* found it */ 1067 if (netp != NULL) { 1068 *netp = net; 1069 } 1070 /* 1071 * Update the endpoint 1072 * pointer 1073 */ 1074 *inp_p = inp; 1075 SCTP_INP_RUNLOCK(inp); 1076 return (stcb); 1077 } 1078 break; 1079 } 1080 #endif 1081 #ifdef INET6 1082 case AF_INET6: 1083 { 1084 struct sockaddr_in6 *sin6, *rsin6; 1085 1086 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1087 rsin6 = (struct sockaddr_in6 *)from; 1088 if (SCTP6_ARE_ADDR_EQUAL(sin6, 1089 rsin6)) { 1090 /* found it */ 1091 if (netp != NULL) { 1092 *netp = net; 1093 } 1094 /* 1095 * Update the endpoint 1096 * pointer 1097 */ 1098 *inp_p = inp; 1099 SCTP_INP_RUNLOCK(inp); 1100 return (stcb); 1101 } 1102 break; 1103 } 1104 #endif 1105 default: 1106 /* TSNH */ 1107 break; 1108 } 1109 } 1110 SCTP_TCB_UNLOCK(stcb); 1111 SCTP_INP_RUNLOCK(inp); 1112 } 1113 return (NULL); 1114 } 1115 1116 /* 1117 * rules for use 1118 * 1119 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an 1120 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done 1121 * (if locked == NULL). 3) Decrement happens on return ONLY if locked == 1122 * NULL. 1123 */ 1124 1125 struct sctp_tcb * 1126 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote, 1127 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb) 1128 { 1129 struct sctpasochead *head; 1130 struct sctp_inpcb *inp; 1131 struct sctp_tcb *stcb = NULL; 1132 struct sctp_nets *net; 1133 uint16_t rport; 1134 1135 inp = *inp_p; 1136 switch (remote->sa_family) { 1137 #ifdef INET 1138 case AF_INET: 1139 rport = (((struct sockaddr_in *)remote)->sin_port); 1140 break; 1141 #endif 1142 #ifdef INET6 1143 case AF_INET6: 1144 rport = (((struct sockaddr_in6 *)remote)->sin6_port); 1145 break; 1146 #endif 1147 default: 1148 return (NULL); 1149 } 1150 if (locked_tcb) { 1151 /* 1152 * UN-lock so we can do proper locking here this occurs when 1153 * called from load_addresses_from_init. 1154 */ 1155 atomic_add_int(&locked_tcb->asoc.refcnt, 1); 1156 SCTP_TCB_UNLOCK(locked_tcb); 1157 } 1158 SCTP_INP_INFO_RLOCK(); 1159 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1160 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 1161 /*- 1162 * Now either this guy is our listener or it's the 1163 * connector. If it is the one that issued the connect, then 1164 * it's only chance is to be the first TCB in the list. If 1165 * it is the acceptor, then do the special_lookup to hash 1166 * and find the real inp. 1167 */ 1168 if ((inp->sctp_socket) && SCTP_IS_LISTENING(inp)) { 1169 /* to is peer addr, from is my addr */ 1170 stcb = sctp_tcb_special_locate(inp_p, remote, local, 1171 netp, inp->def_vrf_id); 1172 if ((stcb != NULL) && (locked_tcb == NULL)) { 1173 /* we have a locked tcb, lower refcount */ 1174 SCTP_INP_DECR_REF(inp); 1175 } 1176 if ((locked_tcb != NULL) && (locked_tcb != stcb)) { 1177 SCTP_INP_RLOCK(locked_tcb->sctp_ep); 1178 SCTP_TCB_LOCK(locked_tcb); 1179 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); 1180 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep); 1181 } 1182 SCTP_INP_INFO_RUNLOCK(); 1183 return (stcb); 1184 } else { 1185 SCTP_INP_WLOCK(inp); 1186 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 1187 goto null_return; 1188 } 1189 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1190 if (stcb == NULL) { 1191 goto null_return; 1192 } 1193 SCTP_TCB_LOCK(stcb); 1194 1195 if (stcb->rport != rport) { 1196 /* remote port does not match. */ 1197 SCTP_TCB_UNLOCK(stcb); 1198 goto null_return; 1199 } 1200 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 1201 SCTP_TCB_UNLOCK(stcb); 1202 goto null_return; 1203 } 1204 if (local && !sctp_does_stcb_own_this_addr(stcb, local)) { 1205 SCTP_TCB_UNLOCK(stcb); 1206 goto null_return; 1207 } 1208 /* now look at the list of remote addresses */ 1209 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1210 #ifdef INVARIANTS 1211 if (net == (TAILQ_NEXT(net, sctp_next))) { 1212 panic("Corrupt net list"); 1213 } 1214 #endif 1215 if (net->ro._l_addr.sa.sa_family != 1216 remote->sa_family) { 1217 /* not the same family */ 1218 continue; 1219 } 1220 switch (remote->sa_family) { 1221 #ifdef INET 1222 case AF_INET: 1223 { 1224 struct sockaddr_in *sin, 1225 *rsin; 1226 1227 sin = (struct sockaddr_in *) 1228 &net->ro._l_addr; 1229 rsin = (struct sockaddr_in *)remote; 1230 if (sin->sin_addr.s_addr == 1231 rsin->sin_addr.s_addr) { 1232 /* found it */ 1233 if (netp != NULL) { 1234 *netp = net; 1235 } 1236 if (locked_tcb == NULL) { 1237 SCTP_INP_DECR_REF(inp); 1238 } else if (locked_tcb != stcb) { 1239 SCTP_TCB_LOCK(locked_tcb); 1240 } 1241 if (locked_tcb) { 1242 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); 1243 } 1244 1245 SCTP_INP_WUNLOCK(inp); 1246 SCTP_INP_INFO_RUNLOCK(); 1247 return (stcb); 1248 } 1249 break; 1250 } 1251 #endif 1252 #ifdef INET6 1253 case AF_INET6: 1254 { 1255 struct sockaddr_in6 *sin6, 1256 *rsin6; 1257 1258 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1259 rsin6 = (struct sockaddr_in6 *)remote; 1260 if (SCTP6_ARE_ADDR_EQUAL(sin6, 1261 rsin6)) { 1262 /* found it */ 1263 if (netp != NULL) { 1264 *netp = net; 1265 } 1266 if (locked_tcb == NULL) { 1267 SCTP_INP_DECR_REF(inp); 1268 } else if (locked_tcb != stcb) { 1269 SCTP_TCB_LOCK(locked_tcb); 1270 } 1271 if (locked_tcb) { 1272 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); 1273 } 1274 SCTP_INP_WUNLOCK(inp); 1275 SCTP_INP_INFO_RUNLOCK(); 1276 return (stcb); 1277 } 1278 break; 1279 } 1280 #endif 1281 default: 1282 /* TSNH */ 1283 break; 1284 } 1285 } 1286 SCTP_TCB_UNLOCK(stcb); 1287 } 1288 } else { 1289 SCTP_INP_WLOCK(inp); 1290 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 1291 goto null_return; 1292 } 1293 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport, 1294 inp->sctp_hashmark)]; 1295 LIST_FOREACH(stcb, head, sctp_tcbhash) { 1296 if (stcb->rport != rport) { 1297 /* remote port does not match */ 1298 continue; 1299 } 1300 SCTP_TCB_LOCK(stcb); 1301 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 1302 SCTP_TCB_UNLOCK(stcb); 1303 continue; 1304 } 1305 if (local && !sctp_does_stcb_own_this_addr(stcb, local)) { 1306 SCTP_TCB_UNLOCK(stcb); 1307 continue; 1308 } 1309 /* now look at the list of remote addresses */ 1310 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1311 #ifdef INVARIANTS 1312 if (net == (TAILQ_NEXT(net, sctp_next))) { 1313 panic("Corrupt net list"); 1314 } 1315 #endif 1316 if (net->ro._l_addr.sa.sa_family != 1317 remote->sa_family) { 1318 /* not the same family */ 1319 continue; 1320 } 1321 switch (remote->sa_family) { 1322 #ifdef INET 1323 case AF_INET: 1324 { 1325 struct sockaddr_in *sin, 1326 *rsin; 1327 1328 sin = (struct sockaddr_in *) 1329 &net->ro._l_addr; 1330 rsin = (struct sockaddr_in *)remote; 1331 if (sin->sin_addr.s_addr == 1332 rsin->sin_addr.s_addr) { 1333 /* found it */ 1334 if (netp != NULL) { 1335 *netp = net; 1336 } 1337 if (locked_tcb == NULL) { 1338 SCTP_INP_DECR_REF(inp); 1339 } else if (locked_tcb != stcb) { 1340 SCTP_TCB_LOCK(locked_tcb); 1341 } 1342 if (locked_tcb) { 1343 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); 1344 } 1345 SCTP_INP_WUNLOCK(inp); 1346 SCTP_INP_INFO_RUNLOCK(); 1347 return (stcb); 1348 } 1349 break; 1350 } 1351 #endif 1352 #ifdef INET6 1353 case AF_INET6: 1354 { 1355 struct sockaddr_in6 *sin6, 1356 *rsin6; 1357 1358 sin6 = (struct sockaddr_in6 *) 1359 &net->ro._l_addr; 1360 rsin6 = (struct sockaddr_in6 *)remote; 1361 if (SCTP6_ARE_ADDR_EQUAL(sin6, 1362 rsin6)) { 1363 /* found it */ 1364 if (netp != NULL) { 1365 *netp = net; 1366 } 1367 if (locked_tcb == NULL) { 1368 SCTP_INP_DECR_REF(inp); 1369 } else if (locked_tcb != stcb) { 1370 SCTP_TCB_LOCK(locked_tcb); 1371 } 1372 if (locked_tcb) { 1373 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); 1374 } 1375 SCTP_INP_WUNLOCK(inp); 1376 SCTP_INP_INFO_RUNLOCK(); 1377 return (stcb); 1378 } 1379 break; 1380 } 1381 #endif 1382 default: 1383 /* TSNH */ 1384 break; 1385 } 1386 } 1387 SCTP_TCB_UNLOCK(stcb); 1388 } 1389 } 1390 null_return: 1391 /* clean up for returning null */ 1392 if (locked_tcb) { 1393 SCTP_TCB_LOCK(locked_tcb); 1394 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); 1395 } 1396 SCTP_INP_WUNLOCK(inp); 1397 SCTP_INP_INFO_RUNLOCK(); 1398 /* not found */ 1399 return (NULL); 1400 } 1401 1402 /* 1403 * Find an association for a specific endpoint using the association id given 1404 * out in the COMM_UP notification 1405 */ 1406 struct sctp_tcb * 1407 sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock) 1408 { 1409 /* 1410 * Use my the assoc_id to find a endpoint 1411 */ 1412 struct sctpasochead *head; 1413 struct sctp_tcb *stcb; 1414 uint32_t id; 1415 1416 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 1417 SCTP_PRINTF("TSNH ep_associd0\n"); 1418 return (NULL); 1419 } 1420 id = (uint32_t)asoc_id; 1421 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)]; 1422 if (head == NULL) { 1423 /* invalid id TSNH */ 1424 SCTP_PRINTF("TSNH ep_associd1\n"); 1425 return (NULL); 1426 } 1427 LIST_FOREACH(stcb, head, sctp_tcbasocidhash) { 1428 if (stcb->asoc.assoc_id == id) { 1429 if (inp != stcb->sctp_ep) { 1430 /* 1431 * some other guy has the same id active (id 1432 * collision ??). 1433 */ 1434 SCTP_PRINTF("TSNH ep_associd2\n"); 1435 continue; 1436 } 1437 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 1438 continue; 1439 } 1440 if (want_lock) { 1441 SCTP_TCB_LOCK(stcb); 1442 } 1443 return (stcb); 1444 } 1445 } 1446 return (NULL); 1447 } 1448 1449 struct sctp_tcb * 1450 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock) 1451 { 1452 struct sctp_tcb *stcb; 1453 1454 SCTP_INP_RLOCK(inp); 1455 stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock); 1456 SCTP_INP_RUNLOCK(inp); 1457 return (stcb); 1458 } 1459 1460 /* 1461 * Endpoint probe expects that the INP_INFO is locked. 1462 */ 1463 static struct sctp_inpcb * 1464 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head, 1465 uint16_t lport, uint32_t vrf_id) 1466 { 1467 struct sctp_inpcb *inp; 1468 struct sctp_laddr *laddr; 1469 #ifdef INET 1470 struct sockaddr_in *sin; 1471 #endif 1472 #ifdef INET6 1473 struct sockaddr_in6 *sin6; 1474 struct sockaddr_in6 *intf_addr6; 1475 #endif 1476 int fnd; 1477 1478 #ifdef INET 1479 sin = NULL; 1480 #endif 1481 #ifdef INET6 1482 sin6 = NULL; 1483 #endif 1484 switch (nam->sa_family) { 1485 #ifdef INET 1486 case AF_INET: 1487 sin = (struct sockaddr_in *)nam; 1488 break; 1489 #endif 1490 #ifdef INET6 1491 case AF_INET6: 1492 sin6 = (struct sockaddr_in6 *)nam; 1493 break; 1494 #endif 1495 default: 1496 /* unsupported family */ 1497 return (NULL); 1498 } 1499 1500 if (head == NULL) 1501 return (NULL); 1502 1503 LIST_FOREACH(inp, head, sctp_hash) { 1504 SCTP_INP_RLOCK(inp); 1505 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 1506 SCTP_INP_RUNLOCK(inp); 1507 continue; 1508 } 1509 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) && 1510 (inp->sctp_lport == lport)) { 1511 /* got it */ 1512 switch (nam->sa_family) { 1513 #ifdef INET 1514 case AF_INET: 1515 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && 1516 SCTP_IPV6_V6ONLY(inp)) { 1517 /* 1518 * IPv4 on a IPv6 socket with ONLY 1519 * IPv6 set 1520 */ 1521 SCTP_INP_RUNLOCK(inp); 1522 continue; 1523 } 1524 if (prison_check_ip4(inp->ip_inp.inp.inp_cred, 1525 &sin->sin_addr) != 0) { 1526 SCTP_INP_RUNLOCK(inp); 1527 continue; 1528 } 1529 break; 1530 #endif 1531 #ifdef INET6 1532 case AF_INET6: 1533 /* 1534 * A V6 address and the endpoint is NOT 1535 * bound V6 1536 */ 1537 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) { 1538 SCTP_INP_RUNLOCK(inp); 1539 continue; 1540 } 1541 if (prison_check_ip6(inp->ip_inp.inp.inp_cred, 1542 &sin6->sin6_addr) != 0) { 1543 SCTP_INP_RUNLOCK(inp); 1544 continue; 1545 } 1546 break; 1547 #endif 1548 default: 1549 break; 1550 } 1551 /* does a VRF id match? */ 1552 fnd = 0; 1553 if (inp->def_vrf_id == vrf_id) 1554 fnd = 1; 1555 1556 SCTP_INP_RUNLOCK(inp); 1557 if (!fnd) 1558 continue; 1559 return (inp); 1560 } 1561 SCTP_INP_RUNLOCK(inp); 1562 } 1563 switch (nam->sa_family) { 1564 #ifdef INET 1565 case AF_INET: 1566 if (sin->sin_addr.s_addr == INADDR_ANY) { 1567 /* Can't hunt for one that has no address specified */ 1568 return (NULL); 1569 } 1570 break; 1571 #endif 1572 #ifdef INET6 1573 case AF_INET6: 1574 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1575 /* Can't hunt for one that has no address specified */ 1576 return (NULL); 1577 } 1578 break; 1579 #endif 1580 default: 1581 break; 1582 } 1583 /* 1584 * ok, not bound to all so see if we can find a EP bound to this 1585 * address. 1586 */ 1587 LIST_FOREACH(inp, head, sctp_hash) { 1588 SCTP_INP_RLOCK(inp); 1589 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 1590 SCTP_INP_RUNLOCK(inp); 1591 continue; 1592 } 1593 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) { 1594 SCTP_INP_RUNLOCK(inp); 1595 continue; 1596 } 1597 /* 1598 * Ok this could be a likely candidate, look at all of its 1599 * addresses 1600 */ 1601 if (inp->sctp_lport != lport) { 1602 SCTP_INP_RUNLOCK(inp); 1603 continue; 1604 } 1605 /* does a VRF id match? */ 1606 fnd = 0; 1607 if (inp->def_vrf_id == vrf_id) 1608 fnd = 1; 1609 1610 if (!fnd) { 1611 SCTP_INP_RUNLOCK(inp); 1612 continue; 1613 } 1614 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 1615 if (laddr->ifa == NULL) { 1616 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", 1617 __func__); 1618 continue; 1619 } 1620 SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ", 1621 (void *)laddr->ifa); 1622 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { 1623 SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n"); 1624 continue; 1625 } 1626 if (laddr->ifa->address.sa.sa_family == nam->sa_family) { 1627 /* possible, see if it matches */ 1628 switch (nam->sa_family) { 1629 #ifdef INET 1630 case AF_INET: 1631 if (sin->sin_addr.s_addr == 1632 laddr->ifa->address.sin.sin_addr.s_addr) { 1633 SCTP_INP_RUNLOCK(inp); 1634 return (inp); 1635 } 1636 break; 1637 #endif 1638 #ifdef INET6 1639 case AF_INET6: 1640 intf_addr6 = &laddr->ifa->address.sin6; 1641 if (SCTP6_ARE_ADDR_EQUAL(sin6, 1642 intf_addr6)) { 1643 SCTP_INP_RUNLOCK(inp); 1644 return (inp); 1645 } 1646 break; 1647 #endif 1648 } 1649 } 1650 } 1651 SCTP_INP_RUNLOCK(inp); 1652 } 1653 return (NULL); 1654 } 1655 1656 static struct sctp_inpcb * 1657 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id) 1658 { 1659 struct sctppcbhead *head; 1660 struct sctp_inpcb *t_inp; 1661 int fnd; 1662 1663 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, 1664 SCTP_BASE_INFO(hashmark))]; 1665 LIST_FOREACH(t_inp, head, sctp_hash) { 1666 if (t_inp->sctp_lport != lport) { 1667 continue; 1668 } 1669 /* is it in the VRF in question */ 1670 fnd = 0; 1671 if (t_inp->def_vrf_id == vrf_id) 1672 fnd = 1; 1673 if (!fnd) 1674 continue; 1675 1676 /* This one is in use. */ 1677 /* check the v6/v4 binding issue */ 1678 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && 1679 SCTP_IPV6_V6ONLY(t_inp)) { 1680 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 1681 /* collision in V6 space */ 1682 return (t_inp); 1683 } else { 1684 /* inp is BOUND_V4 no conflict */ 1685 continue; 1686 } 1687 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 1688 /* t_inp is bound v4 and v6, conflict always */ 1689 return (t_inp); 1690 } else { 1691 /* t_inp is bound only V4 */ 1692 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && 1693 SCTP_IPV6_V6ONLY(inp)) { 1694 /* no conflict */ 1695 continue; 1696 } 1697 /* else fall through to conflict */ 1698 } 1699 return (t_inp); 1700 } 1701 return (NULL); 1702 } 1703 1704 int 1705 sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp) 1706 { 1707 /* For 1-2-1 with port reuse */ 1708 struct sctppcbhead *head; 1709 struct sctp_inpcb *tinp, *ninp; 1710 1711 SCTP_INP_INFO_WLOCK_ASSERT(); 1712 SCTP_INP_WLOCK_ASSERT(inp); 1713 1714 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) { 1715 /* only works with port reuse on */ 1716 return (-1); 1717 } 1718 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) { 1719 return (0); 1720 } 1721 SCTP_INP_WUNLOCK(inp); 1722 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, 1723 SCTP_BASE_INFO(hashmark))]; 1724 /* Kick out all non-listeners to the TCP hash */ 1725 LIST_FOREACH_SAFE(tinp, head, sctp_hash, ninp) { 1726 if (tinp->sctp_lport != inp->sctp_lport) { 1727 continue; 1728 } 1729 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 1730 continue; 1731 } 1732 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 1733 continue; 1734 } 1735 if (SCTP_IS_LISTENING(tinp)) { 1736 continue; 1737 } 1738 SCTP_INP_WLOCK(tinp); 1739 LIST_REMOVE(tinp, sctp_hash); 1740 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))]; 1741 tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL; 1742 LIST_INSERT_HEAD(head, tinp, sctp_hash); 1743 SCTP_INP_WUNLOCK(tinp); 1744 } 1745 SCTP_INP_WLOCK(inp); 1746 /* Pull from where he was */ 1747 LIST_REMOVE(inp, sctp_hash); 1748 inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL; 1749 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))]; 1750 LIST_INSERT_HEAD(head, inp, sctp_hash); 1751 return (0); 1752 } 1753 1754 struct sctp_inpcb * 1755 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock, 1756 uint32_t vrf_id) 1757 { 1758 /* 1759 * First we check the hash table to see if someone has this port 1760 * bound with just the port. 1761 */ 1762 struct sctp_inpcb *inp; 1763 struct sctppcbhead *head; 1764 int lport; 1765 unsigned int i; 1766 #ifdef INET 1767 struct sockaddr_in *sin; 1768 #endif 1769 #ifdef INET6 1770 struct sockaddr_in6 *sin6; 1771 #endif 1772 1773 switch (nam->sa_family) { 1774 #ifdef INET 1775 case AF_INET: 1776 sin = (struct sockaddr_in *)nam; 1777 lport = sin->sin_port; 1778 break; 1779 #endif 1780 #ifdef INET6 1781 case AF_INET6: 1782 sin6 = (struct sockaddr_in6 *)nam; 1783 lport = sin6->sin6_port; 1784 break; 1785 #endif 1786 default: 1787 return (NULL); 1788 } 1789 /* 1790 * I could cheat here and just cast to one of the types but we will 1791 * do it right. It also provides the check against an Unsupported 1792 * type too. 1793 */ 1794 /* Find the head of the ALLADDR chain */ 1795 if (have_lock == 0) { 1796 SCTP_INP_INFO_RLOCK(); 1797 } 1798 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, 1799 SCTP_BASE_INFO(hashmark))]; 1800 inp = sctp_endpoint_probe(nam, head, lport, vrf_id); 1801 1802 /* 1803 * If the TCP model exists it could be that the main listening 1804 * endpoint is gone but there still exists a connected socket for 1805 * this guy. If so we can return the first one that we find. This 1806 * may NOT be the correct one so the caller should be wary on the 1807 * returned INP. Currently the only caller that sets find_tcp_pool 1808 * is in bindx where we are verifying that a user CAN bind the 1809 * address. He either has bound it already, or someone else has, or 1810 * its open to bind, so this is good enough. 1811 */ 1812 if (inp == NULL && find_tcp_pool) { 1813 for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) { 1814 head = &SCTP_BASE_INFO(sctp_tcpephash)[i]; 1815 inp = sctp_endpoint_probe(nam, head, lport, vrf_id); 1816 if (inp) { 1817 break; 1818 } 1819 } 1820 } 1821 if (inp) { 1822 SCTP_INP_INCR_REF(inp); 1823 } 1824 if (have_lock == 0) { 1825 SCTP_INP_INFO_RUNLOCK(); 1826 } 1827 return (inp); 1828 } 1829 1830 /* 1831 * Find an association for an endpoint with the pointer to whom you want to 1832 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may 1833 * need to change the *to to some other struct like a mbuf... 1834 */ 1835 struct sctp_tcb * 1836 sctp_findassociation_addr_sa(struct sockaddr *from, struct sockaddr *to, 1837 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool, 1838 uint32_t vrf_id) 1839 { 1840 struct sctp_inpcb *inp = NULL; 1841 struct sctp_tcb *stcb; 1842 1843 SCTP_INP_INFO_RLOCK(); 1844 if (find_tcp_pool) { 1845 if (inp_p != NULL) { 1846 stcb = sctp_tcb_special_locate(inp_p, from, to, netp, 1847 vrf_id); 1848 } else { 1849 stcb = sctp_tcb_special_locate(&inp, from, to, netp, 1850 vrf_id); 1851 } 1852 if (stcb != NULL) { 1853 SCTP_INP_INFO_RUNLOCK(); 1854 return (stcb); 1855 } 1856 } 1857 inp = sctp_pcb_findep(to, 0, 1, vrf_id); 1858 if (inp_p != NULL) { 1859 *inp_p = inp; 1860 } 1861 SCTP_INP_INFO_RUNLOCK(); 1862 if (inp == NULL) { 1863 return (NULL); 1864 } 1865 /* 1866 * ok, we have an endpoint, now lets find the assoc for it (if any) 1867 * we now place the source address or from in the to of the find 1868 * endpoint call. Since in reality this chain is used from the 1869 * inbound packet side. 1870 */ 1871 if (inp_p != NULL) { 1872 stcb = sctp_findassociation_ep_addr(inp_p, from, netp, to, 1873 NULL); 1874 } else { 1875 stcb = sctp_findassociation_ep_addr(&inp, from, netp, to, 1876 NULL); 1877 } 1878 return (stcb); 1879 } 1880 1881 /* 1882 * This routine will grub through the mbuf that is a INIT or INIT-ACK and 1883 * find all addresses that the sender has specified in any address list. Each 1884 * address will be used to lookup the TCB and see if one exits. 1885 */ 1886 static struct sctp_tcb * 1887 sctp_findassociation_special_addr(struct mbuf *m, int offset, 1888 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp, 1889 struct sockaddr *dst) 1890 { 1891 struct sctp_paramhdr *phdr, param_buf; 1892 #if defined(INET) || defined(INET6) 1893 struct sctp_tcb *stcb; 1894 uint16_t ptype; 1895 #endif 1896 uint16_t plen; 1897 #ifdef INET 1898 struct sockaddr_in sin4; 1899 #endif 1900 #ifdef INET6 1901 struct sockaddr_in6 sin6; 1902 #endif 1903 1904 #ifdef INET 1905 memset(&sin4, 0, sizeof(sin4)); 1906 sin4.sin_len = sizeof(sin4); 1907 sin4.sin_family = AF_INET; 1908 sin4.sin_port = sh->src_port; 1909 #endif 1910 #ifdef INET6 1911 memset(&sin6, 0, sizeof(sin6)); 1912 sin6.sin6_len = sizeof(sin6); 1913 sin6.sin6_family = AF_INET6; 1914 sin6.sin6_port = sh->src_port; 1915 #endif 1916 1917 offset += sizeof(struct sctp_init_chunk); 1918 1919 phdr = sctp_get_next_param(m, offset, ¶m_buf, sizeof(param_buf)); 1920 while (phdr != NULL) { 1921 /* now we must see if we want the parameter */ 1922 #if defined(INET) || defined(INET6) 1923 ptype = ntohs(phdr->param_type); 1924 #endif 1925 plen = ntohs(phdr->param_length); 1926 if (plen == 0) { 1927 break; 1928 } 1929 #ifdef INET 1930 if (ptype == SCTP_IPV4_ADDRESS && 1931 plen == sizeof(struct sctp_ipv4addr_param)) { 1932 /* Get the rest of the address */ 1933 struct sctp_ipv4addr_param ip4_param, *p4; 1934 1935 phdr = sctp_get_next_param(m, offset, 1936 (struct sctp_paramhdr *)&ip4_param, sizeof(ip4_param)); 1937 if (phdr == NULL) { 1938 return (NULL); 1939 } 1940 p4 = (struct sctp_ipv4addr_param *)phdr; 1941 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr)); 1942 /* look it up */ 1943 stcb = sctp_findassociation_ep_addr(inp_p, 1944 (struct sockaddr *)&sin4, netp, dst, NULL); 1945 if (stcb != NULL) { 1946 return (stcb); 1947 } 1948 } 1949 #endif 1950 #ifdef INET6 1951 if (ptype == SCTP_IPV6_ADDRESS && 1952 plen == sizeof(struct sctp_ipv6addr_param)) { 1953 /* Get the rest of the address */ 1954 struct sctp_ipv6addr_param ip6_param, *p6; 1955 1956 phdr = sctp_get_next_param(m, offset, 1957 (struct sctp_paramhdr *)&ip6_param, sizeof(ip6_param)); 1958 if (phdr == NULL) { 1959 return (NULL); 1960 } 1961 p6 = (struct sctp_ipv6addr_param *)phdr; 1962 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr)); 1963 /* look it up */ 1964 stcb = sctp_findassociation_ep_addr(inp_p, 1965 (struct sockaddr *)&sin6, netp, dst, NULL); 1966 if (stcb != NULL) { 1967 return (stcb); 1968 } 1969 } 1970 #endif 1971 offset += SCTP_SIZE32(plen); 1972 phdr = sctp_get_next_param(m, offset, ¶m_buf, 1973 sizeof(param_buf)); 1974 } 1975 return (NULL); 1976 } 1977 1978 static struct sctp_tcb * 1979 sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag, 1980 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport, 1981 uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag) 1982 { 1983 /* 1984 * Use my vtag to hash. If we find it we then verify the source addr 1985 * is in the assoc. If all goes well we save a bit on rec of a 1986 * packet. 1987 */ 1988 struct sctpasochead *head; 1989 struct sctp_nets *net; 1990 struct sctp_tcb *stcb; 1991 1992 SCTP_INP_INFO_RLOCK(); 1993 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag, 1994 SCTP_BASE_INFO(hashasocmark))]; 1995 LIST_FOREACH(stcb, head, sctp_asocs) { 1996 SCTP_INP_RLOCK(stcb->sctp_ep); 1997 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 1998 SCTP_INP_RUNLOCK(stcb->sctp_ep); 1999 continue; 2000 } 2001 if (stcb->sctp_ep->def_vrf_id != vrf_id) { 2002 SCTP_INP_RUNLOCK(stcb->sctp_ep); 2003 continue; 2004 } 2005 SCTP_TCB_LOCK(stcb); 2006 SCTP_INP_RUNLOCK(stcb->sctp_ep); 2007 if (stcb->asoc.my_vtag == vtag) { 2008 /* candidate */ 2009 if (stcb->rport != rport) { 2010 SCTP_TCB_UNLOCK(stcb); 2011 continue; 2012 } 2013 if (stcb->sctp_ep->sctp_lport != lport) { 2014 SCTP_TCB_UNLOCK(stcb); 2015 continue; 2016 } 2017 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 2018 SCTP_TCB_UNLOCK(stcb); 2019 continue; 2020 } 2021 /* RRS:Need toaddr check here */ 2022 if (sctp_does_stcb_own_this_addr(stcb, to) == 0) { 2023 /* Endpoint does not own this address */ 2024 SCTP_TCB_UNLOCK(stcb); 2025 continue; 2026 } 2027 if (remote_tag) { 2028 /* 2029 * If we have both vtags that's all we match 2030 * on 2031 */ 2032 if (stcb->asoc.peer_vtag == remote_tag) { 2033 /* 2034 * If both tags match we consider it 2035 * conclusive and check NO 2036 * source/destination addresses 2037 */ 2038 goto conclusive; 2039 } 2040 } 2041 if (skip_src_check) { 2042 conclusive: 2043 if (from) { 2044 *netp = sctp_findnet(stcb, from); 2045 } else { 2046 *netp = NULL; /* unknown */ 2047 } 2048 if (inp_p) 2049 *inp_p = stcb->sctp_ep; 2050 SCTP_INP_INFO_RUNLOCK(); 2051 return (stcb); 2052 } 2053 net = sctp_findnet(stcb, from); 2054 if (net) { 2055 /* yep its him. */ 2056 *netp = net; 2057 SCTP_STAT_INCR(sctps_vtagexpress); 2058 *inp_p = stcb->sctp_ep; 2059 SCTP_INP_INFO_RUNLOCK(); 2060 return (stcb); 2061 } else { 2062 /* 2063 * not him, this should only happen in rare 2064 * cases so I peg it. 2065 */ 2066 SCTP_STAT_INCR(sctps_vtagbogus); 2067 } 2068 } 2069 SCTP_TCB_UNLOCK(stcb); 2070 } 2071 SCTP_INP_INFO_RUNLOCK(); 2072 return (NULL); 2073 } 2074 2075 /* 2076 * Find an association with the pointer to the inbound IP packet. This can be 2077 * a IPv4 or IPv6 packet. 2078 */ 2079 struct sctp_tcb * 2080 sctp_findassociation_addr(struct mbuf *m, int offset, 2081 struct sockaddr *src, struct sockaddr *dst, 2082 struct sctphdr *sh, struct sctp_chunkhdr *ch, 2083 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id) 2084 { 2085 struct sctp_tcb *stcb; 2086 struct sctp_inpcb *inp; 2087 2088 if (sh->v_tag) { 2089 /* we only go down this path if vtag is non-zero */ 2090 stcb = sctp_findassoc_by_vtag(src, dst, ntohl(sh->v_tag), 2091 inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0); 2092 if (stcb) { 2093 return (stcb); 2094 } 2095 } 2096 2097 if (inp_p) { 2098 stcb = sctp_findassociation_addr_sa(src, dst, inp_p, netp, 2099 1, vrf_id); 2100 inp = *inp_p; 2101 } else { 2102 stcb = sctp_findassociation_addr_sa(src, dst, &inp, netp, 2103 1, vrf_id); 2104 } 2105 SCTPDBG(SCTP_DEBUG_PCB1, "stcb:%p inp:%p\n", (void *)stcb, (void *)inp); 2106 if (stcb == NULL && inp) { 2107 /* Found a EP but not this address */ 2108 if ((ch->chunk_type == SCTP_INITIATION) || 2109 (ch->chunk_type == SCTP_INITIATION_ACK)) { 2110 /*- 2111 * special hook, we do NOT return linp or an 2112 * association that is linked to an existing 2113 * association that is under the TCP pool (i.e. no 2114 * listener exists). The endpoint finding routine 2115 * will always find a listener before examining the 2116 * TCP pool. 2117 */ 2118 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) { 2119 if (inp_p) { 2120 *inp_p = NULL; 2121 } 2122 return (NULL); 2123 } 2124 stcb = sctp_findassociation_special_addr(m, 2125 offset, sh, &inp, netp, dst); 2126 if (inp_p != NULL) { 2127 *inp_p = inp; 2128 } 2129 } 2130 } 2131 SCTPDBG(SCTP_DEBUG_PCB1, "stcb is %p\n", (void *)stcb); 2132 return (stcb); 2133 } 2134 2135 /* 2136 * lookup an association by an ASCONF lookup address. 2137 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup 2138 */ 2139 struct sctp_tcb * 2140 sctp_findassociation_ep_asconf(struct mbuf *m, int offset, 2141 struct sockaddr *dst, struct sctphdr *sh, 2142 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id) 2143 { 2144 struct sctp_tcb *stcb; 2145 union sctp_sockstore remote_store; 2146 struct sctp_paramhdr param_buf, *phdr; 2147 int ptype; 2148 int zero_address = 0; 2149 #ifdef INET 2150 struct sockaddr_in *sin; 2151 #endif 2152 #ifdef INET6 2153 struct sockaddr_in6 *sin6; 2154 #endif 2155 2156 memset(&remote_store, 0, sizeof(remote_store)); 2157 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk), 2158 ¶m_buf, sizeof(struct sctp_paramhdr)); 2159 if (phdr == NULL) { 2160 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n", 2161 __func__); 2162 return NULL; 2163 } 2164 ptype = (int)((uint32_t)ntohs(phdr->param_type)); 2165 /* get the correlation address */ 2166 switch (ptype) { 2167 #ifdef INET6 2168 case SCTP_IPV6_ADDRESS: 2169 { 2170 /* ipv6 address param */ 2171 struct sctp_ipv6addr_param *p6, p6_buf; 2172 2173 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) { 2174 return NULL; 2175 } 2176 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m, 2177 offset + sizeof(struct sctp_asconf_chunk), 2178 &p6_buf.ph, sizeof(p6_buf)); 2179 if (p6 == NULL) { 2180 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n", 2181 __func__); 2182 return (NULL); 2183 } 2184 sin6 = &remote_store.sin6; 2185 sin6->sin6_family = AF_INET6; 2186 sin6->sin6_len = sizeof(*sin6); 2187 sin6->sin6_port = sh->src_port; 2188 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr)); 2189 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 2190 zero_address = 1; 2191 break; 2192 } 2193 #endif 2194 #ifdef INET 2195 case SCTP_IPV4_ADDRESS: 2196 { 2197 /* ipv4 address param */ 2198 struct sctp_ipv4addr_param *p4, p4_buf; 2199 2200 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) { 2201 return NULL; 2202 } 2203 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m, 2204 offset + sizeof(struct sctp_asconf_chunk), 2205 &p4_buf.ph, sizeof(p4_buf)); 2206 if (p4 == NULL) { 2207 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n", 2208 __func__); 2209 return (NULL); 2210 } 2211 sin = &remote_store.sin; 2212 sin->sin_family = AF_INET; 2213 sin->sin_len = sizeof(*sin); 2214 sin->sin_port = sh->src_port; 2215 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr)); 2216 if (sin->sin_addr.s_addr == INADDR_ANY) 2217 zero_address = 1; 2218 break; 2219 } 2220 #endif 2221 default: 2222 /* invalid address param type */ 2223 return NULL; 2224 } 2225 2226 if (zero_address) { 2227 stcb = sctp_findassoc_by_vtag(NULL, dst, ntohl(sh->v_tag), inp_p, 2228 netp, sh->src_port, sh->dest_port, 1, vrf_id, 0); 2229 if (stcb != NULL) { 2230 SCTP_INP_DECR_REF(*inp_p); 2231 } 2232 } else { 2233 stcb = sctp_findassociation_ep_addr(inp_p, 2234 &remote_store.sa, netp, 2235 dst, NULL); 2236 } 2237 return (stcb); 2238 } 2239 2240 /* 2241 * allocate a sctp_inpcb and setup a temporary binding to a port/all 2242 * addresses. This way if we don't get a bind we by default pick a ephemeral 2243 * port with all addresses bound. 2244 */ 2245 int 2246 sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id) 2247 { 2248 /* 2249 * we get called when a new endpoint starts up. We need to allocate 2250 * the sctp_inpcb structure from the zone and init it. Mark it as 2251 * unbound and find a port that we can use as an ephemeral with 2252 * INADDR_ANY. If the user binds later no problem we can then add in 2253 * the specific addresses. And setup the default parameters for the 2254 * EP. 2255 */ 2256 int i, error; 2257 struct sctp_inpcb *inp; 2258 struct sctp_pcb *m; 2259 struct timeval time; 2260 sctp_sharedkey_t *null_key; 2261 2262 error = 0; 2263 2264 SCTP_INP_INFO_WLOCK(); 2265 inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb); 2266 if (inp == NULL) { 2267 SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n"); 2268 SCTP_INP_INFO_WUNLOCK(); 2269 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); 2270 return (ENOBUFS); 2271 } 2272 /* zap it */ 2273 memset(inp, 0, sizeof(*inp)); 2274 2275 /* bump generations */ 2276 /* setup socket pointers */ 2277 inp->sctp_socket = so; 2278 inp->ip_inp.inp.inp_socket = so; 2279 inp->ip_inp.inp.inp_cred = crhold(so->so_cred); 2280 #ifdef INET6 2281 if (INP_SOCKAF(so) == AF_INET6) { 2282 if (MODULE_GLOBAL(ip6_auto_flowlabel)) { 2283 inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL; 2284 } 2285 if (MODULE_GLOBAL(ip6_v6only)) { 2286 inp->ip_inp.inp.inp_flags |= IN6P_IPV6_V6ONLY; 2287 } 2288 } 2289 #endif 2290 inp->sctp_associd_counter = 1; 2291 inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT; 2292 inp->sctp_frag_point = 0; 2293 inp->max_cwnd = 0; 2294 inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off); 2295 inp->ecn_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_ecn_enable); 2296 inp->prsctp_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pr_enable); 2297 inp->auth_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_auth_enable); 2298 inp->asconf_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_asconf_enable); 2299 inp->reconfig_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_reconfig_enable); 2300 inp->nrsack_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_nrsack_enable); 2301 inp->pktdrop_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pktdrop_enable); 2302 inp->idata_supported = 0; 2303 inp->rcv_edmid = SCTP_EDMID_NONE; 2304 2305 inp->fibnum = so->so_fibnum; 2306 /* init the small hash table we use to track asocid <-> tcb */ 2307 inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark); 2308 if (inp->sctp_asocidhash == NULL) { 2309 crfree(inp->ip_inp.inp.inp_cred); 2310 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); 2311 SCTP_INP_INFO_WUNLOCK(); 2312 return (ENOBUFS); 2313 } 2314 SCTP_INCR_EP_COUNT(); 2315 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl); 2316 SCTP_INP_INFO_WUNLOCK(); 2317 2318 so->so_pcb = (caddr_t)inp; 2319 2320 if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) { 2321 /* UDP style socket */ 2322 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE | 2323 SCTP_PCB_FLAGS_UNBOUND); 2324 /* Be sure it is NON-BLOCKING IO for UDP */ 2325 /* SCTP_SET_SO_NBIO(so); */ 2326 } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) { 2327 /* TCP style socket */ 2328 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2329 SCTP_PCB_FLAGS_UNBOUND); 2330 /* Be sure we have blocking IO by default */ 2331 SOCK_LOCK(so); 2332 SCTP_CLEAR_SO_NBIO(so); 2333 SOCK_UNLOCK(so); 2334 } else { 2335 /* 2336 * unsupported socket type (RAW, etc)- in case we missed it 2337 * in protosw 2338 */ 2339 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP); 2340 so->so_pcb = NULL; 2341 crfree(inp->ip_inp.inp.inp_cred); 2342 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); 2343 return (EOPNOTSUPP); 2344 } 2345 if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) { 2346 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); 2347 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); 2348 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) { 2349 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); 2350 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); 2351 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) { 2352 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); 2353 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); 2354 } 2355 inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize), 2356 &inp->sctp_hashmark); 2357 if (inp->sctp_tcbhash == NULL) { 2358 SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n"); 2359 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); 2360 so->so_pcb = NULL; 2361 crfree(inp->ip_inp.inp.inp_cred); 2362 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); 2363 return (ENOBUFS); 2364 } 2365 inp->def_vrf_id = vrf_id; 2366 2367 SCTP_INP_INFO_WLOCK(); 2368 SCTP_INP_LOCK_INIT(inp); 2369 rw_init_flags(&inp->ip_inp.inp.inp_lock, "sctpinp", 2370 RW_RECURSE | RW_DUPOK); 2371 SCTP_INP_READ_LOCK_INIT(inp); 2372 SCTP_ASOC_CREATE_LOCK_INIT(inp); 2373 /* lock the new ep */ 2374 SCTP_INP_WLOCK(inp); 2375 2376 /* add it to the info area */ 2377 LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list); 2378 SCTP_INP_INFO_WUNLOCK(); 2379 2380 TAILQ_INIT(&inp->read_queue); 2381 LIST_INIT(&inp->sctp_addr_list); 2382 2383 LIST_INIT(&inp->sctp_asoc_list); 2384 2385 #ifdef SCTP_TRACK_FREED_ASOCS 2386 /* TEMP CODE */ 2387 LIST_INIT(&inp->sctp_asoc_free_list); 2388 #endif 2389 /* Init the timer structure for signature change */ 2390 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer); 2391 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE; 2392 2393 /* now init the actual endpoint default data */ 2394 m = &inp->sctp_ep; 2395 2396 /* setup the base timeout information */ 2397 m->sctp_timeoutticks[SCTP_TIMER_SEND] = sctp_secs_to_ticks(SCTP_SEND_SEC); /* needed ? */ 2398 m->sctp_timeoutticks[SCTP_TIMER_INIT] = sctp_secs_to_ticks(SCTP_INIT_SEC); /* needed ? */ 2399 m->sctp_timeoutticks[SCTP_TIMER_RECV] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default)); 2400 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default)); 2401 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default)); 2402 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default)); 2403 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default)); 2404 /* all max/min max are in ms */ 2405 m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default); 2406 m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default); 2407 m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default); 2408 m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default); 2409 m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default); 2410 m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default); 2411 m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default); 2412 m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default); 2413 m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold); 2414 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF; 2415 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF; 2416 m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default); 2417 m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default); 2418 2419 m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module); 2420 m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module); 2421 m->max_open_streams_intome = SCTP_BASE_SYSCTL(sctp_nr_incoming_streams_default); 2422 /* number of streams to pre-open on a association */ 2423 m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default); 2424 2425 m->default_mtu = 0; 2426 /* Add adaptation cookie */ 2427 m->adaptation_layer_indicator = 0; 2428 m->adaptation_layer_indicator_provided = 0; 2429 2430 /* seed random number generator */ 2431 m->random_counter = 1; 2432 m->store_at = SCTP_SIGNATURE_SIZE; 2433 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers)); 2434 sctp_fill_random_store(m); 2435 2436 /* Minimum cookie size */ 2437 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) + 2438 sizeof(struct sctp_state_cookie); 2439 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE; 2440 2441 /* Setup the initial secret */ 2442 (void)SCTP_GETTIME_TIMEVAL(&time); 2443 m->time_of_secret_change = time.tv_sec; 2444 2445 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) { 2446 m->secret_key[0][i] = sctp_select_initial_TSN(m); 2447 } 2448 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL); 2449 2450 /* How long is a cookie good for ? */ 2451 m->def_cookie_life = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default)); 2452 /* 2453 * Initialize authentication parameters 2454 */ 2455 m->local_hmacs = sctp_default_supported_hmaclist(); 2456 m->local_auth_chunks = sctp_alloc_chunklist(); 2457 if (inp->asconf_supported) { 2458 sctp_auth_add_chunk(SCTP_ASCONF, m->local_auth_chunks); 2459 sctp_auth_add_chunk(SCTP_ASCONF_ACK, m->local_auth_chunks); 2460 } 2461 m->default_dscp = 0; 2462 #ifdef INET6 2463 m->default_flowlabel = 0; 2464 #endif 2465 m->port = 0; /* encapsulation disabled by default */ 2466 LIST_INIT(&m->shared_keys); 2467 /* add default NULL key as key id 0 */ 2468 null_key = sctp_alloc_sharedkey(); 2469 sctp_insert_sharedkey(&m->shared_keys, null_key); 2470 SCTP_INP_WUNLOCK(inp); 2471 #ifdef SCTP_LOG_CLOSING 2472 sctp_log_closing(inp, NULL, 12); 2473 #endif 2474 return (error); 2475 } 2476 2477 void 2478 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp, 2479 struct sctp_tcb *stcb) 2480 { 2481 struct sctp_nets *net; 2482 uint16_t lport, rport; 2483 struct sctppcbhead *head; 2484 struct sctp_laddr *laddr, *oladdr; 2485 2486 atomic_add_int(&stcb->asoc.refcnt, 1); 2487 SCTP_TCB_UNLOCK(stcb); 2488 SCTP_INP_INFO_WLOCK(); 2489 SCTP_INP_WLOCK(old_inp); 2490 SCTP_INP_WLOCK(new_inp); 2491 SCTP_TCB_LOCK(stcb); 2492 atomic_subtract_int(&stcb->asoc.refcnt, 1); 2493 2494 #ifdef INET6 2495 if (old_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 2496 new_inp->ip_inp.inp.inp_flags |= old_inp->ip_inp.inp.inp_flags & INP_CONTROLOPTS; 2497 if (old_inp->ip_inp.inp.in6p_outputopts) { 2498 new_inp->ip_inp.inp.in6p_outputopts = ip6_copypktopts(old_inp->ip_inp.inp.in6p_outputopts, M_NOWAIT); 2499 } 2500 } 2501 #endif 2502 #if defined(INET) && defined(INET6) 2503 else 2504 #endif 2505 #ifdef INET 2506 { 2507 new_inp->ip_inp.inp.inp_ip_tos = old_inp->ip_inp.inp.inp_ip_tos; 2508 new_inp->ip_inp.inp.inp_ip_ttl = old_inp->ip_inp.inp.inp_ip_ttl; 2509 } 2510 #endif 2511 new_inp->sctp_ep.time_of_secret_change = 2512 old_inp->sctp_ep.time_of_secret_change; 2513 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key, 2514 sizeof(old_inp->sctp_ep.secret_key)); 2515 new_inp->sctp_ep.current_secret_number = 2516 old_inp->sctp_ep.current_secret_number; 2517 new_inp->sctp_ep.last_secret_number = 2518 old_inp->sctp_ep.last_secret_number; 2519 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie; 2520 2521 /* make it so new data pours into the new socket */ 2522 stcb->sctp_socket = new_inp->sctp_socket; 2523 stcb->sctp_ep = new_inp; 2524 2525 /* Copy the port across */ 2526 lport = new_inp->sctp_lport = old_inp->sctp_lport; 2527 rport = stcb->rport; 2528 /* Pull the tcb from the old association */ 2529 LIST_REMOVE(stcb, sctp_tcbhash); 2530 LIST_REMOVE(stcb, sctp_tcblist); 2531 if (stcb->asoc.in_asocid_hash) { 2532 LIST_REMOVE(stcb, sctp_tcbasocidhash); 2533 } 2534 /* Now insert the new_inp into the TCP connected hash */ 2535 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))]; 2536 2537 LIST_INSERT_HEAD(head, new_inp, sctp_hash); 2538 /* Its safe to access */ 2539 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND; 2540 2541 /* Now move the tcb into the endpoint list */ 2542 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist); 2543 /* 2544 * Question, do we even need to worry about the ep-hash since we 2545 * only have one connection? Probably not :> so lets get rid of it 2546 * and not suck up any kernel memory in that. 2547 */ 2548 if (stcb->asoc.in_asocid_hash) { 2549 struct sctpasochead *lhd; 2550 2551 lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id, 2552 new_inp->hashasocidmark)]; 2553 LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash); 2554 } 2555 /* Ok. Let's restart timer. */ 2556 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 2557 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp, 2558 stcb, net); 2559 } 2560 2561 SCTP_INP_INFO_WUNLOCK(); 2562 if (new_inp->sctp_tcbhash != NULL) { 2563 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark); 2564 new_inp->sctp_tcbhash = NULL; 2565 } 2566 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { 2567 /* Subset bound, so copy in the laddr list from the old_inp */ 2568 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) { 2569 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); 2570 if (laddr == NULL) { 2571 /* 2572 * Gak, what can we do? This assoc is really 2573 * HOSED. We probably should send an abort 2574 * here. 2575 */ 2576 SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n"); 2577 continue; 2578 } 2579 SCTP_INCR_LADDR_COUNT(); 2580 memset(laddr, 0, sizeof(*laddr)); 2581 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time); 2582 laddr->ifa = oladdr->ifa; 2583 atomic_add_int(&laddr->ifa->refcount, 1); 2584 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr, 2585 sctp_nxt_addr); 2586 new_inp->laddr_count++; 2587 if (oladdr == stcb->asoc.last_used_address) { 2588 stcb->asoc.last_used_address = laddr; 2589 } 2590 } 2591 } 2592 /* Now any running timers need to be adjusted. */ 2593 if (stcb->asoc.dack_timer.ep == old_inp) { 2594 SCTP_INP_DECR_REF(old_inp); 2595 stcb->asoc.dack_timer.ep = new_inp; 2596 SCTP_INP_INCR_REF(new_inp); 2597 } 2598 if (stcb->asoc.asconf_timer.ep == old_inp) { 2599 SCTP_INP_DECR_REF(old_inp); 2600 stcb->asoc.asconf_timer.ep = new_inp; 2601 SCTP_INP_INCR_REF(new_inp); 2602 } 2603 if (stcb->asoc.strreset_timer.ep == old_inp) { 2604 SCTP_INP_DECR_REF(old_inp); 2605 stcb->asoc.strreset_timer.ep = new_inp; 2606 SCTP_INP_INCR_REF(new_inp); 2607 } 2608 if (stcb->asoc.shut_guard_timer.ep == old_inp) { 2609 SCTP_INP_DECR_REF(old_inp); 2610 stcb->asoc.shut_guard_timer.ep = new_inp; 2611 SCTP_INP_INCR_REF(new_inp); 2612 } 2613 if (stcb->asoc.autoclose_timer.ep == old_inp) { 2614 SCTP_INP_DECR_REF(old_inp); 2615 stcb->asoc.autoclose_timer.ep = new_inp; 2616 SCTP_INP_INCR_REF(new_inp); 2617 } 2618 if (stcb->asoc.delete_prim_timer.ep == old_inp) { 2619 SCTP_INP_DECR_REF(old_inp); 2620 stcb->asoc.delete_prim_timer.ep = new_inp; 2621 SCTP_INP_INCR_REF(new_inp); 2622 } 2623 /* now what about the nets? */ 2624 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 2625 if (net->pmtu_timer.ep == old_inp) { 2626 SCTP_INP_DECR_REF(old_inp); 2627 net->pmtu_timer.ep = new_inp; 2628 SCTP_INP_INCR_REF(new_inp); 2629 } 2630 if (net->hb_timer.ep == old_inp) { 2631 SCTP_INP_DECR_REF(old_inp); 2632 net->hb_timer.ep = new_inp; 2633 SCTP_INP_INCR_REF(new_inp); 2634 } 2635 if (net->rxt_timer.ep == old_inp) { 2636 SCTP_INP_DECR_REF(old_inp); 2637 net->rxt_timer.ep = new_inp; 2638 SCTP_INP_INCR_REF(new_inp); 2639 } 2640 } 2641 SCTP_INP_WUNLOCK(new_inp); 2642 SCTP_INP_WUNLOCK(old_inp); 2643 } 2644 2645 /* 2646 * insert an laddr entry with the given ifa for the desired list 2647 */ 2648 static int 2649 sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act) 2650 { 2651 struct sctp_laddr *laddr; 2652 2653 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); 2654 if (laddr == NULL) { 2655 /* out of memory? */ 2656 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 2657 return (EINVAL); 2658 } 2659 SCTP_INCR_LADDR_COUNT(); 2660 memset(laddr, 0, sizeof(*laddr)); 2661 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time); 2662 laddr->ifa = ifa; 2663 laddr->action = act; 2664 atomic_add_int(&ifa->refcount, 1); 2665 /* insert it */ 2666 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr); 2667 2668 return (0); 2669 } 2670 2671 /* 2672 * Remove an laddr entry from the local address list (on an assoc) 2673 */ 2674 static void 2675 sctp_remove_laddr(struct sctp_laddr *laddr) 2676 { 2677 2678 /* remove from the list */ 2679 LIST_REMOVE(laddr, sctp_nxt_addr); 2680 sctp_free_ifa(laddr->ifa); 2681 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr); 2682 SCTP_DECR_LADDR_COUNT(); 2683 } 2684 2685 /* 2686 * Bind the socket, with the PCB and global info locks held. Note, if a 2687 * socket address is specified, the PCB lock may be dropped and re-acquired. 2688 * 2689 * sctp_ifap is used to bypass normal local address validation checks. 2690 */ 2691 int 2692 sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr, 2693 struct sctp_ifa *sctp_ifap, struct thread *td) 2694 { 2695 /* bind a ep to a socket address */ 2696 struct sctppcbhead *head; 2697 struct sctp_inpcb *inp_tmp; 2698 struct inpcb *ip_inp; 2699 int port_reuse_active = 0; 2700 int bindall; 2701 uint16_t lport; 2702 int error; 2703 uint32_t vrf_id; 2704 2705 KASSERT(td != NULL, ("%s: null thread", __func__)); 2706 2707 error = 0; 2708 lport = 0; 2709 bindall = 1; 2710 ip_inp = &inp->ip_inp.inp; 2711 2712 SCTP_INP_INFO_WLOCK_ASSERT(); 2713 SCTP_INP_WLOCK_ASSERT(inp); 2714 2715 #ifdef SCTP_DEBUG 2716 if (addr) { 2717 SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port: %d\n", 2718 ntohs(((struct sockaddr_in *)addr)->sin_port)); 2719 SCTPDBG(SCTP_DEBUG_PCB1, "Addr: "); 2720 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr); 2721 } 2722 #endif 2723 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) { 2724 error = EINVAL; 2725 /* already did a bind, subsequent binds NOT allowed ! */ 2726 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 2727 goto out; 2728 } 2729 if (addr != NULL) { 2730 switch (addr->sa_family) { 2731 #ifdef INET 2732 case AF_INET: 2733 { 2734 struct sockaddr_in *sin; 2735 2736 /* IPV6_V6ONLY socket? */ 2737 if (SCTP_IPV6_V6ONLY(inp)) { 2738 error = EINVAL; 2739 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 2740 goto out; 2741 } 2742 if (addr->sa_len != sizeof(*sin)) { 2743 error = EINVAL; 2744 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 2745 goto out; 2746 } 2747 2748 sin = (struct sockaddr_in *)addr; 2749 lport = sin->sin_port; 2750 /* 2751 * For LOOPBACK the prison_local_ip4() call 2752 * will transmute the ip address to the 2753 * proper value. 2754 */ 2755 if ((error = prison_local_ip4(td->td_ucred, &sin->sin_addr)) != 0) { 2756 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 2757 goto out; 2758 } 2759 if (sin->sin_addr.s_addr != INADDR_ANY) { 2760 bindall = 0; 2761 } 2762 break; 2763 } 2764 #endif 2765 #ifdef INET6 2766 case AF_INET6: 2767 { 2768 /* 2769 * Only for pure IPv6 Address. (No IPv4 2770 * Mapped!) 2771 */ 2772 struct sockaddr_in6 *sin6; 2773 2774 sin6 = (struct sockaddr_in6 *)addr; 2775 if (addr->sa_len != sizeof(*sin6)) { 2776 error = EINVAL; 2777 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 2778 goto out; 2779 } 2780 lport = sin6->sin6_port; 2781 /* 2782 * For LOOPBACK the prison_local_ip6() call 2783 * will transmute the ipv6 address to the 2784 * proper value. 2785 */ 2786 if ((error = prison_local_ip6(td->td_ucred, &sin6->sin6_addr, 2787 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) { 2788 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 2789 goto out; 2790 } 2791 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 2792 bindall = 0; 2793 /* KAME hack: embed scopeid */ 2794 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) { 2795 error = EINVAL; 2796 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 2797 goto out; 2798 } 2799 } 2800 /* this must be cleared for ifa_ifwithaddr() */ 2801 sin6->sin6_scope_id = 0; 2802 break; 2803 } 2804 #endif 2805 default: 2806 error = EAFNOSUPPORT; 2807 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 2808 goto out; 2809 } 2810 } 2811 /* Setup a vrf_id to be the default for the non-bind-all case. */ 2812 vrf_id = inp->def_vrf_id; 2813 2814 if (lport) { 2815 /* 2816 * Did the caller specify a port? if so we must see if an ep 2817 * already has this one bound. 2818 */ 2819 /* got to be root to get at low ports */ 2820 if (ntohs(lport) < IPPORT_RESERVED && 2821 (error = priv_check(td, PRIV_NETINET_RESERVEDPORT)) != 0) { 2822 goto out; 2823 } 2824 SCTP_INP_INCR_REF(inp); 2825 SCTP_INP_WUNLOCK(inp); 2826 if (bindall) { 2827 vrf_id = inp->def_vrf_id; 2828 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id); 2829 if (inp_tmp != NULL) { 2830 /* 2831 * lock guy returned and lower count note 2832 * that we are not bound so inp_tmp should 2833 * NEVER be inp. And it is this inp 2834 * (inp_tmp) that gets the reference bump, 2835 * so we must lower it. 2836 */ 2837 SCTP_INP_DECR_REF(inp_tmp); 2838 /* unlock info */ 2839 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && 2840 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { 2841 /* 2842 * Ok, must be one-2-one and 2843 * allowing port re-use 2844 */ 2845 port_reuse_active = 1; 2846 goto continue_anyway; 2847 } 2848 SCTP_INP_WLOCK(inp); 2849 SCTP_INP_DECR_REF(inp); 2850 error = EADDRINUSE; 2851 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 2852 goto out; 2853 } 2854 } else { 2855 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id); 2856 if (inp_tmp != NULL) { 2857 /* 2858 * lock guy returned and lower count note 2859 * that we are not bound so inp_tmp should 2860 * NEVER be inp. And it is this inp 2861 * (inp_tmp) that gets the reference bump, 2862 * so we must lower it. 2863 */ 2864 SCTP_INP_DECR_REF(inp_tmp); 2865 /* unlock info */ 2866 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && 2867 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { 2868 /* 2869 * Ok, must be one-2-one and 2870 * allowing port re-use 2871 */ 2872 port_reuse_active = 1; 2873 goto continue_anyway; 2874 } 2875 SCTP_INP_WLOCK(inp); 2876 SCTP_INP_DECR_REF(inp); 2877 error = EADDRINUSE; 2878 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 2879 goto out; 2880 } 2881 } 2882 continue_anyway: 2883 SCTP_INP_WLOCK(inp); 2884 SCTP_INP_DECR_REF(inp); 2885 if (bindall) { 2886 /* verify that no lport is not used by a singleton */ 2887 if ((port_reuse_active == 0) && 2888 (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) { 2889 /* Sorry someone already has this one bound */ 2890 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && 2891 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { 2892 port_reuse_active = 1; 2893 } else { 2894 error = EADDRINUSE; 2895 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 2896 goto out; 2897 } 2898 } 2899 } 2900 } else { 2901 uint16_t first, last, candidate; 2902 uint16_t count; 2903 2904 if (ip_inp->inp_flags & INP_HIGHPORT) { 2905 first = MODULE_GLOBAL(ipport_hifirstauto); 2906 last = MODULE_GLOBAL(ipport_hilastauto); 2907 } else if (ip_inp->inp_flags & INP_LOWPORT) { 2908 if ((error = priv_check(td, PRIV_NETINET_RESERVEDPORT)) != 0) { 2909 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 2910 goto out; 2911 } 2912 first = MODULE_GLOBAL(ipport_lowfirstauto); 2913 last = MODULE_GLOBAL(ipport_lowlastauto); 2914 } else { 2915 first = MODULE_GLOBAL(ipport_firstauto); 2916 last = MODULE_GLOBAL(ipport_lastauto); 2917 } 2918 if (first > last) { 2919 uint16_t temp; 2920 2921 temp = first; 2922 first = last; 2923 last = temp; 2924 } 2925 count = last - first + 1; /* number of candidates */ 2926 candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count); 2927 2928 for (;;) { 2929 if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) { 2930 lport = htons(candidate); 2931 break; 2932 } 2933 if (--count == 0) { 2934 error = EADDRINUSE; 2935 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 2936 goto out; 2937 } 2938 if (candidate == last) 2939 candidate = first; 2940 else 2941 candidate = candidate + 1; 2942 } 2943 } 2944 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | 2945 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 2946 /* 2947 * this really should not happen. The guy did a non-blocking 2948 * bind and then did a close at the same time. 2949 */ 2950 error = EINVAL; 2951 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 2952 goto out; 2953 } 2954 /* ok we look clear to give out this port, so lets setup the binding */ 2955 if (bindall) { 2956 /* binding to all addresses, so just set in the proper flags */ 2957 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL; 2958 /* set the automatic addr changes from kernel flag */ 2959 if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) { 2960 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF); 2961 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); 2962 } else { 2963 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF); 2964 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); 2965 } 2966 if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) { 2967 sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS); 2968 } else { 2969 sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS); 2970 } 2971 /* 2972 * set the automatic mobility_base from kernel flag (by 2973 * micchie) 2974 */ 2975 if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) { 2976 sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE); 2977 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); 2978 } else { 2979 sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE); 2980 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); 2981 } 2982 /* 2983 * set the automatic mobility_fasthandoff from kernel flag 2984 * (by micchie) 2985 */ 2986 if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) { 2987 sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF); 2988 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); 2989 } else { 2990 sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF); 2991 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); 2992 } 2993 } else { 2994 /* 2995 * bind specific, make sure flags is off and add a new 2996 * address structure to the sctp_addr_list inside the ep 2997 * structure. 2998 * 2999 * We will need to allocate one and insert it at the head. 3000 * The socketopt call can just insert new addresses in there 3001 * as well. It will also have to do the embed scope kame 3002 * hack too (before adding). 3003 */ 3004 struct sctp_ifa *ifa; 3005 union sctp_sockstore store; 3006 3007 memset(&store, 0, sizeof(store)); 3008 switch (addr->sa_family) { 3009 #ifdef INET 3010 case AF_INET: 3011 memcpy(&store.sin, addr, sizeof(struct sockaddr_in)); 3012 store.sin.sin_port = 0; 3013 break; 3014 #endif 3015 #ifdef INET6 3016 case AF_INET6: 3017 memcpy(&store.sin6, addr, sizeof(struct sockaddr_in6)); 3018 store.sin6.sin6_port = 0; 3019 break; 3020 #endif 3021 default: 3022 break; 3023 } 3024 /* 3025 * first find the interface with the bound address need to 3026 * zero out the port to find the address! yuck! can't do 3027 * this earlier since need port for sctp_pcb_findep() 3028 */ 3029 if (sctp_ifap != NULL) { 3030 ifa = sctp_ifap; 3031 } else { 3032 /* 3033 * Note for BSD we hit here always other O/S's will 3034 * pass things in via the sctp_ifap argument. 3035 */ 3036 ifa = sctp_find_ifa_by_addr(&store.sa, 3037 vrf_id, SCTP_ADDR_NOT_LOCKED); 3038 } 3039 if (ifa == NULL) { 3040 error = EADDRNOTAVAIL; 3041 /* Can't find an interface with that address */ 3042 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 3043 goto out; 3044 } 3045 #ifdef INET6 3046 if (addr->sa_family == AF_INET6) { 3047 /* GAK, more FIXME IFA lock? */ 3048 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { 3049 /* Can't bind a non-existent addr. */ 3050 error = EINVAL; 3051 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 3052 goto out; 3053 } 3054 } 3055 #endif 3056 /* we're not bound all */ 3057 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL; 3058 /* allow bindx() to send ASCONF's for binding changes */ 3059 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF); 3060 /* clear automatic addr changes from kernel flag */ 3061 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); 3062 3063 /* add this address to the endpoint list */ 3064 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0); 3065 if (error != 0) 3066 goto out; 3067 inp->laddr_count++; 3068 } 3069 /* find the bucket */ 3070 if (port_reuse_active) { 3071 /* Put it into tcp 1-2-1 hash */ 3072 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))]; 3073 inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL; 3074 } else { 3075 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))]; 3076 } 3077 /* put it in the bucket */ 3078 LIST_INSERT_HEAD(head, inp, sctp_hash); 3079 SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d - in tcp_pool=%d\n", 3080 (void *)head, ntohs(lport), port_reuse_active); 3081 /* set in the port */ 3082 inp->sctp_lport = lport; 3083 3084 /* turn off just the unbound flag */ 3085 KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) != 0, 3086 ("%s: inp %p is already bound", __func__, inp)); 3087 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND; 3088 out: 3089 return (error); 3090 } 3091 3092 int 3093 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, 3094 struct sctp_ifa *sctp_ifap, struct thread *td) 3095 { 3096 struct sctp_inpcb *inp; 3097 int error; 3098 3099 inp = so->so_pcb; 3100 SCTP_INP_INFO_WLOCK(); 3101 SCTP_INP_WLOCK(inp); 3102 error = sctp_inpcb_bind_locked(inp, addr, sctp_ifap, td); 3103 SCTP_INP_WUNLOCK(inp); 3104 SCTP_INP_INFO_WUNLOCK(); 3105 return (error); 3106 } 3107 3108 static void 3109 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp) 3110 { 3111 struct sctp_iterator *it, *nit; 3112 3113 /* 3114 * We enter with the only the ITERATOR_LOCK in place and a write 3115 * lock on the inp_info stuff. 3116 */ 3117 it = sctp_it_ctl.cur_it; 3118 if (it && (it->vn != curvnet)) { 3119 /* Its not looking at our VNET */ 3120 return; 3121 } 3122 if (it && (it->inp == inp)) { 3123 /* 3124 * This is tricky and we hold the iterator lock, but when it 3125 * returns and gets the lock (when we release it) the 3126 * iterator will try to operate on inp. We need to stop that 3127 * from happening. But of course the iterator has a 3128 * reference on the stcb and inp. We can mark it and it will 3129 * stop. 3130 * 3131 * If its a single iterator situation, we set the end 3132 * iterator flag. Otherwise we set the iterator to go to the 3133 * next inp. 3134 * 3135 */ 3136 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { 3137 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT; 3138 } else { 3139 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP; 3140 } 3141 } 3142 /* 3143 * Now go through and remove any single reference to our inp that 3144 * may be still pending on the list 3145 */ 3146 SCTP_IPI_ITERATOR_WQ_LOCK(); 3147 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) { 3148 if (it->vn != curvnet) { 3149 continue; 3150 } 3151 if (it->inp == inp) { 3152 /* This one points to me is it inp specific? */ 3153 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { 3154 /* Remove and free this one */ 3155 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, 3156 it, sctp_nxt_itr); 3157 if (it->function_atend != NULL) { 3158 (*it->function_atend) (it->pointer, it->val); 3159 } 3160 SCTP_FREE(it, SCTP_M_ITER); 3161 } else { 3162 it->inp = LIST_NEXT(it->inp, sctp_list); 3163 if (it->inp) { 3164 SCTP_INP_INCR_REF(it->inp); 3165 } 3166 } 3167 /* 3168 * When its put in the refcnt is incremented so decr 3169 * it 3170 */ 3171 SCTP_INP_DECR_REF(inp); 3172 } 3173 } 3174 SCTP_IPI_ITERATOR_WQ_UNLOCK(); 3175 } 3176 3177 /* release sctp_inpcb unbind the port */ 3178 void 3179 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from) 3180 { 3181 /* 3182 * Here we free a endpoint. We must find it (if it is in the Hash 3183 * table) and remove it from there. Then we must also find it in the 3184 * overall list and remove it from there. After all removals are 3185 * complete then any timer has to be stopped. Then start the actual 3186 * freeing. a) Any local lists. b) Any associations. c) The hash of 3187 * all associations. d) finally the ep itself. 3188 */ 3189 struct sctp_tcb *stcb, *nstcb; 3190 struct sctp_laddr *laddr, *nladdr; 3191 struct inpcb *ip_pcb; 3192 struct socket *so; 3193 int being_refed = 0; 3194 struct sctp_queued_to_read *sq, *nsq; 3195 int cnt; 3196 sctp_sharedkey_t *shared_key, *nshared_key; 3197 3198 #ifdef SCTP_LOG_CLOSING 3199 sctp_log_closing(inp, NULL, 0); 3200 #endif 3201 SCTP_ITERATOR_LOCK(); 3202 /* mark any iterators on the list or being processed */ 3203 sctp_iterator_inp_being_freed(inp); 3204 SCTP_ITERATOR_UNLOCK(); 3205 3206 SCTP_ASOC_CREATE_LOCK(inp); 3207 SCTP_INP_INFO_WLOCK(); 3208 SCTP_INP_WLOCK(inp); 3209 so = inp->sctp_socket; 3210 KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) != 0, 3211 ("%s: inp %p still has socket", __func__, inp)); 3212 KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0, 3213 ("%s: double free of inp %p", __func__, inp)); 3214 if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) { 3215 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP; 3216 /* socket is gone, so no more wakeups allowed */ 3217 inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE; 3218 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT; 3219 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT; 3220 } 3221 /* First time through we have the socket lock, after that no more. */ 3222 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL, 3223 SCTP_FROM_SCTP_PCB + SCTP_LOC_1); 3224 3225 if (inp->control) { 3226 sctp_m_freem(inp->control); 3227 inp->control = NULL; 3228 } 3229 if (inp->pkt) { 3230 sctp_m_freem(inp->pkt); 3231 inp->pkt = NULL; 3232 } 3233 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer 3234 * here but I will be nice :> (i.e. 3235 * ip_pcb = ep;) */ 3236 if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) { 3237 int cnt_in_sd; 3238 3239 cnt_in_sd = 0; 3240 LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_list, sctp_tcblist, nstcb) { 3241 SCTP_TCB_LOCK(stcb); 3242 /* Disconnect the socket please. */ 3243 stcb->sctp_socket = NULL; 3244 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_CLOSED_SOCKET); 3245 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 3246 /* Skip guys being freed */ 3247 cnt_in_sd++; 3248 if (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) { 3249 /* 3250 * Special case - we did not start a 3251 * kill timer on the asoc due to it 3252 * was not closed. So go ahead and 3253 * start it now. 3254 */ 3255 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE); 3256 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); 3257 } 3258 SCTP_TCB_UNLOCK(stcb); 3259 continue; 3260 } 3261 if (((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) || 3262 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) && 3263 (stcb->asoc.total_output_queue_size == 0)) { 3264 /* 3265 * If we have data in queue, we don't want 3266 * to just free since the app may have done, 3267 * send()/close or connect/send/close. And 3268 * it wants the data to get across first. 3269 */ 3270 /* Just abandon things in the front states */ 3271 if (sctp_free_assoc(inp, stcb, SCTP_PCBFREE_NOFORCE, 3272 SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) { 3273 cnt_in_sd++; 3274 } 3275 continue; 3276 } 3277 if ((stcb->asoc.size_on_reasm_queue > 0) || 3278 (stcb->asoc.size_on_all_streams > 0) || 3279 ((so != NULL) && (SCTP_SBAVAIL(&so->so_rcv) > 0))) { 3280 /* Left with Data unread */ 3281 struct mbuf *op_err; 3282 3283 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, ""); 3284 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3; 3285 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED); 3286 SCTP_STAT_INCR_COUNTER32(sctps_aborted); 3287 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) || 3288 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 3289 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 3290 } 3291 if (sctp_free_assoc(inp, stcb, 3292 SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) { 3293 cnt_in_sd++; 3294 } 3295 continue; 3296 } else if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 3297 TAILQ_EMPTY(&stcb->asoc.sent_queue) && 3298 (stcb->asoc.stream_queue_cnt == 0)) { 3299 if ((*stcb->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, &stcb->asoc)) { 3300 goto abort_anyway; 3301 } 3302 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) && 3303 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 3304 struct sctp_nets *netp; 3305 3306 /* 3307 * there is nothing queued to send, 3308 * so I send shutdown 3309 */ 3310 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) || 3311 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 3312 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 3313 } 3314 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT); 3315 sctp_stop_timers_for_shutdown(stcb); 3316 if (stcb->asoc.alternate) { 3317 netp = stcb->asoc.alternate; 3318 } else { 3319 netp = stcb->asoc.primary_destination; 3320 } 3321 sctp_send_shutdown(stcb, netp); 3322 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, 3323 netp); 3324 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, NULL); 3325 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED); 3326 } 3327 } else { 3328 /* mark into shutdown pending */ 3329 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING); 3330 if ((*stcb->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, &stcb->asoc)) { 3331 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT); 3332 } 3333 if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 3334 TAILQ_EMPTY(&stcb->asoc.sent_queue) && 3335 (stcb->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) { 3336 struct mbuf *op_err; 3337 3338 abort_anyway: 3339 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, ""); 3340 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5; 3341 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED); 3342 SCTP_STAT_INCR_COUNTER32(sctps_aborted); 3343 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) || 3344 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 3345 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 3346 } 3347 if (sctp_free_assoc(inp, stcb, 3348 SCTP_PCBFREE_NOFORCE, 3349 SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) { 3350 cnt_in_sd++; 3351 } 3352 continue; 3353 } else { 3354 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED); 3355 } 3356 } 3357 cnt_in_sd++; 3358 SCTP_TCB_UNLOCK(stcb); 3359 } 3360 /* now is there some left in our SHUTDOWN state? */ 3361 if (cnt_in_sd) { 3362 #ifdef SCTP_LOG_CLOSING 3363 sctp_log_closing(inp, NULL, 2); 3364 #endif 3365 inp->sctp_socket = NULL; 3366 SCTP_INP_WUNLOCK(inp); 3367 SCTP_ASOC_CREATE_UNLOCK(inp); 3368 SCTP_INP_INFO_WUNLOCK(); 3369 return; 3370 } 3371 } 3372 inp->sctp_socket = NULL; 3373 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) { 3374 /* 3375 * ok, this guy has been bound. It's port is somewhere in 3376 * the SCTP_BASE_INFO(hash table). Remove it! 3377 */ 3378 LIST_REMOVE(inp, sctp_hash); 3379 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND; 3380 } 3381 3382 /* 3383 * If there is a timer running to kill us, forget it, since it may 3384 * have a contest on the INP lock.. which would cause us to die ... 3385 */ 3386 cnt = 0; 3387 LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_list, sctp_tcblist, nstcb) { 3388 SCTP_TCB_LOCK(stcb); 3389 if (immediate != SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) { 3390 /* Disconnect the socket please */ 3391 stcb->sctp_socket = NULL; 3392 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_CLOSED_SOCKET); 3393 } 3394 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 3395 if (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) { 3396 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE); 3397 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); 3398 } 3399 cnt++; 3400 SCTP_TCB_UNLOCK(stcb); 3401 continue; 3402 } 3403 /* Free associations that are NOT killing us */ 3404 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT) && 3405 ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) { 3406 struct mbuf *op_err; 3407 3408 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, ""); 3409 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7; 3410 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED); 3411 SCTP_STAT_INCR_COUNTER32(sctps_aborted); 3412 } else if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 3413 cnt++; 3414 SCTP_TCB_UNLOCK(stcb); 3415 continue; 3416 } 3417 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) || 3418 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 3419 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 3420 } 3421 if (sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, 3422 SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) { 3423 cnt++; 3424 } 3425 } 3426 if (cnt) { 3427 /* Ok we have someone out there that will kill us */ 3428 #ifdef SCTP_LOG_CLOSING 3429 sctp_log_closing(inp, NULL, 3); 3430 #endif 3431 SCTP_INP_WUNLOCK(inp); 3432 SCTP_ASOC_CREATE_UNLOCK(inp); 3433 SCTP_INP_INFO_WUNLOCK(); 3434 return; 3435 } 3436 if (SCTP_INP_LOCK_CONTENDED(inp)) 3437 being_refed++; 3438 if (SCTP_INP_READ_CONTENDED(inp)) 3439 being_refed++; 3440 if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp)) 3441 being_refed++; 3442 /* NOTE: 0 refcount also means no timers are referencing us. */ 3443 if ((inp->refcount) || 3444 (being_refed) || 3445 (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) { 3446 #ifdef SCTP_LOG_CLOSING 3447 sctp_log_closing(inp, NULL, 4); 3448 #endif 3449 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL); 3450 SCTP_INP_WUNLOCK(inp); 3451 SCTP_ASOC_CREATE_UNLOCK(inp); 3452 SCTP_INP_INFO_WUNLOCK(); 3453 return; 3454 } 3455 inp->sctp_ep.signature_change.type = 0; 3456 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE; 3457 /* 3458 * Remove it from the list .. last thing we need a lock for. 3459 */ 3460 LIST_REMOVE(inp, sctp_list); 3461 SCTP_INP_WUNLOCK(inp); 3462 SCTP_ASOC_CREATE_UNLOCK(inp); 3463 SCTP_INP_INFO_WUNLOCK(); 3464 3465 #ifdef SCTP_LOG_CLOSING 3466 sctp_log_closing(inp, NULL, 5); 3467 #endif 3468 if ((inp->sctp_asocidhash) != NULL) { 3469 SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark); 3470 inp->sctp_asocidhash = NULL; 3471 } 3472 /* sa_ignore FREED_MEMORY */ 3473 TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) { 3474 /* Its only abandoned if it had data left */ 3475 if (sq->length) 3476 SCTP_STAT_INCR(sctps_left_abandon); 3477 3478 TAILQ_REMOVE(&inp->read_queue, sq, next); 3479 sctp_free_remote_addr(sq->whoFrom); 3480 if (so) 3481 SCTP_SB_DECR(&so->so_rcv, sq->length); 3482 if (sq->data) { 3483 sctp_m_freem(sq->data); 3484 sq->data = NULL; 3485 } 3486 /* 3487 * no need to free the net count, since at this point all 3488 * assoc's are gone. 3489 */ 3490 sctp_free_a_readq(NULL, sq); 3491 } 3492 /* Now the sctp_pcb things */ 3493 /* 3494 * free each asoc if it is not already closed/free. we can't use the 3495 * macro here since le_next will get freed as part of the 3496 * sctp_free_assoc() call. 3497 */ 3498 if (ip_pcb->inp_options) { 3499 (void)sctp_m_free(ip_pcb->inp_options); 3500 ip_pcb->inp_options = 0; 3501 } 3502 #ifdef INET6 3503 if (ip_pcb->inp_vflag & INP_IPV6) { 3504 ip6_freepcbopts(ip_pcb->in6p_outputopts); 3505 } 3506 #endif /* INET6 */ 3507 ip_pcb->inp_vflag = 0; 3508 /* free up authentication fields */ 3509 if (inp->sctp_ep.local_auth_chunks != NULL) 3510 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 3511 if (inp->sctp_ep.local_hmacs != NULL) 3512 sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 3513 3514 LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) { 3515 LIST_REMOVE(shared_key, next); 3516 sctp_free_sharedkey(shared_key); 3517 /* sa_ignore FREED_MEMORY */ 3518 } 3519 3520 /* 3521 * if we have an address list the following will free the list of 3522 * ifaddr's that are set into this ep. Again macro limitations here, 3523 * since the LIST_FOREACH could be a bad idea. 3524 */ 3525 LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) { 3526 sctp_remove_laddr(laddr); 3527 } 3528 3529 #ifdef SCTP_TRACK_FREED_ASOCS 3530 /* TEMP CODE */ 3531 LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_free_list, sctp_tcblist, nstcb) { 3532 LIST_REMOVE(stcb, sctp_tcblist); 3533 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); 3534 SCTP_DECR_ASOC_COUNT(); 3535 } 3536 /* *** END TEMP CODE *** */ 3537 #endif 3538 /* Now lets see about freeing the EP hash table. */ 3539 if (inp->sctp_tcbhash != NULL) { 3540 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); 3541 inp->sctp_tcbhash = NULL; 3542 } 3543 /* Now we must put the ep memory back into the zone pool */ 3544 crfree(inp->ip_inp.inp.inp_cred); 3545 INP_LOCK_DESTROY(&inp->ip_inp.inp); 3546 SCTP_INP_LOCK_DESTROY(inp); 3547 SCTP_INP_READ_LOCK_DESTROY(inp); 3548 SCTP_ASOC_CREATE_LOCK_DESTROY(inp); 3549 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); 3550 SCTP_DECR_EP_COUNT(); 3551 } 3552 3553 struct sctp_nets * 3554 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr) 3555 { 3556 struct sctp_nets *net; 3557 3558 /* locate the address */ 3559 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3560 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr)) 3561 return (net); 3562 } 3563 return (NULL); 3564 } 3565 3566 int 3567 sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id) 3568 { 3569 struct sctp_ifa *sctp_ifa; 3570 3571 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED); 3572 if (sctp_ifa) { 3573 return (1); 3574 } else { 3575 return (0); 3576 } 3577 } 3578 3579 /* 3580 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as 3581 * when a ASCONF arrives that adds it. It will also initialize all the cwnd 3582 * stats of stuff. 3583 */ 3584 int 3585 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr, 3586 struct sctp_nets **netp, uint16_t port, int set_scope, int from) 3587 { 3588 /* 3589 * The following is redundant to the same lines in the 3590 * sctp_aloc_assoc() but is needed since others call the add address 3591 * function 3592 */ 3593 struct sctp_nets *net, *netfirst; 3594 int addr_inscope; 3595 3596 SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ", 3597 from); 3598 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr); 3599 3600 netfirst = sctp_findnet(stcb, newaddr); 3601 if (netfirst) { 3602 /* 3603 * Lie and return ok, we don't want to make the association 3604 * go away for this behavior. It will happen in the TCP 3605 * model in a connected socket. It does not reach the hash 3606 * table until after the association is built so it can't be 3607 * found. Mark as reachable, since the initial creation will 3608 * have been cleared and the NOT_IN_ASSOC flag will have 3609 * been added... and we don't want to end up removing it 3610 * back out. 3611 */ 3612 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) { 3613 netfirst->dest_state = (SCTP_ADDR_REACHABLE | 3614 SCTP_ADDR_UNCONFIRMED); 3615 } else { 3616 netfirst->dest_state = SCTP_ADDR_REACHABLE; 3617 } 3618 3619 return (0); 3620 } 3621 addr_inscope = 1; 3622 switch (newaddr->sa_family) { 3623 #ifdef INET 3624 case AF_INET: 3625 { 3626 struct sockaddr_in *sin; 3627 3628 sin = (struct sockaddr_in *)newaddr; 3629 if (sin->sin_addr.s_addr == 0) { 3630 /* Invalid address */ 3631 return (-1); 3632 } 3633 /* zero out the zero area */ 3634 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); 3635 3636 /* assure len is set */ 3637 sin->sin_len = sizeof(struct sockaddr_in); 3638 if (set_scope) { 3639 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) { 3640 stcb->asoc.scope.ipv4_local_scope = 1; 3641 } 3642 } else { 3643 /* Validate the address is in scope */ 3644 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) && 3645 (stcb->asoc.scope.ipv4_local_scope == 0)) { 3646 addr_inscope = 0; 3647 } 3648 } 3649 break; 3650 } 3651 #endif 3652 #ifdef INET6 3653 case AF_INET6: 3654 { 3655 struct sockaddr_in6 *sin6; 3656 3657 sin6 = (struct sockaddr_in6 *)newaddr; 3658 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 3659 /* Invalid address */ 3660 return (-1); 3661 } 3662 /* assure len is set */ 3663 sin6->sin6_len = sizeof(struct sockaddr_in6); 3664 if (set_scope) { 3665 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) { 3666 stcb->asoc.scope.loopback_scope = 1; 3667 stcb->asoc.scope.local_scope = 0; 3668 stcb->asoc.scope.ipv4_local_scope = 1; 3669 stcb->asoc.scope.site_scope = 1; 3670 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 3671 /* 3672 * If the new destination is a 3673 * LINK_LOCAL we must have common 3674 * site scope. Don't set the local 3675 * scope since we may not share all 3676 * links, only loopback can do this. 3677 * Links on the local network would 3678 * also be on our private network 3679 * for v4 too. 3680 */ 3681 stcb->asoc.scope.ipv4_local_scope = 1; 3682 stcb->asoc.scope.site_scope = 1; 3683 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) { 3684 /* 3685 * If the new destination is 3686 * SITE_LOCAL then we must have site 3687 * scope in common. 3688 */ 3689 stcb->asoc.scope.site_scope = 1; 3690 } 3691 } else { 3692 /* Validate the address is in scope */ 3693 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) && 3694 (stcb->asoc.scope.loopback_scope == 0)) { 3695 addr_inscope = 0; 3696 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && 3697 (stcb->asoc.scope.local_scope == 0)) { 3698 addr_inscope = 0; 3699 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && 3700 (stcb->asoc.scope.site_scope == 0)) { 3701 addr_inscope = 0; 3702 } 3703 } 3704 break; 3705 } 3706 #endif 3707 default: 3708 /* not supported family type */ 3709 return (-1); 3710 } 3711 net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets); 3712 if (net == NULL) { 3713 return (-1); 3714 } 3715 SCTP_INCR_RADDR_COUNT(); 3716 memset(net, 0, sizeof(struct sctp_nets)); 3717 (void)SCTP_GETTIME_TIMEVAL(&net->start_time); 3718 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len); 3719 switch (newaddr->sa_family) { 3720 #ifdef INET 3721 case AF_INET: 3722 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport; 3723 break; 3724 #endif 3725 #ifdef INET6 3726 case AF_INET6: 3727 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport; 3728 break; 3729 #endif 3730 default: 3731 break; 3732 } 3733 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id); 3734 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) { 3735 stcb->asoc.scope.loopback_scope = 1; 3736 stcb->asoc.scope.ipv4_local_scope = 1; 3737 stcb->asoc.scope.local_scope = 0; 3738 stcb->asoc.scope.site_scope = 1; 3739 addr_inscope = 1; 3740 } 3741 net->failure_threshold = stcb->asoc.def_net_failure; 3742 net->pf_threshold = stcb->asoc.def_net_pf_threshold; 3743 if (addr_inscope == 0) { 3744 net->dest_state = (SCTP_ADDR_REACHABLE | 3745 SCTP_ADDR_OUT_OF_SCOPE); 3746 } else { 3747 if (from == SCTP_ADDR_IS_CONFIRMED) 3748 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */ 3749 net->dest_state = SCTP_ADDR_REACHABLE; 3750 else 3751 net->dest_state = SCTP_ADDR_REACHABLE | 3752 SCTP_ADDR_UNCONFIRMED; 3753 } 3754 /* 3755 * We set this to 0, the timer code knows that this means its an 3756 * initial value 3757 */ 3758 net->rto_needed = 1; 3759 net->RTO = 0; 3760 net->RTO_measured = 0; 3761 stcb->asoc.numnets++; 3762 net->ref_count = 1; 3763 net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1; 3764 net->port = port; 3765 net->dscp = stcb->asoc.default_dscp; 3766 #ifdef INET6 3767 net->flowlabel = stcb->asoc.default_flowlabel; 3768 #endif 3769 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) { 3770 net->dest_state |= SCTP_ADDR_NOHB; 3771 } else { 3772 net->dest_state &= ~SCTP_ADDR_NOHB; 3773 } 3774 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) { 3775 net->dest_state |= SCTP_ADDR_NO_PMTUD; 3776 } else { 3777 net->dest_state &= ~SCTP_ADDR_NO_PMTUD; 3778 } 3779 net->heart_beat_delay = stcb->asoc.heart_beat_delay; 3780 /* Init the timer structure */ 3781 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer); 3782 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer); 3783 SCTP_OS_TIMER_INIT(&net->hb_timer.timer); 3784 3785 /* Now generate a route for this guy */ 3786 #ifdef INET6 3787 /* KAME hack: embed scopeid */ 3788 if (newaddr->sa_family == AF_INET6) { 3789 struct sockaddr_in6 *sin6; 3790 3791 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; 3792 (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)); 3793 sin6->sin6_scope_id = 0; 3794 } 3795 #endif 3796 SCTP_RTALLOC((sctp_route_t *)&net->ro, 3797 stcb->asoc.vrf_id, 3798 stcb->sctp_ep->fibnum); 3799 3800 net->src_addr_selected = 0; 3801 if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) { 3802 /* Get source address */ 3803 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep, 3804 stcb, 3805 (sctp_route_t *)&net->ro, 3806 net, 3807 0, 3808 stcb->asoc.vrf_id); 3809 if (stcb->asoc.default_mtu > 0) { 3810 net->mtu = stcb->asoc.default_mtu; 3811 switch (net->ro._l_addr.sa.sa_family) { 3812 #ifdef INET 3813 case AF_INET: 3814 net->mtu += SCTP_MIN_V4_OVERHEAD; 3815 break; 3816 #endif 3817 #ifdef INET6 3818 case AF_INET6: 3819 net->mtu += SCTP_MIN_OVERHEAD; 3820 break; 3821 #endif 3822 default: 3823 break; 3824 } 3825 #if defined(INET) || defined(INET6) 3826 if (net->port) { 3827 net->mtu += (uint32_t)sizeof(struct udphdr); 3828 } 3829 #endif 3830 } else if (net->ro._s_addr != NULL) { 3831 uint32_t imtu, rmtu, hcmtu; 3832 3833 net->src_addr_selected = 1; 3834 /* Now get the interface MTU */ 3835 if (net->ro._s_addr->ifn_p != NULL) { 3836 /* 3837 * XXX: Should we here just use 3838 * net->ro._s_addr->ifn_p->ifn_mtu 3839 */ 3840 imtu = SCTP_GATHER_MTU_FROM_IFN_INFO(net->ro._s_addr->ifn_p->ifn_p, 3841 net->ro._s_addr->ifn_p->ifn_index); 3842 } else { 3843 imtu = 0; 3844 } 3845 rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_nh); 3846 hcmtu = sctp_hc_get_mtu(&net->ro._l_addr, stcb->sctp_ep->fibnum); 3847 net->mtu = sctp_min_mtu(hcmtu, rmtu, imtu); 3848 } 3849 } 3850 if (net->mtu == 0) { 3851 if (stcb->asoc.default_mtu > 0) { 3852 net->mtu = stcb->asoc.default_mtu; 3853 switch (net->ro._l_addr.sa.sa_family) { 3854 #ifdef INET 3855 case AF_INET: 3856 net->mtu += SCTP_MIN_V4_OVERHEAD; 3857 break; 3858 #endif 3859 #ifdef INET6 3860 case AF_INET6: 3861 net->mtu += SCTP_MIN_OVERHEAD; 3862 break; 3863 #endif 3864 default: 3865 break; 3866 } 3867 #if defined(INET) || defined(INET6) 3868 if (net->port) { 3869 net->mtu += (uint32_t)sizeof(struct udphdr); 3870 } 3871 #endif 3872 } else { 3873 switch (newaddr->sa_family) { 3874 #ifdef INET 3875 case AF_INET: 3876 net->mtu = SCTP_DEFAULT_MTU; 3877 break; 3878 #endif 3879 #ifdef INET6 3880 case AF_INET6: 3881 net->mtu = 1280; 3882 break; 3883 #endif 3884 default: 3885 break; 3886 } 3887 } 3888 } 3889 #if defined(INET) || defined(INET6) 3890 if (net->port) { 3891 net->mtu -= (uint32_t)sizeof(struct udphdr); 3892 } 3893 #endif 3894 if (from == SCTP_ALLOC_ASOC) { 3895 stcb->asoc.smallest_mtu = net->mtu; 3896 } 3897 if (stcb->asoc.smallest_mtu > net->mtu) { 3898 sctp_pathmtu_adjustment(stcb, net->mtu, true); 3899 } 3900 #ifdef INET6 3901 if (newaddr->sa_family == AF_INET6) { 3902 struct sockaddr_in6 *sin6; 3903 3904 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; 3905 (void)sa6_recoverscope(sin6); 3906 } 3907 #endif 3908 3909 /* JRS - Use the congestion control given in the CC module */ 3910 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) 3911 (*stcb->asoc.cc_functions.sctp_set_initial_cc_param) (stcb, net); 3912 3913 /* 3914 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning 3915 * of assoc (2005/06/27, iyengar@cis.udel.edu) 3916 */ 3917 net->find_pseudo_cumack = 1; 3918 net->find_rtx_pseudo_cumack = 1; 3919 /* Choose an initial flowid. */ 3920 net->flowid = stcb->asoc.my_vtag ^ 3921 ntohs(stcb->rport) ^ 3922 ntohs(stcb->sctp_ep->sctp_lport); 3923 net->flowtype = M_HASHTYPE_OPAQUE_HASH; 3924 if (netp) { 3925 *netp = net; 3926 } 3927 netfirst = TAILQ_FIRST(&stcb->asoc.nets); 3928 if (net->ro.ro_nh == NULL) { 3929 /* Since we have no route put it at the back */ 3930 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next); 3931 } else if (netfirst == NULL) { 3932 /* We are the first one in the pool. */ 3933 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); 3934 } else if (netfirst->ro.ro_nh == NULL) { 3935 /* 3936 * First one has NO route. Place this one ahead of the first 3937 * one. 3938 */ 3939 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); 3940 } else if (net->ro.ro_nh->nh_ifp != netfirst->ro.ro_nh->nh_ifp) { 3941 /* 3942 * This one has a different interface than the one at the 3943 * top of the list. Place it ahead. 3944 */ 3945 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); 3946 } else { 3947 /* 3948 * Ok we have the same interface as the first one. Move 3949 * forward until we find either a) one with a NULL route... 3950 * insert ahead of that b) one with a different ifp.. insert 3951 * after that. c) end of the list.. insert at the tail. 3952 */ 3953 struct sctp_nets *netlook; 3954 3955 do { 3956 netlook = TAILQ_NEXT(netfirst, sctp_next); 3957 if (netlook == NULL) { 3958 /* End of the list */ 3959 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next); 3960 break; 3961 } else if (netlook->ro.ro_nh == NULL) { 3962 /* next one has NO route */ 3963 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next); 3964 break; 3965 } else if (netlook->ro.ro_nh->nh_ifp != net->ro.ro_nh->nh_ifp) { 3966 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook, 3967 net, sctp_next); 3968 break; 3969 } 3970 /* Shift forward */ 3971 netfirst = netlook; 3972 } while (netlook != NULL); 3973 } 3974 3975 /* got to have a primary set */ 3976 if (stcb->asoc.primary_destination == 0) { 3977 stcb->asoc.primary_destination = net; 3978 } else if ((stcb->asoc.primary_destination->ro.ro_nh == NULL) && 3979 (net->ro.ro_nh) && 3980 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) { 3981 /* No route to current primary adopt new primary */ 3982 stcb->asoc.primary_destination = net; 3983 } 3984 /* Validate primary is first */ 3985 net = TAILQ_FIRST(&stcb->asoc.nets); 3986 if ((net != stcb->asoc.primary_destination) && 3987 (stcb->asoc.primary_destination)) { 3988 /* 3989 * first one on the list is NOT the primary sctp_cmpaddr() 3990 * is much more efficient if the primary is the first on the 3991 * list, make it so. 3992 */ 3993 TAILQ_REMOVE(&stcb->asoc.nets, 3994 stcb->asoc.primary_destination, sctp_next); 3995 TAILQ_INSERT_HEAD(&stcb->asoc.nets, 3996 stcb->asoc.primary_destination, sctp_next); 3997 } 3998 return (0); 3999 } 4000 4001 static uint32_t 4002 sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb) 4003 { 4004 uint32_t id; 4005 struct sctpasochead *head; 4006 struct sctp_tcb *lstcb; 4007 4008 try_again: 4009 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 4010 /* TSNH */ 4011 return (0); 4012 } 4013 /* 4014 * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC, 4015 * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC. 4016 */ 4017 if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) { 4018 inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1; 4019 } 4020 id = inp->sctp_associd_counter; 4021 inp->sctp_associd_counter++; 4022 lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0); 4023 if (lstcb) { 4024 goto try_again; 4025 } 4026 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)]; 4027 LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash); 4028 stcb->asoc.in_asocid_hash = 1; 4029 return (id); 4030 } 4031 4032 /* 4033 * allocate an association and add it to the endpoint. The caller must be 4034 * careful to add all additional addresses once they are know right away or 4035 * else the assoc will be may experience a blackout scenario. 4036 */ 4037 static struct sctp_tcb * 4038 sctp_aloc_assoc_locked(struct sctp_inpcb *inp, struct sockaddr *firstaddr, 4039 int *error, uint32_t override_tag, uint32_t initial_tsn, 4040 uint32_t vrf_id, uint16_t o_streams, uint16_t port, 4041 struct thread *p, 4042 int initialize_auth_params) 4043 { 4044 /* note the p argument is only valid in unbound sockets */ 4045 4046 struct sctp_tcb *stcb; 4047 struct sctp_association *asoc; 4048 struct sctpasochead *head; 4049 uint16_t rport; 4050 int err; 4051 4052 SCTP_INP_INFO_WLOCK_ASSERT(); 4053 SCTP_INP_WLOCK_ASSERT(inp); 4054 4055 /* 4056 * Assumption made here: Caller has done a 4057 * sctp_findassociation_ep_addr(ep, addr's); to make sure the 4058 * address does not exist already. 4059 */ 4060 if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) { 4061 /* Hit max assoc, sorry no more */ 4062 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); 4063 *error = ENOBUFS; 4064 return (NULL); 4065 } 4066 if (firstaddr == NULL) { 4067 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 4068 *error = EINVAL; 4069 return (NULL); 4070 } 4071 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 4072 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 4073 *error = EINVAL; 4074 return (NULL); 4075 } 4076 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) && 4077 ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) || 4078 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) { 4079 /* 4080 * If its in the TCP pool, its NOT allowed to create an 4081 * association. The parent listener needs to call 4082 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled 4083 * off, or connected one does this.. its an error. 4084 */ 4085 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 4086 *error = EINVAL; 4087 return (NULL); 4088 } 4089 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 4090 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) { 4091 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) || 4092 (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) { 4093 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 4094 *error = EINVAL; 4095 return (NULL); 4096 } 4097 } 4098 SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:"); 4099 #ifdef SCTP_DEBUG 4100 if (firstaddr) { 4101 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr); 4102 switch (firstaddr->sa_family) { 4103 #ifdef INET 4104 case AF_INET: 4105 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", 4106 ntohs(((struct sockaddr_in *)firstaddr)->sin_port)); 4107 break; 4108 #endif 4109 #ifdef INET6 4110 case AF_INET6: 4111 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", 4112 ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port)); 4113 break; 4114 #endif 4115 default: 4116 break; 4117 } 4118 } else { 4119 SCTPDBG(SCTP_DEBUG_PCB3, "None\n"); 4120 } 4121 #endif /* SCTP_DEBUG */ 4122 switch (firstaddr->sa_family) { 4123 #ifdef INET 4124 case AF_INET: 4125 { 4126 struct sockaddr_in *sin; 4127 4128 sin = (struct sockaddr_in *)firstaddr; 4129 if ((ntohs(sin->sin_port) == 0) || 4130 (sin->sin_addr.s_addr == INADDR_ANY) || 4131 (sin->sin_addr.s_addr == INADDR_BROADCAST) || 4132 IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) || 4133 ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && 4134 (SCTP_IPV6_V6ONLY(inp) != 0))) { 4135 /* Invalid address */ 4136 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 4137 *error = EINVAL; 4138 return (NULL); 4139 } 4140 rport = sin->sin_port; 4141 break; 4142 } 4143 #endif 4144 #ifdef INET6 4145 case AF_INET6: 4146 { 4147 struct sockaddr_in6 *sin6; 4148 4149 sin6 = (struct sockaddr_in6 *)firstaddr; 4150 if ((ntohs(sin6->sin6_port) == 0) || 4151 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 4152 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) || 4153 ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)) { 4154 /* Invalid address */ 4155 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 4156 *error = EINVAL; 4157 return (NULL); 4158 } 4159 rport = sin6->sin6_port; 4160 break; 4161 } 4162 #endif 4163 default: 4164 /* not supported family type */ 4165 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 4166 *error = EINVAL; 4167 return (NULL); 4168 } 4169 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) { 4170 /* 4171 * If you have not performed a bind, then we need to do the 4172 * ephemeral bind for you. 4173 */ 4174 if ((err = sctp_inpcb_bind_locked(inp, NULL, NULL, p))) { 4175 /* bind error, probably perm */ 4176 *error = err; 4177 return (NULL); 4178 } 4179 } 4180 stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb); 4181 if (stcb == NULL) { 4182 /* out of memory? */ 4183 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); 4184 *error = ENOMEM; 4185 return (NULL); 4186 } 4187 SCTP_INCR_ASOC_COUNT(); 4188 4189 memset(stcb, 0, sizeof(*stcb)); 4190 asoc = &stcb->asoc; 4191 4192 SCTP_TCB_LOCK_INIT(stcb); 4193 stcb->rport = rport; 4194 /* setup back pointer's */ 4195 stcb->sctp_ep = inp; 4196 stcb->sctp_socket = inp->sctp_socket; 4197 if ((err = sctp_init_asoc(inp, stcb, override_tag, initial_tsn, vrf_id, o_streams))) { 4198 /* failed */ 4199 SCTP_TCB_LOCK_DESTROY(stcb); 4200 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); 4201 SCTP_DECR_ASOC_COUNT(); 4202 *error = err; 4203 return (NULL); 4204 } 4205 SCTP_TCB_LOCK(stcb); 4206 4207 asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb); 4208 /* now that my_vtag is set, add it to the hash */ 4209 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; 4210 /* put it in the bucket in the vtag hash of assoc's for the system */ 4211 LIST_INSERT_HEAD(head, stcb, sctp_asocs); 4212 4213 if (sctp_add_remote_addr(stcb, firstaddr, NULL, port, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC)) { 4214 /* failure.. memory error? */ 4215 if (asoc->strmout) { 4216 SCTP_FREE(asoc->strmout, SCTP_M_STRMO); 4217 asoc->strmout = NULL; 4218 } 4219 if (asoc->mapping_array) { 4220 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); 4221 asoc->mapping_array = NULL; 4222 } 4223 if (asoc->nr_mapping_array) { 4224 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP); 4225 asoc->nr_mapping_array = NULL; 4226 } 4227 SCTP_DECR_ASOC_COUNT(); 4228 SCTP_TCB_UNLOCK(stcb); 4229 SCTP_TCB_LOCK_DESTROY(stcb); 4230 LIST_REMOVE(stcb, sctp_asocs); 4231 LIST_REMOVE(stcb, sctp_tcbasocidhash); 4232 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); 4233 SCTP_INP_WUNLOCK(inp); 4234 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); 4235 *error = ENOBUFS; 4236 return (NULL); 4237 } 4238 /* Init all the timers */ 4239 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer); 4240 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer); 4241 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer); 4242 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer); 4243 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer); 4244 SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer); 4245 4246 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist); 4247 /* now file the port under the hash as well */ 4248 if (inp->sctp_tcbhash != NULL) { 4249 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport, 4250 inp->sctp_hashmark)]; 4251 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash); 4252 } 4253 if (initialize_auth_params == SCTP_INITIALIZE_AUTH_PARAMS) { 4254 sctp_initialize_auth_params(inp, stcb); 4255 } 4256 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb); 4257 return (stcb); 4258 } 4259 4260 struct sctp_tcb * 4261 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr, 4262 int *error, uint32_t override_tag, uint32_t initial_tsn, 4263 uint32_t vrf_id, uint16_t o_streams, uint16_t port, 4264 struct thread *p, 4265 int initialize_auth_params) 4266 { 4267 struct sctp_tcb *stcb; 4268 4269 SCTP_INP_INFO_WLOCK(); 4270 SCTP_INP_WLOCK(inp); 4271 stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag, 4272 initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params); 4273 SCTP_INP_INFO_WUNLOCK(); 4274 SCTP_INP_WUNLOCK(inp); 4275 return (stcb); 4276 } 4277 4278 struct sctp_tcb * 4279 sctp_aloc_assoc_connected(struct sctp_inpcb *inp, struct sockaddr *firstaddr, 4280 int *error, uint32_t override_tag, uint32_t initial_tsn, 4281 uint32_t vrf_id, uint16_t o_streams, uint16_t port, 4282 struct thread *p, 4283 int initialize_auth_params) 4284 { 4285 struct sctp_tcb *stcb; 4286 4287 SCTP_INP_INFO_WLOCK(); 4288 SCTP_INP_WLOCK(inp); 4289 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 4290 SCTP_IS_LISTENING(inp)) { 4291 SCTP_INP_INFO_WUNLOCK(); 4292 SCTP_INP_WUNLOCK(inp); 4293 *error = EINVAL; 4294 return (NULL); 4295 } 4296 stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag, 4297 initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params); 4298 SCTP_INP_INFO_WUNLOCK(); 4299 if (stcb != NULL && (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) { 4300 inp->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 4301 soisconnecting(inp->sctp_socket); 4302 } 4303 SCTP_INP_WUNLOCK(inp); 4304 return (stcb); 4305 } 4306 4307 void 4308 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net) 4309 { 4310 struct sctp_inpcb *inp; 4311 struct sctp_association *asoc; 4312 4313 inp = stcb->sctp_ep; 4314 asoc = &stcb->asoc; 4315 asoc->numnets--; 4316 TAILQ_REMOVE(&asoc->nets, net, sctp_next); 4317 if (net == asoc->primary_destination) { 4318 /* Reset primary */ 4319 struct sctp_nets *lnet; 4320 4321 lnet = TAILQ_FIRST(&asoc->nets); 4322 /* 4323 * Mobility adaptation Ideally, if deleted destination is 4324 * the primary, it becomes a fast retransmission trigger by 4325 * the subsequent SET PRIMARY. (by micchie) 4326 */ 4327 if (sctp_is_mobility_feature_on(stcb->sctp_ep, 4328 SCTP_MOBILITY_BASE) || 4329 sctp_is_mobility_feature_on(stcb->sctp_ep, 4330 SCTP_MOBILITY_FASTHANDOFF)) { 4331 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n"); 4332 if (asoc->deleted_primary != NULL) { 4333 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n"); 4334 goto out; 4335 } 4336 asoc->deleted_primary = net; 4337 atomic_add_int(&net->ref_count, 1); 4338 memset(&net->lastsa, 0, sizeof(net->lastsa)); 4339 memset(&net->lastsv, 0, sizeof(net->lastsv)); 4340 sctp_mobility_feature_on(stcb->sctp_ep, 4341 SCTP_MOBILITY_PRIM_DELETED); 4342 sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED, 4343 stcb->sctp_ep, stcb, NULL); 4344 } 4345 out: 4346 /* Try to find a confirmed primary */ 4347 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0); 4348 } 4349 if (net == asoc->last_data_chunk_from) { 4350 /* Reset primary */ 4351 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets); 4352 } 4353 if (net == asoc->last_control_chunk_from) { 4354 /* Clear net */ 4355 asoc->last_control_chunk_from = NULL; 4356 } 4357 if (net == asoc->last_net_cmt_send_started) { 4358 /* Clear net */ 4359 asoc->last_net_cmt_send_started = NULL; 4360 } 4361 if (net == stcb->asoc.alternate) { 4362 sctp_free_remote_addr(stcb->asoc.alternate); 4363 stcb->asoc.alternate = NULL; 4364 } 4365 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net, 4366 SCTP_FROM_SCTP_PCB + SCTP_LOC_9); 4367 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, 4368 SCTP_FROM_SCTP_PCB + SCTP_LOC_10); 4369 net->dest_state |= SCTP_ADDR_BEING_DELETED; 4370 sctp_free_remote_addr(net); 4371 } 4372 4373 /* 4374 * remove a remote endpoint address from an association, it will fail if the 4375 * address does not exist. 4376 */ 4377 int 4378 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr) 4379 { 4380 /* 4381 * Here we need to remove a remote address. This is quite simple, we 4382 * first find it in the list of address for the association 4383 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE 4384 * on that item. Note we do not allow it to be removed if there are 4385 * no other addresses. 4386 */ 4387 struct sctp_association *asoc; 4388 struct sctp_nets *net, *nnet; 4389 4390 asoc = &stcb->asoc; 4391 4392 /* locate the address */ 4393 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) { 4394 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) { 4395 continue; 4396 } 4397 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr, 4398 remaddr)) { 4399 /* we found the guy */ 4400 if (asoc->numnets < 2) { 4401 /* Must have at LEAST two remote addresses */ 4402 return (-1); 4403 } else { 4404 sctp_remove_net(stcb, net); 4405 return (0); 4406 } 4407 } 4408 } 4409 /* not found. */ 4410 return (-2); 4411 } 4412 4413 static bool 4414 sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport, time_t now) 4415 { 4416 struct sctpvtaghead *chain; 4417 struct sctp_tagblock *twait_block; 4418 int i; 4419 4420 SCTP_INP_INFO_LOCK_ASSERT(); 4421 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; 4422 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { 4423 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { 4424 if ((twait_block->vtag_block[i].tv_sec_at_expire >= now) && 4425 (twait_block->vtag_block[i].v_tag == tag) && 4426 (twait_block->vtag_block[i].lport == lport) && 4427 (twait_block->vtag_block[i].rport == rport)) { 4428 return (true); 4429 } 4430 } 4431 } 4432 return (false); 4433 } 4434 4435 static void 4436 sctp_set_vtag_block(struct sctp_timewait *vtag_block, time_t time, 4437 uint32_t tag, uint16_t lport, uint16_t rport) 4438 { 4439 vtag_block->tv_sec_at_expire = time; 4440 vtag_block->v_tag = tag; 4441 vtag_block->lport = lport; 4442 vtag_block->rport = rport; 4443 } 4444 4445 static void 4446 sctp_add_vtag_to_timewait(uint32_t tag, uint16_t lport, uint16_t rport) 4447 { 4448 struct sctpvtaghead *chain; 4449 struct sctp_tagblock *twait_block; 4450 struct timeval now; 4451 time_t time; 4452 int i; 4453 bool set; 4454 4455 SCTP_INP_INFO_WLOCK_ASSERT(); 4456 (void)SCTP_GETTIME_TIMEVAL(&now); 4457 time = now.tv_sec + SCTP_BASE_SYSCTL(sctp_vtag_time_wait); 4458 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; 4459 set = false; 4460 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { 4461 /* Block(s) present, lets find space, and expire on the fly */ 4462 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { 4463 if ((twait_block->vtag_block[i].v_tag == 0) && !set) { 4464 sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport); 4465 set = true; 4466 continue; 4467 } 4468 if ((twait_block->vtag_block[i].v_tag != 0) && 4469 (twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) { 4470 if (set) { 4471 /* Audit expires this guy */ 4472 sctp_set_vtag_block(twait_block->vtag_block + i, 0, 0, 0, 0); 4473 } else { 4474 /* Reuse it for the new tag */ 4475 sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport); 4476 set = true; 4477 } 4478 } 4479 } 4480 if (set) { 4481 /* 4482 * We only do up to the block where we can place our 4483 * tag for audits 4484 */ 4485 break; 4486 } 4487 } 4488 /* Need to add a new block to chain */ 4489 if (!set) { 4490 SCTP_MALLOC(twait_block, struct sctp_tagblock *, 4491 sizeof(struct sctp_tagblock), SCTP_M_TIMW); 4492 if (twait_block == NULL) { 4493 return; 4494 } 4495 memset(twait_block, 0, sizeof(struct sctp_tagblock)); 4496 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock); 4497 sctp_set_vtag_block(twait_block->vtag_block, time, tag, lport, rport); 4498 } 4499 } 4500 4501 void 4502 sctp_clean_up_stream(struct sctp_tcb *stcb, struct sctp_readhead *rh) 4503 { 4504 struct sctp_tmit_chunk *chk, *nchk; 4505 struct sctp_queued_to_read *control, *ncontrol; 4506 4507 TAILQ_FOREACH_SAFE(control, rh, next_instrm, ncontrol) { 4508 TAILQ_REMOVE(rh, control, next_instrm); 4509 control->on_strm_q = 0; 4510 if (control->on_read_q == 0) { 4511 sctp_free_remote_addr(control->whoFrom); 4512 if (control->data) { 4513 sctp_m_freem(control->data); 4514 control->data = NULL; 4515 } 4516 } 4517 /* Reassembly free? */ 4518 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) { 4519 TAILQ_REMOVE(&control->reasm, chk, sctp_next); 4520 if (chk->data) { 4521 sctp_m_freem(chk->data); 4522 chk->data = NULL; 4523 } 4524 if (chk->holds_key_ref) 4525 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); 4526 sctp_free_remote_addr(chk->whoTo); 4527 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); 4528 SCTP_DECR_CHK_COUNT(); 4529 /* sa_ignore FREED_MEMORY */ 4530 } 4531 /* 4532 * We don't free the address here since all the net's were 4533 * freed above. 4534 */ 4535 if (control->on_read_q == 0) { 4536 sctp_free_a_readq(stcb, control); 4537 } 4538 } 4539 } 4540 4541 /*- 4542 * Free the association after un-hashing the remote port. This 4543 * function ALWAYS returns holding NO LOCK on the stcb. It DOES 4544 * expect that the input to this function IS a locked TCB. 4545 * It will return 0, if it did NOT destroy the association (instead 4546 * it unlocks it. It will return NON-zero if it either destroyed the 4547 * association OR the association is already destroyed. 4548 */ 4549 int 4550 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location) 4551 { 4552 int i; 4553 struct sctp_association *asoc; 4554 struct sctp_nets *net, *nnet; 4555 struct sctp_laddr *laddr, *naddr; 4556 struct sctp_tmit_chunk *chk, *nchk; 4557 struct sctp_asconf_addr *aparam, *naparam; 4558 struct sctp_asconf_ack *aack, *naack; 4559 struct sctp_stream_reset_list *strrst, *nstrrst; 4560 struct sctp_queued_to_read *sq, *nsq; 4561 struct sctp_stream_queue_pending *sp, *nsp; 4562 sctp_sharedkey_t *shared_key, *nshared_key; 4563 struct socket *so; 4564 4565 /* first, lets purge the entry from the hash table. */ 4566 SCTP_TCB_LOCK_ASSERT(stcb); 4567 4568 #ifdef SCTP_LOG_CLOSING 4569 sctp_log_closing(inp, stcb, 6); 4570 #endif 4571 if (stcb->asoc.state == 0) { 4572 #ifdef SCTP_LOG_CLOSING 4573 sctp_log_closing(inp, NULL, 7); 4574 #endif 4575 /* there is no asoc, really TSNH :-0 */ 4576 return (1); 4577 } 4578 if (stcb->asoc.alternate) { 4579 sctp_free_remote_addr(stcb->asoc.alternate); 4580 stcb->asoc.alternate = NULL; 4581 } 4582 /* TEMP CODE */ 4583 if (stcb->freed_from_where == 0) { 4584 /* Only record the first place free happened from */ 4585 stcb->freed_from_where = from_location; 4586 } 4587 /* TEMP CODE */ 4588 4589 asoc = &stcb->asoc; 4590 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 4591 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) 4592 /* nothing around */ 4593 so = NULL; 4594 else 4595 so = inp->sctp_socket; 4596 4597 /* 4598 * We used timer based freeing if a reader or writer is in the way. 4599 * So we first check if we are actually being called from a timer, 4600 * if so we abort early if a reader or writer is still in the way. 4601 */ 4602 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) && 4603 (from_inpcbfree == SCTP_NORMAL_PROC)) { 4604 /* 4605 * is it the timer driving us? if so are the reader/writers 4606 * gone? 4607 */ 4608 if (stcb->asoc.refcnt) { 4609 /* nope, reader or writer in the way */ 4610 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); 4611 /* no asoc destroyed */ 4612 SCTP_TCB_UNLOCK(stcb); 4613 #ifdef SCTP_LOG_CLOSING 4614 sctp_log_closing(inp, stcb, 8); 4615 #endif 4616 return (0); 4617 } 4618 } 4619 /* Now clean up any other timers */ 4620 sctp_stop_association_timers(stcb, false); 4621 /* Now the read queue needs to be cleaned up (only once) */ 4622 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) { 4623 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_ABOUT_TO_BE_FREED); 4624 SCTP_INP_READ_LOCK(inp); 4625 TAILQ_FOREACH(sq, &inp->read_queue, next) { 4626 if (sq->stcb == stcb) { 4627 sq->do_not_ref_stcb = 1; 4628 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn; 4629 /* 4630 * If there is no end, there never will be 4631 * now. 4632 */ 4633 if (sq->end_added == 0) { 4634 /* Held for PD-API, clear that. */ 4635 sq->pdapi_aborted = 1; 4636 sq->held_length = 0; 4637 if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) { 4638 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION, 4639 stcb, 4640 SCTP_PARTIAL_DELIVERY_ABORTED, 4641 (void *)sq, 4642 SCTP_SO_LOCKED); 4643 } 4644 /* Add an end to wake them */ 4645 sq->end_added = 1; 4646 } 4647 } 4648 } 4649 SCTP_INP_READ_UNLOCK(inp); 4650 if (stcb->block_entry) { 4651 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET); 4652 stcb->block_entry->error = ECONNRESET; 4653 stcb->block_entry = NULL; 4654 } 4655 } 4656 if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) { 4657 /* 4658 * Someone holds a reference OR the socket is unaccepted 4659 * yet. 4660 */ 4661 if ((stcb->asoc.refcnt) || 4662 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 4663 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) { 4664 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE); 4665 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); 4666 } 4667 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 4668 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) 4669 /* nothing around */ 4670 so = NULL; 4671 if (so) { 4672 /* Wake any reader/writers */ 4673 sctp_sorwakeup(inp, so); 4674 sctp_sowwakeup(inp, so); 4675 } 4676 SCTP_TCB_UNLOCK(stcb); 4677 4678 #ifdef SCTP_LOG_CLOSING 4679 sctp_log_closing(inp, stcb, 9); 4680 #endif 4681 /* no asoc destroyed */ 4682 return (0); 4683 } 4684 #ifdef SCTP_LOG_CLOSING 4685 sctp_log_closing(inp, stcb, 10); 4686 #endif 4687 /* 4688 * When I reach here, no others want to kill the assoc yet.. and I 4689 * own the lock. Now its possible an abort comes in when I do the 4690 * lock exchange below to grab all the locks to do the final take 4691 * out. to prevent this we increment the count, which will start a 4692 * timer and blow out above thus assuring us that we hold exclusive 4693 * killing of the asoc. Note that after getting back the TCB lock we 4694 * will go ahead and increment the counter back up and stop any 4695 * timer a passing stranger may have started :-S 4696 */ 4697 if (from_inpcbfree == SCTP_NORMAL_PROC) { 4698 atomic_add_int(&stcb->asoc.refcnt, 1); 4699 4700 SCTP_TCB_UNLOCK(stcb); 4701 SCTP_INP_INFO_WLOCK(); 4702 SCTP_INP_WLOCK(inp); 4703 SCTP_TCB_LOCK(stcb); 4704 } 4705 /* Double check the GONE flag */ 4706 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 4707 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) 4708 /* nothing around */ 4709 so = NULL; 4710 4711 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 4712 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 4713 /* 4714 * For TCP type we need special handling when we are 4715 * connected. We also include the peel'ed off ones to. 4716 */ 4717 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 4718 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED; 4719 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED; 4720 if (so) { 4721 SOCKBUF_LOCK(&so->so_rcv); 4722 so->so_state &= ~(SS_ISCONNECTING | 4723 SS_ISDISCONNECTING | 4724 SS_ISCONNECTED); 4725 so->so_state |= SS_ISDISCONNECTED; 4726 socantrcvmore_locked(so); 4727 socantsendmore(so); 4728 sctp_sowwakeup(inp, so); 4729 sctp_sorwakeup(inp, so); 4730 SCTP_SOWAKEUP(so); 4731 } 4732 } 4733 } 4734 4735 /* 4736 * Make it invalid too, that way if its about to run it will abort 4737 * and return. 4738 */ 4739 /* re-increment the lock */ 4740 if (from_inpcbfree == SCTP_NORMAL_PROC) { 4741 atomic_subtract_int(&stcb->asoc.refcnt, 1); 4742 } 4743 if (stcb->asoc.refcnt) { 4744 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE); 4745 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); 4746 if (from_inpcbfree == SCTP_NORMAL_PROC) { 4747 SCTP_INP_INFO_WUNLOCK(); 4748 SCTP_INP_WUNLOCK(inp); 4749 } 4750 SCTP_TCB_UNLOCK(stcb); 4751 return (0); 4752 } 4753 asoc->state = 0; 4754 if (inp->sctp_tcbhash) { 4755 LIST_REMOVE(stcb, sctp_tcbhash); 4756 } 4757 if (stcb->asoc.in_asocid_hash) { 4758 LIST_REMOVE(stcb, sctp_tcbasocidhash); 4759 } 4760 if (inp->sctp_socket == NULL) { 4761 stcb->sctp_socket = NULL; 4762 } 4763 /* Now lets remove it from the list of ALL associations in the EP */ 4764 LIST_REMOVE(stcb, sctp_tcblist); 4765 if (from_inpcbfree == SCTP_NORMAL_PROC) { 4766 SCTP_INP_INCR_REF(inp); 4767 SCTP_INP_WUNLOCK(inp); 4768 } 4769 /* pull from vtag hash */ 4770 LIST_REMOVE(stcb, sctp_asocs); 4771 sctp_add_vtag_to_timewait(asoc->my_vtag, inp->sctp_lport, stcb->rport); 4772 4773 /* 4774 * Now restop the timers to be sure this is paranoia at is finest! 4775 */ 4776 sctp_stop_association_timers(stcb, true); 4777 4778 /* 4779 * The chunk lists and such SHOULD be empty but we check them just 4780 * in case. 4781 */ 4782 /* anything on the wheel needs to be removed */ 4783 for (i = 0; i < asoc->streamoutcnt; i++) { 4784 struct sctp_stream_out *outs; 4785 4786 outs = &asoc->strmout[i]; 4787 /* now clean up any chunks here */ 4788 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { 4789 atomic_subtract_int(&asoc->stream_queue_cnt, 1); 4790 TAILQ_REMOVE(&outs->outqueue, sp, next); 4791 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp); 4792 sctp_free_spbufspace(stcb, asoc, sp); 4793 if (sp->data) { 4794 if (so) { 4795 /* Still an open socket - report */ 4796 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb, 4797 0, (void *)sp, SCTP_SO_LOCKED); 4798 } 4799 if (sp->data) { 4800 sctp_m_freem(sp->data); 4801 sp->data = NULL; 4802 sp->tail_mbuf = NULL; 4803 sp->length = 0; 4804 } 4805 } 4806 if (sp->net) { 4807 sctp_free_remote_addr(sp->net); 4808 sp->net = NULL; 4809 } 4810 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED); 4811 } 4812 } 4813 /* sa_ignore FREED_MEMORY */ 4814 TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) { 4815 TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp); 4816 SCTP_FREE(strrst, SCTP_M_STRESET); 4817 } 4818 TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) { 4819 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next); 4820 if (sq->data) { 4821 sctp_m_freem(sq->data); 4822 sq->data = NULL; 4823 } 4824 sctp_free_remote_addr(sq->whoFrom); 4825 sq->whoFrom = NULL; 4826 sq->stcb = NULL; 4827 /* Free the ctl entry */ 4828 sctp_free_a_readq(stcb, sq); 4829 /* sa_ignore FREED_MEMORY */ 4830 } 4831 TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) { 4832 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next); 4833 if (chk->data) { 4834 sctp_m_freem(chk->data); 4835 chk->data = NULL; 4836 } 4837 if (chk->holds_key_ref) 4838 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); 4839 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); 4840 SCTP_DECR_CHK_COUNT(); 4841 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); 4842 asoc->free_chunk_cnt--; 4843 /* sa_ignore FREED_MEMORY */ 4844 } 4845 /* pending send queue SHOULD be empty */ 4846 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { 4847 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) { 4848 asoc->strmout[chk->rec.data.sid].chunks_on_queues--; 4849 #ifdef INVARIANTS 4850 } else { 4851 panic("No chunks on the queues for sid %u.", chk->rec.data.sid); 4852 #endif 4853 } 4854 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); 4855 if (chk->data) { 4856 if (so) { 4857 /* Still a socket? */ 4858 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 4859 0, chk, SCTP_SO_LOCKED); 4860 } 4861 if (chk->data) { 4862 sctp_m_freem(chk->data); 4863 chk->data = NULL; 4864 } 4865 } 4866 if (chk->holds_key_ref) 4867 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); 4868 if (chk->whoTo) { 4869 sctp_free_remote_addr(chk->whoTo); 4870 chk->whoTo = NULL; 4871 } 4872 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); 4873 SCTP_DECR_CHK_COUNT(); 4874 /* sa_ignore FREED_MEMORY */ 4875 } 4876 /* sent queue SHOULD be empty */ 4877 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) { 4878 if (chk->sent != SCTP_DATAGRAM_NR_ACKED) { 4879 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) { 4880 asoc->strmout[chk->rec.data.sid].chunks_on_queues--; 4881 #ifdef INVARIANTS 4882 } else { 4883 panic("No chunks on the queues for sid %u.", chk->rec.data.sid); 4884 #endif 4885 } 4886 } 4887 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next); 4888 if (chk->data) { 4889 if (so) { 4890 /* Still a socket? */ 4891 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 4892 0, chk, SCTP_SO_LOCKED); 4893 } 4894 if (chk->data) { 4895 sctp_m_freem(chk->data); 4896 chk->data = NULL; 4897 } 4898 } 4899 if (chk->holds_key_ref) 4900 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); 4901 sctp_free_remote_addr(chk->whoTo); 4902 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); 4903 SCTP_DECR_CHK_COUNT(); 4904 /* sa_ignore FREED_MEMORY */ 4905 } 4906 #ifdef INVARIANTS 4907 for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 4908 if (stcb->asoc.strmout[i].chunks_on_queues > 0) { 4909 panic("%u chunks left for stream %u.", stcb->asoc.strmout[i].chunks_on_queues, i); 4910 } 4911 } 4912 #endif 4913 /* control queue MAY not be empty */ 4914 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { 4915 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); 4916 if (chk->data) { 4917 sctp_m_freem(chk->data); 4918 chk->data = NULL; 4919 } 4920 if (chk->holds_key_ref) 4921 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); 4922 sctp_free_remote_addr(chk->whoTo); 4923 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); 4924 SCTP_DECR_CHK_COUNT(); 4925 /* sa_ignore FREED_MEMORY */ 4926 } 4927 /* ASCONF queue MAY not be empty */ 4928 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) { 4929 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next); 4930 if (chk->data) { 4931 sctp_m_freem(chk->data); 4932 chk->data = NULL; 4933 } 4934 if (chk->holds_key_ref) 4935 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); 4936 sctp_free_remote_addr(chk->whoTo); 4937 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); 4938 SCTP_DECR_CHK_COUNT(); 4939 /* sa_ignore FREED_MEMORY */ 4940 } 4941 if (asoc->mapping_array) { 4942 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); 4943 asoc->mapping_array = NULL; 4944 } 4945 if (asoc->nr_mapping_array) { 4946 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP); 4947 asoc->nr_mapping_array = NULL; 4948 } 4949 /* the stream outs */ 4950 if (asoc->strmout) { 4951 SCTP_FREE(asoc->strmout, SCTP_M_STRMO); 4952 asoc->strmout = NULL; 4953 } 4954 asoc->strm_realoutsize = asoc->streamoutcnt = 0; 4955 if (asoc->strmin) { 4956 for (i = 0; i < asoc->streamincnt; i++) { 4957 sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue); 4958 sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue); 4959 } 4960 SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 4961 asoc->strmin = NULL; 4962 } 4963 asoc->streamincnt = 0; 4964 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) { 4965 #ifdef INVARIANTS 4966 if (SCTP_BASE_INFO(ipi_count_raddr) == 0) { 4967 panic("no net's left alloc'ed, or list points to itself"); 4968 } 4969 #endif 4970 TAILQ_REMOVE(&asoc->nets, net, sctp_next); 4971 sctp_free_remote_addr(net); 4972 } 4973 LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) { 4974 /* sa_ignore FREED_MEMORY */ 4975 sctp_remove_laddr(laddr); 4976 } 4977 4978 /* pending asconf (address) parameters */ 4979 TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) { 4980 /* sa_ignore FREED_MEMORY */ 4981 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next); 4982 SCTP_FREE(aparam, SCTP_M_ASC_ADDR); 4983 } 4984 TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) { 4985 /* sa_ignore FREED_MEMORY */ 4986 TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next); 4987 if (aack->data != NULL) { 4988 sctp_m_freem(aack->data); 4989 } 4990 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack); 4991 } 4992 /* clean up auth stuff */ 4993 if (asoc->local_hmacs) 4994 sctp_free_hmaclist(asoc->local_hmacs); 4995 if (asoc->peer_hmacs) 4996 sctp_free_hmaclist(asoc->peer_hmacs); 4997 4998 if (asoc->local_auth_chunks) 4999 sctp_free_chunklist(asoc->local_auth_chunks); 5000 if (asoc->peer_auth_chunks) 5001 sctp_free_chunklist(asoc->peer_auth_chunks); 5002 5003 sctp_free_authinfo(&asoc->authinfo); 5004 5005 LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) { 5006 LIST_REMOVE(shared_key, next); 5007 sctp_free_sharedkey(shared_key); 5008 /* sa_ignore FREED_MEMORY */ 5009 } 5010 5011 /* Insert new items here :> */ 5012 5013 /* Get rid of LOCK */ 5014 SCTP_TCB_UNLOCK(stcb); 5015 SCTP_TCB_LOCK_DESTROY(stcb); 5016 if (from_inpcbfree == SCTP_NORMAL_PROC) { 5017 SCTP_INP_INFO_WUNLOCK(); 5018 SCTP_INP_RLOCK(inp); 5019 } 5020 #ifdef SCTP_TRACK_FREED_ASOCS 5021 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5022 /* now clean up the tasoc itself */ 5023 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); 5024 SCTP_DECR_ASOC_COUNT(); 5025 } else { 5026 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist); 5027 } 5028 #else 5029 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); 5030 SCTP_DECR_ASOC_COUNT(); 5031 #endif 5032 if (from_inpcbfree == SCTP_NORMAL_PROC) { 5033 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5034 /* 5035 * If its NOT the inp_free calling us AND sctp_close 5036 * as been called, we call back... 5037 */ 5038 SCTP_INP_RUNLOCK(inp); 5039 /* 5040 * This will start the kill timer (if we are the 5041 * last one) since we hold an increment yet. But 5042 * this is the only safe way to do this since 5043 * otherwise if the socket closes at the same time 5044 * we are here we might collide in the cleanup. 5045 */ 5046 sctp_inpcb_free(inp, 5047 SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE, 5048 SCTP_CALLED_DIRECTLY_NOCMPSET); 5049 SCTP_INP_DECR_REF(inp); 5050 } else { 5051 /* The socket is still open. */ 5052 SCTP_INP_DECR_REF(inp); 5053 SCTP_INP_RUNLOCK(inp); 5054 } 5055 } 5056 /* destroyed the asoc */ 5057 #ifdef SCTP_LOG_CLOSING 5058 sctp_log_closing(inp, NULL, 11); 5059 #endif 5060 return (1); 5061 } 5062 5063 /* 5064 * determine if a destination is "reachable" based upon the addresses bound 5065 * to the current endpoint (e.g. only v4 or v6 currently bound) 5066 */ 5067 /* 5068 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use 5069 * assoc level v4/v6 flags, as the assoc *may* not have the same address 5070 * types bound as its endpoint 5071 */ 5072 int 5073 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr) 5074 { 5075 struct sctp_inpcb *inp; 5076 int answer; 5077 5078 /* 5079 * No locks here, the TCB, in all cases is already locked and an 5080 * assoc is up. There is either a INP lock by the caller applied (in 5081 * asconf case when deleting an address) or NOT in the HB case, 5082 * however if HB then the INP increment is up and the INP will not 5083 * be removed (on top of the fact that we have a TCB lock). So we 5084 * only want to read the sctp_flags, which is either bound-all or 5085 * not.. no protection needed since once an assoc is up you can't be 5086 * changing your binding. 5087 */ 5088 inp = stcb->sctp_ep; 5089 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 5090 /* if bound all, destination is not restricted */ 5091 /* 5092 * RRS: Question during lock work: Is this correct? If you 5093 * are bound-all you still might need to obey the V4--V6 5094 * flags??? IMO this bound-all stuff needs to be removed! 5095 */ 5096 return (1); 5097 } 5098 /* NOTE: all "scope" checks are done when local addresses are added */ 5099 switch (destaddr->sa_family) { 5100 #ifdef INET6 5101 case AF_INET6: 5102 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6; 5103 break; 5104 #endif 5105 #ifdef INET 5106 case AF_INET: 5107 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4; 5108 break; 5109 #endif 5110 default: 5111 /* invalid family, so it's unreachable */ 5112 answer = 0; 5113 break; 5114 } 5115 return (answer); 5116 } 5117 5118 /* 5119 * update the inp_vflags on an endpoint 5120 */ 5121 static void 5122 sctp_update_ep_vflag(struct sctp_inpcb *inp) 5123 { 5124 struct sctp_laddr *laddr; 5125 5126 /* first clear the flag */ 5127 inp->ip_inp.inp.inp_vflag = 0; 5128 /* set the flag based on addresses on the ep list */ 5129 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 5130 if (laddr->ifa == NULL) { 5131 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", 5132 __func__); 5133 continue; 5134 } 5135 5136 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { 5137 continue; 5138 } 5139 switch (laddr->ifa->address.sa.sa_family) { 5140 #ifdef INET6 5141 case AF_INET6: 5142 inp->ip_inp.inp.inp_vflag |= INP_IPV6; 5143 break; 5144 #endif 5145 #ifdef INET 5146 case AF_INET: 5147 inp->ip_inp.inp.inp_vflag |= INP_IPV4; 5148 break; 5149 #endif 5150 default: 5151 break; 5152 } 5153 } 5154 } 5155 5156 /* 5157 * Add the address to the endpoint local address list There is nothing to be 5158 * done if we are bound to all addresses 5159 */ 5160 void 5161 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action) 5162 { 5163 struct sctp_laddr *laddr; 5164 struct sctp_tcb *stcb; 5165 int fnd, error = 0; 5166 5167 fnd = 0; 5168 5169 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 5170 /* You are already bound to all. You have it already */ 5171 return; 5172 } 5173 #ifdef INET6 5174 if (ifa->address.sa.sa_family == AF_INET6) { 5175 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { 5176 /* Can't bind a non-useable addr. */ 5177 return; 5178 } 5179 } 5180 #endif 5181 /* first, is it already present? */ 5182 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 5183 if (laddr->ifa == ifa) { 5184 fnd = 1; 5185 break; 5186 } 5187 } 5188 5189 if (fnd == 0) { 5190 /* Not in the ep list */ 5191 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action); 5192 if (error != 0) 5193 return; 5194 inp->laddr_count++; 5195 /* update inp_vflag flags */ 5196 switch (ifa->address.sa.sa_family) { 5197 #ifdef INET6 5198 case AF_INET6: 5199 inp->ip_inp.inp.inp_vflag |= INP_IPV6; 5200 break; 5201 #endif 5202 #ifdef INET 5203 case AF_INET: 5204 inp->ip_inp.inp.inp_vflag |= INP_IPV4; 5205 break; 5206 #endif 5207 default: 5208 break; 5209 } 5210 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 5211 sctp_add_local_addr_restricted(stcb, ifa); 5212 } 5213 } 5214 return; 5215 } 5216 5217 /* 5218 * select a new (hopefully reachable) destination net (should only be used 5219 * when we deleted an ep addr that is the only usable source address to reach 5220 * the destination net) 5221 */ 5222 static void 5223 sctp_select_primary_destination(struct sctp_tcb *stcb) 5224 { 5225 struct sctp_nets *net; 5226 5227 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 5228 /* for now, we'll just pick the first reachable one we find */ 5229 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) 5230 continue; 5231 if (sctp_destination_is_reachable(stcb, 5232 (struct sockaddr *)&net->ro._l_addr)) { 5233 /* found a reachable destination */ 5234 stcb->asoc.primary_destination = net; 5235 } 5236 } 5237 /* I can't there from here! ...we're gonna die shortly... */ 5238 } 5239 5240 /* 5241 * Delete the address from the endpoint local address list. There is nothing 5242 * to be done if we are bound to all addresses 5243 */ 5244 void 5245 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa) 5246 { 5247 struct sctp_laddr *laddr; 5248 int fnd; 5249 5250 fnd = 0; 5251 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 5252 /* You are already bound to all. You have it already */ 5253 return; 5254 } 5255 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 5256 if (laddr->ifa == ifa) { 5257 fnd = 1; 5258 break; 5259 } 5260 } 5261 if (fnd && (inp->laddr_count < 2)) { 5262 /* can't delete unless there are at LEAST 2 addresses */ 5263 return; 5264 } 5265 if (fnd) { 5266 /* 5267 * clean up any use of this address go through our 5268 * associations and clear any last_used_address that match 5269 * this one for each assoc, see if a new primary_destination 5270 * is needed 5271 */ 5272 struct sctp_tcb *stcb; 5273 5274 /* clean up "next_addr_touse" */ 5275 if (inp->next_addr_touse == laddr) 5276 /* delete this address */ 5277 inp->next_addr_touse = NULL; 5278 5279 /* clean up "last_used_address" */ 5280 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 5281 struct sctp_nets *net; 5282 5283 SCTP_TCB_LOCK(stcb); 5284 if (stcb->asoc.last_used_address == laddr) 5285 /* delete this address */ 5286 stcb->asoc.last_used_address = NULL; 5287 /* 5288 * Now spin through all the nets and purge any ref 5289 * to laddr 5290 */ 5291 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 5292 if (net->ro._s_addr == laddr->ifa) { 5293 /* Yep, purge src address selected */ 5294 RO_NHFREE(&net->ro); 5295 sctp_free_ifa(net->ro._s_addr); 5296 net->ro._s_addr = NULL; 5297 net->src_addr_selected = 0; 5298 } 5299 } 5300 SCTP_TCB_UNLOCK(stcb); 5301 } /* for each tcb */ 5302 /* remove it from the ep list */ 5303 sctp_remove_laddr(laddr); 5304 inp->laddr_count--; 5305 /* update inp_vflag flags */ 5306 sctp_update_ep_vflag(inp); 5307 } 5308 return; 5309 } 5310 5311 /* 5312 * Add the address to the TCB local address restricted list. 5313 * This is a "pending" address list (eg. addresses waiting for an 5314 * ASCONF-ACK response) and cannot be used as a valid source address. 5315 */ 5316 void 5317 sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) 5318 { 5319 struct sctp_laddr *laddr; 5320 struct sctpladdr *list; 5321 5322 /* 5323 * Assumes TCB is locked.. and possibly the INP. May need to 5324 * confirm/fix that if we need it and is not the case. 5325 */ 5326 list = &stcb->asoc.sctp_restricted_addrs; 5327 5328 #ifdef INET6 5329 if (ifa->address.sa.sa_family == AF_INET6) { 5330 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { 5331 /* Can't bind a non-existent addr. */ 5332 return; 5333 } 5334 } 5335 #endif 5336 /* does the address already exist? */ 5337 LIST_FOREACH(laddr, list, sctp_nxt_addr) { 5338 if (laddr->ifa == ifa) { 5339 return; 5340 } 5341 } 5342 5343 /* add to the list */ 5344 (void)sctp_insert_laddr(list, ifa, 0); 5345 return; 5346 } 5347 5348 /* 5349 * Remove a local address from the TCB local address restricted list 5350 */ 5351 void 5352 sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) 5353 { 5354 struct sctp_inpcb *inp; 5355 struct sctp_laddr *laddr; 5356 5357 /* 5358 * This is called by asconf work. It is assumed that a) The TCB is 5359 * locked and b) The INP is locked. This is true in as much as I can 5360 * trace through the entry asconf code where I did these locks. 5361 * Again, the ASCONF code is a bit different in that it does lock 5362 * the INP during its work often times. This must be since we don't 5363 * want other proc's looking up things while what they are looking 5364 * up is changing :-D 5365 */ 5366 5367 inp = stcb->sctp_ep; 5368 /* if subset bound and don't allow ASCONF's, can't delete last */ 5369 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && 5370 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) { 5371 if (stcb->sctp_ep->laddr_count < 2) { 5372 /* can't delete last address */ 5373 return; 5374 } 5375 } 5376 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) { 5377 /* remove the address if it exists */ 5378 if (laddr->ifa == NULL) 5379 continue; 5380 if (laddr->ifa == ifa) { 5381 sctp_remove_laddr(laddr); 5382 return; 5383 } 5384 } 5385 5386 /* address not found! */ 5387 return; 5388 } 5389 5390 /* sysctl */ 5391 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC; 5392 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR; 5393 5394 #if defined(SCTP_MCORE_INPUT) && defined(SMP) 5395 struct sctp_mcore_ctrl *sctp_mcore_workers = NULL; 5396 int *sctp_cpuarry = NULL; 5397 5398 void 5399 sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use) 5400 { 5401 /* Queue a packet to a processor for the specified core */ 5402 struct sctp_mcore_queue *qent; 5403 struct sctp_mcore_ctrl *wkq; 5404 int need_wake = 0; 5405 5406 if (sctp_mcore_workers == NULL) { 5407 /* Something went way bad during setup */ 5408 sctp_input_with_port(m, off, 0); 5409 return; 5410 } 5411 SCTP_MALLOC(qent, struct sctp_mcore_queue *, 5412 (sizeof(struct sctp_mcore_queue)), 5413 SCTP_M_MCORE); 5414 if (qent == NULL) { 5415 /* This is trouble */ 5416 sctp_input_with_port(m, off, 0); 5417 return; 5418 } 5419 qent->vn = curvnet; 5420 qent->m = m; 5421 qent->off = off; 5422 qent->v6 = 0; 5423 wkq = &sctp_mcore_workers[cpu_to_use]; 5424 SCTP_MCORE_QLOCK(wkq); 5425 5426 TAILQ_INSERT_TAIL(&wkq->que, qent, next); 5427 if (wkq->running == 0) { 5428 need_wake = 1; 5429 } 5430 SCTP_MCORE_QUNLOCK(wkq); 5431 if (need_wake) { 5432 wakeup(&wkq->running); 5433 } 5434 } 5435 5436 static void 5437 sctp_mcore_thread(void *arg) 5438 { 5439 5440 struct sctp_mcore_ctrl *wkq; 5441 struct sctp_mcore_queue *qent; 5442 5443 wkq = (struct sctp_mcore_ctrl *)arg; 5444 struct mbuf *m; 5445 int off, v6; 5446 5447 /* Wait for first tickle */ 5448 SCTP_MCORE_LOCK(wkq); 5449 wkq->running = 0; 5450 msleep(&wkq->running, 5451 &wkq->core_mtx, 5452 0, "wait for pkt", 0); 5453 SCTP_MCORE_UNLOCK(wkq); 5454 5455 /* Bind to our cpu */ 5456 thread_lock(curthread); 5457 sched_bind(curthread, wkq->cpuid); 5458 thread_unlock(curthread); 5459 5460 /* Now lets start working */ 5461 SCTP_MCORE_LOCK(wkq); 5462 /* Now grab lock and go */ 5463 for (;;) { 5464 SCTP_MCORE_QLOCK(wkq); 5465 skip_sleep: 5466 wkq->running = 1; 5467 qent = TAILQ_FIRST(&wkq->que); 5468 if (qent) { 5469 TAILQ_REMOVE(&wkq->que, qent, next); 5470 SCTP_MCORE_QUNLOCK(wkq); 5471 CURVNET_SET(qent->vn); 5472 m = qent->m; 5473 off = qent->off; 5474 v6 = qent->v6; 5475 SCTP_FREE(qent, SCTP_M_MCORE); 5476 if (v6 == 0) { 5477 sctp_input_with_port(m, off, 0); 5478 } else { 5479 SCTP_PRINTF("V6 not yet supported\n"); 5480 sctp_m_freem(m); 5481 } 5482 CURVNET_RESTORE(); 5483 SCTP_MCORE_QLOCK(wkq); 5484 } 5485 wkq->running = 0; 5486 if (!TAILQ_EMPTY(&wkq->que)) { 5487 goto skip_sleep; 5488 } 5489 SCTP_MCORE_QUNLOCK(wkq); 5490 msleep(&wkq->running, 5491 &wkq->core_mtx, 5492 0, "wait for pkt", 0); 5493 } 5494 } 5495 5496 static void 5497 sctp_startup_mcore_threads(void) 5498 { 5499 int i, cpu; 5500 5501 if (mp_ncpus == 1) 5502 return; 5503 5504 if (sctp_mcore_workers != NULL) { 5505 /* 5506 * Already been here in some previous vnet? 5507 */ 5508 return; 5509 } 5510 SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *, 5511 ((mp_maxid + 1) * sizeof(struct sctp_mcore_ctrl)), 5512 SCTP_M_MCORE); 5513 if (sctp_mcore_workers == NULL) { 5514 /* TSNH I hope */ 5515 return; 5516 } 5517 memset(sctp_mcore_workers, 0, ((mp_maxid + 1) * 5518 sizeof(struct sctp_mcore_ctrl))); 5519 /* Init the structures */ 5520 for (i = 0; i <= mp_maxid; i++) { 5521 TAILQ_INIT(&sctp_mcore_workers[i].que); 5522 SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]); 5523 SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]); 5524 sctp_mcore_workers[i].cpuid = i; 5525 } 5526 if (sctp_cpuarry == NULL) { 5527 SCTP_MALLOC(sctp_cpuarry, int *, 5528 (mp_ncpus * sizeof(int)), 5529 SCTP_M_MCORE); 5530 i = 0; 5531 CPU_FOREACH(cpu) { 5532 sctp_cpuarry[i] = cpu; 5533 i++; 5534 } 5535 } 5536 /* Now start them all */ 5537 CPU_FOREACH(cpu) { 5538 (void)kproc_create(sctp_mcore_thread, 5539 (void *)&sctp_mcore_workers[cpu], 5540 &sctp_mcore_workers[cpu].thread_proc, 5541 0, 5542 SCTP_KTHREAD_PAGES, 5543 SCTP_MCORE_NAME); 5544 } 5545 } 5546 #endif 5547 5548 #define VALIDATE_LOADER_TUNABLE(var_name, prefix) \ 5549 if (SCTP_BASE_SYSCTL(var_name) < prefix##_MIN || \ 5550 SCTP_BASE_SYSCTL(var_name) > prefix##_MAX) \ 5551 SCTP_BASE_SYSCTL(var_name) = prefix##_DEFAULT 5552 5553 void 5554 sctp_pcb_init(void) 5555 { 5556 /* 5557 * SCTP initialization for the PCB structures should be called by 5558 * the sctp_init() function. 5559 */ 5560 int i; 5561 struct timeval tv; 5562 5563 if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) { 5564 /* error I was called twice */ 5565 return; 5566 } 5567 SCTP_BASE_VAR(sctp_pcb_initialized) = 1; 5568 5569 #if defined(SCTP_LOCAL_TRACE_BUF) 5570 memset(&SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log)); 5571 #endif 5572 #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT) 5573 SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *, 5574 ((mp_maxid + 1) * sizeof(struct sctpstat)), 5575 SCTP_M_MCORE); 5576 #endif 5577 (void)SCTP_GETTIME_TIMEVAL(&tv); 5578 #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT) 5579 memset(SCTP_BASE_STATS, 0, sizeof(struct sctpstat) * (mp_maxid + 1)); 5580 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec; 5581 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec; 5582 #else 5583 memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat)); 5584 SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec; 5585 SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec; 5586 #endif 5587 /* init the empty list of (All) Endpoints */ 5588 LIST_INIT(&SCTP_BASE_INFO(listhead)); 5589 5590 /* init the hash table of endpoints */ 5591 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize)); 5592 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize)); 5593 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale)); 5594 VALIDATE_LOADER_TUNABLE(sctp_hashtblsize, SCTPCTL_TCBHASHSIZE); 5595 VALIDATE_LOADER_TUNABLE(sctp_pcbtblsize, SCTPCTL_PCBHASHSIZE); 5596 VALIDATE_LOADER_TUNABLE(sctp_chunkscale, SCTPCTL_CHUNKSCALE); 5597 SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31), 5598 &SCTP_BASE_INFO(hashasocmark)); 5599 SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize), 5600 &SCTP_BASE_INFO(hashmark)); 5601 SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize), 5602 &SCTP_BASE_INFO(hashtcpmark)); 5603 SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize); 5604 SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH, 5605 &SCTP_BASE_INFO(hashvrfmark)); 5606 5607 SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE, 5608 &SCTP_BASE_INFO(vrf_ifn_hashmark)); 5609 /* init the zones */ 5610 /* 5611 * FIX ME: Should check for NULL returns, but if it does fail we are 5612 * doomed to panic anyways... add later maybe. 5613 */ 5614 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep", 5615 sizeof(struct sctp_inpcb), maxsockets); 5616 5617 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc", 5618 sizeof(struct sctp_tcb), sctp_max_number_of_assoc); 5619 5620 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr", 5621 sizeof(struct sctp_laddr), 5622 (sctp_max_number_of_assoc * sctp_scale_up_for_address)); 5623 5624 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr", 5625 sizeof(struct sctp_nets), 5626 (sctp_max_number_of_assoc * sctp_scale_up_for_address)); 5627 5628 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk", 5629 sizeof(struct sctp_tmit_chunk), 5630 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); 5631 5632 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq", 5633 sizeof(struct sctp_queued_to_read), 5634 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); 5635 5636 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out", 5637 sizeof(struct sctp_stream_queue_pending), 5638 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); 5639 5640 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf", 5641 sizeof(struct sctp_asconf), 5642 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); 5643 5644 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack", 5645 sizeof(struct sctp_asconf_ack), 5646 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); 5647 5648 /* Master Lock INIT for info structure */ 5649 SCTP_INP_INFO_LOCK_INIT(); 5650 SCTP_STATLOG_INIT_LOCK(); 5651 5652 SCTP_IPI_COUNT_INIT(); 5653 SCTP_IPI_ADDR_INIT(); 5654 #ifdef SCTP_PACKET_LOGGING 5655 SCTP_IP_PKTLOG_INIT(); 5656 #endif 5657 LIST_INIT(&SCTP_BASE_INFO(addr_wq)); 5658 5659 SCTP_WQ_ADDR_INIT(); 5660 /* not sure if we need all the counts */ 5661 SCTP_BASE_INFO(ipi_count_ep) = 0; 5662 /* assoc/tcb zone info */ 5663 SCTP_BASE_INFO(ipi_count_asoc) = 0; 5664 /* local addrlist zone info */ 5665 SCTP_BASE_INFO(ipi_count_laddr) = 0; 5666 /* remote addrlist zone info */ 5667 SCTP_BASE_INFO(ipi_count_raddr) = 0; 5668 /* chunk info */ 5669 SCTP_BASE_INFO(ipi_count_chunk) = 0; 5670 5671 /* socket queue zone info */ 5672 SCTP_BASE_INFO(ipi_count_readq) = 0; 5673 5674 /* stream out queue cont */ 5675 SCTP_BASE_INFO(ipi_count_strmoq) = 0; 5676 5677 SCTP_BASE_INFO(ipi_free_strmoq) = 0; 5678 SCTP_BASE_INFO(ipi_free_chunks) = 0; 5679 5680 SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer)); 5681 5682 /* Init the TIMEWAIT list */ 5683 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) { 5684 LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]); 5685 } 5686 sctp_startup_iterator(); 5687 5688 #if defined(SCTP_MCORE_INPUT) && defined(SMP) 5689 sctp_startup_mcore_threads(); 5690 #endif 5691 5692 /* 5693 * INIT the default VRF which for BSD is the only one, other O/S's 5694 * may have more. But initially they must start with one and then 5695 * add the VRF's as addresses are added. 5696 */ 5697 sctp_init_vrf_list(SCTP_DEFAULT_VRF); 5698 } 5699 5700 /* 5701 * Assumes that the SCTP_BASE_INFO() lock is NOT held. 5702 */ 5703 void 5704 sctp_pcb_finish(void) 5705 { 5706 struct sctp_vrflist *vrf_bucket; 5707 struct sctp_vrf *vrf, *nvrf; 5708 struct sctp_ifn *ifn, *nifn; 5709 struct sctp_ifa *ifa, *nifa; 5710 struct sctpvtaghead *chain; 5711 struct sctp_tagblock *twait_block, *prev_twait_block; 5712 struct sctp_laddr *wi, *nwi; 5713 int i; 5714 struct sctp_iterator *it, *nit; 5715 5716 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { 5717 SCTP_PRINTF("%s: race condition on teardown.\n", __func__); 5718 return; 5719 } 5720 SCTP_BASE_VAR(sctp_pcb_initialized) = 0; 5721 /* 5722 * In FreeBSD the iterator thread never exits but we do clean up. 5723 * The only way FreeBSD reaches here is if we have VRF's but we 5724 * still add the ifdef to make it compile on old versions. 5725 */ 5726 retry: 5727 SCTP_IPI_ITERATOR_WQ_LOCK(); 5728 /* 5729 * sctp_iterator_worker() might be working on an it entry without 5730 * holding the lock. We won't find it on the list either and 5731 * continue and free/destroy it. While holding the lock, spin, to 5732 * avoid the race condition as sctp_iterator_worker() will have to 5733 * wait to re-acquire the lock. 5734 */ 5735 if (sctp_it_ctl.iterator_running != 0 || sctp_it_ctl.cur_it != NULL) { 5736 SCTP_IPI_ITERATOR_WQ_UNLOCK(); 5737 SCTP_PRINTF("%s: Iterator running while we held the lock. Retry. " 5738 "cur_it=%p\n", __func__, sctp_it_ctl.cur_it); 5739 DELAY(10); 5740 goto retry; 5741 } 5742 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) { 5743 if (it->vn != curvnet) { 5744 continue; 5745 } 5746 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); 5747 if (it->function_atend != NULL) { 5748 (*it->function_atend) (it->pointer, it->val); 5749 } 5750 SCTP_FREE(it, SCTP_M_ITER); 5751 } 5752 SCTP_IPI_ITERATOR_WQ_UNLOCK(); 5753 SCTP_ITERATOR_LOCK(); 5754 if ((sctp_it_ctl.cur_it) && 5755 (sctp_it_ctl.cur_it->vn == curvnet)) { 5756 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT; 5757 } 5758 SCTP_ITERATOR_UNLOCK(); 5759 SCTP_OS_TIMER_STOP_DRAIN(&SCTP_BASE_INFO(addr_wq_timer.timer)); 5760 SCTP_WQ_ADDR_LOCK(); 5761 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) { 5762 LIST_REMOVE(wi, sctp_nxt_addr); 5763 SCTP_DECR_LADDR_COUNT(); 5764 if (wi->action == SCTP_DEL_IP_ADDRESS) { 5765 SCTP_FREE(wi->ifa, SCTP_M_IFA); 5766 } 5767 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi); 5768 } 5769 SCTP_WQ_ADDR_UNLOCK(); 5770 5771 /* 5772 * free the vrf/ifn/ifa lists and hashes (be sure address monitor is 5773 * destroyed first). 5774 */ 5775 SCTP_IPI_ADDR_WLOCK(); 5776 vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))]; 5777 LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) { 5778 LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) { 5779 LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) { 5780 /* free the ifa */ 5781 LIST_REMOVE(ifa, next_bucket); 5782 LIST_REMOVE(ifa, next_ifa); 5783 SCTP_FREE(ifa, SCTP_M_IFA); 5784 } 5785 /* free the ifn */ 5786 LIST_REMOVE(ifn, next_bucket); 5787 LIST_REMOVE(ifn, next_ifn); 5788 SCTP_FREE(ifn, SCTP_M_IFN); 5789 } 5790 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark); 5791 /* free the vrf */ 5792 LIST_REMOVE(vrf, next_vrf); 5793 SCTP_FREE(vrf, SCTP_M_VRF); 5794 } 5795 SCTP_IPI_ADDR_WUNLOCK(); 5796 /* free the vrf hashes */ 5797 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark)); 5798 SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark)); 5799 5800 /* 5801 * free the TIMEWAIT list elements malloc'd in the function 5802 * sctp_add_vtag_to_timewait()... 5803 */ 5804 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) { 5805 chain = &SCTP_BASE_INFO(vtag_timewait)[i]; 5806 if (!LIST_EMPTY(chain)) { 5807 prev_twait_block = NULL; 5808 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { 5809 if (prev_twait_block) { 5810 SCTP_FREE(prev_twait_block, SCTP_M_TIMW); 5811 } 5812 prev_twait_block = twait_block; 5813 } 5814 SCTP_FREE(prev_twait_block, SCTP_M_TIMW); 5815 } 5816 } 5817 5818 /* free the locks and mutexes */ 5819 #ifdef SCTP_PACKET_LOGGING 5820 SCTP_IP_PKTLOG_DESTROY(); 5821 #endif 5822 SCTP_IPI_ADDR_DESTROY(); 5823 SCTP_STATLOG_DESTROY(); 5824 SCTP_INP_INFO_LOCK_DESTROY(); 5825 5826 SCTP_WQ_ADDR_DESTROY(); 5827 5828 /* Get rid of other stuff too. */ 5829 if (SCTP_BASE_INFO(sctp_asochash) != NULL) 5830 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark)); 5831 if (SCTP_BASE_INFO(sctp_ephash) != NULL) 5832 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark)); 5833 if (SCTP_BASE_INFO(sctp_tcpephash) != NULL) 5834 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark)); 5835 5836 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep)); 5837 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc)); 5838 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr)); 5839 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net)); 5840 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk)); 5841 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq)); 5842 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq)); 5843 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf)); 5844 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack)); 5845 #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT) 5846 SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE); 5847 #endif 5848 } 5849 5850 int 5851 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m, 5852 int offset, int limit, 5853 struct sockaddr *src, struct sockaddr *dst, 5854 struct sockaddr *altsa, uint16_t port) 5855 { 5856 /* 5857 * grub through the INIT pulling addresses and loading them to the 5858 * nets structure in the asoc. The from address in the mbuf should 5859 * also be loaded (if it is not already). This routine can be called 5860 * with either INIT or INIT-ACK's as long as the m points to the IP 5861 * packet and the offset points to the beginning of the parameters. 5862 */ 5863 struct sctp_inpcb *inp; 5864 struct sctp_nets *net, *nnet, *net_tmp; 5865 struct sctp_paramhdr *phdr, param_buf; 5866 struct sctp_tcb *stcb_tmp; 5867 uint16_t ptype, plen; 5868 struct sockaddr *sa; 5869 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE]; 5870 struct sctp_auth_random *p_random = NULL; 5871 uint16_t random_len = 0; 5872 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE]; 5873 struct sctp_auth_hmac_algo *hmacs = NULL; 5874 uint16_t hmacs_len = 0; 5875 uint8_t saw_asconf = 0; 5876 uint8_t saw_asconf_ack = 0; 5877 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE]; 5878 struct sctp_auth_chunk_list *chunks = NULL; 5879 uint16_t num_chunks = 0; 5880 sctp_key_t *new_key; 5881 uint32_t keylen; 5882 int got_random = 0, got_hmacs = 0, got_chklist = 0; 5883 uint8_t peer_supports_ecn; 5884 uint8_t peer_supports_prsctp; 5885 uint8_t peer_supports_auth; 5886 uint8_t peer_supports_asconf; 5887 uint8_t peer_supports_asconf_ack; 5888 uint8_t peer_supports_reconfig; 5889 uint8_t peer_supports_nrsack; 5890 uint8_t peer_supports_pktdrop; 5891 uint8_t peer_supports_idata; 5892 #ifdef INET 5893 struct sockaddr_in sin; 5894 #endif 5895 #ifdef INET6 5896 struct sockaddr_in6 sin6; 5897 #endif 5898 5899 /* First get the destination address setup too. */ 5900 #ifdef INET 5901 memset(&sin, 0, sizeof(sin)); 5902 sin.sin_family = AF_INET; 5903 sin.sin_len = sizeof(sin); 5904 sin.sin_port = stcb->rport; 5905 #endif 5906 #ifdef INET6 5907 memset(&sin6, 0, sizeof(sin6)); 5908 sin6.sin6_family = AF_INET6; 5909 sin6.sin6_len = sizeof(struct sockaddr_in6); 5910 sin6.sin6_port = stcb->rport; 5911 #endif 5912 if (altsa) { 5913 sa = altsa; 5914 } else { 5915 sa = src; 5916 } 5917 peer_supports_idata = 0; 5918 peer_supports_ecn = 0; 5919 peer_supports_prsctp = 0; 5920 peer_supports_auth = 0; 5921 peer_supports_asconf = 0; 5922 peer_supports_asconf_ack = 0; 5923 peer_supports_reconfig = 0; 5924 peer_supports_nrsack = 0; 5925 peer_supports_pktdrop = 0; 5926 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 5927 /* mark all addresses that we have currently on the list */ 5928 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC; 5929 } 5930 /* does the source address already exist? if so skip it */ 5931 inp = stcb->sctp_ep; 5932 atomic_add_int(&stcb->asoc.refcnt, 1); 5933 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb); 5934 atomic_subtract_int(&stcb->asoc.refcnt, 1); 5935 5936 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) { 5937 /* we must add the source address */ 5938 /* no scope set here since we have a tcb already. */ 5939 switch (sa->sa_family) { 5940 #ifdef INET 5941 case AF_INET: 5942 if (stcb->asoc.scope.ipv4_addr_legal) { 5943 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) { 5944 return (-1); 5945 } 5946 } 5947 break; 5948 #endif 5949 #ifdef INET6 5950 case AF_INET6: 5951 if (stcb->asoc.scope.ipv6_addr_legal) { 5952 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) { 5953 return (-2); 5954 } 5955 } 5956 break; 5957 #endif 5958 default: 5959 break; 5960 } 5961 } else { 5962 if (net_tmp != NULL && stcb_tmp == stcb) { 5963 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC; 5964 } else if (stcb_tmp != stcb) { 5965 /* It belongs to another association? */ 5966 if (stcb_tmp) 5967 SCTP_TCB_UNLOCK(stcb_tmp); 5968 return (-3); 5969 } 5970 } 5971 if (stcb->asoc.state == 0) { 5972 /* the assoc was freed? */ 5973 return (-4); 5974 } 5975 /* now we must go through each of the params. */ 5976 phdr = sctp_get_next_param(m, offset, ¶m_buf, sizeof(param_buf)); 5977 while (phdr) { 5978 ptype = ntohs(phdr->param_type); 5979 plen = ntohs(phdr->param_length); 5980 /* 5981 * SCTP_PRINTF("ptype => %0x, plen => %d\n", 5982 * (uint32_t)ptype, (int)plen); 5983 */ 5984 if (offset + plen > limit) { 5985 break; 5986 } 5987 if (plen < sizeof(struct sctp_paramhdr)) { 5988 break; 5989 } 5990 #ifdef INET 5991 if (ptype == SCTP_IPV4_ADDRESS) { 5992 if (stcb->asoc.scope.ipv4_addr_legal) { 5993 struct sctp_ipv4addr_param *p4, p4_buf; 5994 5995 /* ok get the v4 address and check/add */ 5996 phdr = sctp_get_next_param(m, offset, 5997 (struct sctp_paramhdr *)&p4_buf, 5998 sizeof(p4_buf)); 5999 if (plen != sizeof(struct sctp_ipv4addr_param) || 6000 phdr == NULL) { 6001 return (-5); 6002 } 6003 p4 = (struct sctp_ipv4addr_param *)phdr; 6004 sin.sin_addr.s_addr = p4->addr; 6005 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 6006 /* Skip multi-cast addresses */ 6007 goto next_param; 6008 } 6009 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) || 6010 (sin.sin_addr.s_addr == INADDR_ANY)) { 6011 goto next_param; 6012 } 6013 sa = (struct sockaddr *)&sin; 6014 inp = stcb->sctp_ep; 6015 atomic_add_int(&stcb->asoc.refcnt, 1); 6016 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, 6017 dst, stcb); 6018 atomic_subtract_int(&stcb->asoc.refcnt, 1); 6019 6020 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || 6021 inp == NULL) { 6022 /* we must add the source address */ 6023 /* 6024 * no scope set since we have a tcb 6025 * already 6026 */ 6027 6028 /* 6029 * we must validate the state again 6030 * here 6031 */ 6032 add_it_now: 6033 if (stcb->asoc.state == 0) { 6034 /* the assoc was freed? */ 6035 return (-7); 6036 } 6037 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) { 6038 return (-8); 6039 } 6040 } else if (stcb_tmp == stcb) { 6041 if (stcb->asoc.state == 0) { 6042 /* the assoc was freed? */ 6043 return (-10); 6044 } 6045 if (net != NULL) { 6046 /* clear flag */ 6047 net->dest_state &= 6048 ~SCTP_ADDR_NOT_IN_ASSOC; 6049 } 6050 } else { 6051 /* 6052 * strange, address is in another 6053 * assoc? straighten out locks. 6054 */ 6055 if (stcb_tmp) { 6056 if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) { 6057 struct mbuf *op_err; 6058 char msg[SCTP_DIAG_INFO_LEN]; 6059 6060 /* 6061 * in setup state we 6062 * abort this guy 6063 */ 6064 SCTP_SNPRINTF(msg, sizeof(msg), 6065 "%s:%d at %s", __FILE__, __LINE__, __func__); 6066 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 6067 msg); 6068 sctp_abort_an_association(stcb_tmp->sctp_ep, 6069 stcb_tmp, op_err, false, 6070 SCTP_SO_NOT_LOCKED); 6071 goto add_it_now; 6072 } 6073 SCTP_TCB_UNLOCK(stcb_tmp); 6074 } 6075 6076 if (stcb->asoc.state == 0) { 6077 /* the assoc was freed? */ 6078 return (-12); 6079 } 6080 return (-13); 6081 } 6082 } 6083 } else 6084 #endif 6085 #ifdef INET6 6086 if (ptype == SCTP_IPV6_ADDRESS) { 6087 if (stcb->asoc.scope.ipv6_addr_legal) { 6088 /* ok get the v6 address and check/add */ 6089 struct sctp_ipv6addr_param *p6, p6_buf; 6090 6091 phdr = sctp_get_next_param(m, offset, 6092 (struct sctp_paramhdr *)&p6_buf, 6093 sizeof(p6_buf)); 6094 if (plen != sizeof(struct sctp_ipv6addr_param) || 6095 phdr == NULL) { 6096 return (-14); 6097 } 6098 p6 = (struct sctp_ipv6addr_param *)phdr; 6099 memcpy((caddr_t)&sin6.sin6_addr, p6->addr, 6100 sizeof(p6->addr)); 6101 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) { 6102 /* Skip multi-cast addresses */ 6103 goto next_param; 6104 } 6105 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) { 6106 /* 6107 * Link local make no sense without 6108 * scope 6109 */ 6110 goto next_param; 6111 } 6112 sa = (struct sockaddr *)&sin6; 6113 inp = stcb->sctp_ep; 6114 atomic_add_int(&stcb->asoc.refcnt, 1); 6115 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, 6116 dst, stcb); 6117 atomic_subtract_int(&stcb->asoc.refcnt, 1); 6118 if (stcb_tmp == NULL && 6119 (inp == stcb->sctp_ep || inp == NULL)) { 6120 /* 6121 * we must validate the state again 6122 * here 6123 */ 6124 add_it_now6: 6125 if (stcb->asoc.state == 0) { 6126 /* the assoc was freed? */ 6127 return (-16); 6128 } 6129 /* 6130 * we must add the address, no scope 6131 * set 6132 */ 6133 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) { 6134 return (-17); 6135 } 6136 } else if (stcb_tmp == stcb) { 6137 /* 6138 * we must validate the state again 6139 * here 6140 */ 6141 if (stcb->asoc.state == 0) { 6142 /* the assoc was freed? */ 6143 return (-19); 6144 } 6145 if (net != NULL) { 6146 /* clear flag */ 6147 net->dest_state &= 6148 ~SCTP_ADDR_NOT_IN_ASSOC; 6149 } 6150 } else { 6151 /* 6152 * strange, address is in another 6153 * assoc? straighten out locks. 6154 */ 6155 if (stcb_tmp) { 6156 if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) { 6157 struct mbuf *op_err; 6158 char msg[SCTP_DIAG_INFO_LEN]; 6159 6160 /* 6161 * in setup state we 6162 * abort this guy 6163 */ 6164 SCTP_SNPRINTF(msg, sizeof(msg), 6165 "%s:%d at %s", __FILE__, __LINE__, __func__); 6166 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 6167 msg); 6168 sctp_abort_an_association(stcb_tmp->sctp_ep, 6169 stcb_tmp, op_err, false, 6170 SCTP_SO_NOT_LOCKED); 6171 goto add_it_now6; 6172 } 6173 SCTP_TCB_UNLOCK(stcb_tmp); 6174 } 6175 if (stcb->asoc.state == 0) { 6176 /* the assoc was freed? */ 6177 return (-21); 6178 } 6179 return (-22); 6180 } 6181 } 6182 } else 6183 #endif 6184 if (ptype == SCTP_ECN_CAPABLE) { 6185 peer_supports_ecn = 1; 6186 } else if (ptype == SCTP_ULP_ADAPTATION) { 6187 if (stcb->asoc.state != SCTP_STATE_OPEN) { 6188 struct sctp_adaptation_layer_indication ai, 6189 *aip; 6190 6191 phdr = sctp_get_next_param(m, offset, 6192 (struct sctp_paramhdr *)&ai, sizeof(ai)); 6193 aip = (struct sctp_adaptation_layer_indication *)phdr; 6194 if (aip) { 6195 stcb->asoc.peers_adaptation = ntohl(aip->indication); 6196 stcb->asoc.adaptation_needed = 1; 6197 } 6198 } 6199 } else if (ptype == SCTP_SET_PRIM_ADDR) { 6200 struct sctp_asconf_addr_param lstore, *fee; 6201 int lptype; 6202 struct sockaddr *lsa = NULL; 6203 #ifdef INET 6204 struct sctp_asconf_addrv4_param *fii; 6205 #endif 6206 6207 if (stcb->asoc.asconf_supported == 0) { 6208 return (-100); 6209 } 6210 if (plen > sizeof(lstore)) { 6211 return (-23); 6212 } 6213 if (plen < sizeof(struct sctp_asconf_addrv4_param)) { 6214 return (-101); 6215 } 6216 phdr = sctp_get_next_param(m, offset, 6217 (struct sctp_paramhdr *)&lstore, 6218 plen); 6219 if (phdr == NULL) { 6220 return (-24); 6221 } 6222 fee = (struct sctp_asconf_addr_param *)phdr; 6223 lptype = ntohs(fee->addrp.ph.param_type); 6224 switch (lptype) { 6225 #ifdef INET 6226 case SCTP_IPV4_ADDRESS: 6227 if (plen != 6228 sizeof(struct sctp_asconf_addrv4_param)) { 6229 SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n", 6230 (int)sizeof(struct sctp_asconf_addrv4_param), 6231 plen); 6232 } else { 6233 fii = (struct sctp_asconf_addrv4_param *)fee; 6234 sin.sin_addr.s_addr = fii->addrp.addr; 6235 lsa = (struct sockaddr *)&sin; 6236 } 6237 break; 6238 #endif 6239 #ifdef INET6 6240 case SCTP_IPV6_ADDRESS: 6241 if (plen != 6242 sizeof(struct sctp_asconf_addr_param)) { 6243 SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n", 6244 (int)sizeof(struct sctp_asconf_addr_param), 6245 plen); 6246 } else { 6247 memcpy(sin6.sin6_addr.s6_addr, 6248 fee->addrp.addr, 6249 sizeof(fee->addrp.addr)); 6250 lsa = (struct sockaddr *)&sin6; 6251 } 6252 break; 6253 #endif 6254 default: 6255 break; 6256 } 6257 if (lsa) { 6258 (void)sctp_set_primary_addr(stcb, sa, NULL); 6259 } 6260 } else if (ptype == SCTP_HAS_NAT_SUPPORT) { 6261 stcb->asoc.peer_supports_nat = 1; 6262 } else if (ptype == SCTP_PRSCTP_SUPPORTED) { 6263 /* Peer supports pr-sctp */ 6264 peer_supports_prsctp = 1; 6265 } else if (ptype == SCTP_ZERO_CHECKSUM_ACCEPTABLE) { 6266 struct sctp_zero_checksum_acceptable zero_chksum, 6267 *zero_chksum_p; 6268 6269 phdr = sctp_get_next_param(m, offset, 6270 (struct sctp_paramhdr *)&zero_chksum, 6271 sizeof(struct sctp_zero_checksum_acceptable)); 6272 if (phdr != NULL) { 6273 /* 6274 * Only send zero checksums if the upper 6275 * layer has enabled the support for the 6276 * same method as allowed by the peer. 6277 */ 6278 zero_chksum_p = (struct sctp_zero_checksum_acceptable *)phdr; 6279 if ((ntohl(zero_chksum_p->edmid) != SCTP_EDMID_NONE) && 6280 (ntohl(zero_chksum_p->edmid) == stcb->asoc.rcv_edmid)) { 6281 stcb->asoc.snd_edmid = stcb->asoc.rcv_edmid; 6282 } 6283 } 6284 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) { 6285 /* A supported extension chunk */ 6286 struct sctp_supported_chunk_types_param *pr_supported; 6287 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE]; 6288 int num_ent, i; 6289 6290 if (plen > sizeof(local_store)) { 6291 return (-35); 6292 } 6293 phdr = sctp_get_next_param(m, offset, 6294 (struct sctp_paramhdr *)&local_store, plen); 6295 if (phdr == NULL) { 6296 return (-25); 6297 } 6298 pr_supported = (struct sctp_supported_chunk_types_param *)phdr; 6299 num_ent = plen - sizeof(struct sctp_paramhdr); 6300 for (i = 0; i < num_ent; i++) { 6301 switch (pr_supported->chunk_types[i]) { 6302 case SCTP_ASCONF: 6303 peer_supports_asconf = 1; 6304 break; 6305 case SCTP_ASCONF_ACK: 6306 peer_supports_asconf_ack = 1; 6307 break; 6308 case SCTP_FORWARD_CUM_TSN: 6309 peer_supports_prsctp = 1; 6310 break; 6311 case SCTP_PACKET_DROPPED: 6312 peer_supports_pktdrop = 1; 6313 break; 6314 case SCTP_NR_SELECTIVE_ACK: 6315 peer_supports_nrsack = 1; 6316 break; 6317 case SCTP_STREAM_RESET: 6318 peer_supports_reconfig = 1; 6319 break; 6320 case SCTP_AUTHENTICATION: 6321 peer_supports_auth = 1; 6322 break; 6323 case SCTP_IDATA: 6324 peer_supports_idata = 1; 6325 break; 6326 default: 6327 /* one I have not learned yet */ 6328 break; 6329 } 6330 } 6331 } else if (ptype == SCTP_RANDOM) { 6332 if (plen > sizeof(random_store)) 6333 break; 6334 if (got_random) { 6335 /* already processed a RANDOM */ 6336 goto next_param; 6337 } 6338 phdr = sctp_get_next_param(m, offset, 6339 (struct sctp_paramhdr *)random_store, 6340 plen); 6341 if (phdr == NULL) 6342 return (-26); 6343 p_random = (struct sctp_auth_random *)phdr; 6344 random_len = plen - sizeof(*p_random); 6345 /* enforce the random length */ 6346 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) { 6347 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n"); 6348 return (-27); 6349 } 6350 got_random = 1; 6351 } else if (ptype == SCTP_HMAC_LIST) { 6352 uint16_t num_hmacs; 6353 uint16_t i; 6354 6355 if (plen > sizeof(hmacs_store)) 6356 break; 6357 if (got_hmacs) { 6358 /* already processed a HMAC list */ 6359 goto next_param; 6360 } 6361 phdr = sctp_get_next_param(m, offset, 6362 (struct sctp_paramhdr *)hmacs_store, 6363 plen); 6364 if (phdr == NULL) 6365 return (-28); 6366 hmacs = (struct sctp_auth_hmac_algo *)phdr; 6367 hmacs_len = plen - sizeof(*hmacs); 6368 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]); 6369 /* validate the hmac list */ 6370 if (sctp_verify_hmac_param(hmacs, num_hmacs)) { 6371 return (-29); 6372 } 6373 if (stcb->asoc.peer_hmacs != NULL) 6374 sctp_free_hmaclist(stcb->asoc.peer_hmacs); 6375 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs); 6376 if (stcb->asoc.peer_hmacs != NULL) { 6377 for (i = 0; i < num_hmacs; i++) { 6378 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs, 6379 ntohs(hmacs->hmac_ids[i])); 6380 } 6381 } 6382 got_hmacs = 1; 6383 } else if (ptype == SCTP_CHUNK_LIST) { 6384 int i; 6385 6386 if (plen > sizeof(chunks_store)) 6387 break; 6388 if (got_chklist) { 6389 /* already processed a Chunks list */ 6390 goto next_param; 6391 } 6392 phdr = sctp_get_next_param(m, offset, 6393 (struct sctp_paramhdr *)chunks_store, 6394 plen); 6395 if (phdr == NULL) 6396 return (-30); 6397 chunks = (struct sctp_auth_chunk_list *)phdr; 6398 num_chunks = plen - sizeof(*chunks); 6399 if (stcb->asoc.peer_auth_chunks != NULL) 6400 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks); 6401 else 6402 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist(); 6403 for (i = 0; i < num_chunks; i++) { 6404 (void)sctp_auth_add_chunk(chunks->chunk_types[i], 6405 stcb->asoc.peer_auth_chunks); 6406 /* record asconf/asconf-ack if listed */ 6407 if (chunks->chunk_types[i] == SCTP_ASCONF) 6408 saw_asconf = 1; 6409 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK) 6410 saw_asconf_ack = 1; 6411 } 6412 got_chklist = 1; 6413 } else if ((ptype == SCTP_HEARTBEAT_INFO) || 6414 (ptype == SCTP_STATE_COOKIE) || 6415 (ptype == SCTP_UNRECOG_PARAM) || 6416 (ptype == SCTP_COOKIE_PRESERVE) || 6417 (ptype == SCTP_SUPPORTED_ADDRTYPE) || 6418 (ptype == SCTP_ADD_IP_ADDRESS) || 6419 (ptype == SCTP_DEL_IP_ADDRESS) || 6420 (ptype == SCTP_ERROR_CAUSE_IND) || 6421 (ptype == SCTP_SUCCESS_REPORT)) { 6422 /* don't care */ 6423 } else { 6424 if ((ptype & 0x8000) == 0x0000) { 6425 /* 6426 * must stop processing the rest of the 6427 * param's. Any report bits were handled 6428 * with the call to 6429 * sctp_arethere_unrecognized_parameters() 6430 * when the INIT or INIT-ACK was first seen. 6431 */ 6432 break; 6433 } 6434 } 6435 6436 next_param: 6437 offset += SCTP_SIZE32(plen); 6438 if (offset >= limit) { 6439 break; 6440 } 6441 phdr = sctp_get_next_param(m, offset, ¶m_buf, 6442 sizeof(param_buf)); 6443 } 6444 /* Now check to see if we need to purge any addresses */ 6445 TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) { 6446 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) == 6447 SCTP_ADDR_NOT_IN_ASSOC) { 6448 /* This address has been removed from the asoc */ 6449 /* remove and free it */ 6450 stcb->asoc.numnets--; 6451 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next); 6452 if (net == stcb->asoc.alternate) { 6453 sctp_free_remote_addr(stcb->asoc.alternate); 6454 stcb->asoc.alternate = NULL; 6455 } 6456 if (net == stcb->asoc.primary_destination) { 6457 stcb->asoc.primary_destination = NULL; 6458 sctp_select_primary_destination(stcb); 6459 } 6460 sctp_free_remote_addr(net); 6461 } 6462 } 6463 if ((stcb->asoc.ecn_supported == 1) && 6464 (peer_supports_ecn == 0)) { 6465 stcb->asoc.ecn_supported = 0; 6466 } 6467 if ((stcb->asoc.prsctp_supported == 1) && 6468 (peer_supports_prsctp == 0)) { 6469 stcb->asoc.prsctp_supported = 0; 6470 } 6471 if ((stcb->asoc.auth_supported == 1) && 6472 ((peer_supports_auth == 0) || 6473 (got_random == 0) || (got_hmacs == 0))) { 6474 stcb->asoc.auth_supported = 0; 6475 } 6476 if ((stcb->asoc.asconf_supported == 1) && 6477 ((peer_supports_asconf == 0) || (peer_supports_asconf_ack == 0) || 6478 (stcb->asoc.auth_supported == 0) || 6479 (saw_asconf == 0) || (saw_asconf_ack == 0))) { 6480 stcb->asoc.asconf_supported = 0; 6481 } 6482 if ((stcb->asoc.reconfig_supported == 1) && 6483 (peer_supports_reconfig == 0)) { 6484 stcb->asoc.reconfig_supported = 0; 6485 } 6486 if ((stcb->asoc.idata_supported == 1) && 6487 (peer_supports_idata == 0)) { 6488 stcb->asoc.idata_supported = 0; 6489 } 6490 if ((stcb->asoc.nrsack_supported == 1) && 6491 (peer_supports_nrsack == 0)) { 6492 stcb->asoc.nrsack_supported = 0; 6493 } 6494 if ((stcb->asoc.pktdrop_supported == 1) && 6495 (peer_supports_pktdrop == 0)) { 6496 stcb->asoc.pktdrop_supported = 0; 6497 } 6498 /* validate authentication required parameters */ 6499 if ((peer_supports_auth == 0) && (got_chklist == 1)) { 6500 /* peer does not support auth but sent a chunks list? */ 6501 return (-31); 6502 } 6503 if ((peer_supports_asconf == 1) && (peer_supports_auth == 0)) { 6504 /* peer supports asconf but not auth? */ 6505 return (-32); 6506 } else if ((peer_supports_asconf == 1) && 6507 (peer_supports_auth == 1) && 6508 ((saw_asconf == 0) || (saw_asconf_ack == 0))) { 6509 return (-33); 6510 } 6511 /* concatenate the full random key */ 6512 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len; 6513 if (chunks != NULL) { 6514 keylen += sizeof(*chunks) + num_chunks; 6515 } 6516 new_key = sctp_alloc_key(keylen); 6517 if (new_key != NULL) { 6518 /* copy in the RANDOM */ 6519 if (p_random != NULL) { 6520 keylen = sizeof(*p_random) + random_len; 6521 memcpy(new_key->key, p_random, keylen); 6522 } else { 6523 keylen = 0; 6524 } 6525 /* append in the AUTH chunks */ 6526 if (chunks != NULL) { 6527 memcpy(new_key->key + keylen, chunks, 6528 sizeof(*chunks) + num_chunks); 6529 keylen += sizeof(*chunks) + num_chunks; 6530 } 6531 /* append in the HMACs */ 6532 if (hmacs != NULL) { 6533 memcpy(new_key->key + keylen, hmacs, 6534 sizeof(*hmacs) + hmacs_len); 6535 } 6536 } else { 6537 /* failed to get memory for the key */ 6538 return (-34); 6539 } 6540 if (stcb->asoc.authinfo.peer_random != NULL) 6541 sctp_free_key(stcb->asoc.authinfo.peer_random); 6542 stcb->asoc.authinfo.peer_random = new_key; 6543 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid); 6544 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid); 6545 6546 return (0); 6547 } 6548 6549 int 6550 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa, 6551 struct sctp_nets *net) 6552 { 6553 /* make sure the requested primary address exists in the assoc */ 6554 if (net == NULL && sa) 6555 net = sctp_findnet(stcb, sa); 6556 6557 if (net == NULL) { 6558 /* didn't find the requested primary address! */ 6559 return (-1); 6560 } else { 6561 /* set the primary address */ 6562 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { 6563 /* Must be confirmed, so queue to set */ 6564 net->dest_state |= SCTP_ADDR_REQ_PRIMARY; 6565 return (0); 6566 } 6567 stcb->asoc.primary_destination = net; 6568 if (((net->dest_state & SCTP_ADDR_PF) == 0) && 6569 (stcb->asoc.alternate != NULL)) { 6570 sctp_free_remote_addr(stcb->asoc.alternate); 6571 stcb->asoc.alternate = NULL; 6572 } 6573 net = TAILQ_FIRST(&stcb->asoc.nets); 6574 if (net != stcb->asoc.primary_destination) { 6575 /* 6576 * first one on the list is NOT the primary 6577 * sctp_cmpaddr() is much more efficient if the 6578 * primary is the first on the list, make it so. 6579 */ 6580 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 6581 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 6582 } 6583 return (0); 6584 } 6585 } 6586 6587 bool 6588 sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now) 6589 { 6590 struct sctpasochead *head; 6591 struct sctp_tcb *stcb; 6592 6593 SCTP_INP_INFO_LOCK_ASSERT(); 6594 6595 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, SCTP_BASE_INFO(hashasocmark))]; 6596 LIST_FOREACH(stcb, head, sctp_asocs) { 6597 /* 6598 * We choose not to lock anything here. TCB's can't be 6599 * removed since we have the read lock, so they can't be 6600 * freed on us, same thing for the INP. I may be wrong with 6601 * this assumption, but we will go with it for now :-) 6602 */ 6603 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 6604 continue; 6605 } 6606 if (stcb->asoc.my_vtag == tag) { 6607 /* candidate */ 6608 if (stcb->rport != rport) { 6609 continue; 6610 } 6611 if (stcb->sctp_ep->sctp_lport != lport) { 6612 continue; 6613 } 6614 /* The tag is currently used, so don't use it. */ 6615 return (false); 6616 } 6617 } 6618 return (!sctp_is_in_timewait(tag, lport, rport, now->tv_sec)); 6619 } 6620 6621 static void 6622 sctp_drain_mbufs(struct sctp_tcb *stcb) 6623 { 6624 /* 6625 * We must hunt this association for MBUF's past the cumack (i.e. 6626 * out of order data that we can renege on). 6627 */ 6628 struct sctp_association *asoc; 6629 struct sctp_tmit_chunk *chk, *nchk; 6630 uint32_t cumulative_tsn_p1; 6631 struct sctp_queued_to_read *control, *ncontrol; 6632 int cnt, strmat; 6633 uint32_t gap, i; 6634 int fnd = 0; 6635 6636 /* We look for anything larger than the cum-ack + 1 */ 6637 6638 asoc = &stcb->asoc; 6639 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) { 6640 /* none we can reneg on. */ 6641 return; 6642 } 6643 SCTP_STAT_INCR(sctps_protocol_drains_done); 6644 cumulative_tsn_p1 = asoc->cumulative_tsn + 1; 6645 cnt = 0; 6646 /* Ok that was fun, now we will drain all the inbound streams? */ 6647 for (strmat = 0; strmat < asoc->streamincnt; strmat++) { 6648 TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].inqueue, next_instrm, ncontrol) { 6649 #ifdef INVARIANTS 6650 if (control->on_strm_q != SCTP_ON_ORDERED) { 6651 panic("Huh control: %p on_q: %d -- not ordered?", 6652 control, control->on_strm_q); 6653 } 6654 #endif 6655 if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) { 6656 /* Yep it is above cum-ack */ 6657 cnt++; 6658 SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn); 6659 KASSERT(control->length > 0, ("control has zero length")); 6660 if (asoc->size_on_all_streams >= control->length) { 6661 asoc->size_on_all_streams -= control->length; 6662 } else { 6663 #ifdef INVARIANTS 6664 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length); 6665 #else 6666 asoc->size_on_all_streams = 0; 6667 #endif 6668 } 6669 sctp_ucount_decr(asoc->cnt_on_all_streams); 6670 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); 6671 if (control->on_read_q) { 6672 TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next); 6673 control->on_read_q = 0; 6674 } 6675 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, control, next_instrm); 6676 control->on_strm_q = 0; 6677 if (control->data) { 6678 sctp_m_freem(control->data); 6679 control->data = NULL; 6680 } 6681 sctp_free_remote_addr(control->whoFrom); 6682 /* Now its reasm? */ 6683 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) { 6684 cnt++; 6685 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn); 6686 KASSERT(chk->send_size > 0, ("chunk has zero length")); 6687 if (asoc->size_on_reasm_queue >= chk->send_size) { 6688 asoc->size_on_reasm_queue -= chk->send_size; 6689 } else { 6690 #ifdef INVARIANTS 6691 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size); 6692 #else 6693 asoc->size_on_reasm_queue = 0; 6694 #endif 6695 } 6696 sctp_ucount_decr(asoc->cnt_on_reasm_queue); 6697 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); 6698 TAILQ_REMOVE(&control->reasm, chk, sctp_next); 6699 if (chk->data) { 6700 sctp_m_freem(chk->data); 6701 chk->data = NULL; 6702 } 6703 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 6704 } 6705 sctp_free_a_readq(stcb, control); 6706 } 6707 } 6708 TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].uno_inqueue, next_instrm, ncontrol) { 6709 #ifdef INVARIANTS 6710 if (control->on_strm_q != SCTP_ON_UNORDERED) { 6711 panic("Huh control: %p on_q: %d -- not unordered?", 6712 control, control->on_strm_q); 6713 } 6714 #endif 6715 if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) { 6716 /* Yep it is above cum-ack */ 6717 cnt++; 6718 SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn); 6719 KASSERT(control->length > 0, ("control has zero length")); 6720 if (asoc->size_on_all_streams >= control->length) { 6721 asoc->size_on_all_streams -= control->length; 6722 } else { 6723 #ifdef INVARIANTS 6724 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length); 6725 #else 6726 asoc->size_on_all_streams = 0; 6727 #endif 6728 } 6729 sctp_ucount_decr(asoc->cnt_on_all_streams); 6730 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); 6731 if (control->on_read_q) { 6732 TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next); 6733 control->on_read_q = 0; 6734 } 6735 TAILQ_REMOVE(&asoc->strmin[strmat].uno_inqueue, control, next_instrm); 6736 control->on_strm_q = 0; 6737 if (control->data) { 6738 sctp_m_freem(control->data); 6739 control->data = NULL; 6740 } 6741 sctp_free_remote_addr(control->whoFrom); 6742 /* Now its reasm? */ 6743 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) { 6744 cnt++; 6745 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn); 6746 KASSERT(chk->send_size > 0, ("chunk has zero length")); 6747 if (asoc->size_on_reasm_queue >= chk->send_size) { 6748 asoc->size_on_reasm_queue -= chk->send_size; 6749 } else { 6750 #ifdef INVARIANTS 6751 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size); 6752 #else 6753 asoc->size_on_reasm_queue = 0; 6754 #endif 6755 } 6756 sctp_ucount_decr(asoc->cnt_on_reasm_queue); 6757 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); 6758 TAILQ_REMOVE(&control->reasm, chk, sctp_next); 6759 if (chk->data) { 6760 sctp_m_freem(chk->data); 6761 chk->data = NULL; 6762 } 6763 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 6764 } 6765 sctp_free_a_readq(stcb, control); 6766 } 6767 } 6768 } 6769 if (cnt) { 6770 /* We must back down to see what the new highest is */ 6771 for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) { 6772 SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn); 6773 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) { 6774 asoc->highest_tsn_inside_map = i; 6775 fnd = 1; 6776 break; 6777 } 6778 } 6779 if (!fnd) { 6780 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1; 6781 } 6782 6783 /* 6784 * Question, should we go through the delivery queue? The 6785 * only reason things are on here is the app not reading OR 6786 * a p-d-api up. An attacker COULD send enough in to 6787 * initiate the PD-API and then send a bunch of stuff to 6788 * other streams... these would wind up on the delivery 6789 * queue.. and then we would not get to them. But in order 6790 * to do this I then have to back-track and un-deliver 6791 * sequence numbers in streams.. el-yucko. I think for now 6792 * we will NOT look at the delivery queue and leave it to be 6793 * something to consider later. An alternative would be to 6794 * abort the P-D-API with a notification and then deliver 6795 * the data.... Or another method might be to keep track of 6796 * how many times the situation occurs and if we see a 6797 * possible attack underway just abort the association. 6798 */ 6799 #ifdef SCTP_DEBUG 6800 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt); 6801 #endif 6802 /* 6803 * Now do we need to find a new 6804 * asoc->highest_tsn_inside_map? 6805 */ 6806 asoc->last_revoke_count = cnt; 6807 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, 6808 SCTP_FROM_SCTP_PCB + SCTP_LOC_11); 6809 /* sa_ignore NO_NULL_CHK */ 6810 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); 6811 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED); 6812 } 6813 /* 6814 * Another issue, in un-setting the TSN's in the mapping array we 6815 * DID NOT adjust the highest_tsn marker. This will cause one of 6816 * two things to occur. It may cause us to do extra work in checking 6817 * for our mapping array movement. More importantly it may cause us 6818 * to SACK every datagram. This may not be a bad thing though since 6819 * we will recover once we get our cum-ack above and all this stuff 6820 * we dumped recovered. 6821 */ 6822 } 6823 6824 static void 6825 sctp_drain(void) 6826 { 6827 struct epoch_tracker et; 6828 6829 VNET_ITERATOR_DECL(vnet_iter); 6830 6831 NET_EPOCH_ENTER(et); 6832 /* 6833 * We must walk the PCB lists for ALL associations here. The system 6834 * is LOW on MBUF's and needs help. This is where reneging will 6835 * occur. We really hope this does NOT happen! 6836 */ 6837 VNET_LIST_RLOCK_NOSLEEP(); 6838 VNET_FOREACH(vnet_iter) { 6839 CURVNET_SET(vnet_iter); 6840 struct sctp_inpcb *inp; 6841 struct sctp_tcb *stcb; 6842 6843 SCTP_STAT_INCR(sctps_protocol_drain_calls); 6844 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) { 6845 #ifdef VIMAGE 6846 continue; 6847 #else 6848 NET_EPOCH_EXIT(et); 6849 return; 6850 #endif 6851 } 6852 SCTP_INP_INFO_RLOCK(); 6853 LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) { 6854 /* For each endpoint */ 6855 SCTP_INP_RLOCK(inp); 6856 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 6857 /* For each association */ 6858 SCTP_TCB_LOCK(stcb); 6859 sctp_drain_mbufs(stcb); 6860 SCTP_TCB_UNLOCK(stcb); 6861 } 6862 SCTP_INP_RUNLOCK(inp); 6863 } 6864 SCTP_INP_INFO_RUNLOCK(); 6865 CURVNET_RESTORE(); 6866 } 6867 VNET_LIST_RUNLOCK_NOSLEEP(); 6868 NET_EPOCH_EXIT(et); 6869 } 6870 6871 EVENTHANDLER_DEFINE(vm_lowmem, sctp_drain, NULL, LOWMEM_PRI_DEFAULT); 6872 EVENTHANDLER_DEFINE(mbuf_lowmem, sctp_drain, NULL, LOWMEM_PRI_DEFAULT); 6873 6874 /* 6875 * start a new iterator 6876 * iterates through all endpoints and associations based on the pcb_state 6877 * flags and asoc_state. "af" (mandatory) is executed for all matching 6878 * assocs and "ef" (optional) is executed when the iterator completes. 6879 * "inpf" (optional) is executed for each new endpoint as it is being 6880 * iterated through. inpe (optional) is called when the inp completes 6881 * its way through all the stcbs. 6882 */ 6883 int 6884 sctp_initiate_iterator(inp_func inpf, 6885 asoc_func af, 6886 inp_func inpe, 6887 uint32_t pcb_state, 6888 uint32_t pcb_features, 6889 uint32_t asoc_state, 6890 void *argp, 6891 uint32_t argi, 6892 end_func ef, 6893 struct sctp_inpcb *s_inp, 6894 uint8_t chunk_output_off) 6895 { 6896 struct sctp_iterator *it = NULL; 6897 6898 if (af == NULL) { 6899 return (-1); 6900 } 6901 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { 6902 SCTP_PRINTF("%s: abort on initialize being %d\n", __func__, 6903 SCTP_BASE_VAR(sctp_pcb_initialized)); 6904 return (-1); 6905 } 6906 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator), 6907 SCTP_M_ITER); 6908 if (it == NULL) { 6909 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); 6910 return (-1); 6911 } 6912 memset(it, 0, sizeof(*it)); 6913 it->function_assoc = af; 6914 it->function_inp = inpf; 6915 if (inpf) 6916 it->done_current_ep = 0; 6917 else 6918 it->done_current_ep = 1; 6919 it->function_atend = ef; 6920 it->pointer = argp; 6921 it->val = argi; 6922 it->pcb_flags = pcb_state; 6923 it->pcb_features = pcb_features; 6924 it->asoc_state = asoc_state; 6925 it->function_inp_end = inpe; 6926 it->no_chunk_output = chunk_output_off; 6927 it->vn = curvnet; 6928 if (s_inp) { 6929 /* Assume lock is held here */ 6930 it->inp = s_inp; 6931 SCTP_INP_INCR_REF(it->inp); 6932 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP; 6933 } else { 6934 SCTP_INP_INFO_RLOCK(); 6935 it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead)); 6936 if (it->inp) { 6937 SCTP_INP_INCR_REF(it->inp); 6938 } 6939 SCTP_INP_INFO_RUNLOCK(); 6940 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP; 6941 } 6942 SCTP_IPI_ITERATOR_WQ_LOCK(); 6943 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { 6944 SCTP_IPI_ITERATOR_WQ_UNLOCK(); 6945 SCTP_PRINTF("%s: rollback on initialize being %d it=%p\n", __func__, 6946 SCTP_BASE_VAR(sctp_pcb_initialized), it); 6947 SCTP_FREE(it, SCTP_M_ITER); 6948 return (-1); 6949 } 6950 TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); 6951 if (sctp_it_ctl.iterator_running == 0) { 6952 sctp_wakeup_iterator(); 6953 } 6954 SCTP_IPI_ITERATOR_WQ_UNLOCK(); 6955 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */ 6956 return (0); 6957 } 6958 6959 /* 6960 * Atomically add flags to the sctp_flags of an inp. 6961 * To be used when the write lock of the inp is not held. 6962 */ 6963 void 6964 sctp_pcb_add_flags(struct sctp_inpcb *inp, uint32_t flags) 6965 { 6966 uint32_t old_flags, new_flags; 6967 6968 do { 6969 old_flags = inp->sctp_flags; 6970 new_flags = old_flags | flags; 6971 } while (atomic_cmpset_int(&inp->sctp_flags, old_flags, new_flags) == 0); 6972 } 6973