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