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