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 = NULL; 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(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 /* Its only abandoned if it had data left */ 2693 if (sq->length) 2694 SCTP_STAT_INCR(sctps_left_abandon); 2695 2696 TAILQ_REMOVE(&inp->read_queue, sq, next); 2697 sctp_free_remote_addr(sq->whoFrom); 2698 if (so) 2699 so->so_rcv.sb_cc -= sq->length; 2700 if (sq->data) { 2701 sctp_m_freem(sq->data); 2702 sq->data = NULL; 2703 } 2704 /* 2705 * no need to free the net count, since at this point all 2706 * assoc's are gone. 2707 */ 2708 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq); 2709 SCTP_DECR_READQ_COUNT(); 2710 } 2711 /* Now the sctp_pcb things */ 2712 /* 2713 * free each asoc if it is not already closed/free. we can't use the 2714 * macro here since le_next will get freed as part of the 2715 * sctp_free_assoc() call. 2716 */ 2717 cnt = 0; 2718 if (so) { 2719 #ifdef IPSEC 2720 ipsec4_delete_pcbpolicy(ip_pcb); 2721 #endif /* IPSEC */ 2722 2723 /* Unlocks not needed since the socket is gone now */ 2724 } 2725 if (ip_pcb->inp_options) { 2726 (void)sctp_m_free(ip_pcb->inp_options); 2727 ip_pcb->inp_options = 0; 2728 } 2729 if (ip_pcb->inp_moptions) { 2730 ip_freemoptions(ip_pcb->inp_moptions); 2731 ip_pcb->inp_moptions = 0; 2732 } 2733 #ifdef INET6 2734 if (ip_pcb->inp_vflag & INP_IPV6) { 2735 struct in6pcb *in6p; 2736 2737 in6p = (struct in6pcb *)inp; 2738 ip6_freepcbopts(in6p->in6p_outputopts); 2739 } 2740 #endif /* INET6 */ 2741 ip_pcb->inp_vflag = 0; 2742 /* free up authentication fields */ 2743 if (inp->sctp_ep.local_auth_chunks != NULL) 2744 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 2745 if (inp->sctp_ep.local_hmacs != NULL) 2746 sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2747 2748 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys); 2749 while (shared_key) { 2750 LIST_REMOVE(shared_key, next); 2751 sctp_free_sharedkey(shared_key); 2752 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys); 2753 } 2754 2755 inp_save = LIST_NEXT(inp, sctp_list); 2756 LIST_REMOVE(inp, sctp_list); 2757 2758 /* fix any iterators only after out of the list */ 2759 sctp_iterator_inp_being_freed(inp, inp_save); 2760 /* 2761 * if we have an address list the following will free the list of 2762 * ifaddr's that are set into this ep. Again macro limitations here, 2763 * since the LIST_FOREACH could be a bad idea. 2764 */ 2765 for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL; 2766 laddr = nladdr) { 2767 nladdr = LIST_NEXT(laddr, sctp_nxt_addr); 2768 sctp_remove_laddr(laddr); 2769 } 2770 2771 #ifdef SCTP_TRACK_FREED_ASOCS 2772 /* TEMP CODE */ 2773 for ((asoc = LIST_FIRST(&inp->sctp_asoc_free_list)); asoc != NULL; 2774 asoc = nasoc) { 2775 nasoc = LIST_NEXT(asoc, sctp_tcblist); 2776 LIST_REMOVE(asoc, sctp_tcblist); 2777 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, asoc); 2778 SCTP_DECR_ASOC_COUNT(); 2779 } 2780 /* *** END TEMP CODE *** */ 2781 #endif 2782 /* Now lets see about freeing the EP hash table. */ 2783 if (inp->sctp_tcbhash != NULL) { 2784 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); 2785 inp->sctp_tcbhash = NULL; 2786 } 2787 /* Now we must put the ep memory back into the zone pool */ 2788 SCTP_INP_LOCK_DESTROY(inp); 2789 SCTP_INP_READ_DESTROY(inp); 2790 SCTP_ASOC_CREATE_LOCK_DESTROY(inp); 2791 SCTP_INP_INFO_WUNLOCK(); 2792 2793 SCTP_ITERATOR_UNLOCK(); 2794 2795 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp); 2796 SCTP_DECR_EP_COUNT(); 2797 2798 } 2799 2800 2801 struct sctp_nets * 2802 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr) 2803 { 2804 struct sctp_nets *net; 2805 2806 /* locate the address */ 2807 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 2808 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr)) 2809 return (net); 2810 } 2811 return (NULL); 2812 } 2813 2814 2815 /* 2816 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as 2817 * when a ASCONF arrives that adds it. It will also initialize all the cwnd 2818 * stats of stuff. 2819 */ 2820 int 2821 sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id) 2822 { 2823 struct sctp_ifa *sctp_ifa; 2824 2825 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, 0); 2826 if (sctp_ifa) { 2827 return (1); 2828 } else { 2829 return (0); 2830 } 2831 } 2832 2833 void 2834 sctp_set_initial_cc_param(struct sctp_tcb *stcb, struct sctp_nets *net) 2835 { 2836 net->cwnd = min((net->mtu * 4), max((2 * net->mtu), SCTP_INITIAL_CWND)); 2837 /* we always get at LEAST 2 MTU's */ 2838 if (net->cwnd < (2 * net->mtu)) { 2839 net->cwnd = 2 * net->mtu; 2840 } 2841 net->ssthresh = stcb->asoc.peers_rwnd; 2842 } 2843 2844 2845 int 2846 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr, 2847 int set_scope, int from) 2848 { 2849 /* 2850 * The following is redundant to the same lines in the 2851 * sctp_aloc_assoc() but is needed since other's call the add 2852 * address function 2853 */ 2854 struct sctp_nets *net, *netfirst; 2855 int addr_inscope; 2856 2857 #ifdef SCTP_DEBUG 2858 if (sctp_debug_on & SCTP_DEBUG_PCB1) { 2859 printf("Adding an address (from:%d) to the peer: ", from); 2860 sctp_print_address(newaddr); 2861 } 2862 #endif 2863 2864 netfirst = sctp_findnet(stcb, newaddr); 2865 if (netfirst) { 2866 /* 2867 * Lie and return ok, we don't want to make the association 2868 * go away for this behavior. It will happen in the TCP 2869 * model in a connected socket. It does not reach the hash 2870 * table until after the association is built so it can't be 2871 * found. Mark as reachable, since the initial creation will 2872 * have been cleared and the NOT_IN_ASSOC flag will have 2873 * been added... and we don't want to end up removing it 2874 * back out. 2875 */ 2876 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) { 2877 netfirst->dest_state = (SCTP_ADDR_REACHABLE | 2878 SCTP_ADDR_UNCONFIRMED); 2879 } else { 2880 netfirst->dest_state = SCTP_ADDR_REACHABLE; 2881 } 2882 2883 return (0); 2884 } 2885 addr_inscope = 1; 2886 if (newaddr->sa_family == AF_INET) { 2887 struct sockaddr_in *sin; 2888 2889 sin = (struct sockaddr_in *)newaddr; 2890 if (sin->sin_addr.s_addr == 0) { 2891 /* Invalid address */ 2892 return (-1); 2893 } 2894 /* zero out the bzero area */ 2895 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); 2896 2897 /* assure len is set */ 2898 sin->sin_len = sizeof(struct sockaddr_in); 2899 if (set_scope) { 2900 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE 2901 stcb->ipv4_local_scope = 1; 2902 #else 2903 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) { 2904 stcb->asoc.ipv4_local_scope = 1; 2905 } 2906 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */ 2907 } else { 2908 /* Validate the address is in scope */ 2909 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) && 2910 (stcb->asoc.ipv4_local_scope == 0)) { 2911 addr_inscope = 0; 2912 } 2913 } 2914 } else if (newaddr->sa_family == AF_INET6) { 2915 struct sockaddr_in6 *sin6; 2916 2917 sin6 = (struct sockaddr_in6 *)newaddr; 2918 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 2919 /* Invalid address */ 2920 return (-1); 2921 } 2922 /* assure len is set */ 2923 sin6->sin6_len = sizeof(struct sockaddr_in6); 2924 if (set_scope) { 2925 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) { 2926 stcb->asoc.loopback_scope = 1; 2927 stcb->asoc.local_scope = 0; 2928 stcb->asoc.ipv4_local_scope = 1; 2929 stcb->asoc.site_scope = 1; 2930 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 2931 /* 2932 * If the new destination is a LINK_LOCAL we 2933 * must have common site scope. Don't set 2934 * the local scope since we may not share 2935 * all links, only loopback can do this. 2936 * Links on the local network would also be 2937 * on our private network for v4 too. 2938 */ 2939 stcb->asoc.ipv4_local_scope = 1; 2940 stcb->asoc.site_scope = 1; 2941 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) { 2942 /* 2943 * If the new destination is SITE_LOCAL then 2944 * we must have site scope in common. 2945 */ 2946 stcb->asoc.site_scope = 1; 2947 } 2948 } else { 2949 /* Validate the address is in scope */ 2950 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) && 2951 (stcb->asoc.loopback_scope == 0)) { 2952 addr_inscope = 0; 2953 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && 2954 (stcb->asoc.local_scope == 0)) { 2955 addr_inscope = 0; 2956 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && 2957 (stcb->asoc.site_scope == 0)) { 2958 addr_inscope = 0; 2959 } 2960 } 2961 } else { 2962 /* not supported family type */ 2963 return (-1); 2964 } 2965 net = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_net, struct sctp_nets); 2966 if (net == NULL) { 2967 return (-1); 2968 } 2969 SCTP_INCR_RADDR_COUNT(); 2970 bzero(net, sizeof(*net)); 2971 SCTP_GETTIME_TIMEVAL(&net->start_time); 2972 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len); 2973 if (newaddr->sa_family == AF_INET) { 2974 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport; 2975 } else if (newaddr->sa_family == AF_INET6) { 2976 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport; 2977 } 2978 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id); 2979 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) { 2980 stcb->asoc.loopback_scope = 1; 2981 stcb->asoc.ipv4_local_scope = 1; 2982 stcb->asoc.local_scope = 0; 2983 stcb->asoc.site_scope = 1; 2984 addr_inscope = 1; 2985 } 2986 net->failure_threshold = stcb->asoc.def_net_failure; 2987 if (addr_inscope == 0) { 2988 net->dest_state = (SCTP_ADDR_REACHABLE | 2989 SCTP_ADDR_OUT_OF_SCOPE); 2990 } else { 2991 if (from == SCTP_ADDR_IS_CONFIRMED) 2992 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */ 2993 net->dest_state = SCTP_ADDR_REACHABLE; 2994 else 2995 net->dest_state = SCTP_ADDR_REACHABLE | 2996 SCTP_ADDR_UNCONFIRMED; 2997 } 2998 net->RTO = stcb->asoc.initial_rto; 2999 stcb->asoc.numnets++; 3000 *(&net->ref_count) = 1; 3001 net->tos_flowlabel = 0; 3002 #ifdef INET 3003 if (newaddr->sa_family == AF_INET) 3004 net->tos_flowlabel = stcb->asoc.default_tos; 3005 #endif 3006 #ifdef INET6 3007 if (newaddr->sa_family == AF_INET6) 3008 net->tos_flowlabel = stcb->asoc.default_flowlabel; 3009 #endif 3010 /* Init the timer structure */ 3011 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer); 3012 SCTP_OS_TIMER_INIT(&net->fr_timer.timer); 3013 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer); 3014 3015 /* Now generate a route for this guy */ 3016 /* KAME hack: embed scopeid */ 3017 if (newaddr->sa_family == AF_INET6) { 3018 struct sockaddr_in6 *sin6; 3019 3020 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; 3021 (void)sa6_embedscope(sin6, ip6_use_defzone); 3022 sin6->sin6_scope_id = 0; 3023 } 3024 rtalloc_ign((struct route *)&net->ro, 0UL); 3025 3026 if (newaddr->sa_family == AF_INET6) { 3027 struct sockaddr_in6 *sin6; 3028 3029 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; 3030 (void)sa6_recoverscope(sin6); 3031 } 3032 if ((net->ro.ro_rt) && 3033 (net->ro.ro_rt->rt_ifp)) { 3034 net->mtu = net->ro.ro_rt->rt_ifp->if_mtu; 3035 if (from == SCTP_ALLOC_ASOC) { 3036 stcb->asoc.smallest_mtu = net->mtu; 3037 } 3038 /* start things off to match mtu of interface please. */ 3039 net->ro.ro_rt->rt_rmx.rmx_mtu = net->ro.ro_rt->rt_ifp->if_mtu; 3040 } else { 3041 net->mtu = stcb->asoc.smallest_mtu; 3042 } 3043 3044 if (stcb->asoc.smallest_mtu > net->mtu) { 3045 stcb->asoc.smallest_mtu = net->mtu; 3046 } 3047 /* 3048 * We take the max of the burst limit times a MTU or the 3049 * INITIAL_CWND. We then limit this to 4 MTU's of sending. 3050 */ 3051 sctp_set_initial_cc_param(stcb, net); 3052 3053 3054 #if defined(SCTP_CWND_MONITOR) || defined(SCTP_CWND_LOGGING) 3055 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_INITIALIZATION); 3056 #endif 3057 3058 /* 3059 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning 3060 * of assoc (2005/06/27, iyengar@cis.udel.edu) 3061 */ 3062 net->find_pseudo_cumack = 1; 3063 net->find_rtx_pseudo_cumack = 1; 3064 net->src_addr_selected = 0; 3065 netfirst = TAILQ_FIRST(&stcb->asoc.nets); 3066 if (net->ro.ro_rt == NULL) { 3067 /* Since we have no route put it at the back */ 3068 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next); 3069 } else if (netfirst == NULL) { 3070 /* We are the first one in the pool. */ 3071 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); 3072 } else if (netfirst->ro.ro_rt == NULL) { 3073 /* 3074 * First one has NO route. Place this one ahead of the first 3075 * one. 3076 */ 3077 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); 3078 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) { 3079 /* 3080 * This one has a different interface than the one at the 3081 * top of the list. Place it ahead. 3082 */ 3083 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); 3084 } else { 3085 /* 3086 * Ok we have the same interface as the first one. Move 3087 * forward until we find either a) one with a NULL route... 3088 * insert ahead of that b) one with a different ifp.. insert 3089 * after that. c) end of the list.. insert at the tail. 3090 */ 3091 struct sctp_nets *netlook; 3092 3093 do { 3094 netlook = TAILQ_NEXT(netfirst, sctp_next); 3095 if (netlook == NULL) { 3096 /* End of the list */ 3097 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next); 3098 break; 3099 } else if (netlook->ro.ro_rt == NULL) { 3100 /* next one has NO route */ 3101 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next); 3102 break; 3103 } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) { 3104 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook, 3105 net, sctp_next); 3106 break; 3107 } 3108 /* Shift forward */ 3109 netfirst = netlook; 3110 } while (netlook != NULL); 3111 } 3112 3113 /* got to have a primary set */ 3114 if (stcb->asoc.primary_destination == 0) { 3115 stcb->asoc.primary_destination = net; 3116 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) && 3117 (net->ro.ro_rt) && 3118 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) { 3119 /* No route to current primary adopt new primary */ 3120 stcb->asoc.primary_destination = net; 3121 } 3122 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, 3123 net); 3124 /* Validate primary is first */ 3125 net = TAILQ_FIRST(&stcb->asoc.nets); 3126 if ((net != stcb->asoc.primary_destination) && 3127 (stcb->asoc.primary_destination)) { 3128 /* 3129 * first one on the list is NOT the primary sctp_cmpaddr() 3130 * is much more efficent if the primary is the first on the 3131 * list, make it so. 3132 */ 3133 TAILQ_REMOVE(&stcb->asoc.nets, 3134 stcb->asoc.primary_destination, sctp_next); 3135 TAILQ_INSERT_HEAD(&stcb->asoc.nets, 3136 stcb->asoc.primary_destination, sctp_next); 3137 } 3138 return (0); 3139 } 3140 3141 3142 /* 3143 * allocate an association and add it to the endpoint. The caller must be 3144 * careful to add all additional addresses once they are know right away or 3145 * else the assoc will be may experience a blackout scenario. 3146 */ 3147 struct sctp_tcb * 3148 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr, 3149 int for_a_init, int *error, uint32_t override_tag, uint32_t vrf) 3150 { 3151 struct sctp_tcb *stcb; 3152 struct sctp_association *asoc; 3153 struct sctpasochead *head; 3154 uint16_t rport; 3155 int err; 3156 3157 /* 3158 * Assumption made here: Caller has done a 3159 * sctp_findassociation_ep_addr(ep, addr's); to make sure the 3160 * address does not exist already. 3161 */ 3162 if (sctppcbinfo.ipi_count_asoc >= SCTP_MAX_NUM_OF_ASOC) { 3163 /* Hit max assoc, sorry no more */ 3164 *error = ENOBUFS; 3165 return (NULL); 3166 } 3167 SCTP_INP_RLOCK(inp); 3168 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) { 3169 /* 3170 * If its in the TCP pool, its NOT allowed to create an 3171 * association. The parent listener needs to call 3172 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled 3173 * off, or connected one does this.. its an error. 3174 */ 3175 SCTP_INP_RUNLOCK(inp); 3176 *error = EINVAL; 3177 return (NULL); 3178 } 3179 #ifdef SCTP_DEBUG 3180 if (sctp_debug_on & SCTP_DEBUG_PCB3) { 3181 printf("Allocate an association for peer:"); 3182 if (firstaddr) 3183 sctp_print_address(firstaddr); 3184 else 3185 printf("None\n"); 3186 printf("Port:%d\n", 3187 ntohs(((struct sockaddr_in *)firstaddr)->sin_port)); 3188 } 3189 #endif /* SCTP_DEBUG */ 3190 if (firstaddr->sa_family == AF_INET) { 3191 struct sockaddr_in *sin; 3192 3193 sin = (struct sockaddr_in *)firstaddr; 3194 if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) { 3195 /* Invalid address */ 3196 SCTP_INP_RUNLOCK(inp); 3197 *error = EINVAL; 3198 return (NULL); 3199 } 3200 rport = sin->sin_port; 3201 } else if (firstaddr->sa_family == AF_INET6) { 3202 struct sockaddr_in6 *sin6; 3203 3204 sin6 = (struct sockaddr_in6 *)firstaddr; 3205 if ((sin6->sin6_port == 0) || 3206 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) { 3207 /* Invalid address */ 3208 SCTP_INP_RUNLOCK(inp); 3209 *error = EINVAL; 3210 return (NULL); 3211 } 3212 rport = sin6->sin6_port; 3213 } else { 3214 /* not supported family type */ 3215 SCTP_INP_RUNLOCK(inp); 3216 *error = EINVAL; 3217 return (NULL); 3218 } 3219 SCTP_INP_RUNLOCK(inp); 3220 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) { 3221 /* 3222 * If you have not performed a bind, then we need to do the 3223 * ephemerial bind for you. 3224 */ 3225 if ((err = sctp_inpcb_bind(inp->sctp_socket, 3226 (struct sockaddr *)NULL, 3227 (struct thread *)NULL 3228 ))) { 3229 /* bind error, probably perm */ 3230 *error = err; 3231 return (NULL); 3232 } 3233 } 3234 stcb = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_asoc, struct sctp_tcb); 3235 if (stcb == NULL) { 3236 /* out of memory? */ 3237 *error = ENOMEM; 3238 return (NULL); 3239 } 3240 SCTP_INCR_ASOC_COUNT(); 3241 3242 bzero(stcb, sizeof(*stcb)); 3243 asoc = &stcb->asoc; 3244 SCTP_TCB_LOCK_INIT(stcb); 3245 SCTP_TCB_SEND_LOCK_INIT(stcb); 3246 /* setup back pointer's */ 3247 stcb->sctp_ep = inp; 3248 stcb->sctp_socket = inp->sctp_socket; 3249 if ((err = sctp_init_asoc(inp, asoc, for_a_init, override_tag, vrf))) { 3250 /* failed */ 3251 SCTP_TCB_LOCK_DESTROY(stcb); 3252 SCTP_TCB_SEND_LOCK_DESTROY(stcb); 3253 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb); 3254 SCTP_DECR_ASOC_COUNT(); 3255 *error = err; 3256 return (NULL); 3257 } 3258 /* and the port */ 3259 stcb->rport = rport; 3260 SCTP_INP_INFO_WLOCK(); 3261 SCTP_INP_WLOCK(inp); 3262 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 3263 /* inpcb freed while alloc going on */ 3264 SCTP_TCB_LOCK_DESTROY(stcb); 3265 SCTP_TCB_SEND_LOCK_DESTROY(stcb); 3266 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb); 3267 SCTP_INP_WUNLOCK(inp); 3268 SCTP_INP_INFO_WUNLOCK(); 3269 SCTP_DECR_ASOC_COUNT(); 3270 *error = EINVAL; 3271 return (NULL); 3272 } 3273 SCTP_TCB_LOCK(stcb); 3274 3275 /* now that my_vtag is set, add it to the hash */ 3276 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 3277 sctppcbinfo.hashasocmark)]; 3278 /* put it in the bucket in the vtag hash of assoc's for the system */ 3279 LIST_INSERT_HEAD(head, stcb, sctp_asocs); 3280 SCTP_INP_INFO_WUNLOCK(); 3281 3282 if ((err = sctp_add_remote_addr(stcb, firstaddr, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) { 3283 /* failure.. memory error? */ 3284 if (asoc->strmout) 3285 SCTP_FREE(asoc->strmout); 3286 if (asoc->mapping_array) 3287 SCTP_FREE(asoc->mapping_array); 3288 3289 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb); 3290 SCTP_DECR_ASOC_COUNT(); 3291 SCTP_TCB_LOCK_DESTROY(stcb); 3292 SCTP_TCB_SEND_LOCK_DESTROY(stcb); 3293 SCTP_INP_WUNLOCK(inp); 3294 *error = ENOBUFS; 3295 return (NULL); 3296 } 3297 /* Init all the timers */ 3298 SCTP_OS_TIMER_INIT(&asoc->hb_timer.timer); 3299 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer); 3300 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer); 3301 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer); 3302 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer); 3303 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer); 3304 SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer); 3305 3306 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist); 3307 /* now file the port under the hash as well */ 3308 if (inp->sctp_tcbhash != NULL) { 3309 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport, 3310 inp->sctp_hashmark)]; 3311 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash); 3312 } 3313 SCTP_INP_WUNLOCK(inp); 3314 #ifdef SCTP_DEBUG 3315 if (sctp_debug_on & SCTP_DEBUG_PCB1) { 3316 printf("Association %p now allocated\n", stcb); 3317 } 3318 #endif 3319 return (stcb); 3320 } 3321 3322 3323 void 3324 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net) 3325 { 3326 struct sctp_association *asoc; 3327 3328 asoc = &stcb->asoc; 3329 asoc->numnets--; 3330 TAILQ_REMOVE(&asoc->nets, net, sctp_next); 3331 if (net == asoc->primary_destination) { 3332 /* Reset primary */ 3333 struct sctp_nets *lnet; 3334 3335 lnet = TAILQ_FIRST(&asoc->nets); 3336 /* Try to find a confirmed primary */ 3337 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0); 3338 } 3339 if (net == asoc->last_data_chunk_from) { 3340 /* Reset primary */ 3341 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets); 3342 } 3343 if (net == asoc->last_control_chunk_from) { 3344 /* Clear net */ 3345 asoc->last_control_chunk_from = NULL; 3346 } 3347 sctp_free_remote_addr(net); 3348 } 3349 3350 /* 3351 * remove a remote endpoint address from an association, it will fail if the 3352 * address does not exist. 3353 */ 3354 int 3355 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr) 3356 { 3357 /* 3358 * Here we need to remove a remote address. This is quite simple, we 3359 * first find it in the list of address for the association 3360 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE 3361 * on that item. Note we do not allow it to be removed if there are 3362 * no other addresses. 3363 */ 3364 struct sctp_association *asoc; 3365 struct sctp_nets *net, *net_tmp; 3366 3367 asoc = &stcb->asoc; 3368 3369 /* locate the address */ 3370 for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) { 3371 net_tmp = TAILQ_NEXT(net, sctp_next); 3372 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) { 3373 continue; 3374 } 3375 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr, 3376 remaddr)) { 3377 /* we found the guy */ 3378 if (asoc->numnets < 2) { 3379 /* Must have at LEAST two remote addresses */ 3380 return (-1); 3381 } else { 3382 sctp_remove_net(stcb, net); 3383 return (0); 3384 } 3385 } 3386 } 3387 /* not found. */ 3388 return (-2); 3389 } 3390 3391 3392 void 3393 sctp_add_vtag_to_timewait(struct sctp_inpcb *inp, uint32_t tag, uint32_t time) 3394 { 3395 struct sctpvtaghead *chain; 3396 struct sctp_tagblock *twait_block; 3397 struct timeval now; 3398 int set, i; 3399 3400 SCTP_GETTIME_TIMEVAL(&now); 3401 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; 3402 set = 0; 3403 if (!SCTP_LIST_EMPTY(chain)) { 3404 /* Block(s) present, lets find space, and expire on the fly */ 3405 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { 3406 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { 3407 if ((twait_block->vtag_block[i].v_tag == 0) && 3408 !set) { 3409 twait_block->vtag_block[i].tv_sec_at_expire = 3410 now.tv_sec + time; 3411 twait_block->vtag_block[i].v_tag = tag; 3412 set = 1; 3413 } else if ((twait_block->vtag_block[i].v_tag) && 3414 ((long)twait_block->vtag_block[i].tv_sec_at_expire > 3415 now.tv_sec)) { 3416 /* Audit expires this guy */ 3417 twait_block->vtag_block[i].tv_sec_at_expire = 0; 3418 twait_block->vtag_block[i].v_tag = 0; 3419 if (set == 0) { 3420 /* Reuse it for my new tag */ 3421 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + SCTP_TIME_WAIT; 3422 twait_block->vtag_block[0].v_tag = tag; 3423 set = 1; 3424 } 3425 } 3426 } 3427 if (set) { 3428 /* 3429 * We only do up to the block where we can 3430 * place our tag for audits 3431 */ 3432 break; 3433 } 3434 } 3435 } 3436 /* Need to add a new block to chain */ 3437 if (!set) { 3438 SCTP_MALLOC(twait_block, struct sctp_tagblock *, 3439 sizeof(struct sctp_tagblock), "TimeWait"); 3440 if (twait_block == NULL) { 3441 return; 3442 } 3443 memset(twait_block, 0, sizeof(struct sctp_tagblock)); 3444 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock); 3445 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + 3446 SCTP_TIME_WAIT; 3447 twait_block->vtag_block[0].v_tag = tag; 3448 } 3449 } 3450 3451 3452 static void 3453 sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb) 3454 { 3455 struct sctp_iterator *it; 3456 3457 /* 3458 * Unlock the tcb lock we do this so we avoid a dead lock scenario 3459 * where the iterator is waiting on the TCB lock and the TCB lock is 3460 * waiting on the iterator lock. 3461 */ 3462 it = stcb->asoc.stcb_starting_point_for_iterator; 3463 if (it == NULL) { 3464 return; 3465 } 3466 if (it->inp != stcb->sctp_ep) { 3467 /* hmm, focused on the wrong one? */ 3468 return; 3469 } 3470 if (it->stcb != stcb) { 3471 return; 3472 } 3473 it->stcb = LIST_NEXT(stcb, sctp_tcblist); 3474 if (it->stcb == NULL) { 3475 /* done with all asoc's in this assoc */ 3476 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { 3477 it->inp = NULL; 3478 } else { 3479 it->inp = LIST_NEXT(inp, sctp_list); 3480 } 3481 } 3482 } 3483 3484 3485 /* 3486 * Free the association after un-hashing the remote port. 3487 */ 3488 int 3489 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location) 3490 { 3491 int i; 3492 struct sctp_association *asoc; 3493 struct sctp_nets *net, *prev; 3494 struct sctp_laddr *laddr; 3495 struct sctp_tmit_chunk *chk; 3496 struct sctp_asconf_addr *aparam; 3497 struct sctp_stream_reset_list *liste; 3498 struct sctp_queued_to_read *sq; 3499 struct sctp_stream_queue_pending *sp; 3500 sctp_sharedkey_t *shared_key; 3501 struct socket *so; 3502 int ccnt = 0; 3503 int cnt = 0; 3504 3505 /* first, lets purge the entry from the hash table. */ 3506 3507 #ifdef SCTP_LOG_CLOSING 3508 sctp_log_closing(inp, stcb, 6); 3509 #endif 3510 if (stcb->asoc.state == 0) { 3511 #ifdef SCTP_LOG_CLOSING 3512 sctp_log_closing(inp, NULL, 7); 3513 #endif 3514 /* there is no asoc, really TSNH :-0 */ 3515 return (1); 3516 } 3517 /* TEMP CODE */ 3518 if (stcb->freed_from_where == 0) { 3519 /* Only record the first place free happened from */ 3520 stcb->freed_from_where = from_location; 3521 } 3522 /* TEMP CODE */ 3523 3524 asoc = &stcb->asoc; 3525 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 3526 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) 3527 /* nothing around */ 3528 so = NULL; 3529 else 3530 so = inp->sctp_socket; 3531 3532 /* 3533 * We used timer based freeing if a reader or writer is in the way. 3534 * So we first check if we are actually being called from a timer, 3535 * if so we abort early if a reader or writer is still in the way. 3536 */ 3537 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) && 3538 (from_inpcbfree == SCTP_NORMAL_PROC)) { 3539 /* 3540 * is it the timer driving us? if so are the reader/writers 3541 * gone? 3542 */ 3543 if (stcb->asoc.refcnt) { 3544 /* nope, reader or writer in the way */ 3545 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); 3546 /* no asoc destroyed */ 3547 SCTP_TCB_UNLOCK(stcb); 3548 #ifdef SCTP_LOG_CLOSING 3549 sctp_log_closing(inp, stcb, 8); 3550 #endif 3551 return (0); 3552 } 3553 } 3554 /* now clean up any other timers */ 3555 SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer); 3556 asoc->hb_timer.self = NULL; 3557 SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer); 3558 asoc->dack_timer.self = NULL; 3559 SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer); 3560 /*- 3561 * For stream reset we don't blast this unless 3562 * it is a str-reset timer, it might be the 3563 * free-asoc timer which we DON'T want to 3564 * disturb. 3565 */ 3566 if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET) 3567 asoc->strreset_timer.self = NULL; 3568 SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer); 3569 asoc->asconf_timer.self = NULL; 3570 SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer); 3571 asoc->autoclose_timer.self = NULL; 3572 SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer); 3573 asoc->shut_guard_timer.self = NULL; 3574 SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer); 3575 asoc->delayed_event_timer.self = NULL; 3576 TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 3577 SCTP_OS_TIMER_STOP(&net->fr_timer.timer); 3578 net->fr_timer.self = NULL; 3579 SCTP_OS_TIMER_STOP(&net->rxt_timer.timer); 3580 net->rxt_timer.self = NULL; 3581 SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer); 3582 net->pmtu_timer.self = NULL; 3583 } 3584 /* Now the read queue needs to be cleaned up (only once) */ 3585 cnt = 0; 3586 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) { 3587 SCTP_INP_READ_LOCK(inp); 3588 TAILQ_FOREACH(sq, &inp->read_queue, next) { 3589 if (sq->stcb == stcb) { 3590 sq->do_not_ref_stcb = 1; 3591 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn; 3592 /* 3593 * If there is no end, there never will be 3594 * now. 3595 */ 3596 if (sq->end_added == 0) { 3597 /* Held for PD-API clear that. */ 3598 sq->pdapi_aborted = 1; 3599 sq->held_length = 0; 3600 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT)) { 3601 /* 3602 * Need to add a PD-API 3603 * aborted indication. 3604 * Setting the control_pdapi 3605 * assures that it will be 3606 * added right after this 3607 * msg. 3608 */ 3609 uint32_t strseq; 3610 3611 stcb->asoc.control_pdapi = sq; 3612 strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn; 3613 sctp_notify_partial_delivery_indication(stcb, 3614 SCTP_PARTIAL_DELIVERY_ABORTED, 1, strseq); 3615 stcb->asoc.control_pdapi = NULL; 3616 } 3617 } 3618 /* Add an end to wake them */ 3619 sq->end_added = 1; 3620 cnt++; 3621 } 3622 } 3623 SCTP_INP_READ_UNLOCK(inp); 3624 if (stcb->block_entry) { 3625 cnt++; 3626 stcb->block_entry->error = ECONNRESET; 3627 stcb->block_entry = NULL; 3628 } 3629 } 3630 stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED; 3631 if ((from_inpcbfree != SCTP_PCBFREE_FORCE) && (stcb->asoc.refcnt)) { 3632 /* 3633 * reader or writer in the way, we have hopefully given him 3634 * something to chew on above. 3635 */ 3636 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); 3637 SCTP_TCB_UNLOCK(stcb); 3638 if (so) { 3639 SCTP_INP_RLOCK(inp); 3640 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 3641 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) 3642 /* nothing around */ 3643 so = NULL; 3644 if (so) { 3645 /* Wake any reader/writers */ 3646 sctp_sorwakeup(inp, so); 3647 sctp_sowwakeup(inp, so); 3648 } 3649 SCTP_INP_RUNLOCK(inp); 3650 3651 } 3652 #ifdef SCTP_LOG_CLOSING 3653 sctp_log_closing(inp, stcb, 9); 3654 #endif 3655 /* no asoc destroyed */ 3656 return (0); 3657 } 3658 #ifdef SCTP_LOG_CLOSING 3659 sctp_log_closing(inp, stcb, 10); 3660 #endif 3661 /* 3662 * When I reach here, no others want to kill the assoc yet.. and I 3663 * own the lock. Now its possible an abort comes in when I do the 3664 * lock exchange below to grab all the locks to do the final take 3665 * out. to prevent this we increment the count, which will start a 3666 * timer and blow out above thus assuring us that we hold exclusive 3667 * killing of the asoc. Note that after getting back the TCB lock we 3668 * will go ahead and increment the counter back up and stop any 3669 * timer a passing stranger may have started :-S 3670 */ 3671 if (from_inpcbfree == SCTP_NORMAL_PROC) { 3672 atomic_add_int(&stcb->asoc.refcnt, 1); 3673 3674 SCTP_TCB_UNLOCK(stcb); 3675 3676 SCTP_ITERATOR_LOCK(); 3677 SCTP_INP_INFO_WLOCK(); 3678 SCTP_INP_WLOCK(inp); 3679 SCTP_TCB_LOCK(stcb); 3680 } 3681 /* Double check the GONE flag */ 3682 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 3683 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) 3684 /* nothing around */ 3685 so = NULL; 3686 3687 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 3688 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 3689 /* 3690 * For TCP type we need special handling when we are 3691 * connected. We also include the peel'ed off ones to. 3692 */ 3693 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 3694 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED; 3695 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED; 3696 if (so) { 3697 SOCK_LOCK(so); 3698 if (so->so_rcv.sb_cc == 0) { 3699 so->so_state &= ~(SS_ISCONNECTING | 3700 SS_ISDISCONNECTING | 3701 SS_ISCONFIRMING | 3702 SS_ISCONNECTED); 3703 } 3704 SOCK_UNLOCK(so); 3705 sctp_sowwakeup(inp, so); 3706 sctp_sorwakeup(inp, so); 3707 wakeup(&so->so_timeo); 3708 } 3709 } 3710 } 3711 /* 3712 * Make it invalid too, that way if its about to run it will abort 3713 * and return. 3714 */ 3715 sctp_iterator_asoc_being_freed(inp, stcb); 3716 /* re-increment the lock */ 3717 if (from_inpcbfree == SCTP_NORMAL_PROC) { 3718 atomic_add_int(&stcb->asoc.refcnt, -1); 3719 } 3720 asoc->state = 0; 3721 if (inp->sctp_tcbhash) { 3722 LIST_REMOVE(stcb, sctp_tcbhash); 3723 } 3724 if (stcb->asoc.in_restart_hash) { 3725 LIST_REMOVE(stcb, sctp_tcbrestarhash); 3726 } 3727 /* Now lets remove it from the list of ALL associations in the EP */ 3728 LIST_REMOVE(stcb, sctp_tcblist); 3729 if (from_inpcbfree == SCTP_NORMAL_PROC) { 3730 SCTP_INP_INCR_REF(inp); 3731 SCTP_INP_WUNLOCK(inp); 3732 SCTP_ITERATOR_UNLOCK(); 3733 } 3734 /* pull from vtag hash */ 3735 LIST_REMOVE(stcb, sctp_asocs); 3736 sctp_add_vtag_to_timewait(inp, asoc->my_vtag, SCTP_TIME_WAIT); 3737 3738 3739 /* 3740 * Now restop the timers to be sure - this is paranoia at is finest! 3741 */ 3742 SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer); 3743 SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer); 3744 SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer); 3745 SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer); 3746 SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer); 3747 SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer); 3748 SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer); 3749 SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer); 3750 3751 TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 3752 SCTP_OS_TIMER_STOP(&net->fr_timer.timer); 3753 SCTP_OS_TIMER_STOP(&net->rxt_timer.timer); 3754 SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer); 3755 } 3756 3757 asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE; 3758 prev = NULL; 3759 /* 3760 * The chunk lists and such SHOULD be empty but we check them just 3761 * in case. 3762 */ 3763 /* anything on the wheel needs to be removed */ 3764 for (i = 0; i < asoc->streamoutcnt; i++) { 3765 struct sctp_stream_out *outs; 3766 3767 outs = &asoc->strmout[i]; 3768 /* now clean up any chunks here */ 3769 sp = TAILQ_FIRST(&outs->outqueue); 3770 while (sp) { 3771 TAILQ_REMOVE(&outs->outqueue, sp, next); 3772 if (sp->data) { 3773 sctp_m_freem(sp->data); 3774 sp->data = NULL; 3775 sp->tail_mbuf = NULL; 3776 } 3777 sctp_free_remote_addr(sp->net); 3778 sctp_free_spbufspace(stcb, asoc, sp); 3779 /* Free the zone stuff */ 3780 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_strmoq, sp); 3781 SCTP_DECR_STRMOQ_COUNT(); 3782 sp = TAILQ_FIRST(&outs->outqueue); 3783 } 3784 } 3785 3786 while ((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) { 3787 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp); 3788 SCTP_FREE(liste); 3789 } 3790 3791 sq = TAILQ_FIRST(&asoc->pending_reply_queue); 3792 while (sq) { 3793 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next); 3794 if (sq->data) { 3795 sctp_m_freem(sq->data); 3796 sq->data = NULL; 3797 } 3798 sctp_free_remote_addr(sq->whoFrom); 3799 sq->whoFrom = NULL; 3800 sq->stcb = NULL; 3801 /* Free the ctl entry */ 3802 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq); 3803 SCTP_DECR_READQ_COUNT(); 3804 sq = TAILQ_FIRST(&asoc->pending_reply_queue); 3805 } 3806 3807 chk = TAILQ_FIRST(&asoc->free_chunks); 3808 while (chk) { 3809 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next); 3810 if (chk->data) { 3811 sctp_m_freem(chk->data); 3812 chk->data = NULL; 3813 } 3814 ccnt++; 3815 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk); 3816 SCTP_DECR_CHK_COUNT(); 3817 atomic_subtract_int(&sctppcbinfo.ipi_free_chunks, 1); 3818 asoc->free_chunk_cnt--; 3819 chk = TAILQ_FIRST(&asoc->free_chunks); 3820 } 3821 /* pending send queue SHOULD be empty */ 3822 if (!TAILQ_EMPTY(&asoc->send_queue)) { 3823 chk = TAILQ_FIRST(&asoc->send_queue); 3824 while (chk) { 3825 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); 3826 if (chk->data) { 3827 sctp_m_freem(chk->data); 3828 chk->data = NULL; 3829 } 3830 ccnt++; 3831 sctp_free_remote_addr(chk->whoTo); 3832 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk); 3833 SCTP_DECR_CHK_COUNT(); 3834 chk = TAILQ_FIRST(&asoc->send_queue); 3835 } 3836 } 3837 /* 3838 if(ccnt) { 3839 printf("Freed %d from send_queue\n", ccnt); 3840 ccnt = 0; 3841 } 3842 */ 3843 /* sent queue SHOULD be empty */ 3844 if (!TAILQ_EMPTY(&asoc->sent_queue)) { 3845 chk = TAILQ_FIRST(&asoc->sent_queue); 3846 while (chk) { 3847 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next); 3848 if (chk->data) { 3849 sctp_m_freem(chk->data); 3850 chk->data = NULL; 3851 } 3852 ccnt++; 3853 sctp_free_remote_addr(chk->whoTo); 3854 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk); 3855 SCTP_DECR_CHK_COUNT(); 3856 chk = TAILQ_FIRST(&asoc->sent_queue); 3857 } 3858 } 3859 /* 3860 if(ccnt) { 3861 printf("Freed %d from sent_queue\n", ccnt); 3862 ccnt = 0; 3863 } 3864 */ 3865 /* control queue MAY not be empty */ 3866 if (!TAILQ_EMPTY(&asoc->control_send_queue)) { 3867 chk = TAILQ_FIRST(&asoc->control_send_queue); 3868 while (chk) { 3869 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); 3870 if (chk->data) { 3871 sctp_m_freem(chk->data); 3872 chk->data = NULL; 3873 } 3874 ccnt++; 3875 sctp_free_remote_addr(chk->whoTo); 3876 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk); 3877 SCTP_DECR_CHK_COUNT(); 3878 chk = TAILQ_FIRST(&asoc->control_send_queue); 3879 } 3880 } 3881 /* 3882 if(ccnt) { 3883 printf("Freed %d from ctrl_queue\n", ccnt); 3884 ccnt = 0; 3885 } 3886 */ 3887 if (!TAILQ_EMPTY(&asoc->reasmqueue)) { 3888 chk = TAILQ_FIRST(&asoc->reasmqueue); 3889 while (chk) { 3890 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next); 3891 if (chk->data) { 3892 sctp_m_freem(chk->data); 3893 chk->data = NULL; 3894 } 3895 sctp_free_remote_addr(chk->whoTo); 3896 ccnt++; 3897 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk); 3898 SCTP_DECR_CHK_COUNT(); 3899 chk = TAILQ_FIRST(&asoc->reasmqueue); 3900 } 3901 } 3902 /* 3903 if(ccnt) { 3904 printf("Freed %d from reasm_queue\n", ccnt); 3905 ccnt = 0; 3906 } 3907 */ 3908 if (asoc->mapping_array) { 3909 SCTP_FREE(asoc->mapping_array); 3910 asoc->mapping_array = NULL; 3911 } 3912 /* the stream outs */ 3913 if (asoc->strmout) { 3914 SCTP_FREE(asoc->strmout); 3915 asoc->strmout = NULL; 3916 } 3917 asoc->streamoutcnt = 0; 3918 if (asoc->strmin) { 3919 struct sctp_queued_to_read *ctl; 3920 3921 for (i = 0; i < asoc->streamincnt; i++) { 3922 if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) { 3923 /* We have somethings on the streamin queue */ 3924 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 3925 while (ctl) { 3926 TAILQ_REMOVE(&asoc->strmin[i].inqueue, 3927 ctl, next); 3928 sctp_free_remote_addr(ctl->whoFrom); 3929 if (ctl->data) { 3930 sctp_m_freem(ctl->data); 3931 ctl->data = NULL; 3932 } 3933 /* 3934 * We don't free the address here 3935 * since all the net's were freed 3936 * above. 3937 */ 3938 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl); 3939 SCTP_DECR_READQ_COUNT(); 3940 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 3941 } 3942 } 3943 } 3944 SCTP_FREE(asoc->strmin); 3945 asoc->strmin = NULL; 3946 } 3947 asoc->streamincnt = 0; 3948 while (!TAILQ_EMPTY(&asoc->nets)) { 3949 net = TAILQ_FIRST(&asoc->nets); 3950 /* pull from list */ 3951 if ((sctppcbinfo.ipi_count_raddr == 0) || (prev == net)) { 3952 #ifdef INVARIANTS 3953 panic("no net's left alloc'ed, or list points to itself"); 3954 #endif 3955 break; 3956 } 3957 prev = net; 3958 TAILQ_REMOVE(&asoc->nets, net, sctp_next); 3959 sctp_free_remote_addr(net); 3960 } 3961 3962 while (!SCTP_LIST_EMPTY(&asoc->sctp_restricted_addrs)) { 3963 laddr = LIST_FIRST(&asoc->sctp_restricted_addrs); 3964 sctp_remove_laddr(laddr); 3965 } 3966 3967 /* pending asconf (address) parameters */ 3968 while (!TAILQ_EMPTY(&asoc->asconf_queue)) { 3969 aparam = TAILQ_FIRST(&asoc->asconf_queue); 3970 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next); 3971 SCTP_FREE(aparam); 3972 } 3973 if (asoc->last_asconf_ack_sent != NULL) { 3974 sctp_m_freem(asoc->last_asconf_ack_sent); 3975 asoc->last_asconf_ack_sent = NULL; 3976 } 3977 /* clean up auth stuff */ 3978 if (asoc->local_hmacs) 3979 sctp_free_hmaclist(asoc->local_hmacs); 3980 if (asoc->peer_hmacs) 3981 sctp_free_hmaclist(asoc->peer_hmacs); 3982 3983 if (asoc->local_auth_chunks) 3984 sctp_free_chunklist(asoc->local_auth_chunks); 3985 if (asoc->peer_auth_chunks) 3986 sctp_free_chunklist(asoc->peer_auth_chunks); 3987 3988 sctp_free_authinfo(&asoc->authinfo); 3989 3990 shared_key = LIST_FIRST(&asoc->shared_keys); 3991 while (shared_key) { 3992 LIST_REMOVE(shared_key, next); 3993 sctp_free_sharedkey(shared_key); 3994 shared_key = LIST_FIRST(&asoc->shared_keys); 3995 } 3996 3997 /* Insert new items here :> */ 3998 3999 /* Get rid of LOCK */ 4000 SCTP_TCB_LOCK_DESTROY(stcb); 4001 SCTP_TCB_SEND_LOCK_DESTROY(stcb); 4002 if (from_inpcbfree == SCTP_NORMAL_PROC) { 4003 SCTP_INP_INFO_WUNLOCK(); 4004 SCTP_INP_RLOCK(inp); 4005 } 4006 #ifdef SCTP_TRACK_FREED_ASOCS 4007 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4008 /* now clean up the tasoc itself */ 4009 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb); 4010 SCTP_DECR_ASOC_COUNT(); 4011 } else { 4012 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist); 4013 } 4014 #else 4015 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb); 4016 SCTP_DECR_ASOC_COUNT(); 4017 #endif 4018 if (from_inpcbfree == SCTP_NORMAL_PROC) { 4019 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4020 /* 4021 * If its NOT the inp_free calling us AND sctp_close 4022 * as been called, we call back... 4023 */ 4024 SCTP_INP_RUNLOCK(inp); 4025 /* 4026 * This will start the kill timer (if we are the 4027 * lastone) since we hold an increment yet. But this 4028 * is the only safe way to do this since otherwise 4029 * if the socket closes at the same time we are here 4030 * we might collide in the cleanup. 4031 */ 4032 sctp_inpcb_free(inp, 0, 0); 4033 SCTP_INP_DECR_REF(inp); 4034 goto out_of; 4035 } else { 4036 /* The socket is still open. */ 4037 SCTP_INP_DECR_REF(inp); 4038 } 4039 } 4040 if (from_inpcbfree == SCTP_NORMAL_PROC) { 4041 SCTP_INP_RUNLOCK(inp); 4042 } 4043 out_of: 4044 /* destroyed the asoc */ 4045 #ifdef SCTP_LOG_CLOSING 4046 sctp_log_closing(inp, NULL, 11); 4047 #endif 4048 return (1); 4049 } 4050 4051 4052 4053 /* 4054 * determine if a destination is "reachable" based upon the addresses bound 4055 * to the current endpoint (e.g. only v4 or v6 currently bound) 4056 */ 4057 /* 4058 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use 4059 * assoc level v4/v6 flags, as the assoc *may* not have the same address 4060 * types bound as its endpoint 4061 */ 4062 int 4063 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr) 4064 { 4065 struct sctp_inpcb *inp; 4066 int answer; 4067 4068 /* 4069 * No locks here, the TCB, in all cases is already locked and an 4070 * assoc is up. There is either a INP lock by the caller applied (in 4071 * asconf case when deleting an address) or NOT in the HB case, 4072 * however if HB then the INP increment is up and the INP will not 4073 * be removed (on top of the fact that we have a TCB lock). So we 4074 * only want to read the sctp_flags, which is either bound-all or 4075 * not.. no protection needed since once an assoc is up you can't be 4076 * changing your binding. 4077 */ 4078 inp = stcb->sctp_ep; 4079 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 4080 /* if bound all, destination is not restricted */ 4081 /* 4082 * RRS: Question during lock work: Is this correct? If you 4083 * are bound-all you still might need to obey the V4--V6 4084 * flags??? IMO this bound-all stuff needs to be removed! 4085 */ 4086 return (1); 4087 } 4088 /* NOTE: all "scope" checks are done when local addresses are added */ 4089 if (destaddr->sa_family == AF_INET6) { 4090 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6; 4091 } else if (destaddr->sa_family == AF_INET) { 4092 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4; 4093 } else { 4094 /* invalid family, so it's unreachable */ 4095 answer = 0; 4096 } 4097 return (answer); 4098 } 4099 4100 /* 4101 * update the inp_vflags on an endpoint 4102 */ 4103 static void 4104 sctp_update_ep_vflag(struct sctp_inpcb *inp) 4105 { 4106 struct sctp_laddr *laddr; 4107 4108 /* first clear the flag */ 4109 inp->ip_inp.inp.inp_vflag = 0; 4110 /* set the flag based on addresses on the ep list */ 4111 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 4112 if (laddr->ifa == NULL) { 4113 #ifdef SCTP_DEBUG 4114 if (sctp_debug_on & SCTP_DEBUG_PCB1) { 4115 printf("An ounce of prevention is worth a pound of cure\n"); 4116 } 4117 #endif /* SCTP_DEBUG */ 4118 continue; 4119 } 4120 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { 4121 continue; 4122 } 4123 if (laddr->ifa->address.sa.sa_family == AF_INET6) { 4124 inp->ip_inp.inp.inp_vflag |= INP_IPV6; 4125 } else if (laddr->ifa->address.sa.sa_family == AF_INET) { 4126 inp->ip_inp.inp.inp_vflag |= INP_IPV4; 4127 } 4128 } 4129 } 4130 4131 /* 4132 * Add the address to the endpoint local address list There is nothing to be 4133 * done if we are bound to all addresses 4134 */ 4135 int 4136 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action) 4137 { 4138 struct sctp_laddr *laddr; 4139 int fnd, error; 4140 4141 fnd = 0; 4142 4143 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 4144 /* You are already bound to all. You have it already */ 4145 return (0); 4146 } 4147 if (ifa->address.sa.sa_family == AF_INET6) { 4148 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { 4149 /* Can't bind a non-useable addr. */ 4150 return (-1); 4151 } 4152 } 4153 /* first, is it already present? */ 4154 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 4155 if (laddr->ifa == ifa) { 4156 fnd = 1; 4157 break; 4158 } 4159 } 4160 4161 if (fnd == 0) { 4162 /* Not in the ep list */ 4163 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action); 4164 if (error != 0) 4165 return (error); 4166 inp->laddr_count++; 4167 /* update inp_vflag flags */ 4168 if (ifa->address.sa.sa_family == AF_INET6) { 4169 inp->ip_inp.inp.inp_vflag |= INP_IPV6; 4170 } else if (ifa->address.sa.sa_family == AF_INET) { 4171 inp->ip_inp.inp.inp_vflag |= INP_IPV4; 4172 } 4173 } 4174 return (0); 4175 } 4176 4177 4178 /* 4179 * select a new (hopefully reachable) destination net (should only be used 4180 * when we deleted an ep addr that is the only usable source address to reach 4181 * the destination net) 4182 */ 4183 static void 4184 sctp_select_primary_destination(struct sctp_tcb *stcb) 4185 { 4186 struct sctp_nets *net; 4187 4188 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 4189 /* for now, we'll just pick the first reachable one we find */ 4190 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) 4191 continue; 4192 if (sctp_destination_is_reachable(stcb, 4193 (struct sockaddr *)&net->ro._l_addr)) { 4194 /* found a reachable destination */ 4195 stcb->asoc.primary_destination = net; 4196 } 4197 } 4198 /* I can't there from here! ...we're gonna die shortly... */ 4199 } 4200 4201 4202 /* 4203 * Delete the address from the endpoint local address list There is nothing 4204 * to be done if we are bound to all addresses 4205 */ 4206 int 4207 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa) 4208 { 4209 struct sctp_laddr *laddr; 4210 int fnd; 4211 4212 fnd = 0; 4213 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 4214 /* You are already bound to all. You have it already */ 4215 return (EINVAL); 4216 } 4217 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 4218 if (laddr->ifa == ifa) { 4219 fnd = 1; 4220 break; 4221 } 4222 } 4223 if (fnd && (inp->laddr_count < 2)) { 4224 /* can't delete unless there are at LEAST 2 addresses */ 4225 return (-1); 4226 } 4227 if (fnd) { 4228 /* 4229 * clean up any use of this address go through our 4230 * associations and clear any last_used_address that match 4231 * this one for each assoc, see if a new primary_destination 4232 * is needed 4233 */ 4234 struct sctp_tcb *stcb; 4235 4236 /* clean up "next_addr_touse" */ 4237 if (inp->next_addr_touse == laddr) 4238 /* delete this address */ 4239 inp->next_addr_touse = NULL; 4240 4241 /* clean up "last_used_address" */ 4242 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 4243 struct sctp_nets *net; 4244 4245 SCTP_TCB_LOCK(stcb); 4246 if (stcb->asoc.last_used_address == laddr) 4247 /* delete this address */ 4248 stcb->asoc.last_used_address = NULL; 4249 /* 4250 * Now spin through all the nets and purge any ref 4251 * to laddr 4252 */ 4253 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 4254 if (net->ro._s_addr && 4255 (net->ro._s_addr->ifa == laddr->ifa)) { 4256 /* Yep, purge src address selected */ 4257 struct rtentry *rt; 4258 4259 /* delete this address if cached */ 4260 rt = net->ro.ro_rt; 4261 if (rt != NULL) { 4262 RTFREE(rt); 4263 net->ro.ro_rt = NULL; 4264 } 4265 sctp_free_ifa(net->ro._s_addr); 4266 net->ro._s_addr = NULL; 4267 net->src_addr_selected = 0; 4268 } 4269 } 4270 SCTP_TCB_UNLOCK(stcb); 4271 } /* for each tcb */ 4272 /* remove it from the ep list */ 4273 sctp_remove_laddr(laddr); 4274 inp->laddr_count--; 4275 /* update inp_vflag flags */ 4276 sctp_update_ep_vflag(inp); 4277 } 4278 return (0); 4279 } 4280 4281 /* 4282 * Add the addr to the TCB local address list For the BOUNDALL or dynamic 4283 * case, this is a "pending" address list (eg. addresses waiting for an 4284 * ASCONF-ACK response) For the subset binding, static case, this is a 4285 * "valid" address list 4286 */ 4287 int 4288 sctp_add_local_addr_assoc(struct sctp_tcb *stcb, struct sctp_ifa *ifa, int restricted_list) 4289 { 4290 struct sctp_inpcb *inp; 4291 struct sctp_laddr *laddr; 4292 struct sctpladdr *list; 4293 int error; 4294 4295 /* 4296 * Assumes TCB is locked.. and possibly the INP. May need to 4297 * confirm/fix that if we need it and is not the case. 4298 */ 4299 list = &stcb->asoc.sctp_restricted_addrs; 4300 4301 inp = stcb->sctp_ep; 4302 if (ifa->address.sa.sa_family == AF_INET6) { 4303 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { 4304 /* Can't bind a non-existent addr. */ 4305 return (-1); 4306 } 4307 } 4308 /* does the address already exist? */ 4309 LIST_FOREACH(laddr, list, sctp_nxt_addr) { 4310 if (laddr->ifa == ifa) { 4311 return (-1); 4312 } 4313 } 4314 4315 /* add to the list */ 4316 error = sctp_insert_laddr(list, ifa, 0); 4317 if (error != 0) 4318 return (error); 4319 return (0); 4320 } 4321 4322 /* 4323 * insert an laddr entry with the given ifa for the desired list 4324 */ 4325 int 4326 sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act) 4327 { 4328 struct sctp_laddr *laddr; 4329 4330 laddr = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr); 4331 if (laddr == NULL) { 4332 /* out of memory? */ 4333 return (EINVAL); 4334 } 4335 SCTP_INCR_LADDR_COUNT(); 4336 bzero(laddr, sizeof(*laddr)); 4337 laddr->ifa = ifa; 4338 laddr->action = act; 4339 atomic_add_int(&ifa->refcount, 1); 4340 /* insert it */ 4341 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr); 4342 4343 return (0); 4344 } 4345 4346 /* 4347 * Remove an laddr entry from the local address list (on an assoc) 4348 */ 4349 void 4350 sctp_remove_laddr(struct sctp_laddr *laddr) 4351 { 4352 4353 /* remove from the list */ 4354 LIST_REMOVE(laddr, sctp_nxt_addr); 4355 sctp_free_ifa(laddr->ifa); 4356 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr); 4357 SCTP_DECR_LADDR_COUNT(); 4358 } 4359 4360 /* 4361 * Remove an address from the TCB local address list 4362 */ 4363 int 4364 sctp_del_local_addr_assoc(struct sctp_tcb *stcb, struct sctp_ifa *ifa) 4365 { 4366 struct sctp_inpcb *inp; 4367 struct sctp_laddr *laddr; 4368 4369 /* 4370 * This is called by asconf work. It is assumed that a) The TCB is 4371 * locked and b) The INP is locked. This is true in as much as I can 4372 * trace through the entry asconf code where I did these locks. 4373 * Again, the ASCONF code is a bit different in that it does lock 4374 * the INP during its work often times. This must be since we don't 4375 * want other proc's looking up things while what they are looking 4376 * up is changing :-D 4377 */ 4378 4379 inp = stcb->sctp_ep; 4380 /* if subset bound and don't allow ASCONF's, can't delete last */ 4381 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && 4382 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF) == 0)) { 4383 if (stcb->asoc.numnets < 2) { 4384 /* can't delete last address */ 4385 return (-1); 4386 } 4387 } 4388 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) { 4389 /* remove the address if it exists */ 4390 if (laddr->ifa == NULL) 4391 continue; 4392 if (laddr->ifa == ifa) { 4393 sctp_remove_laddr(laddr); 4394 return (0); 4395 } 4396 } 4397 4398 /* address not found! */ 4399 return (-1); 4400 } 4401 4402 static char sctp_pcb_initialized = 0; 4403 4404 /* 4405 * Temporarily remove for __APPLE__ until we use the Tiger equivalents 4406 */ 4407 /* sysctl */ 4408 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC; 4409 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR; 4410 4411 void 4412 sctp_pcb_init() 4413 { 4414 /* 4415 * SCTP initialization for the PCB structures should be called by 4416 * the sctp_init() funciton. 4417 */ 4418 int i; 4419 4420 if (sctp_pcb_initialized != 0) { 4421 /* error I was called twice */ 4422 return; 4423 } 4424 sctp_pcb_initialized = 1; 4425 4426 bzero(&sctpstat, sizeof(struct sctpstat)); 4427 SCTP_GETTIME_TIMEVAL(&sctpstat.sctps_discontinuitytime); 4428 /* init the empty list of (All) Endpoints */ 4429 LIST_INIT(&sctppcbinfo.listhead); 4430 4431 /* init the iterator head */ 4432 TAILQ_INIT(&sctppcbinfo.iteratorhead); 4433 4434 /* init the hash table of endpoints */ 4435 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &sctp_hashtblsize); 4436 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &sctp_pcbtblsize); 4437 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &sctp_chunkscale); 4438 sctppcbinfo.sctp_asochash = SCTP_HASH_INIT((sctp_hashtblsize * 31), 4439 &sctppcbinfo.hashasocmark); 4440 sctppcbinfo.sctp_ephash = SCTP_HASH_INIT(sctp_hashtblsize, 4441 &sctppcbinfo.hashmark); 4442 sctppcbinfo.sctp_tcpephash = SCTP_HASH_INIT(sctp_hashtblsize, 4443 &sctppcbinfo.hashtcpmark); 4444 sctppcbinfo.hashtblsize = sctp_hashtblsize; 4445 4446 /* init the small hash table we use to track restarted asoc's */ 4447 sctppcbinfo.sctp_restarthash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, 4448 &sctppcbinfo.hashrestartmark); 4449 4450 4451 sctppcbinfo.sctp_vrfhash = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH, 4452 &sctppcbinfo.hashvrfmark); 4453 4454 /* init the zones */ 4455 /* 4456 * FIX ME: Should check for NULL returns, but if it does fail we are 4457 * doomed to panic anyways... add later maybe. 4458 */ 4459 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_ep, "sctp_ep", 4460 sizeof(struct sctp_inpcb), maxsockets); 4461 4462 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asoc, "sctp_asoc", 4463 sizeof(struct sctp_tcb), sctp_max_number_of_assoc); 4464 4465 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_laddr, "sctp_laddr", 4466 sizeof(struct sctp_laddr), 4467 (sctp_max_number_of_assoc * sctp_scale_up_for_address)); 4468 4469 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_net, "sctp_raddr", 4470 sizeof(struct sctp_nets), 4471 (sctp_max_number_of_assoc * sctp_scale_up_for_address)); 4472 4473 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_chunk, "sctp_chunk", 4474 sizeof(struct sctp_tmit_chunk), 4475 (sctp_max_number_of_assoc * sctp_chunkscale)); 4476 4477 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_readq, "sctp_readq", 4478 sizeof(struct sctp_queued_to_read), 4479 (sctp_max_number_of_assoc * sctp_chunkscale)); 4480 4481 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_strmoq, "sctp_stream_msg_out", 4482 sizeof(struct sctp_stream_queue_pending), 4483 (sctp_max_number_of_assoc * sctp_chunkscale)); 4484 4485 /* Master Lock INIT for info structure */ 4486 SCTP_INP_INFO_LOCK_INIT(); 4487 SCTP_STATLOG_INIT_LOCK(); 4488 SCTP_ITERATOR_LOCK_INIT(); 4489 4490 SCTP_IPI_COUNT_INIT(); 4491 SCTP_IPI_ADDR_INIT(); 4492 SCTP_IPI_ITERATOR_WQ_INIT(); 4493 4494 LIST_INIT(&sctppcbinfo.addr_wq); 4495 4496 /* not sure if we need all the counts */ 4497 sctppcbinfo.ipi_count_ep = 0; 4498 /* assoc/tcb zone info */ 4499 sctppcbinfo.ipi_count_asoc = 0; 4500 /* local addrlist zone info */ 4501 sctppcbinfo.ipi_count_laddr = 0; 4502 /* remote addrlist zone info */ 4503 sctppcbinfo.ipi_count_raddr = 0; 4504 /* chunk info */ 4505 sctppcbinfo.ipi_count_chunk = 0; 4506 4507 /* socket queue zone info */ 4508 sctppcbinfo.ipi_count_readq = 0; 4509 4510 /* stream out queue cont */ 4511 sctppcbinfo.ipi_count_strmoq = 0; 4512 4513 sctppcbinfo.ipi_free_strmoq = 0; 4514 sctppcbinfo.ipi_free_chunks = 0; 4515 4516 SCTP_OS_TIMER_INIT(&sctppcbinfo.addr_wq_timer.timer); 4517 4518 /* Init the TIMEWAIT list */ 4519 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) { 4520 LIST_INIT(&sctppcbinfo.vtag_timewait[i]); 4521 } 4522 4523 #if defined(SCTP_USE_THREAD_BASED_ITERATOR) 4524 sctppcbinfo.iterator_running = 0; 4525 sctp_startup_iterator(); 4526 #endif 4527 4528 /* 4529 * INIT the default VRF which for BSD is the only one, other O/S's 4530 * may have more. But initially they must start with one and then 4531 * add the VRF's as addresses are added. 4532 */ 4533 sctp_init_vrf_list(SCTP_DEFAULT_VRF); 4534 4535 } 4536 4537 4538 int 4539 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m, 4540 int iphlen, int offset, int limit, struct sctphdr *sh, 4541 struct sockaddr *altsa) 4542 { 4543 /* 4544 * grub through the INIT pulling addresses and loading them to the 4545 * nets structure in the asoc. The from address in the mbuf should 4546 * also be loaded (if it is not already). This routine can be called 4547 * with either INIT or INIT-ACK's as long as the m points to the IP 4548 * packet and the offset points to the beginning of the parameters. 4549 */ 4550 struct sctp_inpcb *inp, *l_inp; 4551 struct sctp_nets *net, *net_tmp; 4552 struct ip *iph; 4553 struct sctp_paramhdr *phdr, parm_buf; 4554 struct sctp_tcb *stcb_tmp; 4555 uint16_t ptype, plen; 4556 struct sockaddr *sa; 4557 struct sockaddr_storage dest_store; 4558 struct sockaddr *local_sa = (struct sockaddr *)&dest_store; 4559 struct sockaddr_in sin; 4560 struct sockaddr_in6 sin6; 4561 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE]; 4562 struct sctp_auth_random *p_random = NULL; 4563 uint16_t random_len = 0; 4564 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE]; 4565 struct sctp_auth_hmac_algo *hmacs = NULL; 4566 uint16_t hmacs_len = 0; 4567 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE]; 4568 struct sctp_auth_chunk_list *chunks = NULL; 4569 uint16_t num_chunks = 0; 4570 sctp_key_t *new_key; 4571 uint32_t keylen; 4572 int got_random = 0, got_hmacs = 0, got_chklist = 0; 4573 4574 /* First get the destination address setup too. */ 4575 memset(&sin, 0, sizeof(sin)); 4576 memset(&sin6, 0, sizeof(sin6)); 4577 4578 sin.sin_family = AF_INET; 4579 sin.sin_len = sizeof(sin); 4580 sin.sin_port = stcb->rport; 4581 4582 sin6.sin6_family = AF_INET6; 4583 sin6.sin6_len = sizeof(struct sockaddr_in6); 4584 sin6.sin6_port = stcb->rport; 4585 if (altsa == NULL) { 4586 iph = mtod(m, struct ip *); 4587 if (iph->ip_v == IPVERSION) { 4588 /* its IPv4 */ 4589 struct sockaddr_in *sin_2; 4590 4591 sin_2 = (struct sockaddr_in *)(local_sa); 4592 memset(sin_2, 0, sizeof(sin)); 4593 sin_2->sin_family = AF_INET; 4594 sin_2->sin_len = sizeof(sin); 4595 sin_2->sin_port = sh->dest_port; 4596 sin_2->sin_addr.s_addr = iph->ip_dst.s_addr; 4597 sin.sin_addr = iph->ip_src; 4598 sa = (struct sockaddr *)&sin; 4599 } else if (iph->ip_v == (IPV6_VERSION >> 4)) { 4600 /* its IPv6 */ 4601 struct ip6_hdr *ip6; 4602 struct sockaddr_in6 *sin6_2; 4603 4604 ip6 = mtod(m, struct ip6_hdr *); 4605 sin6_2 = (struct sockaddr_in6 *)(local_sa); 4606 memset(sin6_2, 0, sizeof(sin6)); 4607 sin6_2->sin6_family = AF_INET6; 4608 sin6_2->sin6_len = sizeof(struct sockaddr_in6); 4609 sin6_2->sin6_port = sh->dest_port; 4610 sin6.sin6_addr = ip6->ip6_src; 4611 sa = (struct sockaddr *)&sin6; 4612 } else { 4613 sa = NULL; 4614 } 4615 } else { 4616 /* 4617 * For cookies we use the src address NOT from the packet 4618 * but from the original INIT 4619 */ 4620 sa = altsa; 4621 } 4622 /* Turn off ECN until we get through all params */ 4623 stcb->asoc.ecn_allowed = 0; 4624 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 4625 /* mark all addresses that we have currently on the list */ 4626 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC; 4627 } 4628 /* does the source address already exist? if so skip it */ 4629 l_inp = inp = stcb->sctp_ep; 4630 4631 atomic_add_int(&stcb->asoc.refcnt, 1); 4632 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb); 4633 atomic_add_int(&stcb->asoc.refcnt, -1); 4634 4635 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) { 4636 /* we must add the source address */ 4637 /* no scope set here since we have a tcb already. */ 4638 if ((sa->sa_family == AF_INET) && 4639 (stcb->asoc.ipv4_addr_legal)) { 4640 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) { 4641 return (-1); 4642 } 4643 } else if ((sa->sa_family == AF_INET6) && 4644 (stcb->asoc.ipv6_addr_legal)) { 4645 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) { 4646 return (-2); 4647 } 4648 } 4649 } else { 4650 if (net_tmp != NULL && stcb_tmp == stcb) { 4651 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC; 4652 } else if (stcb_tmp != stcb) { 4653 /* It belongs to another association? */ 4654 SCTP_TCB_UNLOCK(stcb_tmp); 4655 return (-3); 4656 } 4657 } 4658 if (stcb->asoc.state == 0) { 4659 /* the assoc was freed? */ 4660 return (-4); 4661 } 4662 /* now we must go through each of the params. */ 4663 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf)); 4664 while (phdr) { 4665 ptype = ntohs(phdr->param_type); 4666 plen = ntohs(phdr->param_length); 4667 /* 4668 * printf("ptype => %0x, plen => %d\n", (uint32_t)ptype, 4669 * (int)plen); 4670 */ 4671 if (offset + plen > limit) { 4672 break; 4673 } 4674 if (plen == 0) { 4675 break; 4676 } 4677 if (ptype == SCTP_IPV4_ADDRESS) { 4678 if (stcb->asoc.ipv4_addr_legal) { 4679 struct sctp_ipv4addr_param *p4, p4_buf; 4680 4681 /* ok get the v4 address and check/add */ 4682 phdr = sctp_get_next_param(m, offset, 4683 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf)); 4684 if (plen != sizeof(struct sctp_ipv4addr_param) || 4685 phdr == NULL) { 4686 return (-5); 4687 } 4688 p4 = (struct sctp_ipv4addr_param *)phdr; 4689 sin.sin_addr.s_addr = p4->addr; 4690 sa = (struct sockaddr *)&sin; 4691 inp = stcb->sctp_ep; 4692 atomic_add_int(&stcb->asoc.refcnt, 1); 4693 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, 4694 local_sa, stcb); 4695 atomic_add_int(&stcb->asoc.refcnt, -1); 4696 4697 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || 4698 inp == NULL) { 4699 /* we must add the source address */ 4700 /* 4701 * no scope set since we have a tcb 4702 * already 4703 */ 4704 4705 /* 4706 * we must validate the state again 4707 * here 4708 */ 4709 if (stcb->asoc.state == 0) { 4710 /* the assoc was freed? */ 4711 return (-7); 4712 } 4713 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) { 4714 return (-8); 4715 } 4716 } else if (stcb_tmp == stcb) { 4717 if (stcb->asoc.state == 0) { 4718 /* the assoc was freed? */ 4719 return (-10); 4720 } 4721 if (net != NULL) { 4722 /* clear flag */ 4723 net->dest_state &= 4724 ~SCTP_ADDR_NOT_IN_ASSOC; 4725 } 4726 } else { 4727 /* 4728 * strange, address is in another 4729 * assoc? straighten out locks. 4730 */ 4731 if (stcb->asoc.state == 0) { 4732 /* the assoc was freed? */ 4733 return (-12); 4734 } 4735 return (-13); 4736 } 4737 } 4738 } else if (ptype == SCTP_IPV6_ADDRESS) { 4739 if (stcb->asoc.ipv6_addr_legal) { 4740 /* ok get the v6 address and check/add */ 4741 struct sctp_ipv6addr_param *p6, p6_buf; 4742 4743 phdr = sctp_get_next_param(m, offset, 4744 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf)); 4745 if (plen != sizeof(struct sctp_ipv6addr_param) || 4746 phdr == NULL) { 4747 return (-14); 4748 } 4749 p6 = (struct sctp_ipv6addr_param *)phdr; 4750 memcpy((caddr_t)&sin6.sin6_addr, p6->addr, 4751 sizeof(p6->addr)); 4752 sa = (struct sockaddr *)&sin6; 4753 inp = stcb->sctp_ep; 4754 atomic_add_int(&stcb->asoc.refcnt, 1); 4755 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, 4756 local_sa, stcb); 4757 atomic_add_int(&stcb->asoc.refcnt, -1); 4758 if (stcb_tmp == NULL && (inp == stcb->sctp_ep || 4759 inp == NULL)) { 4760 /* 4761 * we must validate the state again 4762 * here 4763 */ 4764 if (stcb->asoc.state == 0) { 4765 /* the assoc was freed? */ 4766 return (-16); 4767 } 4768 /* 4769 * we must add the address, no scope 4770 * set 4771 */ 4772 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) { 4773 return (-17); 4774 } 4775 } else if (stcb_tmp == stcb) { 4776 /* 4777 * we must validate the state again 4778 * here 4779 */ 4780 if (stcb->asoc.state == 0) { 4781 /* the assoc was freed? */ 4782 return (-19); 4783 } 4784 if (net != NULL) { 4785 /* clear flag */ 4786 net->dest_state &= 4787 ~SCTP_ADDR_NOT_IN_ASSOC; 4788 } 4789 } else { 4790 /* 4791 * strange, address is in another 4792 * assoc? straighten out locks. 4793 */ 4794 if (stcb->asoc.state == 0) { 4795 /* the assoc was freed? */ 4796 return (-21); 4797 } 4798 return (-22); 4799 } 4800 } 4801 } else if (ptype == SCTP_ECN_CAPABLE) { 4802 stcb->asoc.ecn_allowed = 1; 4803 } else if (ptype == SCTP_ULP_ADAPTATION) { 4804 if (stcb->asoc.state != SCTP_STATE_OPEN) { 4805 struct sctp_adaptation_layer_indication ai, 4806 *aip; 4807 4808 phdr = sctp_get_next_param(m, offset, 4809 (struct sctp_paramhdr *)&ai, sizeof(ai)); 4810 aip = (struct sctp_adaptation_layer_indication *)phdr; 4811 sctp_ulp_notify(SCTP_NOTIFY_ADAPTATION_INDICATION, 4812 stcb, ntohl(aip->indication), NULL); 4813 } 4814 } else if (ptype == SCTP_SET_PRIM_ADDR) { 4815 struct sctp_asconf_addr_param lstore, *fee; 4816 struct sctp_asconf_addrv4_param *fii; 4817 int lptype; 4818 struct sockaddr *lsa = NULL; 4819 4820 stcb->asoc.peer_supports_asconf = 1; 4821 if (plen > sizeof(lstore)) { 4822 return (-23); 4823 } 4824 phdr = sctp_get_next_param(m, offset, 4825 (struct sctp_paramhdr *)&lstore, plen); 4826 if (phdr == NULL) { 4827 return (-24); 4828 } 4829 fee = (struct sctp_asconf_addr_param *)phdr; 4830 lptype = ntohs(fee->addrp.ph.param_type); 4831 if (lptype == SCTP_IPV4_ADDRESS) { 4832 if (plen != 4833 sizeof(struct sctp_asconf_addrv4_param)) { 4834 printf("Sizeof setprim in init/init ack not %d but %d - ignored\n", 4835 (int)sizeof(struct sctp_asconf_addrv4_param), 4836 plen); 4837 } else { 4838 fii = (struct sctp_asconf_addrv4_param *)fee; 4839 sin.sin_addr.s_addr = fii->addrp.addr; 4840 lsa = (struct sockaddr *)&sin; 4841 } 4842 } else if (lptype == SCTP_IPV6_ADDRESS) { 4843 if (plen != 4844 sizeof(struct sctp_asconf_addr_param)) { 4845 printf("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n", 4846 (int)sizeof(struct sctp_asconf_addr_param), 4847 plen); 4848 } else { 4849 memcpy(sin6.sin6_addr.s6_addr, 4850 fee->addrp.addr, 4851 sizeof(fee->addrp.addr)); 4852 lsa = (struct sockaddr *)&sin6; 4853 } 4854 } 4855 if (lsa) { 4856 sctp_set_primary_addr(stcb, sa, NULL); 4857 } 4858 } else if (ptype == SCTP_PRSCTP_SUPPORTED) { 4859 /* Peer supports pr-sctp */ 4860 stcb->asoc.peer_supports_prsctp = 1; 4861 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) { 4862 /* A supported extension chunk */ 4863 struct sctp_supported_chunk_types_param *pr_supported; 4864 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE]; 4865 int num_ent, i; 4866 4867 phdr = sctp_get_next_param(m, offset, 4868 (struct sctp_paramhdr *)&local_store, plen); 4869 if (phdr == NULL) { 4870 return (-25); 4871 } 4872 stcb->asoc.peer_supports_asconf = 0; 4873 stcb->asoc.peer_supports_prsctp = 0; 4874 stcb->asoc.peer_supports_pktdrop = 0; 4875 stcb->asoc.peer_supports_strreset = 0; 4876 stcb->asoc.peer_supports_auth = 0; 4877 pr_supported = (struct sctp_supported_chunk_types_param *)phdr; 4878 num_ent = plen - sizeof(struct sctp_paramhdr); 4879 for (i = 0; i < num_ent; i++) { 4880 switch (pr_supported->chunk_types[i]) { 4881 case SCTP_ASCONF: 4882 case SCTP_ASCONF_ACK: 4883 stcb->asoc.peer_supports_asconf = 1; 4884 break; 4885 case SCTP_FORWARD_CUM_TSN: 4886 stcb->asoc.peer_supports_prsctp = 1; 4887 break; 4888 case SCTP_PACKET_DROPPED: 4889 stcb->asoc.peer_supports_pktdrop = 1; 4890 break; 4891 case SCTP_STREAM_RESET: 4892 stcb->asoc.peer_supports_strreset = 1; 4893 break; 4894 case SCTP_AUTHENTICATION: 4895 stcb->asoc.peer_supports_auth = 1; 4896 break; 4897 default: 4898 /* one I have not learned yet */ 4899 break; 4900 4901 } 4902 } 4903 } else if (ptype == SCTP_ECN_NONCE_SUPPORTED) { 4904 /* Peer supports ECN-nonce */ 4905 stcb->asoc.peer_supports_ecn_nonce = 1; 4906 stcb->asoc.ecn_nonce_allowed = 1; 4907 } else if (ptype == SCTP_RANDOM) { 4908 if (plen > sizeof(random_store)) 4909 break; 4910 if (got_random) { 4911 /* already processed a RANDOM */ 4912 goto next_param; 4913 } 4914 phdr = sctp_get_next_param(m, offset, 4915 (struct sctp_paramhdr *)random_store, 4916 plen); 4917 if (phdr == NULL) 4918 return (-26); 4919 p_random = (struct sctp_auth_random *)phdr; 4920 random_len = plen - sizeof(*p_random); 4921 /* enforce the random length */ 4922 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) { 4923 #ifdef SCTP_DEBUG 4924 if (sctp_debug_on & SCTP_DEBUG_AUTH1) 4925 printf("SCTP: invalid RANDOM len\n"); 4926 #endif 4927 return (-27); 4928 } 4929 got_random = 1; 4930 } else if (ptype == SCTP_HMAC_LIST) { 4931 int num_hmacs; 4932 int i; 4933 4934 if (plen > sizeof(hmacs_store)) 4935 break; 4936 if (got_hmacs) { 4937 /* already processed a HMAC list */ 4938 goto next_param; 4939 } 4940 phdr = sctp_get_next_param(m, offset, 4941 (struct sctp_paramhdr *)hmacs_store, 4942 plen); 4943 if (phdr == NULL) 4944 return (-28); 4945 hmacs = (struct sctp_auth_hmac_algo *)phdr; 4946 hmacs_len = plen - sizeof(*hmacs); 4947 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]); 4948 /* validate the hmac list */ 4949 if (sctp_verify_hmac_param(hmacs, num_hmacs)) { 4950 return (-29); 4951 } 4952 if (stcb->asoc.peer_hmacs != NULL) 4953 sctp_free_hmaclist(stcb->asoc.peer_hmacs); 4954 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs); 4955 if (stcb->asoc.peer_hmacs != NULL) { 4956 for (i = 0; i < num_hmacs; i++) { 4957 sctp_auth_add_hmacid(stcb->asoc.peer_hmacs, 4958 ntohs(hmacs->hmac_ids[i])); 4959 } 4960 } 4961 got_hmacs = 1; 4962 } else if (ptype == SCTP_CHUNK_LIST) { 4963 int i; 4964 4965 if (plen > sizeof(chunks_store)) 4966 break; 4967 if (got_chklist) { 4968 /* already processed a Chunks list */ 4969 goto next_param; 4970 } 4971 phdr = sctp_get_next_param(m, offset, 4972 (struct sctp_paramhdr *)chunks_store, 4973 plen); 4974 if (phdr == NULL) 4975 return (-30); 4976 chunks = (struct sctp_auth_chunk_list *)phdr; 4977 num_chunks = plen - sizeof(*chunks); 4978 if (stcb->asoc.peer_auth_chunks != NULL) 4979 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks); 4980 else 4981 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist(); 4982 for (i = 0; i < num_chunks; i++) { 4983 sctp_auth_add_chunk(chunks->chunk_types[i], 4984 stcb->asoc.peer_auth_chunks); 4985 } 4986 got_chklist = 1; 4987 } else if ((ptype == SCTP_HEARTBEAT_INFO) || 4988 (ptype == SCTP_STATE_COOKIE) || 4989 (ptype == SCTP_UNRECOG_PARAM) || 4990 (ptype == SCTP_COOKIE_PRESERVE) || 4991 (ptype == SCTP_SUPPORTED_ADDRTYPE) || 4992 (ptype == SCTP_ADD_IP_ADDRESS) || 4993 (ptype == SCTP_DEL_IP_ADDRESS) || 4994 (ptype == SCTP_ERROR_CAUSE_IND) || 4995 (ptype == SCTP_SUCCESS_REPORT)) { 4996 /* don't care */ ; 4997 } else { 4998 if ((ptype & 0x8000) == 0x0000) { 4999 /* 5000 * must stop processing the rest of the 5001 * param's. Any report bits were handled 5002 * with the call to 5003 * sctp_arethere_unrecognized_parameters() 5004 * when the INIT or INIT-ACK was first seen. 5005 */ 5006 break; 5007 } 5008 } 5009 next_param: 5010 offset += SCTP_SIZE32(plen); 5011 if (offset >= limit) { 5012 break; 5013 } 5014 phdr = sctp_get_next_param(m, offset, &parm_buf, 5015 sizeof(parm_buf)); 5016 } 5017 /* Now check to see if we need to purge any addresses */ 5018 for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) { 5019 net_tmp = TAILQ_NEXT(net, sctp_next); 5020 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) == 5021 SCTP_ADDR_NOT_IN_ASSOC) { 5022 /* This address has been removed from the asoc */ 5023 /* remove and free it */ 5024 stcb->asoc.numnets--; 5025 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next); 5026 sctp_free_remote_addr(net); 5027 if (net == stcb->asoc.primary_destination) { 5028 stcb->asoc.primary_destination = NULL; 5029 sctp_select_primary_destination(stcb); 5030 } 5031 } 5032 } 5033 /* validate authentication required parameters */ 5034 if (got_random && got_hmacs) { 5035 stcb->asoc.peer_supports_auth = 1; 5036 } else { 5037 stcb->asoc.peer_supports_auth = 0; 5038 } 5039 if (!stcb->asoc.peer_supports_auth && got_chklist) { 5040 /* peer does not support auth but sent a chunks list? */ 5041 return (-31); 5042 } 5043 if (!sctp_asconf_auth_nochk && stcb->asoc.peer_supports_asconf && 5044 !stcb->asoc.peer_supports_auth) { 5045 /* peer supports asconf but not auth? */ 5046 return (-32); 5047 } 5048 /* concatenate the full random key */ 5049 #ifdef SCTP_AUTH_DRAFT_04 5050 keylen = random_len; 5051 new_key = sctp_alloc_key(keylen); 5052 if (new_key != NULL) { 5053 /* copy in the RANDOM */ 5054 if (p_random != NULL) 5055 bcopy(p_random->random_data, new_key->key, random_len); 5056 } 5057 #else 5058 keylen = sizeof(*p_random) + random_len + sizeof(*chunks) + num_chunks + 5059 sizeof(*hmacs) + hmacs_len; 5060 new_key = sctp_alloc_key(keylen); 5061 if (new_key != NULL) { 5062 /* copy in the RANDOM */ 5063 if (p_random != NULL) { 5064 keylen = sizeof(*p_random) + random_len; 5065 bcopy(p_random, new_key->key, keylen); 5066 } 5067 /* append in the AUTH chunks */ 5068 if (chunks != NULL) { 5069 bcopy(chunks, new_key->key + keylen, 5070 sizeof(*chunks) + num_chunks); 5071 keylen += sizeof(*chunks) + num_chunks; 5072 } 5073 /* append in the HMACs */ 5074 if (hmacs != NULL) { 5075 bcopy(hmacs, new_key->key + keylen, 5076 sizeof(*hmacs) + hmacs_len); 5077 } 5078 } 5079 #endif 5080 else { 5081 /* failed to get memory for the key */ 5082 return (-33); 5083 } 5084 if (stcb->asoc.authinfo.peer_random != NULL) 5085 sctp_free_key(stcb->asoc.authinfo.peer_random); 5086 stcb->asoc.authinfo.peer_random = new_key; 5087 #ifdef SCTP_AUTH_DRAFT_04 5088 /* don't include the chunks and hmacs for draft -04 */ 5089 stcb->asoc.authinfo.peer_random->keylen = random_len; 5090 #endif 5091 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid); 5092 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid); 5093 5094 return (0); 5095 } 5096 5097 int 5098 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa, 5099 struct sctp_nets *net) 5100 { 5101 /* make sure the requested primary address exists in the assoc */ 5102 if (net == NULL && sa) 5103 net = sctp_findnet(stcb, sa); 5104 5105 if (net == NULL) { 5106 /* didn't find the requested primary address! */ 5107 return (-1); 5108 } else { 5109 /* set the primary address */ 5110 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { 5111 /* Must be confirmed, so queue to set */ 5112 net->dest_state |= SCTP_ADDR_REQ_PRIMARY; 5113 return (0); 5114 } 5115 stcb->asoc.primary_destination = net; 5116 net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY; 5117 net = TAILQ_FIRST(&stcb->asoc.nets); 5118 if (net != stcb->asoc.primary_destination) { 5119 /* 5120 * first one on the list is NOT the primary 5121 * sctp_cmpaddr() is much more efficent if the 5122 * primary is the first on the list, make it so. 5123 */ 5124 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 5125 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 5126 } 5127 return (0); 5128 } 5129 } 5130 5131 5132 int 5133 sctp_is_vtag_good(struct sctp_inpcb *inp, uint32_t tag, struct timeval *now) 5134 { 5135 /* 5136 * This function serves two purposes. It will see if a TAG can be 5137 * re-used and return 1 for yes it is ok and 0 for don't use that 5138 * tag. A secondary function it will do is purge out old tags that 5139 * can be removed. 5140 */ 5141 struct sctpasochead *head; 5142 struct sctpvtaghead *chain; 5143 struct sctp_tagblock *twait_block; 5144 struct sctp_tcb *stcb; 5145 int i; 5146 5147 SCTP_INP_INFO_WLOCK(); 5148 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; 5149 /* First is the vtag in use ? */ 5150 5151 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(tag, 5152 sctppcbinfo.hashasocmark)]; 5153 if (head == NULL) { 5154 goto check_restart; 5155 } 5156 LIST_FOREACH(stcb, head, sctp_asocs) { 5157 5158 if (stcb->asoc.my_vtag == tag) { 5159 /* 5160 * We should remove this if and return 0 always if 5161 * we want vtags unique across all endpoints. For 5162 * now within a endpoint is ok. 5163 */ 5164 if (inp == stcb->sctp_ep) { 5165 /* bad tag, in use */ 5166 SCTP_INP_INFO_WUNLOCK(); 5167 return (0); 5168 } 5169 } 5170 } 5171 check_restart: 5172 /* Now lets check the restart hash */ 5173 head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(tag, 5174 sctppcbinfo.hashrestartmark)]; 5175 if (head == NULL) { 5176 goto check_time_wait; 5177 } 5178 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) { 5179 if (stcb->asoc.assoc_id == tag) { 5180 /* candidate */ 5181 if (inp == stcb->sctp_ep) { 5182 /* bad tag, in use */ 5183 SCTP_INP_INFO_WUNLOCK(); 5184 return (0); 5185 } 5186 } 5187 } 5188 check_time_wait: 5189 /* Now what about timed wait ? */ 5190 if (!SCTP_LIST_EMPTY(chain)) { 5191 /* 5192 * Block(s) are present, lets see if we have this tag in the 5193 * list 5194 */ 5195 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { 5196 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { 5197 if (twait_block->vtag_block[i].v_tag == 0) { 5198 /* not used */ 5199 continue; 5200 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire > 5201 now->tv_sec) { 5202 /* Audit expires this guy */ 5203 twait_block->vtag_block[i].tv_sec_at_expire = 0; 5204 twait_block->vtag_block[i].v_tag = 0; 5205 } else if (twait_block->vtag_block[i].v_tag == 5206 tag) { 5207 /* Bad tag, sorry :< */ 5208 SCTP_INP_INFO_WUNLOCK(); 5209 return (0); 5210 } 5211 } 5212 } 5213 } 5214 /* Not found, ok to use the tag */ 5215 SCTP_INP_INFO_WUNLOCK(); 5216 return (1); 5217 } 5218 5219 5220 static sctp_assoc_t reneged_asoc_ids[256]; 5221 static uint8_t reneged_at = 0; 5222 5223 5224 static void 5225 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb) 5226 { 5227 /* 5228 * We must hunt this association for MBUF's past the cumack (i.e. 5229 * out of order data that we can renege on). 5230 */ 5231 struct sctp_association *asoc; 5232 struct sctp_tmit_chunk *chk, *nchk; 5233 uint32_t cumulative_tsn_p1, tsn; 5234 struct sctp_queued_to_read *ctl, *nctl; 5235 int cnt, strmat, gap; 5236 5237 /* We look for anything larger than the cum-ack + 1 */ 5238 5239 SCTP_STAT_INCR(sctps_protocol_drain_calls); 5240 if (sctp_do_drain == 0) { 5241 return; 5242 } 5243 asoc = &stcb->asoc; 5244 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) { 5245 /* none we can reneg on. */ 5246 return; 5247 } 5248 SCTP_STAT_INCR(sctps_protocol_drains_done); 5249 cumulative_tsn_p1 = asoc->cumulative_tsn + 1; 5250 cnt = 0; 5251 /* First look in the re-assembly queue */ 5252 chk = TAILQ_FIRST(&asoc->reasmqueue); 5253 while (chk) { 5254 /* Get the next one */ 5255 nchk = TAILQ_NEXT(chk, sctp_next); 5256 if (compare_with_wrap(chk->rec.data.TSN_seq, 5257 cumulative_tsn_p1, MAX_TSN)) { 5258 /* Yep it is above cum-ack */ 5259 cnt++; 5260 tsn = chk->rec.data.TSN_seq; 5261 if (tsn >= asoc->mapping_array_base_tsn) { 5262 gap = tsn - asoc->mapping_array_base_tsn; 5263 } else { 5264 gap = (MAX_TSN - asoc->mapping_array_base_tsn) + 5265 tsn + 1; 5266 } 5267 asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size); 5268 sctp_ucount_decr(asoc->cnt_on_reasm_queue); 5269 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); 5270 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next); 5271 if (chk->data) { 5272 sctp_m_freem(chk->data); 5273 chk->data = NULL; 5274 } 5275 sctp_free_remote_addr(chk->whoTo); 5276 sctp_free_a_chunk(stcb, chk); 5277 } 5278 chk = nchk; 5279 } 5280 /* Ok that was fun, now we will drain all the inbound streams? */ 5281 for (strmat = 0; strmat < asoc->streamincnt; strmat++) { 5282 ctl = TAILQ_FIRST(&asoc->strmin[strmat].inqueue); 5283 while (ctl) { 5284 nctl = TAILQ_NEXT(ctl, next); 5285 if (compare_with_wrap(ctl->sinfo_tsn, 5286 cumulative_tsn_p1, MAX_TSN)) { 5287 /* Yep it is above cum-ack */ 5288 cnt++; 5289 tsn = ctl->sinfo_tsn; 5290 if (tsn >= asoc->mapping_array_base_tsn) { 5291 gap = tsn - 5292 asoc->mapping_array_base_tsn; 5293 } else { 5294 gap = (MAX_TSN - 5295 asoc->mapping_array_base_tsn) + 5296 tsn + 1; 5297 } 5298 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length); 5299 sctp_ucount_decr(asoc->cnt_on_all_streams); 5300 5301 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, 5302 gap); 5303 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, 5304 ctl, next); 5305 if (ctl->data) { 5306 sctp_m_freem(ctl->data); 5307 ctl->data = NULL; 5308 } 5309 sctp_free_remote_addr(ctl->whoFrom); 5310 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl); 5311 SCTP_DECR_READQ_COUNT(); 5312 } 5313 ctl = nctl; 5314 } 5315 } 5316 /* 5317 * Question, should we go through the delivery queue? The only 5318 * reason things are on here is the app not reading OR a p-d-api up. 5319 * An attacker COULD send enough in to initiate the PD-API and then 5320 * send a bunch of stuff to other streams... these would wind up on 5321 * the delivery queue.. and then we would not get to them. But in 5322 * order to do this I then have to back-track and un-deliver 5323 * sequence numbers in streams.. el-yucko. I think for now we will 5324 * NOT look at the delivery queue and leave it to be something to 5325 * consider later. An alternative would be to abort the P-D-API with 5326 * a notification and then deliver the data.... Or another method 5327 * might be to keep track of how many times the situation occurs and 5328 * if we see a possible attack underway just abort the association. 5329 */ 5330 #ifdef SCTP_DEBUG 5331 if (sctp_debug_on & SCTP_DEBUG_PCB1) { 5332 if (cnt) { 5333 printf("Freed %d chunks from reneg harvest\n", cnt); 5334 } 5335 } 5336 #endif /* SCTP_DEBUG */ 5337 if (cnt) { 5338 /* 5339 * Now do we need to find a new 5340 * asoc->highest_tsn_inside_map? 5341 */ 5342 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) { 5343 gap = asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn; 5344 } else { 5345 gap = (MAX_TSN - asoc->mapping_array_base_tsn) + 5346 asoc->highest_tsn_inside_map + 1; 5347 } 5348 if (gap >= (asoc->mapping_array_size << 3)) { 5349 /* 5350 * Something bad happened or cum-ack and high were 5351 * behind the base, but if so earlier checks should 5352 * have found NO data... wierd... we will start at 5353 * end of mapping array. 5354 */ 5355 printf("Gap was larger than array?? %d set to max:%d maparraymax:%x\n", 5356 (int)gap, 5357 (int)(asoc->mapping_array_size << 3), 5358 (int)asoc->highest_tsn_inside_map); 5359 gap = asoc->mapping_array_size << 3; 5360 } 5361 while (gap > 0) { 5362 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) { 5363 /* found the new highest */ 5364 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn + gap; 5365 break; 5366 } 5367 gap--; 5368 } 5369 if (gap == 0) { 5370 /* Nothing left in map */ 5371 memset(asoc->mapping_array, 0, asoc->mapping_array_size); 5372 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1; 5373 asoc->highest_tsn_inside_map = asoc->cumulative_tsn; 5374 } 5375 asoc->last_revoke_count = cnt; 5376 SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer); 5377 sctp_send_sack(stcb); 5378 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN); 5379 reneged_asoc_ids[reneged_at] = sctp_get_associd(stcb); 5380 reneged_at++; 5381 } 5382 /* 5383 * Another issue, in un-setting the TSN's in the mapping array we 5384 * DID NOT adjust the higest_tsn marker. This will cause one of two 5385 * things to occur. It may cause us to do extra work in checking for 5386 * our mapping array movement. More importantly it may cause us to 5387 * SACK every datagram. This may not be a bad thing though since we 5388 * will recover once we get our cum-ack above and all this stuff we 5389 * dumped recovered. 5390 */ 5391 } 5392 5393 void 5394 sctp_drain() 5395 { 5396 /* 5397 * We must walk the PCB lists for ALL associations here. The system 5398 * is LOW on MBUF's and needs help. This is where reneging will 5399 * occur. We really hope this does NOT happen! 5400 */ 5401 struct sctp_inpcb *inp; 5402 struct sctp_tcb *stcb; 5403 5404 SCTP_INP_INFO_RLOCK(); 5405 LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) { 5406 /* For each endpoint */ 5407 SCTP_INP_RLOCK(inp); 5408 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 5409 /* For each association */ 5410 SCTP_TCB_LOCK(stcb); 5411 sctp_drain_mbufs(inp, stcb); 5412 SCTP_TCB_UNLOCK(stcb); 5413 } 5414 SCTP_INP_RUNLOCK(inp); 5415 } 5416 SCTP_INP_INFO_RUNLOCK(); 5417 } 5418 5419 /* 5420 * start a new iterator 5421 * iterates through all endpoints and associations based on the pcb_state 5422 * flags and asoc_state. "af" (mandatory) is executed for all matching 5423 * assocs and "ef" (optional) is executed when the iterator completes. 5424 * "inpf" (optional) is executed for each new endpoint as it is being 5425 * iterated through. inpe (optional) is called when the inp completes 5426 * its way through all the stcbs. 5427 */ 5428 int 5429 sctp_initiate_iterator(inp_func inpf, 5430 asoc_func af, 5431 inp_func inpe, 5432 uint32_t pcb_state, 5433 uint32_t pcb_features, 5434 uint32_t asoc_state, 5435 void *argp, 5436 uint32_t argi, 5437 end_func ef, 5438 struct sctp_inpcb *s_inp, 5439 uint8_t chunk_output_off) 5440 { 5441 struct sctp_iterator *it = NULL; 5442 5443 if (af == NULL) { 5444 return (-1); 5445 } 5446 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator), 5447 "Iterator"); 5448 if (it == NULL) { 5449 return (ENOMEM); 5450 } 5451 memset(it, 0, sizeof(*it)); 5452 it->function_assoc = af; 5453 it->function_inp = inpf; 5454 if (inpf) 5455 it->done_current_ep = 0; 5456 else 5457 it->done_current_ep = 1; 5458 it->function_atend = ef; 5459 it->pointer = argp; 5460 it->val = argi; 5461 it->pcb_flags = pcb_state; 5462 it->pcb_features = pcb_features; 5463 it->asoc_state = asoc_state; 5464 it->function_inp_end = inpe; 5465 it->no_chunk_output = chunk_output_off; 5466 if (s_inp) { 5467 it->inp = s_inp; 5468 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP; 5469 } else { 5470 SCTP_INP_INFO_RLOCK(); 5471 it->inp = LIST_FIRST(&sctppcbinfo.listhead); 5472 5473 SCTP_INP_INFO_RUNLOCK(); 5474 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP; 5475 5476 } 5477 SCTP_IPI_ITERATOR_WQ_LOCK(); 5478 if (it->inp) 5479 SCTP_INP_INCR_REF(it->inp); 5480 TAILQ_INSERT_TAIL(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr); 5481 #if defined(SCTP_USE_THREAD_BASED_ITERATOR) 5482 if (sctppcbinfo.iterator_running == 0) { 5483 sctp_wakeup_iterator(); 5484 } 5485 SCTP_IPI_ITERATOR_WQ_UNLOCK(); 5486 #else 5487 if (it->inp) 5488 SCTP_INP_DECR_REF(it->inp); 5489 SCTP_IPI_ITERATOR_WQ_UNLOCK(); 5490 /* Init the timer */ 5491 SCTP_OS_TIMER_INIT(&it->tmr.timer); 5492 /* add to the list of all iterators */ 5493 sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR, (struct sctp_inpcb *)it, 5494 NULL, NULL); 5495 #endif 5496 return (0); 5497 } 5498