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