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