tcp_var.h revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1993, 1994, 1995
5 *	The Regents of the University of California.  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 * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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 *	@(#)tcp_var.h	8.4 (Berkeley) 5/24/95
32 * $FreeBSD: stable/11/sys/netinet/tcp_var.h 330897 2018-03-14 03:19:51Z eadler $
33 */
34
35#ifndef _NETINET_TCP_VAR_H_
36#define _NETINET_TCP_VAR_H_
37
38#include <netinet/tcp.h>
39#include <netinet/tcp_fsm.h>
40
41#ifdef _KERNEL
42#include <net/vnet.h>
43#include <sys/mbuf.h>
44
45/*
46 * Kernel variables for tcp.
47 */
48VNET_DECLARE(int, tcp_do_rfc1323);
49#define	V_tcp_do_rfc1323	VNET(tcp_do_rfc1323)
50
51#endif /* _KERNEL */
52
53/* TCP segment queue entry */
54struct tseg_qent {
55	LIST_ENTRY(tseg_qent) tqe_q;
56	int	tqe_len;		/* TCP segment data length */
57	struct	tcphdr *tqe_th;		/* a pointer to tcp header */
58	struct	mbuf	*tqe_m;		/* mbuf contains packet */
59};
60LIST_HEAD(tsegqe_head, tseg_qent);
61
62struct sackblk {
63	tcp_seq start;		/* start seq no. of sack block */
64	tcp_seq end;		/* end seq no. */
65};
66
67struct sackhole {
68	tcp_seq start;		/* start seq no. of hole */
69	tcp_seq end;		/* end seq no. */
70	tcp_seq rxmit;		/* next seq. no in hole to be retransmitted */
71	TAILQ_ENTRY(sackhole) scblink;	/* scoreboard linkage */
72};
73
74struct sackhint {
75	struct sackhole	*nexthole;
76	int		sack_bytes_rexmit;
77	tcp_seq		last_sack_ack;	/* Most recent/largest sacked ack */
78
79	int		ispare;		/* explicit pad for 64bit alignment */
80	int             sacked_bytes;	/*
81					 * Total sacked bytes reported by the
82					 * receiver via sack option
83					 */
84	uint32_t	_pad1[1];	/* TBD */
85	uint64_t	_pad[1];	/* TBD */
86};
87
88struct tcptemp {
89	u_char	tt_ipgen[40]; /* the size must be of max ip header, now IPv6 */
90	struct	tcphdr tt_t;
91};
92
93#define tcp6cb		tcpcb  /* for KAME src sync over BSD*'s */
94
95/*
96 * TODO: We yet need to brave plowing in
97 * to tcp_input() and the pru_usrreq() block.
98 * Right now these go to the old standards which
99 * are somewhat ok, but in the long term may
100 * need to be changed. If we do tackle tcp_input()
101 * then we need to get rid of the tcp_do_segment()
102 * function below.
103 */
104/* Flags for tcp functions */
105#define TCP_FUNC_BEING_REMOVED 0x01   	/* Can no longer be referenced */
106struct tcpcb;
107struct inpcb;
108struct sockopt;
109struct socket;
110
111/*
112 * If defining the optional tcp_timers, in the
113 * tfb_tcp_timer_stop call you must use the
114 * callout_async_drain() function with the
115 * tcp_timer_discard callback. You should check
116 * the return of callout_async_drain() and if 0
117 * increment tt_draincnt. Since the timer sub-system
118 * does not know your callbacks you must provide a
119 * stop_all function that loops through and calls
120 * tcp_timer_stop() with each of your defined timers.
121 */
122struct tcp_function_block {
123	char tfb_tcp_block_name[TCP_FUNCTION_NAME_LEN_MAX];
124	int	(*tfb_tcp_output)(struct tcpcb *);
125	void	(*tfb_tcp_do_segment)(struct mbuf *, struct tcphdr *,
126			    struct socket *, struct tcpcb *,
127			    int, int, uint8_t,
128			    int);
129	int     (*tfb_tcp_ctloutput)(struct socket *so, struct sockopt *sopt,
130			    struct inpcb *inp, struct tcpcb *tp);
131	/* Optional memory allocation/free routine */
132	void	(*tfb_tcp_fb_init)(struct tcpcb *);
133	void	(*tfb_tcp_fb_fini)(struct tcpcb *);
134	/* Optional timers, must define all if you define one */
135	int	(*tfb_tcp_timer_stop_all)(struct tcpcb *);
136	void	(*tfb_tcp_timer_activate)(struct tcpcb *,
137			    uint32_t, u_int);
138	int	(*tfb_tcp_timer_active)(struct tcpcb *, uint32_t);
139	void	(*tfb_tcp_timer_stop)(struct tcpcb *, uint32_t);
140	void	(*tfb_tcp_rexmit_tmr)(struct tcpcb *);
141	volatile uint32_t tfb_refcnt;
142	uint32_t  tfb_flags;
143};
144
145struct tcp_function {
146	TAILQ_ENTRY(tcp_function) tf_next;
147	struct tcp_function_block *tf_fb;
148};
149
150TAILQ_HEAD(tcp_funchead, tcp_function);
151
152/*
153 * Tcp control block, one per tcp; fields:
154 * Organized for 16 byte cacheline efficiency.
155 */
156struct tcpcb {
157	struct	tsegqe_head t_segq;	/* segment reassembly queue */
158	void	*t_pspare[2];		/* new reassembly queue */
159	int	t_segqlen;		/* segment reassembly queue length */
160	int	t_dupacks;		/* consecutive dup acks recd */
161
162	struct tcp_timer *t_timers;	/* All the TCP timers in one struct */
163
164	struct	inpcb *t_inpcb;		/* back pointer to internet pcb */
165	int	t_state;		/* state of this connection */
166	u_int	t_flags;
167
168	struct	vnet *t_vnet;		/* back pointer to parent vnet */
169
170	tcp_seq	snd_una;		/* sent but unacknowledged */
171	tcp_seq	snd_max;		/* highest sequence number sent;
172					 * used to recognize retransmits
173					 */
174	tcp_seq	snd_nxt;		/* send next */
175	tcp_seq	snd_up;			/* send urgent pointer */
176
177	tcp_seq	snd_wl1;		/* window update seg seq number */
178	tcp_seq	snd_wl2;		/* window update seg ack number */
179	tcp_seq	iss;			/* initial send sequence number */
180	tcp_seq	irs;			/* initial receive sequence number */
181
182	tcp_seq	rcv_nxt;		/* receive next */
183	tcp_seq	rcv_adv;		/* advertised window */
184	u_long	rcv_wnd;		/* receive window */
185	tcp_seq	rcv_up;			/* receive urgent pointer */
186
187	u_long	snd_wnd;		/* send window */
188	u_long	snd_cwnd;		/* congestion-controlled window */
189	u_long	snd_spare1;		/* unused */
190	u_long	snd_ssthresh;		/* snd_cwnd size threshold for
191					 * for slow start exponential to
192					 * linear switch
193					 */
194	u_long	snd_spare2;		/* unused */
195	tcp_seq	snd_recover;		/* for use in NewReno Fast Recovery */
196
197	u_int	t_rcvtime;		/* inactivity time */
198	u_int	t_starttime;		/* time connection was established */
199	u_int	t_rtttime;		/* RTT measurement start time */
200	tcp_seq	t_rtseq;		/* sequence number being timed */
201
202	u_int	t_bw_spare1;		/* unused */
203	tcp_seq	t_bw_spare2;		/* unused */
204
205	int	t_rxtcur;		/* current retransmit value (ticks) */
206	u_int	t_maxseg;		/* maximum segment size */
207	u_int	t_pmtud_saved_maxseg;	/* pre-blackhole MSS */
208	int	t_srtt;			/* smoothed round-trip time */
209	int	t_rttvar;		/* variance in round-trip time */
210
211	int	t_rxtshift;		/* log(2) of rexmt exp. backoff */
212	u_int	t_rttmin;		/* minimum rtt allowed */
213	u_int	t_rttbest;		/* best rtt we've seen */
214	u_long	t_rttupdated;		/* number of times rtt sampled */
215	u_long	max_sndwnd;		/* largest window peer has offered */
216
217	int	t_softerror;		/* possible error not yet reported */
218/* out-of-band data */
219	char	t_oobflags;		/* have some */
220	char	t_iobc;			/* input character */
221/* RFC 1323 variables */
222	u_char	snd_scale;		/* window scaling for send window */
223	u_char	rcv_scale;		/* window scaling for recv window */
224	u_char	request_r_scale;	/* pending window scaling */
225	u_int32_t  ts_recent;		/* timestamp echo data */
226	u_int	ts_recent_age;		/* when last updated */
227	u_int32_t  ts_offset;		/* our timestamp offset */
228
229	tcp_seq	last_ack_sent;
230/* experimental */
231	u_long	snd_cwnd_prev;		/* cwnd prior to retransmit */
232	u_long	snd_ssthresh_prev;	/* ssthresh prior to retransmit */
233	tcp_seq	snd_recover_prev;	/* snd_recover prior to retransmit */
234	int	t_sndzerowin;		/* zero-window updates sent */
235	u_int	t_badrxtwin;		/* window for retransmit recovery */
236	u_char	snd_limited;		/* segments limited transmitted */
237/* SACK related state */
238	int	snd_numholes;		/* number of holes seen by sender */
239	TAILQ_HEAD(sackhole_head, sackhole) snd_holes;
240					/* SACK scoreboard (sorted) */
241	tcp_seq	snd_fack;		/* last seq number(+1) sack'd by rcv'r*/
242	int	rcv_numsacks;		/* # distinct sack blks present */
243	struct sackblk sackblks[MAX_SACK_BLKS]; /* seq nos. of sack blocks */
244	tcp_seq sack_newdata;		/* New data xmitted in this recovery
245					   episode starts at this seq number */
246	struct sackhint	sackhint;	/* SACK scoreboard hint */
247	int	t_rttlow;		/* smallest observerved RTT */
248	u_int32_t	rfbuf_ts;	/* recv buffer autoscaling timestamp */
249	int	rfbuf_cnt;		/* recv buffer autoscaling byte count */
250	struct toedev	*tod;		/* toedev handling this connection */
251	int	t_sndrexmitpack;	/* retransmit packets sent */
252	int	t_rcvoopack;		/* out-of-order packets received */
253	void	*t_toe;			/* TOE pcb pointer */
254	int	t_bytes_acked;		/* # bytes acked during current RTT */
255	struct cc_algo	*cc_algo;	/* congestion control algorithm */
256	struct cc_var	*ccv;		/* congestion control specific vars */
257	struct osd	*osd;		/* storage for Khelp module data */
258
259	u_int	t_keepinit;		/* time to establish connection */
260	u_int	t_keepidle;		/* time before keepalive probes begin */
261	u_int	t_keepintvl;		/* interval between keepalives */
262	u_int	t_keepcnt;		/* number of keepalives before close */
263
264	u_int	t_tsomax;		/* TSO total burst length limit in bytes */
265	u_int	t_tsomaxsegcount;	/* TSO maximum segment count */
266	u_int	t_tsomaxsegsize;	/* TSO maximum segment size in bytes */
267	u_int	t_flags2;		/* More tcpcb flags storage */
268#if defined(_KERNEL) && defined(TCP_RFC7413)
269	uint32_t t_ispare[6];		/* 5 UTO, 1 TBD */
270	uint64_t t_tfo_cookie;		/* TCP Fast Open cookie */
271#else
272	uint32_t t_ispare[8];		/* 5 UTO, 3 TBD */
273#endif
274	struct tcp_function_block *t_fb;/* TCP function call block */
275	void	*t_fb_ptr;		/* Pointer to t_fb specific data */
276#if defined(_KERNEL) && defined(TCP_RFC7413)
277	unsigned int *t_tfo_pending;	/* TCP Fast Open pending counter */
278	void	*t_pspare2[1];		/* 1 TCP_SIGNATURE */
279#else
280	void	*t_pspare2[2];		/* 1 TCP_SIGNATURE, 1 TBD */
281#endif
282#if defined(_KERNEL) && defined(TCPPCAP)
283	struct mbufq t_inpkts;		/* List of saved input packets. */
284	struct mbufq t_outpkts;		/* List of saved output packets. */
285#ifdef _LP64
286	uint64_t _pad[0];		/* all used! */
287#else
288	uint64_t _pad[2];		/* 2 are available */
289#endif /* _LP64 */
290#else
291	uint64_t _pad[6];
292#endif /* defined(_KERNEL) && defined(TCPPCAP) */
293};
294
295/*
296 * Flags and utility macros for the t_flags field.
297 */
298#define	TF_ACKNOW	0x000001	/* ack peer immediately */
299#define	TF_DELACK	0x000002	/* ack, but try to delay it */
300#define	TF_NODELAY	0x000004	/* don't delay packets to coalesce */
301#define	TF_NOOPT	0x000008	/* don't use tcp options */
302#define	TF_SENTFIN	0x000010	/* have sent FIN */
303#define	TF_REQ_SCALE	0x000020	/* have/will request window scaling */
304#define	TF_RCVD_SCALE	0x000040	/* other side has requested scaling */
305#define	TF_REQ_TSTMP	0x000080	/* have/will request timestamps */
306#define	TF_RCVD_TSTMP	0x000100	/* a timestamp was received in SYN */
307#define	TF_SACK_PERMIT	0x000200	/* other side said I could SACK */
308#define	TF_NEEDSYN	0x000400	/* send SYN (implicit state) */
309#define	TF_NEEDFIN	0x000800	/* send FIN (implicit state) */
310#define	TF_NOPUSH	0x001000	/* don't push */
311#define	TF_PREVVALID	0x002000	/* saved values for bad rxmit valid */
312#define	TF_MORETOCOME	0x010000	/* More data to be appended to sock */
313#define	TF_LQ_OVERFLOW	0x020000	/* listen queue overflow */
314#define	TF_LASTIDLE	0x040000	/* connection was previously idle */
315#define	TF_RXWIN0SENT	0x080000	/* sent a receiver win 0 in response */
316#define	TF_FASTRECOVERY	0x100000	/* in NewReno Fast Recovery */
317#define	TF_WASFRECOVERY	0x200000	/* was in NewReno Fast Recovery */
318#define	TF_SIGNATURE	0x400000	/* require MD5 digests (RFC2385) */
319#define	TF_FORCEDATA	0x800000	/* force out a byte */
320#define	TF_TSO		0x1000000	/* TSO enabled on this connection */
321#define	TF_TOE		0x2000000	/* this connection is offloaded */
322#define	TF_ECN_PERMIT	0x4000000	/* connection ECN-ready */
323#define	TF_ECN_SND_CWR	0x8000000	/* ECN CWR in queue */
324#define	TF_ECN_SND_ECE	0x10000000	/* ECN ECE in queue */
325#define	TF_CONGRECOVERY	0x20000000	/* congestion recovery mode */
326#define	TF_WASCRECOVERY	0x40000000	/* was in congestion recovery */
327#define	TF_FASTOPEN	0x80000000	/* TCP Fast Open indication */
328
329#define	IN_FASTRECOVERY(t_flags)	(t_flags & TF_FASTRECOVERY)
330#define	ENTER_FASTRECOVERY(t_flags)	t_flags |= TF_FASTRECOVERY
331#define	EXIT_FASTRECOVERY(t_flags)	t_flags &= ~TF_FASTRECOVERY
332
333#define	IN_CONGRECOVERY(t_flags)	(t_flags & TF_CONGRECOVERY)
334#define	ENTER_CONGRECOVERY(t_flags)	t_flags |= TF_CONGRECOVERY
335#define	EXIT_CONGRECOVERY(t_flags)	t_flags &= ~TF_CONGRECOVERY
336
337#define	IN_RECOVERY(t_flags) (t_flags & (TF_CONGRECOVERY | TF_FASTRECOVERY))
338#define	ENTER_RECOVERY(t_flags) t_flags |= (TF_CONGRECOVERY | TF_FASTRECOVERY)
339#define	EXIT_RECOVERY(t_flags) t_flags &= ~(TF_CONGRECOVERY | TF_FASTRECOVERY)
340
341#define	BYTES_THIS_ACK(tp, th)	(th->th_ack - tp->snd_una)
342
343/*
344 * Flags for the t_oobflags field.
345 */
346#define	TCPOOB_HAVEDATA	0x01
347#define	TCPOOB_HADDATA	0x02
348
349/*
350 * Flags for PLPMTU handling, t_flags2
351 */
352#define	TF2_PLPMTU_BLACKHOLE	0x00000001 /* Possible PLPMTUD Black Hole. */
353#define	TF2_PLPMTU_PMTUD	0x00000002 /* Allowed to attempt PLPMTUD. */
354#define	TF2_PLPMTU_MAXSEGSNT	0x00000004 /* Last seg sent was full seg. */
355
356/*
357 * Structure to hold TCP options that are only used during segment
358 * processing (in tcp_input), but not held in the tcpcb.
359 * It's basically used to reduce the number of parameters
360 * to tcp_dooptions and tcp_addoptions.
361 * The binary order of the to_flags is relevant for packing of the
362 * options in tcp_addoptions.
363 */
364struct tcpopt {
365	u_int32_t	to_flags;	/* which options are present */
366#define	TOF_MSS		0x0001		/* maximum segment size */
367#define	TOF_SCALE	0x0002		/* window scaling */
368#define	TOF_SACKPERM	0x0004		/* SACK permitted */
369#define	TOF_TS		0x0010		/* timestamp */
370#define	TOF_SIGNATURE	0x0040		/* TCP-MD5 signature option (RFC2385) */
371#define	TOF_SACK	0x0080		/* Peer sent SACK option */
372#define	TOF_FASTOPEN	0x0100		/* TCP Fast Open (TFO) cookie */
373#define	TOF_MAXOPT	0x0200
374	u_int32_t	to_tsval;	/* new timestamp */
375	u_int32_t	to_tsecr;	/* reflected timestamp */
376	u_char		*to_sacks;	/* pointer to the first SACK blocks */
377	u_char		*to_signature;	/* pointer to the TCP-MD5 signature */
378	u_char		*to_tfo_cookie; /* pointer to the TFO cookie */
379	u_int16_t	to_mss;		/* maximum segment size */
380	u_int8_t	to_wscale;	/* window scaling */
381	u_int8_t	to_nsacks;	/* number of SACK blocks */
382	u_int8_t	to_tfo_len;	/* TFO cookie length */
383	u_int32_t	to_spare;	/* UTO */
384};
385
386/*
387 * Flags for tcp_dooptions.
388 */
389#define	TO_SYN		0x01		/* parse SYN-only options */
390
391struct hc_metrics_lite {	/* must stay in sync with hc_metrics */
392	u_long	rmx_mtu;	/* MTU for this path */
393	u_long	rmx_ssthresh;	/* outbound gateway buffer limit */
394	u_long	rmx_rtt;	/* estimated round trip time */
395	u_long	rmx_rttvar;	/* estimated rtt variance */
396	u_long	rmx_cwnd;	/* congestion window */
397	u_long	rmx_sendpipe;   /* outbound delay-bandwidth product */
398	u_long	rmx_recvpipe;   /* inbound delay-bandwidth product */
399};
400
401/*
402 * Used by tcp_maxmtu() to communicate interface specific features
403 * and limits at the time of connection setup.
404 */
405struct tcp_ifcap {
406	int	ifcap;
407	u_int	tsomax;
408	u_int	tsomaxsegcount;
409	u_int	tsomaxsegsize;
410};
411
412#ifndef _NETINET_IN_PCB_H_
413struct in_conninfo;
414#endif /* _NETINET_IN_PCB_H_ */
415
416struct tcptw {
417	struct inpcb	*tw_inpcb;	/* XXX back pointer to internet pcb */
418	tcp_seq		snd_nxt;
419	tcp_seq		rcv_nxt;
420	tcp_seq		iss;
421	tcp_seq		irs;
422	u_short		last_win;	/* cached window value */
423	u_short		tw_so_options;	/* copy of so_options */
424	struct ucred	*tw_cred;	/* user credentials */
425	u_int32_t	t_recent;
426	u_int32_t	ts_offset;	/* our timestamp offset */
427	u_int		t_starttime;
428	int		tw_time;
429	TAILQ_ENTRY(tcptw) tw_2msl;
430	void		*tw_pspare;	/* TCP_SIGNATURE */
431	u_int		*tw_spare;	/* TCP_SIGNATURE */
432};
433
434#define	intotcpcb(ip)	((struct tcpcb *)(ip)->inp_ppcb)
435#define	intotw(ip)	((struct tcptw *)(ip)->inp_ppcb)
436#define	sototcpcb(so)	(intotcpcb(sotoinpcb(so)))
437
438/*
439 * The smoothed round-trip time and estimated variance
440 * are stored as fixed point numbers scaled by the values below.
441 * For convenience, these scales are also used in smoothing the average
442 * (smoothed = (1/scale)sample + ((scale-1)/scale)smoothed).
443 * With these scales, srtt has 3 bits to the right of the binary point,
444 * and thus an "ALPHA" of 0.875.  rttvar has 2 bits to the right of the
445 * binary point, and is smoothed with an ALPHA of 0.75.
446 */
447#define	TCP_RTT_SCALE		32	/* multiplier for srtt; 3 bits frac. */
448#define	TCP_RTT_SHIFT		5	/* shift for srtt; 3 bits frac. */
449#define	TCP_RTTVAR_SCALE	16	/* multiplier for rttvar; 2 bits */
450#define	TCP_RTTVAR_SHIFT	4	/* shift for rttvar; 2 bits */
451#define	TCP_DELTA_SHIFT		2	/* see tcp_input.c */
452
453/*
454 * The initial retransmission should happen at rtt + 4 * rttvar.
455 * Because of the way we do the smoothing, srtt and rttvar
456 * will each average +1/2 tick of bias.  When we compute
457 * the retransmit timer, we want 1/2 tick of rounding and
458 * 1 extra tick because of +-1/2 tick uncertainty in the
459 * firing of the timer.  The bias will give us exactly the
460 * 1.5 tick we need.  But, because the bias is
461 * statistical, we have to test that we don't drop below
462 * the minimum feasible timer (which is 2 ticks).
463 * This version of the macro adapted from a paper by Lawrence
464 * Brakmo and Larry Peterson which outlines a problem caused
465 * by insufficient precision in the original implementation,
466 * which results in inappropriately large RTO values for very
467 * fast networks.
468 */
469#define	TCP_REXMTVAL(tp) \
470	max((tp)->t_rttmin, (((tp)->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT))  \
471	  + (tp)->t_rttvar) >> TCP_DELTA_SHIFT)
472
473/*
474 * TCP statistics.
475 * Many of these should be kept per connection,
476 * but that's inconvenient at the moment.
477 */
478struct	tcpstat {
479	uint64_t tcps_connattempt;	/* connections initiated */
480	uint64_t tcps_accepts;		/* connections accepted */
481	uint64_t tcps_connects;		/* connections established */
482	uint64_t tcps_drops;		/* connections dropped */
483	uint64_t tcps_conndrops;	/* embryonic connections dropped */
484	uint64_t tcps_minmssdrops;	/* average minmss too low drops */
485	uint64_t tcps_closed;		/* conn. closed (includes drops) */
486	uint64_t tcps_segstimed;	/* segs where we tried to get rtt */
487	uint64_t tcps_rttupdated;	/* times we succeeded */
488	uint64_t tcps_delack;		/* delayed acks sent */
489	uint64_t tcps_timeoutdrop;	/* conn. dropped in rxmt timeout */
490	uint64_t tcps_rexmttimeo;	/* retransmit timeouts */
491	uint64_t tcps_persisttimeo;	/* persist timeouts */
492	uint64_t tcps_keeptimeo;	/* keepalive timeouts */
493	uint64_t tcps_keepprobe;	/* keepalive probes sent */
494	uint64_t tcps_keepdrops;	/* connections dropped in keepalive */
495
496	uint64_t tcps_sndtotal;		/* total packets sent */
497	uint64_t tcps_sndpack;		/* data packets sent */
498	uint64_t tcps_sndbyte;		/* data bytes sent */
499	uint64_t tcps_sndrexmitpack;	/* data packets retransmitted */
500	uint64_t tcps_sndrexmitbyte;	/* data bytes retransmitted */
501	uint64_t tcps_sndrexmitbad;	/* unnecessary packet retransmissions */
502	uint64_t tcps_sndacks;		/* ack-only packets sent */
503	uint64_t tcps_sndprobe;		/* window probes sent */
504	uint64_t tcps_sndurg;		/* packets sent with URG only */
505	uint64_t tcps_sndwinup;		/* window update-only packets sent */
506	uint64_t tcps_sndctrl;		/* control (SYN|FIN|RST) packets sent */
507
508	uint64_t tcps_rcvtotal;		/* total packets received */
509	uint64_t tcps_rcvpack;		/* packets received in sequence */
510	uint64_t tcps_rcvbyte;		/* bytes received in sequence */
511	uint64_t tcps_rcvbadsum;	/* packets received with ccksum errs */
512	uint64_t tcps_rcvbadoff;	/* packets received with bad offset */
513	uint64_t tcps_rcvreassfull;	/* packets dropped for no reass space */
514	uint64_t tcps_rcvshort;		/* packets received too short */
515	uint64_t tcps_rcvduppack;	/* duplicate-only packets received */
516	uint64_t tcps_rcvdupbyte;	/* duplicate-only bytes received */
517	uint64_t tcps_rcvpartduppack;	/* packets with some duplicate data */
518	uint64_t tcps_rcvpartdupbyte;	/* dup. bytes in part-dup. packets */
519	uint64_t tcps_rcvoopack;	/* out-of-order packets received */
520	uint64_t tcps_rcvoobyte;	/* out-of-order bytes received */
521	uint64_t tcps_rcvpackafterwin;	/* packets with data after window */
522	uint64_t tcps_rcvbyteafterwin;	/* bytes rcvd after window */
523	uint64_t tcps_rcvafterclose;	/* packets rcvd after "close" */
524	uint64_t tcps_rcvwinprobe;	/* rcvd window probe packets */
525	uint64_t tcps_rcvdupack;	/* rcvd duplicate acks */
526	uint64_t tcps_rcvacktoomuch;	/* rcvd acks for unsent data */
527	uint64_t tcps_rcvackpack;	/* rcvd ack packets */
528	uint64_t tcps_rcvackbyte;	/* bytes acked by rcvd acks */
529	uint64_t tcps_rcvwinupd;	/* rcvd window update packets */
530	uint64_t tcps_pawsdrop;		/* segments dropped due to PAWS */
531	uint64_t tcps_predack;		/* times hdr predict ok for acks */
532	uint64_t tcps_preddat;		/* times hdr predict ok for data pkts */
533	uint64_t tcps_pcbcachemiss;
534	uint64_t tcps_cachedrtt;	/* times cached RTT in route updated */
535	uint64_t tcps_cachedrttvar;	/* times cached rttvar updated */
536	uint64_t tcps_cachedssthresh;	/* times cached ssthresh updated */
537	uint64_t tcps_usedrtt;		/* times RTT initialized from route */
538	uint64_t tcps_usedrttvar;	/* times RTTVAR initialized from rt */
539	uint64_t tcps_usedssthresh;	/* times ssthresh initialized from rt*/
540	uint64_t tcps_persistdrop;	/* timeout in persist state */
541	uint64_t tcps_badsyn;		/* bogus SYN, e.g. premature ACK */
542	uint64_t tcps_mturesent;	/* resends due to MTU discovery */
543	uint64_t tcps_listendrop;	/* listen queue overflows */
544	uint64_t tcps_badrst;		/* ignored RSTs in the window */
545
546	uint64_t tcps_sc_added;		/* entry added to syncache */
547	uint64_t tcps_sc_retransmitted;	/* syncache entry was retransmitted */
548	uint64_t tcps_sc_dupsyn;	/* duplicate SYN packet */
549	uint64_t tcps_sc_dropped;	/* could not reply to packet */
550	uint64_t tcps_sc_completed;	/* successful extraction of entry */
551	uint64_t tcps_sc_bucketoverflow;/* syncache per-bucket limit hit */
552	uint64_t tcps_sc_cacheoverflow;	/* syncache cache limit hit */
553	uint64_t tcps_sc_reset;		/* RST removed entry from syncache */
554	uint64_t tcps_sc_stale;		/* timed out or listen socket gone */
555	uint64_t tcps_sc_aborted;	/* syncache entry aborted */
556	uint64_t tcps_sc_badack;	/* removed due to bad ACK */
557	uint64_t tcps_sc_unreach;	/* ICMP unreachable received */
558	uint64_t tcps_sc_zonefail;	/* zalloc() failed */
559	uint64_t tcps_sc_sendcookie;	/* SYN cookie sent */
560	uint64_t tcps_sc_recvcookie;	/* SYN cookie received */
561
562	uint64_t tcps_hc_added;		/* entry added to hostcache */
563	uint64_t tcps_hc_bucketoverflow;/* hostcache per bucket limit hit */
564
565	uint64_t tcps_finwait2_drops;    /* Drop FIN_WAIT_2 connection after time limit */
566
567	/* SACK related stats */
568	uint64_t tcps_sack_recovery_episode; /* SACK recovery episodes */
569	uint64_t tcps_sack_rexmits;	    /* SACK rexmit segments   */
570	uint64_t tcps_sack_rexmit_bytes;    /* SACK rexmit bytes      */
571	uint64_t tcps_sack_rcv_blocks;	    /* SACK blocks (options) received */
572	uint64_t tcps_sack_send_blocks;	    /* SACK blocks (options) sent     */
573	uint64_t tcps_sack_sboverflow;	    /* times scoreboard overflowed */
574
575	/* ECN related stats */
576	uint64_t tcps_ecn_ce;		/* ECN Congestion Experienced */
577	uint64_t tcps_ecn_ect0;		/* ECN Capable Transport */
578	uint64_t tcps_ecn_ect1;		/* ECN Capable Transport */
579	uint64_t tcps_ecn_shs;		/* ECN successful handshakes */
580	uint64_t tcps_ecn_rcwnd;	/* # times ECN reduced the cwnd */
581
582	/* TCP_SIGNATURE related stats */
583	uint64_t tcps_sig_rcvgoodsig;	/* Total matching signature received */
584	uint64_t tcps_sig_rcvbadsig;	/* Total bad signature received */
585	uint64_t tcps_sig_err_buildsig;	/* Failed to make signature */
586	uint64_t tcps_sig_err_sigopt;	/* No signature expected by socket */
587	uint64_t tcps_sig_err_nosigopt;	/* No signature provided by segment */
588
589	uint64_t _pad[12];		/* 6 UTO, 6 TBD */
590};
591
592#define	tcps_rcvmemdrop	tcps_rcvreassfull	/* compat */
593
594#ifdef _KERNEL
595#define	TI_UNLOCKED	1
596#define	TI_RLOCKED	2
597#include <sys/counter.h>
598
599VNET_PCPUSTAT_DECLARE(struct tcpstat, tcpstat);	/* tcp statistics */
600/*
601 * In-kernel consumers can use these accessor macros directly to update
602 * stats.
603 */
604#define	TCPSTAT_ADD(name, val)	\
605    VNET_PCPUSTAT_ADD(struct tcpstat, tcpstat, name, (val))
606#define	TCPSTAT_INC(name)	TCPSTAT_ADD(name, 1)
607
608/*
609 * Kernel module consumers must use this accessor macro.
610 */
611void	kmod_tcpstat_inc(int statnum);
612#define	KMOD_TCPSTAT_INC(name)						\
613    kmod_tcpstat_inc(offsetof(struct tcpstat, name) / sizeof(uint64_t))
614
615/*
616 * Running TCP connection count by state.
617 */
618VNET_DECLARE(counter_u64_t, tcps_states[TCP_NSTATES]);
619#define	V_tcps_states	VNET(tcps_states)
620#define	TCPSTATES_INC(state)	counter_u64_add(V_tcps_states[state], 1)
621#define	TCPSTATES_DEC(state)	counter_u64_add(V_tcps_states[state], -1)
622
623/*
624 * TCP specific helper hook point identifiers.
625 */
626#define	HHOOK_TCP_EST_IN		0
627#define	HHOOK_TCP_EST_OUT		1
628#define	HHOOK_TCP_LAST			HHOOK_TCP_EST_OUT
629
630struct tcp_hhook_data {
631	struct tcpcb	*tp;
632	struct tcphdr	*th;
633	struct tcpopt	*to;
634	long		len;
635	int		tso;
636	tcp_seq		curack;
637};
638#endif
639
640/*
641 * TCB structure exported to user-land via sysctl(3).
642 * Evil hack: declare only if in_pcb.h and sys/socketvar.h have been
643 * included.  Not all of our clients do.
644 */
645#if defined(_NETINET_IN_PCB_H_) && defined(_SYS_SOCKETVAR_H_)
646struct xtcp_timer {
647	int tt_rexmt;	/* retransmit timer */
648	int tt_persist;	/* retransmit persistence */
649	int tt_keep;	/* keepalive */
650	int tt_2msl;	/* 2*msl TIME_WAIT timer */
651	int tt_delack;	/* delayed ACK timer */
652	int t_rcvtime;	/* Time since last packet received */
653};
654struct	xtcpcb {
655	size_t	xt_len;
656	struct	inpcb	xt_inp;
657	struct	tcpcb	xt_tp;
658	struct	xsocket	xt_socket;
659	struct	xtcp_timer xt_timer;
660	u_quad_t	xt_alignment_hack;
661};
662#endif
663
664/*
665 * Identifiers for TCP sysctl nodes
666 */
667#define	TCPCTL_DO_RFC1323	1	/* use RFC-1323 extensions */
668#define	TCPCTL_MSSDFLT		3	/* MSS default */
669#define TCPCTL_STATS		4	/* statistics */
670#define	TCPCTL_RTTDFLT		5	/* default RTT estimate */
671#define	TCPCTL_KEEPIDLE		6	/* keepalive idle timer */
672#define	TCPCTL_KEEPINTVL	7	/* interval to send keepalives */
673#define	TCPCTL_SENDSPACE	8	/* send buffer space */
674#define	TCPCTL_RECVSPACE	9	/* receive buffer space */
675#define	TCPCTL_KEEPINIT		10	/* timeout for establishing syn */
676#define	TCPCTL_PCBLIST		11	/* list of all outstanding PCBs */
677#define	TCPCTL_DELACKTIME	12	/* time before sending delayed ACK */
678#define	TCPCTL_V6MSSDFLT	13	/* MSS default for IPv6 */
679#define	TCPCTL_SACK		14	/* Selective Acknowledgement,rfc 2018 */
680#define	TCPCTL_DROP		15	/* drop tcp connection */
681#define	TCPCTL_STATES		16	/* connection counts by TCP state */
682
683#ifdef _KERNEL
684#ifdef SYSCTL_DECL
685SYSCTL_DECL(_net_inet_tcp);
686SYSCTL_DECL(_net_inet_tcp_sack);
687MALLOC_DECLARE(M_TCPLOG);
688#endif
689
690VNET_DECLARE(struct inpcbhead, tcb);		/* queue of active tcpcb's */
691VNET_DECLARE(struct inpcbinfo, tcbinfo);
692extern	int tcp_log_in_vain;
693VNET_DECLARE(int, tcp_mssdflt);	/* XXX */
694VNET_DECLARE(int, tcp_minmss);
695VNET_DECLARE(int, tcp_delack_enabled);
696VNET_DECLARE(int, tcp_do_rfc3390);
697VNET_DECLARE(int, tcp_initcwnd_segments);
698VNET_DECLARE(int, tcp_sendspace);
699VNET_DECLARE(int, tcp_recvspace);
700VNET_DECLARE(int, path_mtu_discovery);
701VNET_DECLARE(int, tcp_do_rfc3465);
702VNET_DECLARE(int, tcp_abc_l_var);
703#define	V_tcb			VNET(tcb)
704#define	V_tcbinfo		VNET(tcbinfo)
705#define	V_tcp_mssdflt		VNET(tcp_mssdflt)
706#define	V_tcp_minmss		VNET(tcp_minmss)
707#define	V_tcp_delack_enabled	VNET(tcp_delack_enabled)
708#define	V_tcp_do_rfc3390	VNET(tcp_do_rfc3390)
709#define	V_tcp_initcwnd_segments	VNET(tcp_initcwnd_segments)
710#define	V_tcp_sendspace		VNET(tcp_sendspace)
711#define	V_tcp_recvspace		VNET(tcp_recvspace)
712#define	V_path_mtu_discovery	VNET(path_mtu_discovery)
713#define	V_tcp_do_rfc3465	VNET(tcp_do_rfc3465)
714#define	V_tcp_abc_l_var		VNET(tcp_abc_l_var)
715
716VNET_DECLARE(int, tcp_do_sack);			/* SACK enabled/disabled */
717VNET_DECLARE(int, tcp_sc_rst_sock_fail);	/* RST on sock alloc failure */
718#define	V_tcp_do_sack		VNET(tcp_do_sack)
719#define	V_tcp_sc_rst_sock_fail	VNET(tcp_sc_rst_sock_fail)
720
721VNET_DECLARE(int, tcp_do_ecn);			/* TCP ECN enabled/disabled */
722VNET_DECLARE(int, tcp_ecn_maxretries);
723#define	V_tcp_do_ecn		VNET(tcp_do_ecn)
724#define	V_tcp_ecn_maxretries	VNET(tcp_ecn_maxretries)
725
726VNET_DECLARE(struct hhook_head *, tcp_hhh[HHOOK_TCP_LAST + 1]);
727#define	V_tcp_hhh		VNET(tcp_hhh)
728
729VNET_DECLARE(int, tcp_do_rfc6675_pipe);
730#define V_tcp_do_rfc6675_pipe	VNET(tcp_do_rfc6675_pipe)
731
732int	 tcp_addoptions(struct tcpopt *, u_char *);
733int	 tcp_ccalgounload(struct cc_algo *unload_algo);
734struct tcpcb *
735	 tcp_close(struct tcpcb *);
736void	 tcp_discardcb(struct tcpcb *);
737void	 tcp_twstart(struct tcpcb *);
738void	 tcp_twclose(struct tcptw *, int);
739void	 tcp_ctlinput(int, struct sockaddr *, void *);
740int	 tcp_ctloutput(struct socket *, struct sockopt *);
741struct tcpcb *
742	 tcp_drop(struct tcpcb *, int);
743void	 tcp_drain(void);
744void	 tcp_init(void);
745void	 tcp_fini(void *);
746char	*tcp_log_addrs(struct in_conninfo *, struct tcphdr *, void *,
747	    const void *);
748char	*tcp_log_vain(struct in_conninfo *, struct tcphdr *, void *,
749	    const void *);
750int	 tcp_reass(struct tcpcb *, struct tcphdr *, int *, struct mbuf *);
751void	 tcp_reass_global_init(void);
752void	 tcp_reass_flush(struct tcpcb *);
753void	 tcp_dooptions(struct tcpopt *, u_char *, int, int);
754void	tcp_dropwithreset(struct mbuf *, struct tcphdr *,
755		     struct tcpcb *, int, int);
756void	tcp_pulloutofband(struct socket *,
757		     struct tcphdr *, struct mbuf *, int);
758void	tcp_xmit_timer(struct tcpcb *, int);
759void	tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
760void	cc_ack_received(struct tcpcb *tp, struct tcphdr *th,
761			    uint16_t type);
762void 	cc_conn_init(struct tcpcb *tp);
763void 	cc_post_recovery(struct tcpcb *tp, struct tcphdr *th);
764void	cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type);
765void	hhook_run_tcp_est_in(struct tcpcb *tp,
766			    struct tcphdr *th, struct tcpopt *to);
767
768int	 tcp_input(struct mbuf **, int *, int);
769int	 tcp_autorcvbuf(struct mbuf *, struct tcphdr *, struct socket *,
770	    struct tcpcb *, int);
771void	 tcp_do_segment(struct mbuf *, struct tcphdr *,
772			struct socket *, struct tcpcb *, int, int, uint8_t,
773			int);
774
775int register_tcp_functions(struct tcp_function_block *blk, int wait);
776int deregister_tcp_functions(struct tcp_function_block *blk);
777struct tcp_function_block *find_and_ref_tcp_functions(struct tcp_function_set *fs);
778struct tcp_function_block *find_and_ref_tcp_fb(struct tcp_function_block *blk);
779int tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp);
780
781u_long	 tcp_maxmtu(struct in_conninfo *, struct tcp_ifcap *);
782u_long	 tcp_maxmtu6(struct in_conninfo *, struct tcp_ifcap *);
783u_int	 tcp_maxseg(const struct tcpcb *);
784void	 tcp_mss_update(struct tcpcb *, int, int, struct hc_metrics_lite *,
785	    struct tcp_ifcap *);
786void	 tcp_mss(struct tcpcb *, int);
787int	 tcp_mssopt(struct in_conninfo *);
788struct inpcb *
789	 tcp_drop_syn_sent(struct inpcb *, int);
790struct tcpcb *
791	 tcp_newtcpcb(struct inpcb *);
792int	 tcp_output(struct tcpcb *);
793void	 tcp_state_change(struct tcpcb *, int);
794void	 tcp_respond(struct tcpcb *, void *,
795	    struct tcphdr *, struct mbuf *, tcp_seq, tcp_seq, int);
796void	 tcp_tw_init(void);
797#ifdef VIMAGE
798void	 tcp_tw_destroy(void);
799#endif
800void	 tcp_tw_zone_change(void);
801int	 tcp_twcheck(struct inpcb *, struct tcpopt *, struct tcphdr *,
802	    struct mbuf *, int);
803void	 tcp_setpersist(struct tcpcb *);
804void	 tcp_slowtimo(void);
805struct tcptemp *
806	 tcpip_maketemplate(struct inpcb *);
807void	 tcpip_fillheaders(struct inpcb *, void *, void *);
808void	 tcp_timer_activate(struct tcpcb *, uint32_t, u_int);
809int	 tcp_timer_active(struct tcpcb *, uint32_t);
810void	 tcp_timer_stop(struct tcpcb *, uint32_t);
811void	 tcp_trace(short, short, struct tcpcb *, void *, struct tcphdr *, int);
812/*
813 * All tcp_hc_* functions are IPv4 and IPv6 (via in_conninfo)
814 */
815void	 tcp_hc_init(void);
816#ifdef VIMAGE
817void	 tcp_hc_destroy(void);
818#endif
819void	 tcp_hc_get(struct in_conninfo *, struct hc_metrics_lite *);
820u_long	 tcp_hc_getmtu(struct in_conninfo *);
821void	 tcp_hc_updatemtu(struct in_conninfo *, u_long);
822void	 tcp_hc_update(struct in_conninfo *, struct hc_metrics_lite *);
823
824extern	struct pr_usrreqs tcp_usrreqs;
825tcp_seq tcp_new_isn(struct tcpcb *);
826
827int	 tcp_sack_doack(struct tcpcb *, struct tcpopt *, tcp_seq);
828void	 tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_laststart, tcp_seq rcv_lastend);
829void	 tcp_clean_sackreport(struct tcpcb *tp);
830void	 tcp_sack_adjust(struct tcpcb *tp);
831struct sackhole *tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt);
832void	 tcp_sack_partialack(struct tcpcb *, struct tcphdr *);
833void	 tcp_free_sackholes(struct tcpcb *tp);
834int	 tcp_newreno(struct tcpcb *, struct tcphdr *);
835int	 tcp_compute_pipe(struct tcpcb *);
836
837static inline void
838tcp_fields_to_host(struct tcphdr *th)
839{
840
841	th->th_seq = ntohl(th->th_seq);
842	th->th_ack = ntohl(th->th_ack);
843	th->th_win = ntohs(th->th_win);
844	th->th_urp = ntohs(th->th_urp);
845}
846
847static inline void
848tcp_fields_to_net(struct tcphdr *th)
849{
850
851	th->th_seq = htonl(th->th_seq);
852	th->th_ack = htonl(th->th_ack);
853	th->th_win = htons(th->th_win);
854	th->th_urp = htons(th->th_urp);
855}
856#endif /* _KERNEL */
857
858#endif /* _NETINET_TCP_VAR_H_ */
859