1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/types.h> 30 #include <sys/systm.h> 31 #include <sys/stream.h> 32 #include <sys/cmn_err.h> 33 #include <sys/kmem.h> 34 #define _SUN_TPI_VERSION 2 35 #include <sys/tihdr.h> 36 #include <sys/socket.h> 37 #include <sys/strsun.h> 38 #include <sys/strsubr.h> 39 40 #include <netinet/in.h> 41 #include <netinet/ip6.h> 42 #include <netinet/tcp_seq.h> 43 #include <netinet/sctp.h> 44 45 #include <inet/common.h> 46 #include <inet/ip.h> 47 #include <inet/ip6.h> 48 #include <inet/mib2.h> 49 #include <inet/ipclassifier.h> 50 #include <inet/ipp_common.h> 51 #include <inet/ipsec_impl.h> 52 #include <inet/sctp_ip.h> 53 54 #include "sctp_impl.h" 55 #include "sctp_asconf.h" 56 #include "sctp_addr.h" 57 58 static struct kmem_cache *sctp_kmem_set_cache; 59 60 /* 61 * PR-SCTP comments. 62 * 63 * When we get a valid Forward TSN chunk, we check the fragment list for this 64 * SSN and preceeding SSNs free all them. Further, if this Forward TSN causes 65 * the next expected SSN to be present in the stream queue, we deliver any 66 * such stranded messages upstream. We also update the SACK info. appropriately. 67 * When checking for advancing the cumulative ack (in sctp_cumack()) we must 68 * check for abandoned chunks and messages. While traversing the tramsmit 69 * list if we come across an abandoned chunk, we can skip the message (i.e. 70 * take it out of the (re)transmit list) since this message, and hence this 71 * chunk, has been marked abandoned by sctp_rexmit(). If we come across an 72 * unsent chunk for a message this now abandoned we need to check if a 73 * Forward TSN needs to be sent, this could be a case where we deferred sending 74 * a Forward TSN in sctp_get_msg_to_send(). Further, after processing a 75 * SACK we check if the Advanced peer ack point can be moved ahead, i.e. 76 * if we can send a Forward TSN via sctp_check_abandoned_data(). 77 */ 78 void 79 sctp_free_set(sctp_set_t *s) 80 { 81 sctp_set_t *p; 82 83 while (s) { 84 p = s->next; 85 kmem_cache_free(sctp_kmem_set_cache, s); 86 s = p; 87 } 88 } 89 90 static void 91 sctp_ack_add(sctp_set_t **head, uint32_t tsn, int *num) 92 { 93 sctp_set_t *p, *t; 94 95 if (head == NULL || num == NULL) 96 return; 97 98 ASSERT(*num >= 0); 99 ASSERT((*num == 0 && *head == NULL) || (*num > 0 && *head != NULL)); 100 101 if (*head == NULL) { 102 *head = kmem_cache_alloc(sctp_kmem_set_cache, KM_NOSLEEP); 103 if (*head == NULL) 104 return; 105 (*head)->prev = (*head)->next = NULL; 106 (*head)->begin = tsn; 107 (*head)->end = tsn; 108 *num = 1; 109 return; 110 } 111 112 ASSERT((*head)->prev == NULL); 113 114 /* 115 * Handle this special case here so we don't have to check 116 * for it each time in the loop. 117 */ 118 if (SEQ_LT(tsn + 1, (*head)->begin)) { 119 /* add a new set, and move the head pointer */ 120 t = kmem_cache_alloc(sctp_kmem_set_cache, KM_NOSLEEP); 121 if (t == NULL) 122 return; 123 t->next = *head; 124 t->prev = NULL; 125 (*head)->prev = t; 126 t->begin = tsn; 127 t->end = tsn; 128 (*num)++; 129 *head = t; 130 return; 131 } 132 133 /* 134 * We need to handle the following cases, where p points to 135 * the current set (as we walk through the loop): 136 * 137 * 1. tsn is entirely less than p; create a new set before p. 138 * 2. tsn borders p from less; coalesce p with tsn. 139 * 3. tsn is withing p; do nothing. 140 * 4. tsn borders p from greater; coalesce p with tsn. 141 * 4a. p may now border p->next from less; if so, coalesce those 142 * two sets. 143 * 5. tsn is entirely greater then all sets; add a new set at 144 * the end. 145 */ 146 for (p = *head; ; p = p->next) { 147 if (SEQ_LT(tsn + 1, p->begin)) { 148 /* 1: add a new set before p. */ 149 t = kmem_cache_alloc(sctp_kmem_set_cache, KM_NOSLEEP); 150 if (t == NULL) 151 return; 152 t->next = p; 153 t->prev = NULL; 154 t->begin = tsn; 155 t->end = tsn; 156 if (p->prev) { 157 t->prev = p->prev; 158 p->prev->next = t; 159 } 160 p->prev = t; 161 (*num)++; 162 return; 163 } 164 165 if ((tsn + 1) == p->begin) { 166 /* 2: adjust p->begin */ 167 p->begin = tsn; 168 return; 169 } 170 171 if (SEQ_GEQ(tsn, p->begin) && SEQ_LEQ(tsn, p->end)) { 172 /* 3; do nothing */ 173 return; 174 } 175 176 if ((p->end + 1) == tsn) { 177 /* 4; adjust p->end */ 178 p->end = tsn; 179 180 if (p->next != NULL && (tsn + 1) == p->next->begin) { 181 /* 4a: coalesce p and p->next */ 182 t = p->next; 183 p->end = t->end; 184 p->next = t->next; 185 if (t->next != NULL) 186 t->next->prev = p; 187 kmem_cache_free(sctp_kmem_set_cache, t); 188 (*num)--; 189 } 190 return; 191 } 192 193 if (p->next == NULL) { 194 /* 5: add new set at the end */ 195 t = kmem_cache_alloc(sctp_kmem_set_cache, KM_NOSLEEP); 196 if (t == NULL) 197 return; 198 t->next = NULL; 199 t->prev = p; 200 t->begin = tsn; 201 t->end = tsn; 202 p->next = t; 203 (*num)++; 204 return; 205 } 206 207 if (SEQ_GT(tsn, p->end + 1)) 208 continue; 209 } 210 } 211 212 static void 213 sctp_ack_rem(sctp_set_t **head, uint32_t end, int *num) 214 { 215 sctp_set_t *p, *t; 216 217 if (head == NULL || *head == NULL || num == NULL) 218 return; 219 220 /* Nothing to remove */ 221 if (SEQ_LT(end, (*head)->begin)) 222 return; 223 224 /* Find out where to start removing sets */ 225 for (p = *head; p->next; p = p->next) { 226 if (SEQ_LEQ(end, p->end)) 227 break; 228 } 229 230 if (SEQ_LT(end, p->end) && SEQ_GEQ(end, p->begin)) { 231 /* adjust p */ 232 p->begin = end + 1; 233 /* all done */ 234 if (p == *head) 235 return; 236 } else if (SEQ_GEQ(end, p->end)) { 237 /* remove this set too */ 238 p = p->next; 239 } 240 241 /* unlink everything before this set */ 242 t = *head; 243 *head = p; 244 if (p != NULL && p->prev != NULL) { 245 p->prev->next = NULL; 246 p->prev = NULL; 247 } 248 249 sctp_free_set(t); 250 251 /* recount the number of sets */ 252 *num = 0; 253 254 for (p = *head; p != NULL; p = p->next) 255 (*num)++; 256 } 257 258 void 259 sctp_sets_init() 260 { 261 sctp_kmem_set_cache = kmem_cache_create("sctp_set_cache", 262 sizeof (sctp_set_t), 0, NULL, NULL, NULL, NULL, 263 NULL, 0); 264 } 265 266 void 267 sctp_sets_fini() 268 { 269 kmem_cache_destroy(sctp_kmem_set_cache); 270 } 271 272 sctp_chunk_hdr_t * 273 sctp_first_chunk(uchar_t *rptr, ssize_t remaining) 274 { 275 sctp_chunk_hdr_t *ch; 276 uint16_t ch_len; 277 278 if (remaining < sizeof (*ch)) { 279 return (NULL); 280 } 281 282 ch = (sctp_chunk_hdr_t *)rptr; 283 ch_len = ntohs(ch->sch_len); 284 285 if (ch_len < sizeof (*ch) || remaining < ch_len) { 286 return (NULL); 287 } 288 289 return (ch); 290 } 291 292 sctp_chunk_hdr_t * 293 sctp_next_chunk(sctp_chunk_hdr_t *ch, ssize_t *remaining) 294 { 295 int pad; 296 uint16_t ch_len; 297 298 if (!ch) { 299 return (NULL); 300 } 301 302 ch_len = ntohs(ch->sch_len); 303 304 if ((pad = ch_len & (SCTP_ALIGN - 1)) != 0) { 305 pad = SCTP_ALIGN - pad; 306 } 307 308 *remaining -= (ch_len + pad); 309 ch = (sctp_chunk_hdr_t *)((char *)ch + ch_len + pad); 310 311 return (sctp_first_chunk((uchar_t *)ch, *remaining)); 312 } 313 314 /* 315 * Attach ancillary data to a received SCTP segments. 316 * If the source address (fp) is not the primary, send up a 317 * unitdata_ind so recvfrom() can populate the msg_name field. 318 * If ancillary data is also requested, we append it to the 319 * unitdata_req. Otherwise, we just send up an optdata_ind. 320 */ 321 static int 322 sctp_input_add_ancillary(sctp_t *sctp, mblk_t **mp, sctp_data_hdr_t *dcp, 323 sctp_faddr_t *fp, ip6_pkt_t *ipp) 324 { 325 struct T_unitdata_ind *tudi; 326 int optlen; 327 int hdrlen; 328 uchar_t *optptr; 329 struct cmsghdr *cmsg; 330 mblk_t *mp1; 331 struct sockaddr_in6 sin_buf[1]; 332 struct sockaddr_in6 *sin6; 333 struct sockaddr_in *sin4; 334 uint_t addflag = 0; 335 336 sin4 = NULL; 337 sin6 = NULL; 338 339 optlen = hdrlen = 0; 340 341 /* Figure out address size */ 342 if (sctp->sctp_ipversion == IPV4_VERSION) { 343 sin4 = (struct sockaddr_in *)sin_buf; 344 sin4->sin_family = AF_INET; 345 sin4->sin_port = sctp->sctp_fport; 346 IN6_V4MAPPED_TO_IPADDR(&fp->faddr, sin4->sin_addr.s_addr); 347 hdrlen = sizeof (*tudi) + sizeof (*sin4); 348 } else { 349 sin6 = sin_buf; 350 sin6->sin6_family = AF_INET6; 351 sin6->sin6_port = sctp->sctp_fport; 352 sin6->sin6_addr = fp->faddr; 353 hdrlen = sizeof (*tudi) + sizeof (*sin6); 354 } 355 356 /* If app asked to receive send / recv info */ 357 if (sctp->sctp_recvsndrcvinfo) { 358 optlen += sizeof (*cmsg) + sizeof (struct sctp_sndrcvinfo); 359 if (hdrlen == 0) 360 hdrlen = sizeof (struct T_optdata_ind); 361 } 362 363 if (sctp->sctp_ipv6_recvancillary == 0) 364 goto noancillary; 365 366 if ((ipp->ipp_fields & IPPF_IFINDEX) && 367 ipp->ipp_ifindex != sctp->sctp_recvifindex && 368 (sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVPKTINFO)) { 369 optlen += sizeof (*cmsg) + sizeof (struct in6_pktinfo); 370 if (hdrlen == 0) 371 hdrlen = sizeof (struct T_unitdata_ind); 372 addflag |= SCTP_IPV6_RECVPKTINFO; 373 } 374 /* If app asked for hoplimit and it has changed ... */ 375 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 376 ipp->ipp_hoplimit != sctp->sctp_recvhops && 377 (sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVHOPLIMIT)) { 378 optlen += sizeof (*cmsg) + sizeof (uint_t); 379 if (hdrlen == 0) 380 hdrlen = sizeof (struct T_unitdata_ind); 381 addflag |= SCTP_IPV6_RECVHOPLIMIT; 382 } 383 /* If app asked for hopbyhop headers and it has changed ... */ 384 if ((sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVHOPOPTS) && 385 ip_cmpbuf(sctp->sctp_hopopts, sctp->sctp_hopoptslen, 386 (ipp->ipp_fields & IPPF_HOPOPTS), 387 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 388 optlen += sizeof (*cmsg) + ipp->ipp_hopoptslen - 389 sctp->sctp_v6label_len; 390 if (hdrlen == 0) 391 hdrlen = sizeof (struct T_unitdata_ind); 392 addflag |= SCTP_IPV6_RECVHOPOPTS; 393 if (!ip_allocbuf((void **)&sctp->sctp_hopopts, 394 &sctp->sctp_hopoptslen, 395 (ipp->ipp_fields & IPPF_HOPOPTS), 396 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 397 return (-1); 398 } 399 /* If app asked for dst headers before routing headers ... */ 400 if ((sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVRTDSTOPTS) && 401 ip_cmpbuf(sctp->sctp_rtdstopts, sctp->sctp_rtdstoptslen, 402 (ipp->ipp_fields & IPPF_RTDSTOPTS), 403 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 404 optlen += sizeof (*cmsg) + ipp->ipp_rtdstoptslen; 405 if (hdrlen == 0) 406 hdrlen = sizeof (struct T_unitdata_ind); 407 addflag |= SCTP_IPV6_RECVRTDSTOPTS; 408 if (!ip_allocbuf((void **)&sctp->sctp_rtdstopts, 409 &sctp->sctp_rtdstoptslen, 410 (ipp->ipp_fields & IPPF_RTDSTOPTS), 411 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 412 return (-1); 413 } 414 /* If app asked for routing headers and it has changed ... */ 415 if (sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVRTHDR) { 416 if (ip_cmpbuf(sctp->sctp_rthdr, sctp->sctp_rthdrlen, 417 (ipp->ipp_fields & IPPF_RTHDR), 418 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 419 optlen += sizeof (*cmsg) + ipp->ipp_rthdrlen; 420 if (hdrlen == 0) 421 hdrlen = sizeof (struct T_unitdata_ind); 422 addflag |= SCTP_IPV6_RECVRTHDR; 423 if (!ip_allocbuf((void **)&sctp->sctp_rthdr, 424 &sctp->sctp_rthdrlen, 425 (ipp->ipp_fields & IPPF_RTHDR), 426 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 427 return (-1); 428 } 429 } 430 /* If app asked for dest headers and it has changed ... */ 431 if ((sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVDSTOPTS) && 432 ip_cmpbuf(sctp->sctp_dstopts, sctp->sctp_dstoptslen, 433 (ipp->ipp_fields & IPPF_DSTOPTS), 434 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 435 optlen += sizeof (*cmsg) + ipp->ipp_dstoptslen; 436 if (hdrlen == 0) 437 hdrlen = sizeof (struct T_unitdata_ind); 438 addflag |= SCTP_IPV6_RECVDSTOPTS; 439 if (!ip_allocbuf((void **)&sctp->sctp_dstopts, 440 &sctp->sctp_dstoptslen, 441 (ipp->ipp_fields & IPPF_DSTOPTS), 442 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 443 return (-1); 444 } 445 noancillary: 446 /* Nothing to add */ 447 if (hdrlen == 0) 448 return (-1); 449 450 mp1 = allocb(hdrlen + optlen + sizeof (void *), BPRI_MED); 451 if (mp1 == NULL) 452 return (-1); 453 mp1->b_cont = *mp; 454 *mp = mp1; 455 mp1->b_rptr += sizeof (void *); /* pointer worth of padding */ 456 mp1->b_wptr = mp1->b_rptr + hdrlen + optlen; 457 DB_TYPE(mp1) = M_PROTO; 458 tudi = (struct T_unitdata_ind *)mp1->b_rptr; 459 tudi->PRIM_type = T_UNITDATA_IND; 460 tudi->SRC_length = sin4 ? sizeof (*sin4) : sizeof (*sin6); 461 tudi->SRC_offset = sizeof (*tudi); 462 tudi->OPT_offset = sizeof (*tudi) + tudi->SRC_length; 463 tudi->OPT_length = optlen; 464 if (sin4) { 465 bcopy(sin4, tudi + 1, sizeof (*sin4)); 466 } else { 467 bcopy(sin6, tudi + 1, sizeof (*sin6)); 468 } 469 optptr = (uchar_t *)tudi + tudi->OPT_offset; 470 471 if (sctp->sctp_recvsndrcvinfo) { 472 /* XXX need backout method if memory allocation fails. */ 473 struct sctp_sndrcvinfo *sri; 474 475 cmsg = (struct cmsghdr *)optptr; 476 cmsg->cmsg_level = IPPROTO_SCTP; 477 cmsg->cmsg_type = SCTP_SNDRCV; 478 cmsg->cmsg_len = sizeof (*cmsg) + sizeof (*sri); 479 optptr += sizeof (*cmsg); 480 481 sri = (struct sctp_sndrcvinfo *)(cmsg + 1); 482 ASSERT(OK_32PTR(sri)); 483 sri->sinfo_stream = ntohs(dcp->sdh_sid); 484 sri->sinfo_ssn = ntohs(dcp->sdh_ssn); 485 if (SCTP_DATA_GET_UBIT(dcp)) { 486 sri->sinfo_flags = MSG_UNORDERED; 487 } else { 488 sri->sinfo_flags = 0; 489 } 490 sri->sinfo_ppid = dcp->sdh_payload_id; 491 sri->sinfo_context = 0; 492 sri->sinfo_timetolive = 0; 493 sri->sinfo_tsn = ntohl(dcp->sdh_tsn); 494 sri->sinfo_cumtsn = sctp->sctp_ftsn; 495 sri->sinfo_assoc_id = 0; 496 497 optptr += sizeof (*sri); 498 } 499 500 /* 501 * If app asked for pktinfo and the index has changed ... 502 * Note that the local address never changes for the connection. 503 */ 504 if (addflag & SCTP_IPV6_RECVPKTINFO) { 505 struct in6_pktinfo *pkti; 506 507 cmsg = (struct cmsghdr *)optptr; 508 cmsg->cmsg_level = IPPROTO_IPV6; 509 cmsg->cmsg_type = IPV6_PKTINFO; 510 cmsg->cmsg_len = sizeof (*cmsg) + sizeof (*pkti); 511 optptr += sizeof (*cmsg); 512 513 pkti = (struct in6_pktinfo *)optptr; 514 if (sctp->sctp_ipversion == IPV6_VERSION) 515 pkti->ipi6_addr = sctp->sctp_ip6h->ip6_src; 516 else 517 IN6_IPADDR_TO_V4MAPPED(sctp->sctp_ipha->ipha_src, 518 &pkti->ipi6_addr); 519 pkti->ipi6_ifindex = ipp->ipp_ifindex; 520 optptr += sizeof (*pkti); 521 ASSERT(OK_32PTR(optptr)); 522 /* Save as "last" value */ 523 sctp->sctp_recvifindex = ipp->ipp_ifindex; 524 } 525 /* If app asked for hoplimit and it has changed ... */ 526 if (addflag & SCTP_IPV6_RECVHOPLIMIT) { 527 cmsg = (struct cmsghdr *)optptr; 528 cmsg->cmsg_level = IPPROTO_IPV6; 529 cmsg->cmsg_type = IPV6_HOPLIMIT; 530 cmsg->cmsg_len = sizeof (*cmsg) + sizeof (uint_t); 531 optptr += sizeof (*cmsg); 532 533 *(uint_t *)optptr = ipp->ipp_hoplimit; 534 optptr += sizeof (uint_t); 535 ASSERT(OK_32PTR(optptr)); 536 /* Save as "last" value */ 537 sctp->sctp_recvhops = ipp->ipp_hoplimit; 538 } 539 if (addflag & SCTP_IPV6_RECVHOPOPTS) { 540 cmsg = (struct cmsghdr *)optptr; 541 cmsg->cmsg_level = IPPROTO_IPV6; 542 cmsg->cmsg_type = IPV6_HOPOPTS; 543 cmsg->cmsg_len = sizeof (*cmsg) + ipp->ipp_hopoptslen; 544 optptr += sizeof (*cmsg); 545 546 bcopy(ipp->ipp_hopopts, optptr, ipp->ipp_hopoptslen); 547 optptr += ipp->ipp_hopoptslen; 548 ASSERT(OK_32PTR(optptr)); 549 /* Save as last value */ 550 ip_savebuf((void **)&sctp->sctp_hopopts, 551 &sctp->sctp_hopoptslen, 552 (ipp->ipp_fields & IPPF_HOPOPTS), 553 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 554 } 555 if (addflag & SCTP_IPV6_RECVRTDSTOPTS) { 556 cmsg = (struct cmsghdr *)optptr; 557 cmsg->cmsg_level = IPPROTO_IPV6; 558 cmsg->cmsg_type = IPV6_RTHDRDSTOPTS; 559 cmsg->cmsg_len = sizeof (*cmsg) + ipp->ipp_rtdstoptslen; 560 optptr += sizeof (*cmsg); 561 562 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 563 optptr += ipp->ipp_rtdstoptslen; 564 ASSERT(OK_32PTR(optptr)); 565 /* Save as last value */ 566 ip_savebuf((void **)&sctp->sctp_rtdstopts, 567 &sctp->sctp_rtdstoptslen, 568 (ipp->ipp_fields & IPPF_RTDSTOPTS), 569 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 570 } 571 if (addflag & SCTP_IPV6_RECVRTHDR) { 572 cmsg = (struct cmsghdr *)optptr; 573 cmsg->cmsg_level = IPPROTO_IPV6; 574 cmsg->cmsg_type = IPV6_RTHDR; 575 cmsg->cmsg_len = sizeof (*cmsg) + ipp->ipp_rthdrlen; 576 optptr += sizeof (*cmsg); 577 578 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 579 optptr += ipp->ipp_rthdrlen; 580 ASSERT(OK_32PTR(optptr)); 581 /* Save as last value */ 582 ip_savebuf((void **)&sctp->sctp_rthdr, 583 &sctp->sctp_rthdrlen, 584 (ipp->ipp_fields & IPPF_RTHDR), 585 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 586 } 587 if (addflag & SCTP_IPV6_RECVDSTOPTS) { 588 cmsg = (struct cmsghdr *)optptr; 589 cmsg->cmsg_level = IPPROTO_IPV6; 590 cmsg->cmsg_type = IPV6_DSTOPTS; 591 cmsg->cmsg_len = sizeof (*cmsg) + ipp->ipp_dstoptslen; 592 optptr += sizeof (*cmsg); 593 594 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 595 optptr += ipp->ipp_dstoptslen; 596 ASSERT(OK_32PTR(optptr)); 597 /* Save as last value */ 598 ip_savebuf((void **)&sctp->sctp_dstopts, 599 &sctp->sctp_dstoptslen, 600 (ipp->ipp_fields & IPPF_DSTOPTS), 601 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 602 } 603 604 ASSERT(optptr == mp1->b_wptr); 605 606 return (0); 607 } 608 609 void 610 sctp_free_reass(sctp_instr_t *sip) 611 { 612 mblk_t *mp, *mpnext, *mctl; 613 614 for (mp = sip->istr_reass; mp != NULL; mp = mpnext) { 615 mpnext = mp->b_next; 616 mp->b_next = NULL; 617 mp->b_prev = NULL; 618 if (DB_TYPE(mp) == M_CTL) { 619 mctl = mp; 620 ASSERT(mp->b_cont != NULL); 621 mp = mp->b_cont; 622 mctl->b_cont = NULL; 623 freeb(mctl); 624 } 625 freemsg(mp); 626 } 627 } 628 629 /* 630 * If the series of data fragments of which dmp is a part is successfully 631 * reassembled, the first mblk in the series is returned. dc is adjusted 632 * to point at the data chunk in the lead mblk, and b_rptr also points to 633 * the data chunk; the following mblk's b_rptr's point at the actual payload. 634 * 635 * If the series is not yet reassembled, NULL is returned. dc is not changed. 636 * XXX should probably move this up into the state machine. 637 */ 638 639 /* Fragment list for un-ordered messages. Partial delivery is not supported */ 640 static mblk_t * 641 sctp_uodata_frag(sctp_t *sctp, mblk_t *dmp, sctp_data_hdr_t **dc) 642 { 643 mblk_t *hmp; 644 mblk_t *begin = NULL; 645 mblk_t *end = NULL; 646 sctp_data_hdr_t *qdc; 647 uint32_t ntsn; 648 uint32_t tsn = ntohl((*dc)->sdh_tsn); 649 #ifdef DEBUG 650 mblk_t *mp1; 651 #endif 652 653 /* First frag. */ 654 if (sctp->sctp_uo_frags == NULL) { 655 sctp->sctp_uo_frags = dmp; 656 return (NULL); 657 } 658 hmp = sctp->sctp_uo_frags; 659 /* 660 * Insert the segment according to the TSN, fragmented unordered 661 * chunks are sequenced by TSN. 662 */ 663 while (hmp != NULL) { 664 qdc = (sctp_data_hdr_t *)hmp->b_rptr; 665 ntsn = ntohl(qdc->sdh_tsn); 666 if (SEQ_GT(ntsn, tsn)) { 667 if (hmp->b_prev == NULL) { 668 dmp->b_next = hmp; 669 hmp->b_prev = dmp; 670 sctp->sctp_uo_frags = dmp; 671 } else { 672 dmp->b_next = hmp; 673 dmp->b_prev = hmp->b_prev; 674 hmp->b_prev->b_next = dmp; 675 hmp->b_prev = dmp; 676 } 677 break; 678 } 679 if (hmp->b_next == NULL) { 680 hmp->b_next = dmp; 681 dmp->b_prev = hmp; 682 break; 683 } 684 hmp = hmp->b_next; 685 } 686 /* check if we completed a msg */ 687 if (SCTP_DATA_GET_BBIT(*dc)) { 688 begin = dmp; 689 } else if (SCTP_DATA_GET_EBIT(*dc)) { 690 end = dmp; 691 } 692 /* 693 * We walk consecutive TSNs backwards till we get a seg. with 694 * the B bit 695 */ 696 if (begin == NULL) { 697 for (hmp = dmp->b_prev; hmp != NULL; hmp = hmp->b_prev) { 698 qdc = (sctp_data_hdr_t *)hmp->b_rptr; 699 ntsn = ntohl(qdc->sdh_tsn); 700 if ((int32_t)(tsn - ntsn) > 1) { 701 return (NULL); 702 } 703 if (SCTP_DATA_GET_BBIT(qdc)) { 704 begin = hmp; 705 break; 706 } 707 tsn = ntsn; 708 } 709 } 710 tsn = ntohl((*dc)->sdh_tsn); 711 /* 712 * We walk consecutive TSNs till we get a seg. with the E bit 713 */ 714 if (end == NULL) { 715 for (hmp = dmp->b_next; hmp != NULL; hmp = hmp->b_next) { 716 qdc = (sctp_data_hdr_t *)hmp->b_rptr; 717 ntsn = ntohl(qdc->sdh_tsn); 718 if ((int32_t)(ntsn - tsn) > 1) { 719 return (NULL); 720 } 721 if (SCTP_DATA_GET_EBIT(qdc)) { 722 end = hmp; 723 break; 724 } 725 tsn = ntsn; 726 } 727 } 728 if (begin == NULL || end == NULL) { 729 return (NULL); 730 } 731 /* Got one!, Remove the msg from the list */ 732 if (sctp->sctp_uo_frags == begin) { 733 ASSERT(begin->b_prev == NULL); 734 sctp->sctp_uo_frags = end->b_next; 735 if (end->b_next != NULL) 736 end->b_next->b_prev = NULL; 737 } else { 738 begin->b_prev->b_next = end->b_next; 739 if (end->b_next != NULL) 740 end->b_next->b_prev = begin->b_prev; 741 } 742 begin->b_prev = NULL; 743 end->b_next = NULL; 744 745 /* 746 * Null out b_next and b_prev and chain using b_cont. 747 */ 748 dmp = end = begin; 749 hmp = begin->b_next; 750 *dc = (sctp_data_hdr_t *)begin->b_rptr; 751 begin->b_next = NULL; 752 while (hmp != NULL) { 753 qdc = (sctp_data_hdr_t *)hmp->b_rptr; 754 hmp->b_rptr = (uchar_t *)(qdc + 1); 755 end = hmp->b_next; 756 dmp->b_cont = hmp; 757 dmp = hmp; 758 759 if (end != NULL) 760 hmp->b_next = NULL; 761 hmp->b_prev = NULL; 762 hmp = end; 763 } 764 BUMP_LOCAL(sctp->sctp_reassmsgs); 765 #ifdef DEBUG 766 mp1 = begin; 767 while (mp1 != NULL) { 768 ASSERT(mp1->b_next == NULL); 769 ASSERT(mp1->b_prev == NULL); 770 mp1 = mp1->b_cont; 771 } 772 #endif 773 return (begin); 774 } 775 /* 776 * Fragment list for ordered messages. 777 * If no error occures, error is set to 0. If we run out of memory, error 778 * is set to 1. If the peer commits a fatal error (like using different 779 * sequence numbers for the same data fragment series), the association is 780 * aborted and error is set to 2. 781 */ 782 static mblk_t * 783 sctp_data_frag(sctp_t *sctp, mblk_t *dmp, sctp_data_hdr_t **dc, int *error, 784 sctp_instr_t *sip, int trypartial, int *tpfinished) 785 { 786 mblk_t *hmp; 787 mblk_t *pmp; 788 mblk_t *qmp; 789 mblk_t *mp; 790 mblk_t *prev; 791 mblk_t *prevprev; 792 mblk_t *first_mp; 793 sctp_reass_t *srp; 794 sctp_data_hdr_t *qdc; 795 sctp_data_hdr_t *bdc; 796 sctp_data_hdr_t *edc; 797 uint32_t tsn; 798 799 /* 800 * We can overwrite the Link Layer + IP header here, I suppose. 801 * The M_CTL does not leave this function. We need to check 802 * DB_REF(dmp) before using DB_BASE(dmp), since there could be 803 * two fragments for different ssns in the same mblk. 804 */ 805 #define SCTP_NEW_REASS(nmp, dmp, srp, seterror) \ 806 if ((DB_REF(dmp) == 2) && (MBLKHEAD(dmp) >= \ 807 (sizeof (*(srp)) + sizeof (sctp_hdr_t))) && \ 808 (IS_P2ALIGNED(DB_BASE(dmp), sizeof (uintptr_t)))) { \ 809 (nmp) = (dmp); \ 810 } else { \ 811 (nmp) = allocb(sizeof (*(srp)), BPRI_MED); \ 812 if ((nmp) == NULL) { \ 813 switch (seterror) { \ 814 case B_TRUE: \ 815 *error = 1; \ 816 break; \ 817 } \ 818 return (NULL); \ 819 } \ 820 DB_TYPE(nmp) = M_CTL; \ 821 (nmp)->b_cont = dmp; \ 822 } \ 823 (srp) = (sctp_reass_t *)DB_BASE(nmp); 824 825 *error = 0; 826 827 /* find the reassembly queue for this data chunk */ 828 hmp = qmp = sip->istr_reass; 829 for (; hmp != NULL; hmp = hmp->b_next) { 830 srp = (sctp_reass_t *)DB_BASE(hmp); 831 if (ntohs((*dc)->sdh_ssn) == srp->ssn) 832 goto foundit; 833 else if (SSN_GT(srp->ssn, ntohs((*dc)->sdh_ssn))) 834 break; 835 qmp = hmp; 836 } 837 838 SCTP_NEW_REASS(pmp, dmp, srp, B_TRUE); 839 srp->ssn = ntohs((*dc)->sdh_ssn); 840 srp->needed = 0; 841 srp->got = 1; 842 srp->tail = dmp; 843 srp->partial_delivered = B_FALSE; 844 845 if (hmp != NULL) { 846 if (sip->istr_reass == hmp) { 847 sip->istr_reass = pmp; 848 pmp->b_next = hmp; 849 pmp->b_prev = NULL; 850 hmp->b_prev = pmp; 851 } else { 852 qmp->b_next = pmp; 853 pmp->b_prev = qmp; 854 pmp->b_next = hmp; 855 hmp->b_prev = pmp; 856 } 857 } else { 858 /* make a new reass head and stick it on the end */ 859 if (sip->istr_reass == NULL) { 860 sip->istr_reass = pmp; 861 pmp->b_prev = NULL; 862 } else { 863 qmp->b_next = pmp; 864 pmp->b_prev = qmp; 865 } 866 pmp->b_next = NULL; 867 } 868 return (NULL); 869 foundit: 870 /* 871 * else already have a reassembly queue. Insert the new data chunk 872 * in the reassemble queue. Try the tail first, on the assumption 873 * that the fragments are coming in in order. 874 */ 875 876 qmp = srp->tail; 877 qdc = (sctp_data_hdr_t *)qmp->b_rptr; 878 ASSERT(qmp->b_cont == NULL); 879 880 /* XXXIs it fine to do this just here? */ 881 if ((*dc)->sdh_sid != qdc->sdh_sid) { 882 /* our peer is fatally confused; XXX abort the assc */ 883 *error = 2; 884 return (NULL); 885 } 886 if (SEQ_GT(ntohl((*dc)->sdh_tsn), ntohl(qdc->sdh_tsn))) { 887 qmp->b_cont = dmp; 888 srp->tail = dmp; 889 dmp->b_cont = NULL; 890 goto inserted; 891 } 892 893 /* Next check for insertion at the beginning */ 894 qmp = (DB_TYPE(hmp) == M_DATA) ? hmp : hmp->b_cont; 895 qdc = (sctp_data_hdr_t *)qmp->b_rptr; 896 if (SEQ_LT(ntohl((*dc)->sdh_tsn), ntohl(qdc->sdh_tsn))) { 897 if (DB_TYPE(hmp) == M_DATA) { 898 sctp_reass_t *srp1 = srp; 899 900 SCTP_NEW_REASS(pmp, dmp, srp, B_TRUE); 901 ASSERT(pmp->b_prev == NULL && pmp->b_next == NULL); 902 if (sip->istr_reass == hmp) { 903 sip->istr_reass = pmp; 904 if (hmp->b_next != NULL) { 905 hmp->b_next->b_prev = pmp; 906 pmp->b_next = hmp->b_next; 907 } 908 } else { 909 hmp->b_prev->b_next = pmp; 910 pmp->b_prev = hmp->b_prev; 911 if (hmp->b_next != NULL) { 912 hmp->b_next->b_prev = pmp; 913 pmp->b_next = hmp->b_next; 914 } 915 } 916 srp->ssn = srp1->ssn; 917 srp->needed = srp1->needed; 918 srp->got = srp1->got; 919 srp->tail = srp1->tail; 920 srp->partial_delivered = srp1->partial_delivered; 921 hmp->b_next = hmp->b_prev = NULL; 922 dmp->b_cont = hmp; 923 hmp = pmp; 924 } else { 925 ASSERT(DB_TYPE(hmp) == M_CTL); 926 dmp->b_cont = qmp; 927 hmp->b_cont = dmp; 928 } 929 goto inserted; 930 } 931 932 /* Insert somewhere in the middle */ 933 for (;;) { 934 /* Tail check above should have caught this */ 935 ASSERT(qmp->b_cont != NULL); 936 937 qdc = (sctp_data_hdr_t *)qmp->b_cont->b_rptr; 938 if (SEQ_LT(ntohl((*dc)->sdh_tsn), ntohl(qdc->sdh_tsn))) { 939 /* insert here */ 940 dmp->b_cont = qmp->b_cont; 941 qmp->b_cont = dmp; 942 break; 943 } 944 qmp = qmp->b_cont; 945 } 946 947 inserted: 948 (srp->got)++; 949 first_mp = (DB_TYPE(hmp) == M_DATA) ? hmp : hmp->b_cont; 950 if (srp->needed == 0) { 951 /* check if we have the first and last fragments */ 952 bdc = (sctp_data_hdr_t *)first_mp->b_rptr; 953 edc = (sctp_data_hdr_t *)srp->tail->b_rptr; 954 955 /* calculate how many fragments are needed, if possible */ 956 if (SCTP_DATA_GET_BBIT(bdc) && SCTP_DATA_GET_EBIT(edc)) 957 srp->needed = ntohl(edc->sdh_tsn) - 958 ntohl(bdc->sdh_tsn) + 1; 959 } 960 961 if (srp->needed != srp->got) { 962 if (!trypartial) 963 return (NULL); 964 /* 965 * Try partial delivery. We need a consecutive run of 966 * at least two chunks, starting from the first chunk 967 * (which may have been the last + 1 chunk from a 968 * previous partial delivery). 969 */ 970 dprint(4, ("trypartial: got=%d, needed=%d\n", 971 (int)(srp->got), (int)(srp->needed))); 972 mp = first_mp; 973 if (mp->b_cont == NULL) { 974 /* need at least two chunks */ 975 dprint(4, ("trypartial: only 1 chunk\n")); 976 return (NULL); 977 } 978 979 qdc = (sctp_data_hdr_t *)mp->b_rptr; 980 if (!SCTP_DATA_GET_BBIT(qdc)) { 981 /* don't have first chunk; can't do it. */ 982 dprint(4, ("trypartial: no beginning\n")); 983 return (NULL); 984 } 985 986 tsn = ntohl(qdc->sdh_tsn) + 1; 987 988 /* 989 * This loop has two exit conditions: the 990 * end of received chunks has been reached, or 991 * there is a break in the sequence. We want 992 * to chop the reassembly list as follows (the 993 * numbers are TSNs): 994 * 10 -> 11 -> | 12 (end of chunks) 995 * 10 -> 11 -> | 12 -> 14 (break in sequence) 996 */ 997 prevprev = prev = mp; 998 mp = mp->b_cont; 999 while (mp != NULL) { 1000 qdc = (sctp_data_hdr_t *)mp->b_rptr; 1001 if (ntohl(qdc->sdh_tsn) != tsn) { 1002 /* 1003 * break in sequence. 1004 * 1st and 2nd chunks are not sequntial. 1005 */ 1006 if (mp == first_mp->b_cont) 1007 return (NULL); 1008 /* Back up mp and prev */ 1009 mp = prev; 1010 prev = prevprev; 1011 break; 1012 } 1013 1014 /* end of sequence */ 1015 if (mp->b_cont == NULL) 1016 break; 1017 1018 prevprev = prev; 1019 prev = mp; 1020 mp = mp->b_cont; 1021 tsn++; 1022 } 1023 if (DB_TYPE(hmp) == M_DATA) { 1024 sctp_reass_t *srp1 = srp; 1025 1026 SCTP_NEW_REASS(pmp, mp, srp, B_FALSE); 1027 ASSERT(pmp->b_prev == NULL && pmp->b_next == NULL); 1028 if (sip->istr_reass == hmp) { 1029 sip->istr_reass = pmp; 1030 if (hmp->b_next != NULL) { 1031 hmp->b_next->b_prev = pmp; 1032 pmp->b_next = hmp->b_next; 1033 } 1034 } else { 1035 hmp->b_prev->b_next = pmp; 1036 pmp->b_prev = hmp->b_prev; 1037 if (hmp->b_next != NULL) { 1038 hmp->b_next->b_prev = pmp; 1039 pmp->b_next = hmp->b_next; 1040 } 1041 } 1042 srp->ssn = srp1->ssn; 1043 srp->needed = srp1->needed; 1044 srp->got = srp1->got; 1045 srp->tail = srp1->tail; 1046 hmp->b_next = hmp->b_prev = NULL; 1047 dmp = hmp; 1048 hmp = pmp; 1049 } else { 1050 ASSERT(DB_TYPE(hmp) == M_CTL); 1051 dmp = hmp->b_cont; 1052 hmp->b_cont = mp; 1053 } 1054 /* 1055 * mp now points at the last chunk in the sequence, 1056 * and prev points to mp's previous in the list. 1057 * We chop the list at prev, and convert mp into the 1058 * new list head by setting the B bit. Subsequence 1059 * fragment deliveries will follow the normal reassembly 1060 * path. 1061 */ 1062 prev->b_cont = NULL; 1063 bdc = (sctp_data_hdr_t *)mp->b_rptr; 1064 SCTP_DATA_SET_BBIT(bdc); 1065 *tpfinished = 0; 1066 srp->partial_delivered = B_TRUE; 1067 1068 dprint(4, ("trypartial: got some, got=%d, needed=%d\n", 1069 (int)(srp->got), (int)(srp->needed))); 1070 goto fixup; 1071 } 1072 1073 /* 1074 * else reassembly done; prepare the data for delivery. 1075 * First unlink hmp from the ssn list. 1076 */ 1077 if (sip->istr_reass == hmp) { 1078 sip->istr_reass = hmp->b_next; 1079 if (hmp->b_next) { 1080 hmp->b_next->b_prev = NULL; 1081 } 1082 } else { 1083 ASSERT(hmp->b_prev != NULL); 1084 hmp->b_prev->b_next = hmp->b_next; 1085 if (hmp->b_next) { 1086 hmp->b_next->b_prev = hmp->b_prev; 1087 } 1088 } 1089 1090 /* 1091 * Using b_prev and b_next was a little sinful, but OK since 1092 * this mblk is never put*'d. However, freeb() will still 1093 * ASSERT that they are unused, so we need to NULL them out now. 1094 */ 1095 hmp->b_next = NULL; 1096 hmp->b_prev = NULL; 1097 dmp = hmp; 1098 if (DB_TYPE(hmp) == M_CTL) { 1099 dmp = dmp->b_cont; 1100 hmp->b_cont = NULL; 1101 freeb(hmp); 1102 } 1103 *tpfinished = 1; 1104 1105 fixup: 1106 /* 1107 * Adjust all mblk's except the lead so their rptr's point to the 1108 * payload. sctp_data_chunk() will need to process the lead's 1109 * data chunk section, so leave it's rptr pointing at the data chunk. 1110 */ 1111 *dc = (sctp_data_hdr_t *)dmp->b_rptr; 1112 if (trypartial && !(*tpfinished)) { 1113 (srp->got)--; 1114 ASSERT(srp->got != 0); 1115 if (srp->needed != 0) { 1116 (srp->needed)--; 1117 ASSERT(srp->needed != 0); 1118 } 1119 } 1120 for (qmp = dmp->b_cont; qmp; qmp = qmp->b_cont) { 1121 qdc = (sctp_data_hdr_t *)qmp->b_rptr; 1122 qmp->b_rptr = (uchar_t *)(qdc + 1); 1123 1124 /* 1125 * If in partial delivery, deduct the balance from got 1126 * and needed here, now that we know we are actually 1127 * delivering these data. 1128 */ 1129 if (trypartial && !(*tpfinished)) { 1130 (srp->got)--; 1131 ASSERT(srp->got != 0); 1132 if (srp->needed != 0) { 1133 (srp->needed)--; 1134 ASSERT(srp->needed != 0); 1135 } 1136 } 1137 } 1138 BUMP_LOCAL(sctp->sctp_reassmsgs); 1139 1140 return (dmp); 1141 } 1142 1143 static void 1144 sctp_add_dup(uint32_t tsn, mblk_t **dups) 1145 { 1146 mblk_t *mp; 1147 size_t bsize = SCTP_DUP_MBLK_SZ * sizeof (tsn); 1148 1149 if (dups == NULL) { 1150 return; 1151 } 1152 1153 /* first time? */ 1154 if (*dups == NULL) { 1155 *dups = allocb(bsize, BPRI_MED); 1156 if (*dups == NULL) { 1157 return; 1158 } 1159 } 1160 1161 mp = *dups; 1162 if ((mp->b_wptr - mp->b_rptr) >= bsize) { 1163 /* maximum reached */ 1164 return; 1165 } 1166 1167 /* add the duplicate tsn */ 1168 bcopy(&tsn, mp->b_wptr, sizeof (tsn)); 1169 mp->b_wptr += sizeof (tsn); 1170 ASSERT((mp->b_wptr - mp->b_rptr) <= bsize); 1171 } 1172 1173 static void 1174 sctp_data_chunk(sctp_t *sctp, sctp_chunk_hdr_t *ch, mblk_t *mp, mblk_t **dups, 1175 sctp_faddr_t *fp, ip6_pkt_t *ipp) 1176 { 1177 sctp_data_hdr_t *dc; 1178 mblk_t *dmp, *pmp; 1179 mblk_t *errmp; 1180 sctp_instr_t *instr; 1181 int ubit; 1182 int isfrag; 1183 uint16_t ssn; 1184 uint32_t oftsn; 1185 boolean_t can_deliver = B_TRUE; 1186 uint32_t tsn; 1187 int dlen; 1188 int trypartial = 0; 1189 int tpfinished = 1; 1190 int32_t new_rwnd; 1191 1192 /* The following are used multiple times, so we inline them */ 1193 #define SCTP_ACK_IT(sctp, tsn) \ 1194 if (tsn == sctp->sctp_ftsn) { \ 1195 dprint(2, ("data_chunk: acking next %x\n", tsn)); \ 1196 (sctp)->sctp_ftsn++; \ 1197 if ((sctp)->sctp_sack_gaps > 0) \ 1198 (sctp)->sctp_force_sack = 1; \ 1199 } else if (SEQ_GT(tsn, sctp->sctp_ftsn)) { \ 1200 /* Got a gap; record it */ \ 1201 dprint(2, ("data_chunk: acking gap %x\n", tsn)); \ 1202 sctp_ack_add(&sctp->sctp_sack_info, tsn, \ 1203 &sctp->sctp_sack_gaps); \ 1204 sctp->sctp_force_sack = 1; \ 1205 } 1206 1207 errmp = NULL; 1208 dmp = NULL; 1209 1210 dc = (sctp_data_hdr_t *)ch; 1211 tsn = ntohl(dc->sdh_tsn); 1212 1213 dprint(3, ("sctp_data_chunk: mp=%p tsn=%x\n", (void *)mp, tsn)); 1214 1215 /* Check for duplicates */ 1216 if (SEQ_LT(tsn, sctp->sctp_ftsn)) { 1217 dprint(4, ("sctp_data_chunk: dropping duplicate\n")); 1218 sctp->sctp_force_sack = 1; 1219 sctp_add_dup(dc->sdh_tsn, dups); 1220 return; 1221 } 1222 1223 if (sctp->sctp_sack_info != NULL) { 1224 sctp_set_t *sp; 1225 1226 for (sp = sctp->sctp_sack_info; sp; sp = sp->next) { 1227 if (SEQ_GEQ(tsn, sp->begin) && SEQ_LEQ(tsn, sp->end)) { 1228 dprint(4, 1229 ("sctp_data_chunk: dropping dup > cumtsn\n")); 1230 sctp->sctp_force_sack = 1; 1231 sctp_add_dup(dc->sdh_tsn, dups); 1232 return; 1233 } 1234 } 1235 } 1236 1237 /* We cannot deliver anything up now but we still need to handle it. */ 1238 if (SCTP_IS_DETACHED(sctp)) { 1239 BUMP_MIB(&sctp_mib, sctpInClosed); 1240 can_deliver = B_FALSE; 1241 } 1242 1243 dlen = ntohs(dc->sdh_len) - sizeof (*dc); 1244 1245 /* Check for buffer space */ 1246 if (sctp->sctp_rwnd - sctp->sctp_rxqueued < dlen) { 1247 /* Drop and SACK, but don't advance the cumulative TSN. */ 1248 sctp->sctp_force_sack = 1; 1249 dprint(0, ("sctp_data_chunk: exceed rwnd %d rxqueued %d " 1250 "ssn %d tsn %x\n", sctp->sctp_rwnd, 1251 sctp->sctp_rxqueued, dc->sdh_ssn, ntohl(dc->sdh_tsn))); 1252 return; 1253 } 1254 1255 if (ntohs(dc->sdh_sid) >= sctp->sctp_num_istr) { 1256 uint16_t inval_parm[2]; 1257 1258 inval_parm[0] = dc->sdh_sid; 1259 /* RESERVED to be ignored at the receiving end */ 1260 inval_parm[1] = 0; 1261 /* ack and drop it */ 1262 errmp = sctp_make_err(sctp, SCTP_ERR_BAD_SID, 1263 (char *)inval_parm, sizeof (inval_parm)); 1264 SCTP_ACK_IT(sctp, tsn); 1265 if (errmp != NULL) 1266 sctp_send_err(sctp, errmp, NULL); 1267 return; 1268 } 1269 1270 ubit = SCTP_DATA_GET_UBIT(dc); 1271 ASSERT(sctp->sctp_instr != NULL); 1272 instr = &sctp->sctp_instr[ntohs(dc->sdh_sid)]; 1273 /* Initialize the stream, if not yet used */ 1274 if (instr->sctp == NULL) 1275 instr->sctp = sctp; 1276 /* 1277 * If we are getting low on buffers set trypartial to try 1278 * a partial delivery if we are reassembling a fragmented 1279 * message. Only do this if we can immediately deliver the 1280 * partially assembled message, and only partially deliver 1281 * one message at a time (i.e. messages cannot be intermixed 1282 * arriving at the upper layer). A simple way to enforce 1283 * this is to only try partial delivery if this TSN is 1284 * the next expected TSN. Partial Delivery not supported 1285 * for un-ordered message. 1286 */ 1287 isfrag = !(SCTP_DATA_GET_BBIT(dc) && SCTP_DATA_GET_EBIT(dc)); 1288 ssn = ntohs(dc->sdh_ssn); 1289 if ((sctp->sctp_rwnd - sctp->sctp_rxqueued < SCTP_RECV_LOWATER) && 1290 !ubit && isfrag && (tsn == sctp->sctp_ftsn)) { 1291 trypartial = 1; 1292 } 1293 1294 dmp = dupb(mp); 1295 if (dmp == NULL) { 1296 /* drop it and don't ack it, causing the peer to retransmit */ 1297 return; 1298 } 1299 dmp->b_wptr = (uchar_t *)ch + ntohs(ch->sch_len); 1300 1301 sctp->sctp_rxqueued += dlen; 1302 1303 oftsn = sctp->sctp_ftsn; 1304 1305 if (isfrag) { 1306 int error = 0; 1307 1308 /* fragmented data chunk */ 1309 dmp->b_rptr = (uchar_t *)dc; 1310 if (ubit) { 1311 dmp = sctp_uodata_frag(sctp, dmp, &dc); 1312 #if DEBUG 1313 if (dmp != NULL) { 1314 ASSERT(instr == 1315 &sctp->sctp_instr[ntohs(dc->sdh_sid)]); 1316 } 1317 #endif 1318 } else { 1319 dmp = sctp_data_frag(sctp, dmp, &dc, &error, instr, 1320 trypartial, &tpfinished); 1321 } 1322 if (error != 0) { 1323 sctp->sctp_rxqueued -= dlen; 1324 if (error == 1) { 1325 /* 1326 * out of memory; don't ack it so 1327 * the peer retransmits 1328 */ 1329 return; 1330 } else if (error == 2) { 1331 /* 1332 * fatal error (i.e. peer used different 1333 * ssn's for same fragmented data) -- 1334 * the association has been aborted. 1335 * XXX need to return errval so state 1336 * machine can also abort processing. 1337 */ 1338 dprint(0, ("error 2: must not happen!\n")); 1339 return; 1340 } 1341 } 1342 1343 if (dmp == NULL) { 1344 /* 1345 * Can't process this data now, but the cumulative 1346 * TSN may be advanced, so do the checks at done. 1347 */ 1348 SCTP_ACK_IT(sctp, tsn); 1349 goto done; 1350 } 1351 } 1352 1353 if (!ubit && !trypartial && ssn != instr->nextseq) { 1354 /* Adjust rptr to point at the data chunk for compares */ 1355 dmp->b_rptr = (uchar_t *)dc; 1356 1357 dprint(2, 1358 ("data_chunk: inserted %x in pq (ssn %d expected %d)\n", 1359 ntohl(dc->sdh_tsn), (int)(ssn), (int)(instr->nextseq))); 1360 1361 if (instr->istr_msgs == NULL) { 1362 instr->istr_msgs = dmp; 1363 ASSERT(dmp->b_prev == NULL && dmp->b_next == NULL); 1364 } else { 1365 mblk_t *imblk = instr->istr_msgs; 1366 sctp_data_hdr_t *idc; 1367 1368 /* 1369 * XXXNeed to take sequence wraps into account, 1370 * ... and a more efficient insertion algo. 1371 */ 1372 for (;;) { 1373 idc = (sctp_data_hdr_t *)imblk->b_rptr; 1374 if (SSN_GT(ntohs(idc->sdh_ssn), 1375 ntohs(dc->sdh_ssn))) { 1376 if (instr->istr_msgs == imblk) { 1377 instr->istr_msgs = dmp; 1378 dmp->b_next = imblk; 1379 imblk->b_prev = dmp; 1380 } else { 1381 ASSERT(imblk->b_prev != NULL); 1382 imblk->b_prev->b_next = dmp; 1383 dmp->b_prev = imblk->b_prev; 1384 imblk->b_prev = dmp; 1385 dmp->b_next = imblk; 1386 } 1387 break; 1388 } 1389 if (imblk->b_next == NULL) { 1390 imblk->b_next = dmp; 1391 dmp->b_prev = imblk; 1392 break; 1393 } 1394 imblk = imblk->b_next; 1395 } 1396 } 1397 (instr->istr_nmsgs)++; 1398 (sctp->sctp_istr_nmsgs)++; 1399 SCTP_ACK_IT(sctp, tsn); 1400 return; 1401 } 1402 1403 /* 1404 * Else we can deliver the data directly. Recalculate 1405 * dlen now since we may have reassembled data. 1406 */ 1407 dlen = dmp->b_wptr - (uchar_t *)dc - sizeof (*dc); 1408 for (pmp = dmp->b_cont; pmp != NULL; pmp = pmp->b_cont) 1409 dlen += pmp->b_wptr - pmp->b_rptr; 1410 ASSERT(sctp->sctp_rxqueued >= dlen); 1411 ASSERT(sctp->sctp_rwnd >= dlen); 1412 1413 /* Deliver the message. */ 1414 sctp->sctp_rxqueued -= dlen; 1415 1416 if (can_deliver) { 1417 dmp->b_rptr = (uchar_t *)(dc + 1); 1418 if (sctp_input_add_ancillary(sctp, &dmp, dc, fp, ipp) == 0) { 1419 dprint(1, ("sctp_data_chunk: delivering %lu bytes\n", 1420 msgdsize(dmp))); 1421 sctp->sctp_rwnd -= dlen; 1422 new_rwnd = sctp->sctp_ulp_recv(sctp->sctp_ulpd, dmp, 1423 tpfinished ? 0 : SCTP_PARTIAL_DATA); 1424 if (new_rwnd > sctp->sctp_rwnd) { 1425 sctp->sctp_rwnd = new_rwnd; 1426 } 1427 SCTP_ACK_IT(sctp, tsn); 1428 } else { 1429 /* Just free the message if we don't have memory. */ 1430 freemsg(dmp); 1431 return; 1432 } 1433 } else { 1434 /* About to free the data */ 1435 freemsg(dmp); 1436 SCTP_ACK_IT(sctp, tsn); 1437 } 1438 1439 /* 1440 * data, now enqueued, may already have been processed and free'd 1441 * by the ULP (or we may have just freed it above, if we could not 1442 * deliver it), so we must not reference it (this is why we kept 1443 * the ssn and ubit above). 1444 */ 1445 if (ubit != 0) { 1446 BUMP_LOCAL(sctp->sctp_iudchunks); 1447 goto done; 1448 } 1449 BUMP_LOCAL(sctp->sctp_idchunks); 1450 1451 /* 1452 * If there was a partial delivery and it has not finished, 1453 * don't pull anything from the pqueues. 1454 */ 1455 if (!tpfinished) { 1456 goto done; 1457 } 1458 1459 instr->nextseq = ssn + 1; 1460 /* Deliver any successive data chunks in the instr queue */ 1461 while (instr->istr_nmsgs > 0) { 1462 dmp = (mblk_t *)instr->istr_msgs; 1463 dc = (sctp_data_hdr_t *)dmp->b_rptr; 1464 ssn = ntohs(dc->sdh_ssn); 1465 /* Gap in the sequence */ 1466 if (ssn != instr->nextseq) 1467 break; 1468 1469 /* Else deliver the data */ 1470 (instr->istr_nmsgs)--; 1471 (instr->nextseq)++; 1472 (sctp->sctp_istr_nmsgs)--; 1473 1474 instr->istr_msgs = instr->istr_msgs->b_next; 1475 if (instr->istr_msgs != NULL) 1476 instr->istr_msgs->b_prev = NULL; 1477 dmp->b_next = dmp->b_prev = NULL; 1478 1479 dprint(2, ("data_chunk: pulling %x from pq (ssn %d)\n", 1480 ntohl(dc->sdh_tsn), (int)ssn)); 1481 1482 /* 1483 * If this chunk was reassembled, each b_cont represents 1484 * another TSN; advance ftsn now. 1485 */ 1486 dlen = dmp->b_wptr - dmp->b_rptr - sizeof (*dc); 1487 for (pmp = dmp->b_cont; pmp; pmp = pmp->b_cont) 1488 dlen += pmp->b_wptr - pmp->b_rptr; 1489 1490 ASSERT(sctp->sctp_rxqueued >= dlen); 1491 ASSERT(sctp->sctp_rwnd >= dlen); 1492 1493 sctp->sctp_rxqueued -= dlen; 1494 if (can_deliver) { 1495 dmp->b_rptr = (uchar_t *)(dc + 1); 1496 if (sctp_input_add_ancillary(sctp, &dmp, dc, fp, 1497 ipp) == 0) { 1498 dprint(1, ("sctp_data_chunk: delivering %lu " 1499 "bytes\n", msgdsize(dmp))); 1500 sctp->sctp_rwnd -= dlen; 1501 new_rwnd = sctp->sctp_ulp_recv(sctp->sctp_ulpd, 1502 dmp, tpfinished ? 0 : SCTP_PARTIAL_DATA); 1503 if (new_rwnd > sctp->sctp_rwnd) { 1504 sctp->sctp_rwnd = new_rwnd; 1505 } 1506 SCTP_ACK_IT(sctp, tsn); 1507 } else { 1508 freemsg(dmp); 1509 return; 1510 } 1511 } else { 1512 /* About to free the data */ 1513 freemsg(dmp); 1514 SCTP_ACK_IT(sctp, tsn); 1515 } 1516 } 1517 1518 done: 1519 1520 /* 1521 * If there are gap reports pending, check if advancing 1522 * the ftsn here closes a gap. If so, we can advance 1523 * ftsn to the end of the set. 1524 */ 1525 if (sctp->sctp_sack_info != NULL && 1526 sctp->sctp_ftsn == sctp->sctp_sack_info->begin) { 1527 sctp->sctp_ftsn = sctp->sctp_sack_info->end + 1; 1528 } 1529 /* 1530 * If ftsn has moved forward, maybe we can remove gap reports. 1531 * NB: dmp may now be NULL, so don't dereference it here. 1532 */ 1533 if (oftsn != sctp->sctp_ftsn && sctp->sctp_sack_info != NULL) { 1534 sctp_ack_rem(&sctp->sctp_sack_info, sctp->sctp_ftsn - 1, 1535 &sctp->sctp_sack_gaps); 1536 dprint(2, ("data_chunk: removed acks before %x (num=%d)\n", 1537 sctp->sctp_ftsn - 1, sctp->sctp_sack_gaps)); 1538 } 1539 1540 #ifdef DEBUG 1541 if (sctp->sctp_sack_info != NULL) { 1542 ASSERT(sctp->sctp_ftsn != sctp->sctp_sack_info->begin); 1543 } 1544 #endif 1545 1546 #undef SCTP_ACK_IT 1547 } 1548 1549 void 1550 sctp_fill_sack(sctp_t *sctp, unsigned char *dst, int sacklen) 1551 { 1552 sctp_chunk_hdr_t *sch; 1553 sctp_sack_chunk_t *sc; 1554 sctp_sack_frag_t *sf; 1555 uint16_t num_gaps = sctp->sctp_sack_gaps; 1556 sctp_set_t *sp; 1557 1558 /* Chunk hdr */ 1559 sch = (sctp_chunk_hdr_t *)dst; 1560 sch->sch_id = CHUNK_SACK; 1561 sch->sch_flags = 0; 1562 sch->sch_len = htons(sacklen); 1563 1564 /* SACK chunk */ 1565 sctp->sctp_lastacked = sctp->sctp_ftsn - 1; 1566 1567 sc = (sctp_sack_chunk_t *)(sch + 1); 1568 sc->ssc_cumtsn = htonl(sctp->sctp_lastacked); 1569 if (sctp->sctp_rxqueued < sctp->sctp_rwnd) { 1570 sc->ssc_a_rwnd = htonl(sctp->sctp_rwnd - sctp->sctp_rxqueued); 1571 } else { 1572 sc->ssc_a_rwnd = 0; 1573 } 1574 sc->ssc_numfrags = htons(num_gaps); 1575 sc->ssc_numdups = 0; 1576 1577 /* lay in gap reports */ 1578 sf = (sctp_sack_frag_t *)(sc + 1); 1579 for (sp = sctp->sctp_sack_info; sp; sp = sp->next) { 1580 uint16_t offset; 1581 1582 /* start */ 1583 if (sp->begin > sctp->sctp_lastacked) { 1584 offset = (uint16_t)(sp->begin - sctp->sctp_lastacked); 1585 } else { 1586 /* sequence number wrap */ 1587 offset = (uint16_t)(UINT32_MAX - sctp->sctp_lastacked + 1588 sp->begin); 1589 } 1590 sf->ssf_start = htons(offset); 1591 1592 /* end */ 1593 if (sp->end >= sp->begin) { 1594 offset += (uint16_t)(sp->end - sp->begin); 1595 } else { 1596 /* sequence number wrap */ 1597 offset += (uint16_t)(UINT32_MAX - sp->begin + sp->end); 1598 } 1599 sf->ssf_end = htons(offset); 1600 1601 sf++; 1602 /* This is just for debugging (a la the following assertion) */ 1603 num_gaps--; 1604 } 1605 1606 ASSERT(num_gaps == 0); 1607 1608 /* If the SACK timer is running, stop it */ 1609 if (sctp->sctp_ack_timer_running) { 1610 sctp_timer_stop(sctp->sctp_ack_mp); 1611 sctp->sctp_ack_timer_running = B_FALSE; 1612 } 1613 1614 BUMP_LOCAL(sctp->sctp_obchunks); 1615 } 1616 1617 mblk_t * 1618 sctp_make_sack(sctp_t *sctp, sctp_faddr_t *sendto, mblk_t *dups) 1619 { 1620 mblk_t *smp; 1621 size_t slen; 1622 sctp_chunk_hdr_t *sch; 1623 sctp_sack_chunk_t *sc; 1624 int32_t acks_max; 1625 1626 if (sctp->sctp_force_sack) { 1627 sctp->sctp_force_sack = 0; 1628 goto checks_done; 1629 } 1630 1631 acks_max = sctp_deferred_acks_max; 1632 if (sctp->sctp_state == SCTPS_ESTABLISHED) { 1633 if (sctp->sctp_sack_toggle < acks_max) { 1634 /* no need to SACK right now */ 1635 dprint(2, ("sctp_make_sack: %p no sack (toggle)\n", 1636 (void *)sctp)); 1637 return (NULL); 1638 } else if (sctp->sctp_sack_toggle >= acks_max) { 1639 sctp->sctp_sack_toggle = 0; 1640 } 1641 } 1642 1643 if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) { 1644 dprint(2, ("sctp_make_sack: %p no sack (already)\n", 1645 (void *)sctp)); 1646 return (NULL); 1647 } 1648 1649 checks_done: 1650 dprint(2, ("sctp_make_sack: acking %x\n", sctp->sctp_ftsn - 1)); 1651 1652 slen = sizeof (*sch) + sizeof (*sc) + 1653 (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps); 1654 smp = sctp_make_mp(sctp, sendto, slen); 1655 if (smp == NULL) { 1656 SCTP_KSTAT(sctp_send_sack_failed); 1657 return (NULL); 1658 } 1659 sch = (sctp_chunk_hdr_t *)smp->b_wptr; 1660 1661 sctp_fill_sack(sctp, smp->b_wptr, slen); 1662 smp->b_wptr += slen; 1663 if (dups) { 1664 sc = (sctp_sack_chunk_t *)(sch + 1); 1665 sc->ssc_numdups = htons((dups->b_wptr - dups->b_rptr) 1666 / sizeof (uint32_t)); 1667 sch->sch_len = htons(slen + (dups->b_wptr - dups->b_rptr)); 1668 smp->b_cont = dups; 1669 } 1670 1671 return (smp); 1672 } 1673 1674 void 1675 sctp_sack(sctp_t *sctp, mblk_t *dups) 1676 { 1677 mblk_t *smp; 1678 1679 /* If we are shutting down, let send_shutdown() bundle the SACK */ 1680 if (sctp->sctp_state == SCTPS_SHUTDOWN_SENT) { 1681 sctp_send_shutdown(sctp, 0); 1682 } 1683 1684 ASSERT(sctp->sctp_lastdata != NULL); 1685 1686 if ((smp = sctp_make_sack(sctp, sctp->sctp_lastdata, dups)) == NULL) { 1687 /* The caller of sctp_sack() will not free the dups mblk. */ 1688 if (dups != NULL) 1689 freeb(dups); 1690 return; 1691 } 1692 1693 sctp_set_iplen(sctp, smp); 1694 1695 dprint(2, ("sctp_sack: sending to %p %x:%x:%x:%x\n", 1696 (void *)sctp->sctp_lastdata, 1697 SCTP_PRINTADDR(sctp->sctp_lastdata->faddr))); 1698 1699 sctp->sctp_active = lbolt64; 1700 1701 BUMP_MIB(&sctp_mib, sctpOutAck); 1702 sctp_add_sendq(sctp, smp); 1703 } 1704 1705 /* 1706 * This is called if we have a message that was partially sent and is 1707 * abandoned. The cum TSN will be the last chunk sent for this message, 1708 * subsequent chunks will be marked ABANDONED. We send a Forward TSN 1709 * chunk in this case with the TSN of the last sent chunk so that the 1710 * peer can clean up its fragment list for this message. This message 1711 * will be removed from the transmit list when the peer sends a SACK 1712 * back. 1713 */ 1714 int 1715 sctp_check_abandoned_msg(sctp_t *sctp, mblk_t *meta) 1716 { 1717 sctp_data_hdr_t *dh; 1718 mblk_t *nmp; 1719 mblk_t *head; 1720 int32_t unsent = 0; 1721 mblk_t *mp1 = meta->b_cont; 1722 uint32_t adv_pap = sctp->sctp_adv_pap; 1723 sctp_faddr_t *fp = sctp->sctp_current; 1724 1725 dh = (sctp_data_hdr_t *)mp1->b_rptr; 1726 if (SEQ_GEQ(sctp->sctp_lastack_rxd, ntohl(dh->sdh_tsn))) { 1727 sctp_ftsn_set_t *sets = NULL; 1728 uint_t nsets = 0; 1729 uint32_t seglen = sizeof (uint32_t); 1730 boolean_t ubit = SCTP_DATA_GET_UBIT(dh); 1731 1732 while (mp1->b_next != NULL && SCTP_CHUNK_ISSENT(mp1->b_next)) 1733 mp1 = mp1->b_next; 1734 dh = (sctp_data_hdr_t *)mp1->b_rptr; 1735 sctp->sctp_adv_pap = ntohl(dh->sdh_tsn); 1736 if (!ubit && 1737 !sctp_add_ftsn_set(&sets, fp, meta, &nsets, &seglen)) { 1738 sctp->sctp_adv_pap = adv_pap; 1739 return (ENOMEM); 1740 } 1741 nmp = sctp_make_ftsn_chunk(sctp, fp, sets, nsets, seglen); 1742 sctp_free_ftsn_set(sets); 1743 if (nmp == NULL) { 1744 sctp->sctp_adv_pap = adv_pap; 1745 return (ENOMEM); 1746 } 1747 head = sctp_add_proto_hdr(sctp, fp, nmp, 0, NULL); 1748 if (head == NULL) { 1749 sctp->sctp_adv_pap = adv_pap; 1750 freemsg(nmp); 1751 SCTP_KSTAT(sctp_send_ftsn_failed); 1752 return (ENOMEM); 1753 } 1754 SCTP_MSG_SET_ABANDONED(meta); 1755 sctp_set_iplen(sctp, head); 1756 sctp_add_sendq(sctp, head); 1757 if (!fp->timer_running) 1758 SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto); 1759 mp1 = mp1->b_next; 1760 while (mp1 != NULL) { 1761 ASSERT(!SCTP_CHUNK_ISSENT(mp1)); 1762 ASSERT(!SCTP_CHUNK_ABANDONED(mp1)); 1763 SCTP_ABANDON_CHUNK(mp1); 1764 dh = (sctp_data_hdr_t *)mp1->b_rptr; 1765 unsent += ntohs(dh->sdh_len) - sizeof (*dh); 1766 mp1 = mp1->b_next; 1767 } 1768 ASSERT(sctp->sctp_unsent >= unsent); 1769 sctp->sctp_unsent -= unsent; 1770 /* 1771 * Update ULP the amount of queued data, which is 1772 * sent-unack'ed + unsent. 1773 */ 1774 if (!SCTP_IS_DETACHED(sctp)) { 1775 sctp->sctp_ulp_xmitted(sctp->sctp_ulpd, 1776 sctp->sctp_unacked + sctp->sctp_unsent); 1777 } 1778 return (0); 1779 } 1780 return (-1); 1781 } 1782 1783 uint32_t 1784 sctp_cumack(sctp_t *sctp, uint32_t tsn, mblk_t **first_unacked) 1785 { 1786 mblk_t *ump, *nump, *mp = NULL; 1787 uint16_t chunklen; 1788 uint32_t xtsn; 1789 sctp_faddr_t *fp; 1790 sctp_data_hdr_t *sdc; 1791 uint32_t cumack_forward = 0; 1792 sctp_msg_hdr_t *mhdr; 1793 1794 ump = sctp->sctp_xmit_head; 1795 1796 /* 1797 * Free messages only when they're completely acked. 1798 */ 1799 while (ump != NULL) { 1800 mhdr = (sctp_msg_hdr_t *)ump->b_rptr; 1801 for (mp = ump->b_cont; mp != NULL; mp = mp->b_next) { 1802 if (SCTP_CHUNK_ABANDONED(mp)) { 1803 ASSERT(SCTP_IS_MSG_ABANDONED(ump)); 1804 mp = NULL; 1805 break; 1806 } 1807 /* 1808 * We check for abandoned message if we are PR-SCTP 1809 * aware, if this is not the first chunk in the 1810 * message (b_cont) and if the message is marked 1811 * abandoned. 1812 */ 1813 if (!SCTP_CHUNK_ISSENT(mp)) { 1814 if (sctp->sctp_prsctp_aware && 1815 mp != ump->b_cont && 1816 (SCTP_IS_MSG_ABANDONED(ump) || 1817 SCTP_MSG_TO_BE_ABANDONED(ump, mhdr, 1818 sctp))) { 1819 (void) sctp_check_abandoned_msg(sctp, 1820 ump); 1821 } 1822 goto cum_ack_done; 1823 } 1824 sdc = (sctp_data_hdr_t *)mp->b_rptr; 1825 xtsn = ntohl(sdc->sdh_tsn); 1826 if (SEQ_GEQ(sctp->sctp_lastack_rxd, xtsn)) 1827 continue; 1828 if (SEQ_GEQ(tsn, xtsn)) { 1829 fp = SCTP_CHUNK_DEST(mp); 1830 chunklen = ntohs(sdc->sdh_len); 1831 1832 if (sctp->sctp_out_time != 0 && 1833 xtsn == sctp->sctp_rtt_tsn) { 1834 /* Got a new RTT measurement */ 1835 sctp_update_rtt(sctp, fp, 1836 lbolt64 - sctp->sctp_out_time); 1837 sctp->sctp_out_time = 0; 1838 } 1839 if (SCTP_CHUNK_ISACKED(mp)) 1840 continue; 1841 SCTP_CHUNK_SET_SACKCNT(mp, 0); 1842 SCTP_CHUNK_ACKED(mp); 1843 ASSERT(fp->suna >= chunklen); 1844 fp->suna -= chunklen; 1845 fp->acked += chunklen; 1846 cumack_forward += chunklen; 1847 ASSERT(sctp->sctp_unacked >= 1848 (chunklen - sizeof (*sdc))); 1849 sctp->sctp_unacked -= 1850 (chunklen - sizeof (*sdc)); 1851 if (fp->suna == 0) { 1852 /* all outstanding data acked */ 1853 fp->pba = 0; 1854 SCTP_FADDR_TIMER_STOP(fp); 1855 } else { 1856 SCTP_FADDR_TIMER_RESTART(sctp, fp, 1857 fp->rto); 1858 } 1859 } else { 1860 goto cum_ack_done; 1861 } 1862 } 1863 nump = ump->b_next; 1864 if (nump != NULL) 1865 nump->b_prev = NULL; 1866 if (ump == sctp->sctp_xmit_tail) 1867 sctp->sctp_xmit_tail = nump; 1868 if (SCTP_IS_MSG_ABANDONED(ump)) { 1869 BUMP_LOCAL(sctp->sctp_prsctpdrop); 1870 ump->b_next = NULL; 1871 sctp_sendfail_event(sctp, ump, 0, B_TRUE); 1872 } else { 1873 sctp_free_msg(ump); 1874 } 1875 sctp->sctp_xmit_head = ump = nump; 1876 } 1877 cum_ack_done: 1878 *first_unacked = mp; 1879 if (cumack_forward > 0) { 1880 BUMP_MIB(&sctp_mib, sctpInAck); 1881 if (SEQ_GT(sctp->sctp_lastack_rxd, sctp->sctp_recovery_tsn)) { 1882 sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd; 1883 } 1884 1885 /* 1886 * Update ULP the amount of queued data, which is 1887 * sent-unack'ed + unsent. 1888 */ 1889 if (!SCTP_IS_DETACHED(sctp)) { 1890 sctp->sctp_ulp_xmitted(sctp->sctp_ulpd, 1891 sctp->sctp_unacked + sctp->sctp_unsent); 1892 } 1893 1894 /* Time to send a shutdown? */ 1895 if (sctp->sctp_state == SCTPS_SHUTDOWN_PENDING) { 1896 sctp_send_shutdown(sctp, 0); 1897 } 1898 sctp->sctp_xmit_unacked = mp; 1899 } else { 1900 /* dup ack */ 1901 BUMP_MIB(&sctp_mib, sctpInDupAck); 1902 } 1903 sctp->sctp_lastack_rxd = tsn; 1904 if (SEQ_LT(sctp->sctp_adv_pap, sctp->sctp_lastack_rxd)) 1905 sctp->sctp_adv_pap = sctp->sctp_lastack_rxd; 1906 ASSERT(sctp->sctp_xmit_head || sctp->sctp_unacked == 0); 1907 1908 return (cumack_forward); 1909 } 1910 1911 static int 1912 sctp_set_frwnd(sctp_t *sctp, uint32_t frwnd) 1913 { 1914 uint32_t orwnd; 1915 1916 if (sctp->sctp_unacked > frwnd) { 1917 sctp->sctp_frwnd = 0; 1918 return (0); 1919 } 1920 orwnd = sctp->sctp_frwnd; 1921 sctp->sctp_frwnd = frwnd - sctp->sctp_unacked; 1922 if (orwnd < sctp->sctp_frwnd) { 1923 return (1); 1924 } else { 1925 return (0); 1926 } 1927 } 1928 1929 /* 1930 * For un-ordered messages. 1931 * Walk the sctp->sctp_uo_frag list and remove any fragments with TSN 1932 * less than/equal to ftsn. Fragments for un-ordered messages are 1933 * strictly in sequence (w.r.t TSN). 1934 */ 1935 static int 1936 sctp_ftsn_check_uo_frag(sctp_t *sctp, uint32_t ftsn) 1937 { 1938 mblk_t *hmp; 1939 mblk_t *hmp_next; 1940 sctp_data_hdr_t *dc; 1941 int dlen = 0; 1942 1943 hmp = sctp->sctp_uo_frags; 1944 while (hmp != NULL) { 1945 hmp_next = hmp->b_next; 1946 dc = (sctp_data_hdr_t *)hmp->b_rptr; 1947 if (SEQ_GT(ntohl(dc->sdh_tsn), ftsn)) 1948 return (dlen); 1949 sctp->sctp_uo_frags = hmp_next; 1950 if (hmp_next != NULL) 1951 hmp_next->b_prev = NULL; 1952 hmp->b_next = NULL; 1953 dlen += ntohs(dc->sdh_len) - sizeof (*dc); 1954 freeb(hmp); 1955 hmp = hmp_next; 1956 } 1957 return (dlen); 1958 } 1959 1960 /* 1961 * For ordered messages. 1962 * Check for existing fragments for an sid-ssn pair reported as abandoned, 1963 * hence will not receive, in the Forward TSN. If there are fragments, then 1964 * we just nuke them. If and when Partial Delivery API is supported, we 1965 * would need to send a notification to the upper layer about this. 1966 */ 1967 static int 1968 sctp_ftsn_check_frag(sctp_t *sctp, uint16_t ssn, sctp_instr_t *sip) 1969 { 1970 sctp_reass_t *srp; 1971 mblk_t *hmp; 1972 mblk_t *dmp; 1973 mblk_t *hmp_next; 1974 sctp_data_hdr_t *dc; 1975 int dlen = 0; 1976 1977 hmp = sip->istr_reass; 1978 while (hmp != NULL) { 1979 hmp_next = hmp->b_next; 1980 srp = (sctp_reass_t *)DB_BASE(hmp); 1981 if (SSN_GT(srp->ssn, ssn)) 1982 return (dlen); 1983 /* 1984 * If we had sent part of this message up, send a partial 1985 * delivery event. Since this is ordered delivery, we should 1986 * have sent partial message only for the next in sequence, 1987 * hence the ASSERT. See comments in sctp_data_chunk() for 1988 * trypartial. 1989 */ 1990 if (srp->partial_delivered) { 1991 ASSERT(sip->nextseq == srp->ssn); 1992 sctp_partial_delivery_event(sctp); 1993 } 1994 /* Take it out of the reass queue */ 1995 sip->istr_reass = hmp_next; 1996 if (hmp_next != NULL) 1997 hmp_next->b_prev = NULL; 1998 hmp->b_next = NULL; 1999 ASSERT(hmp->b_prev == NULL); 2000 dmp = hmp; 2001 if (DB_TYPE(hmp) == M_CTL) { 2002 dmp = hmp->b_cont; 2003 hmp->b_cont = NULL; 2004 freeb(hmp); 2005 hmp = dmp; 2006 } 2007 while (dmp != NULL) { 2008 dc = (sctp_data_hdr_t *)dmp->b_rptr; 2009 dlen += ntohs(dc->sdh_len) - sizeof (*dc); 2010 dmp = dmp->b_cont; 2011 } 2012 freemsg(hmp); 2013 hmp = hmp_next; 2014 } 2015 return (dlen); 2016 } 2017 2018 /* 2019 * Update sctp_ftsn to the cumulative TSN from the Forward TSN chunk. Remove 2020 * any SACK gaps less than the newly updated sctp_ftsn. Walk through the 2021 * sid-ssn pair in the Forward TSN and for each, clean the fragment list 2022 * for this pair, if needed, and check if we can deliver subsequent 2023 * messages, if any, from the instream queue (that were waiting for this 2024 * sid-ssn message to show up). Once we are done try to update the SACK 2025 * info. We could get a duplicate Forward TSN, in which case just send 2026 * a SACK. If any of the sid values in the the Forward TSN is invalid, 2027 * send back an "Invalid Stream Identifier" error and continue processing 2028 * the rest. 2029 */ 2030 static void 2031 sctp_process_forward_tsn(sctp_t *sctp, sctp_chunk_hdr_t *ch, sctp_faddr_t *fp, 2032 ip6_pkt_t *ipp) 2033 { 2034 uint32_t *ftsn = (uint32_t *)(ch + 1); 2035 ftsn_entry_t *ftsn_entry; 2036 sctp_instr_t *instr; 2037 boolean_t can_deliver = B_TRUE; 2038 size_t dlen; 2039 int flen; 2040 mblk_t *dmp; 2041 mblk_t *pmp; 2042 sctp_data_hdr_t *dc; 2043 ssize_t remaining; 2044 2045 *ftsn = ntohl(*ftsn); 2046 remaining = ntohs(ch->sch_len) - sizeof (*ch) - sizeof (*ftsn); 2047 2048 if (SCTP_IS_DETACHED(sctp)) { 2049 BUMP_MIB(&sctp_mib, sctpInClosed); 2050 can_deliver = B_FALSE; 2051 } 2052 /* 2053 * un-ordered messages don't have SID-SSN pair entries, we check 2054 * for any fragments (for un-ordered message) to be discarded using 2055 * the cumulative FTSN. 2056 */ 2057 flen = sctp_ftsn_check_uo_frag(sctp, *ftsn); 2058 if (flen > 0) { 2059 ASSERT(sctp->sctp_rxqueued >= flen); 2060 sctp->sctp_rxqueued -= flen; 2061 } 2062 ftsn_entry = (ftsn_entry_t *)(ftsn + 1); 2063 while (remaining >= sizeof (*ftsn_entry)) { 2064 ftsn_entry->ftsn_sid = ntohs(ftsn_entry->ftsn_sid); 2065 ftsn_entry->ftsn_ssn = ntohs(ftsn_entry->ftsn_ssn); 2066 if (ftsn_entry->ftsn_sid >= sctp->sctp_num_istr) { 2067 uint16_t inval_parm[2]; 2068 mblk_t *errmp; 2069 2070 inval_parm[0] = htons(ftsn_entry->ftsn_sid); 2071 /* RESERVED to be ignored at the receiving end */ 2072 inval_parm[1] = 0; 2073 errmp = sctp_make_err(sctp, SCTP_ERR_BAD_SID, 2074 (char *)inval_parm, sizeof (inval_parm)); 2075 if (errmp != NULL) 2076 sctp_send_err(sctp, errmp, NULL); 2077 ftsn_entry++; 2078 remaining -= sizeof (*ftsn_entry); 2079 continue; 2080 } 2081 instr = &sctp->sctp_instr[ftsn_entry->ftsn_sid]; 2082 flen = sctp_ftsn_check_frag(sctp, ftsn_entry->ftsn_ssn, instr); 2083 /* Indicates frags were nuked, update rxqueued */ 2084 if (flen > 0) { 2085 ASSERT(sctp->sctp_rxqueued >= flen); 2086 sctp->sctp_rxqueued -= flen; 2087 } 2088 /* 2089 * It is possible to receive an FTSN chunk with SSN smaller 2090 * than then nextseq if this chunk is a retransmission because 2091 * of incomplete processing when it was first processed. 2092 */ 2093 if (SSN_GE(ftsn_entry->ftsn_ssn, instr->nextseq)) 2094 instr->nextseq = ftsn_entry->ftsn_ssn + 1; 2095 while (instr->istr_nmsgs > 0) { 2096 mblk_t *next; 2097 2098 dmp = (mblk_t *)instr->istr_msgs; 2099 dc = (sctp_data_hdr_t *)dmp->b_rptr; 2100 if (ntohs(dc->sdh_ssn) != instr->nextseq) 2101 break; 2102 2103 next = dmp->b_next; 2104 dlen = dmp->b_wptr - dmp->b_rptr - sizeof (*dc); 2105 for (pmp = dmp->b_cont; pmp != NULL; 2106 pmp = pmp->b_cont) { 2107 dlen += pmp->b_wptr - pmp->b_rptr; 2108 } 2109 if (can_deliver) { 2110 int32_t nrwnd; 2111 2112 dmp->b_rptr = (uchar_t *)(dc + 1); 2113 dmp->b_next = NULL; 2114 ASSERT(dmp->b_prev == NULL); 2115 if (sctp_input_add_ancillary(sctp, 2116 &dmp, dc, fp, ipp) == 0) { 2117 sctp->sctp_rxqueued -= dlen; 2118 sctp->sctp_rwnd -= dlen; 2119 nrwnd = sctp->sctp_ulp_recv( 2120 sctp->sctp_ulpd, dmp, 0); 2121 if (nrwnd > sctp->sctp_rwnd) 2122 sctp->sctp_rwnd = nrwnd; 2123 } else { 2124 /* 2125 * We will resume processing when 2126 * the FTSN chunk is re-xmitted. 2127 */ 2128 dmp->b_rptr = (uchar_t *)dc; 2129 dmp->b_next = next; 2130 dprint(0, 2131 ("FTSN dequeuing %u failed\n", 2132 ntohs(dc->sdh_ssn))); 2133 return; 2134 } 2135 } else { 2136 sctp->sctp_rxqueued -= dlen; 2137 ASSERT(dmp->b_prev == NULL); 2138 dmp->b_next = NULL; 2139 freemsg(dmp); 2140 } 2141 instr->istr_nmsgs--; 2142 instr->nextseq++; 2143 sctp->sctp_istr_nmsgs--; 2144 if (next != NULL) 2145 next->b_prev = NULL; 2146 instr->istr_msgs = next; 2147 } 2148 ftsn_entry++; 2149 remaining -= sizeof (*ftsn_entry); 2150 } 2151 /* Duplicate FTSN */ 2152 if (*ftsn <= (sctp->sctp_ftsn - 1)) { 2153 sctp->sctp_force_sack = 1; 2154 return; 2155 } 2156 /* Advance cum TSN to that reported in the Forward TSN chunk */ 2157 sctp->sctp_ftsn = *ftsn + 1; 2158 2159 /* Remove all the SACK gaps before the new cum TSN */ 2160 if (sctp->sctp_sack_info != NULL) { 2161 sctp_ack_rem(&sctp->sctp_sack_info, sctp->sctp_ftsn - 1, 2162 &sctp->sctp_sack_gaps); 2163 } 2164 /* 2165 * If there are gap reports pending, check if advancing 2166 * the ftsn here closes a gap. If so, we can advance 2167 * ftsn to the end of the set. 2168 * If ftsn has moved forward, maybe we can remove gap reports. 2169 */ 2170 if (sctp->sctp_sack_info != NULL && 2171 sctp->sctp_ftsn == sctp->sctp_sack_info->begin) { 2172 sctp->sctp_ftsn = sctp->sctp_sack_info->end + 1; 2173 sctp_ack_rem(&sctp->sctp_sack_info, sctp->sctp_ftsn - 1, 2174 &sctp->sctp_sack_gaps); 2175 } 2176 } 2177 2178 /* 2179 * When we have processed a SACK we check to see if we can advance the 2180 * cumulative TSN if there are abandoned chunks immediately following 2181 * the updated cumulative TSN. If there are, we attempt to send a 2182 * Forward TSN chunk. 2183 */ 2184 static void 2185 sctp_check_abandoned_data(sctp_t *sctp, sctp_faddr_t *fp) 2186 { 2187 mblk_t *meta = sctp->sctp_xmit_head; 2188 mblk_t *mp; 2189 mblk_t *nmp; 2190 uint32_t seglen; 2191 uint32_t adv_pap = sctp->sctp_adv_pap; 2192 2193 /* 2194 * We only check in the first meta since otherwise we can't 2195 * advance the cumulative ack point. We just look for chunks 2196 * marked for retransmission, else we might prematurely 2197 * send an FTSN for a sent, but unacked, chunk. 2198 */ 2199 for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) { 2200 if (!SCTP_CHUNK_ISSENT(mp)) 2201 return; 2202 if (SCTP_CHUNK_WANT_REXMIT(mp)) 2203 break; 2204 } 2205 if (mp == NULL) 2206 return; 2207 sctp_check_adv_ack_pt(sctp, meta, mp); 2208 if (SEQ_GT(sctp->sctp_adv_pap, adv_pap)) { 2209 sctp_make_ftsns(sctp, meta, mp, &nmp, fp, &seglen); 2210 if (nmp == NULL) { 2211 sctp->sctp_adv_pap = adv_pap; 2212 if (!fp->timer_running) 2213 SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto); 2214 return; 2215 } 2216 sctp_set_iplen(sctp, nmp); 2217 sctp_add_sendq(sctp, nmp); 2218 if (!fp->timer_running) 2219 SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto); 2220 } 2221 } 2222 2223 /* 2224 * The processing here follows the same logic in sctp_got_sack(), the reason 2225 * we do this separately is because, usually, gap blocks are ordered and 2226 * we can process it in sctp_got_sack(). However if they aren't we would 2227 * need to do some additional non-optimal stuff when we start processing the 2228 * unordered gaps. To that effect sctp_got_sack() does the processing in the 2229 * simple case and this does the same in the more involved case. 2230 */ 2231 static uint32_t 2232 sctp_process_uo_gaps(sctp_t *sctp, uint32_t ctsn, sctp_sack_frag_t *ssf, 2233 int num_gaps, mblk_t *umphead, mblk_t *mphead, int *trysend, 2234 boolean_t *fast_recovery, uint32_t fr_xtsn) 2235 { 2236 uint32_t xtsn; 2237 uint32_t gapstart = 0; 2238 uint32_t gapend = 0; 2239 int gapcnt; 2240 uint16_t chunklen; 2241 sctp_data_hdr_t *sdc; 2242 int gstart; 2243 mblk_t *ump = umphead; 2244 mblk_t *mp = mphead; 2245 sctp_faddr_t *fp; 2246 uint32_t acked = 0; 2247 2248 /* 2249 * gstart tracks the last (in the order of TSN) gapstart that 2250 * we process in this SACK gaps walk. 2251 */ 2252 gstart = ctsn; 2253 2254 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2255 xtsn = ntohl(sdc->sdh_tsn); 2256 for (gapcnt = 0; gapcnt < num_gaps; gapcnt++, ssf++) { 2257 if (gapstart != 0) { 2258 /* 2259 * If we have reached the end of the transmit list or 2260 * hit an unsent chunk or encountered an unordered gap 2261 * block start from the ctsn again. 2262 */ 2263 if (ump == NULL || !SCTP_CHUNK_ISSENT(mp) || 2264 SEQ_LT(ctsn + ntohs(ssf->ssf_start), xtsn)) { 2265 ump = umphead; 2266 mp = mphead; 2267 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2268 xtsn = ntohl(sdc->sdh_tsn); 2269 } 2270 } 2271 2272 gapstart = ctsn + ntohs(ssf->ssf_start); 2273 gapend = ctsn + ntohs(ssf->ssf_end); 2274 2275 /* SACK for TSN we have not sent - ABORT */ 2276 if (SEQ_GT(gapstart, sctp->sctp_ltsn - 1) || 2277 SEQ_GT(gapend, sctp->sctp_ltsn - 1)) { 2278 BUMP_MIB(&sctp_mib, sctpInAckUnsent); 2279 *trysend = -1; 2280 return (acked); 2281 } else if (SEQ_LT(gapend, gapstart)) { 2282 break; 2283 } 2284 /* 2285 * The xtsn can be the TSN processed for the last gap 2286 * (gapend) or it could be the cumulative TSN. We continue 2287 * with the last xtsn as long as the gaps are ordered, when 2288 * we hit an unordered gap, we re-start from the cumulative 2289 * TSN. For the first gap it is always the cumulative TSN. 2290 */ 2291 while (xtsn != gapstart) { 2292 /* 2293 * We can't reliably check for reneged chunks 2294 * when walking the unordered list, so we don't. 2295 * In case the peer reneges then we will end up 2296 * sending the reneged chunk via timeout. 2297 */ 2298 mp = mp->b_next; 2299 if (mp == NULL) { 2300 ump = ump->b_next; 2301 /* 2302 * ump can't be NULL because of the sanity 2303 * check above. 2304 */ 2305 ASSERT(ump != NULL); 2306 mp = ump->b_cont; 2307 } 2308 /* 2309 * mp can't be unsent because of the sanity check 2310 * above. 2311 */ 2312 ASSERT(SCTP_CHUNK_ISSENT(mp)); 2313 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2314 xtsn = ntohl(sdc->sdh_tsn); 2315 } 2316 /* 2317 * Now that we have found the chunk with TSN == 'gapstart', 2318 * let's walk till we hit the chunk with TSN == 'gapend'. 2319 * All intermediate chunks will be marked ACKED, if they 2320 * haven't already been. 2321 */ 2322 while (SEQ_LEQ(xtsn, gapend)) { 2323 /* 2324 * SACKed 2325 */ 2326 SCTP_CHUNK_SET_SACKCNT(mp, 0); 2327 if (!SCTP_CHUNK_ISACKED(mp)) { 2328 SCTP_CHUNK_ACKED(mp); 2329 2330 fp = SCTP_CHUNK_DEST(mp); 2331 chunklen = ntohs(sdc->sdh_len); 2332 ASSERT(fp->suna >= chunklen); 2333 fp->suna -= chunklen; 2334 if (fp->suna == 0) { 2335 /* All outstanding data acked. */ 2336 fp->pba = 0; 2337 SCTP_FADDR_TIMER_STOP(fp); 2338 } 2339 fp->acked += chunklen; 2340 acked += chunklen; 2341 sctp->sctp_unacked -= chunklen - sizeof (*sdc); 2342 ASSERT(sctp->sctp_unacked >= 0); 2343 } 2344 /* 2345 * Move to the next message in the transmit list 2346 * if we are done with all the chunks from the current 2347 * message. Note, it is possible to hit the end of the 2348 * transmit list here, i.e. if we have already completed 2349 * processing the gap block. 2350 */ 2351 mp = mp->b_next; 2352 if (mp == NULL) { 2353 ump = ump->b_next; 2354 if (ump == NULL) { 2355 ASSERT(xtsn == gapend); 2356 break; 2357 } 2358 mp = ump->b_cont; 2359 } 2360 /* 2361 * Likewise, we can hit an unsent chunk once we have 2362 * completed processing the gap block. 2363 */ 2364 if (!SCTP_CHUNK_ISSENT(mp)) { 2365 ASSERT(xtsn == gapend); 2366 break; 2367 } 2368 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2369 xtsn = ntohl(sdc->sdh_tsn); 2370 } 2371 /* 2372 * We keep track of the last gap we successfully processed 2373 * so that we can terminate the walk below for incrementing 2374 * the SACK count. 2375 */ 2376 if (SEQ_LT(gstart, gapstart)) 2377 gstart = gapstart; 2378 } 2379 /* 2380 * Check if have incremented the SACK count for all unacked TSNs in 2381 * sctp_got_sack(), if so we are done. 2382 */ 2383 if (SEQ_LEQ(gstart, fr_xtsn)) 2384 return (acked); 2385 2386 ump = umphead; 2387 mp = mphead; 2388 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2389 xtsn = ntohl(sdc->sdh_tsn); 2390 while (SEQ_LT(xtsn, gstart)) { 2391 /* 2392 * We have incremented SACK count for TSNs less than fr_tsn 2393 * in sctp_got_sack(), so don't increment them again here. 2394 */ 2395 if (SEQ_GT(xtsn, fr_xtsn) && !SCTP_CHUNK_ISACKED(mp)) { 2396 SCTP_CHUNK_SET_SACKCNT(mp, SCTP_CHUNK_SACKCNT(mp) + 1); 2397 if (SCTP_CHUNK_SACKCNT(mp) == sctp_fast_rxt_thresh) { 2398 SCTP_CHUNK_REXMIT(mp); 2399 sctp->sctp_chk_fast_rexmit = B_TRUE; 2400 *trysend = 1; 2401 if (!*fast_recovery) { 2402 /* 2403 * Entering fast recovery. 2404 */ 2405 fp = SCTP_CHUNK_DEST(mp); 2406 fp->ssthresh = fp->cwnd / 2; 2407 if (fp->ssthresh < 2 * fp->sfa_pmss) { 2408 fp->ssthresh = 2409 2 * fp->sfa_pmss; 2410 } 2411 fp->cwnd = fp->ssthresh; 2412 fp->pba = 0; 2413 sctp->sctp_recovery_tsn = 2414 sctp->sctp_ltsn - 1; 2415 *fast_recovery = B_TRUE; 2416 } 2417 } 2418 } 2419 mp = mp->b_next; 2420 if (mp == NULL) { 2421 ump = ump->b_next; 2422 /* We can't get to the end of the transmit list here */ 2423 ASSERT(ump != NULL); 2424 mp = ump->b_cont; 2425 } 2426 /* We can't hit an unsent chunk here */ 2427 ASSERT(SCTP_CHUNK_ISSENT(mp)); 2428 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2429 xtsn = ntohl(sdc->sdh_tsn); 2430 } 2431 return (acked); 2432 } 2433 2434 static int 2435 sctp_got_sack(sctp_t *sctp, sctp_chunk_hdr_t *sch) 2436 { 2437 sctp_sack_chunk_t *sc; 2438 sctp_data_hdr_t *sdc; 2439 sctp_sack_frag_t *ssf; 2440 mblk_t *ump; 2441 mblk_t *mp; 2442 mblk_t *mp1; 2443 uint32_t cumtsn; 2444 uint32_t xtsn; 2445 uint32_t gapstart = 0; 2446 uint32_t gapend = 0; 2447 uint32_t acked = 0; 2448 uint16_t chunklen; 2449 sctp_faddr_t *fp; 2450 int num_gaps; 2451 int trysend = 0; 2452 int i; 2453 boolean_t fast_recovery = B_FALSE; 2454 boolean_t cumack_forward = B_FALSE; 2455 boolean_t fwd_tsn = B_FALSE; 2456 2457 BUMP_LOCAL(sctp->sctp_ibchunks); 2458 chunklen = ntohs(sch->sch_len); 2459 if (chunklen < (sizeof (*sch) + sizeof (*sc))) 2460 return (0); 2461 2462 sc = (sctp_sack_chunk_t *)(sch + 1); 2463 cumtsn = ntohl(sc->ssc_cumtsn); 2464 2465 dprint(2, ("got sack cumtsn %x -> %x\n", sctp->sctp_lastack_rxd, 2466 cumtsn)); 2467 2468 /* out of order */ 2469 if (SEQ_LT(cumtsn, sctp->sctp_lastack_rxd)) 2470 return (0); 2471 2472 if (SEQ_GT(cumtsn, sctp->sctp_ltsn - 1)) { 2473 BUMP_MIB(&sctp_mib, sctpInAckUnsent); 2474 /* Send an ABORT */ 2475 return (-1); 2476 } 2477 2478 /* 2479 * Cwnd only done when not in fast recovery mode. 2480 */ 2481 if (SEQ_LT(sctp->sctp_lastack_rxd, sctp->sctp_recovery_tsn)) 2482 fast_recovery = B_TRUE; 2483 2484 /* 2485 * .. and if the cum TSN is not moving ahead on account Forward TSN 2486 */ 2487 if (SEQ_LT(sctp->sctp_lastack_rxd, sctp->sctp_adv_pap)) 2488 fwd_tsn = B_TRUE; 2489 2490 if (cumtsn == sctp->sctp_lastack_rxd && 2491 (sctp->sctp_xmit_unacked == NULL || 2492 !SCTP_CHUNK_ABANDONED(sctp->sctp_xmit_unacked))) { 2493 if (sctp->sctp_xmit_unacked != NULL) 2494 mp = sctp->sctp_xmit_unacked; 2495 else if (sctp->sctp_xmit_head != NULL) 2496 mp = sctp->sctp_xmit_head->b_cont; 2497 else 2498 mp = NULL; 2499 BUMP_MIB(&sctp_mib, sctpInDupAck); 2500 /* 2501 * If we were doing a zero win probe and the win 2502 * has now opened to at least MSS, re-transmit the 2503 * zero win probe via sctp_rexmit_packet(). 2504 */ 2505 if (mp != NULL && sctp->sctp_zero_win_probe && 2506 ntohl(sc->ssc_a_rwnd) >= sctp->sctp_current->sfa_pmss) { 2507 mblk_t *pkt; 2508 uint_t pkt_len; 2509 mblk_t *mp1 = mp; 2510 mblk_t *meta = sctp->sctp_xmit_head; 2511 2512 /* 2513 * Reset the RTO since we have been backing-off 2514 * to send the ZWP. 2515 */ 2516 fp = sctp->sctp_current; 2517 fp->rto = fp->srtt + 4 * fp->rttvar; 2518 /* Resend the ZWP */ 2519 pkt = sctp_rexmit_packet(sctp, &meta, &mp1, fp, 2520 &pkt_len); 2521 if (pkt == NULL) { 2522 SCTP_KSTAT(sctp_ss_rexmit_failed); 2523 return (0); 2524 } 2525 ASSERT(pkt_len <= fp->sfa_pmss); 2526 sctp->sctp_zero_win_probe = B_FALSE; 2527 sctp->sctp_rxt_nxttsn = sctp->sctp_ltsn; 2528 sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn; 2529 sctp_set_iplen(sctp, pkt); 2530 sctp_add_sendq(sctp, pkt); 2531 } 2532 } else { 2533 if (sctp->sctp_zero_win_probe) { 2534 /* 2535 * Reset the RTO since we have been backing-off 2536 * to send the ZWP. 2537 */ 2538 fp = sctp->sctp_current; 2539 fp->rto = fp->srtt + 4 * fp->rttvar; 2540 sctp->sctp_zero_win_probe = B_FALSE; 2541 /* This is probably not required */ 2542 if (!sctp->sctp_rexmitting) { 2543 sctp->sctp_rxt_nxttsn = sctp->sctp_ltsn; 2544 sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn; 2545 } 2546 } 2547 acked = sctp_cumack(sctp, cumtsn, &mp); 2548 sctp->sctp_xmit_unacked = mp; 2549 if (acked > 0) { 2550 trysend = 1; 2551 cumack_forward = B_TRUE; 2552 if (fwd_tsn && SEQ_GEQ(sctp->sctp_lastack_rxd, 2553 sctp->sctp_adv_pap)) { 2554 cumack_forward = B_FALSE; 2555 } 2556 } 2557 } 2558 num_gaps = ntohs(sc->ssc_numfrags); 2559 if (num_gaps == 0 || mp == NULL || !SCTP_CHUNK_ISSENT(mp) || 2560 chunklen < (sizeof (*sch) + sizeof (*sc) + 2561 num_gaps * sizeof (*ssf))) { 2562 goto ret; 2563 } 2564 #ifdef DEBUG 2565 /* 2566 * Since we delete any message that has been acked completely, 2567 * the unacked chunk must belong to sctp_xmit_head (as 2568 * we don't have a back pointer from the mp to the meta data 2569 * we do this). 2570 */ 2571 { 2572 mblk_t *mp2 = sctp->sctp_xmit_head->b_cont; 2573 2574 while (mp2 != NULL) { 2575 if (mp2 == mp) 2576 break; 2577 mp2 = mp2->b_next; 2578 } 2579 ASSERT(mp2 != NULL); 2580 } 2581 #endif 2582 ump = sctp->sctp_xmit_head; 2583 2584 /* 2585 * Just remember where we started from, in case we need to call 2586 * sctp_process_uo_gaps() if the gap blocks are unordered. 2587 */ 2588 mp1 = mp; 2589 2590 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2591 xtsn = ntohl(sdc->sdh_tsn); 2592 ASSERT(xtsn == cumtsn + 1); 2593 2594 /* 2595 * Go through SACK gaps. They are ordered based on start TSN. 2596 */ 2597 ssf = (sctp_sack_frag_t *)(sc + 1); 2598 for (i = 0; i < num_gaps; i++, ssf++) { 2599 if (gapstart != 0) { 2600 /* check for unordered gap */ 2601 if (SEQ_LEQ(cumtsn + ntohs(ssf->ssf_start), gapstart)) { 2602 acked += sctp_process_uo_gaps(sctp, 2603 cumtsn, ssf, num_gaps - i, 2604 sctp->sctp_xmit_head, mp1, 2605 &trysend, &fast_recovery, gapstart); 2606 if (trysend < 0) { 2607 BUMP_MIB(&sctp_mib, sctpInAckUnsent); 2608 return (-1); 2609 } 2610 break; 2611 } 2612 } 2613 gapstart = cumtsn + ntohs(ssf->ssf_start); 2614 gapend = cumtsn + ntohs(ssf->ssf_end); 2615 2616 /* SACK for TSN we have not sent - ABORT */ 2617 if (SEQ_GT(gapstart, sctp->sctp_ltsn - 1) || 2618 SEQ_GT(gapend, sctp->sctp_ltsn - 1)) { 2619 BUMP_MIB(&sctp_mib, sctpInAckUnsent); 2620 return (-1); 2621 } else if (SEQ_LT(gapend, gapstart)) { 2622 break; 2623 } 2624 /* 2625 * Let's start at the current TSN (for the 1st gap we start 2626 * from the cumulative TSN, for subsequent ones we start from 2627 * where the previous gapend was found - second while loop 2628 * below) and walk the transmit list till we find the TSN 2629 * corresponding to gapstart. All the unacked chunks till we 2630 * get to the chunk with TSN == gapstart will have their 2631 * SACKCNT incremented by 1. Note since the gap blocks are 2632 * ordered, we won't be incrementing the SACKCNT for an 2633 * unacked chunk by more than one while processing the gap 2634 * blocks. If the SACKCNT for any unacked chunk exceeds 2635 * the fast retransmit threshold, we will fast retransmit 2636 * after processing all the gap blocks. 2637 */ 2638 ASSERT(SEQ_LT(xtsn, gapstart)); 2639 while (xtsn != gapstart) { 2640 SCTP_CHUNK_SET_SACKCNT(mp, SCTP_CHUNK_SACKCNT(mp) + 1); 2641 if (SCTP_CHUNK_SACKCNT(mp) == sctp_fast_rxt_thresh) { 2642 SCTP_CHUNK_REXMIT(mp); 2643 sctp->sctp_chk_fast_rexmit = B_TRUE; 2644 trysend = 1; 2645 if (!fast_recovery) { 2646 /* 2647 * Entering fast recovery. 2648 */ 2649 fp = SCTP_CHUNK_DEST(mp); 2650 fp->ssthresh = fp->cwnd / 2; 2651 if (fp->ssthresh < 2 * fp->sfa_pmss) { 2652 fp->ssthresh = 2653 2 * fp->sfa_pmss; 2654 } 2655 fp->cwnd = fp->ssthresh; 2656 fp->pba = 0; 2657 sctp->sctp_recovery_tsn = 2658 sctp->sctp_ltsn - 1; 2659 fast_recovery = B_TRUE; 2660 } 2661 } 2662 2663 /* 2664 * Peer may have reneged on this chunk, so un-sack 2665 * it now. If the peer did renege, we need to 2666 * readjust unacked. 2667 */ 2668 if (SCTP_CHUNK_ISACKED(mp)) { 2669 chunklen = ntohs(sdc->sdh_len); 2670 fp = SCTP_CHUNK_DEST(mp); 2671 fp->suna += chunklen; 2672 sctp->sctp_unacked += chunklen - sizeof (*sdc); 2673 SCTP_CHUNK_CLEAR_ACKED(mp); 2674 if (!fp->timer_running) { 2675 SCTP_FADDR_TIMER_RESTART(sctp, fp, 2676 fp->rto); 2677 } 2678 } 2679 2680 mp = mp->b_next; 2681 if (mp == NULL) { 2682 ump = ump->b_next; 2683 /* 2684 * ump can't be NULL given the sanity check 2685 * above. 2686 */ 2687 ASSERT(ump != NULL); 2688 mp = ump->b_cont; 2689 } 2690 /* 2691 * mp can't be unsent given the sanity check above. 2692 */ 2693 ASSERT(SCTP_CHUNK_ISSENT(mp)); 2694 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2695 xtsn = ntohl(sdc->sdh_tsn); 2696 } 2697 /* 2698 * Now that we have found the chunk with TSN == 'gapstart', 2699 * let's walk till we hit the chunk with TSN == 'gapend'. 2700 * All intermediate chunks will be marked ACKED, if they 2701 * haven't already been. 2702 */ 2703 while (SEQ_LEQ(xtsn, gapend)) { 2704 /* 2705 * SACKed 2706 */ 2707 SCTP_CHUNK_SET_SACKCNT(mp, 0); 2708 if (!SCTP_CHUNK_ISACKED(mp)) { 2709 SCTP_CHUNK_ACKED(mp); 2710 2711 fp = SCTP_CHUNK_DEST(mp); 2712 chunklen = ntohs(sdc->sdh_len); 2713 ASSERT(fp->suna >= chunklen); 2714 fp->suna -= chunklen; 2715 if (fp->suna == 0) { 2716 /* All outstanding data acked. */ 2717 fp->pba = 0; 2718 SCTP_FADDR_TIMER_STOP(fp); 2719 } 2720 fp->acked += chunklen; 2721 acked += chunklen; 2722 sctp->sctp_unacked -= chunklen - sizeof (*sdc); 2723 ASSERT(sctp->sctp_unacked >= 0); 2724 } 2725 /* Go to the next chunk of the current message */ 2726 mp = mp->b_next; 2727 /* 2728 * Move to the next message in the transmit list 2729 * if we are done with all the chunks from the current 2730 * message. Note, it is possible to hit the end of the 2731 * transmit list here, i.e. if we have already completed 2732 * processing the gap block. 2733 * Also, note that we break here, which means we 2734 * continue processing gap blocks, if any. In case of 2735 * ordered gap blocks there can't be any following 2736 * this (if there is it will fail the sanity check 2737 * above). In case of un-ordered gap blocks we will 2738 * switch to sctp_process_uo_gaps(). In either case 2739 * it should be fine to continue with NULL ump/mp, 2740 * but we just reset it to xmit_head. 2741 */ 2742 if (mp == NULL) { 2743 ump = ump->b_next; 2744 if (ump == NULL) { 2745 ASSERT(xtsn == gapend); 2746 ump = sctp->sctp_xmit_head; 2747 mp = mp1; 2748 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2749 xtsn = ntohl(sdc->sdh_tsn); 2750 break; 2751 } 2752 mp = ump->b_cont; 2753 } 2754 /* 2755 * Likewise, we could hit an unsent chunk once we have 2756 * completed processing the gap block. Again, it is 2757 * fine to continue processing gap blocks with mp 2758 * pointing to the unsent chunk, because if there 2759 * are more ordered gap blocks, they will fail the 2760 * sanity check, and if there are un-ordered gap blocks, 2761 * we will continue processing in sctp_process_uo_gaps() 2762 * We just reset the mp to the one we started with. 2763 */ 2764 if (!SCTP_CHUNK_ISSENT(mp)) { 2765 ASSERT(xtsn == gapend); 2766 ump = sctp->sctp_xmit_head; 2767 mp = mp1; 2768 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2769 xtsn = ntohl(sdc->sdh_tsn); 2770 break; 2771 } 2772 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2773 xtsn = ntohl(sdc->sdh_tsn); 2774 } 2775 } 2776 if (sctp->sctp_prsctp_aware) 2777 sctp_check_abandoned_data(sctp, sctp->sctp_current); 2778 if (sctp->sctp_chk_fast_rexmit) 2779 sctp_fast_rexmit(sctp); 2780 ret: 2781 trysend += sctp_set_frwnd(sctp, ntohl(sc->ssc_a_rwnd)); 2782 2783 /* 2784 * If receive window is closed while there is unsent data, 2785 * set a timer for doing zero window probes. 2786 */ 2787 if (sctp->sctp_frwnd == 0 && sctp->sctp_unacked == 0 && 2788 sctp->sctp_unsent != 0) { 2789 SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current, 2790 sctp->sctp_current->rto); 2791 } 2792 2793 /* 2794 * Set cwnd for all destinations. 2795 * Congestion window gets increased only when cumulative 2796 * TSN moves forward, we're not in fast recovery, and 2797 * cwnd has been fully utilized (almost fully, need to allow 2798 * some leeway due to non-MSS sized messages). 2799 */ 2800 if (sctp->sctp_current->acked == acked) { 2801 /* 2802 * Fast-path, only data sent to sctp_current got acked. 2803 */ 2804 fp = sctp->sctp_current; 2805 if (cumack_forward && !fast_recovery && 2806 (fp->acked + fp->suna > fp->cwnd - fp->sfa_pmss)) { 2807 if (fp->cwnd < fp->ssthresh) { 2808 /* 2809 * Slow start 2810 */ 2811 if (fp->acked > fp->sfa_pmss) { 2812 fp->cwnd += fp->sfa_pmss; 2813 } else { 2814 fp->cwnd += fp->acked; 2815 } 2816 fp->cwnd = MIN(fp->cwnd, sctp->sctp_cwnd_max); 2817 } else { 2818 /* 2819 * Congestion avoidance 2820 */ 2821 fp->pba += fp->acked; 2822 if (fp->pba >= fp->cwnd) { 2823 fp->pba -= fp->cwnd; 2824 fp->cwnd += fp->sfa_pmss; 2825 fp->cwnd = MIN(fp->cwnd, 2826 sctp->sctp_cwnd_max); 2827 } 2828 } 2829 } 2830 /* 2831 * Limit the burst of transmitted data segments. 2832 */ 2833 if (fp->suna + sctp_maxburst * fp->sfa_pmss < fp->cwnd) { 2834 fp->cwnd = fp->suna + sctp_maxburst * fp->sfa_pmss; 2835 } 2836 fp->acked = 0; 2837 goto check_ss_rxmit; 2838 } 2839 for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) { 2840 if (cumack_forward && fp->acked && !fast_recovery && 2841 (fp->acked + fp->suna > fp->cwnd - fp->sfa_pmss)) { 2842 if (fp->cwnd < fp->ssthresh) { 2843 if (fp->acked > fp->sfa_pmss) { 2844 fp->cwnd += fp->sfa_pmss; 2845 } else { 2846 fp->cwnd += fp->acked; 2847 } 2848 fp->cwnd = MIN(fp->cwnd, sctp->sctp_cwnd_max); 2849 } else { 2850 fp->pba += fp->acked; 2851 if (fp->pba >= fp->cwnd) { 2852 fp->pba -= fp->cwnd; 2853 fp->cwnd += fp->sfa_pmss; 2854 fp->cwnd = MIN(fp->cwnd, 2855 sctp->sctp_cwnd_max); 2856 } 2857 } 2858 } 2859 if (fp->suna + sctp_maxburst * fp->sfa_pmss < fp->cwnd) { 2860 fp->cwnd = fp->suna + sctp_maxburst * fp->sfa_pmss; 2861 } 2862 fp->acked = 0; 2863 } 2864 check_ss_rxmit: 2865 /* 2866 * If this is a SACK following a timeout, check if there are 2867 * still unacked chunks (sent before the timeout) that we can 2868 * send. 2869 */ 2870 if (sctp->sctp_rexmitting) { 2871 if (SEQ_LT(sctp->sctp_lastack_rxd, sctp->sctp_rxt_maxtsn)) { 2872 /* 2873 * As we are in retransmission phase, we may get a 2874 * SACK which indicates some new chunks are received 2875 * but cum_tsn does not advance. During this 2876 * phase, the other side advances cum_tsn only because 2877 * it receives our retransmitted chunks. Only 2878 * this signals that some chunks are still 2879 * missing. 2880 */ 2881 if (cumack_forward) 2882 sctp_ss_rexmit(sctp); 2883 } else { 2884 sctp->sctp_rexmitting = B_FALSE; 2885 sctp->sctp_rxt_nxttsn = sctp->sctp_ltsn; 2886 sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn; 2887 } 2888 } 2889 return (trysend); 2890 } 2891 2892 /* 2893 * Returns 0 if the caller should stop processing any more chunks, 2894 * 1 if the caller should skip this chunk and continue processing. 2895 */ 2896 static int 2897 sctp_strange_chunk(sctp_t *sctp, sctp_chunk_hdr_t *ch, sctp_faddr_t *fp) 2898 { 2899 mblk_t *errmp; 2900 size_t len; 2901 2902 BUMP_LOCAL(sctp->sctp_ibchunks); 2903 /* check top two bits for action required */ 2904 if (ch->sch_id & 0x40) { /* also matches 0xc0 */ 2905 len = ntohs(ch->sch_len); 2906 errmp = sctp_make_err(sctp, SCTP_ERR_UNREC_CHUNK, ch, len); 2907 if (errmp != NULL) 2908 sctp_send_err(sctp, errmp, fp); 2909 if ((ch->sch_id & 0xc0) == 0xc0) { 2910 /* skip and continue */ 2911 return (1); 2912 } else { 2913 /* stop processing */ 2914 return (0); 2915 } 2916 } 2917 if (ch->sch_id & 0x80) { 2918 /* skip and continue, no error */ 2919 return (1); 2920 } 2921 /* top two bits are clear; stop processing and no error */ 2922 return (0); 2923 } 2924 2925 /* 2926 * Basic sanity checks on all input chunks and parameters: they must 2927 * be of legitimate size for their purported type, and must follow 2928 * ordering conventions as defined in rfc2960. 2929 * 2930 * Returns 1 if the chunk and all encloded params are legitimate, 2931 * 0 otherwise. 2932 */ 2933 /*ARGSUSED*/ 2934 static int 2935 sctp_check_input(sctp_t *sctp, sctp_chunk_hdr_t *ch, ssize_t len, int first) 2936 { 2937 sctp_parm_hdr_t *ph; 2938 void *p = NULL; 2939 ssize_t clen; 2940 uint16_t ch_len; 2941 2942 ch_len = ntohs(ch->sch_len); 2943 if (ch_len > len) { 2944 return (0); 2945 } 2946 2947 switch (ch->sch_id) { 2948 case CHUNK_DATA: 2949 if (ch_len < sizeof (sctp_data_hdr_t)) { 2950 return (0); 2951 } 2952 return (1); 2953 case CHUNK_INIT: 2954 case CHUNK_INIT_ACK: 2955 { 2956 ssize_t remlen = len; 2957 2958 /* 2959 * INIT and INIT-ACK chunks must not be bundled with 2960 * any other. 2961 */ 2962 if (!first || sctp_next_chunk(ch, &remlen) != NULL || 2963 (ch_len < (sizeof (*ch) + 2964 sizeof (sctp_init_chunk_t)))) { 2965 return (0); 2966 } 2967 /* may have params that need checking */ 2968 p = (char *)(ch + 1) + sizeof (sctp_init_chunk_t); 2969 clen = ch_len - (sizeof (*ch) + 2970 sizeof (sctp_init_chunk_t)); 2971 } 2972 break; 2973 case CHUNK_SACK: 2974 if (ch_len < (sizeof (*ch) + sizeof (sctp_sack_chunk_t))) { 2975 return (0); 2976 } 2977 /* dup and gap reports checked by got_sack() */ 2978 return (1); 2979 case CHUNK_SHUTDOWN: 2980 if (ch_len < (sizeof (*ch) + sizeof (uint32_t))) { 2981 return (0); 2982 } 2983 return (1); 2984 case CHUNK_ABORT: 2985 case CHUNK_ERROR: 2986 if (ch_len < sizeof (*ch)) { 2987 return (0); 2988 } 2989 /* may have params that need checking */ 2990 p = ch + 1; 2991 clen = ch_len - sizeof (*ch); 2992 break; 2993 case CHUNK_ECNE: 2994 case CHUNK_CWR: 2995 case CHUNK_HEARTBEAT: 2996 case CHUNK_HEARTBEAT_ACK: 2997 /* Full ASCONF chunk and parameter checks are in asconf.c */ 2998 case CHUNK_ASCONF: 2999 case CHUNK_ASCONF_ACK: 3000 if (ch_len < sizeof (*ch)) { 3001 return (0); 3002 } 3003 /* heartbeat data checked by process_heartbeat() */ 3004 return (1); 3005 case CHUNK_SHUTDOWN_COMPLETE: 3006 { 3007 ssize_t remlen = len; 3008 3009 /* 3010 * SHUTDOWN-COMPLETE chunk must not be bundled with any 3011 * other 3012 */ 3013 if (!first || sctp_next_chunk(ch, &remlen) != NULL || 3014 ch_len < sizeof (*ch)) { 3015 return (0); 3016 } 3017 } 3018 return (1); 3019 case CHUNK_COOKIE: 3020 case CHUNK_COOKIE_ACK: 3021 case CHUNK_SHUTDOWN_ACK: 3022 if (ch_len < sizeof (*ch) || !first) { 3023 return (0); 3024 } 3025 return (1); 3026 case CHUNK_FORWARD_TSN: 3027 if (ch_len < (sizeof (*ch) + sizeof (uint32_t))) 3028 return (0); 3029 return (1); 3030 default: 3031 return (1); /* handled by strange_chunk() */ 3032 } 3033 3034 /* check and byteorder parameters */ 3035 if (clen <= 0) { 3036 return (1); 3037 } 3038 ASSERT(p != NULL); 3039 3040 ph = p; 3041 while (ph != NULL && clen > 0) { 3042 ch_len = ntohs(ph->sph_len); 3043 if (ch_len > len || ch_len < sizeof (*ph)) { 3044 return (0); 3045 } 3046 ph = sctp_next_parm(ph, &clen); 3047 } 3048 3049 /* All OK */ 3050 return (1); 3051 } 3052 3053 /* ARGSUSED */ 3054 static sctp_hdr_t * 3055 find_sctp_hdrs(mblk_t *mp, in6_addr_t *src, in6_addr_t *dst, 3056 uint_t *ifindex, uint_t *ip_hdr_len, ip6_pkt_t *ipp, ip_pktinfo_t *pinfo) 3057 { 3058 uchar_t *rptr; 3059 ipha_t *ip4h; 3060 ip6_t *ip6h; 3061 mblk_t *mp1; 3062 3063 rptr = mp->b_rptr; 3064 if (IPH_HDR_VERSION(rptr) == IPV4_VERSION) { 3065 *ip_hdr_len = IPH_HDR_LENGTH(rptr); 3066 ip4h = (ipha_t *)rptr; 3067 IN6_IPADDR_TO_V4MAPPED(ip4h->ipha_src, src); 3068 IN6_IPADDR_TO_V4MAPPED(ip4h->ipha_dst, dst); 3069 3070 ipp->ipp_fields |= IPPF_HOPLIMIT; 3071 ipp->ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 3072 if (pinfo != NULL && (pinfo->ip_pkt_flags & IPF_RECVIF)) { 3073 ipp->ipp_fields |= IPPF_IFINDEX; 3074 ipp->ipp_ifindex = pinfo->ip_pkt_ifindex; 3075 } 3076 } else { 3077 ASSERT(IPH_HDR_VERSION(rptr) == IPV6_VERSION); 3078 ip6h = (ip6_t *)rptr; 3079 ipp->ipp_fields = IPPF_HOPLIMIT; 3080 ipp->ipp_hoplimit = ip6h->ip6_hops; 3081 3082 if (ip6h->ip6_nxt != IPPROTO_SCTP) { 3083 /* Look for ifindex information */ 3084 if (ip6h->ip6_nxt == IPPROTO_RAW) { 3085 ip6i_t *ip6i = (ip6i_t *)ip6h; 3086 3087 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 3088 ASSERT(ip6i->ip6i_ifindex != 0); 3089 ipp->ipp_fields |= IPPF_IFINDEX; 3090 ipp->ipp_ifindex = ip6i->ip6i_ifindex; 3091 } 3092 rptr = (uchar_t *)&ip6i[1]; 3093 mp->b_rptr = rptr; 3094 if (rptr == mp->b_wptr) { 3095 mp1 = mp->b_cont; 3096 freeb(mp); 3097 mp = mp1; 3098 rptr = mp->b_rptr; 3099 } 3100 ASSERT(mp->b_wptr - rptr >= 3101 IPV6_HDR_LEN + sizeof (sctp_hdr_t)); 3102 ip6h = (ip6_t *)rptr; 3103 } 3104 /* 3105 * Find any potentially interesting extension headers 3106 * as well as the length of the IPv6 + extension 3107 * headers. 3108 */ 3109 *ip_hdr_len = ip_find_hdr_v6(mp, ip6h, ipp, NULL); 3110 } else { 3111 *ip_hdr_len = IPV6_HDR_LEN; 3112 } 3113 *src = ip6h->ip6_src; 3114 *dst = ip6h->ip6_dst; 3115 } 3116 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 3117 return ((sctp_hdr_t *)&rptr[*ip_hdr_len]); 3118 #undef IPVER 3119 } 3120 3121 static mblk_t * 3122 sctp_check_in_policy(mblk_t *mp, mblk_t *ipsec_mp) 3123 { 3124 ipsec_in_t *ii; 3125 boolean_t check = B_TRUE; 3126 boolean_t policy_present; 3127 ipha_t *ipha; 3128 ip6_t *ip6h; 3129 3130 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 3131 ASSERT(ii->ipsec_in_type == IPSEC_IN); 3132 if (ii->ipsec_in_dont_check) { 3133 check = B_FALSE; 3134 if (!ii->ipsec_in_secure) { 3135 freeb(ipsec_mp); 3136 ipsec_mp = NULL; 3137 } 3138 } 3139 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 3140 policy_present = ipsec_inbound_v4_policy_present; 3141 ipha = (ipha_t *)mp->b_rptr; 3142 ip6h = NULL; 3143 } else { 3144 policy_present = ipsec_inbound_v6_policy_present; 3145 ipha = NULL; 3146 ip6h = (ip6_t *)mp->b_rptr; 3147 } 3148 3149 if (check && policy_present) { 3150 /* 3151 * The conn_t parameter is NULL because we already know 3152 * nobody's home. 3153 */ 3154 ipsec_mp = ipsec_check_global_policy(ipsec_mp, (conn_t *)NULL, 3155 ipha, ip6h, B_TRUE); 3156 if (ipsec_mp == NULL) 3157 return (NULL); 3158 } 3159 if (ipsec_mp != NULL) 3160 freeb(ipsec_mp); 3161 return (mp); 3162 } 3163 3164 /* Handle out-of-the-blue packets */ 3165 void 3166 sctp_ootb_input(mblk_t *mp, ill_t *recv_ill, uint_t ipif_seqid, 3167 zoneid_t zoneid, boolean_t mctl_present) 3168 { 3169 sctp_t *sctp; 3170 sctp_chunk_hdr_t *ch; 3171 sctp_hdr_t *sctph; 3172 in6_addr_t src, dst; 3173 uint_t ip_hdr_len; 3174 uint_t ifindex; 3175 ip6_pkt_t ipp; 3176 ssize_t mlen; 3177 ip_pktinfo_t *pinfo = NULL; 3178 mblk_t *first_mp; 3179 3180 BUMP_MIB(&sctp_mib, sctpOutOfBlue); 3181 BUMP_MIB(&sctp_mib, sctpInSCTPPkts); 3182 3183 first_mp = mp; 3184 if (mctl_present) 3185 mp = mp->b_cont; 3186 3187 /* Initiate IPPf processing, if needed. */ 3188 if (IPP_ENABLED(IPP_LOCAL_IN)) { 3189 ip_process(IPP_LOCAL_IN, &mp, 3190 recv_ill->ill_phyint->phyint_ifindex); 3191 if (mp == NULL) { 3192 if (mctl_present) 3193 freeb(first_mp); 3194 return; 3195 } 3196 } 3197 3198 if (mp->b_cont != NULL) { 3199 /* 3200 * All subsequent code is vastly simplified if it can 3201 * assume a single contiguous chunk of data. 3202 */ 3203 if (pullupmsg(mp, -1) == 0) { 3204 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 3205 freemsg(first_mp); 3206 return; 3207 } 3208 } 3209 3210 /* 3211 * We don't really need to call this function... Need to 3212 * optimize later. 3213 */ 3214 sctph = find_sctp_hdrs(mp, &src, &dst, &ifindex, &ip_hdr_len, 3215 &ipp, pinfo); 3216 mlen = mp->b_wptr - (uchar_t *)(sctph + 1); 3217 if ((ch = sctp_first_chunk((uchar_t *)(sctph + 1), mlen)) == NULL) { 3218 dprint(3, ("sctp_ootb_input: invalid packet\n")); 3219 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 3220 freemsg(first_mp); 3221 return; 3222 } 3223 3224 switch (ch->sch_id) { 3225 case CHUNK_INIT: 3226 /* no listener; send abort */ 3227 if (mctl_present && sctp_check_in_policy(mp, first_mp) == NULL) 3228 return; 3229 sctp_send_abort(gsctp, sctp_init2vtag(ch), 0, 3230 NULL, 0, mp, 0, B_TRUE); 3231 break; 3232 case CHUNK_INIT_ACK: 3233 /* check for changed src addr */ 3234 sctp = sctp_addrlist2sctp(mp, sctph, ch, ipif_seqid, zoneid); 3235 if (sctp != NULL) { 3236 /* success; proceed to normal path */ 3237 mutex_enter(&sctp->sctp_lock); 3238 if (sctp->sctp_running) { 3239 if (!sctp_add_recvq(sctp, mp, B_FALSE)) { 3240 BUMP_MIB(recv_ill->ill_ip_mib, 3241 ipIfStatsInDiscards); 3242 freemsg(mp); 3243 } 3244 mutex_exit(&sctp->sctp_lock); 3245 } else { 3246 /* 3247 * If the source address is changed, we 3248 * don't need to worry too much about 3249 * out of order processing. So we don't 3250 * check if the recvq is empty or not here. 3251 */ 3252 sctp->sctp_running = B_TRUE; 3253 mutex_exit(&sctp->sctp_lock); 3254 sctp_input_data(sctp, mp, NULL); 3255 WAKE_SCTP(sctp); 3256 sctp_process_sendq(sctp); 3257 } 3258 SCTP_REFRELE(sctp); 3259 return; 3260 } 3261 if (mctl_present) 3262 freeb(first_mp); 3263 /* else bogus init ack; drop it */ 3264 break; 3265 case CHUNK_SHUTDOWN_ACK: 3266 if (mctl_present && sctp_check_in_policy(mp, first_mp) == NULL) 3267 return; 3268 sctp_ootb_shutdown_ack(gsctp, mp, ip_hdr_len); 3269 sctp_process_sendq(gsctp); 3270 return; 3271 case CHUNK_ERROR: 3272 case CHUNK_ABORT: 3273 case CHUNK_COOKIE_ACK: 3274 case CHUNK_SHUTDOWN_COMPLETE: 3275 if (mctl_present) 3276 freeb(first_mp); 3277 break; 3278 default: 3279 if (mctl_present && sctp_check_in_policy(mp, first_mp) == NULL) 3280 return; 3281 sctp_send_abort(gsctp, sctph->sh_verf, 0, NULL, 0, mp, 0, 3282 B_TRUE); 3283 break; 3284 } 3285 sctp_process_sendq(gsctp); 3286 freemsg(mp); 3287 } 3288 3289 void 3290 sctp_input(conn_t *connp, ipha_t *ipha, mblk_t *mp, mblk_t *first_mp, 3291 ill_t *recv_ill, boolean_t isv4, boolean_t mctl_present) 3292 { 3293 sctp_t *sctp = CONN2SCTP(connp); 3294 3295 /* 3296 * We check some fields in conn_t without holding a lock. 3297 * This should be fine. 3298 */ 3299 if (CONN_INBOUND_POLICY_PRESENT(connp) || mctl_present) { 3300 first_mp = ipsec_check_inbound_policy(first_mp, connp, 3301 ipha, NULL, mctl_present); 3302 if (first_mp == NULL) { 3303 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 3304 SCTP_REFRELE(sctp); 3305 return; 3306 } 3307 } 3308 3309 /* Initiate IPPF processing for fastpath */ 3310 if (IPP_ENABLED(IPP_LOCAL_IN)) { 3311 ip_process(IPP_LOCAL_IN, &mp, 3312 recv_ill->ill_phyint->phyint_ifindex); 3313 if (mp == NULL) { 3314 SCTP_REFRELE(sctp); 3315 if (mctl_present) 3316 freeb(first_mp); 3317 return; 3318 } else if (mctl_present) { 3319 /* 3320 * ip_process might return a new mp. 3321 */ 3322 ASSERT(first_mp != mp); 3323 first_mp->b_cont = mp; 3324 } else { 3325 first_mp = mp; 3326 } 3327 } 3328 3329 if (connp->conn_recvif || connp->conn_recvslla || 3330 connp->conn_ip_recvpktinfo) { 3331 int in_flags = 0; 3332 3333 if (connp->conn_recvif || connp->conn_ip_recvpktinfo) { 3334 in_flags = IPF_RECVIF; 3335 } 3336 if (connp->conn_recvslla) { 3337 in_flags |= IPF_RECVSLLA; 3338 } 3339 if (isv4) { 3340 mp = ip_add_info(mp, recv_ill, in_flags, 3341 IPCL_ZONEID(connp)); 3342 } else { 3343 mp = ip_add_info_v6(mp, recv_ill, 3344 &(((ip6_t *)ipha)->ip6_dst)); 3345 } 3346 if (mp == NULL) { 3347 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 3348 SCTP_REFRELE(sctp); 3349 if (mctl_present) 3350 freeb(first_mp); 3351 return; 3352 } else if (mctl_present) { 3353 /* 3354 * ip_add_info might return a new mp. 3355 */ 3356 ASSERT(first_mp != mp); 3357 first_mp->b_cont = mp; 3358 } else { 3359 first_mp = mp; 3360 } 3361 } 3362 3363 mutex_enter(&sctp->sctp_lock); 3364 if (sctp->sctp_running) { 3365 if (mctl_present) 3366 mp->b_prev = first_mp; 3367 if (!sctp_add_recvq(sctp, mp, B_FALSE)) { 3368 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 3369 freemsg(first_mp); 3370 } 3371 mutex_exit(&sctp->sctp_lock); 3372 SCTP_REFRELE(sctp); 3373 return; 3374 } else { 3375 sctp->sctp_running = B_TRUE; 3376 mutex_exit(&sctp->sctp_lock); 3377 3378 mutex_enter(&sctp->sctp_recvq_lock); 3379 if (sctp->sctp_recvq != NULL) { 3380 if (mctl_present) 3381 mp->b_prev = first_mp; 3382 if (!sctp_add_recvq(sctp, mp, B_TRUE)) { 3383 BUMP_MIB(recv_ill->ill_ip_mib, 3384 ipIfStatsInDiscards); 3385 freemsg(first_mp); 3386 } 3387 mutex_exit(&sctp->sctp_recvq_lock); 3388 WAKE_SCTP(sctp); 3389 SCTP_REFRELE(sctp); 3390 return; 3391 } 3392 } 3393 mutex_exit(&sctp->sctp_recvq_lock); 3394 sctp_input_data(sctp, mp, (mctl_present ? first_mp : NULL)); 3395 WAKE_SCTP(sctp); 3396 sctp_process_sendq(sctp); 3397 SCTP_REFRELE(sctp); 3398 } 3399 3400 static void 3401 sctp_process_abort(sctp_t *sctp, sctp_chunk_hdr_t *ch, int err) 3402 { 3403 BUMP_MIB(&sctp_mib, sctpAborted); 3404 BUMP_LOCAL(sctp->sctp_ibchunks); 3405 3406 sctp_assoc_event(sctp, SCTP_COMM_LOST, 3407 ntohs(((sctp_parm_hdr_t *)(ch + 1))->sph_type), ch); 3408 sctp_clean_death(sctp, err); 3409 } 3410 3411 void 3412 sctp_input_data(sctp_t *sctp, mblk_t *mp, mblk_t *ipsec_mp) 3413 { 3414 sctp_chunk_hdr_t *ch; 3415 ssize_t mlen; 3416 int gotdata; 3417 int trysend; 3418 sctp_faddr_t *fp; 3419 sctp_init_chunk_t *iack; 3420 uint32_t tsn; 3421 sctp_data_hdr_t *sdc; 3422 ip6_pkt_t ipp; 3423 in6_addr_t src; 3424 in6_addr_t dst; 3425 uint_t ifindex; 3426 sctp_hdr_t *sctph; 3427 uint_t ip_hdr_len; 3428 mblk_t *dups = NULL; 3429 int recv_adaption; 3430 boolean_t wake_eager = B_FALSE; 3431 mblk_t *pinfo_mp; 3432 ip_pktinfo_t *pinfo = NULL; 3433 in6_addr_t peer_src; 3434 int64_t now; 3435 3436 if (DB_TYPE(mp) != M_DATA) { 3437 ASSERT(DB_TYPE(mp) == M_CTL); 3438 if (MBLKL(mp) == sizeof (ip_pktinfo_t) && 3439 ((ip_pktinfo_t *)mp->b_rptr)->ip_pkt_ulp_type == 3440 IN_PKTINFO) { 3441 pinfo = (ip_pktinfo_t *)mp->b_rptr; 3442 pinfo_mp = mp; 3443 mp = mp->b_cont; 3444 } else { 3445 if (ipsec_mp != NULL) 3446 freeb(ipsec_mp); 3447 sctp_icmp_error(sctp, mp); 3448 return; 3449 } 3450 } 3451 ASSERT(DB_TYPE(mp) == M_DATA); 3452 3453 if (mp->b_cont != NULL) { 3454 /* 3455 * All subsequent code is vastly simplified if it can 3456 * assume a single contiguous chunk of data. 3457 */ 3458 if (pullupmsg(mp, -1) == 0) { 3459 BUMP_MIB(&ip_mib, ipIfStatsInDiscards); 3460 if (ipsec_mp != NULL) 3461 freeb(ipsec_mp); 3462 if (pinfo != NULL) 3463 freeb(pinfo_mp); 3464 freemsg(mp); 3465 return; 3466 } 3467 } 3468 3469 BUMP_LOCAL(sctp->sctp_ipkts); 3470 sctph = find_sctp_hdrs(mp, &src, &dst, &ifindex, &ip_hdr_len, 3471 &ipp, pinfo); 3472 if (pinfo != NULL) 3473 freeb(pinfo_mp); 3474 mlen = mp->b_wptr - (uchar_t *)(sctph + 1); 3475 ch = sctp_first_chunk((uchar_t *)(sctph + 1), mlen); 3476 if (ch == NULL) { 3477 BUMP_MIB(&ip_mib, ipIfStatsInDiscards); 3478 if (ipsec_mp != NULL) 3479 freeb(ipsec_mp); 3480 freemsg(mp); 3481 return; 3482 } 3483 3484 if (!sctp_check_input(sctp, ch, mlen, 1)) { 3485 BUMP_MIB(&ip_mib, ipIfStatsInDiscards); 3486 goto done; 3487 } 3488 /* 3489 * Check verfication tag (special handling for INIT, 3490 * COOKIE, SHUTDOWN_COMPLETE and SHUTDOWN_ACK chunks). 3491 * ABORTs are handled in the chunk processing loop, since 3492 * may not appear first. All other checked chunks must 3493 * appear first, or will have been dropped by check_input(). 3494 */ 3495 switch (ch->sch_id) { 3496 case CHUNK_INIT: 3497 if (sctph->sh_verf != 0) { 3498 /* drop it */ 3499 goto done; 3500 } 3501 break; 3502 case CHUNK_SHUTDOWN_COMPLETE: 3503 if (sctph->sh_verf == sctp->sctp_lvtag) 3504 break; 3505 if (sctph->sh_verf == sctp->sctp_fvtag && 3506 SCTP_GET_TBIT(ch)) { 3507 break; 3508 } 3509 /* else drop it */ 3510 goto done; 3511 case CHUNK_ABORT: 3512 case CHUNK_COOKIE: 3513 /* handled below */ 3514 break; 3515 case CHUNK_SHUTDOWN_ACK: 3516 if (sctp->sctp_state > SCTPS_BOUND && 3517 sctp->sctp_state < SCTPS_ESTABLISHED) { 3518 /* treat as OOTB */ 3519 sctp_ootb_shutdown_ack(sctp, mp, ip_hdr_len); 3520 if (ipsec_mp != NULL) 3521 freeb(ipsec_mp); 3522 return; 3523 } 3524 /* else fallthru */ 3525 default: 3526 /* 3527 * All other packets must have a valid 3528 * verification tag, however if this is a 3529 * listener, we use a refined version of 3530 * out-of-the-blue logic. 3531 */ 3532 if (sctph->sh_verf != sctp->sctp_lvtag && 3533 sctp->sctp_state != SCTPS_LISTEN) { 3534 /* drop it */ 3535 goto done; 3536 } 3537 break; 3538 } 3539 3540 /* Have a valid sctp for this packet */ 3541 fp = sctp_lookup_faddr(sctp, &src); 3542 dprint(2, ("sctp_dispatch_rput: mp=%p fp=%p sctp=%p\n", (void *)mp, 3543 (void *)fp, (void *)sctp)); 3544 3545 gotdata = 0; 3546 trysend = 0; 3547 3548 now = lbolt64; 3549 /* Process the chunks */ 3550 do { 3551 dprint(3, ("sctp_dispatch_rput: state=%d, chunk id=%d\n", 3552 sctp->sctp_state, (int)(ch->sch_id))); 3553 3554 if (ch->sch_id == CHUNK_ABORT) { 3555 if (sctph->sh_verf != sctp->sctp_lvtag && 3556 sctph->sh_verf != sctp->sctp_fvtag) { 3557 /* drop it */ 3558 goto done; 3559 } 3560 } 3561 3562 switch (sctp->sctp_state) { 3563 3564 case SCTPS_ESTABLISHED: 3565 case SCTPS_SHUTDOWN_PENDING: 3566 case SCTPS_SHUTDOWN_SENT: 3567 switch (ch->sch_id) { 3568 case CHUNK_DATA: 3569 /* 0-length data chunks are not allowed */ 3570 if (ntohs(ch->sch_len) == sizeof (*sdc)) { 3571 sdc = (sctp_data_hdr_t *)ch; 3572 tsn = sdc->sdh_tsn; 3573 sctp_send_abort(sctp, sctp->sctp_fvtag, 3574 SCTP_ERR_NO_USR_DATA, (char *)&tsn, 3575 sizeof (tsn), mp, 0, B_FALSE); 3576 sctp_assoc_event(sctp, SCTP_COMM_LOST, 3577 0, NULL); 3578 sctp_clean_death(sctp, ECONNABORTED); 3579 goto done; 3580 } 3581 3582 ASSERT(fp != NULL); 3583 sctp->sctp_lastdata = fp; 3584 sctp_data_chunk(sctp, ch, mp, &dups, fp, &ipp); 3585 gotdata = 1; 3586 /* Restart shutdown timer if shutting down */ 3587 if (sctp->sctp_state == SCTPS_SHUTDOWN_SENT) { 3588 /* 3589 * If we have exceeded our max 3590 * wait bound for waiting for a 3591 * shutdown ack from the peer, 3592 * abort the association. 3593 */ 3594 if (sctp_shutack_wait_bound != 0 && 3595 TICK_TO_MSEC(now - 3596 sctp->sctp_out_time) > 3597 sctp_shutack_wait_bound) { 3598 sctp_send_abort(sctp, 3599 sctp->sctp_fvtag, 0, NULL, 3600 0, mp, 0, B_FALSE); 3601 sctp_assoc_event(sctp, 3602 SCTP_COMM_LOST, 0, NULL); 3603 sctp_clean_death(sctp, 3604 ECONNABORTED); 3605 goto done; 3606 } 3607 SCTP_FADDR_TIMER_RESTART(sctp, fp, 3608 fp->rto); 3609 } 3610 break; 3611 case CHUNK_SACK: 3612 ASSERT(fp != NULL); 3613 /* 3614 * Peer is real and alive if it can ack our 3615 * data. 3616 */ 3617 sctp_faddr_alive(sctp, fp); 3618 trysend = sctp_got_sack(sctp, ch); 3619 if (trysend < 0) { 3620 sctp_send_abort(sctp, sctph->sh_verf, 3621 0, NULL, 0, mp, 0, B_FALSE); 3622 sctp_assoc_event(sctp, 3623 SCTP_COMM_LOST, 0, NULL); 3624 sctp_clean_death(sctp, 3625 ECONNABORTED); 3626 goto done; 3627 } 3628 break; 3629 case CHUNK_HEARTBEAT: 3630 sctp_return_heartbeat(sctp, ch, mp); 3631 break; 3632 case CHUNK_HEARTBEAT_ACK: 3633 sctp_process_heartbeat(sctp, ch); 3634 break; 3635 case CHUNK_SHUTDOWN: 3636 sctp_shutdown_event(sctp); 3637 trysend = sctp_shutdown_received(sctp, ch, 3638 B_FALSE, B_FALSE, fp); 3639 BUMP_LOCAL(sctp->sctp_ibchunks); 3640 break; 3641 case CHUNK_SHUTDOWN_ACK: 3642 BUMP_LOCAL(sctp->sctp_ibchunks); 3643 if (sctp->sctp_state == SCTPS_SHUTDOWN_SENT) { 3644 sctp_shutdown_complete(sctp); 3645 BUMP_MIB(&sctp_mib, sctpShutdowns); 3646 sctp_assoc_event(sctp, 3647 SCTP_SHUTDOWN_COMP, 0, NULL); 3648 sctp_clean_death(sctp, 0); 3649 goto done; 3650 } 3651 break; 3652 case CHUNK_ABORT: { 3653 sctp_saddr_ipif_t *sp; 3654 3655 /* Ignore if delete pending */ 3656 sp = sctp_saddr_lookup(sctp, &dst, 0); 3657 ASSERT(sp != NULL); 3658 if (sp->saddr_ipif_delete_pending) { 3659 BUMP_LOCAL(sctp->sctp_ibchunks); 3660 break; 3661 } 3662 3663 sctp_process_abort(sctp, ch, ECONNRESET); 3664 goto done; 3665 } 3666 case CHUNK_INIT: 3667 sctp_send_initack(sctp, sctph, ch, mp); 3668 break; 3669 case CHUNK_COOKIE: 3670 if (sctp_process_cookie(sctp, ch, mp, &iack, 3671 sctph, &recv_adaption, NULL) != -1) { 3672 sctp_send_cookie_ack(sctp); 3673 sctp_assoc_event(sctp, SCTP_RESTART, 3674 0, NULL); 3675 if (recv_adaption) { 3676 sctp->sctp_recv_adaption = 1; 3677 sctp_adaption_event(sctp); 3678 } 3679 } else { 3680 BUMP_MIB(&sctp_mib, 3681 sctpInInvalidCookie); 3682 } 3683 break; 3684 case CHUNK_ERROR: { 3685 int error; 3686 3687 BUMP_LOCAL(sctp->sctp_ibchunks); 3688 error = sctp_handle_error(sctp, sctph, ch, mp); 3689 if (error != 0) { 3690 sctp_assoc_event(sctp, SCTP_COMM_LOST, 3691 0, NULL); 3692 sctp_clean_death(sctp, error); 3693 goto done; 3694 } 3695 break; 3696 } 3697 case CHUNK_ASCONF: 3698 ASSERT(fp != NULL); 3699 sctp_input_asconf(sctp, ch, fp); 3700 BUMP_LOCAL(sctp->sctp_ibchunks); 3701 break; 3702 case CHUNK_ASCONF_ACK: 3703 ASSERT(fp != NULL); 3704 sctp_faddr_alive(sctp, fp); 3705 sctp_input_asconf_ack(sctp, ch, fp); 3706 BUMP_LOCAL(sctp->sctp_ibchunks); 3707 break; 3708 case CHUNK_FORWARD_TSN: 3709 ASSERT(fp != NULL); 3710 sctp->sctp_lastdata = fp; 3711 sctp_process_forward_tsn(sctp, ch, fp, &ipp); 3712 gotdata = 1; 3713 BUMP_LOCAL(sctp->sctp_ibchunks); 3714 break; 3715 default: 3716 if (sctp_strange_chunk(sctp, ch, fp) == 0) { 3717 goto nomorechunks; 3718 } /* else skip and continue processing */ 3719 break; 3720 } 3721 break; 3722 3723 case SCTPS_LISTEN: 3724 switch (ch->sch_id) { 3725 case CHUNK_INIT: 3726 sctp_send_initack(sctp, sctph, ch, mp); 3727 break; 3728 case CHUNK_COOKIE: { 3729 sctp_t *eager; 3730 3731 if (sctp_process_cookie(sctp, ch, mp, &iack, 3732 sctph, &recv_adaption, &peer_src) == -1) { 3733 BUMP_MIB(&sctp_mib, 3734 sctpInInvalidCookie); 3735 goto done; 3736 } 3737 3738 /* 3739 * The cookie is good; ensure that 3740 * the peer used the verification 3741 * tag from the init ack in the header. 3742 */ 3743 if (iack->sic_inittag != sctph->sh_verf) 3744 goto done; 3745 3746 eager = sctp_conn_request(sctp, mp, ifindex, 3747 ip_hdr_len, iack, ipsec_mp); 3748 if (eager == NULL) { 3749 sctp_send_abort(sctp, sctph->sh_verf, 3750 SCTP_ERR_NO_RESOURCES, NULL, 0, mp, 3751 0, B_FALSE); 3752 goto done; 3753 } 3754 3755 /* 3756 * If there were extra chunks 3757 * bundled with the cookie, 3758 * they must be processed 3759 * on the eager's queue. We 3760 * accomplish this by refeeding 3761 * the whole packet into the 3762 * state machine on the right 3763 * q. The packet (mp) gets 3764 * there via the eager's 3765 * cookie_mp field (overloaded 3766 * with the active open role). 3767 * This is picked up when 3768 * processing the null bind 3769 * request put on the eager's 3770 * q by sctp_accept(). We must 3771 * first revert the cookie 3772 * chunk's length field to network 3773 * byteorder so it can be 3774 * properly reprocessed on the 3775 * eager's queue. 3776 */ 3777 BUMP_MIB(&sctp_mib, sctpPassiveEstab); 3778 if (mlen > ntohs(ch->sch_len)) { 3779 eager->sctp_cookie_mp = dupb(mp); 3780 mblk_setcred(eager->sctp_cookie_mp, 3781 CONN_CRED(eager->sctp_connp)); 3782 /* 3783 * If no mem, just let 3784 * the peer retransmit. 3785 */ 3786 } 3787 sctp_assoc_event(eager, SCTP_COMM_UP, 0, NULL); 3788 if (recv_adaption) { 3789 eager->sctp_recv_adaption = 1; 3790 eager->sctp_rx_adaption_code = 3791 sctp->sctp_rx_adaption_code; 3792 sctp_adaption_event(eager); 3793 } 3794 3795 eager->sctp_active = now; 3796 sctp_send_cookie_ack(eager); 3797 3798 wake_eager = B_TRUE; 3799 3800 /* 3801 * Process rest of the chunks with eager. 3802 */ 3803 sctp = eager; 3804 fp = sctp_lookup_faddr(sctp, &peer_src); 3805 /* 3806 * Confirm peer's original source. fp can 3807 * only be NULL if peer does not use the 3808 * original source as one of its addresses... 3809 */ 3810 if (fp == NULL) 3811 fp = sctp_lookup_faddr(sctp, &src); 3812 else 3813 sctp_faddr_alive(sctp, fp); 3814 3815 /* 3816 * Validate the peer addresses. It also starts 3817 * the heartbeat timer. 3818 */ 3819 sctp_validate_peer(sctp); 3820 break; 3821 } 3822 /* Anything else is considered out-of-the-blue */ 3823 case CHUNK_ERROR: 3824 case CHUNK_ABORT: 3825 case CHUNK_COOKIE_ACK: 3826 case CHUNK_SHUTDOWN_COMPLETE: 3827 BUMP_LOCAL(sctp->sctp_ibchunks); 3828 goto done; 3829 default: 3830 BUMP_LOCAL(sctp->sctp_ibchunks); 3831 sctp_send_abort(sctp, sctph->sh_verf, 0, NULL, 3832 0, mp, 0, B_TRUE); 3833 goto done; 3834 } 3835 break; 3836 3837 case SCTPS_COOKIE_WAIT: 3838 switch (ch->sch_id) { 3839 case CHUNK_INIT_ACK: 3840 sctp_stop_faddr_timers(sctp); 3841 sctp_faddr_alive(sctp, sctp->sctp_current); 3842 sctp_send_cookie_echo(sctp, ch, mp); 3843 BUMP_LOCAL(sctp->sctp_ibchunks); 3844 break; 3845 case CHUNK_ABORT: 3846 sctp_process_abort(sctp, ch, ECONNREFUSED); 3847 goto done; 3848 case CHUNK_INIT: 3849 sctp_send_initack(sctp, sctph, ch, mp); 3850 break; 3851 case CHUNK_COOKIE: 3852 if (sctp_process_cookie(sctp, ch, mp, &iack, 3853 sctph, &recv_adaption, NULL) == -1) { 3854 BUMP_MIB(&sctp_mib, 3855 sctpInInvalidCookie); 3856 break; 3857 } 3858 sctp_send_cookie_ack(sctp); 3859 sctp_stop_faddr_timers(sctp); 3860 if (!SCTP_IS_DETACHED(sctp)) { 3861 sctp->sctp_ulp_connected(sctp->sctp_ulpd); 3862 sctp_set_ulp_prop(sctp); 3863 } 3864 sctp->sctp_state = SCTPS_ESTABLISHED; 3865 sctp->sctp_assoc_start_time = (uint32_t)lbolt; 3866 BUMP_MIB(&sctp_mib, sctpActiveEstab); 3867 if (sctp->sctp_cookie_mp) { 3868 freemsg(sctp->sctp_cookie_mp); 3869 sctp->sctp_cookie_mp = NULL; 3870 } 3871 3872 /* Validate the peer addresses. */ 3873 sctp->sctp_active = now; 3874 sctp_validate_peer(sctp); 3875 3876 sctp_assoc_event(sctp, SCTP_COMM_UP, 0, NULL); 3877 if (recv_adaption) { 3878 sctp->sctp_recv_adaption = 1; 3879 sctp_adaption_event(sctp); 3880 } 3881 /* Try sending queued data, or ASCONFs */ 3882 trysend = 1; 3883 break; 3884 default: 3885 if (sctp_strange_chunk(sctp, ch, fp) == 0) { 3886 goto nomorechunks; 3887 } /* else skip and continue processing */ 3888 break; 3889 } 3890 break; 3891 3892 case SCTPS_COOKIE_ECHOED: 3893 switch (ch->sch_id) { 3894 case CHUNK_COOKIE_ACK: 3895 if (!SCTP_IS_DETACHED(sctp)) { 3896 sctp->sctp_ulp_connected(sctp->sctp_ulpd); 3897 sctp_set_ulp_prop(sctp); 3898 } 3899 if (sctp->sctp_unacked == 0) 3900 sctp_stop_faddr_timers(sctp); 3901 sctp->sctp_state = SCTPS_ESTABLISHED; 3902 sctp->sctp_assoc_start_time = (uint32_t)lbolt; 3903 BUMP_MIB(&sctp_mib, sctpActiveEstab); 3904 BUMP_LOCAL(sctp->sctp_ibchunks); 3905 if (sctp->sctp_cookie_mp) { 3906 freemsg(sctp->sctp_cookie_mp); 3907 sctp->sctp_cookie_mp = NULL; 3908 } 3909 sctp_faddr_alive(sctp, fp); 3910 /* Validate the peer addresses. */ 3911 sctp->sctp_active = now; 3912 sctp_validate_peer(sctp); 3913 3914 /* Try sending queued data, or ASCONFs */ 3915 trysend = 1; 3916 sctp_assoc_event(sctp, SCTP_COMM_UP, 0, NULL); 3917 sctp_adaption_event(sctp); 3918 break; 3919 case CHUNK_ABORT: 3920 sctp_process_abort(sctp, ch, ECONNREFUSED); 3921 goto done; 3922 case CHUNK_COOKIE: 3923 if (sctp_process_cookie(sctp, ch, mp, &iack, 3924 sctph, &recv_adaption, NULL) == -1) { 3925 BUMP_MIB(&sctp_mib, 3926 sctpInInvalidCookie); 3927 break; 3928 } 3929 sctp_send_cookie_ack(sctp); 3930 3931 if (!SCTP_IS_DETACHED(sctp)) { 3932 sctp->sctp_ulp_connected(sctp->sctp_ulpd); 3933 sctp_set_ulp_prop(sctp); 3934 } 3935 if (sctp->sctp_unacked == 0) 3936 sctp_stop_faddr_timers(sctp); 3937 sctp->sctp_state = SCTPS_ESTABLISHED; 3938 sctp->sctp_assoc_start_time = (uint32_t)lbolt; 3939 BUMP_MIB(&sctp_mib, sctpActiveEstab); 3940 if (sctp->sctp_cookie_mp) { 3941 freemsg(sctp->sctp_cookie_mp); 3942 sctp->sctp_cookie_mp = NULL; 3943 } 3944 /* Validate the peer addresses. */ 3945 sctp->sctp_active = now; 3946 sctp_validate_peer(sctp); 3947 3948 sctp_assoc_event(sctp, SCTP_COMM_UP, 0, NULL); 3949 if (recv_adaption) { 3950 sctp->sctp_recv_adaption = 1; 3951 sctp_adaption_event(sctp); 3952 } 3953 /* Try sending queued data, or ASCONFs */ 3954 trysend = 1; 3955 break; 3956 case CHUNK_INIT: 3957 sctp_send_initack(sctp, sctph, ch, mp); 3958 break; 3959 case CHUNK_ERROR: { 3960 sctp_parm_hdr_t *p; 3961 3962 BUMP_LOCAL(sctp->sctp_ibchunks); 3963 /* check for a stale cookie */ 3964 if (ntohs(ch->sch_len) >= 3965 (sizeof (*p) + sizeof (*ch)) + 3966 sizeof (uint32_t)) { 3967 3968 p = (sctp_parm_hdr_t *)(ch + 1); 3969 if (p->sph_type == 3970 htons(SCTP_ERR_STALE_COOKIE)) { 3971 BUMP_MIB(&sctp_mib, 3972 sctpAborted); 3973 sctp_error_event(sctp, ch); 3974 sctp_assoc_event(sctp, 3975 SCTP_COMM_LOST, 0, NULL); 3976 sctp_clean_death(sctp, 3977 ECONNREFUSED); 3978 goto done; 3979 } 3980 } 3981 break; 3982 } 3983 case CHUNK_HEARTBEAT: 3984 sctp_return_heartbeat(sctp, ch, mp); 3985 break; 3986 default: 3987 if (sctp_strange_chunk(sctp, ch, fp) == 0) { 3988 goto nomorechunks; 3989 } /* else skip and continue processing */ 3990 } /* switch (ch->sch_id) */ 3991 break; 3992 3993 case SCTPS_SHUTDOWN_ACK_SENT: 3994 switch (ch->sch_id) { 3995 case CHUNK_ABORT: 3996 /* Pass gathered wisdom to IP for keeping */ 3997 sctp_update_ire(sctp); 3998 sctp_process_abort(sctp, ch, 0); 3999 goto done; 4000 case CHUNK_SHUTDOWN_COMPLETE: 4001 BUMP_LOCAL(sctp->sctp_ibchunks); 4002 BUMP_MIB(&sctp_mib, sctpShutdowns); 4003 sctp_assoc_event(sctp, SCTP_SHUTDOWN_COMP, 0, 4004 NULL); 4005 4006 /* Pass gathered wisdom to IP for keeping */ 4007 sctp_update_ire(sctp); 4008 sctp_clean_death(sctp, 0); 4009 goto done; 4010 case CHUNK_SHUTDOWN_ACK: 4011 sctp_shutdown_complete(sctp); 4012 BUMP_LOCAL(sctp->sctp_ibchunks); 4013 BUMP_MIB(&sctp_mib, sctpShutdowns); 4014 sctp_assoc_event(sctp, SCTP_SHUTDOWN_COMP, 0, 4015 NULL); 4016 sctp_clean_death(sctp, 0); 4017 goto done; 4018 case CHUNK_COOKIE: 4019 (void) sctp_shutdown_received(sctp, NULL, 4020 B_TRUE, B_FALSE, fp); 4021 BUMP_LOCAL(sctp->sctp_ibchunks); 4022 break; 4023 case CHUNK_HEARTBEAT: 4024 sctp_return_heartbeat(sctp, ch, mp); 4025 break; 4026 default: 4027 if (sctp_strange_chunk(sctp, ch, fp) == 0) { 4028 goto nomorechunks; 4029 } /* else skip and continue processing */ 4030 break; 4031 } 4032 break; 4033 4034 case SCTPS_SHUTDOWN_RECEIVED: 4035 switch (ch->sch_id) { 4036 case CHUNK_SHUTDOWN: 4037 trysend = sctp_shutdown_received(sctp, ch, 4038 B_FALSE, B_FALSE, fp); 4039 break; 4040 case CHUNK_SACK: 4041 trysend = sctp_got_sack(sctp, ch); 4042 if (trysend < 0) { 4043 sctp_send_abort(sctp, sctph->sh_verf, 4044 0, NULL, 0, mp, 0, B_FALSE); 4045 sctp_assoc_event(sctp, 4046 SCTP_COMM_LOST, 0, NULL); 4047 sctp_clean_death(sctp, 4048 ECONNABORTED); 4049 goto done; 4050 } 4051 break; 4052 case CHUNK_ABORT: 4053 sctp_process_abort(sctp, ch, ECONNRESET); 4054 goto done; 4055 case CHUNK_HEARTBEAT: 4056 sctp_return_heartbeat(sctp, ch, mp); 4057 break; 4058 default: 4059 if (sctp_strange_chunk(sctp, ch, fp) == 0) { 4060 goto nomorechunks; 4061 } /* else skip and continue processing */ 4062 break; 4063 } 4064 break; 4065 4066 default: 4067 /* 4068 * The only remaining states are SCTPS_IDLE and 4069 * SCTPS_BOUND, and we should not be getting here 4070 * for these. 4071 */ 4072 ASSERT(0); 4073 } /* switch (sctp->sctp_state) */ 4074 4075 ch = sctp_next_chunk(ch, &mlen); 4076 if (ch != NULL && !sctp_check_input(sctp, ch, mlen, 0)) 4077 goto done; 4078 } while (ch != NULL); 4079 4080 /* Finished processing all chunks in packet */ 4081 4082 nomorechunks: 4083 /* SACK if necessary */ 4084 if (gotdata) { 4085 (sctp->sctp_sack_toggle)++; 4086 sctp_sack(sctp, dups); 4087 dups = NULL; 4088 4089 if (!sctp->sctp_ack_timer_running) { 4090 sctp->sctp_ack_timer_running = B_TRUE; 4091 sctp_timer(sctp, sctp->sctp_ack_mp, 4092 MSEC_TO_TICK(sctp_deferred_ack_interval)); 4093 } 4094 } 4095 4096 if (trysend) { 4097 sctp_output(sctp); 4098 if (sctp->sctp_cxmit_list != NULL) 4099 sctp_wput_asconf(sctp, NULL); 4100 } 4101 /* If there is unsent data, make sure a timer is running */ 4102 if (sctp->sctp_unsent > 0 && !sctp->sctp_current->timer_running) { 4103 SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current, 4104 sctp->sctp_current->rto); 4105 } 4106 4107 done: 4108 if (dups != NULL) 4109 freeb(dups); 4110 if (ipsec_mp != NULL) 4111 freeb(ipsec_mp); 4112 freemsg(mp); 4113 4114 if (wake_eager) { 4115 /* 4116 * sctp points to newly created control block, need to 4117 * release it before exiting. Before releasing it and 4118 * processing the sendq, need to grab a hold on it. 4119 * Otherwise, another thread can close it while processing 4120 * the sendq. 4121 */ 4122 SCTP_REFHOLD(sctp); 4123 WAKE_SCTP(sctp); 4124 sctp_process_sendq(sctp); 4125 SCTP_REFRELE(sctp); 4126 } 4127 } 4128 4129 /* 4130 * Some amount of data got removed from rx q. 4131 * Check if we should send a window update. 4132 * 4133 * Due to way sctp_rwnd updates are made, ULP can give reports out-of-order. 4134 * To keep from dropping incoming data due to this, we only update 4135 * sctp_rwnd when if it's larger than what we've reported to peer earlier. 4136 */ 4137 void 4138 sctp_recvd(sctp_t *sctp, int len) 4139 { 4140 int32_t old, new; 4141 4142 ASSERT(sctp != NULL); 4143 RUN_SCTP(sctp); 4144 4145 if (len < sctp->sctp_rwnd) { 4146 WAKE_SCTP(sctp); 4147 return; 4148 } 4149 ASSERT(sctp->sctp_rwnd >= sctp->sctp_rxqueued); 4150 old = sctp->sctp_rwnd - sctp->sctp_rxqueued; 4151 new = len - sctp->sctp_rxqueued; 4152 sctp->sctp_rwnd = len; 4153 4154 if (sctp->sctp_state >= SCTPS_ESTABLISHED && 4155 ((old <= new >> 1) || (old < sctp->sctp_mss))) { 4156 sctp->sctp_force_sack = 1; 4157 BUMP_MIB(&sctp_mib, sctpOutWinUpdate); 4158 sctp_sack(sctp, NULL); 4159 old = 1; 4160 } else { 4161 old = 0; 4162 } 4163 WAKE_SCTP(sctp); 4164 if (old > 0) { 4165 sctp_process_sendq(sctp); 4166 } 4167 } 4168