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