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