+
+/*
+ * This function returns TRUE if more than (tcprexmtthresh - 1) * SMSS
+ * bytes with sequence numbers greater than snd_una have been SACKed.
+ */
+boolean_t
+tcp_sack_byte_islost(struct tcpcb *tp)
+{
+ u_int32_t unacked_bytes, sndhole_bytes = 0;
+ struct sackhole *sndhole;
+ if (!SACK_ENABLED(tp) || IN_FASTRECOVERY(tp) ||
+ TAILQ_EMPTY(&tp->snd_holes) ||
+ (tp->t_flagsext & TF_PKTS_REORDERED)) {
+ return FALSE;
+ }
+
+ unacked_bytes = tp->snd_max - tp->snd_una;
+
+ TAILQ_FOREACH(sndhole, &tp->snd_holes, scblink) {
+ sndhole_bytes += (sndhole->end - sndhole->start);
+ }
+
+ VERIFY(unacked_bytes >= sndhole_bytes);
+ return (unacked_bytes - sndhole_bytes) >
+ ((tcprexmtthresh - 1) * tp->t_maxseg);
+}
+
+/*
+ * Process any DSACK options that might be present on an input packet
+ */
+
+boolean_t
+tcp_sack_process_dsack(struct tcpcb *tp, struct tcpopt *to,
+ struct tcphdr *th)
+{
+ struct sackblk first_sack, second_sack;
+ struct tcp_rxt_seg *rxseg;
+
+ bcopy(to->to_sacks, &first_sack, sizeof(first_sack));
+ first_sack.start = ntohl(first_sack.start);
+ first_sack.end = ntohl(first_sack.end);
+
+ if (to->to_nsacks > 1) {
+ bcopy((to->to_sacks + TCPOLEN_SACK), &second_sack,
+ sizeof(second_sack));
+ second_sack.start = ntohl(second_sack.start);
+ second_sack.end = ntohl(second_sack.end);
+ }
+
+ if (SEQ_LT(first_sack.start, th->th_ack) &&
+ SEQ_LEQ(first_sack.end, th->th_ack)) {
+ /*
+ * There is a dsack option reporting a duplicate segment
+ * also covered by cumulative acknowledgement.
+ *
+ * Validate the sequence numbers before looking at dsack
+ * option. The duplicate notification can come after
+ * snd_una moves forward. In order to set a window of valid
+ * sequence numbers to look for, we set a maximum send
+ * window within which the DSACK option will be processed.
+ */
+ if (!(TCP_DSACK_SEQ_IN_WINDOW(tp, first_sack.start, th->th_ack) &&
+ TCP_DSACK_SEQ_IN_WINDOW(tp, first_sack.end, th->th_ack))) {
+ to->to_nsacks--;
+ to->to_sacks += TCPOLEN_SACK;
+ tcpstat.tcps_dsack_recvd_old++;
+
+ /*
+ * returning true here so that the ack will not be
+ * treated as duplicate ack.
+ */
+ return TRUE;
+ }
+ } else if (to->to_nsacks > 1 &&
+ SEQ_LEQ(second_sack.start, first_sack.start) &&
+ SEQ_GEQ(second_sack.end, first_sack.end)) {
+ /*
+ * there is a dsack option in the first block not
+ * covered by the cumulative acknowledgement but covered
+ * by the second sack block.
+ *
+ * verify the sequence numbes on the second sack block
+ * before processing the DSACK option. Returning false
+ * here will treat the ack as a duplicate ack.
+ */
+ if (!TCP_VALIDATE_SACK_SEQ_NUMBERS(tp, &second_sack,
+ th->th_ack)) {
+ to->to_nsacks--;
+ to->to_sacks += TCPOLEN_SACK;
+ tcpstat.tcps_dsack_recvd_old++;
+ return TRUE;
+ }
+ } else {
+ /* no dsack options, proceed with processing the sack */
+ return FALSE;
+ }
+
+ /* Update the tcpopt pointer to exclude dsack block */
+ to->to_nsacks--;
+ to->to_sacks += TCPOLEN_SACK;
+ tcpstat.tcps_dsack_recvd++;
+ tp->t_dsack_recvd++;
+
+ /* If the DSACK is for TLP mark it as such */
+ if ((tp->t_flagsext & TF_SENT_TLPROBE) &&
+ first_sack.end == tp->t_tlphighrxt) {
+ if ((rxseg = tcp_rxtseg_find(tp, first_sack.start,
+ (first_sack.end - 1))) != NULL) {
+ rxseg->rx_flags |= TCP_RXT_DSACK_FOR_TLP;
+ }
+ }
+ /* Update the sender's retransmit segment state */
+ if (((tp->t_rxtshift == 1 && first_sack.start == tp->snd_una) ||
+ ((tp->t_flagsext & TF_SENT_TLPROBE) &&
+ first_sack.end == tp->t_tlphighrxt)) &&
+ TAILQ_EMPTY(&tp->snd_holes) &&
+ SEQ_GT(th->th_ack, tp->snd_una)) {
+ /*
+ * If the dsack is for a retransmitted packet and one of
+ * the two cases is true, it indicates ack loss:
+ * - retransmit timeout and first_sack.start == snd_una
+ * - TLP probe and first_sack.end == tlphighrxt
+ *
+ * Ignore dsack and do not update state when there is
+ * ack loss
+ */
+ tcpstat.tcps_dsack_ackloss++;
+
+ return TRUE;
+ } else {
+ tcp_rxtseg_set_spurious(tp, first_sack.start, (first_sack.end - 1));
+ }
+ return TRUE;
+}