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