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