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_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); 4236 *error = ENOBUFS; 4237 return (NULL); 4238 } 4239 /* Init all the timers */ 4240 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer); 4241 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer); 4242 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer); 4243 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer); 4244 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer); 4245 SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer); 4246 4247 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist); 4248 /* now file the port under the hash as well */ 4249 if (inp->sctp_tcbhash != NULL) { 4250 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport, 4251 inp->sctp_hashmark)]; 4252 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash); 4253 } 4254 if (initialize_auth_params == SCTP_INITIALIZE_AUTH_PARAMS) { 4255 sctp_initialize_auth_params(inp, stcb); 4256 } 4257 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb); 4258 return (stcb); 4259 } 4260 4261 struct sctp_tcb * 4262 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr, 4263 int *error, uint32_t override_tag, uint32_t initial_tsn, 4264 uint32_t vrf_id, uint16_t o_streams, uint16_t port, 4265 struct thread *p, 4266 int initialize_auth_params) 4267 { 4268 struct sctp_tcb *stcb; 4269 4270 SCTP_INP_INFO_WLOCK(); 4271 SCTP_INP_WLOCK(inp); 4272 stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag, 4273 initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params); 4274 SCTP_INP_INFO_WUNLOCK(); 4275 SCTP_INP_WUNLOCK(inp); 4276 return (stcb); 4277 } 4278 4279 struct sctp_tcb * 4280 sctp_aloc_assoc_connected(struct sctp_inpcb *inp, struct sockaddr *firstaddr, 4281 int *error, uint32_t override_tag, uint32_t initial_tsn, 4282 uint32_t vrf_id, uint16_t o_streams, uint16_t port, 4283 struct thread *p, 4284 int initialize_auth_params) 4285 { 4286 struct sctp_tcb *stcb; 4287 4288 SCTP_INP_INFO_WLOCK(); 4289 SCTP_INP_WLOCK(inp); 4290 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 4291 SCTP_IS_LISTENING(inp)) { 4292 SCTP_INP_INFO_WUNLOCK(); 4293 SCTP_INP_WUNLOCK(inp); 4294 *error = EINVAL; 4295 return (NULL); 4296 } 4297 stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag, 4298 initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params); 4299 SCTP_INP_INFO_WUNLOCK(); 4300 if (stcb != NULL && (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) { 4301 inp->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 4302 soisconnecting(inp->sctp_socket); 4303 } 4304 SCTP_INP_WUNLOCK(inp); 4305 return (stcb); 4306 } 4307 4308 void 4309 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net) 4310 { 4311 struct sctp_inpcb *inp; 4312 struct sctp_association *asoc; 4313 4314 inp = stcb->sctp_ep; 4315 asoc = &stcb->asoc; 4316 asoc->numnets--; 4317 TAILQ_REMOVE(&asoc->nets, net, sctp_next); 4318 if (net == asoc->primary_destination) { 4319 /* Reset primary */ 4320 struct sctp_nets *lnet; 4321 4322 lnet = TAILQ_FIRST(&asoc->nets); 4323 /* 4324 * Mobility adaptation Ideally, if deleted destination is 4325 * the primary, it becomes a fast retransmission trigger by 4326 * the subsequent SET PRIMARY. (by micchie) 4327 */ 4328 if (sctp_is_mobility_feature_on(stcb->sctp_ep, 4329 SCTP_MOBILITY_BASE) || 4330 sctp_is_mobility_feature_on(stcb->sctp_ep, 4331 SCTP_MOBILITY_FASTHANDOFF)) { 4332 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n"); 4333 if (asoc->deleted_primary != NULL) { 4334 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n"); 4335 goto out; 4336 } 4337 asoc->deleted_primary = net; 4338 atomic_add_int(&net->ref_count, 1); 4339 memset(&net->lastsa, 0, sizeof(net->lastsa)); 4340 memset(&net->lastsv, 0, sizeof(net->lastsv)); 4341 sctp_mobility_feature_on(stcb->sctp_ep, 4342 SCTP_MOBILITY_PRIM_DELETED); 4343 sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED, 4344 stcb->sctp_ep, stcb, NULL); 4345 } 4346 out: 4347 /* Try to find a confirmed primary */ 4348 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0); 4349 } 4350 if (net == asoc->last_data_chunk_from) { 4351 /* Reset primary */ 4352 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets); 4353 } 4354 if (net == asoc->last_control_chunk_from) { 4355 /* Clear net */ 4356 asoc->last_control_chunk_from = NULL; 4357 } 4358 if (net == asoc->last_net_cmt_send_started) { 4359 /* Clear net */ 4360 asoc->last_net_cmt_send_started = NULL; 4361 } 4362 if (net == stcb->asoc.alternate) { 4363 sctp_free_remote_addr(stcb->asoc.alternate); 4364 stcb->asoc.alternate = NULL; 4365 } 4366 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net, 4367 SCTP_FROM_SCTP_PCB + SCTP_LOC_9); 4368 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, 4369 SCTP_FROM_SCTP_PCB + SCTP_LOC_10); 4370 net->dest_state |= SCTP_ADDR_BEING_DELETED; 4371 sctp_free_remote_addr(net); 4372 } 4373 4374 /* 4375 * remove a remote endpoint address from an association, it will fail if the 4376 * address does not exist. 4377 */ 4378 int 4379 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr) 4380 { 4381 /* 4382 * Here we need to remove a remote address. This is quite simple, we 4383 * first find it in the list of address for the association 4384 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE 4385 * on that item. Note we do not allow it to be removed if there are 4386 * no other addresses. 4387 */ 4388 struct sctp_association *asoc; 4389 struct sctp_nets *net, *nnet; 4390 4391 asoc = &stcb->asoc; 4392 4393 /* locate the address */ 4394 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) { 4395 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) { 4396 continue; 4397 } 4398 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr, 4399 remaddr)) { 4400 /* we found the guy */ 4401 if (asoc->numnets < 2) { 4402 /* Must have at LEAST two remote addresses */ 4403 return (-1); 4404 } else { 4405 sctp_remove_net(stcb, net); 4406 return (0); 4407 } 4408 } 4409 } 4410 /* not found. */ 4411 return (-2); 4412 } 4413 4414 static bool 4415 sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport, time_t now) 4416 { 4417 struct sctpvtaghead *chain; 4418 struct sctp_tagblock *twait_block; 4419 int i; 4420 4421 SCTP_INP_INFO_LOCK_ASSERT(); 4422 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; 4423 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { 4424 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { 4425 if ((twait_block->vtag_block[i].tv_sec_at_expire >= now) && 4426 (twait_block->vtag_block[i].v_tag == tag) && 4427 (twait_block->vtag_block[i].lport == lport) && 4428 (twait_block->vtag_block[i].rport == rport)) { 4429 return (true); 4430 } 4431 } 4432 } 4433 return (false); 4434 } 4435 4436 static void 4437 sctp_set_vtag_block(struct sctp_timewait *vtag_block, time_t time, 4438 uint32_t tag, uint16_t lport, uint16_t rport) 4439 { 4440 vtag_block->tv_sec_at_expire = time; 4441 vtag_block->v_tag = tag; 4442 vtag_block->lport = lport; 4443 vtag_block->rport = rport; 4444 } 4445 4446 static void 4447 sctp_add_vtag_to_timewait(uint32_t tag, uint16_t lport, uint16_t rport) 4448 { 4449 struct sctpvtaghead *chain; 4450 struct sctp_tagblock *twait_block; 4451 struct timeval now; 4452 time_t time; 4453 int i; 4454 bool set; 4455 4456 SCTP_INP_INFO_WLOCK_ASSERT(); 4457 (void)SCTP_GETTIME_TIMEVAL(&now); 4458 time = now.tv_sec + SCTP_BASE_SYSCTL(sctp_vtag_time_wait); 4459 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; 4460 set = false; 4461 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { 4462 /* Block(s) present, lets find space, and expire on the fly */ 4463 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { 4464 if ((twait_block->vtag_block[i].v_tag == 0) && !set) { 4465 sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport); 4466 set = true; 4467 continue; 4468 } 4469 if ((twait_block->vtag_block[i].v_tag != 0) && 4470 (twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) { 4471 if (set) { 4472 /* Audit expires this guy */ 4473 sctp_set_vtag_block(twait_block->vtag_block + i, 0, 0, 0, 0); 4474 } else { 4475 /* Reuse it for the new tag */ 4476 sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport); 4477 set = true; 4478 } 4479 } 4480 } 4481 if (set) { 4482 /* 4483 * We only do up to the block where we can place our 4484 * tag for audits 4485 */ 4486 break; 4487 } 4488 } 4489 /* Need to add a new block to chain */ 4490 if (!set) { 4491 SCTP_MALLOC(twait_block, struct sctp_tagblock *, 4492 sizeof(struct sctp_tagblock), SCTP_M_TIMW); 4493 if (twait_block == NULL) { 4494 return; 4495 } 4496 memset(twait_block, 0, sizeof(struct sctp_tagblock)); 4497 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock); 4498 sctp_set_vtag_block(twait_block->vtag_block, time, tag, lport, rport); 4499 } 4500 } 4501 4502 void 4503 sctp_clean_up_stream(struct sctp_tcb *stcb, struct sctp_readhead *rh) 4504 { 4505 struct sctp_tmit_chunk *chk, *nchk; 4506 struct sctp_queued_to_read *control, *ncontrol; 4507 4508 TAILQ_FOREACH_SAFE(control, rh, next_instrm, ncontrol) { 4509 TAILQ_REMOVE(rh, control, next_instrm); 4510 control->on_strm_q = 0; 4511 if (control->on_read_q == 0) { 4512 sctp_free_remote_addr(control->whoFrom); 4513 if (control->data) { 4514 sctp_m_freem(control->data); 4515 control->data = NULL; 4516 } 4517 } 4518 /* Reassembly free? */ 4519 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) { 4520 TAILQ_REMOVE(&control->reasm, chk, sctp_next); 4521 if (chk->data) { 4522 sctp_m_freem(chk->data); 4523 chk->data = NULL; 4524 } 4525 if (chk->holds_key_ref) 4526 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); 4527 sctp_free_remote_addr(chk->whoTo); 4528 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); 4529 SCTP_DECR_CHK_COUNT(); 4530 /* sa_ignore FREED_MEMORY */ 4531 } 4532 /* 4533 * We don't free the address here since all the net's were 4534 * freed above. 4535 */ 4536 if (control->on_read_q == 0) { 4537 sctp_free_a_readq(stcb, control); 4538 } 4539 } 4540 } 4541 4542 /*- 4543 * Free the association after un-hashing the remote port. This 4544 * function ALWAYS returns holding NO LOCK on the stcb. It DOES 4545 * expect that the input to this function IS a locked TCB. 4546 * It will return 0, if it did NOT destroy the association (instead 4547 * it unlocks it. It will return NON-zero if it either destroyed the 4548 * association OR the association is already destroyed. 4549 */ 4550 int 4551 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location) 4552 { 4553 int i; 4554 struct sctp_association *asoc; 4555 struct sctp_nets *net, *nnet; 4556 struct sctp_laddr *laddr, *naddr; 4557 struct sctp_tmit_chunk *chk, *nchk; 4558 struct sctp_asconf_addr *aparam, *naparam; 4559 struct sctp_asconf_ack *aack, *naack; 4560 struct sctp_stream_reset_list *strrst, *nstrrst; 4561 struct sctp_queued_to_read *sq, *nsq; 4562 struct sctp_stream_queue_pending *sp, *nsp; 4563 sctp_sharedkey_t *shared_key, *nshared_key; 4564 struct socket *so; 4565 4566 /* first, lets purge the entry from the hash table. */ 4567 SCTP_TCB_LOCK_ASSERT(stcb); 4568 4569 #ifdef SCTP_LOG_CLOSING 4570 sctp_log_closing(inp, stcb, 6); 4571 #endif 4572 if (stcb->asoc.state == 0) { 4573 #ifdef SCTP_LOG_CLOSING 4574 sctp_log_closing(inp, NULL, 7); 4575 #endif 4576 /* there is no asoc, really TSNH :-0 */ 4577 return (1); 4578 } 4579 if (stcb->asoc.alternate) { 4580 sctp_free_remote_addr(stcb->asoc.alternate); 4581 stcb->asoc.alternate = NULL; 4582 } 4583 /* TEMP CODE */ 4584 if (stcb->freed_from_where == 0) { 4585 /* Only record the first place free happened from */ 4586 stcb->freed_from_where = from_location; 4587 } 4588 /* TEMP CODE */ 4589 4590 asoc = &stcb->asoc; 4591 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 4592 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) 4593 /* nothing around */ 4594 so = NULL; 4595 else 4596 so = inp->sctp_socket; 4597 4598 /* 4599 * We used timer based freeing if a reader or writer is in the way. 4600 * So we first check if we are actually being called from a timer, 4601 * if so we abort early if a reader or writer is still in the way. 4602 */ 4603 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) && 4604 (from_inpcbfree == SCTP_NORMAL_PROC)) { 4605 /* 4606 * is it the timer driving us? if so are the reader/writers 4607 * gone? 4608 */ 4609 if (stcb->asoc.refcnt) { 4610 /* nope, reader or writer in the way */ 4611 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); 4612 /* no asoc destroyed */ 4613 SCTP_TCB_UNLOCK(stcb); 4614 #ifdef SCTP_LOG_CLOSING 4615 sctp_log_closing(inp, stcb, 8); 4616 #endif 4617 return (0); 4618 } 4619 } 4620 /* Now clean up any other timers */ 4621 sctp_stop_association_timers(stcb, false); 4622 /* Now the read queue needs to be cleaned up (only once) */ 4623 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) { 4624 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_ABOUT_TO_BE_FREED); 4625 SCTP_INP_READ_LOCK(inp); 4626 TAILQ_FOREACH(sq, &inp->read_queue, next) { 4627 if (sq->stcb == stcb) { 4628 sq->do_not_ref_stcb = 1; 4629 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn; 4630 /* 4631 * If there is no end, there never will be 4632 * now. 4633 */ 4634 if (sq->end_added == 0) { 4635 /* Held for PD-API, clear that. */ 4636 sq->pdapi_aborted = 1; 4637 sq->held_length = 0; 4638 if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) { 4639 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION, 4640 stcb, 4641 SCTP_PARTIAL_DELIVERY_ABORTED, 4642 (void *)sq, 4643 SCTP_SO_LOCKED); 4644 } 4645 /* Add an end to wake them */ 4646 sq->end_added = 1; 4647 } 4648 } 4649 } 4650 SCTP_INP_READ_UNLOCK(inp); 4651 if (stcb->block_entry) { 4652 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET); 4653 stcb->block_entry->error = ECONNRESET; 4654 stcb->block_entry = NULL; 4655 } 4656 } 4657 if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) { 4658 /* 4659 * Someone holds a reference OR the socket is unaccepted 4660 * yet. 4661 */ 4662 if ((stcb->asoc.refcnt) || 4663 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 4664 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) { 4665 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE); 4666 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); 4667 } 4668 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 4669 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) 4670 /* nothing around */ 4671 so = NULL; 4672 if (so) { 4673 /* Wake any reader/writers */ 4674 sctp_sorwakeup(inp, so); 4675 sctp_sowwakeup(inp, so); 4676 } 4677 SCTP_TCB_UNLOCK(stcb); 4678 4679 #ifdef SCTP_LOG_CLOSING 4680 sctp_log_closing(inp, stcb, 9); 4681 #endif 4682 /* no asoc destroyed */ 4683 return (0); 4684 } 4685 #ifdef SCTP_LOG_CLOSING 4686 sctp_log_closing(inp, stcb, 10); 4687 #endif 4688 /* 4689 * When I reach here, no others want to kill the assoc yet.. and I 4690 * own the lock. Now its possible an abort comes in when I do the 4691 * lock exchange below to grab all the locks to do the final take 4692 * out. to prevent this we increment the count, which will start a 4693 * timer and blow out above thus assuring us that we hold exclusive 4694 * killing of the asoc. Note that after getting back the TCB lock we 4695 * will go ahead and increment the counter back up and stop any 4696 * timer a passing stranger may have started :-S 4697 */ 4698 if (from_inpcbfree == SCTP_NORMAL_PROC) { 4699 atomic_add_int(&stcb->asoc.refcnt, 1); 4700 4701 SCTP_TCB_UNLOCK(stcb); 4702 SCTP_INP_INFO_WLOCK(); 4703 SCTP_INP_WLOCK(inp); 4704 SCTP_TCB_LOCK(stcb); 4705 } 4706 /* Double check the GONE flag */ 4707 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 4708 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) 4709 /* nothing around */ 4710 so = NULL; 4711 4712 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 4713 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 4714 /* 4715 * For TCP type we need special handling when we are 4716 * connected. We also include the peel'ed off ones to. 4717 */ 4718 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 4719 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED; 4720 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED; 4721 if (so) { 4722 SOCKBUF_LOCK(&so->so_rcv); 4723 so->so_state &= ~(SS_ISCONNECTING | 4724 SS_ISDISCONNECTING | 4725 SS_ISCONNECTED); 4726 so->so_state |= SS_ISDISCONNECTED; 4727 socantrcvmore_locked(so); 4728 socantsendmore(so); 4729 sctp_sowwakeup(inp, so); 4730 sctp_sorwakeup(inp, so); 4731 SCTP_SOWAKEUP(so); 4732 } 4733 } 4734 } 4735 4736 /* 4737 * Make it invalid too, that way if its about to run it will abort 4738 * and return. 4739 */ 4740 /* re-increment the lock */ 4741 if (from_inpcbfree == SCTP_NORMAL_PROC) { 4742 atomic_subtract_int(&stcb->asoc.refcnt, 1); 4743 } 4744 if (stcb->asoc.refcnt) { 4745 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE); 4746 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); 4747 if (from_inpcbfree == SCTP_NORMAL_PROC) { 4748 SCTP_INP_INFO_WUNLOCK(); 4749 SCTP_INP_WUNLOCK(inp); 4750 } 4751 SCTP_TCB_UNLOCK(stcb); 4752 return (0); 4753 } 4754 asoc->state = 0; 4755 if (inp->sctp_tcbhash) { 4756 LIST_REMOVE(stcb, sctp_tcbhash); 4757 } 4758 if (stcb->asoc.in_asocid_hash) { 4759 LIST_REMOVE(stcb, sctp_tcbasocidhash); 4760 } 4761 if (inp->sctp_socket == NULL) { 4762 stcb->sctp_socket = NULL; 4763 } 4764 /* Now lets remove it from the list of ALL associations in the EP */ 4765 LIST_REMOVE(stcb, sctp_tcblist); 4766 if (from_inpcbfree == SCTP_NORMAL_PROC) { 4767 SCTP_INP_INCR_REF(inp); 4768 SCTP_INP_WUNLOCK(inp); 4769 } 4770 /* pull from vtag hash */ 4771 LIST_REMOVE(stcb, sctp_asocs); 4772 sctp_add_vtag_to_timewait(asoc->my_vtag, inp->sctp_lport, stcb->rport); 4773 4774 /* 4775 * Now restop the timers to be sure this is paranoia at is finest! 4776 */ 4777 sctp_stop_association_timers(stcb, true); 4778 4779 /* 4780 * The chunk lists and such SHOULD be empty but we check them just 4781 * in case. 4782 */ 4783 /* anything on the wheel needs to be removed */ 4784 for (i = 0; i < asoc->streamoutcnt; i++) { 4785 struct sctp_stream_out *outs; 4786 4787 outs = &asoc->strmout[i]; 4788 /* now clean up any chunks here */ 4789 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { 4790 atomic_subtract_int(&asoc->stream_queue_cnt, 1); 4791 TAILQ_REMOVE(&outs->outqueue, sp, next); 4792 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp); 4793 sctp_free_spbufspace(stcb, asoc, sp); 4794 if (sp->data) { 4795 if (so) { 4796 /* Still an open socket - report */ 4797 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb, 4798 0, (void *)sp, SCTP_SO_LOCKED); 4799 } 4800 if (sp->data) { 4801 sctp_m_freem(sp->data); 4802 sp->data = NULL; 4803 sp->tail_mbuf = NULL; 4804 sp->length = 0; 4805 } 4806 } 4807 if (sp->net) { 4808 sctp_free_remote_addr(sp->net); 4809 sp->net = NULL; 4810 } 4811 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED); 4812 } 4813 } 4814 /* sa_ignore FREED_MEMORY */ 4815 TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) { 4816 TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp); 4817 SCTP_FREE(strrst, SCTP_M_STRESET); 4818 } 4819 TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) { 4820 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next); 4821 if (sq->data) { 4822 sctp_m_freem(sq->data); 4823 sq->data = NULL; 4824 } 4825 sctp_free_remote_addr(sq->whoFrom); 4826 sq->whoFrom = NULL; 4827 sq->stcb = NULL; 4828 /* Free the ctl entry */ 4829 sctp_free_a_readq(stcb, sq); 4830 /* sa_ignore FREED_MEMORY */ 4831 } 4832 TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) { 4833 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next); 4834 if (chk->data) { 4835 sctp_m_freem(chk->data); 4836 chk->data = NULL; 4837 } 4838 if (chk->holds_key_ref) 4839 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); 4840 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); 4841 SCTP_DECR_CHK_COUNT(); 4842 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); 4843 asoc->free_chunk_cnt--; 4844 /* sa_ignore FREED_MEMORY */ 4845 } 4846 /* pending send queue SHOULD be empty */ 4847 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { 4848 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) { 4849 asoc->strmout[chk->rec.data.sid].chunks_on_queues--; 4850 #ifdef INVARIANTS 4851 } else { 4852 panic("No chunks on the queues for sid %u.", chk->rec.data.sid); 4853 #endif 4854 } 4855 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); 4856 if (chk->data) { 4857 if (so) { 4858 /* Still a socket? */ 4859 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 4860 0, chk, SCTP_SO_LOCKED); 4861 } 4862 if (chk->data) { 4863 sctp_m_freem(chk->data); 4864 chk->data = NULL; 4865 } 4866 } 4867 if (chk->holds_key_ref) 4868 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); 4869 if (chk->whoTo) { 4870 sctp_free_remote_addr(chk->whoTo); 4871 chk->whoTo = NULL; 4872 } 4873 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); 4874 SCTP_DECR_CHK_COUNT(); 4875 /* sa_ignore FREED_MEMORY */ 4876 } 4877 /* sent queue SHOULD be empty */ 4878 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) { 4879 if (chk->sent != SCTP_DATAGRAM_NR_ACKED) { 4880 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) { 4881 asoc->strmout[chk->rec.data.sid].chunks_on_queues--; 4882 #ifdef INVARIANTS 4883 } else { 4884 panic("No chunks on the queues for sid %u.", chk->rec.data.sid); 4885 #endif 4886 } 4887 } 4888 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next); 4889 if (chk->data) { 4890 if (so) { 4891 /* Still a socket? */ 4892 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 4893 0, chk, SCTP_SO_LOCKED); 4894 } 4895 if (chk->data) { 4896 sctp_m_freem(chk->data); 4897 chk->data = NULL; 4898 } 4899 } 4900 if (chk->holds_key_ref) 4901 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); 4902 sctp_free_remote_addr(chk->whoTo); 4903 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); 4904 SCTP_DECR_CHK_COUNT(); 4905 /* sa_ignore FREED_MEMORY */ 4906 } 4907 #ifdef INVARIANTS 4908 for (i = 0; i < stcb->asoc.streamoutcnt; i++) { 4909 if (stcb->asoc.strmout[i].chunks_on_queues > 0) { 4910 panic("%u chunks left for stream %u.", stcb->asoc.strmout[i].chunks_on_queues, i); 4911 } 4912 } 4913 #endif 4914 /* control queue MAY not be empty */ 4915 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { 4916 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); 4917 if (chk->data) { 4918 sctp_m_freem(chk->data); 4919 chk->data = NULL; 4920 } 4921 if (chk->holds_key_ref) 4922 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); 4923 sctp_free_remote_addr(chk->whoTo); 4924 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); 4925 SCTP_DECR_CHK_COUNT(); 4926 /* sa_ignore FREED_MEMORY */ 4927 } 4928 /* ASCONF queue MAY not be empty */ 4929 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) { 4930 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next); 4931 if (chk->data) { 4932 sctp_m_freem(chk->data); 4933 chk->data = NULL; 4934 } 4935 if (chk->holds_key_ref) 4936 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); 4937 sctp_free_remote_addr(chk->whoTo); 4938 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); 4939 SCTP_DECR_CHK_COUNT(); 4940 /* sa_ignore FREED_MEMORY */ 4941 } 4942 if (asoc->mapping_array) { 4943 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); 4944 asoc->mapping_array = NULL; 4945 } 4946 if (asoc->nr_mapping_array) { 4947 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP); 4948 asoc->nr_mapping_array = NULL; 4949 } 4950 /* the stream outs */ 4951 if (asoc->strmout) { 4952 SCTP_FREE(asoc->strmout, SCTP_M_STRMO); 4953 asoc->strmout = NULL; 4954 } 4955 asoc->strm_realoutsize = asoc->streamoutcnt = 0; 4956 if (asoc->strmin) { 4957 for (i = 0; i < asoc->streamincnt; i++) { 4958 sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue); 4959 sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue); 4960 } 4961 SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 4962 asoc->strmin = NULL; 4963 } 4964 asoc->streamincnt = 0; 4965 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) { 4966 #ifdef INVARIANTS 4967 if (SCTP_BASE_INFO(ipi_count_raddr) == 0) { 4968 panic("no net's left alloc'ed, or list points to itself"); 4969 } 4970 #endif 4971 TAILQ_REMOVE(&asoc->nets, net, sctp_next); 4972 sctp_free_remote_addr(net); 4973 } 4974 LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) { 4975 /* sa_ignore FREED_MEMORY */ 4976 sctp_remove_laddr(laddr); 4977 } 4978 4979 /* pending asconf (address) parameters */ 4980 TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) { 4981 /* sa_ignore FREED_MEMORY */ 4982 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next); 4983 SCTP_FREE(aparam, SCTP_M_ASC_ADDR); 4984 } 4985 TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) { 4986 /* sa_ignore FREED_MEMORY */ 4987 TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next); 4988 if (aack->data != NULL) { 4989 sctp_m_freem(aack->data); 4990 } 4991 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack); 4992 } 4993 /* clean up auth stuff */ 4994 if (asoc->local_hmacs) 4995 sctp_free_hmaclist(asoc->local_hmacs); 4996 if (asoc->peer_hmacs) 4997 sctp_free_hmaclist(asoc->peer_hmacs); 4998 4999 if (asoc->local_auth_chunks) 5000 sctp_free_chunklist(asoc->local_auth_chunks); 5001 if (asoc->peer_auth_chunks) 5002 sctp_free_chunklist(asoc->peer_auth_chunks); 5003 5004 sctp_free_authinfo(&asoc->authinfo); 5005 5006 LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) { 5007 LIST_REMOVE(shared_key, next); 5008 sctp_free_sharedkey(shared_key); 5009 /* sa_ignore FREED_MEMORY */ 5010 } 5011 5012 /* Insert new items here :> */ 5013 5014 /* Get rid of LOCK */ 5015 SCTP_TCB_UNLOCK(stcb); 5016 SCTP_TCB_LOCK_DESTROY(stcb); 5017 if (from_inpcbfree == SCTP_NORMAL_PROC) { 5018 SCTP_INP_INFO_WUNLOCK(); 5019 SCTP_INP_RLOCK(inp); 5020 } 5021 #ifdef SCTP_TRACK_FREED_ASOCS 5022 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5023 /* now clean up the tasoc itself */ 5024 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); 5025 SCTP_DECR_ASOC_COUNT(); 5026 } else { 5027 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist); 5028 } 5029 #else 5030 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); 5031 SCTP_DECR_ASOC_COUNT(); 5032 #endif 5033 if (from_inpcbfree == SCTP_NORMAL_PROC) { 5034 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5035 /* 5036 * If its NOT the inp_free calling us AND sctp_close 5037 * as been called, we call back... 5038 */ 5039 SCTP_INP_RUNLOCK(inp); 5040 /* 5041 * This will start the kill timer (if we are the 5042 * last one) since we hold an increment yet. But 5043 * this is the only safe way to do this since 5044 * otherwise if the socket closes at the same time 5045 * we are here we might collide in the cleanup. 5046 */ 5047 sctp_inpcb_free(inp, 5048 SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE, 5049 SCTP_CALLED_DIRECTLY_NOCMPSET); 5050 SCTP_INP_DECR_REF(inp); 5051 } else { 5052 /* The socket is still open. */ 5053 SCTP_INP_DECR_REF(inp); 5054 SCTP_INP_RUNLOCK(inp); 5055 } 5056 } 5057 /* destroyed the asoc */ 5058 #ifdef SCTP_LOG_CLOSING 5059 sctp_log_closing(inp, NULL, 11); 5060 #endif 5061 return (1); 5062 } 5063 5064 /* 5065 * determine if a destination is "reachable" based upon the addresses bound 5066 * to the current endpoint (e.g. only v4 or v6 currently bound) 5067 */ 5068 /* 5069 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use 5070 * assoc level v4/v6 flags, as the assoc *may* not have the same address 5071 * types bound as its endpoint 5072 */ 5073 int 5074 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr) 5075 { 5076 struct sctp_inpcb *inp; 5077 int answer; 5078 5079 /* 5080 * No locks here, the TCB, in all cases is already locked and an 5081 * assoc is up. There is either a INP lock by the caller applied (in 5082 * asconf case when deleting an address) or NOT in the HB case, 5083 * however if HB then the INP increment is up and the INP will not 5084 * be removed (on top of the fact that we have a TCB lock). So we 5085 * only want to read the sctp_flags, which is either bound-all or 5086 * not.. no protection needed since once an assoc is up you can't be 5087 * changing your binding. 5088 */ 5089 inp = stcb->sctp_ep; 5090 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 5091 /* if bound all, destination is not restricted */ 5092 /* 5093 * RRS: Question during lock work: Is this correct? If you 5094 * are bound-all you still might need to obey the V4--V6 5095 * flags??? IMO this bound-all stuff needs to be removed! 5096 */ 5097 return (1); 5098 } 5099 /* NOTE: all "scope" checks are done when local addresses are added */ 5100 switch (destaddr->sa_family) { 5101 #ifdef INET6 5102 case AF_INET6: 5103 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6; 5104 break; 5105 #endif 5106 #ifdef INET 5107 case AF_INET: 5108 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4; 5109 break; 5110 #endif 5111 default: 5112 /* invalid family, so it's unreachable */ 5113 answer = 0; 5114 break; 5115 } 5116 return (answer); 5117 } 5118 5119 /* 5120 * update the inp_vflags on an endpoint 5121 */ 5122 static void 5123 sctp_update_ep_vflag(struct sctp_inpcb *inp) 5124 { 5125 struct sctp_laddr *laddr; 5126 5127 /* first clear the flag */ 5128 inp->ip_inp.inp.inp_vflag = 0; 5129 /* set the flag based on addresses on the ep list */ 5130 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 5131 if (laddr->ifa == NULL) { 5132 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", 5133 __func__); 5134 continue; 5135 } 5136 5137 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { 5138 continue; 5139 } 5140 switch (laddr->ifa->address.sa.sa_family) { 5141 #ifdef INET6 5142 case AF_INET6: 5143 inp->ip_inp.inp.inp_vflag |= INP_IPV6; 5144 break; 5145 #endif 5146 #ifdef INET 5147 case AF_INET: 5148 inp->ip_inp.inp.inp_vflag |= INP_IPV4; 5149 break; 5150 #endif 5151 default: 5152 break; 5153 } 5154 } 5155 } 5156 5157 /* 5158 * Add the address to the endpoint local address list There is nothing to be 5159 * done if we are bound to all addresses 5160 */ 5161 void 5162 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action) 5163 { 5164 struct sctp_laddr *laddr; 5165 struct sctp_tcb *stcb; 5166 int fnd, error = 0; 5167 5168 fnd = 0; 5169 5170 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 5171 /* You are already bound to all. You have it already */ 5172 return; 5173 } 5174 #ifdef INET6 5175 if (ifa->address.sa.sa_family == AF_INET6) { 5176 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { 5177 /* Can't bind a non-useable addr. */ 5178 return; 5179 } 5180 } 5181 #endif 5182 /* first, is it already present? */ 5183 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 5184 if (laddr->ifa == ifa) { 5185 fnd = 1; 5186 break; 5187 } 5188 } 5189 5190 if (fnd == 0) { 5191 /* Not in the ep list */ 5192 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action); 5193 if (error != 0) 5194 return; 5195 inp->laddr_count++; 5196 /* update inp_vflag flags */ 5197 switch (ifa->address.sa.sa_family) { 5198 #ifdef INET6 5199 case AF_INET6: 5200 inp->ip_inp.inp.inp_vflag |= INP_IPV6; 5201 break; 5202 #endif 5203 #ifdef INET 5204 case AF_INET: 5205 inp->ip_inp.inp.inp_vflag |= INP_IPV4; 5206 break; 5207 #endif 5208 default: 5209 break; 5210 } 5211 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 5212 sctp_add_local_addr_restricted(stcb, ifa); 5213 } 5214 } 5215 return; 5216 } 5217 5218 /* 5219 * select a new (hopefully reachable) destination net (should only be used 5220 * when we deleted an ep addr that is the only usable source address to reach 5221 * the destination net) 5222 */ 5223 static void 5224 sctp_select_primary_destination(struct sctp_tcb *stcb) 5225 { 5226 struct sctp_nets *net; 5227 5228 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 5229 /* for now, we'll just pick the first reachable one we find */ 5230 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) 5231 continue; 5232 if (sctp_destination_is_reachable(stcb, 5233 (struct sockaddr *)&net->ro._l_addr)) { 5234 /* found a reachable destination */ 5235 stcb->asoc.primary_destination = net; 5236 } 5237 } 5238 /* I can't there from here! ...we're gonna die shortly... */ 5239 } 5240 5241 /* 5242 * Delete the address from the endpoint local address list. There is nothing 5243 * to be done if we are bound to all addresses 5244 */ 5245 void 5246 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa) 5247 { 5248 struct sctp_laddr *laddr; 5249 int fnd; 5250 5251 fnd = 0; 5252 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 5253 /* You are already bound to all. You have it already */ 5254 return; 5255 } 5256 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 5257 if (laddr->ifa == ifa) { 5258 fnd = 1; 5259 break; 5260 } 5261 } 5262 if (fnd && (inp->laddr_count < 2)) { 5263 /* can't delete unless there are at LEAST 2 addresses */ 5264 return; 5265 } 5266 if (fnd) { 5267 /* 5268 * clean up any use of this address go through our 5269 * associations and clear any last_used_address that match 5270 * this one for each assoc, see if a new primary_destination 5271 * is needed 5272 */ 5273 struct sctp_tcb *stcb; 5274 5275 /* clean up "next_addr_touse" */ 5276 if (inp->next_addr_touse == laddr) 5277 /* delete this address */ 5278 inp->next_addr_touse = NULL; 5279 5280 /* clean up "last_used_address" */ 5281 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 5282 struct sctp_nets *net; 5283 5284 SCTP_TCB_LOCK(stcb); 5285 if (stcb->asoc.last_used_address == laddr) 5286 /* delete this address */ 5287 stcb->asoc.last_used_address = NULL; 5288 /* 5289 * Now spin through all the nets and purge any ref 5290 * to laddr 5291 */ 5292 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 5293 if (net->ro._s_addr == laddr->ifa) { 5294 /* Yep, purge src address selected */ 5295 RO_NHFREE(&net->ro); 5296 sctp_free_ifa(net->ro._s_addr); 5297 net->ro._s_addr = NULL; 5298 net->src_addr_selected = 0; 5299 } 5300 } 5301 SCTP_TCB_UNLOCK(stcb); 5302 } /* for each tcb */ 5303 /* remove it from the ep list */ 5304 sctp_remove_laddr(laddr); 5305 inp->laddr_count--; 5306 /* update inp_vflag flags */ 5307 sctp_update_ep_vflag(inp); 5308 } 5309 return; 5310 } 5311 5312 /* 5313 * Add the address to the TCB local address restricted list. 5314 * This is a "pending" address list (eg. addresses waiting for an 5315 * ASCONF-ACK response) and cannot be used as a valid source address. 5316 */ 5317 void 5318 sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) 5319 { 5320 struct sctp_laddr *laddr; 5321 struct sctpladdr *list; 5322 5323 /* 5324 * Assumes TCB is locked.. and possibly the INP. May need to 5325 * confirm/fix that if we need it and is not the case. 5326 */ 5327 list = &stcb->asoc.sctp_restricted_addrs; 5328 5329 #ifdef INET6 5330 if (ifa->address.sa.sa_family == AF_INET6) { 5331 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { 5332 /* Can't bind a non-existent addr. */ 5333 return; 5334 } 5335 } 5336 #endif 5337 /* does the address already exist? */ 5338 LIST_FOREACH(laddr, list, sctp_nxt_addr) { 5339 if (laddr->ifa == ifa) { 5340 return; 5341 } 5342 } 5343 5344 /* add to the list */ 5345 (void)sctp_insert_laddr(list, ifa, 0); 5346 return; 5347 } 5348 5349 /* 5350 * Remove a local address from the TCB local address restricted list 5351 */ 5352 void 5353 sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) 5354 { 5355 struct sctp_inpcb *inp; 5356 struct sctp_laddr *laddr; 5357 5358 /* 5359 * This is called by asconf work. It is assumed that a) The TCB is 5360 * locked and b) The INP is locked. This is true in as much as I can 5361 * trace through the entry asconf code where I did these locks. 5362 * Again, the ASCONF code is a bit different in that it does lock 5363 * the INP during its work often times. This must be since we don't 5364 * want other proc's looking up things while what they are looking 5365 * up is changing :-D 5366 */ 5367 5368 inp = stcb->sctp_ep; 5369 /* if subset bound and don't allow ASCONF's, can't delete last */ 5370 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && 5371 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) { 5372 if (stcb->sctp_ep->laddr_count < 2) { 5373 /* can't delete last address */ 5374 return; 5375 } 5376 } 5377 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) { 5378 /* remove the address if it exists */ 5379 if (laddr->ifa == NULL) 5380 continue; 5381 if (laddr->ifa == ifa) { 5382 sctp_remove_laddr(laddr); 5383 return; 5384 } 5385 } 5386 5387 /* address not found! */ 5388 return; 5389 } 5390 5391 /* sysctl */ 5392 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC; 5393 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR; 5394 5395 #if defined(SCTP_MCORE_INPUT) && defined(SMP) 5396 struct sctp_mcore_ctrl *sctp_mcore_workers = NULL; 5397 int *sctp_cpuarry = NULL; 5398 5399 void 5400 sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use) 5401 { 5402 /* Queue a packet to a processor for the specified core */ 5403 struct sctp_mcore_queue *qent; 5404 struct sctp_mcore_ctrl *wkq; 5405 int need_wake = 0; 5406 5407 if (sctp_mcore_workers == NULL) { 5408 /* Something went way bad during setup */ 5409 sctp_input_with_port(m, off, 0); 5410 return; 5411 } 5412 SCTP_MALLOC(qent, struct sctp_mcore_queue *, 5413 (sizeof(struct sctp_mcore_queue)), 5414 SCTP_M_MCORE); 5415 if (qent == NULL) { 5416 /* This is trouble */ 5417 sctp_input_with_port(m, off, 0); 5418 return; 5419 } 5420 qent->vn = curvnet; 5421 qent->m = m; 5422 qent->off = off; 5423 qent->v6 = 0; 5424 wkq = &sctp_mcore_workers[cpu_to_use]; 5425 SCTP_MCORE_QLOCK(wkq); 5426 5427 TAILQ_INSERT_TAIL(&wkq->que, qent, next); 5428 if (wkq->running == 0) { 5429 need_wake = 1; 5430 } 5431 SCTP_MCORE_QUNLOCK(wkq); 5432 if (need_wake) { 5433 wakeup(&wkq->running); 5434 } 5435 } 5436 5437 static void 5438 sctp_mcore_thread(void *arg) 5439 { 5440 5441 struct sctp_mcore_ctrl *wkq; 5442 struct sctp_mcore_queue *qent; 5443 5444 wkq = (struct sctp_mcore_ctrl *)arg; 5445 struct mbuf *m; 5446 int off, v6; 5447 5448 /* Wait for first tickle */ 5449 SCTP_MCORE_LOCK(wkq); 5450 wkq->running = 0; 5451 msleep(&wkq->running, 5452 &wkq->core_mtx, 5453 0, "wait for pkt", 0); 5454 SCTP_MCORE_UNLOCK(wkq); 5455 5456 /* Bind to our cpu */ 5457 thread_lock(curthread); 5458 sched_bind(curthread, wkq->cpuid); 5459 thread_unlock(curthread); 5460 5461 /* Now lets start working */ 5462 SCTP_MCORE_LOCK(wkq); 5463 /* Now grab lock and go */ 5464 for (;;) { 5465 SCTP_MCORE_QLOCK(wkq); 5466 skip_sleep: 5467 wkq->running = 1; 5468 qent = TAILQ_FIRST(&wkq->que); 5469 if (qent) { 5470 TAILQ_REMOVE(&wkq->que, qent, next); 5471 SCTP_MCORE_QUNLOCK(wkq); 5472 CURVNET_SET(qent->vn); 5473 m = qent->m; 5474 off = qent->off; 5475 v6 = qent->v6; 5476 SCTP_FREE(qent, SCTP_M_MCORE); 5477 if (v6 == 0) { 5478 sctp_input_with_port(m, off, 0); 5479 } else { 5480 SCTP_PRINTF("V6 not yet supported\n"); 5481 sctp_m_freem(m); 5482 } 5483 CURVNET_RESTORE(); 5484 SCTP_MCORE_QLOCK(wkq); 5485 } 5486 wkq->running = 0; 5487 if (!TAILQ_EMPTY(&wkq->que)) { 5488 goto skip_sleep; 5489 } 5490 SCTP_MCORE_QUNLOCK(wkq); 5491 msleep(&wkq->running, 5492 &wkq->core_mtx, 5493 0, "wait for pkt", 0); 5494 } 5495 } 5496 5497 static void 5498 sctp_startup_mcore_threads(void) 5499 { 5500 int i, cpu; 5501 5502 if (mp_ncpus == 1) 5503 return; 5504 5505 if (sctp_mcore_workers != NULL) { 5506 /* 5507 * Already been here in some previous vnet? 5508 */ 5509 return; 5510 } 5511 SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *, 5512 ((mp_maxid + 1) * sizeof(struct sctp_mcore_ctrl)), 5513 SCTP_M_MCORE); 5514 if (sctp_mcore_workers == NULL) { 5515 /* TSNH I hope */ 5516 return; 5517 } 5518 memset(sctp_mcore_workers, 0, ((mp_maxid + 1) * 5519 sizeof(struct sctp_mcore_ctrl))); 5520 /* Init the structures */ 5521 for (i = 0; i <= mp_maxid; i++) { 5522 TAILQ_INIT(&sctp_mcore_workers[i].que); 5523 SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]); 5524 SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]); 5525 sctp_mcore_workers[i].cpuid = i; 5526 } 5527 if (sctp_cpuarry == NULL) { 5528 SCTP_MALLOC(sctp_cpuarry, int *, 5529 (mp_ncpus * sizeof(int)), 5530 SCTP_M_MCORE); 5531 i = 0; 5532 CPU_FOREACH(cpu) { 5533 sctp_cpuarry[i] = cpu; 5534 i++; 5535 } 5536 } 5537 /* Now start them all */ 5538 CPU_FOREACH(cpu) { 5539 (void)kproc_create(sctp_mcore_thread, 5540 (void *)&sctp_mcore_workers[cpu], 5541 &sctp_mcore_workers[cpu].thread_proc, 5542 0, 5543 SCTP_KTHREAD_PAGES, 5544 SCTP_MCORE_NAME); 5545 } 5546 } 5547 #endif 5548 5549 #define VALIDATE_LOADER_TUNABLE(var_name, prefix) \ 5550 if (SCTP_BASE_SYSCTL(var_name) < prefix##_MIN || \ 5551 SCTP_BASE_SYSCTL(var_name) > prefix##_MAX) \ 5552 SCTP_BASE_SYSCTL(var_name) = prefix##_DEFAULT 5553 5554 void 5555 sctp_pcb_init(void) 5556 { 5557 /* 5558 * SCTP initialization for the PCB structures should be called by 5559 * the sctp_init() function. 5560 */ 5561 int i; 5562 struct timeval tv; 5563 5564 if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) { 5565 /* error I was called twice */ 5566 return; 5567 } 5568 SCTP_BASE_VAR(sctp_pcb_initialized) = 1; 5569 5570 #if defined(SCTP_LOCAL_TRACE_BUF) 5571 memset(&SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log)); 5572 #endif 5573 #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT) 5574 SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *, 5575 ((mp_maxid + 1) * sizeof(struct sctpstat)), 5576 SCTP_M_MCORE); 5577 #endif 5578 (void)SCTP_GETTIME_TIMEVAL(&tv); 5579 #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT) 5580 memset(SCTP_BASE_STATS, 0, sizeof(struct sctpstat) * (mp_maxid + 1)); 5581 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec; 5582 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec; 5583 #else 5584 memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat)); 5585 SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec; 5586 SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec; 5587 #endif 5588 /* init the empty list of (All) Endpoints */ 5589 LIST_INIT(&SCTP_BASE_INFO(listhead)); 5590 5591 /* init the hash table of endpoints */ 5592 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize)); 5593 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize)); 5594 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale)); 5595 VALIDATE_LOADER_TUNABLE(sctp_hashtblsize, SCTPCTL_TCBHASHSIZE); 5596 VALIDATE_LOADER_TUNABLE(sctp_pcbtblsize, SCTPCTL_PCBHASHSIZE); 5597 VALIDATE_LOADER_TUNABLE(sctp_chunkscale, SCTPCTL_CHUNKSCALE); 5598 SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31), 5599 &SCTP_BASE_INFO(hashasocmark)); 5600 SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize), 5601 &SCTP_BASE_INFO(hashmark)); 5602 SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize), 5603 &SCTP_BASE_INFO(hashtcpmark)); 5604 SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize); 5605 SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH, 5606 &SCTP_BASE_INFO(hashvrfmark)); 5607 5608 SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE, 5609 &SCTP_BASE_INFO(vrf_ifn_hashmark)); 5610 /* init the zones */ 5611 /* 5612 * FIX ME: Should check for NULL returns, but if it does fail we are 5613 * doomed to panic anyways... add later maybe. 5614 */ 5615 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep", 5616 sizeof(struct sctp_inpcb), maxsockets); 5617 5618 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc", 5619 sizeof(struct sctp_tcb), sctp_max_number_of_assoc); 5620 5621 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr", 5622 sizeof(struct sctp_laddr), 5623 (sctp_max_number_of_assoc * sctp_scale_up_for_address)); 5624 5625 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr", 5626 sizeof(struct sctp_nets), 5627 (sctp_max_number_of_assoc * sctp_scale_up_for_address)); 5628 5629 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk", 5630 sizeof(struct sctp_tmit_chunk), 5631 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); 5632 5633 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq", 5634 sizeof(struct sctp_queued_to_read), 5635 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); 5636 5637 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out", 5638 sizeof(struct sctp_stream_queue_pending), 5639 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); 5640 5641 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf", 5642 sizeof(struct sctp_asconf), 5643 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); 5644 5645 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack", 5646 sizeof(struct sctp_asconf_ack), 5647 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); 5648 5649 /* Master Lock INIT for info structure */ 5650 SCTP_INP_INFO_LOCK_INIT(); 5651 SCTP_STATLOG_INIT_LOCK(); 5652 5653 SCTP_IPI_COUNT_INIT(); 5654 SCTP_IPI_ADDR_INIT(); 5655 #ifdef SCTP_PACKET_LOGGING 5656 SCTP_IP_PKTLOG_INIT(); 5657 #endif 5658 LIST_INIT(&SCTP_BASE_INFO(addr_wq)); 5659 5660 SCTP_WQ_ADDR_INIT(); 5661 /* not sure if we need all the counts */ 5662 SCTP_BASE_INFO(ipi_count_ep) = 0; 5663 /* assoc/tcb zone info */ 5664 SCTP_BASE_INFO(ipi_count_asoc) = 0; 5665 /* local addrlist zone info */ 5666 SCTP_BASE_INFO(ipi_count_laddr) = 0; 5667 /* remote addrlist zone info */ 5668 SCTP_BASE_INFO(ipi_count_raddr) = 0; 5669 /* chunk info */ 5670 SCTP_BASE_INFO(ipi_count_chunk) = 0; 5671 5672 /* socket queue zone info */ 5673 SCTP_BASE_INFO(ipi_count_readq) = 0; 5674 5675 /* stream out queue cont */ 5676 SCTP_BASE_INFO(ipi_count_strmoq) = 0; 5677 5678 SCTP_BASE_INFO(ipi_free_strmoq) = 0; 5679 SCTP_BASE_INFO(ipi_free_chunks) = 0; 5680 5681 SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer)); 5682 5683 /* Init the TIMEWAIT list */ 5684 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) { 5685 LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]); 5686 } 5687 sctp_startup_iterator(); 5688 5689 #if defined(SCTP_MCORE_INPUT) && defined(SMP) 5690 sctp_startup_mcore_threads(); 5691 #endif 5692 5693 /* 5694 * INIT the default VRF which for BSD is the only one, other O/S's 5695 * may have more. But initially they must start with one and then 5696 * add the VRF's as addresses are added. 5697 */ 5698 sctp_init_vrf_list(SCTP_DEFAULT_VRF); 5699 } 5700 5701 /* 5702 * Assumes that the SCTP_BASE_INFO() lock is NOT held. 5703 */ 5704 void 5705 sctp_pcb_finish(void) 5706 { 5707 struct sctp_vrflist *vrf_bucket; 5708 struct sctp_vrf *vrf, *nvrf; 5709 struct sctp_ifn *ifn, *nifn; 5710 struct sctp_ifa *ifa, *nifa; 5711 struct sctpvtaghead *chain; 5712 struct sctp_tagblock *twait_block, *prev_twait_block; 5713 struct sctp_laddr *wi, *nwi; 5714 int i; 5715 struct sctp_iterator *it, *nit; 5716 5717 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { 5718 SCTP_PRINTF("%s: race condition on teardown.\n", __func__); 5719 return; 5720 } 5721 SCTP_BASE_VAR(sctp_pcb_initialized) = 0; 5722 /* 5723 * In FreeBSD the iterator thread never exits but we do clean up. 5724 * The only way FreeBSD reaches here is if we have VRF's but we 5725 * still add the ifdef to make it compile on old versions. 5726 */ 5727 retry: 5728 SCTP_IPI_ITERATOR_WQ_LOCK(); 5729 /* 5730 * sctp_iterator_worker() might be working on an it entry without 5731 * holding the lock. We won't find it on the list either and 5732 * continue and free/destroy it. While holding the lock, spin, to 5733 * avoid the race condition as sctp_iterator_worker() will have to 5734 * wait to re-acquire the lock. 5735 */ 5736 if (sctp_it_ctl.iterator_running != 0 || sctp_it_ctl.cur_it != NULL) { 5737 SCTP_IPI_ITERATOR_WQ_UNLOCK(); 5738 SCTP_PRINTF("%s: Iterator running while we held the lock. Retry. " 5739 "cur_it=%p\n", __func__, sctp_it_ctl.cur_it); 5740 DELAY(10); 5741 goto retry; 5742 } 5743 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) { 5744 if (it->vn != curvnet) { 5745 continue; 5746 } 5747 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); 5748 if (it->function_atend != NULL) { 5749 (*it->function_atend) (it->pointer, it->val); 5750 } 5751 SCTP_FREE(it, SCTP_M_ITER); 5752 } 5753 SCTP_IPI_ITERATOR_WQ_UNLOCK(); 5754 SCTP_ITERATOR_LOCK(); 5755 if ((sctp_it_ctl.cur_it) && 5756 (sctp_it_ctl.cur_it->vn == curvnet)) { 5757 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT; 5758 } 5759 SCTP_ITERATOR_UNLOCK(); 5760 SCTP_OS_TIMER_STOP_DRAIN(&SCTP_BASE_INFO(addr_wq_timer.timer)); 5761 SCTP_WQ_ADDR_LOCK(); 5762 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) { 5763 LIST_REMOVE(wi, sctp_nxt_addr); 5764 SCTP_DECR_LADDR_COUNT(); 5765 if (wi->action == SCTP_DEL_IP_ADDRESS) { 5766 SCTP_FREE(wi->ifa, SCTP_M_IFA); 5767 } 5768 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi); 5769 } 5770 SCTP_WQ_ADDR_UNLOCK(); 5771 5772 /* 5773 * free the vrf/ifn/ifa lists and hashes (be sure address monitor is 5774 * destroyed first). 5775 */ 5776 SCTP_IPI_ADDR_WLOCK(); 5777 vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))]; 5778 LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) { 5779 LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) { 5780 LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) { 5781 /* free the ifa */ 5782 LIST_REMOVE(ifa, next_bucket); 5783 LIST_REMOVE(ifa, next_ifa); 5784 SCTP_FREE(ifa, SCTP_M_IFA); 5785 } 5786 /* free the ifn */ 5787 LIST_REMOVE(ifn, next_bucket); 5788 LIST_REMOVE(ifn, next_ifn); 5789 SCTP_FREE(ifn, SCTP_M_IFN); 5790 } 5791 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark); 5792 /* free the vrf */ 5793 LIST_REMOVE(vrf, next_vrf); 5794 SCTP_FREE(vrf, SCTP_M_VRF); 5795 } 5796 SCTP_IPI_ADDR_WUNLOCK(); 5797 /* free the vrf hashes */ 5798 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark)); 5799 SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark)); 5800 5801 /* 5802 * free the TIMEWAIT list elements malloc'd in the function 5803 * sctp_add_vtag_to_timewait()... 5804 */ 5805 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) { 5806 chain = &SCTP_BASE_INFO(vtag_timewait)[i]; 5807 if (!LIST_EMPTY(chain)) { 5808 prev_twait_block = NULL; 5809 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { 5810 if (prev_twait_block) { 5811 SCTP_FREE(prev_twait_block, SCTP_M_TIMW); 5812 } 5813 prev_twait_block = twait_block; 5814 } 5815 SCTP_FREE(prev_twait_block, SCTP_M_TIMW); 5816 } 5817 } 5818 5819 /* free the locks and mutexes */ 5820 #ifdef SCTP_PACKET_LOGGING 5821 SCTP_IP_PKTLOG_DESTROY(); 5822 #endif 5823 SCTP_IPI_ADDR_DESTROY(); 5824 SCTP_STATLOG_DESTROY(); 5825 SCTP_INP_INFO_LOCK_DESTROY(); 5826 5827 SCTP_WQ_ADDR_DESTROY(); 5828 5829 /* Get rid of other stuff too. */ 5830 if (SCTP_BASE_INFO(sctp_asochash) != NULL) 5831 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark)); 5832 if (SCTP_BASE_INFO(sctp_ephash) != NULL) 5833 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark)); 5834 if (SCTP_BASE_INFO(sctp_tcpephash) != NULL) 5835 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark)); 5836 5837 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep)); 5838 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc)); 5839 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr)); 5840 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net)); 5841 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk)); 5842 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq)); 5843 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq)); 5844 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf)); 5845 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack)); 5846 #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT) 5847 SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE); 5848 #endif 5849 } 5850 5851 int 5852 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m, 5853 int offset, int limit, 5854 struct sockaddr *src, struct sockaddr *dst, 5855 struct sockaddr *altsa, uint16_t port) 5856 { 5857 /* 5858 * grub through the INIT pulling addresses and loading them to the 5859 * nets structure in the asoc. The from address in the mbuf should 5860 * also be loaded (if it is not already). This routine can be called 5861 * with either INIT or INIT-ACK's as long as the m points to the IP 5862 * packet and the offset points to the beginning of the parameters. 5863 */ 5864 struct sctp_inpcb *inp; 5865 struct sctp_nets *net, *nnet, *net_tmp; 5866 struct sctp_paramhdr *phdr, param_buf; 5867 struct sctp_tcb *stcb_tmp; 5868 uint16_t ptype, plen; 5869 struct sockaddr *sa; 5870 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE]; 5871 struct sctp_auth_random *p_random = NULL; 5872 uint16_t random_len = 0; 5873 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE]; 5874 struct sctp_auth_hmac_algo *hmacs = NULL; 5875 uint16_t hmacs_len = 0; 5876 uint8_t saw_asconf = 0; 5877 uint8_t saw_asconf_ack = 0; 5878 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE]; 5879 struct sctp_auth_chunk_list *chunks = NULL; 5880 uint16_t num_chunks = 0; 5881 sctp_key_t *new_key; 5882 uint32_t keylen; 5883 int got_random = 0, got_hmacs = 0, got_chklist = 0; 5884 uint8_t peer_supports_ecn; 5885 uint8_t peer_supports_prsctp; 5886 uint8_t peer_supports_auth; 5887 uint8_t peer_supports_asconf; 5888 uint8_t peer_supports_asconf_ack; 5889 uint8_t peer_supports_reconfig; 5890 uint8_t peer_supports_nrsack; 5891 uint8_t peer_supports_pktdrop; 5892 uint8_t peer_supports_idata; 5893 #ifdef INET 5894 struct sockaddr_in sin; 5895 #endif 5896 #ifdef INET6 5897 struct sockaddr_in6 sin6; 5898 #endif 5899 5900 /* First get the destination address setup too. */ 5901 #ifdef INET 5902 memset(&sin, 0, sizeof(sin)); 5903 sin.sin_family = AF_INET; 5904 sin.sin_len = sizeof(sin); 5905 sin.sin_port = stcb->rport; 5906 #endif 5907 #ifdef INET6 5908 memset(&sin6, 0, sizeof(sin6)); 5909 sin6.sin6_family = AF_INET6; 5910 sin6.sin6_len = sizeof(struct sockaddr_in6); 5911 sin6.sin6_port = stcb->rport; 5912 #endif 5913 if (altsa) { 5914 sa = altsa; 5915 } else { 5916 sa = src; 5917 } 5918 peer_supports_idata = 0; 5919 peer_supports_ecn = 0; 5920 peer_supports_prsctp = 0; 5921 peer_supports_auth = 0; 5922 peer_supports_asconf = 0; 5923 peer_supports_asconf_ack = 0; 5924 peer_supports_reconfig = 0; 5925 peer_supports_nrsack = 0; 5926 peer_supports_pktdrop = 0; 5927 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 5928 /* mark all addresses that we have currently on the list */ 5929 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC; 5930 } 5931 /* does the source address already exist? if so skip it */ 5932 inp = stcb->sctp_ep; 5933 atomic_add_int(&stcb->asoc.refcnt, 1); 5934 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb); 5935 atomic_subtract_int(&stcb->asoc.refcnt, 1); 5936 5937 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) { 5938 /* we must add the source address */ 5939 /* no scope set here since we have a tcb already. */ 5940 switch (sa->sa_family) { 5941 #ifdef INET 5942 case AF_INET: 5943 if (stcb->asoc.scope.ipv4_addr_legal) { 5944 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) { 5945 return (-1); 5946 } 5947 } 5948 break; 5949 #endif 5950 #ifdef INET6 5951 case AF_INET6: 5952 if (stcb->asoc.scope.ipv6_addr_legal) { 5953 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) { 5954 return (-2); 5955 } 5956 } 5957 break; 5958 #endif 5959 default: 5960 break; 5961 } 5962 } else { 5963 if (net_tmp != NULL && stcb_tmp == stcb) { 5964 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC; 5965 } else if (stcb_tmp != stcb) { 5966 /* It belongs to another association? */ 5967 if (stcb_tmp) 5968 SCTP_TCB_UNLOCK(stcb_tmp); 5969 return (-3); 5970 } 5971 } 5972 if (stcb->asoc.state == 0) { 5973 /* the assoc was freed? */ 5974 return (-4); 5975 } 5976 /* now we must go through each of the params. */ 5977 phdr = sctp_get_next_param(m, offset, ¶m_buf, sizeof(param_buf)); 5978 while (phdr) { 5979 ptype = ntohs(phdr->param_type); 5980 plen = ntohs(phdr->param_length); 5981 /* 5982 * SCTP_PRINTF("ptype => %0x, plen => %d\n", 5983 * (uint32_t)ptype, (int)plen); 5984 */ 5985 if (offset + plen > limit) { 5986 break; 5987 } 5988 if (plen < sizeof(struct sctp_paramhdr)) { 5989 break; 5990 } 5991 #ifdef INET 5992 if (ptype == SCTP_IPV4_ADDRESS) { 5993 if (stcb->asoc.scope.ipv4_addr_legal) { 5994 struct sctp_ipv4addr_param *p4, p4_buf; 5995 5996 /* ok get the v4 address and check/add */ 5997 phdr = sctp_get_next_param(m, offset, 5998 (struct sctp_paramhdr *)&p4_buf, 5999 sizeof(p4_buf)); 6000 if (plen != sizeof(struct sctp_ipv4addr_param) || 6001 phdr == NULL) { 6002 return (-5); 6003 } 6004 p4 = (struct sctp_ipv4addr_param *)phdr; 6005 sin.sin_addr.s_addr = p4->addr; 6006 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 6007 /* Skip multi-cast addresses */ 6008 goto next_param; 6009 } 6010 if (in_broadcast(sin.sin_addr)) { 6011 goto next_param; 6012 } 6013 sa = (struct sockaddr *)&sin; 6014 inp = stcb->sctp_ep; 6015 atomic_add_int(&stcb->asoc.refcnt, 1); 6016 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, 6017 dst, stcb); 6018 atomic_subtract_int(&stcb->asoc.refcnt, 1); 6019 6020 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || 6021 inp == NULL) { 6022 /* we must add the source address */ 6023 /* 6024 * no scope set since we have a tcb 6025 * already 6026 */ 6027 6028 /* 6029 * we must validate the state again 6030 * here 6031 */ 6032 add_it_now: 6033 if (stcb->asoc.state == 0) { 6034 /* the assoc was freed? */ 6035 return (-7); 6036 } 6037 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) { 6038 return (-8); 6039 } 6040 } else if (stcb_tmp == stcb) { 6041 if (stcb->asoc.state == 0) { 6042 /* the assoc was freed? */ 6043 return (-10); 6044 } 6045 if (net != NULL) { 6046 /* clear flag */ 6047 net->dest_state &= 6048 ~SCTP_ADDR_NOT_IN_ASSOC; 6049 } 6050 } else { 6051 /* 6052 * strange, address is in another 6053 * assoc? straighten out locks. 6054 */ 6055 if (stcb_tmp) { 6056 if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) { 6057 struct mbuf *op_err; 6058 char msg[SCTP_DIAG_INFO_LEN]; 6059 6060 /* 6061 * in setup state we 6062 * abort this guy 6063 */ 6064 SCTP_SNPRINTF(msg, sizeof(msg), 6065 "%s:%d at %s", __FILE__, __LINE__, __func__); 6066 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 6067 msg); 6068 sctp_abort_an_association(stcb_tmp->sctp_ep, 6069 stcb_tmp, op_err, false, 6070 SCTP_SO_NOT_LOCKED); 6071 goto add_it_now; 6072 } 6073 SCTP_TCB_UNLOCK(stcb_tmp); 6074 } 6075 6076 if (stcb->asoc.state == 0) { 6077 /* the assoc was freed? */ 6078 return (-12); 6079 } 6080 return (-13); 6081 } 6082 } 6083 } else 6084 #endif 6085 #ifdef INET6 6086 if (ptype == SCTP_IPV6_ADDRESS) { 6087 if (stcb->asoc.scope.ipv6_addr_legal) { 6088 /* ok get the v6 address and check/add */ 6089 struct sctp_ipv6addr_param *p6, p6_buf; 6090 6091 phdr = sctp_get_next_param(m, offset, 6092 (struct sctp_paramhdr *)&p6_buf, 6093 sizeof(p6_buf)); 6094 if (plen != sizeof(struct sctp_ipv6addr_param) || 6095 phdr == NULL) { 6096 return (-14); 6097 } 6098 p6 = (struct sctp_ipv6addr_param *)phdr; 6099 memcpy((caddr_t)&sin6.sin6_addr, p6->addr, 6100 sizeof(p6->addr)); 6101 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) { 6102 /* Skip multi-cast addresses */ 6103 goto next_param; 6104 } 6105 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) { 6106 /* 6107 * Link local make no sense without 6108 * scope 6109 */ 6110 goto next_param; 6111 } 6112 sa = (struct sockaddr *)&sin6; 6113 inp = stcb->sctp_ep; 6114 atomic_add_int(&stcb->asoc.refcnt, 1); 6115 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, 6116 dst, stcb); 6117 atomic_subtract_int(&stcb->asoc.refcnt, 1); 6118 if (stcb_tmp == NULL && 6119 (inp == stcb->sctp_ep || inp == NULL)) { 6120 /* 6121 * we must validate the state again 6122 * here 6123 */ 6124 add_it_now6: 6125 if (stcb->asoc.state == 0) { 6126 /* the assoc was freed? */ 6127 return (-16); 6128 } 6129 /* 6130 * we must add the address, no scope 6131 * set 6132 */ 6133 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) { 6134 return (-17); 6135 } 6136 } else if (stcb_tmp == stcb) { 6137 /* 6138 * we must validate the state again 6139 * here 6140 */ 6141 if (stcb->asoc.state == 0) { 6142 /* the assoc was freed? */ 6143 return (-19); 6144 } 6145 if (net != NULL) { 6146 /* clear flag */ 6147 net->dest_state &= 6148 ~SCTP_ADDR_NOT_IN_ASSOC; 6149 } 6150 } else { 6151 /* 6152 * strange, address is in another 6153 * assoc? straighten out locks. 6154 */ 6155 if (stcb_tmp) { 6156 if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) { 6157 struct mbuf *op_err; 6158 char msg[SCTP_DIAG_INFO_LEN]; 6159 6160 /* 6161 * in setup state we 6162 * abort this guy 6163 */ 6164 SCTP_SNPRINTF(msg, sizeof(msg), 6165 "%s:%d at %s", __FILE__, __LINE__, __func__); 6166 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), 6167 msg); 6168 sctp_abort_an_association(stcb_tmp->sctp_ep, 6169 stcb_tmp, op_err, false, 6170 SCTP_SO_NOT_LOCKED); 6171 goto add_it_now6; 6172 } 6173 SCTP_TCB_UNLOCK(stcb_tmp); 6174 } 6175 if (stcb->asoc.state == 0) { 6176 /* the assoc was freed? */ 6177 return (-21); 6178 } 6179 return (-22); 6180 } 6181 } 6182 } else 6183 #endif 6184 if (ptype == SCTP_ECN_CAPABLE) { 6185 peer_supports_ecn = 1; 6186 } else if (ptype == SCTP_ULP_ADAPTATION) { 6187 if (stcb->asoc.state != SCTP_STATE_OPEN) { 6188 struct sctp_adaptation_layer_indication ai, 6189 *aip; 6190 6191 phdr = sctp_get_next_param(m, offset, 6192 (struct sctp_paramhdr *)&ai, sizeof(ai)); 6193 aip = (struct sctp_adaptation_layer_indication *)phdr; 6194 if (aip) { 6195 stcb->asoc.peers_adaptation = ntohl(aip->indication); 6196 stcb->asoc.adaptation_needed = 1; 6197 } 6198 } 6199 } else if (ptype == SCTP_SET_PRIM_ADDR) { 6200 struct sctp_asconf_addr_param lstore, *fee; 6201 int lptype; 6202 struct sockaddr *lsa = NULL; 6203 #ifdef INET 6204 struct sctp_asconf_addrv4_param *fii; 6205 #endif 6206 6207 if (stcb->asoc.asconf_supported == 0) { 6208 return (-100); 6209 } 6210 if (plen > sizeof(lstore)) { 6211 return (-23); 6212 } 6213 if (plen < sizeof(struct sctp_asconf_addrv4_param)) { 6214 return (-101); 6215 } 6216 phdr = sctp_get_next_param(m, offset, 6217 (struct sctp_paramhdr *)&lstore, 6218 plen); 6219 if (phdr == NULL) { 6220 return (-24); 6221 } 6222 fee = (struct sctp_asconf_addr_param *)phdr; 6223 lptype = ntohs(fee->addrp.ph.param_type); 6224 switch (lptype) { 6225 #ifdef INET 6226 case SCTP_IPV4_ADDRESS: 6227 if (plen != 6228 sizeof(struct sctp_asconf_addrv4_param)) { 6229 SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n", 6230 (int)sizeof(struct sctp_asconf_addrv4_param), 6231 plen); 6232 } else { 6233 fii = (struct sctp_asconf_addrv4_param *)fee; 6234 sin.sin_addr.s_addr = fii->addrp.addr; 6235 lsa = (struct sockaddr *)&sin; 6236 } 6237 break; 6238 #endif 6239 #ifdef INET6 6240 case SCTP_IPV6_ADDRESS: 6241 if (plen != 6242 sizeof(struct sctp_asconf_addr_param)) { 6243 SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n", 6244 (int)sizeof(struct sctp_asconf_addr_param), 6245 plen); 6246 } else { 6247 memcpy(sin6.sin6_addr.s6_addr, 6248 fee->addrp.addr, 6249 sizeof(fee->addrp.addr)); 6250 lsa = (struct sockaddr *)&sin6; 6251 } 6252 break; 6253 #endif 6254 default: 6255 break; 6256 } 6257 if (lsa) { 6258 (void)sctp_set_primary_addr(stcb, sa, NULL); 6259 } 6260 } else if (ptype == SCTP_HAS_NAT_SUPPORT) { 6261 stcb->asoc.peer_supports_nat = 1; 6262 } else if (ptype == SCTP_PRSCTP_SUPPORTED) { 6263 /* Peer supports pr-sctp */ 6264 peer_supports_prsctp = 1; 6265 } else if (ptype == SCTP_ZERO_CHECKSUM_ACCEPTABLE) { 6266 struct sctp_zero_checksum_acceptable zero_chksum, 6267 *zero_chksum_p; 6268 6269 phdr = sctp_get_next_param(m, offset, 6270 (struct sctp_paramhdr *)&zero_chksum, 6271 sizeof(struct sctp_zero_checksum_acceptable)); 6272 if (phdr != NULL) { 6273 /* 6274 * Only send zero checksums if the upper 6275 * layer has enabled the support for the 6276 * same method as allowed by the peer. 6277 */ 6278 zero_chksum_p = (struct sctp_zero_checksum_acceptable *)phdr; 6279 if ((ntohl(zero_chksum_p->edmid) != SCTP_EDMID_NONE) && 6280 (ntohl(zero_chksum_p->edmid) == stcb->asoc.rcv_edmid)) { 6281 stcb->asoc.snd_edmid = stcb->asoc.rcv_edmid; 6282 } 6283 } 6284 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) { 6285 /* A supported extension chunk */ 6286 struct sctp_supported_chunk_types_param *pr_supported; 6287 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE]; 6288 int num_ent, i; 6289 6290 if (plen > sizeof(local_store)) { 6291 return (-35); 6292 } 6293 phdr = sctp_get_next_param(m, offset, 6294 (struct sctp_paramhdr *)&local_store, plen); 6295 if (phdr == NULL) { 6296 return (-25); 6297 } 6298 pr_supported = (struct sctp_supported_chunk_types_param *)phdr; 6299 num_ent = plen - sizeof(struct sctp_paramhdr); 6300 for (i = 0; i < num_ent; i++) { 6301 switch (pr_supported->chunk_types[i]) { 6302 case SCTP_ASCONF: 6303 peer_supports_asconf = 1; 6304 break; 6305 case SCTP_ASCONF_ACK: 6306 peer_supports_asconf_ack = 1; 6307 break; 6308 case SCTP_FORWARD_CUM_TSN: 6309 peer_supports_prsctp = 1; 6310 break; 6311 case SCTP_PACKET_DROPPED: 6312 peer_supports_pktdrop = 1; 6313 break; 6314 case SCTP_NR_SELECTIVE_ACK: 6315 peer_supports_nrsack = 1; 6316 break; 6317 case SCTP_STREAM_RESET: 6318 peer_supports_reconfig = 1; 6319 break; 6320 case SCTP_AUTHENTICATION: 6321 peer_supports_auth = 1; 6322 break; 6323 case SCTP_IDATA: 6324 peer_supports_idata = 1; 6325 break; 6326 default: 6327 /* one I have not learned yet */ 6328 break; 6329 } 6330 } 6331 } else if (ptype == SCTP_RANDOM) { 6332 if (plen > sizeof(random_store)) 6333 break; 6334 if (got_random) { 6335 /* already processed a RANDOM */ 6336 goto next_param; 6337 } 6338 phdr = sctp_get_next_param(m, offset, 6339 (struct sctp_paramhdr *)random_store, 6340 plen); 6341 if (phdr == NULL) 6342 return (-26); 6343 p_random = (struct sctp_auth_random *)phdr; 6344 random_len = plen - sizeof(*p_random); 6345 /* enforce the random length */ 6346 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) { 6347 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n"); 6348 return (-27); 6349 } 6350 got_random = 1; 6351 } else if (ptype == SCTP_HMAC_LIST) { 6352 uint16_t num_hmacs; 6353 uint16_t i; 6354 6355 if (plen > sizeof(hmacs_store)) 6356 break; 6357 if (got_hmacs) { 6358 /* already processed a HMAC list */ 6359 goto next_param; 6360 } 6361 phdr = sctp_get_next_param(m, offset, 6362 (struct sctp_paramhdr *)hmacs_store, 6363 plen); 6364 if (phdr == NULL) 6365 return (-28); 6366 hmacs = (struct sctp_auth_hmac_algo *)phdr; 6367 hmacs_len = plen - sizeof(*hmacs); 6368 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]); 6369 /* validate the hmac list */ 6370 if (sctp_verify_hmac_param(hmacs, num_hmacs)) { 6371 return (-29); 6372 } 6373 if (stcb->asoc.peer_hmacs != NULL) 6374 sctp_free_hmaclist(stcb->asoc.peer_hmacs); 6375 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs); 6376 if (stcb->asoc.peer_hmacs != NULL) { 6377 for (i = 0; i < num_hmacs; i++) { 6378 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs, 6379 ntohs(hmacs->hmac_ids[i])); 6380 } 6381 } 6382 got_hmacs = 1; 6383 } else if (ptype == SCTP_CHUNK_LIST) { 6384 int i; 6385 6386 if (plen > sizeof(chunks_store)) 6387 break; 6388 if (got_chklist) { 6389 /* already processed a Chunks list */ 6390 goto next_param; 6391 } 6392 phdr = sctp_get_next_param(m, offset, 6393 (struct sctp_paramhdr *)chunks_store, 6394 plen); 6395 if (phdr == NULL) 6396 return (-30); 6397 chunks = (struct sctp_auth_chunk_list *)phdr; 6398 num_chunks = plen - sizeof(*chunks); 6399 if (stcb->asoc.peer_auth_chunks != NULL) 6400 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks); 6401 else 6402 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist(); 6403 for (i = 0; i < num_chunks; i++) { 6404 (void)sctp_auth_add_chunk(chunks->chunk_types[i], 6405 stcb->asoc.peer_auth_chunks); 6406 /* record asconf/asconf-ack if listed */ 6407 if (chunks->chunk_types[i] == SCTP_ASCONF) 6408 saw_asconf = 1; 6409 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK) 6410 saw_asconf_ack = 1; 6411 } 6412 got_chklist = 1; 6413 } else if ((ptype == SCTP_HEARTBEAT_INFO) || 6414 (ptype == SCTP_STATE_COOKIE) || 6415 (ptype == SCTP_UNRECOG_PARAM) || 6416 (ptype == SCTP_COOKIE_PRESERVE) || 6417 (ptype == SCTP_SUPPORTED_ADDRTYPE) || 6418 (ptype == SCTP_ADD_IP_ADDRESS) || 6419 (ptype == SCTP_DEL_IP_ADDRESS) || 6420 (ptype == SCTP_ERROR_CAUSE_IND) || 6421 (ptype == SCTP_SUCCESS_REPORT)) { 6422 /* don't care */ 6423 } else { 6424 if ((ptype & 0x8000) == 0x0000) { 6425 /* 6426 * must stop processing the rest of the 6427 * param's. Any report bits were handled 6428 * with the call to 6429 * sctp_arethere_unrecognized_parameters() 6430 * when the INIT or INIT-ACK was first seen. 6431 */ 6432 break; 6433 } 6434 } 6435 6436 next_param: 6437 offset += SCTP_SIZE32(plen); 6438 if (offset >= limit) { 6439 break; 6440 } 6441 phdr = sctp_get_next_param(m, offset, ¶m_buf, 6442 sizeof(param_buf)); 6443 } 6444 /* Now check to see if we need to purge any addresses */ 6445 TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) { 6446 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) == 6447 SCTP_ADDR_NOT_IN_ASSOC) { 6448 /* This address has been removed from the asoc */ 6449 /* remove and free it */ 6450 stcb->asoc.numnets--; 6451 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next); 6452 if (net == stcb->asoc.alternate) { 6453 sctp_free_remote_addr(stcb->asoc.alternate); 6454 stcb->asoc.alternate = NULL; 6455 } 6456 if (net == stcb->asoc.primary_destination) { 6457 stcb->asoc.primary_destination = NULL; 6458 sctp_select_primary_destination(stcb); 6459 } 6460 sctp_free_remote_addr(net); 6461 } 6462 } 6463 if ((stcb->asoc.ecn_supported == 1) && 6464 (peer_supports_ecn == 0)) { 6465 stcb->asoc.ecn_supported = 0; 6466 } 6467 if ((stcb->asoc.prsctp_supported == 1) && 6468 (peer_supports_prsctp == 0)) { 6469 stcb->asoc.prsctp_supported = 0; 6470 } 6471 if ((stcb->asoc.auth_supported == 1) && 6472 ((peer_supports_auth == 0) || 6473 (got_random == 0) || (got_hmacs == 0))) { 6474 stcb->asoc.auth_supported = 0; 6475 } 6476 if ((stcb->asoc.asconf_supported == 1) && 6477 ((peer_supports_asconf == 0) || (peer_supports_asconf_ack == 0) || 6478 (stcb->asoc.auth_supported == 0) || 6479 (saw_asconf == 0) || (saw_asconf_ack == 0))) { 6480 stcb->asoc.asconf_supported = 0; 6481 } 6482 if ((stcb->asoc.reconfig_supported == 1) && 6483 (peer_supports_reconfig == 0)) { 6484 stcb->asoc.reconfig_supported = 0; 6485 } 6486 if ((stcb->asoc.idata_supported == 1) && 6487 (peer_supports_idata == 0)) { 6488 stcb->asoc.idata_supported = 0; 6489 } 6490 if ((stcb->asoc.nrsack_supported == 1) && 6491 (peer_supports_nrsack == 0)) { 6492 stcb->asoc.nrsack_supported = 0; 6493 } 6494 if ((stcb->asoc.pktdrop_supported == 1) && 6495 (peer_supports_pktdrop == 0)) { 6496 stcb->asoc.pktdrop_supported = 0; 6497 } 6498 /* validate authentication required parameters */ 6499 if ((peer_supports_auth == 0) && (got_chklist == 1)) { 6500 /* peer does not support auth but sent a chunks list? */ 6501 return (-31); 6502 } 6503 if ((peer_supports_asconf == 1) && (peer_supports_auth == 0)) { 6504 /* peer supports asconf but not auth? */ 6505 return (-32); 6506 } else if ((peer_supports_asconf == 1) && 6507 (peer_supports_auth == 1) && 6508 ((saw_asconf == 0) || (saw_asconf_ack == 0))) { 6509 return (-33); 6510 } 6511 /* concatenate the full random key */ 6512 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len; 6513 if (chunks != NULL) { 6514 keylen += sizeof(*chunks) + num_chunks; 6515 } 6516 new_key = sctp_alloc_key(keylen); 6517 if (new_key != NULL) { 6518 /* copy in the RANDOM */ 6519 if (p_random != NULL) { 6520 keylen = sizeof(*p_random) + random_len; 6521 memcpy(new_key->key, p_random, keylen); 6522 } else { 6523 keylen = 0; 6524 } 6525 /* append in the AUTH chunks */ 6526 if (chunks != NULL) { 6527 memcpy(new_key->key + keylen, chunks, 6528 sizeof(*chunks) + num_chunks); 6529 keylen += sizeof(*chunks) + num_chunks; 6530 } 6531 /* append in the HMACs */ 6532 if (hmacs != NULL) { 6533 memcpy(new_key->key + keylen, hmacs, 6534 sizeof(*hmacs) + hmacs_len); 6535 } 6536 } else { 6537 /* failed to get memory for the key */ 6538 return (-34); 6539 } 6540 if (stcb->asoc.authinfo.peer_random != NULL) 6541 sctp_free_key(stcb->asoc.authinfo.peer_random); 6542 stcb->asoc.authinfo.peer_random = new_key; 6543 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid); 6544 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid); 6545 6546 return (0); 6547 } 6548 6549 int 6550 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa, 6551 struct sctp_nets *net) 6552 { 6553 /* make sure the requested primary address exists in the assoc */ 6554 if (net == NULL && sa) 6555 net = sctp_findnet(stcb, sa); 6556 6557 if (net == NULL) { 6558 /* didn't find the requested primary address! */ 6559 return (-1); 6560 } else { 6561 /* set the primary address */ 6562 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { 6563 /* Must be confirmed, so queue to set */ 6564 net->dest_state |= SCTP_ADDR_REQ_PRIMARY; 6565 return (0); 6566 } 6567 stcb->asoc.primary_destination = net; 6568 if (((net->dest_state & SCTP_ADDR_PF) == 0) && 6569 (stcb->asoc.alternate != NULL)) { 6570 sctp_free_remote_addr(stcb->asoc.alternate); 6571 stcb->asoc.alternate = NULL; 6572 } 6573 net = TAILQ_FIRST(&stcb->asoc.nets); 6574 if (net != stcb->asoc.primary_destination) { 6575 /* 6576 * first one on the list is NOT the primary 6577 * sctp_cmpaddr() is much more efficient if the 6578 * primary is the first on the list, make it so. 6579 */ 6580 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 6581 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 6582 } 6583 return (0); 6584 } 6585 } 6586 6587 bool 6588 sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now) 6589 { 6590 struct sctpasochead *head; 6591 struct sctp_tcb *stcb; 6592 6593 SCTP_INP_INFO_LOCK_ASSERT(); 6594 6595 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, SCTP_BASE_INFO(hashasocmark))]; 6596 LIST_FOREACH(stcb, head, sctp_asocs) { 6597 /* 6598 * We choose not to lock anything here. TCB's can't be 6599 * removed since we have the read lock, so they can't be 6600 * freed on us, same thing for the INP. I may be wrong with 6601 * this assumption, but we will go with it for now :-) 6602 */ 6603 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 6604 continue; 6605 } 6606 if (stcb->asoc.my_vtag == tag) { 6607 /* candidate */ 6608 if (stcb->rport != rport) { 6609 continue; 6610 } 6611 if (stcb->sctp_ep->sctp_lport != lport) { 6612 continue; 6613 } 6614 /* The tag is currently used, so don't use it. */ 6615 return (false); 6616 } 6617 } 6618 return (!sctp_is_in_timewait(tag, lport, rport, now->tv_sec)); 6619 } 6620 6621 static void 6622 sctp_drain_mbufs(struct sctp_tcb *stcb) 6623 { 6624 /* 6625 * We must hunt this association for MBUF's past the cumack (i.e. 6626 * out of order data that we can renege on). 6627 */ 6628 struct sctp_association *asoc; 6629 struct sctp_tmit_chunk *chk, *nchk; 6630 uint32_t cumulative_tsn_p1; 6631 struct sctp_queued_to_read *control, *ncontrol; 6632 int cnt, strmat; 6633 uint32_t gap, i; 6634 int fnd = 0; 6635 6636 /* We look for anything larger than the cum-ack + 1 */ 6637 6638 asoc = &stcb->asoc; 6639 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) { 6640 /* none we can reneg on. */ 6641 return; 6642 } 6643 SCTP_STAT_INCR(sctps_protocol_drains_done); 6644 cumulative_tsn_p1 = asoc->cumulative_tsn + 1; 6645 cnt = 0; 6646 /* Ok that was fun, now we will drain all the inbound streams? */ 6647 for (strmat = 0; strmat < asoc->streamincnt; strmat++) { 6648 TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].inqueue, next_instrm, ncontrol) { 6649 #ifdef INVARIANTS 6650 if (control->on_strm_q != SCTP_ON_ORDERED) { 6651 panic("Huh control: %p on_q: %d -- not ordered?", 6652 control, control->on_strm_q); 6653 } 6654 #endif 6655 if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) { 6656 /* Yep it is above cum-ack */ 6657 cnt++; 6658 SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn); 6659 KASSERT(control->length > 0, ("control has zero length")); 6660 if (asoc->size_on_all_streams >= control->length) { 6661 asoc->size_on_all_streams -= control->length; 6662 } else { 6663 #ifdef INVARIANTS 6664 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length); 6665 #else 6666 asoc->size_on_all_streams = 0; 6667 #endif 6668 } 6669 sctp_ucount_decr(asoc->cnt_on_all_streams); 6670 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); 6671 if (control->on_read_q) { 6672 TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next); 6673 control->on_read_q = 0; 6674 } 6675 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, control, next_instrm); 6676 control->on_strm_q = 0; 6677 if (control->data) { 6678 sctp_m_freem(control->data); 6679 control->data = NULL; 6680 } 6681 sctp_free_remote_addr(control->whoFrom); 6682 /* Now its reasm? */ 6683 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) { 6684 cnt++; 6685 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn); 6686 KASSERT(chk->send_size > 0, ("chunk has zero length")); 6687 if (asoc->size_on_reasm_queue >= chk->send_size) { 6688 asoc->size_on_reasm_queue -= chk->send_size; 6689 } else { 6690 #ifdef INVARIANTS 6691 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size); 6692 #else 6693 asoc->size_on_reasm_queue = 0; 6694 #endif 6695 } 6696 sctp_ucount_decr(asoc->cnt_on_reasm_queue); 6697 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); 6698 TAILQ_REMOVE(&control->reasm, chk, sctp_next); 6699 if (chk->data) { 6700 sctp_m_freem(chk->data); 6701 chk->data = NULL; 6702 } 6703 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 6704 } 6705 sctp_free_a_readq(stcb, control); 6706 } 6707 } 6708 TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].uno_inqueue, next_instrm, ncontrol) { 6709 #ifdef INVARIANTS 6710 if (control->on_strm_q != SCTP_ON_UNORDERED) { 6711 panic("Huh control: %p on_q: %d -- not unordered?", 6712 control, control->on_strm_q); 6713 } 6714 #endif 6715 if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) { 6716 /* Yep it is above cum-ack */ 6717 cnt++; 6718 SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn); 6719 KASSERT(control->length > 0, ("control has zero length")); 6720 if (asoc->size_on_all_streams >= control->length) { 6721 asoc->size_on_all_streams -= control->length; 6722 } else { 6723 #ifdef INVARIANTS 6724 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length); 6725 #else 6726 asoc->size_on_all_streams = 0; 6727 #endif 6728 } 6729 sctp_ucount_decr(asoc->cnt_on_all_streams); 6730 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); 6731 if (control->on_read_q) { 6732 TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next); 6733 control->on_read_q = 0; 6734 } 6735 TAILQ_REMOVE(&asoc->strmin[strmat].uno_inqueue, control, next_instrm); 6736 control->on_strm_q = 0; 6737 if (control->data) { 6738 sctp_m_freem(control->data); 6739 control->data = NULL; 6740 } 6741 sctp_free_remote_addr(control->whoFrom); 6742 /* Now its reasm? */ 6743 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) { 6744 cnt++; 6745 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn); 6746 KASSERT(chk->send_size > 0, ("chunk has zero length")); 6747 if (asoc->size_on_reasm_queue >= chk->send_size) { 6748 asoc->size_on_reasm_queue -= chk->send_size; 6749 } else { 6750 #ifdef INVARIANTS 6751 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size); 6752 #else 6753 asoc->size_on_reasm_queue = 0; 6754 #endif 6755 } 6756 sctp_ucount_decr(asoc->cnt_on_reasm_queue); 6757 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); 6758 TAILQ_REMOVE(&control->reasm, chk, sctp_next); 6759 if (chk->data) { 6760 sctp_m_freem(chk->data); 6761 chk->data = NULL; 6762 } 6763 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 6764 } 6765 sctp_free_a_readq(stcb, control); 6766 } 6767 } 6768 } 6769 if (cnt) { 6770 /* We must back down to see what the new highest is */ 6771 for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) { 6772 SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn); 6773 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) { 6774 asoc->highest_tsn_inside_map = i; 6775 fnd = 1; 6776 break; 6777 } 6778 } 6779 if (!fnd) { 6780 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1; 6781 } 6782 6783 /* 6784 * Question, should we go through the delivery queue? The 6785 * only reason things are on here is the app not reading OR 6786 * a p-d-api up. An attacker COULD send enough in to 6787 * initiate the PD-API and then send a bunch of stuff to 6788 * other streams... these would wind up on the delivery 6789 * queue.. and then we would not get to them. But in order 6790 * to do this I then have to back-track and un-deliver 6791 * sequence numbers in streams.. el-yucko. I think for now 6792 * we will NOT look at the delivery queue and leave it to be 6793 * something to consider later. An alternative would be to 6794 * abort the P-D-API with a notification and then deliver 6795 * the data.... Or another method might be to keep track of 6796 * how many times the situation occurs and if we see a 6797 * possible attack underway just abort the association. 6798 */ 6799 #ifdef SCTP_DEBUG 6800 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt); 6801 #endif 6802 /* 6803 * Now do we need to find a new 6804 * asoc->highest_tsn_inside_map? 6805 */ 6806 asoc->last_revoke_count = cnt; 6807 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, 6808 SCTP_FROM_SCTP_PCB + SCTP_LOC_11); 6809 /* sa_ignore NO_NULL_CHK */ 6810 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); 6811 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED); 6812 } 6813 /* 6814 * Another issue, in un-setting the TSN's in the mapping array we 6815 * DID NOT adjust the highest_tsn marker. This will cause one of 6816 * two things to occur. It may cause us to do extra work in checking 6817 * for our mapping array movement. More importantly it may cause us 6818 * to SACK every datagram. This may not be a bad thing though since 6819 * we will recover once we get our cum-ack above and all this stuff 6820 * we dumped recovered. 6821 */ 6822 } 6823 6824 static void 6825 sctp_drain(void *arg __unused, int flags __unused) 6826 { 6827 struct epoch_tracker et; 6828 6829 VNET_ITERATOR_DECL(vnet_iter); 6830 6831 NET_EPOCH_ENTER(et); 6832 /* 6833 * We must walk the PCB lists for ALL associations here. The system 6834 * is LOW on MBUF's and needs help. This is where reneging will 6835 * occur. We really hope this does NOT happen! 6836 */ 6837 VNET_LIST_RLOCK_NOSLEEP(); 6838 VNET_FOREACH(vnet_iter) { 6839 CURVNET_SET(vnet_iter); 6840 struct sctp_inpcb *inp; 6841 struct sctp_tcb *stcb; 6842 6843 SCTP_STAT_INCR(sctps_protocol_drain_calls); 6844 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) { 6845 #ifdef VIMAGE 6846 continue; 6847 #else 6848 NET_EPOCH_EXIT(et); 6849 return; 6850 #endif 6851 } 6852 SCTP_INP_INFO_RLOCK(); 6853 LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) { 6854 /* For each endpoint */ 6855 SCTP_INP_RLOCK(inp); 6856 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 6857 /* For each association */ 6858 SCTP_TCB_LOCK(stcb); 6859 sctp_drain_mbufs(stcb); 6860 SCTP_TCB_UNLOCK(stcb); 6861 } 6862 SCTP_INP_RUNLOCK(inp); 6863 } 6864 SCTP_INP_INFO_RUNLOCK(); 6865 CURVNET_RESTORE(); 6866 } 6867 VNET_LIST_RUNLOCK_NOSLEEP(); 6868 NET_EPOCH_EXIT(et); 6869 } 6870 6871 EVENTHANDLER_DEFINE(vm_lowmem, sctp_drain, NULL, LOWMEM_PRI_DEFAULT); 6872 EVENTHANDLER_DEFINE(mbuf_lowmem, sctp_drain, NULL, LOWMEM_PRI_DEFAULT); 6873 6874 /* 6875 * start a new iterator 6876 * iterates through all endpoints and associations based on the pcb_state 6877 * flags and asoc_state. "af" (mandatory) is executed for all matching 6878 * assocs and "ef" (optional) is executed when the iterator completes. 6879 * "inpf" (optional) is executed for each new endpoint as it is being 6880 * iterated through. inpe (optional) is called when the inp completes 6881 * its way through all the stcbs. 6882 */ 6883 int 6884 sctp_initiate_iterator(inp_func inpf, 6885 asoc_func af, 6886 inp_func inpe, 6887 uint32_t pcb_state, 6888 uint32_t pcb_features, 6889 uint32_t asoc_state, 6890 void *argp, 6891 uint32_t argi, 6892 end_func ef, 6893 struct sctp_inpcb *s_inp, 6894 uint8_t chunk_output_off) 6895 { 6896 struct sctp_iterator *it = NULL; 6897 6898 if (af == NULL) { 6899 return (-1); 6900 } 6901 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { 6902 SCTP_PRINTF("%s: abort on initialize being %d\n", __func__, 6903 SCTP_BASE_VAR(sctp_pcb_initialized)); 6904 return (-1); 6905 } 6906 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator), 6907 SCTP_M_ITER); 6908 if (it == NULL) { 6909 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); 6910 return (-1); 6911 } 6912 memset(it, 0, sizeof(*it)); 6913 it->function_assoc = af; 6914 it->function_inp = inpf; 6915 if (inpf) 6916 it->done_current_ep = 0; 6917 else 6918 it->done_current_ep = 1; 6919 it->function_atend = ef; 6920 it->pointer = argp; 6921 it->val = argi; 6922 it->pcb_flags = pcb_state; 6923 it->pcb_features = pcb_features; 6924 it->asoc_state = asoc_state; 6925 it->function_inp_end = inpe; 6926 it->no_chunk_output = chunk_output_off; 6927 it->vn = curvnet; 6928 if (s_inp) { 6929 /* Assume lock is held here */ 6930 it->inp = s_inp; 6931 SCTP_INP_INCR_REF(it->inp); 6932 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP; 6933 } else { 6934 SCTP_INP_INFO_RLOCK(); 6935 it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead)); 6936 if (it->inp) { 6937 SCTP_INP_INCR_REF(it->inp); 6938 } 6939 SCTP_INP_INFO_RUNLOCK(); 6940 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP; 6941 } 6942 SCTP_IPI_ITERATOR_WQ_LOCK(); 6943 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { 6944 SCTP_IPI_ITERATOR_WQ_UNLOCK(); 6945 SCTP_PRINTF("%s: rollback on initialize being %d it=%p\n", __func__, 6946 SCTP_BASE_VAR(sctp_pcb_initialized), it); 6947 SCTP_FREE(it, SCTP_M_ITER); 6948 return (-1); 6949 } 6950 TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); 6951 if (sctp_it_ctl.iterator_running == 0) { 6952 sctp_wakeup_iterator(); 6953 } 6954 SCTP_IPI_ITERATOR_WQ_UNLOCK(); 6955 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */ 6956 return (0); 6957 } 6958 6959 /* 6960 * Atomically add flags to the sctp_flags of an inp. 6961 * To be used when the write lock of the inp is not held. 6962 */ 6963 void 6964 sctp_pcb_add_flags(struct sctp_inpcb *inp, uint32_t flags) 6965 { 6966 uint32_t old_flags, new_flags; 6967 6968 do { 6969 old_flags = inp->sctp_flags; 6970 new_flags = old_flags | flags; 6971 } while (atomic_cmpset_int(&inp->sctp_flags, old_flags, new_flags) == 0); 6972 } 6973