xref: /netbsd-src/sys/netinet/tcp_var.h (revision fd5cb0acea84d278e04e640d37ca2398f894991f)
1 /*	$NetBSD: tcp_var.h,v 1.118 2005/02/06 20:13:09 pk Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  *      @(#)COPYRIGHT   1.1 (NRL) 17 January 1995
34  *
35  * NRL grants permission for redistribution and use in source and binary
36  * forms, with or without modification, of the software and documentation
37  * created at NRL provided that the following conditions are met:
38  *
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgements:
46  *      This product includes software developed by the University of
47  *      California, Berkeley and its contributors.
48  *      This product includes software developed at the Information
49  *      Technology Division, US Naval Research Laboratory.
50  * 4. Neither the name of the NRL nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
55  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
57  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
58  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
59  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
60  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
61  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65  *
66  * The views and conclusions contained in the software and documentation
67  * are those of the authors and should not be interpreted as representing
68  * official policies, either expressed or implied, of the US Naval
69  * Research Laboratory (NRL).
70  */
71 
72 /*-
73  * Copyright (c) 1997, 1998, 1999, 2001 The NetBSD Foundation, Inc.
74  * All rights reserved.
75  *
76  * This code is derived from software contributed to The NetBSD Foundation
77  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
78  * NASA Ames Research Center.
79  *
80  * Redistribution and use in source and binary forms, with or without
81  * modification, are permitted provided that the following conditions
82  * are met:
83  * 1. Redistributions of source code must retain the above copyright
84  *    notice, this list of conditions and the following disclaimer.
85  * 2. Redistributions in binary form must reproduce the above copyright
86  *    notice, this list of conditions and the following disclaimer in the
87  *    documentation and/or other materials provided with the distribution.
88  * 3. All advertising materials mentioning features or use of this software
89  *    must display the following acknowledgement:
90  *	This product includes software developed by the NetBSD
91  *	Foundation, Inc. and its contributors.
92  * 4. Neither the name of The NetBSD Foundation nor the names of its
93  *    contributors may be used to endorse or promote products derived
94  *    from this software without specific prior written permission.
95  *
96  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
97  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
98  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
99  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
100  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
101  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
102  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
103  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
104  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
105  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
106  * POSSIBILITY OF SUCH DAMAGE.
107  */
108 
109 /*
110  * Copyright (c) 1982, 1986, 1993, 1994, 1995
111  *	The Regents of the University of California.  All rights reserved.
112  *
113  * Redistribution and use in source and binary forms, with or without
114  * modification, are permitted provided that the following conditions
115  * are met:
116  * 1. Redistributions of source code must retain the above copyright
117  *    notice, this list of conditions and the following disclaimer.
118  * 2. Redistributions in binary form must reproduce the above copyright
119  *    notice, this list of conditions and the following disclaimer in the
120  *    documentation and/or other materials provided with the distribution.
121  * 3. Neither the name of the University nor the names of its contributors
122  *    may be used to endorse or promote products derived from this software
123  *    without specific prior written permission.
124  *
125  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
126  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
127  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
128  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
129  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
130  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
131  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
132  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
133  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
134  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
135  * SUCH DAMAGE.
136  *
137  *	@(#)tcp_var.h	8.4 (Berkeley) 5/24/95
138  */
139 
140 #ifndef _NETINET_TCP_VAR_H_
141 #define _NETINET_TCP_VAR_H_
142 
143 #if defined(_KERNEL_OPT)
144 #include "opt_inet.h"
145 #include "opt_mbuftrace.h"
146 #endif
147 
148 /*
149  * Kernel variables for tcp.
150  */
151 
152 #include <sys/callout.h>
153 
154 #ifdef TCP_SIGNATURE
155 /*
156  * Defines which are needed by the xform_tcp module and tcp_[in|out]put
157  * for SADB verification and lookup.
158  */
159 #define	TCP_SIGLEN	16	/* length of computed digest in bytes */
160 #define	TCP_KEYLEN_MIN	1	/* minimum length of TCP-MD5 key */
161 #define	TCP_KEYLEN_MAX	80	/* maximum length of TCP-MD5 key */
162 /*
163  * Only a single SA per host may be specified at this time. An SPI is
164  * needed in order for the KEY_ALLOCSA() lookup to work.
165  */
166 #define	TCP_SIG_SPI	0x1000
167 #endif /* TCP_SIGNATURE */
168 
169 /*
170  * Tcp control block, one per tcp; fields:
171  */
172 struct tcpcb {
173 	int	t_family;		/* address family on the wire */
174 	struct ipqehead segq;		/* sequencing queue */
175 	struct callout t_timer[TCPT_NTIMERS];/* tcp timers */
176 	short	t_state;		/* state of this connection */
177 	short	t_rxtshift;		/* log(2) of rexmt exp. backoff */
178 	uint32_t t_rxtcur;		/* current retransmit value */
179 	short	t_dupacks;		/* consecutive dup acks recd */
180 	short	t_partialacks;		/* partials acks during fast rexmit */
181 	u_short	t_peermss;		/* peer's maximum segment size */
182 	u_short	t_ourmss;		/* our's maximum segment size */
183 	u_short t_segsz;		/* current segment size in use */
184 	char	t_force;		/* 1 if forcing out a byte */
185 	u_int	t_flags;
186 #define	TF_ACKNOW	0x0001		/* ack peer immediately */
187 #define	TF_DELACK	0x0002		/* ack, but try to delay it */
188 #define	TF_NODELAY	0x0004		/* don't delay packets to coalesce */
189 #define	TF_NOOPT	0x0008		/* don't use tcp options */
190 #define	TF_REQ_SCALE	0x0020		/* have/will request window scaling */
191 #define	TF_RCVD_SCALE	0x0040		/* other side has requested scaling */
192 #define	TF_REQ_TSTMP	0x0080		/* have/will request timestamps */
193 #define	TF_RCVD_TSTMP	0x0100		/* a timestamp was received in SYN */
194 #define	TF_SACK_PERMIT	0x0200		/* other side said I could SACK */
195 #define	TF_SYN_REXMT	0x0400		/* rexmit timer fired on SYN */
196 #define	TF_WILL_SACK	0x0800		/* try to use SACK */
197 #define	TF_CANT_TXSACK	0x1000		/* other side said I could not SACK */
198 #define	TF_IGNR_RXSACK	0x2000		/* ignore received SACK blocks */
199 #define	TF_REASSEMBLING	0x4000		/* we're busy reassembling */
200 #define	TF_DEAD		0x8000		/* dead and to-be-released */
201 #define	TF_SIGNATURE	0x400000	/* require MD5 digests (RFC2385) */
202 
203 
204 	struct	mbuf *t_template;	/* skeletal packet for transmit */
205 	struct	inpcb *t_inpcb;		/* back pointer to internet pcb */
206 	struct	in6pcb *t_in6pcb;	/* back pointer to internet pcb */
207 	struct	callout t_delack_ch;	/* delayed ACK callout */
208 /*
209  * The following fields are used as in the protocol specification.
210  * See RFC783, Dec. 1981, page 21.
211  */
212 /* send sequence variables */
213 	tcp_seq	snd_una;		/* send unacknowledged */
214 	tcp_seq	snd_nxt;		/* send next */
215 	tcp_seq	snd_up;			/* send urgent pointer */
216 	tcp_seq	snd_wl1;		/* window update seg seq number */
217 	tcp_seq	snd_wl2;		/* window update seg ack number */
218 	tcp_seq	iss;			/* initial send sequence number */
219 	u_long	snd_wnd;		/* send window */
220 	tcp_seq snd_recover;		/* for use in fast recovery */
221 	tcp_seq	snd_high;		/* NewReno false fast rexmit seq */
222 /* receive sequence variables */
223 	u_long	rcv_wnd;		/* receive window */
224 	tcp_seq	rcv_nxt;		/* receive next */
225 	tcp_seq	rcv_up;			/* receive urgent pointer */
226 	tcp_seq	irs;			/* initial receive sequence number */
227 /*
228  * Additional variables for this implementation.
229  */
230 /* receive variables */
231 	tcp_seq	rcv_adv;		/* advertised window */
232 /* retransmit variables */
233 	tcp_seq	snd_max;		/* highest sequence number sent;
234 					 * used to recognize retransmits
235 					 */
236 /* congestion control (for slow start, source quench, retransmit after loss) */
237 	u_long	snd_cwnd;		/* congestion-controlled window */
238 	u_long	snd_ssthresh;		/* snd_cwnd size threshhold for
239 					 * for slow start exponential to
240 					 * linear switch
241 					 */
242 /*
243  * transmit timing stuff.  See below for scale of srtt and rttvar.
244  * "Variance" is actually smoothed difference.
245  */
246 	uint32_t t_rcvtime;		/* time last segment received */
247 	uint32_t t_rtttime;		/* time we started measuring rtt */
248 	tcp_seq	t_rtseq;		/* sequence number being timed */
249 	int32_t	t_srtt;			/* smoothed round-trip time */
250 	int32_t	t_rttvar;		/* variance in round-trip time */
251 	uint32_t t_rttmin;		/* minimum rtt allowed */
252 	u_long	max_sndwnd;		/* largest window peer has offered */
253 
254 /* out-of-band data */
255 	char	t_oobflags;		/* have some */
256 	char	t_iobc;			/* input character */
257 #define	TCPOOB_HAVEDATA	0x01
258 #define	TCPOOB_HADDATA	0x02
259 	short	t_softerror;		/* possible error not yet reported */
260 
261 /* RFC 1323 variables */
262 	u_char	snd_scale;		/* window scaling for send window */
263 	u_char	rcv_scale;		/* window scaling for recv window */
264 	u_char	request_r_scale;	/* pending window scaling */
265 	u_char	requested_s_scale;
266 	u_int32_t ts_recent;		/* timestamp echo data */
267 	u_int32_t ts_recent_age;	/* when last updated */
268 	u_int32_t ts_timebase;		/* our timebase */
269 	tcp_seq	last_ack_sent;
270 
271 /* SACK stuff */
272 	struct ipqehead timeq;		/* time sequenced queue (for SACK) */
273 
274 /* path MTU discovery blackhole detection */
275 	int t_mtudisc;			/* perform mtudisc for this tcb */
276 
277 /* pointer for syn cache entries*/
278 	LIST_HEAD(, syn_cache) t_sc;	/* list of entries by this tcb */
279 
280 /* prediction of next mbuf when using large window sizes */
281 	struct	mbuf *t_lastm;		/* last mbuf that data was sent from */
282 	int	t_inoff;		/* data offset in previous mbuf */
283 	int	t_lastoff;		/* last data address in mbuf chain */
284 	int	t_lastlen;		/* last length read from mbuf chain */
285 };
286 
287 #ifdef _KERNEL
288 /*
289  * TCP reassembly queue locks.
290  */
291 static __inline int tcp_reass_lock_try (struct tcpcb *)
292 	__attribute__((__unused__));
293 static __inline void tcp_reass_unlock (struct tcpcb *)
294 	__attribute__((__unused__));
295 
296 static __inline int
297 tcp_reass_lock_try(tp)
298 	struct tcpcb *tp;
299 {
300 	int s;
301 
302 	/*
303 	 * Use splvm() -- we're blocking things that would cause
304 	 * mbuf allocation.
305 	 */
306 	s = splvm();
307 	if (tp->t_flags & TF_REASSEMBLING) {
308 		splx(s);
309 		return (0);
310 	}
311 	tp->t_flags |= TF_REASSEMBLING;
312 	splx(s);
313 	return (1);
314 }
315 
316 static __inline void
317 tcp_reass_unlock(tp)
318 	struct tcpcb *tp;
319 {
320 	int s;
321 
322 	s = splvm();
323 	tp->t_flags &= ~TF_REASSEMBLING;
324 	splx(s);
325 }
326 
327 #ifdef DIAGNOSTIC
328 #define	TCP_REASS_LOCK(tp)						\
329 do {									\
330 	if (tcp_reass_lock_try(tp) == 0) {				\
331 		printf("%s:%d: tcpcb %p reass already locked\n",	\
332 		    __FILE__, __LINE__, tp);				\
333 		panic("tcp_reass_lock");				\
334 	}								\
335 } while (/*CONSTCOND*/ 0)
336 #define	TCP_REASS_LOCK_CHECK(tp)					\
337 do {									\
338 	if (((tp)->t_flags & TF_REASSEMBLING) == 0) {			\
339 		printf("%s:%d: tcpcb %p reass lock not held\n",		\
340 		    __FILE__, __LINE__, tp);				\
341 		panic("tcp reass lock check");				\
342 	}								\
343 } while (/*CONSTCOND*/ 0)
344 #else
345 #define	TCP_REASS_LOCK(tp)	(void) tcp_reass_lock_try((tp))
346 #define	TCP_REASS_LOCK_CHECK(tp) /* nothing */
347 #endif
348 
349 #define	TCP_REASS_UNLOCK(tp)	tcp_reass_unlock((tp))
350 #endif /* _KERNEL */
351 
352 /*
353  * Queue for delayed ACK processing.
354  */
355 #ifdef _KERNEL
356 extern int tcp_delack_ticks;
357 void	tcp_delack(void *);
358 
359 #define TCP_RESTART_DELACK(tp)						\
360 	callout_reset(&(tp)->t_delack_ch, tcp_delack_ticks,		\
361 	    tcp_delack, tp)
362 
363 #define	TCP_SET_DELACK(tp)						\
364 do {									\
365 	if (((tp)->t_flags & TF_DELACK) == 0) {				\
366 		(tp)->t_flags |= TF_DELACK;				\
367 		TCP_RESTART_DELACK(tp);					\
368 	}								\
369 } while (/*CONSTCOND*/0)
370 
371 #define	TCP_CLEAR_DELACK(tp)						\
372 do {									\
373 	if ((tp)->t_flags & TF_DELACK) {				\
374 		(tp)->t_flags &= ~TF_DELACK;				\
375 		callout_stop(&(tp)->t_delack_ch);			\
376 	}								\
377 } while (/*CONSTCOND*/0)
378 #endif /* _KERNEL */
379 
380 /*
381  * Compute the current timestamp for a connection.
382  */
383 #define	TCP_TIMESTAMP(tp)	(tcp_now - (tp)->ts_timebase)
384 
385 /*
386  * Handy way of passing around TCP option info.
387  */
388 struct tcp_opt_info {
389 	int		ts_present;
390 	u_int32_t	ts_val;
391 	u_int32_t	ts_ecr;
392 	u_int16_t	maxseg;
393 };
394 
395 #define	TOF_SIGNATURE	0x0040		/* signature option present */
396 #define	TOF_SIGLEN	0x0080		/* sigature length valid (RFC2385) */
397 
398 /*
399  * Data for the TCP compressed state engine.
400  */
401 union syn_cache_sa {
402 	struct sockaddr sa;
403 	struct sockaddr_in sin;
404 #if 1 /*def INET6*/
405 	struct sockaddr_in6 sin6;
406 #endif
407 };
408 
409 struct syn_cache {
410 	TAILQ_ENTRY(syn_cache) sc_bucketq;	/* link on bucket list */
411 	struct callout sc_timer;		/* rexmt timer */
412 	union {					/* cached route */
413 		struct route route4;
414 #ifdef INET6
415 		struct route_in6 route6;
416 #endif
417 	} sc_route_u;
418 #define sc_route4	sc_route_u.route4
419 #ifdef INET6
420 #define sc_route6	sc_route_u.route6
421 #endif
422 	long sc_win;				/* advertised window */
423 	int sc_bucketidx;			/* our bucket index */
424 	u_int32_t sc_hash;
425 	u_int32_t sc_timestamp;			/* timestamp from SYN */
426 	u_int32_t sc_timebase;			/* our local timebase */
427 	union syn_cache_sa sc_src;
428 	union syn_cache_sa sc_dst;
429 	tcp_seq sc_irs;
430 	tcp_seq sc_iss;
431 	u_int sc_rxtcur;			/* current rxt timeout */
432 	u_int sc_rxttot;			/* total time spend on queues */
433 	u_short sc_rxtshift;			/* for computing backoff */
434 	u_short sc_flags;
435 
436 #define	SCF_UNREACH		0x0001		/* we've had an unreach error */
437 #define	SCF_TIMESTAMP		0x0002		/* peer will do timestamps */
438 #define	SCF_DEAD		0x0004		/* this entry to be released */
439 #define SCF_SIGNATURE	0x40			/* send MD5 digests */
440 
441 	struct mbuf *sc_ipopts;			/* IP options */
442 	u_int16_t sc_peermaxseg;
443 	u_int16_t sc_ourmaxseg;
444 	u_int8_t sc_request_r_scale	: 4,
445 		 sc_requested_s_scale	: 4;
446 
447 	struct tcpcb *sc_tp;			/* tcb for listening socket */
448 	LIST_ENTRY(syn_cache) sc_tpq;		/* list of entries by same tp */
449 };
450 
451 struct syn_cache_head {
452 	TAILQ_HEAD(, syn_cache) sch_bucket;	/* bucket entries */
453 	u_short sch_length;			/* # entries in bucket */
454 };
455 
456 #define	intotcpcb(ip)	((struct tcpcb *)(ip)->inp_ppcb)
457 #ifdef INET6
458 #define	in6totcpcb(ip)	((struct tcpcb *)(ip)->in6p_ppcb)
459 #endif
460 #ifndef INET6
461 #define	sototcpcb(so)	(intotcpcb(sotoinpcb(so)))
462 #else
463 #define	sototcpcb(so)	(((so)->so_proto->pr_domain->dom_family == AF_INET) \
464 				? intotcpcb(sotoinpcb(so)) \
465 				: in6totcpcb(sotoin6pcb(so)))
466 #endif
467 
468 /*
469  * The smoothed round-trip time and estimated variance
470  * are stored as fixed point numbers scaled by the values below.
471  * For convenience, these scales are also used in smoothing the average
472  * (smoothed = (1/scale)sample + ((scale-1)/scale)smoothed).
473  * With these scales, srtt has 3 bits to the right of the binary point,
474  * and thus an "ALPHA" of 0.875.  rttvar has 2 bits to the right of the
475  * binary point, and is smoothed with an ALPHA of 0.75.
476  */
477 #define	TCP_RTT_SHIFT		3	/* shift for srtt; 3 bits frac. */
478 #define	TCP_RTTVAR_SHIFT	2	/* multiplier for rttvar; 2 bits */
479 
480 /*
481  * The initial retransmission should happen at rtt + 4 * rttvar.
482  * Because of the way we do the smoothing, srtt and rttvar
483  * will each average +1/2 tick of bias.  When we compute
484  * the retransmit timer, we want 1/2 tick of rounding and
485  * 1 extra tick because of +-1/2 tick uncertainty in the
486  * firing of the timer.  The bias will give us exactly the
487  * 1.5 tick we need.  But, because the bias is
488  * statistical, we have to test that we don't drop below
489  * the minimum feasible timer (which is 2 ticks).
490  * This macro assumes that the value of 1<<TCP_RTTVAR_SHIFT
491  * is the same as the multiplier for rttvar.
492  */
493 #define	TCP_REXMTVAL(tp) \
494 	((((tp)->t_srtt >> TCP_RTT_SHIFT) + (tp)->t_rttvar) >> 2)
495 
496 /*
497  * Compute the initial window for slow start.
498  */
499 #define	TCP_INITIAL_WINDOW(iw, segsz) \
500 	(((iw) == 0) ? (min(4 * (segsz), max(2 * (segsz), 4380))) : \
501 	 ((segsz) * (iw)))
502 
503 /*
504  * TCP statistics.
505  * Many of these should be kept per connection,
506  * but that's inconvenient at the moment.
507  */
508 struct	tcpstat {
509 	u_quad_t tcps_connattempt;	/* connections initiated */
510 	u_quad_t tcps_accepts;		/* connections accepted */
511 	u_quad_t tcps_connects;		/* connections established */
512 	u_quad_t tcps_drops;		/* connections dropped */
513 	u_quad_t tcps_conndrops;	/* embryonic connections dropped */
514 	u_quad_t tcps_closed;		/* conn. closed (includes drops) */
515 	u_quad_t tcps_segstimed;	/* segs where we tried to get rtt */
516 	u_quad_t tcps_rttupdated;	/* times we succeeded */
517 	u_quad_t tcps_delack;		/* delayed acks sent */
518 	u_quad_t tcps_timeoutdrop;	/* conn. dropped in rxmt timeout */
519 	u_quad_t tcps_rexmttimeo;	/* retransmit timeouts */
520 	u_quad_t tcps_persisttimeo;	/* persist timeouts */
521 	u_quad_t tcps_keeptimeo;	/* keepalive timeouts */
522 	u_quad_t tcps_keepprobe;	/* keepalive probes sent */
523 	u_quad_t tcps_keepdrops;	/* connections dropped in keepalive */
524 	u_quad_t tcps_persistdrops;	/* connections dropped in persist */
525 	u_quad_t tcps_connsdrained;	/* connections drained due to memory
526 					   shortage */
527 	u_quad_t tcps_pmtublackhole;	/* PMTUD blackhole detected */
528 
529 	u_quad_t tcps_sndtotal;		/* total packets sent */
530 	u_quad_t tcps_sndpack;		/* data packets sent */
531 	u_quad_t tcps_sndbyte;		/* data bytes sent */
532 	u_quad_t tcps_sndrexmitpack;	/* data packets retransmitted */
533 	u_quad_t tcps_sndrexmitbyte;	/* data bytes retransmitted */
534 	u_quad_t tcps_sndacks;		/* ack-only packets sent */
535 	u_quad_t tcps_sndprobe;		/* window probes sent */
536 	u_quad_t tcps_sndurg;		/* packets sent with URG only */
537 	u_quad_t tcps_sndwinup;		/* window update-only packets sent */
538 	u_quad_t tcps_sndctrl;		/* control (SYN|FIN|RST) packets sent */
539 
540 	u_quad_t tcps_rcvtotal;		/* total packets received */
541 	u_quad_t tcps_rcvpack;		/* packets received in sequence */
542 	u_quad_t tcps_rcvbyte;		/* bytes received in sequence */
543 	u_quad_t tcps_rcvbadsum;	/* packets received with ccksum errs */
544 	u_quad_t tcps_rcvbadoff;	/* packets received with bad offset */
545 	u_quad_t tcps_rcvmemdrop;	/* packets dropped for lack of memory */
546 	u_quad_t tcps_rcvshort;		/* packets received too short */
547 	u_quad_t tcps_rcvduppack;	/* duplicate-only packets received */
548 	u_quad_t tcps_rcvdupbyte;	/* duplicate-only bytes received */
549 	u_quad_t tcps_rcvpartduppack;	/* packets with some duplicate data */
550 	u_quad_t tcps_rcvpartdupbyte;	/* dup. bytes in part-dup. packets */
551 	u_quad_t tcps_rcvoopack;	/* out-of-order packets received */
552 	u_quad_t tcps_rcvoobyte;	/* out-of-order bytes received */
553 	u_quad_t tcps_rcvpackafterwin;	/* packets with data after window */
554 	u_quad_t tcps_rcvbyteafterwin;	/* bytes rcvd after window */
555 	u_quad_t tcps_rcvafterclose;	/* packets rcvd after "close" */
556 	u_quad_t tcps_rcvwinprobe;	/* rcvd window probe packets */
557 	u_quad_t tcps_rcvdupack;	/* rcvd duplicate acks */
558 	u_quad_t tcps_rcvacktoomuch;	/* rcvd acks for unsent data */
559 	u_quad_t tcps_rcvackpack;	/* rcvd ack packets */
560 	u_quad_t tcps_rcvackbyte;	/* bytes acked by rcvd acks */
561 	u_quad_t tcps_rcvwinupd;	/* rcvd window update packets */
562 	u_quad_t tcps_pawsdrop;		/* segments dropped due to PAWS */
563 	u_quad_t tcps_predack;		/* times hdr predict ok for acks */
564 	u_quad_t tcps_preddat;		/* times hdr predict ok for data pkts */
565 
566 	u_quad_t tcps_pcbhashmiss;	/* input packets missing pcb hash */
567 	u_quad_t tcps_noport;		/* no socket on port */
568 	u_quad_t tcps_badsyn;		/* received ack for which we have
569 					   no SYN in compressed state */
570 	u_quad_t tcps_delayed_free;	/* delayed pool_put() of tcpcb */
571 
572 	/* These statistics deal with the SYN cache. */
573 	u_quad_t tcps_sc_added;		/* # of entries added */
574 	u_quad_t tcps_sc_completed;	/* # of connections completed */
575 	u_quad_t tcps_sc_timed_out;	/* # of entries timed out */
576 	u_quad_t tcps_sc_overflowed;	/* # dropped due to overflow */
577 	u_quad_t tcps_sc_reset;		/* # dropped due to RST */
578 	u_quad_t tcps_sc_unreach;	/* # dropped due to ICMP unreach */
579 	u_quad_t tcps_sc_bucketoverflow;/* # dropped due to bucket overflow */
580 	u_quad_t tcps_sc_aborted;	/* # of entries aborted (no mem) */
581 	u_quad_t tcps_sc_dupesyn;	/* # of duplicate SYNs received */
582 	u_quad_t tcps_sc_dropped;	/* # of SYNs dropped (no route/mem) */
583 	u_quad_t tcps_sc_collisions;	/* # of hash collisions */
584 	u_quad_t tcps_sc_retransmitted;	/* # of retransmissions */
585 	u_quad_t tcps_sc_delayed_free;	/* # of delayed pool_put()s */
586 
587 	u_quad_t tcps_selfquench;	/* # of ENOBUFS we get on output */
588 	u_quad_t tcps_badsig;		/* # of drops due to bad signature */
589 	u_quad_t tcps_goodsig;		/* # of packets with good signature */
590 };
591 
592 /*
593  * Names for TCP sysctl objects.
594  */
595 #define	TCPCTL_RFC1323		1	/* RFC1323 timestamps/scaling */
596 #define	TCPCTL_SENDSPACE	2	/* default send buffer */
597 #define	TCPCTL_RECVSPACE	3	/* default recv buffer */
598 #define	TCPCTL_MSSDFLT		4	/* default seg size */
599 #define	TCPCTL_SYN_CACHE_LIMIT	5	/* max size of comp. state engine */
600 #define	TCPCTL_SYN_BUCKET_LIMIT	6	/* max size of hash bucket */
601 #if 0	/*obsoleted*/
602 #define	TCPCTL_SYN_CACHE_INTER	7	/* interval of comp. state timer */
603 #endif
604 #define	TCPCTL_INIT_WIN		8	/* initial window */
605 #define	TCPCTL_MSS_IFMTU	9	/* mss from interface, not in_maxmtu */
606 #define	TCPCTL_SACK		10	/* RFC2018 selective acknowledgement */
607 #define	TCPCTL_WSCALE		11	/* RFC1323 window scaling */
608 #define	TCPCTL_TSTAMP		12	/* RFC1323 timestamps */
609 #define	TCPCTL_COMPAT_42	13	/* 4.2BSD TCP bug work-arounds */
610 #define	TCPCTL_CWM		14	/* Congestion Window Monitoring */
611 #define	TCPCTL_CWM_BURSTSIZE	15	/* burst size allowed by CWM */
612 #define	TCPCTL_ACK_ON_PUSH	16	/* ACK immediately on PUSH */
613 #define	TCPCTL_KEEPIDLE		17	/* keepalive idle time */
614 #define	TCPCTL_KEEPINTVL	18	/* keepalive probe interval */
615 #define	TCPCTL_KEEPCNT		19	/* keepalive count */
616 #define	TCPCTL_SLOWHZ		20	/* PR_SLOWHZ (read-only) */
617 #define	TCPCTL_NEWRENO		21	/* NewReno Congestion Control */
618 #define TCPCTL_LOG_REFUSED	22	/* Log refused connections */
619 #if 0	/*obsoleted*/
620 #define	TCPCTL_RSTRATELIMIT	23	/* RST rate limit */
621 #endif
622 #define	TCPCTL_RSTPPSLIMIT	24	/* RST pps limit */
623 #define	TCPCTL_DELACK_TICKS	25	/* # ticks to delay ACK */
624 #define	TCPCTL_INIT_WIN_LOCAL	26	/* initial window for local nets */
625 #define	TCPCTL_IDENT		27	/* rfc 931 identd */
626 #define	TCPCTL_ACKDROPRATELIMIT	28	/* SYN/RST -> ACK rate limit */
627 #define	TCPCTL_LOOPBACKCKSUM	29	/* do TCP checksum on loopback */
628 #define	TCPCTL_MAXID		30
629 
630 #define	TCPCTL_NAMES { \
631 	{ 0, 0 }, \
632 	{ "rfc1323",	CTLTYPE_INT }, \
633 	{ "sendspace",	CTLTYPE_INT }, \
634 	{ "recvspace",	CTLTYPE_INT }, \
635 	{ "mssdflt",	CTLTYPE_INT }, \
636 	{ "syn_cache_limit", CTLTYPE_INT }, \
637 	{ "syn_bucket_limit", CTLTYPE_INT }, \
638 	{ 0, 0 },\
639 	{ "init_win", CTLTYPE_INT }, \
640 	{ "mss_ifmtu", CTLTYPE_INT }, \
641 	{ "sack", CTLTYPE_INT }, \
642 	{ "win_scale", CTLTYPE_INT }, \
643 	{ "timestamps", CTLTYPE_INT }, \
644 	{ "compat_42", CTLTYPE_INT }, \
645 	{ "cwm", CTLTYPE_INT }, \
646 	{ "cwm_burstsize", CTLTYPE_INT }, \
647 	{ "ack_on_push", CTLTYPE_INT }, \
648 	{ "keepidle",	CTLTYPE_INT }, \
649 	{ "keepintvl",	CTLTYPE_INT }, \
650 	{ "keepcnt",	CTLTYPE_INT }, \
651 	{ "slowhz",	CTLTYPE_INT }, \
652 	{ "newreno",	CTLTYPE_INT }, \
653 	{ "log_refused",CTLTYPE_INT }, \
654 	{ 0, 0 }, \
655 	{ "rstppslimit", CTLTYPE_INT }, \
656 	{ "delack_ticks", CTLTYPE_INT }, \
657 	{ "init_win_local", CTLTYPE_INT }, \
658 	{ "ident", CTLTYPE_STRUCT }, \
659 	{ "ackdropppslimit", CTLTYPE_INT }, \
660 	{ "do_loopback_cksum", CTLTYPE_INT }, \
661 }
662 
663 #ifdef _KERNEL
664 extern	struct inpcbtable tcbtable;	/* head of queue of active tcpcb's */
665 extern	struct tcpstat tcpstat;	/* tcp statistics */
666 extern	u_int32_t tcp_now;	/* for RFC 1323 timestamps */
667 extern	int tcp_do_rfc1323;	/* enabled/disabled? */
668 extern	int tcp_do_sack;	/* SACK enabled/disabled? */
669 extern	int tcp_do_win_scale;	/* RFC1323 window scaling enabled/disabled? */
670 extern	int tcp_do_timestamps;	/* RFC1323 timestamps enabled/disabled? */
671 extern	int tcp_do_newreno;	/* Use the New Reno algorithms */
672 extern	int tcp_mssdflt;	/* default seg size */
673 extern	int tcp_init_win;	/* initial window */
674 extern	int tcp_init_win_local;	/* initial window for local nets */
675 extern	int tcp_mss_ifmtu;	/* take MSS from interface, not in_maxmtu */
676 extern	int tcp_compat_42;	/* work around ancient broken TCP peers */
677 extern	int tcp_cwm;		/* enable Congestion Window Monitoring */
678 extern	int tcp_cwm_burstsize;	/* burst size allowed by CWM */
679 extern	int tcp_ack_on_push;	/* ACK immediately on PUSH */
680 extern	int tcp_syn_cache_limit; /* max entries for compressed state engine */
681 extern	int tcp_syn_bucket_limit;/* max entries per hash bucket */
682 extern	int tcp_log_refused;	/* log refused connections */
683 extern	int tcp_do_loopback_cksum;/* do TCP checksum on loopback? */
684 
685 extern	int tcp_rst_ppslim;
686 extern	int tcp_ackdrop_ppslim;
687 
688 extern	int tcp_syn_cache_size;
689 extern	struct syn_cache_head tcp_syn_cache[];
690 extern	u_long syn_cache_count;
691 
692 extern	struct pool tcpipqent_pool;
693 
694 #ifdef MBUFTRACE
695 extern	struct mowner tcp_rx_mowner;
696 extern	struct mowner tcp_tx_mowner;
697 extern	struct mowner tcp_mowner;
698 #endif
699 
700 #define	TCPCTL_VARIABLES { \
701 	{ 0 },					\
702 	{ 1, 0, &tcp_do_rfc1323 },		\
703 	{ 1, 0, &tcp_sendspace },		\
704 	{ 1, 0, &tcp_recvspace },		\
705 	{ 1, 0, &tcp_mssdflt },			\
706 	{ 1, 0, &tcp_syn_cache_limit },		\
707 	{ 1, 0, &tcp_syn_bucket_limit },	\
708 	{ 0 },					\
709 	{ 1, 0, &tcp_init_win },		\
710 	{ 1, 0, &tcp_mss_ifmtu },		\
711 	{ 1, 0, &tcp_do_sack },			\
712 	{ 1, 0, &tcp_do_win_scale },		\
713 	{ 1, 0, &tcp_do_timestamps },		\
714 	{ 1, 0, &tcp_compat_42 },		\
715 	{ 1, 0, &tcp_cwm },			\
716 	{ 1, 0, &tcp_cwm_burstsize },		\
717 	{ 1, 0, &tcp_ack_on_push },		\
718 	{ 1, 0, &tcp_keepidle },		\
719 	{ 1, 0, &tcp_keepintvl },		\
720 	{ 1, 0, &tcp_keepcnt },			\
721 	{ 1, 1, 0, PR_SLOWHZ },			\
722 	{ 1, 0, &tcp_do_newreno },		\
723 	{ 1, 0, &tcp_log_refused },		\
724 	{ 0 },					\
725 	{ 1, 0, &tcp_rst_ppslim },		\
726 	{ 1, 0, &tcp_delack_ticks },		\
727 	{ 1, 0, &tcp_init_win_local },		\
728 	{ 1, 0, &tcp_ackdrop_ppslim },		\
729 }
730 
731 #ifdef __NO_STRICT_ALIGNMENT
732 #define	TCP_HDR_ALIGNED_P(th)	1
733 #else
734 #define	TCP_HDR_ALIGNED_P(th)	((((vaddr_t)(th)) & 3) == 0)
735 #endif
736 
737 struct secasvar;
738 
739 int	 tcp_attach(struct socket *);
740 void	 tcp_canceltimers(struct tcpcb *);
741 int	 tcp_timers_invoking(struct tcpcb*);
742 struct tcpcb *
743 	 tcp_close(struct tcpcb *);
744 int	 tcp_isdead(struct tcpcb *);
745 #ifdef INET6
746 void	 tcp6_ctlinput(int, struct sockaddr *, void *);
747 #endif
748 void	 *tcp_ctlinput(int, struct sockaddr *, void *);
749 int	 tcp_ctloutput(int, struct socket *, int, int, struct mbuf **);
750 struct tcpcb *
751 	 tcp_disconnect(struct tcpcb *);
752 struct tcpcb *
753 	 tcp_drop(struct tcpcb *, int);
754 #ifdef TCP_SIGNATURE
755 int	 tcp_signature_apply(void *, caddr_t, u_int);
756 struct secasvar *tcp_signature_getsav(struct mbuf *, struct tcphdr *);
757 int	 tcp_signature(struct mbuf *, struct tcphdr *, int, struct secasvar *,
758 	    char *);
759 #endif
760 int	 tcp_dooptions(struct tcpcb *, u_char *, int, struct tcphdr *,
761 	    struct mbuf *, int, struct tcp_opt_info *);
762 void	 tcp_drain(void);
763 void	 tcp_established(struct tcpcb *);
764 void	 tcp_init(void);
765 #ifdef INET6
766 int	 tcp6_input(struct mbuf **, int *, int);
767 #endif
768 void	 tcp_input(struct mbuf *, ...);
769 u_long	 tcp_mss_to_advertise(const struct ifnet *, int);
770 void	 tcp_mss_from_peer(struct tcpcb *, int);
771 void	 tcp_tcpcb_template(void);
772 struct tcpcb *
773 	 tcp_newtcpcb(int, void *);
774 void	 tcp_notify(struct inpcb *, int);
775 #ifdef INET6
776 void	 tcp6_notify(struct in6pcb *, int);
777 #endif
778 u_int	 tcp_optlen(struct tcpcb *);
779 int	 tcp_output(struct tcpcb *);
780 void	 tcp_pulloutofband(struct socket *,
781 	    struct tcphdr *, struct mbuf *, int);
782 void	 tcp_quench(struct inpcb *, int);
783 #ifdef INET6
784 void	 tcp6_quench(struct in6pcb *, int);
785 #endif
786 int	 tcp_reass(struct tcpcb *, struct tcphdr *, struct mbuf *, int *);
787 int	 tcp_respond(struct tcpcb *, struct mbuf *, struct mbuf *,
788 	    struct tcphdr *, tcp_seq, tcp_seq, int);
789 void	 tcp_rmx_rtt(struct tcpcb *);
790 void	 tcp_setpersist(struct tcpcb *);
791 #ifdef TCP_SIGNATURE
792 int	 tcp_signature_compute(struct mbuf *, struct tcphdr *, int, int,
793 	    int, u_char *, u_int);
794 #endif
795 void	 tcp_slowtimo(void);
796 struct mbuf *
797 	 tcp_template(struct tcpcb *);
798 void	 tcp_trace(short, short, struct tcpcb *, struct mbuf *, int);
799 struct tcpcb *
800 	 tcp_usrclosed(struct tcpcb *);
801 int	 tcp_sysctl(int *, u_int, void *, size_t *, void *, size_t);
802 int	 tcp_usrreq(struct socket *,
803 	    int, struct mbuf *, struct mbuf *, struct mbuf *, struct proc *);
804 void	 tcp_xmit_timer(struct tcpcb *, uint32_t);
805 tcp_seq	 tcp_new_iss(struct tcpcb *, tcp_seq);
806 tcp_seq  tcp_new_iss1(void *, void *, u_int16_t, u_int16_t, size_t,
807 	    tcp_seq);
808 
809 int	 syn_cache_add(struct sockaddr *, struct sockaddr *,
810 		struct tcphdr *, unsigned int, struct socket *,
811 		struct mbuf *, u_char *, int, struct tcp_opt_info *);
812 void	 syn_cache_unreach(struct sockaddr *, struct sockaddr *,
813 	   struct tcphdr *);
814 struct socket *syn_cache_get(struct sockaddr *, struct sockaddr *,
815 		struct tcphdr *, unsigned int, unsigned int,
816 		struct socket *so, struct mbuf *);
817 void	 syn_cache_init(void);
818 void	 syn_cache_insert(struct syn_cache *, struct tcpcb *);
819 struct syn_cache *syn_cache_lookup(struct sockaddr *, struct sockaddr *,
820 		struct syn_cache_head **);
821 void	 syn_cache_reset(struct sockaddr *, struct sockaddr *,
822 		struct tcphdr *);
823 int	 syn_cache_respond(struct syn_cache *, struct mbuf *);
824 void	 syn_cache_timer(void *);
825 void	 syn_cache_cleanup(struct tcpcb *);
826 
827 void	 tcp_reno_newack(struct tcpcb *, struct tcphdr *);
828 void	 tcp_newreno_newack(struct tcpcb *, struct tcphdr *);
829 
830 int	 tcp_input_checksum(int, struct mbuf *, const struct tcphdr *, int, int,
831     int);
832 #endif
833 
834 #endif /* _NETINET_TCP_VAR_H_ */
835