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