xref: /freebsd/sys/netinet/tcp_sack.c (revision 71651a2743acfa3756ab1e7c3983e6861c6fada1)
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)tcp_sack.c	8.12 (Berkeley) 5/24/95
30  * $FreeBSD$
31  */
32 
33 /*-
34  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
62  *
63  * NRL grants permission for redistribution and use in source and binary
64  * forms, with or without modification, of the software and documentation
65  * created at NRL provided that the following conditions are met:
66  *
67  * 1. Redistributions of source code must retain the above copyright
68  *    notice, this list of conditions and the following disclaimer.
69  * 2. Redistributions in binary form must reproduce the above copyright
70  *    notice, this list of conditions and the following disclaimer in the
71  *    documentation and/or other materials provided with the distribution.
72  * 3. All advertising materials mentioning features or use of this software
73  *    must display the following acknowledgements:
74  *	This product includes software developed by the University of
75  *	California, Berkeley and its contributors.
76  *	This product includes software developed at the Information
77  *	Technology Division, US Naval Research Laboratory.
78  * 4. Neither the name of the NRL nor the names of its contributors
79  *    may be used to endorse or promote products derived from this software
80  *    without specific prior written permission.
81  *
82  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
83  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
84  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
85  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
86  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
87  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
88  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
89  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
91  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
92  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
93  *
94  * The views and conclusions contained in the software and documentation
95  * are those of the authors and should not be interpreted as representing
96  * official policies, either expressed or implied, of the US Naval
97  * Research Laboratory (NRL).
98  */
99 #include "opt_inet.h"
100 #include "opt_inet6.h"
101 #include "opt_ipsec.h"
102 #include "opt_tcpdebug.h"
103 #include "opt_tcp_input.h"
104 #include "opt_tcp_sack.h"
105 
106 #include <sys/param.h>
107 #include <sys/systm.h>
108 #include <sys/kernel.h>
109 #include <sys/sysctl.h>
110 #include <sys/malloc.h>
111 #include <sys/mbuf.h>
112 #include <sys/proc.h>		/* for proc0 declaration */
113 #include <sys/protosw.h>
114 #include <sys/socket.h>
115 #include <sys/socketvar.h>
116 #include <sys/syslog.h>
117 #include <sys/systm.h>
118 
119 #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
120 
121 #include <vm/uma.h>
122 
123 #include <net/if.h>
124 #include <net/route.h>
125 
126 #include <netinet/in.h>
127 #include <netinet/in_systm.h>
128 #include <netinet/ip.h>
129 #include <netinet/ip_icmp.h>	/* for ICMP_BANDLIM		*/
130 #include <netinet/in_var.h>
131 #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM		*/
132 #include <netinet/in_pcb.h>
133 #include <netinet/ip_var.h>
134 #include <netinet/ip6.h>
135 #include <netinet/icmp6.h>
136 #include <netinet6/nd6.h>
137 #include <netinet6/ip6_var.h>
138 #include <netinet6/in6_pcb.h>
139 #include <netinet/tcp.h>
140 #include <netinet/tcp_fsm.h>
141 #include <netinet/tcp_seq.h>
142 #include <netinet/tcp_timer.h>
143 #include <netinet/tcp_var.h>
144 #include <netinet6/tcp6_var.h>
145 #include <netinet/tcpip.h>
146 #ifdef TCPDEBUG
147 #include <netinet/tcp_debug.h>
148 
149 u_char tcp_saveipgen[40]; /* the size must be of max ip header, now IPv6 */
150 struct tcphdr tcp_savetcp;
151 #endif /* TCPDEBUG */
152 
153 #ifdef FAST_IPSEC
154 #include <netipsec/ipsec.h>
155 #include <netipsec/ipsec6.h>
156 #endif
157 
158 #ifdef IPSEC
159 #include <netinet6/ipsec.h>
160 #include <netinet6/ipsec6.h>
161 #include <netkey/key.h>
162 #endif /*IPSEC*/
163 #include <machine/in_cksum.h>
164 
165 extern struct uma_zone *sack_hole_zone;
166 
167 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW, 0, "TCP SACK");
168 int tcp_do_sack = 1;
169 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_RW,
170 	&tcp_do_sack, 0, "Enable/Disable TCP SACK support");
171 TUNABLE_INT("net.inet.tcp.sack.enable", &tcp_do_sack);
172 
173 /*
174  * This function is called upon receipt of new valid data (while not in header
175  * prediction mode), and it updates the ordered list of sacks.
176  */
177 void
178 tcp_update_sack_list(tp, rcv_laststart, rcv_lastend)
179 	struct tcpcb *tp;
180 	tcp_seq rcv_laststart, rcv_lastend;
181 {
182 	/*
183 	 * First reported block MUST be the most recent one.  Subsequent
184 	 * blocks SHOULD be in the order in which they arrived at the
185 	 * receiver.  These two conditions make the implementation fully
186 	 * compliant with RFC 2018.
187 	 */
188 	int i, j = 0, count = 0, lastpos = -1;
189 	struct sackblk sack, firstsack, temp[MAX_SACK_BLKS];
190 
191 	INP_LOCK_ASSERT(tp->t_inpcb);
192 	/* First clean up current list of sacks */
193 	for (i = 0; i < tp->rcv_numsacks; i++) {
194 		sack = tp->sackblks[i];
195 		if (sack.start == 0 && sack.end == 0) {
196 			count++; /* count = number of blocks to be discarded */
197 			continue;
198 		}
199 		if (SEQ_LEQ(sack.end, tp->rcv_nxt)) {
200 			tp->sackblks[i].start = tp->sackblks[i].end = 0;
201 			count++;
202 		} else {
203 			temp[j].start = tp->sackblks[i].start;
204 			temp[j++].end = tp->sackblks[i].end;
205 		}
206 	}
207 	tp->rcv_numsacks -= count;
208 	if (tp->rcv_numsacks == 0) { /* no sack blocks currently (fast path) */
209 		tcp_clean_sackreport(tp);
210 		if (SEQ_LT(tp->rcv_nxt, rcv_laststart)) {
211 			/* ==> need first sack block */
212 			tp->sackblks[0].start = rcv_laststart;
213 			tp->sackblks[0].end = rcv_lastend;
214 			tp->rcv_numsacks = 1;
215 		}
216 		return;
217 	}
218 	/* Otherwise, sack blocks are already present. */
219 	for (i = 0; i < tp->rcv_numsacks; i++)
220 		tp->sackblks[i] = temp[i]; /* first copy back sack list */
221 	if (SEQ_GEQ(tp->rcv_nxt, rcv_lastend))
222 		return;     /* sack list remains unchanged */
223 	/*
224 	 * From here, segment just received should be (part of) the 1st sack.
225 	 * Go through list, possibly coalescing sack block entries.
226 	 */
227 	firstsack.start = rcv_laststart;
228 	firstsack.end = rcv_lastend;
229 	for (i = 0; i < tp->rcv_numsacks; i++) {
230 		sack = tp->sackblks[i];
231 		if (SEQ_LT(sack.end, firstsack.start) ||
232 		    SEQ_GT(sack.start, firstsack.end))
233 			continue; /* no overlap */
234 		if (sack.start == firstsack.start && sack.end == firstsack.end){
235 			/*
236 			 * identical block; delete it here since we will
237 			 * move it to the front of the list.
238 			 */
239 			tp->sackblks[i].start = tp->sackblks[i].end = 0;
240 			lastpos = i;    /* last posn with a zero entry */
241 			continue;
242 		}
243 		if (SEQ_LEQ(sack.start, firstsack.start))
244 			firstsack.start = sack.start; /* merge blocks */
245 		if (SEQ_GEQ(sack.end, firstsack.end))
246 			firstsack.end = sack.end;     /* merge blocks */
247 		tp->sackblks[i].start = tp->sackblks[i].end = 0;
248 		lastpos = i;    /* last posn with a zero entry */
249 	}
250 	if (lastpos != -1) {    /* at least one merge */
251 		for (i = 0, j = 1; i < tp->rcv_numsacks; i++) {
252 			sack = tp->sackblks[i];
253 			if (sack.start == 0 && sack.end == 0)
254 				continue;
255 			temp[j++] = sack;
256 		}
257 		tp->rcv_numsacks = j; /* including first blk (added later) */
258 		for (i = 1; i < tp->rcv_numsacks; i++) /* now copy back */
259 			tp->sackblks[i] = temp[i];
260 	} else {        /* no merges -- shift sacks by 1 */
261 		if (tp->rcv_numsacks < MAX_SACK_BLKS)
262 			tp->rcv_numsacks++;
263 		for (i = tp->rcv_numsacks-1; i > 0; i--)
264 			tp->sackblks[i] = tp->sackblks[i-1];
265 	}
266 	tp->sackblks[0] = firstsack;
267 	return;
268 }
269 
270 /*
271  * Delete all receiver-side SACK information.
272  */
273 void
274 tcp_clean_sackreport(tp)
275 	struct tcpcb *tp;
276 {
277 	int i;
278 
279 	INP_LOCK_ASSERT(tp->t_inpcb);
280 	tp->rcv_numsacks = 0;
281 	for (i = 0; i < MAX_SACK_BLKS; i++)
282 		tp->sackblks[i].start = tp->sackblks[i].end=0;
283 }
284 
285 /*
286  * Process the TCP SACK option.  Returns 1 if tcp_dooptions() should continue,
287  * and 0 otherwise, if the option was fine.  tp->snd_holes is an ordered list
288  * of holes (oldest to newest, in terms of the sequence space).
289  */
290 int
291 tcp_sack_option(struct tcpcb *tp, struct tcphdr *th, u_char *cp, int optlen)
292 {
293 	int tmp_olen;
294 	u_char *tmp_cp;
295 	struct sackhole *cur, *p, *temp;
296 
297 	INP_LOCK_ASSERT(tp->t_inpcb);
298 	if (!tp->sack_enable)
299 		return (1);
300 
301 	/* Note: TCPOLEN_SACK must be 2*sizeof(tcp_seq) */
302 	if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
303 		return (1);
304 	/* If ack is outside window, ignore the SACK options */
305 	if (SEQ_LT(th->th_ack, tp->snd_una) || SEQ_GT(th->th_ack, tp->snd_max))
306 		return (1);
307 	tmp_cp = cp + 2;
308 	tmp_olen = optlen - 2;
309 	tcpstat.tcps_sack_rcv_blocks++;
310 	if (tp->snd_numholes < 0)
311 		tp->snd_numholes = 0;
312 	if (tp->t_maxseg == 0)
313 		panic("tcp_sack_option"); /* Should never happen */
314 	while (tmp_olen > 0) {
315 		struct sackblk sack;
316 
317 		bcopy(tmp_cp, (char *) &(sack.start), sizeof(tcp_seq));
318 		sack.start = ntohl(sack.start);
319 		bcopy(tmp_cp + sizeof(tcp_seq),
320 		    (char *) &(sack.end), sizeof(tcp_seq));
321 		sack.end = ntohl(sack.end);
322 		tmp_olen -= TCPOLEN_SACK;
323 		tmp_cp += TCPOLEN_SACK;
324 		if (SEQ_LEQ(sack.end, sack.start))
325 			continue; /* bad SACK fields */
326 		if (SEQ_LEQ(sack.end, tp->snd_una))
327 			continue; /* old block */
328 		if (SEQ_GT(th->th_ack, tp->snd_una)) {
329 			if (SEQ_LT(sack.start, th->th_ack))
330 				continue;
331 		}
332 		if (SEQ_GT(sack.end, tp->snd_max))
333 			continue;
334 		if (tp->snd_holes == NULL) { /* first hole */
335 			tp->snd_holes = (struct sackhole *)
336 				uma_zalloc(sack_hole_zone,M_NOWAIT);
337 			if (tp->snd_holes == NULL) {
338 				/* ENOBUFS, so ignore SACKed block for now*/
339 				continue;
340 			}
341 			cur = tp->snd_holes;
342 			cur->start = th->th_ack;
343 			cur->end = sack.start;
344 			cur->rxmit = cur->start;
345 			cur->next = NULL;
346 			tp->snd_numholes = 1;
347 			tp->rcv_lastsack = sack.end;
348 			continue; /* with next sack block */
349 		}
350 		/* Go thru list of holes:  p = previous,  cur = current */
351 		p = cur = tp->snd_holes;
352 		while (cur) {
353 			if (SEQ_LEQ(sack.end, cur->start))
354 				/* SACKs data before the current hole */
355 				break; /* no use going through more holes */
356 			if (SEQ_GEQ(sack.start, cur->end)) {
357 				/* SACKs data beyond the current hole */
358 				p = cur;
359 				cur = cur->next;
360 				continue;
361 			}
362 			if (SEQ_LEQ(sack.start, cur->start)) {
363 				/* Data acks at least the beginning of hole */
364 				if (SEQ_GEQ(sack.end, cur->end)) {
365 					/* Acks entire hole, so delete hole */
366 					if (p != cur) {
367 						p->next = cur->next;
368 						uma_zfree(sack_hole_zone, cur);
369 						cur = p->next;
370 					} else {
371 						cur = cur->next;
372 						uma_zfree(sack_hole_zone, p);
373 						p = cur;
374 						tp->snd_holes = p;
375 					}
376 					tp->snd_numholes--;
377 					continue;
378 				}
379 				/* otherwise, move start of hole forward */
380 				cur->start = sack.end;
381 				cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
382 				p = cur;
383 				cur = cur->next;
384 				continue;
385 			}
386 			/* move end of hole backward */
387 			if (SEQ_GEQ(sack.end, cur->end)) {
388 				cur->end = sack.start;
389 				cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
390 				p = cur;
391 				cur = cur->next;
392 				continue;
393 			}
394 			if (SEQ_LT(cur->start, sack.start) &&
395 			    SEQ_GT(cur->end, sack.end)) {
396 				/*
397 				 * ACKs some data in middle of a hole; need to
398 				 * split current hole
399 				 */
400 				temp = (struct sackhole *)
401 					uma_zalloc(sack_hole_zone,M_NOWAIT);
402 				if (temp == NULL)
403 					continue; /* ENOBUFS */
404 				temp->next = cur->next;
405 				temp->start = sack.end;
406 				temp->end = cur->end;
407 				temp->rxmit = SEQ_MAX(cur->rxmit, temp->start);
408 				cur->end = sack.start;
409 				cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
410 				cur->next = temp;
411 				p = temp;
412 				cur = p->next;
413 				tp->snd_numholes++;
414 			}
415 		}
416 		/* At this point, p points to the last hole on the list */
417 		if (SEQ_LT(tp->rcv_lastsack, sack.start)) {
418 			/*
419 			 * Need to append new hole at end.
420 			 * Last hole is p (and it's not NULL).
421 			 */
422 			temp = (struct sackhole *)
423 				uma_zalloc(sack_hole_zone,M_NOWAIT);
424 			if (temp == NULL)
425 				continue; /* ENOBUFS */
426 			temp->start = tp->rcv_lastsack;
427 			temp->end = sack.start;
428 			temp->rxmit = temp->start;
429 			temp->next = 0;
430 			p->next = temp;
431 			tp->rcv_lastsack = sack.end;
432 			tp->snd_numholes++;
433 		}
434 	}
435 	return (0);
436 }
437 
438 /*
439  * Delete stale (i.e, cumulatively ack'd) holes.  Hole is deleted only if
440  * it is completely acked; otherwise, tcp_sack_option(), called from
441  * tcp_dooptions(), will fix up the hole.
442  */
443 void
444 tcp_del_sackholes(tp, th)
445 	struct tcpcb *tp;
446 	struct tcphdr *th;
447 {
448 	INP_LOCK_ASSERT(tp->t_inpcb);
449 	if (tp->sack_enable && tp->t_state != TCPS_LISTEN) {
450 		/* max because this could be an older ack just arrived */
451 		tcp_seq lastack = SEQ_GT(th->th_ack, tp->snd_una) ?
452 			th->th_ack : tp->snd_una;
453 		struct sackhole *cur = tp->snd_holes;
454 		struct sackhole *prev;
455 		while (cur)
456 			if (SEQ_LEQ(cur->end, lastack)) {
457 				prev = cur;
458 				cur = cur->next;
459 				uma_zfree(sack_hole_zone, prev);
460 				tp->snd_numholes--;
461 			} else if (SEQ_LT(cur->start, lastack)) {
462 				cur->start = lastack;
463 				if (SEQ_LT(cur->rxmit, cur->start))
464 					cur->rxmit = cur->start;
465 				break;
466 			} else
467 				break;
468 		tp->snd_holes = cur;
469 	}
470 }
471 
472 void
473 tcp_free_sackholes(struct tcpcb *tp)
474 {
475 	struct sackhole *p, *q;
476 
477 	INP_LOCK_ASSERT(tp->t_inpcb);
478 	q = tp->snd_holes;
479 	while (q != NULL) {
480 		p = q;
481 		q = q->next;
482 		uma_zfree(sack_hole_zone, p);
483 	}
484 	tp->snd_holes = 0;
485 }
486 
487 /*
488  * Partial ack handling within a sack recovery episode.
489  * Keeping this very simple for now. When a partial ack
490  * is received, force snd_cwnd to a value that will allow
491  * the sender to transmit no more than 2 segments.
492  * If necessary, a better scheme can be adopted at a
493  * later point, but for now, the goal is to prevent the
494  * sender from bursting a large amount of data in the midst
495  * of sack recovery.
496  */
497 void
498 tcp_sack_partialack(tp, th)
499 	struct tcpcb *tp;
500 	struct tcphdr *th;
501 {
502 	INP_LOCK_ASSERT(tp->t_inpcb);
503 	int num_segs = 1;
504 	int sack_bytes_rxmt = 0;
505 
506 	callout_stop(tp->tt_rexmt);
507 	tp->t_rtttime = 0;
508 	/* send one or 2 segments based on how much new data was acked */
509 	if (((th->th_ack - tp->snd_una) / tp->t_maxseg) > 2)
510 		num_segs = 2;
511 	(void)tcp_sack_output(tp, &sack_bytes_rxmt);
512 	tp->snd_cwnd = sack_bytes_rxmt + (tp->snd_nxt - tp->sack_newdata) +
513 		num_segs * tp->t_maxseg;
514 	tp->t_flags |= TF_ACKNOW;
515 	(void) tcp_output(tp);
516 }
517 
518 #ifdef TCP_SACK_DEBUG
519 void
520 tcp_print_holes(struct tcpcb *tp)
521 {
522 	struct sackhole *p = tp->snd_holes;
523 	if (p == 0)
524 		return;
525 	printf("Hole report: start--end dups rxmit\n");
526 	while (p) {
527 		printf("%x--%x r %x\n", p->start, p->end, p->rxmit);
528 		p = p->next;
529 	}
530 	printf("\n");
531 }
532 #endif /* TCP_SACK_DEBUG */
533 
534 /*
535  * Returns pointer to a sackhole if there are any pending retransmissions;
536  * NULL otherwise.
537  */
538 struct sackhole *
539 tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
540 {
541 	struct sackhole *p = NULL;
542 
543 	INP_LOCK_ASSERT(tp->t_inpcb);
544 	if (!tp->sack_enable)
545 		return (NULL);
546 	*sack_bytes_rexmt = 0;
547 	for (p = tp->snd_holes; p ; p = p->next) {
548 		if (SEQ_LT(p->rxmit, p->end)) {
549 			if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
550 				continue;
551 			}
552 #ifdef TCP_SACK_DEBUG
553 			if (p)
554 				tcp_print_holes(tp);
555 #endif
556 			*sack_bytes_rexmt += (p->rxmit - p->start);
557 			break;
558 		}
559 		*sack_bytes_rexmt += (p->rxmit - p->start);
560 	}
561 	return (p);
562 }
563 
564 /*
565  * After a timeout, the SACK list may be rebuilt.  This SACK information
566  * should be used to avoid retransmitting SACKed data.  This function
567  * traverses the SACK list to see if snd_nxt should be moved forward.
568  */
569 void
570 tcp_sack_adjust(struct tcpcb *tp)
571 {
572 	INP_LOCK_ASSERT(tp->t_inpcb);
573 	struct sackhole *cur = tp->snd_holes;
574 	if (cur == NULL)
575 		return; /* No holes */
576 	if (SEQ_GEQ(tp->snd_nxt, tp->rcv_lastsack))
577 		return; /* We're already beyond any SACKed blocks */
578 	/*
579 	 * Two cases for which we want to advance snd_nxt:
580 	 * i) snd_nxt lies between end of one hole and beginning of another
581 	 * ii) snd_nxt lies between end of last hole and rcv_lastsack
582 	 */
583 	while (cur->next) {
584 		if (SEQ_LT(tp->snd_nxt, cur->end))
585 			return;
586 		if (SEQ_GEQ(tp->snd_nxt, cur->next->start))
587 			cur = cur->next;
588 		else {
589 			tp->snd_nxt = cur->next->start;
590 			return;
591 		}
592 	}
593 	if (SEQ_LT(tp->snd_nxt, cur->end))
594 		return;
595 	tp->snd_nxt = tp->rcv_lastsack;
596 	return;
597 }
598