xref: /openbsd/sys/netinet/tcp_var.h (revision 0f9e9ec2)
1 /*	$OpenBSD: tcp_var.h,v 1.178 2024/05/13 01:15:53 jsg Exp $	*/
2 /*	$NetBSD: tcp_var.h,v 1.17 1996/02/13 23:44:24 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)tcp_var.h	8.3 (Berkeley) 4/10/94
33  */
34 
35 #ifndef _NETINET_TCP_VAR_H_
36 #define _NETINET_TCP_VAR_H_
37 
38 #include <sys/timeout.h>
39 
40 /*
41  * Kernel variables for tcp.
42  */
43 
44 struct sackblk {
45 	tcp_seq start;		/* start seq no. of sack block */
46 	tcp_seq end;		/* end seq no. */
47 };
48 
49 struct sackhole {
50 	tcp_seq start;		/* start seq no. of hole */
51 	tcp_seq end;		/* end seq no. */
52 	int	dups;		/* number of dup(s)acks for this hole */
53 	tcp_seq rxmit;		/* next seq. no in hole to be retransmitted */
54 	struct sackhole *next;	/* next in list */
55 };
56 
57 /*
58  * TCP sequence queue structures.
59  */
60 TAILQ_HEAD(tcpqehead, tcpqent);
61 struct tcpqent {
62 	TAILQ_ENTRY(tcpqent) tcpqe_q;
63 	struct tcphdr	*tcpqe_tcp;
64 	struct mbuf	*tcpqe_m;	/* mbuf contains packet */
65 };
66 
67 /*
68  * Tcp control block, one per tcp; fields:
69  */
70 struct tcpcb {
71 	struct tcpqehead t_segq;		/* sequencing queue */
72 	struct timeout t_timer[TCPT_NTIMERS];	/* tcp timers */
73 	short	t_state;		/* state of this connection */
74 	short	t_rxtshift;		/* log(2) of rexmt exp. backoff */
75 	int	t_rxtcur;		/* current retransmit value */
76 	short	t_dupacks;		/* consecutive dup acks recd */
77 	u_short	t_maxseg;		/* maximum segment size */
78 	char	t_force;		/* 1 if forcing out a byte */
79 	u_int	t_flags;
80 #define	TF_ACKNOW	0x0001U		/* ack peer immediately */
81 #define	TF_NODELAY	0x0004U		/* don't delay packets to coalesce */
82 #define	TF_NOOPT	0x0008U		/* don't use tcp options */
83 #define	TF_SENTFIN	0x0010U		/* have sent FIN */
84 #define	TF_REQ_SCALE	0x0020U		/* have/will request window scaling */
85 #define	TF_RCVD_SCALE	0x0040U		/* other side has requested scaling */
86 #define	TF_REQ_TSTMP	0x0080U		/* have/will request timestamps */
87 #define	TF_RCVD_TSTMP	0x0100U		/* a timestamp was received in SYN */
88 #define	TF_SACK_PERMIT	0x0200U		/* other side said I could SACK */
89 #define	TF_SIGNATURE	0x0400U		/* require TCP MD5 signature */
90 #ifdef TCP_ECN
91 #define TF_ECN_PERMIT	0x00008000U	/* other side said I could ECN */
92 #define TF_RCVD_CE	0x00010000U	/* send ECE in subsequent segs */
93 #define TF_SEND_CWR	0x00020000U	/* send CWR in next seg */
94 #define TF_DISABLE_ECN	0x00040000U	/* disable ECN for this connection */
95 #endif
96 #define TF_LASTIDLE	0x00100000U	/* no outstanding ACK on last send */
97 #define TF_PMTUD_PEND	0x00400000U	/* Path MTU Discovery pending */
98 #define TF_NEEDOUTPUT	0x00800000U	/* call tcp_output after tcp_input */
99 #define TF_BLOCKOUTPUT	0x01000000U	/* avert tcp_output during tcp_input */
100 #define TF_NOPUSH	0x02000000U	/* don't push */
101 #define TF_TMR_REXMT	0x04000000U	/* retransmit timer armed */
102 #define TF_TMR_PERSIST	0x08000000U	/* retransmit persistence timer armed */
103 #define TF_TMR_KEEP	0x10000000U	/* keep alive timer armed */
104 #define TF_TMR_2MSL	0x20000000U	/* 2*msl quiet time timer armed */
105 #define TF_TMR_REAPER	0x40000000U	/* delayed cleanup timer armed, dead */
106 #define TF_TMR_DELACK	0x80000000U	/* delayed ack timer armed */
107 #define TF_TIMER	TF_TMR_REXMT	/* used to shift with TCPT values */
108 
109 	struct	mbuf *t_template;	/* skeletal packet for transmit */
110 	struct	inpcb *t_inpcb;		/* back pointer to internet pcb */
111 /*
112  * The following fields are used as in the protocol specification.
113  * See RFC793, Dec. 1981, page 21.
114  */
115 /* send sequence variables */
116 	tcp_seq	snd_una;		/* send unacknowledged */
117 	tcp_seq	snd_nxt;		/* send next */
118 	tcp_seq	snd_up;			/* send urgent pointer */
119 	tcp_seq	snd_wl1;		/* window update seg seq number */
120 	tcp_seq	snd_wl2;		/* window update seg ack number */
121 	tcp_seq	iss;			/* initial send sequence number */
122 	u_long	snd_wnd;		/* send window */
123 	int	sack_enable;		/* enable SACK for this connection */
124 	int	snd_numholes;		/* number of holes seen by sender */
125 	struct sackhole *snd_holes;	/* linked list of holes (sorted) */
126 	tcp_seq snd_last;		/* for use in fast recovery */
127 /* receive sequence variables */
128 	u_long	rcv_wnd;		/* receive window */
129 	tcp_seq	rcv_nxt;		/* receive next */
130 	tcp_seq	rcv_up;			/* receive urgent pointer */
131 	tcp_seq	irs;			/* initial receive sequence number */
132 	tcp_seq rcv_lastsack;		/* last seq number(+1) sack'd by rcv'r*/
133 	int	rcv_numsacks;		/* # distinct sack blks present */
134 	struct sackblk sackblks[MAX_SACK_BLKS]; /* seq nos. of sack blocks */
135 
136 /*
137  * Additional variables for this implementation.
138  */
139 /* receive variables */
140 	tcp_seq	rcv_adv;		/* advertised window */
141 /* retransmit variables */
142 	tcp_seq	snd_max;		/* highest sequence number sent;
143 					 * used to recognize retransmits
144 					 */
145 /* congestion control (for slow start, source quench, retransmit after loss) */
146 	u_long	snd_cwnd;		/* congestion-controlled window */
147 	u_long	snd_ssthresh;		/* snd_cwnd size threshold for
148 					 * for slow start exponential to
149 					 * linear switch
150 					 */
151 
152 /* auto-sizing variables */
153 	uint64_t rfbuf_ts;	/* recv buffer autoscaling time stamp */
154 	u_int	rfbuf_cnt;	/* recv buffer autoscaling byte count */
155 
156 	u_short	t_maxopd;		/* mss plus options */
157 	u_short	t_peermss;		/* peer's maximum segment size */
158 
159 /*
160  * transmit timing stuff.  See below for scale of srtt and rttvar.
161  * "Variance" is actually smoothed difference.
162  */
163 	uint64_t t_rcvtime;		/* time last segment received */
164 	uint64_t t_rcvacktime;		/* time last ack received */
165 	uint64_t t_sndtime;		/* time last segment sent */
166 	uint64_t t_sndacktime;		/* time last ack sent */
167 	uint64_t t_rtttime;		/* time we started measuring rtt */
168 	tcp_seq	t_rtseq;		/* sequence number being timed */
169 	int	t_srtt;			/* smoothed round-trip time */
170 	int	t_rttvar;		/* variance in round-trip time */
171 	u_int	t_rttmin;		/* minimum rtt allowed */
172 	u_long	max_sndwnd;		/* largest window peer has offered */
173 
174 /* out-of-band data */
175 	char	t_oobflags;		/* have some */
176 	char	t_iobc;			/* input character */
177 #define	TCPOOB_HAVEDATA	0x01
178 #define	TCPOOB_HADDATA	0x02
179 	short	t_softerror;		/* possible error not yet reported */
180 
181 /* RFC 1323 variables */
182 	u_char	snd_scale;		/* window scaling for send window */
183 	u_char	rcv_scale;		/* window scaling for recv window */
184 	u_char	request_r_scale;	/* pending window scaling */
185 	u_char	requested_s_scale;
186 	uint32_t ts_recent;		/* timestamp echo data */
187 	uint32_t ts_modulate;		/* modulation on timestamp */
188 	uint64_t ts_recent_age;		/* when last updated */
189 	tcp_seq	last_ack_sent;
190 
191 /* pointer for syn cache entries*/
192 	LIST_HEAD(, syn_cache) t_sc;	/* list of entries by this tcb */
193 
194 /* Path-MTU Discovery Information */
195 	u_int	t_pmtud_mss_acked;	/* MSS acked, lower bound for MTU */
196 	u_int	t_pmtud_mtu_sent;	/* MTU used, upper bound for MTU */
197 	tcp_seq	t_pmtud_th_seq;		/* TCP SEQ from ICMP payload */
198 	u_int	t_pmtud_nextmtu;	/* Advertised Next-Hop MTU from ICMP */
199 	u_short	t_pmtud_ip_len;		/* IP length from ICMP payload */
200 	u_short	t_pmtud_ip_hl;		/* IP header length from ICMP payload */
201 
202 	int pf;
203 
204 /* maintain a few stats per connection: */
205 	u_int	t_rcvoopack;		/* out-of-order packets received */
206 	u_int	t_sndrexmitpack;	/* retransmit packets sent */
207 	u_int	t_sndzerowin;		/* zero-window updates sent */
208 };
209 
210 #define	intotcpcb(ip)	((struct tcpcb *)(ip)->inp_ppcb)
211 #define	sototcpcb(so)	(intotcpcb(sotoinpcb(so)))
212 
213 #ifdef _KERNEL
214 /*
215  * Handy way of passing around TCP option info.
216  */
217 struct tcp_opt_info {
218 	int		ts_present;
219 	u_int32_t	ts_val;
220 	u_int32_t	ts_ecr;
221 	u_int16_t	maxseg;
222 };
223 
224 /*
225  * Data for the TCP compressed state engine.
226  */
227 
228 /*
229  * Locks used to protect global data and struct members:
230  *	I	immutable after creation
231  *	N	net lock
232  *	S	syn_cache_mtx		tcp syn cache global mutex
233  */
234 
235 extern struct mutex syn_cache_mtx;
236 
237 #define	TCP_SYN_HASH_SIZE	293
238 #define	TCP_SYN_BUCKET_SIZE	35
239 
240 union syn_cache_sa {
241 	struct sockaddr sa;
242 	struct sockaddr_in sin;
243 	struct sockaddr_in6 sin6;
244 };
245 
246 struct syn_cache {
247 	TAILQ_ENTRY(syn_cache) sc_bucketq;	/* [S] link on bucket list */
248 	struct refcnt sc_refcnt;		/* ref count list and timer */
249 	struct timeout sc_timer;		/* rexmt timer */
250 	struct route sc_route;			/* [N] cached route */
251 	long sc_win;				/* [I] advertised window */
252 	struct syn_cache_head *sc_buckethead;	/* [S] our bucket index */
253 	struct syn_cache_set *sc_set;		/* [S] our syn cache set */
254 	u_int64_t sc_timestamp;		/* [N] timestamp from SYN */
255 	u_int32_t sc_hash;		/* [S] */
256 	u_int32_t sc_modulate;		/* [I] our timestamp modulator */
257 	union syn_cache_sa sc_src;	/* [I] */
258 	union syn_cache_sa sc_dst;	/* [I] */
259 	tcp_seq sc_irs;			/* [I] */
260 	tcp_seq sc_iss;			/* [I] */
261 	u_int sc_rtableid;		/* [I] */
262 	u_int sc_rxtcur;		/* [S] current rxt timeout */
263 	u_int sc_rxttot;		/* [S] total time spend on queues */
264 	u_int sc_rxtshift;		/* [S] for computing backoff */
265 	u_int sc_dynflags;		/* [S] flags accessed with mutex */
266 #define SCF_UNREACH	0x0001U		/* we've had an unreach error */
267 #define SCF_DEAD	0x0002U		/* this entry to be released */
268 
269 	u_short sc_fixflags;		/* [I] set during initialization */
270 #define SCF_TIMESTAMP	0x0010U		/* peer will do timestamps */
271 #define SCF_SACK_PERMIT	0x0020U		/* permit sack */
272 #define SCF_ECN_PERMIT	0x0040U		/* permit ecn */
273 #define SCF_SIGNATURE	0x0080U		/* enforce tcp signatures */
274 
275 	struct mbuf *sc_ipopts;			/* [N] IP options */
276 	u_int16_t sc_peermaxseg;		/* [I] */
277 	u_int16_t sc_ourmaxseg;			/* [I] */
278 	u_int     sc_request_r_scale	: 4,	/* [I] */
279 		  sc_requested_s_scale	: 4;	/* [I] */
280 
281 	struct tcpcb *sc_tp;		/* [S] tcb for listening socket */
282 	LIST_ENTRY(syn_cache) sc_tpq;	/* [S] list of entries by same tp */
283 };
284 
285 struct syn_cache_head {
286 	TAILQ_HEAD(, syn_cache) sch_bucket;	/* [S] bucket entries */
287 	u_short sch_length;			/* [S] # entries in bucket */
288 };
289 
290 struct syn_cache_set {
291 	struct		syn_cache_head *scs_buckethead;	/* [S] */
292 	long		scs_use;	/* [S] */
293 	int		scs_size;	/* [S] current size of hash table */
294 	int		scs_count;	/* [S] */
295 	u_int32_t	scs_random[5];	/* [S] */
296 };
297 
298 #endif /* _KERNEL */
299 
300 /*
301  * The smoothed round-trip time and estimated variance
302  * are stored as fixed point numbers scaled by the values below.
303  * For convenience, these scales are also used in smoothing the average
304  * (smoothed = (1/scale)sample + ((scale-1)/scale)smoothed).
305  * With these scales, srtt has 5 bits to the right of the binary point,
306  * and thus an "ALPHA" of 0.875.  rttvar has 4 bits to the right of the
307  * binary point, and is smoothed with an ALPHA of 0.75.
308  */
309 #define	TCP_RTT_SHIFT		3	/* shift for srtt; 5 bits frac. */
310 #define	TCP_RTTVAR_SHIFT	2	/* shift for rttvar; 4 bits */
311 #define	TCP_RTT_BASE_SHIFT	2	/* remaining 2 bit shift */
312 #define	TCP_RTT_MAX		(1<<18)	/* maximum rtt */
313 
314 /*
315  * The initial retransmission should happen at rtt + 4 * rttvar.
316  * Because of the way we do the smoothing, srtt and rttvar
317  * will each average +1/2 tick of bias.  When we compute
318  * the retransmit timer, we want 1/2 tick of rounding and
319  * 1 extra tick because of +-1/2 tick uncertainty in the
320  * firing of the timer.  The bias will give us exactly the
321  * 1.5 tick we need.  But, because the bias is
322  * statistical, we have to test that we don't drop below
323  * the minimum feasible timer (which is 2 ticks).
324  * This macro assumes that the value of (1 << TCP_RTTVAR_SHIFT)
325  * is the same as the multiplier for rttvar.
326  */
327 #define	TCP_REXMTVAL(tp) \
328 	((((tp)->t_srtt >> TCP_RTT_SHIFT) + (tp)->t_rttvar) \
329 	    >> TCP_RTT_BASE_SHIFT)
330 
331 /*
332  * TCP statistics.
333  * Many of these should be kept per connection,
334  * but that's inconvenient at the moment.
335  */
336 struct	tcpstat {
337 	u_int32_t tcps_connattempt;	/* connections initiated */
338 	u_int32_t tcps_accepts;		/* connections accepted */
339 	u_int32_t tcps_connects;	/* connections established */
340 	u_int32_t tcps_drops;		/* connections dropped */
341 	u_int32_t tcps_conndrops;	/* embryonic connections dropped */
342 	u_int32_t tcps_closed;		/* conn. closed (includes drops) */
343 	u_int32_t tcps_segstimed;	/* segs where we tried to get rtt */
344 	u_int32_t tcps_rttupdated;	/* times we succeeded */
345 	u_int32_t tcps_delack;		/* delayed acks sent */
346 	u_int32_t tcps_timeoutdrop;	/* conn. dropped in rxmt timeout */
347 	u_int32_t tcps_rexmttimeo;	/* retransmit timeouts */
348 	u_int32_t tcps_persisttimeo;	/* persist timeouts */
349 	u_int32_t tcps_persistdrop;	/* connections dropped in persist */
350 	u_int32_t tcps_keeptimeo;	/* keepalive timeouts */
351 	u_int32_t tcps_keepprobe;	/* keepalive probes sent */
352 	u_int32_t tcps_keepdrops;	/* connections dropped in keepalive */
353 
354 	u_int32_t tcps_sndtotal;		/* total packets sent */
355 	u_int32_t tcps_sndpack;		/* data packets sent */
356 	u_int64_t tcps_sndbyte;		/* data bytes sent */
357 	u_int32_t tcps_sndrexmitpack;	/* data packets retransmitted */
358 	u_int64_t tcps_sndrexmitbyte;	/* data bytes retransmitted */
359 	u_int64_t tcps_sndrexmitfast;	/* Fast retransmits */
360 	u_int32_t tcps_sndacks;		/* ack-only packets sent */
361 	u_int32_t tcps_sndprobe;	/* window probes sent */
362 	u_int32_t tcps_sndurg;		/* packets sent with URG only */
363 	u_int32_t tcps_sndwinup;	/* window update-only packets sent */
364 	u_int32_t tcps_sndctrl;		/* control (SYN|FIN|RST) packets sent */
365 
366 	u_int32_t tcps_rcvtotal;	/* total packets received */
367 	u_int32_t tcps_rcvpack;		/* packets received in sequence */
368 	u_int64_t tcps_rcvbyte;		/* bytes received in sequence */
369 	u_int32_t tcps_rcvbadsum;	/* packets received with ccksum errs */
370 	u_int32_t tcps_rcvbadoff;	/* packets received with bad offset */
371 	u_int32_t tcps_rcvmemdrop;	/* packets dropped for lack of memory */
372 	u_int32_t tcps_rcvnosec;	/* packets dropped for lack of ipsec */
373 	u_int32_t tcps_rcvshort;	/* packets received too short */
374 	u_int32_t tcps_rcvduppack;	/* duplicate-only packets received */
375 	u_int64_t tcps_rcvdupbyte;	/* duplicate-only bytes received */
376 	u_int32_t tcps_rcvpartduppack;	/* packets with some duplicate data */
377 	u_int64_t tcps_rcvpartdupbyte;	/* dup. bytes in part-dup. packets */
378 	u_int32_t tcps_rcvoopack;	/* out-of-order packets received */
379 	u_int64_t tcps_rcvoobyte;	/* out-of-order bytes received */
380 	u_int32_t tcps_rcvpackafterwin;	/* packets with data after window */
381 	u_int64_t tcps_rcvbyteafterwin;	/* bytes rcvd after window */
382 	u_int32_t tcps_rcvafterclose;	/* packets rcvd after "close" */
383 	u_int32_t tcps_rcvwinprobe;	/* rcvd window probe packets */
384 	u_int32_t tcps_rcvdupack;	/* rcvd duplicate acks */
385 	u_int32_t tcps_rcvacktoomuch;	/* rcvd acks for unsent data */
386 	u_int32_t tcps_rcvacktooold;	/* rcvd acks for old data */
387 	u_int32_t tcps_rcvackpack;	/* rcvd ack packets */
388 	u_int64_t tcps_rcvackbyte;	/* bytes acked by rcvd acks */
389 	u_int32_t tcps_rcvwinupd;	/* rcvd window update packets */
390 	u_int32_t tcps_pawsdrop;	/* segments dropped due to PAWS */
391 	u_int32_t tcps_predack;		/* times hdr predict ok for acks */
392 	u_int32_t tcps_preddat;		/* times hdr predict ok for data pkts */
393 
394 	u_int32_t tcps_pcbhashmiss;	/* input packets missing pcb hash */
395 	u_int32_t tcps_noport;		/* no socket on port */
396 	u_int32_t tcps_badsyn;		/* SYN packet with src==dst rcv'ed */
397 	u_int32_t tcps_dropsyn;		/* SYN packet dropped */
398 
399 	u_int32_t tcps_rcvbadsig;	/* rcvd bad/missing TCP signatures */
400 	u_int64_t tcps_rcvgoodsig;	/* rcvd good TCP signatures */
401 	u_int32_t tcps_inswcsum;	/* input software-checksummed pkts */
402 	u_int32_t tcps_outswcsum;	/* output software-checksummed pkts */
403 
404 	/* ECN stats */
405 	u_int32_t tcps_ecn_accepts;	/* ecn connections accepted */
406 	u_int32_t tcps_ecn_rcvece;	/* # of rcvd ece */
407 	u_int32_t tcps_ecn_rcvcwr;	/* # of rcvd cwr */
408 	u_int32_t tcps_ecn_rcvce;	/* # of rcvd ce in ip header */
409 	u_int32_t tcps_ecn_sndect;	/* # of cwr sent */
410 	u_int32_t tcps_ecn_sndece;	/* # of ece sent */
411 	u_int32_t tcps_ecn_sndcwr;	/* # of cwr sent */
412 	u_int32_t tcps_cwr_ecn;		/* # of cwnd reduced by ecn */
413 	u_int32_t tcps_cwr_frecovery;	/* # of cwnd reduced by fastrecovery */
414 	u_int32_t tcps_cwr_timeout;	/* # of cwnd reduced by timeout */
415 
416 	/* These statistics deal with the SYN cache. */
417 	u_int64_t tcps_sc_added;	/* # of entries added */
418 	u_int64_t tcps_sc_completed;	/* # of connections completed */
419 	u_int64_t tcps_sc_timed_out;	/* # of entries timed out */
420 	u_int64_t tcps_sc_overflowed;	/* # dropped due to overflow */
421 	u_int64_t tcps_sc_reset;	/* # dropped due to RST */
422 	u_int64_t tcps_sc_unreach;	/* # dropped due to ICMP unreach */
423 	u_int64_t tcps_sc_bucketoverflow;/* # dropped due to bucket overflow */
424 	u_int64_t tcps_sc_aborted;	/* # of entries aborted (no mem) */
425 	u_int64_t tcps_sc_dupesyn;	/* # of duplicate SYNs received */
426 	u_int64_t tcps_sc_dropped;	/* # of SYNs dropped (no route/mem) */
427 	u_int64_t tcps_sc_collisions;	/* # of hash collisions */
428 	u_int64_t tcps_sc_retransmitted;/* # of retransmissions */
429 	u_int64_t tcps_sc_seedrandom;	/* # of syn cache seeds with random */
430 	u_int64_t tcps_sc_hash_size;	/* hash buckets in current syn cache */
431 	u_int64_t tcps_sc_entry_count;	/* # of entries in current syn cache */
432 	u_int64_t tcps_sc_entry_limit;	/* limit of syn cache entries */
433 	u_int64_t tcps_sc_bucket_maxlen;/* maximum # of entries in any bucket */
434 	u_int64_t tcps_sc_bucket_limit;	/* limit of syn cache bucket list */
435 	int64_t tcps_sc_uses_left;	/* use counter of current syn cache */
436 
437 	u_int64_t tcps_conndrained;	/* # of connections drained */
438 
439 	u_int64_t tcps_sack_recovery_episode;	/* SACK recovery episodes */
440 	u_int64_t tcps_sack_rexmits;		/* SACK rexmit segments */
441 	u_int64_t tcps_sack_rexmit_bytes;	/* SACK rexmit bytes */
442 	u_int64_t tcps_sack_rcv_opts;		/* SACK options received */
443 	u_int64_t tcps_sack_snd_opts;		/* SACK options sent */
444 	u_int64_t tcps_sack_drop_opts;		/* SACK options dropped */
445 
446 	u_int32_t tcps_outswtso;	/* output tso chopped in software */
447 	u_int32_t tcps_outhwtso;	/* output tso processed by hardware */
448 	u_int32_t tcps_outpkttso;	/* packets generated by tso */
449 	u_int32_t tcps_outbadtso;	/* output tso failed, packet dropped */
450 	u_int32_t tcps_inswlro;		/* input lro on pseudo device */
451 	u_int32_t tcps_inhwlro;		/* input lro from hardware */
452 	u_int32_t tcps_inpktlro;	/* packets coalesced by hardware lro */
453 	u_int32_t tcps_inbadlro;	/* input bad lro packets */
454 };
455 
456 /*
457  * Names for TCP sysctl objects.
458  */
459 
460 #define TCPCTL_RFC1323		1 /* enable RFC1323 timestamps/scaling */
461 #define TCPCTL_KEEPINITTIME	2 /* TCPT_KEEP value */
462 #define TCPCTL_KEEPIDLE		3 /* allow tcp_keepidle to be changed */
463 #define TCPCTL_KEEPINTVL	4 /* allow tcp_keepintvl to be changed */
464 #define TCPCTL_SLOWHZ		5 /* return kernel idea of PR_SLOWHZ */
465 #define TCPCTL_BADDYNAMIC	6 /* return bad dynamic port bitmap */
466 #define	TCPCTL_RECVSPACE	7 /* receive buffer space */
467 #define	TCPCTL_SENDSPACE	8 /* send buffer space */
468 #define	TCPCTL_IDENT		9 /* get connection owner */
469 #define	TCPCTL_SACK	       10 /* selective acknowledgement, rfc 2018 */
470 #define TCPCTL_MSSDFLT	       11 /* Default maximum segment size */
471 #define	TCPCTL_RSTPPSLIMIT     12 /* RST pps limit */
472 #define	TCPCTL_ACK_ON_PUSH     13 /* ACK immediately on PUSH */
473 #define	TCPCTL_ECN	       14 /* RFC3168 ECN */
474 #define	TCPCTL_SYN_CACHE_LIMIT 15 /* max size of comp. state engine */
475 #define	TCPCTL_SYN_BUCKET_LIMIT	16 /* max size of hash bucket */
476 #define	TCPCTL_RFC3390	       17 /* enable/disable RFC3390 increased cwnd */
477 #define	TCPCTL_REASS_LIMIT     18 /* max entries for tcp reass queues */
478 #define	TCPCTL_DROP	       19 /* drop tcp connection */
479 #define	TCPCTL_SACKHOLE_LIMIT  20 /* max entries for tcp sack queues */
480 #define	TCPCTL_STATS	       21 /* TCP statistics */
481 #define	TCPCTL_ALWAYS_KEEPALIVE 22 /* assume SO_KEEPALIVE is always set */
482 #define	TCPCTL_SYN_USE_LIMIT   23 /* number of uses before reseeding hash */
483 #define TCPCTL_ROOTONLY	       24 /* return root only port bitmap */
484 #define	TCPCTL_SYN_HASH_SIZE   25 /* number of buckets in the hash */
485 #define	TCPCTL_TSO	       26 /* enable TCP segmentation offload */
486 #define	TCPCTL_MAXID	       27
487 
488 #define	TCPCTL_NAMES { \
489 	{ 0, 0 }, \
490 	{ "rfc1323",	CTLTYPE_INT }, \
491 	{ "keepinittime",	CTLTYPE_INT }, \
492 	{ "keepidle",	CTLTYPE_INT }, \
493 	{ "keepintvl",	CTLTYPE_INT }, \
494 	{ NULL,	0 }, \
495 	{ "baddynamic", CTLTYPE_STRUCT }, \
496 	{ NULL,	0 }, \
497 	{ NULL,	0 }, \
498 	{ "ident",	CTLTYPE_STRUCT }, \
499 	{ "sack",	CTLTYPE_INT }, \
500 	{ "mssdflt",	CTLTYPE_INT }, \
501 	{ "rstppslimit",	CTLTYPE_INT }, \
502 	{ "ackonpush",	CTLTYPE_INT }, \
503 	{ "ecn",	CTLTYPE_INT }, \
504 	{ "syncachelimit",	CTLTYPE_INT }, \
505 	{ "synbucketlimit",	CTLTYPE_INT }, \
506 	{ "rfc3390",	CTLTYPE_INT }, \
507 	{ "reasslimit",	CTLTYPE_INT }, \
508 	{ "drop",	CTLTYPE_STRUCT }, \
509 	{ "sackholelimit",	CTLTYPE_INT }, \
510 	{ "stats",	CTLTYPE_STRUCT }, \
511 	{ "always_keepalive",	CTLTYPE_INT }, \
512 	{ "synuselimit",	CTLTYPE_INT }, \
513 	{ "rootonly",	CTLTYPE_STRUCT }, \
514 	{ "synhashsize",	CTLTYPE_INT }, \
515 	{ "tso",	CTLTYPE_INT }, \
516 }
517 
518 struct tcp_ident_mapping {
519 	struct sockaddr_storage faddr, laddr;
520 	int euid, ruid;
521 	u_int rdomain;
522 };
523 
524 #ifdef _KERNEL
525 
526 #include <sys/percpu.h>
527 #include <sys/stat.h>
528 
529 enum tcpstat_counters {
530 	tcps_connattempt,
531 	tcps_accepts,
532 	tcps_connects,
533 	tcps_drops,
534 	tcps_conndrops,
535 	tcps_closed,
536 	tcps_segstimed,
537 	tcps_rttupdated,
538 	tcps_delack,
539 	tcps_timeoutdrop,
540 	tcps_rexmttimeo,
541 	tcps_persisttimeo,
542 	tcps_persistdrop,
543 	tcps_keeptimeo,
544 	tcps_keepprobe,
545 	tcps_keepdrops,
546 	tcps_sndtotal,
547 	tcps_sndpack,
548 	tcps_sndbyte,
549 	tcps_sndrexmitpack,
550 	tcps_sndrexmitbyte,
551 	tcps_sndrexmitfast,
552 	tcps_sndacks,
553 	tcps_sndprobe,
554 	tcps_sndurg,
555 	tcps_sndwinup,
556 	tcps_sndctrl,
557 	tcps_rcvtotal,
558 	tcps_rcvpack,
559 	tcps_rcvbyte,
560 	tcps_rcvbadsum,
561 	tcps_rcvbadoff,
562 	tcps_rcvmemdrop,
563 	tcps_rcvnosec,
564 	tcps_rcvshort,
565 	tcps_rcvduppack,
566 	tcps_rcvdupbyte,
567 	tcps_rcvpartduppack,
568 	tcps_rcvpartdupbyte,
569 	tcps_rcvoopack,
570 	tcps_rcvoobyte,
571 	tcps_rcvpackafterwin,
572 	tcps_rcvbyteafterwin,
573 	tcps_rcvafterclose,
574 	tcps_rcvwinprobe,
575 	tcps_rcvdupack,
576 	tcps_rcvacktoomuch,
577 	tcps_rcvacktooold,
578 	tcps_rcvackpack,
579 	tcps_rcvackbyte,
580 	tcps_rcvwinupd,
581 	tcps_pawsdrop,
582 	tcps_predack,
583 	tcps_preddat,
584 	tcps_pcbhashmiss,
585 	tcps_noport,
586 	tcps_badsyn,
587 	tcps_dropsyn,
588 	tcps_rcvbadsig,
589 	tcps_rcvgoodsig,
590 	tcps_inswcsum,
591 	tcps_outswcsum,
592 	tcps_ecn_accepts,
593 	tcps_ecn_rcvece,
594 	tcps_ecn_rcvcwr,
595 	tcps_ecn_rcvce,
596 	tcps_ecn_sndect,
597 	tcps_ecn_sndece,
598 	tcps_ecn_sndcwr,
599 	tcps_cwr_ecn,
600 	tcps_cwr_frecovery,
601 	tcps_cwr_timeout,
602 	tcps_sc_added,
603 	tcps_sc_completed,
604 	tcps_sc_timed_out,
605 	tcps_sc_overflowed,
606 	tcps_sc_reset,
607 	tcps_sc_unreach,
608 	tcps_sc_bucketoverflow,
609 	tcps_sc_aborted,
610 	tcps_sc_dupesyn,
611 	tcps_sc_dropped,
612 	tcps_sc_collisions,
613 	tcps_sc_retransmitted,
614 	tcps_sc_seedrandom,
615 	tcps_sc_hash_size,
616 	tcps_sc_entry_count,
617 	tcps_sc_entry_limit,
618 	tcps_sc_bucket_maxlen,
619 	tcps_sc_bucket_limit,
620 	tcps_sc_uses_left,
621 	tcps_conndrained,
622 	tcps_sack_recovery_episode,
623 	tcps_sack_rexmits,
624 	tcps_sack_rexmit_bytes,
625 	tcps_sack_rcv_opts,
626 	tcps_sack_snd_opts,
627 	tcps_sack_drop_opts,
628 	tcps_outswtso,
629 	tcps_outhwtso,
630 	tcps_outpkttso,
631 	tcps_outbadtso,
632 	tcps_inswlro,
633 	tcps_inhwlro,
634 	tcps_inpktlro,
635 	tcps_inbadlro,
636 	tcps_ncounters,
637 };
638 
639 extern struct cpumem *tcpcounters;
640 
641 static inline void
tcpstat_inc(enum tcpstat_counters c)642 tcpstat_inc(enum tcpstat_counters c)
643 {
644 	counters_inc(tcpcounters, c);
645 }
646 
647 static inline void
tcpstat_add(enum tcpstat_counters c,uint64_t v)648 tcpstat_add(enum tcpstat_counters c, uint64_t v)
649 {
650 	counters_add(tcpcounters, c, v);
651 }
652 
653 static inline void
tcpstat_pkt(enum tcpstat_counters pcounter,enum tcpstat_counters bcounter,uint64_t v)654 tcpstat_pkt(enum tcpstat_counters pcounter, enum tcpstat_counters bcounter,
655     uint64_t v)
656 {
657 	counters_pkt(tcpcounters, pcounter, bcounter, v);
658 }
659 
660 extern uint64_t tcp_starttime;
661 
662 static inline uint64_t
tcp_now(void)663 tcp_now(void)
664 {
665 	/* TCP time ticks in 63 bit milliseconds with 63 bit random offset. */
666 	return tcp_starttime + (getnsecruntime() / 1000000ULL);
667 }
668 
669 #define TCP_TIME(_sec)	((_sec) * 1000)	/* tcp_now() is in milliseconds */
670 
671 extern	struct mutex tcp_timer_mtx;
672 extern	const struct pr_usrreqs tcp_usrreqs;
673 
674 #ifdef INET6
675 extern	const struct pr_usrreqs tcp6_usrreqs;
676 #endif
677 
678 extern	struct pool tcpcb_pool;
679 extern	struct inpcbtable tcbtable, tcb6table;	/* queue of active tcpcb's */
680 extern	int tcp_do_rfc1323;	/* enabled/disabled? */
681 extern	int tcptv_keep_init;	/* [N] time to keep alive initial SYN packet */
682 extern	int tcp_mssdflt;	/* default maximum segment size */
683 extern	int tcp_rst_ppslim;	/* maximum outgoing RST packet per second */
684 extern	int tcp_ack_on_push;	/* ACK immediately on PUSH */
685 extern	int tcp_do_sack;	/* SACK enabled/disabled */
686 extern	struct pool sackhl_pool;
687 extern	int tcp_sackhole_limit;	/* max entries for tcp sack queues */
688 extern	int tcp_do_ecn;		/* RFC3168 ECN enabled/disabled? */
689 extern	int tcp_do_rfc3390;	/* RFC3390 Increasing TCP's Initial Window */
690 extern	int tcp_do_tso;		/* enable TSO for TCP output packets */
691 
692 extern	struct pool tcpqe_pool;
693 extern	int tcp_reass_limit;	/* max entries for tcp reass queues */
694 
695 extern	int tcp_syn_hash_size;  /* adjustable size of the hash array */
696 extern	int tcp_syn_cache_limit; /* max entries for compressed state engine */
697 extern	int tcp_syn_bucket_limit;/* max entries per hash bucket */
698 extern	int tcp_syn_use_limit;   /* number of uses before reseeding hash */
699 extern	struct syn_cache_set tcp_syn_cache[];
700 extern	int tcp_syn_cache_active; /* active syn cache, may be 0 or 1 */
701 
702 struct tdb;
703 
704 void	 tcp_canceltimers(struct tcpcb *);
705 struct tcpcb *
706 	 tcp_close(struct tcpcb *);
707 int	 tcp_freeq(struct tcpcb *);
708 #ifdef INET6
709 void	 tcp6_ctlinput(int, struct sockaddr *, u_int, void *);
710 #endif
711 void	 tcp_ctlinput(int, struct sockaddr *, u_int, void *);
712 int	 tcp_ctloutput(int, struct socket *, int, int, struct mbuf *);
713 struct tcpcb *
714 	 tcp_dodisconnect(struct tcpcb *);
715 struct tcpcb *
716 	 tcp_drop(struct tcpcb *, int);
717 int	 tcp_dooptions(struct tcpcb *, u_char *, int, struct tcphdr *,
718 		struct mbuf *, int, struct tcp_opt_info *, u_int, uint64_t);
719 void	 tcp_init(void);
720 int	 tcp_input(struct mbuf **, int *, int, int);
721 int	 tcp_mss(struct tcpcb *, int);
722 void	 tcp_mss_update(struct tcpcb *);
723 u_int	 tcp_hdrsz(struct tcpcb *);
724 void	 tcp_mtudisc(struct inpcb *, int);
725 void	 tcp_mtudisc_increase(struct inpcb *, int);
726 #ifdef INET6
727 void	tcp6_mtudisc_callback(struct sockaddr_in6 *, u_int);
728 #endif
729 struct tcpcb *
730 	 tcp_newtcpcb(struct inpcb *, int);
731 void	 tcp_notify(struct inpcb *, int);
732 int	 tcp_output(struct tcpcb *);
733 int	 tcp_chopper(struct mbuf *, struct mbuf_list *, struct ifnet *, u_int);
734 int	 tcp_if_output_tso(struct ifnet *, struct mbuf **, struct sockaddr *,
735 	    struct rtentry *, uint32_t, u_int);
736 void	 tcp_pulloutofband(struct socket *, u_int, struct mbuf *, int);
737 int	 tcp_reass(struct tcpcb *, struct tcphdr *, struct mbuf *, int *);
738 void	 tcp_rscale(struct tcpcb *, u_long);
739 void	 tcp_respond(struct tcpcb *, caddr_t, struct tcphdr *, tcp_seq,
740 		tcp_seq, int, u_int, uint64_t);
741 void	 tcp_setpersist(struct tcpcb *);
742 void	 tcp_update_sndspace(struct tcpcb *);
743 void	 tcp_update_rcvspace(struct tcpcb *);
744 void	 tcp_slowtimo(void);
745 struct mbuf *
746 	 tcp_template(struct tcpcb *);
747 #ifndef SMALL_KERNEL
748 void	 tcp_trace(short, short, struct tcpcb *, struct tcpcb *, caddr_t,
749 		int, int);
750 #endif
751 struct tcpcb *
752 	 tcp_usrclosed(struct tcpcb *);
753 int	 tcp_sysctl(int *, u_int, void *, size_t *, void *, size_t);
754 int	 tcp_attach(struct socket *, int, int);
755 int	 tcp_detach(struct socket *);
756 int	 tcp_bind(struct socket *, struct mbuf *, struct proc *);
757 int	 tcp_listen(struct socket *);
758 int	 tcp_connect(struct socket *, struct mbuf *);
759 int	 tcp_accept(struct socket *, struct mbuf *);
760 int	 tcp_disconnect(struct socket *);
761 int	 tcp_shutdown(struct socket *);
762 void	 tcp_rcvd(struct socket *);
763 int	 tcp_send(struct socket *, struct mbuf *, struct mbuf *,
764 	     struct mbuf *);
765 void	 tcp_abort(struct socket *);
766 int	 tcp_sockaddr(struct socket *, struct mbuf *);
767 int	 tcp_peeraddr(struct socket *, struct mbuf *);
768 int	 tcp_sense(struct socket *, struct stat *);
769 int	 tcp_rcvoob(struct socket *, struct mbuf *, int);
770 int	 tcp_sendoob(struct socket *, struct mbuf *, struct mbuf *,
771 	     struct mbuf *);
772 void	 tcp_xmit_timer(struct tcpcb *, int32_t);
773 void	 tcp_sack_option(struct tcpcb *,struct tcphdr *,u_char *,int);
774 void	 tcp_update_sack_list(struct tcpcb *tp, tcp_seq, tcp_seq);
775 void	 tcp_del_sackholes(struct tcpcb *, struct tcphdr *);
776 void	 tcp_clean_sackreport(struct tcpcb *tp);
777 void	 tcp_sack_adjust(struct tcpcb *tp);
778 struct sackhole *
779 	 tcp_sack_output(struct tcpcb *tp);
780 #ifdef DEBUG
781 void	 tcp_print_holes(struct tcpcb *tp);
782 #endif
783 u_long	 tcp_seq_subtract(u_long, u_long );
784 #ifdef TCP_SIGNATURE
785 int	tcp_signature_apply(caddr_t, caddr_t, unsigned int);
786 int	tcp_signature(struct tdb *, int, struct mbuf *, struct tcphdr *,
787 	    int, int, char *);
788 #endif /* TCP_SIGNATURE */
789 void     tcp_set_iss_tsm(struct tcpcb *);
790 
791 void	 syn_cache_unreach(const struct sockaddr *, const struct sockaddr *,
792 	   struct tcphdr *, u_int);
793 void	 syn_cache_init(void);
794 void	 syn_cache_cleanup(struct tcpcb *);
795 
796 #ifdef SMALL_KERNEL
797 static inline void
tcp_trace(short act,short ostate,struct tcpcb * tp,struct tcpcb * otp,caddr_t headers,int req,int len)798 tcp_trace(short act, short ostate, struct tcpcb *tp, struct tcpcb *otp,
799     caddr_t headers, int req, int len)
800 {
801 }
802 #endif
803 
804 #endif /* _KERNEL */
805 #endif /* _NETINET_TCP_VAR_H_ */
806