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