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