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