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