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