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