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