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