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