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 /* 777 * Try partial delivery. 778 */ 779 static mblk_t * 780 sctp_try_partial_delivery(sctp_t *sctp, mblk_t *hmp, sctp_reass_t *srp, 781 sctp_data_hdr_t **dc) 782 { 783 mblk_t *first_mp; 784 mblk_t *mp; 785 mblk_t *dmp; 786 mblk_t *qmp; 787 mblk_t *prev; 788 sctp_data_hdr_t *qdc; 789 uint32_t tsn; 790 791 ASSERT(DB_TYPE(hmp) == M_CTL); 792 793 dprint(4, ("trypartial: got=%d, needed=%d\n", 794 (int)(srp->got), (int)(srp->needed))); 795 796 first_mp = hmp->b_cont; 797 mp = first_mp; 798 qdc = (sctp_data_hdr_t *)mp->b_rptr; 799 800 ASSERT(SCTP_DATA_GET_BBIT(qdc) && srp->hasBchunk); 801 802 tsn = ntohl(qdc->sdh_tsn) + 1; 803 804 /* 805 * This loop has two exit conditions: the 806 * end of received chunks has been reached, or 807 * there is a break in the sequence. We want 808 * to chop the reassembly list as follows (the 809 * numbers are TSNs): 810 * 10 -> 11 -> (end of chunks) 811 * 10 -> 11 -> | 13 (break in sequence) 812 */ 813 prev = mp; 814 mp = mp->b_cont; 815 while (mp != NULL) { 816 qdc = (sctp_data_hdr_t *)mp->b_rptr; 817 if (ntohl(qdc->sdh_tsn) != tsn) 818 break; 819 prev = mp; 820 mp = mp->b_cont; 821 tsn++; 822 } 823 /* 824 * We are sending all the fragments upstream, we have to retain 825 * the srp info for further fragments. 826 */ 827 if (mp == NULL) { 828 dmp = hmp->b_cont; 829 hmp->b_cont = NULL; 830 srp->nexttsn = tsn; 831 srp->msglen = 0; 832 srp->needed = 0; 833 srp->got = 0; 834 srp->partial_delivered = B_TRUE; 835 srp->tail = NULL; 836 } else { 837 dmp = hmp->b_cont; 838 hmp->b_cont = mp; 839 } 840 srp->hasBchunk = B_FALSE; 841 /* 842 * mp now points at the last chunk in the sequence, 843 * and prev points to mp's previous in the list. 844 * We chop the list at prev, and convert mp into the 845 * new list head by setting the B bit. Subsequence 846 * fragment deliveries will follow the normal reassembly 847 * path. 848 */ 849 prev->b_cont = NULL; 850 srp->partial_delivered = B_TRUE; 851 852 dprint(4, ("trypartial: got some, got=%d, needed=%d\n", 853 (int)(srp->got), (int)(srp->needed))); 854 855 /* 856 * Adjust all mblk's except the lead so their rptr's point to the 857 * payload. sctp_data_chunk() will need to process the lead's 858 * data chunk section, so leave it's rptr pointing at the data chunk. 859 */ 860 *dc = (sctp_data_hdr_t *)dmp->b_rptr; 861 if (srp->tail != NULL) { 862 srp->got--; 863 ASSERT(srp->got != 0); 864 if (srp->needed != 0) { 865 srp->needed--; 866 ASSERT(srp->needed != 0); 867 } 868 srp->msglen -= ntohs((*dc)->sdh_len); 869 } 870 for (qmp = dmp->b_cont; qmp != NULL; qmp = qmp->b_cont) { 871 qdc = (sctp_data_hdr_t *)qmp->b_rptr; 872 qmp->b_rptr = (uchar_t *)(qdc + 1); 873 874 /* 875 * Deduct the balance from got and needed here, now that 876 * we know we are actually delivering these data. 877 */ 878 if (srp->tail != NULL) { 879 srp->got--; 880 ASSERT(srp->got != 0); 881 if (srp->needed != 0) { 882 srp->needed--; 883 ASSERT(srp->needed != 0); 884 } 885 srp->msglen -= ntohs(qdc->sdh_len); 886 } 887 } 888 ASSERT(srp->msglen == 0); 889 BUMP_LOCAL(sctp->sctp_reassmsgs); 890 891 return (dmp); 892 } 893 894 /* 895 * Fragment list for ordered messages. 896 * If no error occures, error is set to 0. If we run out of memory, error 897 * is set to 1. If the peer commits a fatal error (like using different 898 * sequence numbers for the same data fragment series), the association is 899 * aborted and error is set to 2. tpfinished indicates whether we have 900 * assembled a complete message, this is used in sctp_data_chunk() to 901 * see if we can try to send any queued message for this stream. 902 */ 903 static mblk_t * 904 sctp_data_frag(sctp_t *sctp, mblk_t *dmp, sctp_data_hdr_t **dc, int *error, 905 sctp_instr_t *sip, boolean_t *tpfinished) 906 { 907 mblk_t *hmp; 908 mblk_t *pmp; 909 mblk_t *qmp; 910 mblk_t *first_mp; 911 sctp_reass_t *srp; 912 sctp_data_hdr_t *qdc; 913 sctp_data_hdr_t *bdc; 914 sctp_data_hdr_t *edc; 915 uint32_t tsn; 916 uint16_t fraglen = 0; 917 918 *error = 0; 919 920 /* find the reassembly queue for this data chunk */ 921 hmp = qmp = sip->istr_reass; 922 for (; hmp != NULL; hmp = hmp->b_next) { 923 srp = (sctp_reass_t *)DB_BASE(hmp); 924 if (ntohs((*dc)->sdh_ssn) == srp->ssn) 925 goto foundit; 926 else if (SSN_GT(srp->ssn, ntohs((*dc)->sdh_ssn))) 927 break; 928 qmp = hmp; 929 } 930 931 /* 932 * Allocate a M_CTL that will contain information about this 933 * fragmented message. 934 */ 935 if ((pmp = allocb(sizeof (*srp), BPRI_MED)) == NULL) { 936 *error = 1; 937 return (NULL); 938 } 939 DB_TYPE(pmp) = M_CTL; 940 srp = (sctp_reass_t *)DB_BASE(pmp); 941 pmp->b_cont = dmp; 942 943 if (hmp != NULL) { 944 if (sip->istr_reass == hmp) { 945 sip->istr_reass = pmp; 946 pmp->b_next = hmp; 947 pmp->b_prev = NULL; 948 hmp->b_prev = pmp; 949 } else { 950 qmp->b_next = pmp; 951 pmp->b_prev = qmp; 952 pmp->b_next = hmp; 953 hmp->b_prev = pmp; 954 } 955 } else { 956 /* make a new reass head and stick it on the end */ 957 if (sip->istr_reass == NULL) { 958 sip->istr_reass = pmp; 959 pmp->b_prev = NULL; 960 } else { 961 qmp->b_next = pmp; 962 pmp->b_prev = qmp; 963 } 964 pmp->b_next = NULL; 965 } 966 srp->partial_delivered = B_FALSE; 967 srp->ssn = ntohs((*dc)->sdh_ssn); 968 empty_srp: 969 srp->needed = 0; 970 srp->got = 1; 971 srp->tail = dmp; 972 if (SCTP_DATA_GET_BBIT(*dc)) { 973 srp->msglen = ntohs((*dc)->sdh_len); 974 srp->nexttsn = ntohl((*dc)->sdh_tsn) + 1; 975 srp->hasBchunk = B_TRUE; 976 } else if (srp->partial_delivered && 977 srp->nexttsn == ntohl((*dc)->sdh_tsn)) { 978 SCTP_DATA_SET_BBIT(*dc); 979 /* Last fragment */ 980 if (SCTP_DATA_GET_EBIT(*dc)) { 981 srp->needed = 1; 982 goto frag_done; 983 } 984 srp->hasBchunk = B_TRUE; 985 srp->msglen = ntohs((*dc)->sdh_len); 986 srp->nexttsn++; 987 } 988 return (NULL); 989 foundit: 990 /* 991 * else already have a reassembly queue. Insert the new data chunk 992 * in the reassemble queue. Try the tail first, on the assumption 993 * that the fragments are coming in in order. 994 */ 995 qmp = srp->tail; 996 997 /* 998 * This means the message was partially delivered. 999 */ 1000 if (qmp == NULL) { 1001 ASSERT(srp->got == 0 && srp->needed == 0 && 1002 srp->partial_delivered); 1003 ASSERT(hmp->b_cont == NULL); 1004 hmp->b_cont = dmp; 1005 goto empty_srp; 1006 } 1007 qdc = (sctp_data_hdr_t *)qmp->b_rptr; 1008 ASSERT(qmp->b_cont == NULL); 1009 1010 /* XXXIs it fine to do this just here? */ 1011 if ((*dc)->sdh_sid != qdc->sdh_sid) { 1012 /* our peer is fatally confused; XXX abort the assc */ 1013 *error = 2; 1014 return (NULL); 1015 } 1016 if (SEQ_GT(ntohl((*dc)->sdh_tsn), ntohl(qdc->sdh_tsn))) { 1017 qmp->b_cont = dmp; 1018 srp->tail = dmp; 1019 dmp->b_cont = NULL; 1020 if (srp->hasBchunk && srp->nexttsn == ntohl((*dc)->sdh_tsn)) { 1021 srp->msglen += ntohs((*dc)->sdh_len); 1022 srp->nexttsn++; 1023 } 1024 goto inserted; 1025 } 1026 1027 /* Next check for insertion at the beginning */ 1028 qmp = hmp->b_cont; 1029 qdc = (sctp_data_hdr_t *)qmp->b_rptr; 1030 if (SEQ_LT(ntohl((*dc)->sdh_tsn), ntohl(qdc->sdh_tsn))) { 1031 dmp->b_cont = qmp; 1032 hmp->b_cont = dmp; 1033 if (SCTP_DATA_GET_BBIT(*dc)) { 1034 srp->hasBchunk = B_TRUE; 1035 srp->nexttsn = ntohl((*dc)->sdh_tsn); 1036 } 1037 goto preinserted; 1038 } 1039 1040 /* Insert somewhere in the middle */ 1041 for (;;) { 1042 /* Tail check above should have caught this */ 1043 ASSERT(qmp->b_cont != NULL); 1044 1045 qdc = (sctp_data_hdr_t *)qmp->b_cont->b_rptr; 1046 if (SEQ_LT(ntohl((*dc)->sdh_tsn), ntohl(qdc->sdh_tsn))) { 1047 /* insert here */ 1048 dmp->b_cont = qmp->b_cont; 1049 qmp->b_cont = dmp; 1050 break; 1051 } 1052 qmp = qmp->b_cont; 1053 } 1054 preinserted: 1055 if (!srp->hasBchunk || ntohl((*dc)->sdh_tsn) != srp->nexttsn) 1056 goto inserted; 1057 /* 1058 * fraglen contains the length of consecutive chunks of fragments. 1059 * starting from the chunk inserted recently. 1060 */ 1061 tsn = srp->nexttsn; 1062 for (qmp = dmp; qmp != NULL; qmp = qmp->b_cont) { 1063 qdc = (sctp_data_hdr_t *)qmp->b_rptr; 1064 if (tsn != ntohl(qdc->sdh_tsn)) 1065 break; 1066 fraglen += ntohs(qdc->sdh_len); 1067 tsn++; 1068 } 1069 srp->nexttsn = tsn; 1070 srp->msglen += fraglen; 1071 inserted: 1072 srp->got++; 1073 first_mp = hmp->b_cont; 1074 if (srp->needed == 0) { 1075 /* check if we have the first and last fragments */ 1076 bdc = (sctp_data_hdr_t *)first_mp->b_rptr; 1077 edc = (sctp_data_hdr_t *)srp->tail->b_rptr; 1078 1079 /* calculate how many fragments are needed, if possible */ 1080 if (SCTP_DATA_GET_BBIT(bdc) && SCTP_DATA_GET_EBIT(edc)) { 1081 srp->needed = ntohl(edc->sdh_tsn) - 1082 ntohl(bdc->sdh_tsn) + 1; 1083 } 1084 } 1085 1086 /* 1087 * Try partial delivery if the message length has exceeded the 1088 * partial delivery point. Only do this if we can immediately 1089 * deliver the partially assembled message, and only partially 1090 * deliver one message at a time (i.e. messages cannot be 1091 * intermixed arriving at the upper layer). A simple way to 1092 * enforce this is to only try partial delivery if this TSN is 1093 * the next expected TSN. Partial Delivery not supported 1094 * for un-ordered message. 1095 */ 1096 if (srp->needed != srp->got) { 1097 dmp = NULL; 1098 if (ntohl((*dc)->sdh_tsn) == sctp->sctp_ftsn && 1099 srp->msglen >= sctp->sctp_pd_point) { 1100 dmp = sctp_try_partial_delivery(sctp, hmp, srp, dc); 1101 *tpfinished = B_FALSE; 1102 } 1103 return (dmp); 1104 } 1105 frag_done: 1106 /* 1107 * else reassembly done; prepare the data for delivery. 1108 * First unlink hmp from the ssn list. 1109 */ 1110 if (sip->istr_reass == hmp) { 1111 sip->istr_reass = hmp->b_next; 1112 if (hmp->b_next) 1113 hmp->b_next->b_prev = NULL; 1114 } else { 1115 ASSERT(hmp->b_prev != NULL); 1116 hmp->b_prev->b_next = hmp->b_next; 1117 if (hmp->b_next) 1118 hmp->b_next->b_prev = hmp->b_prev; 1119 } 1120 1121 /* 1122 * Using b_prev and b_next was a little sinful, but OK since 1123 * this mblk is never put*'d. However, freeb() will still 1124 * ASSERT that they are unused, so we need to NULL them out now. 1125 */ 1126 hmp->b_next = NULL; 1127 hmp->b_prev = NULL; 1128 dmp = hmp; 1129 dmp = dmp->b_cont; 1130 hmp->b_cont = NULL; 1131 freeb(hmp); 1132 *tpfinished = B_TRUE; 1133 1134 /* 1135 * Adjust all mblk's except the lead so their rptr's point to the 1136 * payload. sctp_data_chunk() will need to process the lead's 1137 * data chunk section, so leave it's rptr pointing at the data chunk. 1138 */ 1139 *dc = (sctp_data_hdr_t *)dmp->b_rptr; 1140 for (qmp = dmp->b_cont; qmp != NULL; qmp = qmp->b_cont) { 1141 qdc = (sctp_data_hdr_t *)qmp->b_rptr; 1142 qmp->b_rptr = (uchar_t *)(qdc + 1); 1143 } 1144 BUMP_LOCAL(sctp->sctp_reassmsgs); 1145 1146 return (dmp); 1147 } 1148 static void 1149 sctp_add_dup(uint32_t tsn, mblk_t **dups) 1150 { 1151 mblk_t *mp; 1152 size_t bsize = SCTP_DUP_MBLK_SZ * sizeof (tsn); 1153 1154 if (dups == NULL) { 1155 return; 1156 } 1157 1158 /* first time? */ 1159 if (*dups == NULL) { 1160 *dups = allocb(bsize, BPRI_MED); 1161 if (*dups == NULL) { 1162 return; 1163 } 1164 } 1165 1166 mp = *dups; 1167 if ((mp->b_wptr - mp->b_rptr) >= bsize) { 1168 /* maximum reached */ 1169 return; 1170 } 1171 1172 /* add the duplicate tsn */ 1173 bcopy(&tsn, mp->b_wptr, sizeof (tsn)); 1174 mp->b_wptr += sizeof (tsn); 1175 ASSERT((mp->b_wptr - mp->b_rptr) <= bsize); 1176 } 1177 1178 static void 1179 sctp_data_chunk(sctp_t *sctp, sctp_chunk_hdr_t *ch, mblk_t *mp, mblk_t **dups, 1180 sctp_faddr_t *fp, ip6_pkt_t *ipp) 1181 { 1182 sctp_data_hdr_t *dc; 1183 mblk_t *dmp, *pmp; 1184 mblk_t *errmp; 1185 sctp_instr_t *instr; 1186 int ubit; 1187 int isfrag; 1188 uint16_t ssn; 1189 uint32_t oftsn; 1190 boolean_t can_deliver = B_TRUE; 1191 uint32_t tsn; 1192 int dlen; 1193 boolean_t tpfinished = B_TRUE; 1194 int32_t new_rwnd; 1195 sctp_stack_t *sctps = sctp->sctp_sctps; 1196 1197 /* The following are used multiple times, so we inline them */ 1198 #define SCTP_ACK_IT(sctp, tsn) \ 1199 if (tsn == sctp->sctp_ftsn) { \ 1200 dprint(2, ("data_chunk: acking next %x\n", tsn)); \ 1201 (sctp)->sctp_ftsn++; \ 1202 if ((sctp)->sctp_sack_gaps > 0) \ 1203 (sctp)->sctp_force_sack = 1; \ 1204 } else if (SEQ_GT(tsn, sctp->sctp_ftsn)) { \ 1205 /* Got a gap; record it */ \ 1206 dprint(2, ("data_chunk: acking gap %x\n", tsn)); \ 1207 sctp_ack_add(&sctp->sctp_sack_info, tsn, \ 1208 &sctp->sctp_sack_gaps); \ 1209 sctp->sctp_force_sack = 1; \ 1210 } 1211 1212 errmp = NULL; 1213 dmp = NULL; 1214 1215 dc = (sctp_data_hdr_t *)ch; 1216 tsn = ntohl(dc->sdh_tsn); 1217 1218 dprint(3, ("sctp_data_chunk: mp=%p tsn=%x\n", (void *)mp, tsn)); 1219 1220 /* Check for duplicates */ 1221 if (SEQ_LT(tsn, sctp->sctp_ftsn)) { 1222 dprint(4, ("sctp_data_chunk: dropping duplicate\n")); 1223 sctp->sctp_force_sack = 1; 1224 sctp_add_dup(dc->sdh_tsn, dups); 1225 return; 1226 } 1227 1228 if (sctp->sctp_sack_info != NULL) { 1229 sctp_set_t *sp; 1230 1231 for (sp = sctp->sctp_sack_info; sp; sp = sp->next) { 1232 if (SEQ_GEQ(tsn, sp->begin) && SEQ_LEQ(tsn, sp->end)) { 1233 dprint(4, 1234 ("sctp_data_chunk: dropping dup > cumtsn\n")); 1235 sctp->sctp_force_sack = 1; 1236 sctp_add_dup(dc->sdh_tsn, dups); 1237 return; 1238 } 1239 } 1240 } 1241 1242 /* We cannot deliver anything up now but we still need to handle it. */ 1243 if (SCTP_IS_DETACHED(sctp)) { 1244 BUMP_MIB(&sctps->sctps_mib, sctpInClosed); 1245 can_deliver = B_FALSE; 1246 } 1247 1248 dlen = ntohs(dc->sdh_len) - sizeof (*dc); 1249 1250 /* Check for buffer space */ 1251 if (sctp->sctp_rwnd - sctp->sctp_rxqueued < dlen) { 1252 /* Drop and SACK, but don't advance the cumulative TSN. */ 1253 sctp->sctp_force_sack = 1; 1254 dprint(0, ("sctp_data_chunk: exceed rwnd %d rxqueued %d " 1255 "dlen %d ssn %d tsn %x\n", sctp->sctp_rwnd, 1256 sctp->sctp_rxqueued, dlen, ntohs(dc->sdh_ssn), 1257 ntohl(dc->sdh_tsn))); 1258 return; 1259 } 1260 1261 if (ntohs(dc->sdh_sid) >= sctp->sctp_num_istr) { 1262 uint16_t inval_parm[2]; 1263 1264 inval_parm[0] = dc->sdh_sid; 1265 /* RESERVED to be ignored at the receiving end */ 1266 inval_parm[1] = 0; 1267 /* ack and drop it */ 1268 errmp = sctp_make_err(sctp, SCTP_ERR_BAD_SID, 1269 (char *)inval_parm, sizeof (inval_parm)); 1270 SCTP_ACK_IT(sctp, tsn); 1271 if (errmp != NULL) 1272 sctp_send_err(sctp, errmp, NULL); 1273 return; 1274 } 1275 1276 ubit = SCTP_DATA_GET_UBIT(dc); 1277 ASSERT(sctp->sctp_instr != NULL); 1278 instr = &sctp->sctp_instr[ntohs(dc->sdh_sid)]; 1279 /* Initialize the stream, if not yet used */ 1280 if (instr->sctp == NULL) 1281 instr->sctp = sctp; 1282 1283 isfrag = !(SCTP_DATA_GET_BBIT(dc) && SCTP_DATA_GET_EBIT(dc)); 1284 ssn = ntohs(dc->sdh_ssn); 1285 1286 dmp = dupb(mp); 1287 if (dmp == NULL) { 1288 /* drop it and don't ack it, causing the peer to retransmit */ 1289 return; 1290 } 1291 dmp->b_wptr = (uchar_t *)ch + ntohs(ch->sch_len); 1292 1293 sctp->sctp_rxqueued += dlen; 1294 1295 oftsn = sctp->sctp_ftsn; 1296 1297 if (isfrag) { 1298 int error = 0; 1299 1300 /* fragmented data chunk */ 1301 dmp->b_rptr = (uchar_t *)dc; 1302 if (ubit) { 1303 dmp = sctp_uodata_frag(sctp, dmp, &dc); 1304 #if DEBUG 1305 if (dmp != NULL) { 1306 ASSERT(instr == 1307 &sctp->sctp_instr[ntohs(dc->sdh_sid)]); 1308 } 1309 #endif 1310 } else { 1311 dmp = sctp_data_frag(sctp, dmp, &dc, &error, instr, 1312 &tpfinished); 1313 } 1314 if (error != 0) { 1315 sctp->sctp_rxqueued -= dlen; 1316 if (error == 1) { 1317 /* 1318 * out of memory; don't ack it so 1319 * the peer retransmits 1320 */ 1321 return; 1322 } else if (error == 2) { 1323 /* 1324 * fatal error (i.e. peer used different 1325 * ssn's for same fragmented data) -- 1326 * the association has been aborted. 1327 * XXX need to return errval so state 1328 * machine can also abort processing. 1329 */ 1330 dprint(0, ("error 2: must not happen!\n")); 1331 return; 1332 } 1333 } 1334 1335 if (dmp == NULL) { 1336 /* 1337 * Can't process this data now, but the cumulative 1338 * TSN may be advanced, so do the checks at done. 1339 */ 1340 SCTP_ACK_IT(sctp, tsn); 1341 goto done; 1342 } 1343 } 1344 1345 if (!ubit && !isfrag && ssn != instr->nextseq) { 1346 /* Adjust rptr to point at the data chunk for compares */ 1347 dmp->b_rptr = (uchar_t *)dc; 1348 1349 dprint(2, 1350 ("data_chunk: inserted %x in pq (ssn %d expected %d)\n", 1351 ntohl(dc->sdh_tsn), (int)(ssn), (int)(instr->nextseq))); 1352 1353 if (instr->istr_msgs == NULL) { 1354 instr->istr_msgs = dmp; 1355 ASSERT(dmp->b_prev == NULL && dmp->b_next == NULL); 1356 } else { 1357 mblk_t *imblk = instr->istr_msgs; 1358 sctp_data_hdr_t *idc; 1359 1360 /* 1361 * XXXNeed to take sequence wraps into account, 1362 * ... and a more efficient insertion algo. 1363 */ 1364 for (;;) { 1365 idc = (sctp_data_hdr_t *)imblk->b_rptr; 1366 if (SSN_GT(ntohs(idc->sdh_ssn), 1367 ntohs(dc->sdh_ssn))) { 1368 if (instr->istr_msgs == imblk) { 1369 instr->istr_msgs = dmp; 1370 dmp->b_next = imblk; 1371 imblk->b_prev = dmp; 1372 } else { 1373 ASSERT(imblk->b_prev != NULL); 1374 imblk->b_prev->b_next = dmp; 1375 dmp->b_prev = imblk->b_prev; 1376 imblk->b_prev = dmp; 1377 dmp->b_next = imblk; 1378 } 1379 break; 1380 } 1381 if (imblk->b_next == NULL) { 1382 imblk->b_next = dmp; 1383 dmp->b_prev = imblk; 1384 break; 1385 } 1386 imblk = imblk->b_next; 1387 } 1388 } 1389 (instr->istr_nmsgs)++; 1390 (sctp->sctp_istr_nmsgs)++; 1391 SCTP_ACK_IT(sctp, tsn); 1392 return; 1393 } 1394 1395 /* 1396 * Else we can deliver the data directly. Recalculate 1397 * dlen now since we may have reassembled data. 1398 */ 1399 dlen = dmp->b_wptr - (uchar_t *)dc - sizeof (*dc); 1400 for (pmp = dmp->b_cont; pmp != NULL; pmp = pmp->b_cont) 1401 dlen += pmp->b_wptr - pmp->b_rptr; 1402 ASSERT(sctp->sctp_rxqueued >= dlen); 1403 ASSERT(sctp->sctp_rwnd >= dlen); 1404 1405 /* Deliver the message. */ 1406 sctp->sctp_rxqueued -= dlen; 1407 1408 if (can_deliver) { 1409 dmp->b_rptr = (uchar_t *)(dc + 1); 1410 if (sctp_input_add_ancillary(sctp, &dmp, dc, fp, ipp) == 0) { 1411 dprint(1, ("sctp_data_chunk: delivering %lu bytes\n", 1412 msgdsize(dmp))); 1413 sctp->sctp_rwnd -= dlen; 1414 new_rwnd = sctp->sctp_ulp_recv(sctp->sctp_ulpd, dmp, 1415 tpfinished ? 0 : SCTP_PARTIAL_DATA); 1416 if (new_rwnd > sctp->sctp_rwnd) { 1417 sctp->sctp_rwnd = new_rwnd; 1418 } 1419 SCTP_ACK_IT(sctp, tsn); 1420 } else { 1421 /* Just free the message if we don't have memory. */ 1422 freemsg(dmp); 1423 return; 1424 } 1425 } else { 1426 /* About to free the data */ 1427 freemsg(dmp); 1428 SCTP_ACK_IT(sctp, tsn); 1429 } 1430 1431 /* 1432 * data, now enqueued, may already have been processed and free'd 1433 * by the ULP (or we may have just freed it above, if we could not 1434 * deliver it), so we must not reference it (this is why we kept 1435 * the ssn and ubit above). 1436 */ 1437 if (ubit != 0) { 1438 BUMP_LOCAL(sctp->sctp_iudchunks); 1439 goto done; 1440 } 1441 BUMP_LOCAL(sctp->sctp_idchunks); 1442 1443 /* 1444 * If there was a partial delivery and it has not finished, 1445 * don't pull anything from the pqueues. 1446 */ 1447 if (!tpfinished) { 1448 goto done; 1449 } 1450 1451 instr->nextseq = ssn + 1; 1452 /* Deliver any successive data chunks in the instr queue */ 1453 while (instr->istr_nmsgs > 0) { 1454 dmp = (mblk_t *)instr->istr_msgs; 1455 dc = (sctp_data_hdr_t *)dmp->b_rptr; 1456 ssn = ntohs(dc->sdh_ssn); 1457 /* Gap in the sequence */ 1458 if (ssn != instr->nextseq) 1459 break; 1460 1461 /* Else deliver the data */ 1462 (instr->istr_nmsgs)--; 1463 (instr->nextseq)++; 1464 (sctp->sctp_istr_nmsgs)--; 1465 1466 instr->istr_msgs = instr->istr_msgs->b_next; 1467 if (instr->istr_msgs != NULL) 1468 instr->istr_msgs->b_prev = NULL; 1469 dmp->b_next = dmp->b_prev = NULL; 1470 1471 dprint(2, ("data_chunk: pulling %x from pq (ssn %d)\n", 1472 ntohl(dc->sdh_tsn), (int)ssn)); 1473 1474 /* 1475 * If this chunk was reassembled, each b_cont represents 1476 * another TSN; advance ftsn now. 1477 */ 1478 dlen = dmp->b_wptr - dmp->b_rptr - sizeof (*dc); 1479 for (pmp = dmp->b_cont; pmp; pmp = pmp->b_cont) 1480 dlen += pmp->b_wptr - pmp->b_rptr; 1481 1482 ASSERT(sctp->sctp_rxqueued >= dlen); 1483 ASSERT(sctp->sctp_rwnd >= dlen); 1484 1485 sctp->sctp_rxqueued -= dlen; 1486 if (can_deliver) { 1487 dmp->b_rptr = (uchar_t *)(dc + 1); 1488 if (sctp_input_add_ancillary(sctp, &dmp, dc, fp, 1489 ipp) == 0) { 1490 dprint(1, ("sctp_data_chunk: delivering %lu " 1491 "bytes\n", msgdsize(dmp))); 1492 sctp->sctp_rwnd -= dlen; 1493 new_rwnd = sctp->sctp_ulp_recv(sctp->sctp_ulpd, 1494 dmp, tpfinished ? 0 : SCTP_PARTIAL_DATA); 1495 if (new_rwnd > sctp->sctp_rwnd) { 1496 sctp->sctp_rwnd = new_rwnd; 1497 } 1498 SCTP_ACK_IT(sctp, tsn); 1499 } else { 1500 freemsg(dmp); 1501 return; 1502 } 1503 } else { 1504 /* About to free the data */ 1505 freemsg(dmp); 1506 SCTP_ACK_IT(sctp, tsn); 1507 } 1508 } 1509 1510 done: 1511 1512 /* 1513 * If there are gap reports pending, check if advancing 1514 * the ftsn here closes a gap. If so, we can advance 1515 * ftsn to the end of the set. 1516 */ 1517 if (sctp->sctp_sack_info != NULL && 1518 sctp->sctp_ftsn == sctp->sctp_sack_info->begin) { 1519 sctp->sctp_ftsn = sctp->sctp_sack_info->end + 1; 1520 } 1521 /* 1522 * If ftsn has moved forward, maybe we can remove gap reports. 1523 * NB: dmp may now be NULL, so don't dereference it here. 1524 */ 1525 if (oftsn != sctp->sctp_ftsn && sctp->sctp_sack_info != NULL) { 1526 sctp_ack_rem(&sctp->sctp_sack_info, sctp->sctp_ftsn - 1, 1527 &sctp->sctp_sack_gaps); 1528 dprint(2, ("data_chunk: removed acks before %x (num=%d)\n", 1529 sctp->sctp_ftsn - 1, sctp->sctp_sack_gaps)); 1530 } 1531 1532 #ifdef DEBUG 1533 if (sctp->sctp_sack_info != NULL) { 1534 ASSERT(sctp->sctp_ftsn != sctp->sctp_sack_info->begin); 1535 } 1536 #endif 1537 1538 #undef SCTP_ACK_IT 1539 } 1540 1541 void 1542 sctp_fill_sack(sctp_t *sctp, unsigned char *dst, int sacklen) 1543 { 1544 sctp_chunk_hdr_t *sch; 1545 sctp_sack_chunk_t *sc; 1546 sctp_sack_frag_t *sf; 1547 uint16_t num_gaps = sctp->sctp_sack_gaps; 1548 sctp_set_t *sp; 1549 1550 /* Chunk hdr */ 1551 sch = (sctp_chunk_hdr_t *)dst; 1552 sch->sch_id = CHUNK_SACK; 1553 sch->sch_flags = 0; 1554 sch->sch_len = htons(sacklen); 1555 1556 /* SACK chunk */ 1557 sctp->sctp_lastacked = sctp->sctp_ftsn - 1; 1558 1559 sc = (sctp_sack_chunk_t *)(sch + 1); 1560 sc->ssc_cumtsn = htonl(sctp->sctp_lastacked); 1561 if (sctp->sctp_rxqueued < sctp->sctp_rwnd) { 1562 sc->ssc_a_rwnd = htonl(sctp->sctp_rwnd - sctp->sctp_rxqueued); 1563 } else { 1564 sc->ssc_a_rwnd = 0; 1565 } 1566 sc->ssc_numfrags = htons(num_gaps); 1567 sc->ssc_numdups = 0; 1568 1569 /* lay in gap reports */ 1570 sf = (sctp_sack_frag_t *)(sc + 1); 1571 for (sp = sctp->sctp_sack_info; sp; sp = sp->next) { 1572 uint16_t offset; 1573 1574 /* start */ 1575 if (sp->begin > sctp->sctp_lastacked) { 1576 offset = (uint16_t)(sp->begin - sctp->sctp_lastacked); 1577 } else { 1578 /* sequence number wrap */ 1579 offset = (uint16_t)(UINT32_MAX - sctp->sctp_lastacked + 1580 sp->begin); 1581 } 1582 sf->ssf_start = htons(offset); 1583 1584 /* end */ 1585 if (sp->end >= sp->begin) { 1586 offset += (uint16_t)(sp->end - sp->begin); 1587 } else { 1588 /* sequence number wrap */ 1589 offset += (uint16_t)(UINT32_MAX - sp->begin + sp->end); 1590 } 1591 sf->ssf_end = htons(offset); 1592 1593 sf++; 1594 /* This is just for debugging (a la the following assertion) */ 1595 num_gaps--; 1596 } 1597 1598 ASSERT(num_gaps == 0); 1599 1600 /* If the SACK timer is running, stop it */ 1601 if (sctp->sctp_ack_timer_running) { 1602 sctp_timer_stop(sctp->sctp_ack_mp); 1603 sctp->sctp_ack_timer_running = B_FALSE; 1604 } 1605 1606 BUMP_LOCAL(sctp->sctp_obchunks); 1607 } 1608 1609 mblk_t * 1610 sctp_make_sack(sctp_t *sctp, sctp_faddr_t *sendto, mblk_t *dups) 1611 { 1612 mblk_t *smp; 1613 size_t slen; 1614 sctp_chunk_hdr_t *sch; 1615 sctp_sack_chunk_t *sc; 1616 int32_t acks_max; 1617 sctp_stack_t *sctps = sctp->sctp_sctps; 1618 1619 if (sctp->sctp_force_sack) { 1620 sctp->sctp_force_sack = 0; 1621 goto checks_done; 1622 } 1623 1624 acks_max = sctps->sctps_deferred_acks_max; 1625 if (sctp->sctp_state == SCTPS_ESTABLISHED) { 1626 if (sctp->sctp_sack_toggle < acks_max) { 1627 /* no need to SACK right now */ 1628 dprint(2, ("sctp_make_sack: %p no sack (toggle)\n", 1629 (void *)sctp)); 1630 return (NULL); 1631 } else if (sctp->sctp_sack_toggle >= acks_max) { 1632 sctp->sctp_sack_toggle = 0; 1633 } 1634 } 1635 1636 if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) { 1637 dprint(2, ("sctp_make_sack: %p no sack (already)\n", 1638 (void *)sctp)); 1639 return (NULL); 1640 } 1641 1642 checks_done: 1643 dprint(2, ("sctp_make_sack: acking %x\n", sctp->sctp_ftsn - 1)); 1644 1645 slen = sizeof (*sch) + sizeof (*sc) + 1646 (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps); 1647 smp = sctp_make_mp(sctp, sendto, slen); 1648 if (smp == NULL) { 1649 SCTP_KSTAT(sctps, sctp_send_sack_failed); 1650 return (NULL); 1651 } 1652 sch = (sctp_chunk_hdr_t *)smp->b_wptr; 1653 1654 sctp_fill_sack(sctp, smp->b_wptr, slen); 1655 smp->b_wptr += slen; 1656 if (dups) { 1657 sc = (sctp_sack_chunk_t *)(sch + 1); 1658 sc->ssc_numdups = htons((dups->b_wptr - dups->b_rptr) 1659 / sizeof (uint32_t)); 1660 sch->sch_len = htons(slen + (dups->b_wptr - dups->b_rptr)); 1661 smp->b_cont = dups; 1662 } 1663 1664 return (smp); 1665 } 1666 1667 void 1668 sctp_sack(sctp_t *sctp, mblk_t *dups) 1669 { 1670 mblk_t *smp; 1671 sctp_stack_t *sctps = sctp->sctp_sctps; 1672 1673 /* If we are shutting down, let send_shutdown() bundle the SACK */ 1674 if (sctp->sctp_state == SCTPS_SHUTDOWN_SENT) { 1675 sctp_send_shutdown(sctp, 0); 1676 } 1677 1678 ASSERT(sctp->sctp_lastdata != NULL); 1679 1680 if ((smp = sctp_make_sack(sctp, sctp->sctp_lastdata, dups)) == NULL) { 1681 /* The caller of sctp_sack() will not free the dups mblk. */ 1682 if (dups != NULL) 1683 freeb(dups); 1684 return; 1685 } 1686 1687 sctp_set_iplen(sctp, smp); 1688 1689 dprint(2, ("sctp_sack: sending to %p %x:%x:%x:%x\n", 1690 (void *)sctp->sctp_lastdata, 1691 SCTP_PRINTADDR(sctp->sctp_lastdata->faddr))); 1692 1693 sctp->sctp_active = lbolt64; 1694 1695 BUMP_MIB(&sctps->sctps_mib, sctpOutAck); 1696 sctp_add_sendq(sctp, smp); 1697 } 1698 1699 /* 1700 * This is called if we have a message that was partially sent and is 1701 * abandoned. The cum TSN will be the last chunk sent for this message, 1702 * subsequent chunks will be marked ABANDONED. We send a Forward TSN 1703 * chunk in this case with the TSN of the last sent chunk so that the 1704 * peer can clean up its fragment list for this message. This message 1705 * will be removed from the transmit list when the peer sends a SACK 1706 * back. 1707 */ 1708 int 1709 sctp_check_abandoned_msg(sctp_t *sctp, mblk_t *meta) 1710 { 1711 sctp_data_hdr_t *dh; 1712 mblk_t *nmp; 1713 mblk_t *head; 1714 int32_t unsent = 0; 1715 mblk_t *mp1 = meta->b_cont; 1716 uint32_t adv_pap = sctp->sctp_adv_pap; 1717 sctp_faddr_t *fp = sctp->sctp_current; 1718 sctp_stack_t *sctps = sctp->sctp_sctps; 1719 1720 dh = (sctp_data_hdr_t *)mp1->b_rptr; 1721 if (SEQ_GEQ(sctp->sctp_lastack_rxd, ntohl(dh->sdh_tsn))) { 1722 sctp_ftsn_set_t *sets = NULL; 1723 uint_t nsets = 0; 1724 uint32_t seglen = sizeof (uint32_t); 1725 boolean_t ubit = SCTP_DATA_GET_UBIT(dh); 1726 1727 while (mp1->b_next != NULL && SCTP_CHUNK_ISSENT(mp1->b_next)) 1728 mp1 = mp1->b_next; 1729 dh = (sctp_data_hdr_t *)mp1->b_rptr; 1730 sctp->sctp_adv_pap = ntohl(dh->sdh_tsn); 1731 if (!ubit && 1732 !sctp_add_ftsn_set(&sets, fp, meta, &nsets, &seglen)) { 1733 sctp->sctp_adv_pap = adv_pap; 1734 return (ENOMEM); 1735 } 1736 nmp = sctp_make_ftsn_chunk(sctp, fp, sets, nsets, seglen); 1737 sctp_free_ftsn_set(sets); 1738 if (nmp == NULL) { 1739 sctp->sctp_adv_pap = adv_pap; 1740 return (ENOMEM); 1741 } 1742 head = sctp_add_proto_hdr(sctp, fp, nmp, 0, NULL); 1743 if (head == NULL) { 1744 sctp->sctp_adv_pap = adv_pap; 1745 freemsg(nmp); 1746 SCTP_KSTAT(sctps, sctp_send_ftsn_failed); 1747 return (ENOMEM); 1748 } 1749 SCTP_MSG_SET_ABANDONED(meta); 1750 sctp_set_iplen(sctp, head); 1751 sctp_add_sendq(sctp, head); 1752 if (!fp->timer_running) 1753 SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto); 1754 mp1 = mp1->b_next; 1755 while (mp1 != NULL) { 1756 ASSERT(!SCTP_CHUNK_ISSENT(mp1)); 1757 ASSERT(!SCTP_CHUNK_ABANDONED(mp1)); 1758 SCTP_ABANDON_CHUNK(mp1); 1759 dh = (sctp_data_hdr_t *)mp1->b_rptr; 1760 unsent += ntohs(dh->sdh_len) - sizeof (*dh); 1761 mp1 = mp1->b_next; 1762 } 1763 ASSERT(sctp->sctp_unsent >= unsent); 1764 sctp->sctp_unsent -= unsent; 1765 /* 1766 * Update ULP the amount of queued data, which is 1767 * sent-unack'ed + unsent. 1768 */ 1769 if (!SCTP_IS_DETACHED(sctp)) { 1770 sctp->sctp_ulp_xmitted(sctp->sctp_ulpd, 1771 sctp->sctp_unacked + sctp->sctp_unsent); 1772 } 1773 return (0); 1774 } 1775 return (-1); 1776 } 1777 1778 uint32_t 1779 sctp_cumack(sctp_t *sctp, uint32_t tsn, mblk_t **first_unacked) 1780 { 1781 mblk_t *ump, *nump, *mp = NULL; 1782 uint16_t chunklen; 1783 uint32_t xtsn; 1784 sctp_faddr_t *fp; 1785 sctp_data_hdr_t *sdc; 1786 uint32_t cumack_forward = 0; 1787 sctp_msg_hdr_t *mhdr; 1788 sctp_stack_t *sctps = sctp->sctp_sctps; 1789 1790 ump = sctp->sctp_xmit_head; 1791 1792 /* 1793 * Free messages only when they're completely acked. 1794 */ 1795 while (ump != NULL) { 1796 mhdr = (sctp_msg_hdr_t *)ump->b_rptr; 1797 for (mp = ump->b_cont; mp != NULL; mp = mp->b_next) { 1798 if (SCTP_CHUNK_ABANDONED(mp)) { 1799 ASSERT(SCTP_IS_MSG_ABANDONED(ump)); 1800 mp = NULL; 1801 break; 1802 } 1803 /* 1804 * We check for abandoned message if we are PR-SCTP 1805 * aware, if this is not the first chunk in the 1806 * message (b_cont) and if the message is marked 1807 * abandoned. 1808 */ 1809 if (!SCTP_CHUNK_ISSENT(mp)) { 1810 if (sctp->sctp_prsctp_aware && 1811 mp != ump->b_cont && 1812 (SCTP_IS_MSG_ABANDONED(ump) || 1813 SCTP_MSG_TO_BE_ABANDONED(ump, mhdr, 1814 sctp))) { 1815 (void) sctp_check_abandoned_msg(sctp, 1816 ump); 1817 } 1818 goto cum_ack_done; 1819 } 1820 sdc = (sctp_data_hdr_t *)mp->b_rptr; 1821 xtsn = ntohl(sdc->sdh_tsn); 1822 if (SEQ_GEQ(sctp->sctp_lastack_rxd, xtsn)) 1823 continue; 1824 if (SEQ_GEQ(tsn, xtsn)) { 1825 fp = SCTP_CHUNK_DEST(mp); 1826 chunklen = ntohs(sdc->sdh_len); 1827 1828 if (sctp->sctp_out_time != 0 && 1829 xtsn == sctp->sctp_rtt_tsn) { 1830 /* Got a new RTT measurement */ 1831 sctp_update_rtt(sctp, fp, 1832 lbolt64 - sctp->sctp_out_time); 1833 sctp->sctp_out_time = 0; 1834 } 1835 if (SCTP_CHUNK_ISACKED(mp)) 1836 continue; 1837 SCTP_CHUNK_SET_SACKCNT(mp, 0); 1838 SCTP_CHUNK_ACKED(mp); 1839 ASSERT(fp->suna >= chunklen); 1840 fp->suna -= chunklen; 1841 fp->acked += chunklen; 1842 cumack_forward += chunklen; 1843 ASSERT(sctp->sctp_unacked >= 1844 (chunklen - sizeof (*sdc))); 1845 sctp->sctp_unacked -= 1846 (chunklen - sizeof (*sdc)); 1847 if (fp->suna == 0) { 1848 /* all outstanding data acked */ 1849 fp->pba = 0; 1850 SCTP_FADDR_TIMER_STOP(fp); 1851 } else { 1852 SCTP_FADDR_TIMER_RESTART(sctp, fp, 1853 fp->rto); 1854 } 1855 } else { 1856 goto cum_ack_done; 1857 } 1858 } 1859 nump = ump->b_next; 1860 if (nump != NULL) 1861 nump->b_prev = NULL; 1862 if (ump == sctp->sctp_xmit_tail) 1863 sctp->sctp_xmit_tail = nump; 1864 if (SCTP_IS_MSG_ABANDONED(ump)) { 1865 BUMP_LOCAL(sctp->sctp_prsctpdrop); 1866 ump->b_next = NULL; 1867 sctp_sendfail_event(sctp, ump, 0, B_TRUE); 1868 } else { 1869 sctp_free_msg(ump); 1870 } 1871 sctp->sctp_xmit_head = ump = nump; 1872 } 1873 cum_ack_done: 1874 *first_unacked = mp; 1875 if (cumack_forward > 0) { 1876 BUMP_MIB(&sctps->sctps_mib, sctpInAck); 1877 if (SEQ_GT(sctp->sctp_lastack_rxd, sctp->sctp_recovery_tsn)) { 1878 sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd; 1879 } 1880 1881 /* 1882 * Update ULP the amount of queued data, which is 1883 * sent-unack'ed + unsent. 1884 */ 1885 if (!SCTP_IS_DETACHED(sctp)) { 1886 sctp->sctp_ulp_xmitted(sctp->sctp_ulpd, 1887 sctp->sctp_unacked + sctp->sctp_unsent); 1888 } 1889 1890 /* Time to send a shutdown? */ 1891 if (sctp->sctp_state == SCTPS_SHUTDOWN_PENDING) { 1892 sctp_send_shutdown(sctp, 0); 1893 } 1894 sctp->sctp_xmit_unacked = mp; 1895 } else { 1896 /* dup ack */ 1897 BUMP_MIB(&sctps->sctps_mib, sctpInDupAck); 1898 } 1899 sctp->sctp_lastack_rxd = tsn; 1900 if (SEQ_LT(sctp->sctp_adv_pap, sctp->sctp_lastack_rxd)) 1901 sctp->sctp_adv_pap = sctp->sctp_lastack_rxd; 1902 ASSERT(sctp->sctp_xmit_head || sctp->sctp_unacked == 0); 1903 1904 return (cumack_forward); 1905 } 1906 1907 static int 1908 sctp_set_frwnd(sctp_t *sctp, uint32_t frwnd) 1909 { 1910 uint32_t orwnd; 1911 1912 if (sctp->sctp_unacked > frwnd) { 1913 sctp->sctp_frwnd = 0; 1914 return (0); 1915 } 1916 orwnd = sctp->sctp_frwnd; 1917 sctp->sctp_frwnd = frwnd - sctp->sctp_unacked; 1918 if (orwnd < sctp->sctp_frwnd) { 1919 return (1); 1920 } else { 1921 return (0); 1922 } 1923 } 1924 1925 /* 1926 * For un-ordered messages. 1927 * Walk the sctp->sctp_uo_frag list and remove any fragments with TSN 1928 * less than/equal to ftsn. Fragments for un-ordered messages are 1929 * strictly in sequence (w.r.t TSN). 1930 */ 1931 static int 1932 sctp_ftsn_check_uo_frag(sctp_t *sctp, uint32_t ftsn) 1933 { 1934 mblk_t *hmp; 1935 mblk_t *hmp_next; 1936 sctp_data_hdr_t *dc; 1937 int dlen = 0; 1938 1939 hmp = sctp->sctp_uo_frags; 1940 while (hmp != NULL) { 1941 hmp_next = hmp->b_next; 1942 dc = (sctp_data_hdr_t *)hmp->b_rptr; 1943 if (SEQ_GT(ntohl(dc->sdh_tsn), ftsn)) 1944 return (dlen); 1945 sctp->sctp_uo_frags = hmp_next; 1946 if (hmp_next != NULL) 1947 hmp_next->b_prev = NULL; 1948 hmp->b_next = NULL; 1949 dlen += ntohs(dc->sdh_len) - sizeof (*dc); 1950 freeb(hmp); 1951 hmp = hmp_next; 1952 } 1953 return (dlen); 1954 } 1955 1956 /* 1957 * For ordered messages. 1958 * Check for existing fragments for an sid-ssn pair reported as abandoned, 1959 * hence will not receive, in the Forward TSN. If there are fragments, then 1960 * we just nuke them. If and when Partial Delivery API is supported, we 1961 * would need to send a notification to the upper layer about this. 1962 */ 1963 static int 1964 sctp_ftsn_check_frag(sctp_t *sctp, uint16_t ssn, sctp_instr_t *sip) 1965 { 1966 sctp_reass_t *srp; 1967 mblk_t *hmp; 1968 mblk_t *dmp; 1969 mblk_t *hmp_next; 1970 sctp_data_hdr_t *dc; 1971 int dlen = 0; 1972 1973 hmp = sip->istr_reass; 1974 while (hmp != NULL) { 1975 hmp_next = hmp->b_next; 1976 srp = (sctp_reass_t *)DB_BASE(hmp); 1977 if (SSN_GT(srp->ssn, ssn)) 1978 return (dlen); 1979 /* 1980 * If we had sent part of this message up, send a partial 1981 * delivery event. Since this is ordered delivery, we should 1982 * have sent partial message only for the next in sequence, 1983 * hence the ASSERT. See comments in sctp_data_chunk() for 1984 * trypartial. 1985 */ 1986 if (srp->partial_delivered) { 1987 ASSERT(sip->nextseq == srp->ssn); 1988 sctp_partial_delivery_event(sctp); 1989 } 1990 /* Take it out of the reass queue */ 1991 sip->istr_reass = hmp_next; 1992 if (hmp_next != NULL) 1993 hmp_next->b_prev = NULL; 1994 hmp->b_next = NULL; 1995 ASSERT(hmp->b_prev == NULL); 1996 dmp = hmp; 1997 ASSERT(DB_TYPE(hmp) == M_CTL); 1998 dmp = hmp->b_cont; 1999 hmp->b_cont = NULL; 2000 freeb(hmp); 2001 hmp = dmp; 2002 while (dmp != NULL) { 2003 dc = (sctp_data_hdr_t *)dmp->b_rptr; 2004 dlen += ntohs(dc->sdh_len) - sizeof (*dc); 2005 dmp = dmp->b_cont; 2006 } 2007 freemsg(hmp); 2008 hmp = hmp_next; 2009 } 2010 return (dlen); 2011 } 2012 2013 /* 2014 * Update sctp_ftsn to the cumulative TSN from the Forward TSN chunk. Remove 2015 * any SACK gaps less than the newly updated sctp_ftsn. Walk through the 2016 * sid-ssn pair in the Forward TSN and for each, clean the fragment list 2017 * for this pair, if needed, and check if we can deliver subsequent 2018 * messages, if any, from the instream queue (that were waiting for this 2019 * sid-ssn message to show up). Once we are done try to update the SACK 2020 * info. We could get a duplicate Forward TSN, in which case just send 2021 * a SACK. If any of the sid values in the the Forward TSN is invalid, 2022 * send back an "Invalid Stream Identifier" error and continue processing 2023 * the rest. 2024 */ 2025 static void 2026 sctp_process_forward_tsn(sctp_t *sctp, sctp_chunk_hdr_t *ch, sctp_faddr_t *fp, 2027 ip6_pkt_t *ipp) 2028 { 2029 uint32_t *ftsn = (uint32_t *)(ch + 1); 2030 ftsn_entry_t *ftsn_entry; 2031 sctp_instr_t *instr; 2032 boolean_t can_deliver = B_TRUE; 2033 size_t dlen; 2034 int flen; 2035 mblk_t *dmp; 2036 mblk_t *pmp; 2037 sctp_data_hdr_t *dc; 2038 ssize_t remaining; 2039 sctp_stack_t *sctps = sctp->sctp_sctps; 2040 2041 *ftsn = ntohl(*ftsn); 2042 remaining = ntohs(ch->sch_len) - sizeof (*ch) - sizeof (*ftsn); 2043 2044 if (SCTP_IS_DETACHED(sctp)) { 2045 BUMP_MIB(&sctps->sctps_mib, sctpInClosed); 2046 can_deliver = B_FALSE; 2047 } 2048 /* 2049 * un-ordered messages don't have SID-SSN pair entries, we check 2050 * for any fragments (for un-ordered message) to be discarded using 2051 * the cumulative FTSN. 2052 */ 2053 flen = sctp_ftsn_check_uo_frag(sctp, *ftsn); 2054 if (flen > 0) { 2055 ASSERT(sctp->sctp_rxqueued >= flen); 2056 sctp->sctp_rxqueued -= flen; 2057 } 2058 ftsn_entry = (ftsn_entry_t *)(ftsn + 1); 2059 while (remaining >= sizeof (*ftsn_entry)) { 2060 ftsn_entry->ftsn_sid = ntohs(ftsn_entry->ftsn_sid); 2061 ftsn_entry->ftsn_ssn = ntohs(ftsn_entry->ftsn_ssn); 2062 if (ftsn_entry->ftsn_sid >= sctp->sctp_num_istr) { 2063 uint16_t inval_parm[2]; 2064 mblk_t *errmp; 2065 2066 inval_parm[0] = htons(ftsn_entry->ftsn_sid); 2067 /* RESERVED to be ignored at the receiving end */ 2068 inval_parm[1] = 0; 2069 errmp = sctp_make_err(sctp, SCTP_ERR_BAD_SID, 2070 (char *)inval_parm, sizeof (inval_parm)); 2071 if (errmp != NULL) 2072 sctp_send_err(sctp, errmp, NULL); 2073 ftsn_entry++; 2074 remaining -= sizeof (*ftsn_entry); 2075 continue; 2076 } 2077 instr = &sctp->sctp_instr[ftsn_entry->ftsn_sid]; 2078 flen = sctp_ftsn_check_frag(sctp, ftsn_entry->ftsn_ssn, instr); 2079 /* Indicates frags were nuked, update rxqueued */ 2080 if (flen > 0) { 2081 ASSERT(sctp->sctp_rxqueued >= flen); 2082 sctp->sctp_rxqueued -= flen; 2083 } 2084 /* 2085 * It is possible to receive an FTSN chunk with SSN smaller 2086 * than then nextseq if this chunk is a retransmission because 2087 * of incomplete processing when it was first processed. 2088 */ 2089 if (SSN_GE(ftsn_entry->ftsn_ssn, instr->nextseq)) 2090 instr->nextseq = ftsn_entry->ftsn_ssn + 1; 2091 while (instr->istr_nmsgs > 0) { 2092 mblk_t *next; 2093 2094 dmp = (mblk_t *)instr->istr_msgs; 2095 dc = (sctp_data_hdr_t *)dmp->b_rptr; 2096 if (ntohs(dc->sdh_ssn) != instr->nextseq) 2097 break; 2098 2099 next = dmp->b_next; 2100 dlen = dmp->b_wptr - dmp->b_rptr - sizeof (*dc); 2101 for (pmp = dmp->b_cont; pmp != NULL; 2102 pmp = pmp->b_cont) { 2103 dlen += pmp->b_wptr - pmp->b_rptr; 2104 } 2105 if (can_deliver) { 2106 int32_t nrwnd; 2107 2108 dmp->b_rptr = (uchar_t *)(dc + 1); 2109 dmp->b_next = NULL; 2110 ASSERT(dmp->b_prev == NULL); 2111 if (sctp_input_add_ancillary(sctp, 2112 &dmp, dc, fp, ipp) == 0) { 2113 sctp->sctp_rxqueued -= dlen; 2114 sctp->sctp_rwnd -= dlen; 2115 nrwnd = sctp->sctp_ulp_recv( 2116 sctp->sctp_ulpd, dmp, 0); 2117 if (nrwnd > sctp->sctp_rwnd) 2118 sctp->sctp_rwnd = nrwnd; 2119 } else { 2120 /* 2121 * We will resume processing when 2122 * the FTSN chunk is re-xmitted. 2123 */ 2124 dmp->b_rptr = (uchar_t *)dc; 2125 dmp->b_next = next; 2126 dprint(0, 2127 ("FTSN dequeuing %u failed\n", 2128 ntohs(dc->sdh_ssn))); 2129 return; 2130 } 2131 } else { 2132 sctp->sctp_rxqueued -= dlen; 2133 ASSERT(dmp->b_prev == NULL); 2134 dmp->b_next = NULL; 2135 freemsg(dmp); 2136 } 2137 instr->istr_nmsgs--; 2138 instr->nextseq++; 2139 sctp->sctp_istr_nmsgs--; 2140 if (next != NULL) 2141 next->b_prev = NULL; 2142 instr->istr_msgs = next; 2143 } 2144 ftsn_entry++; 2145 remaining -= sizeof (*ftsn_entry); 2146 } 2147 /* Duplicate FTSN */ 2148 if (*ftsn <= (sctp->sctp_ftsn - 1)) { 2149 sctp->sctp_force_sack = 1; 2150 return; 2151 } 2152 /* Advance cum TSN to that reported in the Forward TSN chunk */ 2153 sctp->sctp_ftsn = *ftsn + 1; 2154 2155 /* Remove all the SACK gaps before the new cum TSN */ 2156 if (sctp->sctp_sack_info != NULL) { 2157 sctp_ack_rem(&sctp->sctp_sack_info, sctp->sctp_ftsn - 1, 2158 &sctp->sctp_sack_gaps); 2159 } 2160 /* 2161 * If there are gap reports pending, check if advancing 2162 * the ftsn here closes a gap. If so, we can advance 2163 * ftsn to the end of the set. 2164 * If ftsn has moved forward, maybe we can remove gap reports. 2165 */ 2166 if (sctp->sctp_sack_info != NULL && 2167 sctp->sctp_ftsn == sctp->sctp_sack_info->begin) { 2168 sctp->sctp_ftsn = sctp->sctp_sack_info->end + 1; 2169 sctp_ack_rem(&sctp->sctp_sack_info, sctp->sctp_ftsn - 1, 2170 &sctp->sctp_sack_gaps); 2171 } 2172 } 2173 2174 /* 2175 * When we have processed a SACK we check to see if we can advance the 2176 * cumulative TSN if there are abandoned chunks immediately following 2177 * the updated cumulative TSN. If there are, we attempt to send a 2178 * Forward TSN chunk. 2179 */ 2180 static void 2181 sctp_check_abandoned_data(sctp_t *sctp, sctp_faddr_t *fp) 2182 { 2183 mblk_t *meta = sctp->sctp_xmit_head; 2184 mblk_t *mp; 2185 mblk_t *nmp; 2186 uint32_t seglen; 2187 uint32_t adv_pap = sctp->sctp_adv_pap; 2188 2189 /* 2190 * We only check in the first meta since otherwise we can't 2191 * advance the cumulative ack point. We just look for chunks 2192 * marked for retransmission, else we might prematurely 2193 * send an FTSN for a sent, but unacked, chunk. 2194 */ 2195 for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) { 2196 if (!SCTP_CHUNK_ISSENT(mp)) 2197 return; 2198 if (SCTP_CHUNK_WANT_REXMIT(mp)) 2199 break; 2200 } 2201 if (mp == NULL) 2202 return; 2203 sctp_check_adv_ack_pt(sctp, meta, mp); 2204 if (SEQ_GT(sctp->sctp_adv_pap, adv_pap)) { 2205 sctp_make_ftsns(sctp, meta, mp, &nmp, fp, &seglen); 2206 if (nmp == NULL) { 2207 sctp->sctp_adv_pap = adv_pap; 2208 if (!fp->timer_running) 2209 SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto); 2210 return; 2211 } 2212 sctp_set_iplen(sctp, nmp); 2213 sctp_add_sendq(sctp, nmp); 2214 if (!fp->timer_running) 2215 SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto); 2216 } 2217 } 2218 2219 /* 2220 * The processing here follows the same logic in sctp_got_sack(), the reason 2221 * we do this separately is because, usually, gap blocks are ordered and 2222 * we can process it in sctp_got_sack(). However if they aren't we would 2223 * need to do some additional non-optimal stuff when we start processing the 2224 * unordered gaps. To that effect sctp_got_sack() does the processing in the 2225 * simple case and this does the same in the more involved case. 2226 */ 2227 static uint32_t 2228 sctp_process_uo_gaps(sctp_t *sctp, uint32_t ctsn, sctp_sack_frag_t *ssf, 2229 int num_gaps, mblk_t *umphead, mblk_t *mphead, int *trysend, 2230 boolean_t *fast_recovery, uint32_t fr_xtsn) 2231 { 2232 uint32_t xtsn; 2233 uint32_t gapstart = 0; 2234 uint32_t gapend = 0; 2235 int gapcnt; 2236 uint16_t chunklen; 2237 sctp_data_hdr_t *sdc; 2238 int gstart; 2239 mblk_t *ump = umphead; 2240 mblk_t *mp = mphead; 2241 sctp_faddr_t *fp; 2242 uint32_t acked = 0; 2243 sctp_stack_t *sctps = sctp->sctp_sctps; 2244 2245 /* 2246 * gstart tracks the last (in the order of TSN) gapstart that 2247 * we process in this SACK gaps walk. 2248 */ 2249 gstart = ctsn; 2250 2251 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2252 xtsn = ntohl(sdc->sdh_tsn); 2253 for (gapcnt = 0; gapcnt < num_gaps; gapcnt++, ssf++) { 2254 if (gapstart != 0) { 2255 /* 2256 * If we have reached the end of the transmit list or 2257 * hit an unsent chunk or encountered an unordered gap 2258 * block start from the ctsn again. 2259 */ 2260 if (ump == NULL || !SCTP_CHUNK_ISSENT(mp) || 2261 SEQ_LT(ctsn + ntohs(ssf->ssf_start), xtsn)) { 2262 ump = umphead; 2263 mp = mphead; 2264 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2265 xtsn = ntohl(sdc->sdh_tsn); 2266 } 2267 } 2268 2269 gapstart = ctsn + ntohs(ssf->ssf_start); 2270 gapend = ctsn + ntohs(ssf->ssf_end); 2271 2272 /* SACK for TSN we have not sent - ABORT */ 2273 if (SEQ_GT(gapstart, sctp->sctp_ltsn - 1) || 2274 SEQ_GT(gapend, sctp->sctp_ltsn - 1)) { 2275 BUMP_MIB(&sctps->sctps_mib, sctpInAckUnsent); 2276 *trysend = -1; 2277 return (acked); 2278 } else if (SEQ_LT(gapend, gapstart)) { 2279 break; 2280 } 2281 /* 2282 * The xtsn can be the TSN processed for the last gap 2283 * (gapend) or it could be the cumulative TSN. We continue 2284 * with the last xtsn as long as the gaps are ordered, when 2285 * we hit an unordered gap, we re-start from the cumulative 2286 * TSN. For the first gap it is always the cumulative TSN. 2287 */ 2288 while (xtsn != gapstart) { 2289 /* 2290 * We can't reliably check for reneged chunks 2291 * when walking the unordered list, so we don't. 2292 * In case the peer reneges then we will end up 2293 * sending the reneged chunk via timeout. 2294 */ 2295 mp = mp->b_next; 2296 if (mp == NULL) { 2297 ump = ump->b_next; 2298 /* 2299 * ump can't be NULL because of the sanity 2300 * check above. 2301 */ 2302 ASSERT(ump != NULL); 2303 mp = ump->b_cont; 2304 } 2305 /* 2306 * mp can't be unsent because of the sanity check 2307 * above. 2308 */ 2309 ASSERT(SCTP_CHUNK_ISSENT(mp)); 2310 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2311 xtsn = ntohl(sdc->sdh_tsn); 2312 } 2313 /* 2314 * Now that we have found the chunk with TSN == 'gapstart', 2315 * let's walk till we hit the chunk with TSN == 'gapend'. 2316 * All intermediate chunks will be marked ACKED, if they 2317 * haven't already been. 2318 */ 2319 while (SEQ_LEQ(xtsn, gapend)) { 2320 /* 2321 * SACKed 2322 */ 2323 SCTP_CHUNK_SET_SACKCNT(mp, 0); 2324 if (!SCTP_CHUNK_ISACKED(mp)) { 2325 SCTP_CHUNK_ACKED(mp); 2326 2327 fp = SCTP_CHUNK_DEST(mp); 2328 chunklen = ntohs(sdc->sdh_len); 2329 ASSERT(fp->suna >= chunklen); 2330 fp->suna -= chunklen; 2331 if (fp->suna == 0) { 2332 /* All outstanding data acked. */ 2333 fp->pba = 0; 2334 SCTP_FADDR_TIMER_STOP(fp); 2335 } 2336 fp->acked += chunklen; 2337 acked += chunklen; 2338 sctp->sctp_unacked -= chunklen - sizeof (*sdc); 2339 ASSERT(sctp->sctp_unacked >= 0); 2340 } 2341 /* 2342 * Move to the next message in the transmit list 2343 * if we are done with all the chunks from the current 2344 * message. Note, it is possible to hit the end of the 2345 * transmit list here, i.e. if we have already completed 2346 * processing the gap block. 2347 */ 2348 mp = mp->b_next; 2349 if (mp == NULL) { 2350 ump = ump->b_next; 2351 if (ump == NULL) { 2352 ASSERT(xtsn == gapend); 2353 break; 2354 } 2355 mp = ump->b_cont; 2356 } 2357 /* 2358 * Likewise, we can hit an unsent chunk once we have 2359 * completed processing the gap block. 2360 */ 2361 if (!SCTP_CHUNK_ISSENT(mp)) { 2362 ASSERT(xtsn == gapend); 2363 break; 2364 } 2365 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2366 xtsn = ntohl(sdc->sdh_tsn); 2367 } 2368 /* 2369 * We keep track of the last gap we successfully processed 2370 * so that we can terminate the walk below for incrementing 2371 * the SACK count. 2372 */ 2373 if (SEQ_LT(gstart, gapstart)) 2374 gstart = gapstart; 2375 } 2376 /* 2377 * Check if have incremented the SACK count for all unacked TSNs in 2378 * sctp_got_sack(), if so we are done. 2379 */ 2380 if (SEQ_LEQ(gstart, fr_xtsn)) 2381 return (acked); 2382 2383 ump = umphead; 2384 mp = mphead; 2385 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2386 xtsn = ntohl(sdc->sdh_tsn); 2387 while (SEQ_LT(xtsn, gstart)) { 2388 /* 2389 * We have incremented SACK count for TSNs less than fr_tsn 2390 * in sctp_got_sack(), so don't increment them again here. 2391 */ 2392 if (SEQ_GT(xtsn, fr_xtsn) && !SCTP_CHUNK_ISACKED(mp)) { 2393 SCTP_CHUNK_SET_SACKCNT(mp, SCTP_CHUNK_SACKCNT(mp) + 1); 2394 if (SCTP_CHUNK_SACKCNT(mp) == 2395 sctps->sctps_fast_rxt_thresh) { 2396 SCTP_CHUNK_REXMIT(mp); 2397 sctp->sctp_chk_fast_rexmit = B_TRUE; 2398 *trysend = 1; 2399 if (!*fast_recovery) { 2400 /* 2401 * Entering fast recovery. 2402 */ 2403 fp = SCTP_CHUNK_DEST(mp); 2404 fp->ssthresh = fp->cwnd / 2; 2405 if (fp->ssthresh < 2 * fp->sfa_pmss) { 2406 fp->ssthresh = 2407 2 * fp->sfa_pmss; 2408 } 2409 fp->cwnd = fp->ssthresh; 2410 fp->pba = 0; 2411 sctp->sctp_recovery_tsn = 2412 sctp->sctp_ltsn - 1; 2413 *fast_recovery = B_TRUE; 2414 } 2415 } 2416 } 2417 mp = mp->b_next; 2418 if (mp == NULL) { 2419 ump = ump->b_next; 2420 /* We can't get to the end of the transmit list here */ 2421 ASSERT(ump != NULL); 2422 mp = ump->b_cont; 2423 } 2424 /* We can't hit an unsent chunk here */ 2425 ASSERT(SCTP_CHUNK_ISSENT(mp)); 2426 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2427 xtsn = ntohl(sdc->sdh_tsn); 2428 } 2429 return (acked); 2430 } 2431 2432 static int 2433 sctp_got_sack(sctp_t *sctp, sctp_chunk_hdr_t *sch) 2434 { 2435 sctp_sack_chunk_t *sc; 2436 sctp_data_hdr_t *sdc; 2437 sctp_sack_frag_t *ssf; 2438 mblk_t *ump; 2439 mblk_t *mp; 2440 mblk_t *mp1; 2441 uint32_t cumtsn; 2442 uint32_t xtsn; 2443 uint32_t gapstart = 0; 2444 uint32_t gapend = 0; 2445 uint32_t acked = 0; 2446 uint16_t chunklen; 2447 sctp_faddr_t *fp; 2448 int num_gaps; 2449 int trysend = 0; 2450 int i; 2451 boolean_t fast_recovery = B_FALSE; 2452 boolean_t cumack_forward = B_FALSE; 2453 boolean_t fwd_tsn = B_FALSE; 2454 sctp_stack_t *sctps = sctp->sctp_sctps; 2455 2456 BUMP_LOCAL(sctp->sctp_ibchunks); 2457 chunklen = ntohs(sch->sch_len); 2458 if (chunklen < (sizeof (*sch) + sizeof (*sc))) 2459 return (0); 2460 2461 sc = (sctp_sack_chunk_t *)(sch + 1); 2462 cumtsn = ntohl(sc->ssc_cumtsn); 2463 2464 dprint(2, ("got sack cumtsn %x -> %x\n", sctp->sctp_lastack_rxd, 2465 cumtsn)); 2466 2467 /* out of order */ 2468 if (SEQ_LT(cumtsn, sctp->sctp_lastack_rxd)) 2469 return (0); 2470 2471 if (SEQ_GT(cumtsn, sctp->sctp_ltsn - 1)) { 2472 BUMP_MIB(&sctps->sctps_mib, sctpInAckUnsent); 2473 /* Send an ABORT */ 2474 return (-1); 2475 } 2476 2477 /* 2478 * Cwnd only done when not in fast recovery mode. 2479 */ 2480 if (SEQ_LT(sctp->sctp_lastack_rxd, sctp->sctp_recovery_tsn)) 2481 fast_recovery = B_TRUE; 2482 2483 /* 2484 * .. and if the cum TSN is not moving ahead on account Forward TSN 2485 */ 2486 if (SEQ_LT(sctp->sctp_lastack_rxd, sctp->sctp_adv_pap)) 2487 fwd_tsn = B_TRUE; 2488 2489 if (cumtsn == sctp->sctp_lastack_rxd && 2490 (sctp->sctp_xmit_unacked == NULL || 2491 !SCTP_CHUNK_ABANDONED(sctp->sctp_xmit_unacked))) { 2492 if (sctp->sctp_xmit_unacked != NULL) 2493 mp = sctp->sctp_xmit_unacked; 2494 else if (sctp->sctp_xmit_head != NULL) 2495 mp = sctp->sctp_xmit_head->b_cont; 2496 else 2497 mp = NULL; 2498 BUMP_MIB(&sctps->sctps_mib, sctpInDupAck); 2499 /* 2500 * If we were doing a zero win probe and the win 2501 * has now opened to at least MSS, re-transmit the 2502 * zero win probe via sctp_rexmit_packet(). 2503 */ 2504 if (mp != NULL && sctp->sctp_zero_win_probe && 2505 ntohl(sc->ssc_a_rwnd) >= sctp->sctp_current->sfa_pmss) { 2506 mblk_t *pkt; 2507 uint_t pkt_len; 2508 mblk_t *mp1 = mp; 2509 mblk_t *meta = sctp->sctp_xmit_head; 2510 2511 /* 2512 * Reset the RTO since we have been backing-off 2513 * to send the ZWP. 2514 */ 2515 fp = sctp->sctp_current; 2516 fp->rto = fp->srtt + 4 * fp->rttvar; 2517 /* Resend the ZWP */ 2518 pkt = sctp_rexmit_packet(sctp, &meta, &mp1, fp, 2519 &pkt_len); 2520 if (pkt == NULL) { 2521 SCTP_KSTAT(sctps, sctp_ss_rexmit_failed); 2522 return (0); 2523 } 2524 ASSERT(pkt_len <= fp->sfa_pmss); 2525 sctp->sctp_zero_win_probe = B_FALSE; 2526 sctp->sctp_rxt_nxttsn = sctp->sctp_ltsn; 2527 sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn; 2528 sctp_set_iplen(sctp, pkt); 2529 sctp_add_sendq(sctp, pkt); 2530 } 2531 } else { 2532 if (sctp->sctp_zero_win_probe) { 2533 /* 2534 * Reset the RTO since we have been backing-off 2535 * to send the ZWP. 2536 */ 2537 fp = sctp->sctp_current; 2538 fp->rto = fp->srtt + 4 * fp->rttvar; 2539 sctp->sctp_zero_win_probe = B_FALSE; 2540 /* This is probably not required */ 2541 if (!sctp->sctp_rexmitting) { 2542 sctp->sctp_rxt_nxttsn = sctp->sctp_ltsn; 2543 sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn; 2544 } 2545 } 2546 acked = sctp_cumack(sctp, cumtsn, &mp); 2547 sctp->sctp_xmit_unacked = mp; 2548 if (acked > 0) { 2549 trysend = 1; 2550 cumack_forward = B_TRUE; 2551 if (fwd_tsn && SEQ_GEQ(sctp->sctp_lastack_rxd, 2552 sctp->sctp_adv_pap)) { 2553 cumack_forward = B_FALSE; 2554 } 2555 } 2556 } 2557 num_gaps = ntohs(sc->ssc_numfrags); 2558 if (num_gaps == 0 || mp == NULL || !SCTP_CHUNK_ISSENT(mp) || 2559 chunklen < (sizeof (*sch) + sizeof (*sc) + 2560 num_gaps * sizeof (*ssf))) { 2561 goto ret; 2562 } 2563 #ifdef DEBUG 2564 /* 2565 * Since we delete any message that has been acked completely, 2566 * the unacked chunk must belong to sctp_xmit_head (as 2567 * we don't have a back pointer from the mp to the meta data 2568 * we do this). 2569 */ 2570 { 2571 mblk_t *mp2 = sctp->sctp_xmit_head->b_cont; 2572 2573 while (mp2 != NULL) { 2574 if (mp2 == mp) 2575 break; 2576 mp2 = mp2->b_next; 2577 } 2578 ASSERT(mp2 != NULL); 2579 } 2580 #endif 2581 ump = sctp->sctp_xmit_head; 2582 2583 /* 2584 * Just remember where we started from, in case we need to call 2585 * sctp_process_uo_gaps() if the gap blocks are unordered. 2586 */ 2587 mp1 = mp; 2588 2589 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2590 xtsn = ntohl(sdc->sdh_tsn); 2591 ASSERT(xtsn == cumtsn + 1); 2592 2593 /* 2594 * Go through SACK gaps. They are ordered based on start TSN. 2595 */ 2596 ssf = (sctp_sack_frag_t *)(sc + 1); 2597 for (i = 0; i < num_gaps; i++, ssf++) { 2598 if (gapstart != 0) { 2599 /* check for unordered gap */ 2600 if (SEQ_LEQ(cumtsn + ntohs(ssf->ssf_start), gapstart)) { 2601 acked += sctp_process_uo_gaps(sctp, 2602 cumtsn, ssf, num_gaps - i, 2603 sctp->sctp_xmit_head, mp1, 2604 &trysend, &fast_recovery, gapstart); 2605 if (trysend < 0) { 2606 BUMP_MIB(&sctps->sctps_mib, 2607 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(&sctps->sctps_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) == 2642 sctps->sctps_fast_rxt_thresh) { 2643 SCTP_CHUNK_REXMIT(mp); 2644 sctp->sctp_chk_fast_rexmit = B_TRUE; 2645 trysend = 1; 2646 if (!fast_recovery) { 2647 /* 2648 * Entering fast recovery. 2649 */ 2650 fp = SCTP_CHUNK_DEST(mp); 2651 fp->ssthresh = fp->cwnd / 2; 2652 if (fp->ssthresh < 2 * fp->sfa_pmss) { 2653 fp->ssthresh = 2654 2 * fp->sfa_pmss; 2655 } 2656 fp->cwnd = fp->ssthresh; 2657 fp->pba = 0; 2658 sctp->sctp_recovery_tsn = 2659 sctp->sctp_ltsn - 1; 2660 fast_recovery = B_TRUE; 2661 } 2662 } 2663 2664 /* 2665 * Peer may have reneged on this chunk, so un-sack 2666 * it now. If the peer did renege, we need to 2667 * readjust unacked. 2668 */ 2669 if (SCTP_CHUNK_ISACKED(mp)) { 2670 chunklen = ntohs(sdc->sdh_len); 2671 fp = SCTP_CHUNK_DEST(mp); 2672 fp->suna += chunklen; 2673 sctp->sctp_unacked += chunklen - sizeof (*sdc); 2674 SCTP_CHUNK_CLEAR_ACKED(mp); 2675 if (!fp->timer_running) { 2676 SCTP_FADDR_TIMER_RESTART(sctp, fp, 2677 fp->rto); 2678 } 2679 } 2680 2681 mp = mp->b_next; 2682 if (mp == NULL) { 2683 ump = ump->b_next; 2684 /* 2685 * ump can't be NULL given the sanity check 2686 * above. 2687 */ 2688 ASSERT(ump != NULL); 2689 mp = ump->b_cont; 2690 } 2691 /* 2692 * mp can't be unsent given the sanity check above. 2693 */ 2694 ASSERT(SCTP_CHUNK_ISSENT(mp)); 2695 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2696 xtsn = ntohl(sdc->sdh_tsn); 2697 } 2698 /* 2699 * Now that we have found the chunk with TSN == 'gapstart', 2700 * let's walk till we hit the chunk with TSN == 'gapend'. 2701 * All intermediate chunks will be marked ACKED, if they 2702 * haven't already been. 2703 */ 2704 while (SEQ_LEQ(xtsn, gapend)) { 2705 /* 2706 * SACKed 2707 */ 2708 SCTP_CHUNK_SET_SACKCNT(mp, 0); 2709 if (!SCTP_CHUNK_ISACKED(mp)) { 2710 SCTP_CHUNK_ACKED(mp); 2711 2712 fp = SCTP_CHUNK_DEST(mp); 2713 chunklen = ntohs(sdc->sdh_len); 2714 ASSERT(fp->suna >= chunklen); 2715 fp->suna -= chunklen; 2716 if (fp->suna == 0) { 2717 /* All outstanding data acked. */ 2718 fp->pba = 0; 2719 SCTP_FADDR_TIMER_STOP(fp); 2720 } 2721 fp->acked += chunklen; 2722 acked += chunklen; 2723 sctp->sctp_unacked -= chunklen - sizeof (*sdc); 2724 ASSERT(sctp->sctp_unacked >= 0); 2725 } 2726 /* Go to the next chunk of the current message */ 2727 mp = mp->b_next; 2728 /* 2729 * Move to the next message in the transmit list 2730 * if we are done with all the chunks from the current 2731 * message. Note, it is possible to hit the end of the 2732 * transmit list here, i.e. if we have already completed 2733 * processing the gap block. 2734 * Also, note that we break here, which means we 2735 * continue processing gap blocks, if any. In case of 2736 * ordered gap blocks there can't be any following 2737 * this (if there is it will fail the sanity check 2738 * above). In case of un-ordered gap blocks we will 2739 * switch to sctp_process_uo_gaps(). In either case 2740 * it should be fine to continue with NULL ump/mp, 2741 * but we just reset it to xmit_head. 2742 */ 2743 if (mp == NULL) { 2744 ump = ump->b_next; 2745 if (ump == NULL) { 2746 ASSERT(xtsn == gapend); 2747 ump = sctp->sctp_xmit_head; 2748 mp = mp1; 2749 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2750 xtsn = ntohl(sdc->sdh_tsn); 2751 break; 2752 } 2753 mp = ump->b_cont; 2754 } 2755 /* 2756 * Likewise, we could hit an unsent chunk once we have 2757 * completed processing the gap block. Again, it is 2758 * fine to continue processing gap blocks with mp 2759 * pointing to the unsent chunk, because if there 2760 * are more ordered gap blocks, they will fail the 2761 * sanity check, and if there are un-ordered gap blocks, 2762 * we will continue processing in sctp_process_uo_gaps() 2763 * We just reset the mp to the one we started with. 2764 */ 2765 if (!SCTP_CHUNK_ISSENT(mp)) { 2766 ASSERT(xtsn == gapend); 2767 ump = sctp->sctp_xmit_head; 2768 mp = mp1; 2769 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2770 xtsn = ntohl(sdc->sdh_tsn); 2771 break; 2772 } 2773 sdc = (sctp_data_hdr_t *)mp->b_rptr; 2774 xtsn = ntohl(sdc->sdh_tsn); 2775 } 2776 } 2777 if (sctp->sctp_prsctp_aware) 2778 sctp_check_abandoned_data(sctp, sctp->sctp_current); 2779 if (sctp->sctp_chk_fast_rexmit) 2780 sctp_fast_rexmit(sctp); 2781 ret: 2782 trysend += sctp_set_frwnd(sctp, ntohl(sc->ssc_a_rwnd)); 2783 2784 /* 2785 * If receive window is closed while there is unsent data, 2786 * set a timer for doing zero window probes. 2787 */ 2788 if (sctp->sctp_frwnd == 0 && sctp->sctp_unacked == 0 && 2789 sctp->sctp_unsent != 0) { 2790 SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current, 2791 sctp->sctp_current->rto); 2792 } 2793 2794 /* 2795 * Set cwnd for all destinations. 2796 * Congestion window gets increased only when cumulative 2797 * TSN moves forward, we're not in fast recovery, and 2798 * cwnd has been fully utilized (almost fully, need to allow 2799 * some leeway due to non-MSS sized messages). 2800 */ 2801 if (sctp->sctp_current->acked == acked) { 2802 /* 2803 * Fast-path, only data sent to sctp_current got acked. 2804 */ 2805 fp = sctp->sctp_current; 2806 if (cumack_forward && !fast_recovery && 2807 (fp->acked + fp->suna > fp->cwnd - fp->sfa_pmss)) { 2808 if (fp->cwnd < fp->ssthresh) { 2809 /* 2810 * Slow start 2811 */ 2812 if (fp->acked > fp->sfa_pmss) { 2813 fp->cwnd += fp->sfa_pmss; 2814 } else { 2815 fp->cwnd += fp->acked; 2816 } 2817 fp->cwnd = MIN(fp->cwnd, sctp->sctp_cwnd_max); 2818 } else { 2819 /* 2820 * Congestion avoidance 2821 */ 2822 fp->pba += fp->acked; 2823 if (fp->pba >= fp->cwnd) { 2824 fp->pba -= fp->cwnd; 2825 fp->cwnd += fp->sfa_pmss; 2826 fp->cwnd = MIN(fp->cwnd, 2827 sctp->sctp_cwnd_max); 2828 } 2829 } 2830 } 2831 /* 2832 * Limit the burst of transmitted data segments. 2833 */ 2834 if (fp->suna + sctps->sctps_maxburst * fp->sfa_pmss < 2835 fp->cwnd) { 2836 fp->cwnd = fp->suna + sctps->sctps_maxburst * 2837 fp->sfa_pmss; 2838 } 2839 fp->acked = 0; 2840 goto check_ss_rxmit; 2841 } 2842 for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) { 2843 if (cumack_forward && fp->acked && !fast_recovery && 2844 (fp->acked + fp->suna > fp->cwnd - fp->sfa_pmss)) { 2845 if (fp->cwnd < fp->ssthresh) { 2846 if (fp->acked > fp->sfa_pmss) { 2847 fp->cwnd += fp->sfa_pmss; 2848 } else { 2849 fp->cwnd += fp->acked; 2850 } 2851 fp->cwnd = MIN(fp->cwnd, sctp->sctp_cwnd_max); 2852 } else { 2853 fp->pba += fp->acked; 2854 if (fp->pba >= fp->cwnd) { 2855 fp->pba -= fp->cwnd; 2856 fp->cwnd += fp->sfa_pmss; 2857 fp->cwnd = MIN(fp->cwnd, 2858 sctp->sctp_cwnd_max); 2859 } 2860 } 2861 } 2862 if (fp->suna + sctps->sctps_maxburst * fp->sfa_pmss < 2863 fp->cwnd) { 2864 fp->cwnd = fp->suna + sctps->sctps_maxburst * 2865 fp->sfa_pmss; 2866 } 2867 fp->acked = 0; 2868 } 2869 fp = sctp->sctp_current; 2870 check_ss_rxmit: 2871 /* 2872 * If this is a SACK following a timeout, check if there are 2873 * still unacked chunks (sent before the timeout) that we can 2874 * send. 2875 */ 2876 if (sctp->sctp_rexmitting) { 2877 if (SEQ_LT(sctp->sctp_lastack_rxd, sctp->sctp_rxt_maxtsn)) { 2878 /* 2879 * As we are in retransmission phase, we may get a 2880 * SACK which indicates some new chunks are received 2881 * but cum_tsn does not advance. During this 2882 * phase, the other side advances cum_tsn only because 2883 * it receives our retransmitted chunks. Only 2884 * this signals that some chunks are still 2885 * missing. 2886 */ 2887 if (cumack_forward) { 2888 fp->rxt_unacked -= acked; 2889 sctp_ss_rexmit(sctp); 2890 } 2891 } else { 2892 sctp->sctp_rexmitting = B_FALSE; 2893 sctp->sctp_rxt_nxttsn = sctp->sctp_ltsn; 2894 sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn; 2895 fp->rxt_unacked = 0; 2896 } 2897 } 2898 return (trysend); 2899 } 2900 2901 /* 2902 * Returns 0 if the caller should stop processing any more chunks, 2903 * 1 if the caller should skip this chunk and continue processing. 2904 */ 2905 static int 2906 sctp_strange_chunk(sctp_t *sctp, sctp_chunk_hdr_t *ch, sctp_faddr_t *fp) 2907 { 2908 mblk_t *errmp; 2909 size_t len; 2910 2911 BUMP_LOCAL(sctp->sctp_ibchunks); 2912 /* check top two bits for action required */ 2913 if (ch->sch_id & 0x40) { /* also matches 0xc0 */ 2914 len = ntohs(ch->sch_len); 2915 errmp = sctp_make_err(sctp, SCTP_ERR_UNREC_CHUNK, ch, len); 2916 if (errmp != NULL) 2917 sctp_send_err(sctp, errmp, fp); 2918 if ((ch->sch_id & 0xc0) == 0xc0) { 2919 /* skip and continue */ 2920 return (1); 2921 } else { 2922 /* stop processing */ 2923 return (0); 2924 } 2925 } 2926 if (ch->sch_id & 0x80) { 2927 /* skip and continue, no error */ 2928 return (1); 2929 } 2930 /* top two bits are clear; stop processing and no error */ 2931 return (0); 2932 } 2933 2934 /* 2935 * Basic sanity checks on all input chunks and parameters: they must 2936 * be of legitimate size for their purported type, and must follow 2937 * ordering conventions as defined in rfc2960. 2938 * 2939 * Returns 1 if the chunk and all encloded params are legitimate, 2940 * 0 otherwise. 2941 */ 2942 /*ARGSUSED*/ 2943 static int 2944 sctp_check_input(sctp_t *sctp, sctp_chunk_hdr_t *ch, ssize_t len, int first) 2945 { 2946 sctp_parm_hdr_t *ph; 2947 void *p = NULL; 2948 ssize_t clen; 2949 uint16_t ch_len; 2950 2951 ch_len = ntohs(ch->sch_len); 2952 if (ch_len > len) { 2953 return (0); 2954 } 2955 2956 switch (ch->sch_id) { 2957 case CHUNK_DATA: 2958 if (ch_len < sizeof (sctp_data_hdr_t)) { 2959 return (0); 2960 } 2961 return (1); 2962 case CHUNK_INIT: 2963 case CHUNK_INIT_ACK: 2964 { 2965 ssize_t remlen = len; 2966 2967 /* 2968 * INIT and INIT-ACK chunks must not be bundled with 2969 * any other. 2970 */ 2971 if (!first || sctp_next_chunk(ch, &remlen) != NULL || 2972 (ch_len < (sizeof (*ch) + 2973 sizeof (sctp_init_chunk_t)))) { 2974 return (0); 2975 } 2976 /* may have params that need checking */ 2977 p = (char *)(ch + 1) + sizeof (sctp_init_chunk_t); 2978 clen = ch_len - (sizeof (*ch) + 2979 sizeof (sctp_init_chunk_t)); 2980 } 2981 break; 2982 case CHUNK_SACK: 2983 if (ch_len < (sizeof (*ch) + sizeof (sctp_sack_chunk_t))) { 2984 return (0); 2985 } 2986 /* dup and gap reports checked by got_sack() */ 2987 return (1); 2988 case CHUNK_SHUTDOWN: 2989 if (ch_len < (sizeof (*ch) + sizeof (uint32_t))) { 2990 return (0); 2991 } 2992 return (1); 2993 case CHUNK_ABORT: 2994 case CHUNK_ERROR: 2995 if (ch_len < sizeof (*ch)) { 2996 return (0); 2997 } 2998 /* may have params that need checking */ 2999 p = ch + 1; 3000 clen = ch_len - sizeof (*ch); 3001 break; 3002 case CHUNK_ECNE: 3003 case CHUNK_CWR: 3004 case CHUNK_HEARTBEAT: 3005 case CHUNK_HEARTBEAT_ACK: 3006 /* Full ASCONF chunk and parameter checks are in asconf.c */ 3007 case CHUNK_ASCONF: 3008 case CHUNK_ASCONF_ACK: 3009 if (ch_len < sizeof (*ch)) { 3010 return (0); 3011 } 3012 /* heartbeat data checked by process_heartbeat() */ 3013 return (1); 3014 case CHUNK_SHUTDOWN_COMPLETE: 3015 { 3016 ssize_t remlen = len; 3017 3018 /* 3019 * SHUTDOWN-COMPLETE chunk must not be bundled with any 3020 * other 3021 */ 3022 if (!first || sctp_next_chunk(ch, &remlen) != NULL || 3023 ch_len < sizeof (*ch)) { 3024 return (0); 3025 } 3026 } 3027 return (1); 3028 case CHUNK_COOKIE: 3029 case CHUNK_COOKIE_ACK: 3030 case CHUNK_SHUTDOWN_ACK: 3031 if (ch_len < sizeof (*ch) || !first) { 3032 return (0); 3033 } 3034 return (1); 3035 case CHUNK_FORWARD_TSN: 3036 if (ch_len < (sizeof (*ch) + sizeof (uint32_t))) 3037 return (0); 3038 return (1); 3039 default: 3040 return (1); /* handled by strange_chunk() */ 3041 } 3042 3043 /* check and byteorder parameters */ 3044 if (clen <= 0) { 3045 return (1); 3046 } 3047 ASSERT(p != NULL); 3048 3049 ph = p; 3050 while (ph != NULL && clen > 0) { 3051 ch_len = ntohs(ph->sph_len); 3052 if (ch_len > len || ch_len < sizeof (*ph)) { 3053 return (0); 3054 } 3055 ph = sctp_next_parm(ph, &clen); 3056 } 3057 3058 /* All OK */ 3059 return (1); 3060 } 3061 3062 /* ARGSUSED */ 3063 static sctp_hdr_t * 3064 find_sctp_hdrs(mblk_t *mp, in6_addr_t *src, in6_addr_t *dst, 3065 uint_t *ifindex, uint_t *ip_hdr_len, ip6_pkt_t *ipp, ip_pktinfo_t *pinfo) 3066 { 3067 uchar_t *rptr; 3068 ipha_t *ip4h; 3069 ip6_t *ip6h; 3070 mblk_t *mp1; 3071 3072 rptr = mp->b_rptr; 3073 if (IPH_HDR_VERSION(rptr) == IPV4_VERSION) { 3074 *ip_hdr_len = IPH_HDR_LENGTH(rptr); 3075 ip4h = (ipha_t *)rptr; 3076 IN6_IPADDR_TO_V4MAPPED(ip4h->ipha_src, src); 3077 IN6_IPADDR_TO_V4MAPPED(ip4h->ipha_dst, dst); 3078 3079 ipp->ipp_fields |= IPPF_HOPLIMIT; 3080 ipp->ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 3081 if (pinfo != NULL && (pinfo->ip_pkt_flags & IPF_RECVIF)) { 3082 ipp->ipp_fields |= IPPF_IFINDEX; 3083 ipp->ipp_ifindex = pinfo->ip_pkt_ifindex; 3084 } 3085 } else { 3086 ASSERT(IPH_HDR_VERSION(rptr) == IPV6_VERSION); 3087 ip6h = (ip6_t *)rptr; 3088 ipp->ipp_fields = IPPF_HOPLIMIT; 3089 ipp->ipp_hoplimit = ip6h->ip6_hops; 3090 3091 if (ip6h->ip6_nxt != IPPROTO_SCTP) { 3092 /* Look for ifindex information */ 3093 if (ip6h->ip6_nxt == IPPROTO_RAW) { 3094 ip6i_t *ip6i = (ip6i_t *)ip6h; 3095 3096 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 3097 ASSERT(ip6i->ip6i_ifindex != 0); 3098 ipp->ipp_fields |= IPPF_IFINDEX; 3099 ipp->ipp_ifindex = ip6i->ip6i_ifindex; 3100 } 3101 rptr = (uchar_t *)&ip6i[1]; 3102 mp->b_rptr = rptr; 3103 if (rptr == mp->b_wptr) { 3104 mp1 = mp->b_cont; 3105 freeb(mp); 3106 mp = mp1; 3107 rptr = mp->b_rptr; 3108 } 3109 ASSERT(mp->b_wptr - rptr >= 3110 IPV6_HDR_LEN + sizeof (sctp_hdr_t)); 3111 ip6h = (ip6_t *)rptr; 3112 } 3113 /* 3114 * Find any potentially interesting extension headers 3115 * as well as the length of the IPv6 + extension 3116 * headers. 3117 */ 3118 *ip_hdr_len = ip_find_hdr_v6(mp, ip6h, ipp, NULL); 3119 } else { 3120 *ip_hdr_len = IPV6_HDR_LEN; 3121 } 3122 *src = ip6h->ip6_src; 3123 *dst = ip6h->ip6_dst; 3124 } 3125 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 3126 return ((sctp_hdr_t *)&rptr[*ip_hdr_len]); 3127 #undef IPVER 3128 } 3129 3130 static mblk_t * 3131 sctp_check_in_policy(mblk_t *mp, mblk_t *ipsec_mp) 3132 { 3133 ipsec_in_t *ii; 3134 boolean_t check = B_TRUE; 3135 boolean_t policy_present; 3136 ipha_t *ipha; 3137 ip6_t *ip6h; 3138 netstack_t *ns; 3139 ipsec_stack_t *ipss; 3140 3141 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 3142 ASSERT(ii->ipsec_in_type == IPSEC_IN); 3143 ns = ii->ipsec_in_ns; 3144 ipss = ns->netstack_ipsec; 3145 3146 if (ii->ipsec_in_dont_check) { 3147 check = B_FALSE; 3148 if (!ii->ipsec_in_secure) { 3149 freeb(ipsec_mp); 3150 ipsec_mp = NULL; 3151 } 3152 } 3153 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 3154 policy_present = ipss->ipsec_inbound_v4_policy_present; 3155 ipha = (ipha_t *)mp->b_rptr; 3156 ip6h = NULL; 3157 } else { 3158 policy_present = ipss->ipsec_inbound_v6_policy_present; 3159 ipha = NULL; 3160 ip6h = (ip6_t *)mp->b_rptr; 3161 } 3162 3163 if (check && policy_present) { 3164 /* 3165 * The conn_t parameter is NULL because we already know 3166 * nobody's home. 3167 */ 3168 ipsec_mp = ipsec_check_global_policy(ipsec_mp, (conn_t *)NULL, 3169 ipha, ip6h, B_TRUE, ns); 3170 if (ipsec_mp == NULL) 3171 return (NULL); 3172 } 3173 if (ipsec_mp != NULL) 3174 freeb(ipsec_mp); 3175 return (mp); 3176 } 3177 3178 /* Handle out-of-the-blue packets */ 3179 void 3180 sctp_ootb_input(mblk_t *mp, ill_t *recv_ill, zoneid_t zoneid, 3181 boolean_t mctl_present) 3182 { 3183 sctp_t *sctp; 3184 sctp_chunk_hdr_t *ch; 3185 sctp_hdr_t *sctph; 3186 in6_addr_t src, dst; 3187 uint_t ip_hdr_len; 3188 uint_t ifindex; 3189 ip6_pkt_t ipp; 3190 ssize_t mlen; 3191 ip_pktinfo_t *pinfo = NULL; 3192 mblk_t *first_mp; 3193 sctp_stack_t *sctps; 3194 ip_stack_t *ipst; 3195 3196 ASSERT(recv_ill != NULL); 3197 ipst = recv_ill->ill_ipst; 3198 sctps = ipst->ips_netstack->netstack_sctp; 3199 3200 BUMP_MIB(&sctps->sctps_mib, sctpOutOfBlue); 3201 BUMP_MIB(&sctps->sctps_mib, sctpInSCTPPkts); 3202 3203 if (sctps->sctps_gsctp == NULL) { 3204 /* 3205 * For non-zero stackids the default queue isn't created 3206 * until the first open, thus there can be a need to send 3207 * an error before then. But we can't do that, hence we just 3208 * drop the packet. Later during boot, when the default queue 3209 * has been setup, a retransmitted packet from the peer 3210 * will result in a error. 3211 */ 3212 ASSERT(sctps->sctps_netstack->netstack_stackid != 3213 GLOBAL_NETSTACKID); 3214 freemsg(mp); 3215 return; 3216 } 3217 3218 first_mp = mp; 3219 if (mctl_present) 3220 mp = mp->b_cont; 3221 3222 /* Initiate IPPf processing, if needed. */ 3223 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 3224 ip_process(IPP_LOCAL_IN, &mp, 3225 recv_ill->ill_phyint->phyint_ifindex); 3226 if (mp == NULL) { 3227 if (mctl_present) 3228 freeb(first_mp); 3229 return; 3230 } 3231 } 3232 3233 if (mp->b_cont != NULL) { 3234 /* 3235 * All subsequent code is vastly simplified if it can 3236 * assume a single contiguous chunk of data. 3237 */ 3238 if (pullupmsg(mp, -1) == 0) { 3239 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 3240 freemsg(first_mp); 3241 return; 3242 } 3243 } 3244 3245 /* 3246 * We don't really need to call this function... Need to 3247 * optimize later. 3248 */ 3249 sctph = find_sctp_hdrs(mp, &src, &dst, &ifindex, &ip_hdr_len, 3250 &ipp, pinfo); 3251 mlen = mp->b_wptr - (uchar_t *)(sctph + 1); 3252 if ((ch = sctp_first_chunk((uchar_t *)(sctph + 1), mlen)) == NULL) { 3253 dprint(3, ("sctp_ootb_input: invalid packet\n")); 3254 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 3255 freemsg(first_mp); 3256 return; 3257 } 3258 3259 switch (ch->sch_id) { 3260 case CHUNK_INIT: 3261 /* no listener; send abort */ 3262 if (mctl_present && sctp_check_in_policy(mp, first_mp) == NULL) 3263 return; 3264 sctp_send_abort(sctps->sctps_gsctp, sctp_init2vtag(ch), 0, 3265 NULL, 0, mp, 0, B_TRUE); 3266 break; 3267 case CHUNK_INIT_ACK: 3268 /* check for changed src addr */ 3269 sctp = sctp_addrlist2sctp(mp, sctph, ch, zoneid, sctps); 3270 if (sctp != NULL) { 3271 /* success; proceed to normal path */ 3272 mutex_enter(&sctp->sctp_lock); 3273 if (sctp->sctp_running) { 3274 if (!sctp_add_recvq(sctp, mp, B_FALSE)) { 3275 BUMP_MIB(recv_ill->ill_ip_mib, 3276 ipIfStatsInDiscards); 3277 freemsg(mp); 3278 } 3279 mutex_exit(&sctp->sctp_lock); 3280 } else { 3281 /* 3282 * If the source address is changed, we 3283 * don't need to worry too much about 3284 * out of order processing. So we don't 3285 * check if the recvq is empty or not here. 3286 */ 3287 sctp->sctp_running = B_TRUE; 3288 mutex_exit(&sctp->sctp_lock); 3289 sctp_input_data(sctp, mp, NULL); 3290 WAKE_SCTP(sctp); 3291 sctp_process_sendq(sctp); 3292 } 3293 SCTP_REFRELE(sctp); 3294 return; 3295 } 3296 if (mctl_present) 3297 freeb(first_mp); 3298 /* else bogus init ack; drop it */ 3299 break; 3300 case CHUNK_SHUTDOWN_ACK: 3301 if (mctl_present && sctp_check_in_policy(mp, first_mp) == NULL) 3302 return; 3303 sctp_ootb_shutdown_ack(sctps->sctps_gsctp, mp, ip_hdr_len); 3304 sctp_process_sendq(sctps->sctps_gsctp); 3305 return; 3306 case CHUNK_ERROR: 3307 case CHUNK_ABORT: 3308 case CHUNK_COOKIE_ACK: 3309 case CHUNK_SHUTDOWN_COMPLETE: 3310 if (mctl_present) 3311 freeb(first_mp); 3312 break; 3313 default: 3314 if (mctl_present && sctp_check_in_policy(mp, first_mp) == NULL) 3315 return; 3316 sctp_send_abort(sctps->sctps_gsctp, sctph->sh_verf, 0, 3317 NULL, 0, mp, 0, B_TRUE); 3318 break; 3319 } 3320 sctp_process_sendq(sctps->sctps_gsctp); 3321 freemsg(mp); 3322 } 3323 3324 void 3325 sctp_input(conn_t *connp, ipha_t *ipha, mblk_t *mp, mblk_t *first_mp, 3326 ill_t *recv_ill, boolean_t isv4, boolean_t mctl_present) 3327 { 3328 sctp_t *sctp = CONN2SCTP(connp); 3329 ip_stack_t *ipst = recv_ill->ill_ipst; 3330 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 3331 3332 /* 3333 * We check some fields in conn_t without holding a lock. 3334 * This should be fine. 3335 */ 3336 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || mctl_present) { 3337 first_mp = ipsec_check_inbound_policy(first_mp, connp, 3338 ipha, NULL, mctl_present); 3339 if (first_mp == NULL) { 3340 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 3341 SCTP_REFRELE(sctp); 3342 return; 3343 } 3344 } 3345 3346 /* Initiate IPPF processing for fastpath */ 3347 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { 3348 ip_process(IPP_LOCAL_IN, &mp, 3349 recv_ill->ill_phyint->phyint_ifindex); 3350 if (mp == NULL) { 3351 SCTP_REFRELE(sctp); 3352 if (mctl_present) 3353 freeb(first_mp); 3354 return; 3355 } else if (mctl_present) { 3356 /* 3357 * ip_process might return a new mp. 3358 */ 3359 ASSERT(first_mp != mp); 3360 first_mp->b_cont = mp; 3361 } else { 3362 first_mp = mp; 3363 } 3364 } 3365 3366 if (connp->conn_recvif || connp->conn_recvslla || 3367 connp->conn_ip_recvpktinfo) { 3368 int in_flags = 0; 3369 3370 if (connp->conn_recvif || connp->conn_ip_recvpktinfo) { 3371 in_flags = IPF_RECVIF; 3372 } 3373 if (connp->conn_recvslla) { 3374 in_flags |= IPF_RECVSLLA; 3375 } 3376 if (isv4) { 3377 mp = ip_add_info(mp, recv_ill, in_flags, 3378 IPCL_ZONEID(connp), ipst); 3379 } else { 3380 mp = ip_add_info_v6(mp, recv_ill, 3381 &(((ip6_t *)ipha)->ip6_dst)); 3382 } 3383 if (mp == NULL) { 3384 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 3385 SCTP_REFRELE(sctp); 3386 if (mctl_present) 3387 freeb(first_mp); 3388 return; 3389 } else if (mctl_present) { 3390 /* 3391 * ip_add_info might return a new mp. 3392 */ 3393 ASSERT(first_mp != mp); 3394 first_mp->b_cont = mp; 3395 } else { 3396 first_mp = mp; 3397 } 3398 } 3399 3400 mutex_enter(&sctp->sctp_lock); 3401 if (sctp->sctp_running) { 3402 if (mctl_present) 3403 mp->b_prev = first_mp; 3404 if (!sctp_add_recvq(sctp, mp, B_FALSE)) { 3405 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 3406 freemsg(first_mp); 3407 } 3408 mutex_exit(&sctp->sctp_lock); 3409 SCTP_REFRELE(sctp); 3410 return; 3411 } else { 3412 sctp->sctp_running = B_TRUE; 3413 mutex_exit(&sctp->sctp_lock); 3414 3415 mutex_enter(&sctp->sctp_recvq_lock); 3416 if (sctp->sctp_recvq != NULL) { 3417 if (mctl_present) 3418 mp->b_prev = first_mp; 3419 if (!sctp_add_recvq(sctp, mp, B_TRUE)) { 3420 BUMP_MIB(recv_ill->ill_ip_mib, 3421 ipIfStatsInDiscards); 3422 freemsg(first_mp); 3423 } 3424 mutex_exit(&sctp->sctp_recvq_lock); 3425 WAKE_SCTP(sctp); 3426 SCTP_REFRELE(sctp); 3427 return; 3428 } 3429 } 3430 mutex_exit(&sctp->sctp_recvq_lock); 3431 sctp_input_data(sctp, mp, (mctl_present ? first_mp : NULL)); 3432 WAKE_SCTP(sctp); 3433 sctp_process_sendq(sctp); 3434 SCTP_REFRELE(sctp); 3435 } 3436 3437 static void 3438 sctp_process_abort(sctp_t *sctp, sctp_chunk_hdr_t *ch, int err) 3439 { 3440 sctp_stack_t *sctps = sctp->sctp_sctps; 3441 3442 BUMP_MIB(&sctps->sctps_mib, sctpAborted); 3443 BUMP_LOCAL(sctp->sctp_ibchunks); 3444 3445 sctp_assoc_event(sctp, SCTP_COMM_LOST, 3446 ntohs(((sctp_parm_hdr_t *)(ch + 1))->sph_type), ch); 3447 sctp_clean_death(sctp, err); 3448 } 3449 3450 void 3451 sctp_input_data(sctp_t *sctp, mblk_t *mp, mblk_t *ipsec_mp) 3452 { 3453 sctp_chunk_hdr_t *ch; 3454 ssize_t mlen; 3455 int gotdata; 3456 int trysend; 3457 sctp_faddr_t *fp; 3458 sctp_init_chunk_t *iack; 3459 uint32_t tsn; 3460 sctp_data_hdr_t *sdc; 3461 ip6_pkt_t ipp; 3462 in6_addr_t src; 3463 in6_addr_t dst; 3464 uint_t ifindex; 3465 sctp_hdr_t *sctph; 3466 uint_t ip_hdr_len; 3467 mblk_t *dups = NULL; 3468 int recv_adaption; 3469 boolean_t wake_eager = B_FALSE; 3470 mblk_t *pinfo_mp; 3471 ip_pktinfo_t *pinfo = NULL; 3472 in6_addr_t peer_src; 3473 int64_t now; 3474 sctp_stack_t *sctps = sctp->sctp_sctps; 3475 ip_stack_t *ipst = sctps->sctps_netstack->netstack_ip; 3476 3477 if (DB_TYPE(mp) != M_DATA) { 3478 ASSERT(DB_TYPE(mp) == M_CTL); 3479 if (MBLKL(mp) == sizeof (ip_pktinfo_t) && 3480 ((ip_pktinfo_t *)mp->b_rptr)->ip_pkt_ulp_type == 3481 IN_PKTINFO) { 3482 pinfo = (ip_pktinfo_t *)mp->b_rptr; 3483 pinfo_mp = mp; 3484 mp = mp->b_cont; 3485 } else { 3486 if (ipsec_mp != NULL) 3487 freeb(ipsec_mp); 3488 sctp_icmp_error(sctp, mp); 3489 return; 3490 } 3491 } 3492 ASSERT(DB_TYPE(mp) == M_DATA); 3493 3494 if (mp->b_cont != NULL) { 3495 /* 3496 * All subsequent code is vastly simplified if it can 3497 * assume a single contiguous chunk of data. 3498 */ 3499 if (pullupmsg(mp, -1) == 0) { 3500 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 3501 if (ipsec_mp != NULL) 3502 freeb(ipsec_mp); 3503 if (pinfo != NULL) 3504 freeb(pinfo_mp); 3505 freemsg(mp); 3506 return; 3507 } 3508 } 3509 3510 BUMP_LOCAL(sctp->sctp_ipkts); 3511 sctph = find_sctp_hdrs(mp, &src, &dst, &ifindex, &ip_hdr_len, 3512 &ipp, pinfo); 3513 if (pinfo != NULL) 3514 freeb(pinfo_mp); 3515 mlen = mp->b_wptr - (uchar_t *)(sctph + 1); 3516 ch = sctp_first_chunk((uchar_t *)(sctph + 1), mlen); 3517 if (ch == NULL) { 3518 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 3519 if (ipsec_mp != NULL) 3520 freeb(ipsec_mp); 3521 freemsg(mp); 3522 return; 3523 } 3524 3525 if (!sctp_check_input(sctp, ch, mlen, 1)) { 3526 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 3527 goto done; 3528 } 3529 /* 3530 * Check verfication tag (special handling for INIT, 3531 * COOKIE, SHUTDOWN_COMPLETE and SHUTDOWN_ACK chunks). 3532 * ABORTs are handled in the chunk processing loop, since 3533 * may not appear first. All other checked chunks must 3534 * appear first, or will have been dropped by check_input(). 3535 */ 3536 switch (ch->sch_id) { 3537 case CHUNK_INIT: 3538 if (sctph->sh_verf != 0) { 3539 /* drop it */ 3540 goto done; 3541 } 3542 break; 3543 case CHUNK_SHUTDOWN_COMPLETE: 3544 if (sctph->sh_verf == sctp->sctp_lvtag) 3545 break; 3546 if (sctph->sh_verf == sctp->sctp_fvtag && 3547 SCTP_GET_TBIT(ch)) { 3548 break; 3549 } 3550 /* else drop it */ 3551 goto done; 3552 case CHUNK_ABORT: 3553 case CHUNK_COOKIE: 3554 /* handled below */ 3555 break; 3556 case CHUNK_SHUTDOWN_ACK: 3557 if (sctp->sctp_state > SCTPS_BOUND && 3558 sctp->sctp_state < SCTPS_ESTABLISHED) { 3559 /* treat as OOTB */ 3560 sctp_ootb_shutdown_ack(sctp, mp, ip_hdr_len); 3561 if (ipsec_mp != NULL) 3562 freeb(ipsec_mp); 3563 return; 3564 } 3565 /* else fallthru */ 3566 default: 3567 /* 3568 * All other packets must have a valid 3569 * verification tag, however if this is a 3570 * listener, we use a refined version of 3571 * out-of-the-blue logic. 3572 */ 3573 if (sctph->sh_verf != sctp->sctp_lvtag && 3574 sctp->sctp_state != SCTPS_LISTEN) { 3575 /* drop it */ 3576 goto done; 3577 } 3578 break; 3579 } 3580 3581 /* Have a valid sctp for this packet */ 3582 fp = sctp_lookup_faddr(sctp, &src); 3583 dprint(2, ("sctp_dispatch_rput: mp=%p fp=%p sctp=%p\n", (void *)mp, 3584 (void *)fp, (void *)sctp)); 3585 3586 gotdata = 0; 3587 trysend = 0; 3588 3589 now = lbolt64; 3590 /* Process the chunks */ 3591 do { 3592 dprint(3, ("sctp_dispatch_rput: state=%d, chunk id=%d\n", 3593 sctp->sctp_state, (int)(ch->sch_id))); 3594 3595 if (ch->sch_id == CHUNK_ABORT) { 3596 if (sctph->sh_verf != sctp->sctp_lvtag && 3597 sctph->sh_verf != sctp->sctp_fvtag) { 3598 /* drop it */ 3599 goto done; 3600 } 3601 } 3602 3603 switch (sctp->sctp_state) { 3604 3605 case SCTPS_ESTABLISHED: 3606 case SCTPS_SHUTDOWN_PENDING: 3607 case SCTPS_SHUTDOWN_SENT: 3608 switch (ch->sch_id) { 3609 case CHUNK_DATA: 3610 /* 0-length data chunks are not allowed */ 3611 if (ntohs(ch->sch_len) == sizeof (*sdc)) { 3612 sdc = (sctp_data_hdr_t *)ch; 3613 tsn = sdc->sdh_tsn; 3614 sctp_send_abort(sctp, sctp->sctp_fvtag, 3615 SCTP_ERR_NO_USR_DATA, (char *)&tsn, 3616 sizeof (tsn), mp, 0, B_FALSE); 3617 sctp_assoc_event(sctp, SCTP_COMM_LOST, 3618 0, NULL); 3619 sctp_clean_death(sctp, ECONNABORTED); 3620 goto done; 3621 } 3622 3623 ASSERT(fp != NULL); 3624 sctp->sctp_lastdata = fp; 3625 sctp_data_chunk(sctp, ch, mp, &dups, fp, &ipp); 3626 gotdata = 1; 3627 /* Restart shutdown timer if shutting down */ 3628 if (sctp->sctp_state == SCTPS_SHUTDOWN_SENT) { 3629 /* 3630 * If we have exceeded our max 3631 * wait bound for waiting for a 3632 * shutdown ack from the peer, 3633 * abort the association. 3634 */ 3635 if (sctps->sctps_shutack_wait_bound != 3636 0 && 3637 TICK_TO_MSEC(now - 3638 sctp->sctp_out_time) > 3639 sctps->sctps_shutack_wait_bound) { 3640 sctp_send_abort(sctp, 3641 sctp->sctp_fvtag, 0, NULL, 3642 0, mp, 0, B_FALSE); 3643 sctp_assoc_event(sctp, 3644 SCTP_COMM_LOST, 0, NULL); 3645 sctp_clean_death(sctp, 3646 ECONNABORTED); 3647 goto done; 3648 } 3649 SCTP_FADDR_TIMER_RESTART(sctp, fp, 3650 fp->rto); 3651 } 3652 break; 3653 case CHUNK_SACK: 3654 ASSERT(fp != NULL); 3655 /* 3656 * Peer is real and alive if it can ack our 3657 * data. 3658 */ 3659 sctp_faddr_alive(sctp, fp); 3660 trysend = sctp_got_sack(sctp, ch); 3661 if (trysend < 0) { 3662 sctp_send_abort(sctp, sctph->sh_verf, 3663 0, NULL, 0, mp, 0, B_FALSE); 3664 sctp_assoc_event(sctp, 3665 SCTP_COMM_LOST, 0, NULL); 3666 sctp_clean_death(sctp, 3667 ECONNABORTED); 3668 goto done; 3669 } 3670 break; 3671 case CHUNK_HEARTBEAT: 3672 sctp_return_heartbeat(sctp, ch, mp); 3673 break; 3674 case CHUNK_HEARTBEAT_ACK: 3675 sctp_process_heartbeat(sctp, ch); 3676 break; 3677 case CHUNK_SHUTDOWN: 3678 sctp_shutdown_event(sctp); 3679 trysend = sctp_shutdown_received(sctp, ch, 3680 B_FALSE, B_FALSE, fp); 3681 BUMP_LOCAL(sctp->sctp_ibchunks); 3682 break; 3683 case CHUNK_SHUTDOWN_ACK: 3684 BUMP_LOCAL(sctp->sctp_ibchunks); 3685 if (sctp->sctp_state == SCTPS_SHUTDOWN_SENT) { 3686 sctp_shutdown_complete(sctp); 3687 BUMP_MIB(&sctps->sctps_mib, 3688 sctpShutdowns); 3689 sctp_assoc_event(sctp, 3690 SCTP_SHUTDOWN_COMP, 0, NULL); 3691 sctp_clean_death(sctp, 0); 3692 goto done; 3693 } 3694 break; 3695 case CHUNK_ABORT: { 3696 sctp_saddr_ipif_t *sp; 3697 3698 /* Ignore if delete pending */ 3699 sp = sctp_saddr_lookup(sctp, &dst, 0); 3700 ASSERT(sp != NULL); 3701 if (sp->saddr_ipif_delete_pending) { 3702 BUMP_LOCAL(sctp->sctp_ibchunks); 3703 break; 3704 } 3705 3706 sctp_process_abort(sctp, ch, ECONNRESET); 3707 goto done; 3708 } 3709 case CHUNK_INIT: 3710 sctp_send_initack(sctp, sctph, ch, mp); 3711 break; 3712 case CHUNK_COOKIE: 3713 if (sctp_process_cookie(sctp, ch, mp, &iack, 3714 sctph, &recv_adaption, NULL) != -1) { 3715 sctp_send_cookie_ack(sctp); 3716 sctp_assoc_event(sctp, SCTP_RESTART, 3717 0, NULL); 3718 if (recv_adaption) { 3719 sctp->sctp_recv_adaption = 1; 3720 sctp_adaption_event(sctp); 3721 } 3722 } else { 3723 BUMP_MIB(&sctps->sctps_mib, 3724 sctpInInvalidCookie); 3725 } 3726 break; 3727 case CHUNK_ERROR: { 3728 int error; 3729 3730 BUMP_LOCAL(sctp->sctp_ibchunks); 3731 error = sctp_handle_error(sctp, sctph, ch, mp); 3732 if (error != 0) { 3733 sctp_assoc_event(sctp, SCTP_COMM_LOST, 3734 0, NULL); 3735 sctp_clean_death(sctp, error); 3736 goto done; 3737 } 3738 break; 3739 } 3740 case CHUNK_ASCONF: 3741 ASSERT(fp != NULL); 3742 sctp_input_asconf(sctp, ch, fp); 3743 BUMP_LOCAL(sctp->sctp_ibchunks); 3744 break; 3745 case CHUNK_ASCONF_ACK: 3746 ASSERT(fp != NULL); 3747 sctp_faddr_alive(sctp, fp); 3748 sctp_input_asconf_ack(sctp, ch, fp); 3749 BUMP_LOCAL(sctp->sctp_ibchunks); 3750 break; 3751 case CHUNK_FORWARD_TSN: 3752 ASSERT(fp != NULL); 3753 sctp->sctp_lastdata = fp; 3754 sctp_process_forward_tsn(sctp, ch, fp, &ipp); 3755 gotdata = 1; 3756 BUMP_LOCAL(sctp->sctp_ibchunks); 3757 break; 3758 default: 3759 if (sctp_strange_chunk(sctp, ch, fp) == 0) { 3760 goto nomorechunks; 3761 } /* else skip and continue processing */ 3762 break; 3763 } 3764 break; 3765 3766 case SCTPS_LISTEN: 3767 switch (ch->sch_id) { 3768 case CHUNK_INIT: 3769 sctp_send_initack(sctp, sctph, ch, mp); 3770 break; 3771 case CHUNK_COOKIE: { 3772 sctp_t *eager; 3773 3774 if (sctp_process_cookie(sctp, ch, mp, &iack, 3775 sctph, &recv_adaption, &peer_src) == -1) { 3776 BUMP_MIB(&sctps->sctps_mib, 3777 sctpInInvalidCookie); 3778 goto done; 3779 } 3780 3781 /* 3782 * The cookie is good; ensure that 3783 * the peer used the verification 3784 * tag from the init ack in the header. 3785 */ 3786 if (iack->sic_inittag != sctph->sh_verf) 3787 goto done; 3788 3789 eager = sctp_conn_request(sctp, mp, ifindex, 3790 ip_hdr_len, iack, ipsec_mp); 3791 if (eager == NULL) { 3792 sctp_send_abort(sctp, sctph->sh_verf, 3793 SCTP_ERR_NO_RESOURCES, NULL, 0, mp, 3794 0, B_FALSE); 3795 goto done; 3796 } 3797 3798 /* 3799 * If there were extra chunks 3800 * bundled with the cookie, 3801 * they must be processed 3802 * on the eager's queue. We 3803 * accomplish this by refeeding 3804 * the whole packet into the 3805 * state machine on the right 3806 * q. The packet (mp) gets 3807 * there via the eager's 3808 * cookie_mp field (overloaded 3809 * with the active open role). 3810 * This is picked up when 3811 * processing the null bind 3812 * request put on the eager's 3813 * q by sctp_accept(). We must 3814 * first revert the cookie 3815 * chunk's length field to network 3816 * byteorder so it can be 3817 * properly reprocessed on the 3818 * eager's queue. 3819 */ 3820 BUMP_MIB(&sctps->sctps_mib, sctpPassiveEstab); 3821 if (mlen > ntohs(ch->sch_len)) { 3822 eager->sctp_cookie_mp = dupb(mp); 3823 mblk_setcred(eager->sctp_cookie_mp, 3824 CONN_CRED(eager->sctp_connp)); 3825 /* 3826 * If no mem, just let 3827 * the peer retransmit. 3828 */ 3829 } 3830 sctp_assoc_event(eager, SCTP_COMM_UP, 0, NULL); 3831 if (recv_adaption) { 3832 eager->sctp_recv_adaption = 1; 3833 eager->sctp_rx_adaption_code = 3834 sctp->sctp_rx_adaption_code; 3835 sctp_adaption_event(eager); 3836 } 3837 3838 eager->sctp_active = now; 3839 sctp_send_cookie_ack(eager); 3840 3841 wake_eager = B_TRUE; 3842 3843 /* 3844 * Process rest of the chunks with eager. 3845 */ 3846 sctp = eager; 3847 fp = sctp_lookup_faddr(sctp, &peer_src); 3848 /* 3849 * Confirm peer's original source. fp can 3850 * only be NULL if peer does not use the 3851 * original source as one of its addresses... 3852 */ 3853 if (fp == NULL) 3854 fp = sctp_lookup_faddr(sctp, &src); 3855 else 3856 sctp_faddr_alive(sctp, fp); 3857 3858 /* 3859 * Validate the peer addresses. It also starts 3860 * the heartbeat timer. 3861 */ 3862 sctp_validate_peer(sctp); 3863 break; 3864 } 3865 /* Anything else is considered out-of-the-blue */ 3866 case CHUNK_ERROR: 3867 case CHUNK_ABORT: 3868 case CHUNK_COOKIE_ACK: 3869 case CHUNK_SHUTDOWN_COMPLETE: 3870 BUMP_LOCAL(sctp->sctp_ibchunks); 3871 goto done; 3872 default: 3873 BUMP_LOCAL(sctp->sctp_ibchunks); 3874 sctp_send_abort(sctp, sctph->sh_verf, 0, NULL, 3875 0, mp, 0, B_TRUE); 3876 goto done; 3877 } 3878 break; 3879 3880 case SCTPS_COOKIE_WAIT: 3881 switch (ch->sch_id) { 3882 case CHUNK_INIT_ACK: 3883 sctp_stop_faddr_timers(sctp); 3884 sctp_faddr_alive(sctp, sctp->sctp_current); 3885 sctp_send_cookie_echo(sctp, ch, mp); 3886 BUMP_LOCAL(sctp->sctp_ibchunks); 3887 break; 3888 case CHUNK_ABORT: 3889 sctp_process_abort(sctp, ch, ECONNREFUSED); 3890 goto done; 3891 case CHUNK_INIT: 3892 sctp_send_initack(sctp, sctph, ch, mp); 3893 break; 3894 case CHUNK_COOKIE: 3895 if (sctp_process_cookie(sctp, ch, mp, &iack, 3896 sctph, &recv_adaption, NULL) == -1) { 3897 BUMP_MIB(&sctps->sctps_mib, 3898 sctpInInvalidCookie); 3899 break; 3900 } 3901 sctp_send_cookie_ack(sctp); 3902 sctp_stop_faddr_timers(sctp); 3903 if (!SCTP_IS_DETACHED(sctp)) { 3904 sctp->sctp_ulp_connected(sctp->sctp_ulpd); 3905 sctp_set_ulp_prop(sctp); 3906 } 3907 sctp->sctp_state = SCTPS_ESTABLISHED; 3908 sctp->sctp_assoc_start_time = (uint32_t)lbolt; 3909 BUMP_MIB(&sctps->sctps_mib, sctpActiveEstab); 3910 if (sctp->sctp_cookie_mp) { 3911 freemsg(sctp->sctp_cookie_mp); 3912 sctp->sctp_cookie_mp = NULL; 3913 } 3914 3915 /* Validate the peer addresses. */ 3916 sctp->sctp_active = now; 3917 sctp_validate_peer(sctp); 3918 3919 sctp_assoc_event(sctp, SCTP_COMM_UP, 0, NULL); 3920 if (recv_adaption) { 3921 sctp->sctp_recv_adaption = 1; 3922 sctp_adaption_event(sctp); 3923 } 3924 /* Try sending queued data, or ASCONFs */ 3925 trysend = 1; 3926 break; 3927 default: 3928 if (sctp_strange_chunk(sctp, ch, fp) == 0) { 3929 goto nomorechunks; 3930 } /* else skip and continue processing */ 3931 break; 3932 } 3933 break; 3934 3935 case SCTPS_COOKIE_ECHOED: 3936 switch (ch->sch_id) { 3937 case CHUNK_COOKIE_ACK: 3938 if (!SCTP_IS_DETACHED(sctp)) { 3939 sctp->sctp_ulp_connected(sctp->sctp_ulpd); 3940 sctp_set_ulp_prop(sctp); 3941 } 3942 if (sctp->sctp_unacked == 0) 3943 sctp_stop_faddr_timers(sctp); 3944 sctp->sctp_state = SCTPS_ESTABLISHED; 3945 sctp->sctp_assoc_start_time = (uint32_t)lbolt; 3946 BUMP_MIB(&sctps->sctps_mib, sctpActiveEstab); 3947 BUMP_LOCAL(sctp->sctp_ibchunks); 3948 if (sctp->sctp_cookie_mp) { 3949 freemsg(sctp->sctp_cookie_mp); 3950 sctp->sctp_cookie_mp = NULL; 3951 } 3952 sctp_faddr_alive(sctp, fp); 3953 /* Validate the peer addresses. */ 3954 sctp->sctp_active = now; 3955 sctp_validate_peer(sctp); 3956 3957 /* Try sending queued data, or ASCONFs */ 3958 trysend = 1; 3959 sctp_assoc_event(sctp, SCTP_COMM_UP, 0, NULL); 3960 sctp_adaption_event(sctp); 3961 break; 3962 case CHUNK_ABORT: 3963 sctp_process_abort(sctp, ch, ECONNREFUSED); 3964 goto done; 3965 case CHUNK_COOKIE: 3966 if (sctp_process_cookie(sctp, ch, mp, &iack, 3967 sctph, &recv_adaption, NULL) == -1) { 3968 BUMP_MIB(&sctps->sctps_mib, 3969 sctpInInvalidCookie); 3970 break; 3971 } 3972 sctp_send_cookie_ack(sctp); 3973 3974 if (!SCTP_IS_DETACHED(sctp)) { 3975 sctp->sctp_ulp_connected(sctp->sctp_ulpd); 3976 sctp_set_ulp_prop(sctp); 3977 } 3978 if (sctp->sctp_unacked == 0) 3979 sctp_stop_faddr_timers(sctp); 3980 sctp->sctp_state = SCTPS_ESTABLISHED; 3981 sctp->sctp_assoc_start_time = (uint32_t)lbolt; 3982 BUMP_MIB(&sctps->sctps_mib, sctpActiveEstab); 3983 if (sctp->sctp_cookie_mp) { 3984 freemsg(sctp->sctp_cookie_mp); 3985 sctp->sctp_cookie_mp = NULL; 3986 } 3987 /* Validate the peer addresses. */ 3988 sctp->sctp_active = now; 3989 sctp_validate_peer(sctp); 3990 3991 sctp_assoc_event(sctp, SCTP_COMM_UP, 0, NULL); 3992 if (recv_adaption) { 3993 sctp->sctp_recv_adaption = 1; 3994 sctp_adaption_event(sctp); 3995 } 3996 /* Try sending queued data, or ASCONFs */ 3997 trysend = 1; 3998 break; 3999 case CHUNK_INIT: 4000 sctp_send_initack(sctp, sctph, ch, mp); 4001 break; 4002 case CHUNK_ERROR: { 4003 sctp_parm_hdr_t *p; 4004 4005 BUMP_LOCAL(sctp->sctp_ibchunks); 4006 /* check for a stale cookie */ 4007 if (ntohs(ch->sch_len) >= 4008 (sizeof (*p) + sizeof (*ch)) + 4009 sizeof (uint32_t)) { 4010 4011 p = (sctp_parm_hdr_t *)(ch + 1); 4012 if (p->sph_type == 4013 htons(SCTP_ERR_STALE_COOKIE)) { 4014 BUMP_MIB(&sctps->sctps_mib, 4015 sctpAborted); 4016 sctp_error_event(sctp, ch); 4017 sctp_assoc_event(sctp, 4018 SCTP_COMM_LOST, 0, NULL); 4019 sctp_clean_death(sctp, 4020 ECONNREFUSED); 4021 goto done; 4022 } 4023 } 4024 break; 4025 } 4026 case CHUNK_HEARTBEAT: 4027 sctp_return_heartbeat(sctp, ch, mp); 4028 break; 4029 default: 4030 if (sctp_strange_chunk(sctp, ch, fp) == 0) { 4031 goto nomorechunks; 4032 } /* else skip and continue processing */ 4033 } /* switch (ch->sch_id) */ 4034 break; 4035 4036 case SCTPS_SHUTDOWN_ACK_SENT: 4037 switch (ch->sch_id) { 4038 case CHUNK_ABORT: 4039 /* Pass gathered wisdom to IP for keeping */ 4040 sctp_update_ire(sctp); 4041 sctp_process_abort(sctp, ch, 0); 4042 goto done; 4043 case CHUNK_SHUTDOWN_COMPLETE: 4044 BUMP_LOCAL(sctp->sctp_ibchunks); 4045 BUMP_MIB(&sctps->sctps_mib, sctpShutdowns); 4046 sctp_assoc_event(sctp, SCTP_SHUTDOWN_COMP, 0, 4047 NULL); 4048 4049 /* Pass gathered wisdom to IP for keeping */ 4050 sctp_update_ire(sctp); 4051 sctp_clean_death(sctp, 0); 4052 goto done; 4053 case CHUNK_SHUTDOWN_ACK: 4054 sctp_shutdown_complete(sctp); 4055 BUMP_LOCAL(sctp->sctp_ibchunks); 4056 BUMP_MIB(&sctps->sctps_mib, sctpShutdowns); 4057 sctp_assoc_event(sctp, SCTP_SHUTDOWN_COMP, 0, 4058 NULL); 4059 sctp_clean_death(sctp, 0); 4060 goto done; 4061 case CHUNK_COOKIE: 4062 (void) sctp_shutdown_received(sctp, NULL, 4063 B_TRUE, B_FALSE, fp); 4064 BUMP_LOCAL(sctp->sctp_ibchunks); 4065 break; 4066 case CHUNK_HEARTBEAT: 4067 sctp_return_heartbeat(sctp, ch, mp); 4068 break; 4069 default: 4070 if (sctp_strange_chunk(sctp, ch, fp) == 0) { 4071 goto nomorechunks; 4072 } /* else skip and continue processing */ 4073 break; 4074 } 4075 break; 4076 4077 case SCTPS_SHUTDOWN_RECEIVED: 4078 switch (ch->sch_id) { 4079 case CHUNK_SHUTDOWN: 4080 trysend = sctp_shutdown_received(sctp, ch, 4081 B_FALSE, B_FALSE, fp); 4082 break; 4083 case CHUNK_SACK: 4084 trysend = sctp_got_sack(sctp, ch); 4085 if (trysend < 0) { 4086 sctp_send_abort(sctp, sctph->sh_verf, 4087 0, NULL, 0, mp, 0, B_FALSE); 4088 sctp_assoc_event(sctp, 4089 SCTP_COMM_LOST, 0, NULL); 4090 sctp_clean_death(sctp, 4091 ECONNABORTED); 4092 goto done; 4093 } 4094 break; 4095 case CHUNK_ABORT: 4096 sctp_process_abort(sctp, ch, ECONNRESET); 4097 goto done; 4098 case CHUNK_HEARTBEAT: 4099 sctp_return_heartbeat(sctp, ch, mp); 4100 break; 4101 default: 4102 if (sctp_strange_chunk(sctp, ch, fp) == 0) { 4103 goto nomorechunks; 4104 } /* else skip and continue processing */ 4105 break; 4106 } 4107 break; 4108 4109 default: 4110 /* 4111 * The only remaining states are SCTPS_IDLE and 4112 * SCTPS_BOUND, and we should not be getting here 4113 * for these. 4114 */ 4115 ASSERT(0); 4116 } /* switch (sctp->sctp_state) */ 4117 4118 ch = sctp_next_chunk(ch, &mlen); 4119 if (ch != NULL && !sctp_check_input(sctp, ch, mlen, 0)) 4120 goto done; 4121 } while (ch != NULL); 4122 4123 /* Finished processing all chunks in packet */ 4124 4125 nomorechunks: 4126 /* SACK if necessary */ 4127 if (gotdata) { 4128 (sctp->sctp_sack_toggle)++; 4129 sctp_sack(sctp, dups); 4130 dups = NULL; 4131 4132 if (!sctp->sctp_ack_timer_running) { 4133 sctp->sctp_ack_timer_running = B_TRUE; 4134 sctp_timer(sctp, sctp->sctp_ack_mp, 4135 MSEC_TO_TICK(sctps->sctps_deferred_ack_interval)); 4136 } 4137 } 4138 4139 if (trysend) { 4140 sctp_output(sctp, UINT_MAX); 4141 if (sctp->sctp_cxmit_list != NULL) 4142 sctp_wput_asconf(sctp, NULL); 4143 } 4144 /* If there is unsent data, make sure a timer is running */ 4145 if (sctp->sctp_unsent > 0 && !sctp->sctp_current->timer_running) { 4146 SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current, 4147 sctp->sctp_current->rto); 4148 } 4149 4150 done: 4151 if (dups != NULL) 4152 freeb(dups); 4153 if (ipsec_mp != NULL) 4154 freeb(ipsec_mp); 4155 freemsg(mp); 4156 4157 if (wake_eager) { 4158 /* 4159 * sctp points to newly created control block, need to 4160 * release it before exiting. Before releasing it and 4161 * processing the sendq, need to grab a hold on it. 4162 * Otherwise, another thread can close it while processing 4163 * the sendq. 4164 */ 4165 SCTP_REFHOLD(sctp); 4166 WAKE_SCTP(sctp); 4167 sctp_process_sendq(sctp); 4168 SCTP_REFRELE(sctp); 4169 } 4170 } 4171 4172 /* 4173 * Some amount of data got removed from rx q. 4174 * Check if we should send a window update. 4175 * 4176 * Due to way sctp_rwnd updates are made, ULP can give reports out-of-order. 4177 * To keep from dropping incoming data due to this, we only update 4178 * sctp_rwnd when if it's larger than what we've reported to peer earlier. 4179 */ 4180 void 4181 sctp_recvd(sctp_t *sctp, int len) 4182 { 4183 int32_t old, new; 4184 sctp_stack_t *sctps = sctp->sctp_sctps; 4185 4186 ASSERT(sctp != NULL); 4187 RUN_SCTP(sctp); 4188 4189 if (len < sctp->sctp_rwnd) { 4190 WAKE_SCTP(sctp); 4191 return; 4192 } 4193 ASSERT(sctp->sctp_rwnd >= sctp->sctp_rxqueued); 4194 old = sctp->sctp_rwnd - sctp->sctp_rxqueued; 4195 new = len - sctp->sctp_rxqueued; 4196 sctp->sctp_rwnd = len; 4197 4198 if (sctp->sctp_state >= SCTPS_ESTABLISHED && 4199 ((old <= new >> 1) || (old < sctp->sctp_mss))) { 4200 sctp->sctp_force_sack = 1; 4201 BUMP_MIB(&sctps->sctps_mib, sctpOutWinUpdate); 4202 sctp_sack(sctp, NULL); 4203 old = 1; 4204 } else { 4205 old = 0; 4206 } 4207 WAKE_SCTP(sctp); 4208 if (old > 0) { 4209 sctp_process_sendq(sctp); 4210 } 4211 } 4212