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