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