Home | History | Annotate | Line # | Download | only in netinet
tcp_var.h revision 1.112
      1 /*	$NetBSD: tcp_var.h,v 1.112 2004/05/18 14:44:16 itojun 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 	u_short	t_peermss;		/* peer's maximum segment size */
    181 	u_short	t_ourmss;		/* our's maximum segment size */
    182 	u_short t_segsz;		/* current segment size in use */
    183 	char	t_force;		/* 1 if forcing out a byte */
    184 	u_int	t_flags;
    185 #define	TF_ACKNOW	0x0001		/* ack peer immediately */
    186 #define	TF_DELACK	0x0002		/* ack, but try to delay it */
    187 #define	TF_NODELAY	0x0004		/* don't delay packets to coalesce */
    188 #define	TF_NOOPT	0x0008		/* don't use tcp options */
    189 #define	TF_REQ_SCALE	0x0020		/* have/will request window scaling */
    190 #define	TF_RCVD_SCALE	0x0040		/* other side has requested scaling */
    191 #define	TF_REQ_TSTMP	0x0080		/* have/will request timestamps */
    192 #define	TF_RCVD_TSTMP	0x0100		/* a timestamp was received in SYN */
    193 #define	TF_SACK_PERMIT	0x0200		/* other side said I could SACK */
    194 #define	TF_SYN_REXMT	0x0400		/* rexmit timer fired on SYN */
    195 #define	TF_WILL_SACK	0x0800		/* try to use SACK */
    196 #define	TF_CANT_TXSACK	0x1000		/* other side said I could not SACK */
    197 #define	TF_IGNR_RXSACK	0x2000		/* ignore received SACK blocks */
    198 #define	TF_REASSEMBLING	0x4000		/* we're busy reassembling */
    199 #define	TF_DEAD		0x8000		/* dead and to-be-released */
    200 #define	TF_SIGNATURE	0x400000	/* require MD5 digests (RFC2385) */
    201 
    202 
    203 	struct	mbuf *t_template;	/* skeletal packet for transmit */
    204 	struct	inpcb *t_inpcb;		/* back pointer to internet pcb */
    205 	struct	in6pcb *t_in6pcb;	/* back pointer to internet pcb */
    206 	struct	callout t_delack_ch;	/* delayed ACK callout */
    207 /*
    208  * The following fields are used as in the protocol specification.
    209  * See RFC783, Dec. 1981, page 21.
    210  */
    211 /* send sequence variables */
    212 	tcp_seq	snd_una;		/* send unacknowledged */
    213 	tcp_seq	snd_nxt;		/* send next */
    214 	tcp_seq	snd_up;			/* send urgent pointer */
    215 	tcp_seq	snd_wl1;		/* window update seg seq number */
    216 	tcp_seq	snd_wl2;		/* window update seg ack number */
    217 	tcp_seq	iss;			/* initial send sequence number */
    218 	u_long	snd_wnd;		/* send window */
    219 	tcp_seq snd_recover;		/* for use in fast recovery */
    220 /* receive sequence variables */
    221 	u_long	rcv_wnd;		/* receive window */
    222 	tcp_seq	rcv_nxt;		/* receive next */
    223 	tcp_seq	rcv_up;			/* receive urgent pointer */
    224 	tcp_seq	irs;			/* initial receive sequence number */
    225 /*
    226  * Additional variables for this implementation.
    227  */
    228 /* receive variables */
    229 	tcp_seq	rcv_adv;		/* advertised window */
    230 /* retransmit variables */
    231 	tcp_seq	snd_max;		/* highest sequence number sent;
    232 					 * used to recognize retransmits
    233 					 */
    234 /* congestion control (for slow start, source quench, retransmit after loss) */
    235 	u_long	snd_cwnd;		/* congestion-controlled window */
    236 	u_long	snd_ssthresh;		/* snd_cwnd size threshhold for
    237 					 * for slow start exponential to
    238 					 * linear switch
    239 					 */
    240 /*
    241  * transmit timing stuff.  See below for scale of srtt and rttvar.
    242  * "Variance" is actually smoothed difference.
    243  */
    244 	uint32_t t_rcvtime;		/* time last segment received */
    245 	uint32_t t_rtttime;		/* time we started measuring rtt */
    246 	tcp_seq	t_rtseq;		/* sequence number being timed */
    247 	int32_t	t_srtt;			/* smoothed round-trip time */
    248 	int32_t	t_rttvar;		/* variance in round-trip time */
    249 	uint32_t t_rttmin;		/* minimum rtt allowed */
    250 	u_long	max_sndwnd;		/* largest window peer has offered */
    251 
    252 /* out-of-band data */
    253 	char	t_oobflags;		/* have some */
    254 	char	t_iobc;			/* input character */
    255 #define	TCPOOB_HAVEDATA	0x01
    256 #define	TCPOOB_HADDATA	0x02
    257 	short	t_softerror;		/* possible error not yet reported */
    258 
    259 /* RFC 1323 variables */
    260 	u_char	snd_scale;		/* window scaling for send window */
    261 	u_char	rcv_scale;		/* window scaling for recv window */
    262 	u_char	request_r_scale;	/* pending window scaling */
    263 	u_char	requested_s_scale;
    264 	u_int32_t ts_recent;		/* timestamp echo data */
    265 	u_int32_t ts_recent_age;	/* when last updated */
    266 	u_int32_t ts_timebase;		/* our timebase */
    267 	tcp_seq	last_ack_sent;
    268 
    269 /* SACK stuff */
    270 	struct ipqehead timeq;		/* time sequenced queue (for SACK) */
    271 
    272 /* path MTU discovery blackhole detection */
    273 	int t_mtudisc;			/* perform mtudisc for this tcb */
    274 
    275 /* pointer for syn cache entries*/
    276 	LIST_HEAD(, syn_cache) t_sc;	/* list of entries by this tcb */
    277 
    278 /* prediction of next mbuf when using large window sizes */
    279 	struct	mbuf *t_lastm;		/* last mbuf that data was sent from */
    280 	int	t_inoff;		/* data offset in previous mbuf */
    281 	int	t_lastoff;		/* last data address in mbuf chain */
    282 	int	t_lastlen;		/* last length read from mbuf chain */
    283 };
    284 
    285 #ifdef _KERNEL
    286 /*
    287  * TCP reassembly queue locks.
    288  */
    289 static __inline int tcp_reass_lock_try (struct tcpcb *)
    290 	__attribute__((__unused__));
    291 static __inline void tcp_reass_unlock (struct tcpcb *)
    292 	__attribute__((__unused__));
    293 
    294 static __inline int
    295 tcp_reass_lock_try(tp)
    296 	struct tcpcb *tp;
    297 {
    298 	int s;
    299 
    300 	/*
    301 	 * Use splvm() -- we're blocking things that would cause
    302 	 * mbuf allocation.
    303 	 */
    304 	s = splvm();
    305 	if (tp->t_flags & TF_REASSEMBLING) {
    306 		splx(s);
    307 		return (0);
    308 	}
    309 	tp->t_flags |= TF_REASSEMBLING;
    310 	splx(s);
    311 	return (1);
    312 }
    313 
    314 static __inline void
    315 tcp_reass_unlock(tp)
    316 	struct tcpcb *tp;
    317 {
    318 	int s;
    319 
    320 	s = splvm();
    321 	tp->t_flags &= ~TF_REASSEMBLING;
    322 	splx(s);
    323 }
    324 
    325 #ifdef DIAGNOSTIC
    326 #define	TCP_REASS_LOCK(tp)						\
    327 do {									\
    328 	if (tcp_reass_lock_try(tp) == 0) {				\
    329 		printf("%s:%d: tcpcb %p reass already locked\n",	\
    330 		    __FILE__, __LINE__, tp);				\
    331 		panic("tcp_reass_lock");				\
    332 	}								\
    333 } while (/*CONSTCOND*/ 0)
    334 #define	TCP_REASS_LOCK_CHECK(tp)					\
    335 do {									\
    336 	if (((tp)->t_flags & TF_REASSEMBLING) == 0) {			\
    337 		printf("%s:%d: tcpcb %p reass lock not held\n",		\
    338 		    __FILE__, __LINE__, tp);				\
    339 		panic("tcp reass lock check");				\
    340 	}								\
    341 } while (/*CONSTCOND*/ 0)
    342 #else
    343 #define	TCP_REASS_LOCK(tp)	(void) tcp_reass_lock_try((tp))
    344 #define	TCP_REASS_LOCK_CHECK(tp) /* nothing */
    345 #endif
    346 
    347 #define	TCP_REASS_UNLOCK(tp)	tcp_reass_unlock((tp))
    348 #endif /* _KERNEL */
    349 
    350 /*
    351  * Queue for delayed ACK processing.
    352  */
    353 #ifdef _KERNEL
    354 extern int tcp_delack_ticks;
    355 void	tcp_delack(void *);
    356 
    357 #define TCP_RESTART_DELACK(tp)						\
    358 	callout_reset(&(tp)->t_delack_ch, tcp_delack_ticks,		\
    359 	    tcp_delack, tp)
    360 
    361 #define	TCP_SET_DELACK(tp)						\
    362 do {									\
    363 	if (((tp)->t_flags & TF_DELACK) == 0) {				\
    364 		(tp)->t_flags |= TF_DELACK;				\
    365 		TCP_RESTART_DELACK(tp);					\
    366 	}								\
    367 } while (/*CONSTCOND*/0)
    368 
    369 #define	TCP_CLEAR_DELACK(tp)						\
    370 do {									\
    371 	if ((tp)->t_flags & TF_DELACK) {				\
    372 		(tp)->t_flags &= ~TF_DELACK;				\
    373 		callout_stop(&(tp)->t_delack_ch);			\
    374 	}								\
    375 } while (/*CONSTCOND*/0)
    376 #endif /* _KERNEL */
    377 
    378 /*
    379  * Compute the current timestamp for a connection.
    380  */
    381 #define	TCP_TIMESTAMP(tp)	(tcp_now - (tp)->ts_timebase)
    382 
    383 /*
    384  * Handy way of passing around TCP option info.
    385  */
    386 struct tcp_opt_info {
    387 	int		ts_present;
    388 	u_int32_t	ts_val;
    389 	u_int32_t	ts_ecr;
    390 	u_int16_t	maxseg;
    391 };
    392 
    393 #define	TOF_SIGNATURE	0x0040		/* signature option present */
    394 #define	TOF_SIGLEN	0x0080		/* sigature length valid (RFC2385) */
    395 
    396 /*
    397  * Data for the TCP compressed state engine.
    398  */
    399 union syn_cache_sa {
    400 	struct sockaddr sa;
    401 	struct sockaddr_in sin;
    402 #if 1 /*def INET6*/
    403 	struct sockaddr_in6 sin6;
    404 #endif
    405 };
    406 
    407 struct syn_cache {
    408 	TAILQ_ENTRY(syn_cache) sc_bucketq;	/* link on bucket list */
    409 	struct callout sc_timer;		/* rexmt timer */
    410 	union {					/* cached route */
    411 		struct route route4;
    412 #ifdef INET6
    413 		struct route_in6 route6;
    414 #endif
    415 	} sc_route_u;
    416 #define sc_route4	sc_route_u.route4
    417 #ifdef INET6
    418 #define sc_route6	sc_route_u.route6
    419 #endif
    420 	long sc_win;				/* advertised window */
    421 	int sc_bucketidx;			/* our bucket index */
    422 	u_int32_t sc_hash;
    423 	u_int32_t sc_timestamp;			/* timestamp from SYN */
    424 	u_int32_t sc_timebase;			/* our local timebase */
    425 	union syn_cache_sa sc_src;
    426 	union syn_cache_sa sc_dst;
    427 	tcp_seq sc_irs;
    428 	tcp_seq sc_iss;
    429 	u_int sc_rxtcur;			/* current rxt timeout */
    430 	u_int sc_rxttot;			/* total time spend on queues */
    431 	u_short sc_rxtshift;			/* for computing backoff */
    432 	u_short sc_flags;
    433 
    434 #define	SCF_UNREACH		0x0001		/* we've had an unreach error */
    435 #define	SCF_TIMESTAMP		0x0002		/* peer will do timestamps */
    436 #define	SCF_DEAD		0x0004		/* this entry to be released */
    437 #define SCF_SIGNATURE	0x40			/* send MD5 digests */
    438 
    439 	struct mbuf *sc_ipopts;			/* IP options */
    440 	u_int16_t sc_peermaxseg;
    441 	u_int16_t sc_ourmaxseg;
    442 	u_int8_t sc_request_r_scale	: 4,
    443 		 sc_requested_s_scale	: 4;
    444 
    445 	struct tcpcb *sc_tp;			/* tcb for listening socket */
    446 	LIST_ENTRY(syn_cache) sc_tpq;		/* list of entries by same tp */
    447 };
    448 
    449 struct syn_cache_head {
    450 	TAILQ_HEAD(, syn_cache) sch_bucket;	/* bucket entries */
    451 	u_short sch_length;			/* # entries in bucket */
    452 };
    453 
    454 #define	intotcpcb(ip)	((struct tcpcb *)(ip)->inp_ppcb)
    455 #ifdef INET6
    456 #define	in6totcpcb(ip)	((struct tcpcb *)(ip)->in6p_ppcb)
    457 #endif
    458 #ifndef INET6
    459 #define	sototcpcb(so)	(intotcpcb(sotoinpcb(so)))
    460 #else
    461 #define	sototcpcb(so)	(((so)->so_proto->pr_domain->dom_family == AF_INET) \
    462 				? intotcpcb(sotoinpcb(so)) \
    463 				: in6totcpcb(sotoin6pcb(so)))
    464 #endif
    465 
    466 /*
    467  * The smoothed round-trip time and estimated variance
    468  * are stored as fixed point numbers scaled by the values below.
    469  * For convenience, these scales are also used in smoothing the average
    470  * (smoothed = (1/scale)sample + ((scale-1)/scale)smoothed).
    471  * With these scales, srtt has 3 bits to the right of the binary point,
    472  * and thus an "ALPHA" of 0.875.  rttvar has 2 bits to the right of the
    473  * binary point, and is smoothed with an ALPHA of 0.75.
    474  */
    475 #define	TCP_RTT_SHIFT		3	/* shift for srtt; 3 bits frac. */
    476 #define	TCP_RTTVAR_SHIFT	2	/* multiplier for rttvar; 2 bits */
    477 
    478 /*
    479  * The initial retransmission should happen at rtt + 4 * rttvar.
    480  * Because of the way we do the smoothing, srtt and rttvar
    481  * will each average +1/2 tick of bias.  When we compute
    482  * the retransmit timer, we want 1/2 tick of rounding and
    483  * 1 extra tick because of +-1/2 tick uncertainty in the
    484  * firing of the timer.  The bias will give us exactly the
    485  * 1.5 tick we need.  But, because the bias is
    486  * statistical, we have to test that we don't drop below
    487  * the minimum feasible timer (which is 2 ticks).
    488  * This macro assumes that the value of 1<<TCP_RTTVAR_SHIFT
    489  * is the same as the multiplier for rttvar.
    490  */
    491 #define	TCP_REXMTVAL(tp) \
    492 	((((tp)->t_srtt >> TCP_RTT_SHIFT) + (tp)->t_rttvar) >> 2)
    493 
    494 /*
    495  * Compute the initial window for slow start.
    496  */
    497 #define	TCP_INITIAL_WINDOW(iw, segsz) \
    498 	(((iw) == 0) ? (min(4 * (segsz), max(2 * (segsz), 4380))) : \
    499 	 ((segsz) * (iw)))
    500 
    501 /*
    502  * TCP statistics.
    503  * Many of these should be kept per connection,
    504  * but that's inconvenient at the moment.
    505  */
    506 struct	tcpstat {
    507 	u_quad_t tcps_connattempt;	/* connections initiated */
    508 	u_quad_t tcps_accepts;		/* connections accepted */
    509 	u_quad_t tcps_connects;		/* connections established */
    510 	u_quad_t tcps_drops;		/* connections dropped */
    511 	u_quad_t tcps_conndrops;	/* embryonic connections dropped */
    512 	u_quad_t tcps_closed;		/* conn. closed (includes drops) */
    513 	u_quad_t tcps_segstimed;	/* segs where we tried to get rtt */
    514 	u_quad_t tcps_rttupdated;	/* times we succeeded */
    515 	u_quad_t tcps_delack;		/* delayed acks sent */
    516 	u_quad_t tcps_timeoutdrop;	/* conn. dropped in rxmt timeout */
    517 	u_quad_t tcps_rexmttimeo;	/* retransmit timeouts */
    518 	u_quad_t tcps_persisttimeo;	/* persist timeouts */
    519 	u_quad_t tcps_keeptimeo;	/* keepalive timeouts */
    520 	u_quad_t tcps_keepprobe;	/* keepalive probes sent */
    521 	u_quad_t tcps_keepdrops;	/* connections dropped in keepalive */
    522 	u_quad_t tcps_persistdrops;	/* connections dropped in persist */
    523 	u_quad_t tcps_connsdrained;	/* connections drained due to memory
    524 					   shortage */
    525 	u_quad_t tcps_pmtublackhole;	/* PMTUD blackhole detected */
    526 
    527 	u_quad_t tcps_sndtotal;		/* total packets sent */
    528 	u_quad_t tcps_sndpack;		/* data packets sent */
    529 	u_quad_t tcps_sndbyte;		/* data bytes sent */
    530 	u_quad_t tcps_sndrexmitpack;	/* data packets retransmitted */
    531 	u_quad_t tcps_sndrexmitbyte;	/* data bytes retransmitted */
    532 	u_quad_t tcps_sndacks;		/* ack-only packets sent */
    533 	u_quad_t tcps_sndprobe;		/* window probes sent */
    534 	u_quad_t tcps_sndurg;		/* packets sent with URG only */
    535 	u_quad_t tcps_sndwinup;		/* window update-only packets sent */
    536 	u_quad_t tcps_sndctrl;		/* control (SYN|FIN|RST) packets sent */
    537 
    538 	u_quad_t tcps_rcvtotal;		/* total packets received */
    539 	u_quad_t tcps_rcvpack;		/* packets received in sequence */
    540 	u_quad_t tcps_rcvbyte;		/* bytes received in sequence */
    541 	u_quad_t tcps_rcvbadsum;	/* packets received with ccksum errs */
    542 	u_quad_t tcps_rcvbadoff;	/* packets received with bad offset */
    543 	u_quad_t tcps_rcvmemdrop;	/* packets dropped for lack of memory */
    544 	u_quad_t tcps_rcvshort;		/* packets received too short */
    545 	u_quad_t tcps_rcvduppack;	/* duplicate-only packets received */
    546 	u_quad_t tcps_rcvdupbyte;	/* duplicate-only bytes received */
    547 	u_quad_t tcps_rcvpartduppack;	/* packets with some duplicate data */
    548 	u_quad_t tcps_rcvpartdupbyte;	/* dup. bytes in part-dup. packets */
    549 	u_quad_t tcps_rcvoopack;	/* out-of-order packets received */
    550 	u_quad_t tcps_rcvoobyte;	/* out-of-order bytes received */
    551 	u_quad_t tcps_rcvpackafterwin;	/* packets with data after window */
    552 	u_quad_t tcps_rcvbyteafterwin;	/* bytes rcvd after window */
    553 	u_quad_t tcps_rcvafterclose;	/* packets rcvd after "close" */
    554 	u_quad_t tcps_rcvwinprobe;	/* rcvd window probe packets */
    555 	u_quad_t tcps_rcvdupack;	/* rcvd duplicate acks */
    556 	u_quad_t tcps_rcvacktoomuch;	/* rcvd acks for unsent data */
    557 	u_quad_t tcps_rcvackpack;	/* rcvd ack packets */
    558 	u_quad_t tcps_rcvackbyte;	/* bytes acked by rcvd acks */
    559 	u_quad_t tcps_rcvwinupd;	/* rcvd window update packets */
    560 	u_quad_t tcps_pawsdrop;		/* segments dropped due to PAWS */
    561 	u_quad_t tcps_predack;		/* times hdr predict ok for acks */
    562 	u_quad_t tcps_preddat;		/* times hdr predict ok for data pkts */
    563 
    564 	u_quad_t tcps_pcbhashmiss;	/* input packets missing pcb hash */
    565 	u_quad_t tcps_noport;		/* no socket on port */
    566 	u_quad_t tcps_badsyn;		/* received ack for which we have
    567 					   no SYN in compressed state */
    568 	u_quad_t tcps_delayed_free;	/* delayed pool_put() of tcpcb */
    569 
    570 	/* These statistics deal with the SYN cache. */
    571 	u_quad_t tcps_sc_added;		/* # of entries added */
    572 	u_quad_t tcps_sc_completed;	/* # of connections completed */
    573 	u_quad_t tcps_sc_timed_out;	/* # of entries timed out */
    574 	u_quad_t tcps_sc_overflowed;	/* # dropped due to overflow */
    575 	u_quad_t tcps_sc_reset;		/* # dropped due to RST */
    576 	u_quad_t tcps_sc_unreach;	/* # dropped due to ICMP unreach */
    577 	u_quad_t tcps_sc_bucketoverflow;/* # dropped due to bucket overflow */
    578 	u_quad_t tcps_sc_aborted;	/* # of entries aborted (no mem) */
    579 	u_quad_t tcps_sc_dupesyn;	/* # of duplicate SYNs received */
    580 	u_quad_t tcps_sc_dropped;	/* # of SYNs dropped (no route/mem) */
    581 	u_quad_t tcps_sc_collisions;	/* # of hash collisions */
    582 	u_quad_t tcps_sc_retransmitted;	/* # of retransmissions */
    583 	u_quad_t tcps_sc_delayed_free;	/* # of delayed pool_put()s */
    584 
    585 	u_quad_t tcps_selfquench;	/* # of ENOBUFS we get on output */
    586 	u_quad_t tcps_badsig;		/* # of drops due to bad signature */
    587 	u_quad_t tcps_goodsig;		/* # of packets with good signature */
    588 };
    589 
    590 /*
    591  * Names for TCP sysctl objects.
    592  */
    593 #define	TCPCTL_RFC1323		1	/* RFC1323 timestamps/scaling */
    594 #define	TCPCTL_SENDSPACE	2	/* default send buffer */
    595 #define	TCPCTL_RECVSPACE	3	/* default recv buffer */
    596 #define	TCPCTL_MSSDFLT		4	/* default seg size */
    597 #define	TCPCTL_SYN_CACHE_LIMIT	5	/* max size of comp. state engine */
    598 #define	TCPCTL_SYN_BUCKET_LIMIT	6	/* max size of hash bucket */
    599 #if 0	/*obsoleted*/
    600 #define	TCPCTL_SYN_CACHE_INTER	7	/* interval of comp. state timer */
    601 #endif
    602 #define	TCPCTL_INIT_WIN		8	/* initial window */
    603 #define	TCPCTL_MSS_IFMTU	9	/* mss from interface, not in_maxmtu */
    604 #define	TCPCTL_SACK		10	/* RFC2018 selective acknowledgement */
    605 #define	TCPCTL_WSCALE		11	/* RFC1323 window scaling */
    606 #define	TCPCTL_TSTAMP		12	/* RFC1323 timestamps */
    607 #define	TCPCTL_COMPAT_42	13	/* 4.2BSD TCP bug work-arounds */
    608 #define	TCPCTL_CWM		14	/* Congestion Window Monitoring */
    609 #define	TCPCTL_CWM_BURSTSIZE	15	/* burst size allowed by CWM */
    610 #define	TCPCTL_ACK_ON_PUSH	16	/* ACK immediately on PUSH */
    611 #define	TCPCTL_KEEPIDLE		17	/* keepalive idle time */
    612 #define	TCPCTL_KEEPINTVL	18	/* keepalive probe interval */
    613 #define	TCPCTL_KEEPCNT		19	/* keepalive count */
    614 #define	TCPCTL_SLOWHZ		20	/* PR_SLOWHZ (read-only) */
    615 #define	TCPCTL_NEWRENO		21	/* NewReno Congestion Control */
    616 #define TCPCTL_LOG_REFUSED	22	/* Log refused connections */
    617 #if 0	/*obsoleted*/
    618 #define	TCPCTL_RSTRATELIMIT	23	/* RST rate limit */
    619 #endif
    620 #define	TCPCTL_RSTPPSLIMIT	24	/* RST pps limit */
    621 #define	TCPCTL_DELACK_TICKS	25	/* # ticks to delay ACK */
    622 #define	TCPCTL_INIT_WIN_LOCAL	26	/* initial window for local nets */
    623 #define	TCPCTL_IDENT		27	/* rfc 931 identd */
    624 #define	TCPCTL_ACKDROPRATELIMIT	28	/* SYN/RST -> ACK rate limit */
    625 #define	TCPCTL_MAXID		29
    626 
    627 #define	TCPCTL_NAMES { \
    628 	{ 0, 0 }, \
    629 	{ "rfc1323",	CTLTYPE_INT }, \
    630 	{ "sendspace",	CTLTYPE_INT }, \
    631 	{ "recvspace",	CTLTYPE_INT }, \
    632 	{ "mssdflt",	CTLTYPE_INT }, \
    633 	{ "syn_cache_limit", CTLTYPE_INT }, \
    634 	{ "syn_bucket_limit", CTLTYPE_INT }, \
    635 	{ 0, 0 },\
    636 	{ "init_win", CTLTYPE_INT }, \
    637 	{ "mss_ifmtu", CTLTYPE_INT }, \
    638 	{ "sack", CTLTYPE_INT }, \
    639 	{ "win_scale", CTLTYPE_INT }, \
    640 	{ "timestamps", CTLTYPE_INT }, \
    641 	{ "compat_42", CTLTYPE_INT }, \
    642 	{ "cwm", CTLTYPE_INT }, \
    643 	{ "cwm_burstsize", CTLTYPE_INT }, \
    644 	{ "ack_on_push", CTLTYPE_INT }, \
    645 	{ "keepidle",	CTLTYPE_INT }, \
    646 	{ "keepintvl",	CTLTYPE_INT }, \
    647 	{ "keepcnt",	CTLTYPE_INT }, \
    648 	{ "slowhz",	CTLTYPE_INT }, \
    649 	{ "newreno",	CTLTYPE_INT }, \
    650 	{ "log_refused",CTLTYPE_INT }, \
    651 	{ 0, 0 }, \
    652 	{ "rstppslimit", CTLTYPE_INT }, \
    653 	{ "delack_ticks", CTLTYPE_INT }, \
    654 	{ "init_win_local", CTLTYPE_INT }, \
    655 	{ "ident", CTLTYPE_STRUCT }, \
    656 	{ "ackdropppslimit", CTLTYPE_INT }, \
    657 }
    658 
    659 #ifdef _KERNEL
    660 extern	struct inpcbtable tcbtable;	/* head of queue of active tcpcb's */
    661 extern	struct tcpstat tcpstat;	/* tcp statistics */
    662 extern	u_int32_t tcp_now;	/* for RFC 1323 timestamps */
    663 extern	int tcp_do_rfc1323;	/* enabled/disabled? */
    664 extern	int tcp_do_sack;	/* SACK enabled/disabled? */
    665 extern	int tcp_do_win_scale;	/* RFC1323 window scaling enabled/disabled? */
    666 extern	int tcp_do_timestamps;	/* RFC1323 timestamps enabled/disabled? */
    667 extern	int tcp_do_newreno;	/* Use the New Reno algorithms */
    668 extern	int tcp_mssdflt;	/* default seg size */
    669 extern	int tcp_init_win;	/* initial window */
    670 extern	int tcp_init_win_local;	/* initial window for local nets */
    671 extern	int tcp_mss_ifmtu;	/* take MSS from interface, not in_maxmtu */
    672 extern	int tcp_compat_42;	/* work around ancient broken TCP peers */
    673 extern	int tcp_cwm;		/* enable Congestion Window Monitoring */
    674 extern	int tcp_cwm_burstsize;	/* burst size allowed by CWM */
    675 extern	int tcp_ack_on_push;	/* ACK immediately on PUSH */
    676 extern	int tcp_syn_cache_limit; /* max entries for compressed state engine */
    677 extern	int tcp_syn_bucket_limit;/* max entries per hash bucket */
    678 extern	int tcp_log_refused;	/* log refused connections */
    679 
    680 extern	int tcp_rst_ppslim;
    681 extern	int tcp_ackdrop_ppslim;
    682 
    683 extern	int tcp_syn_cache_size;
    684 extern	struct syn_cache_head tcp_syn_cache[];
    685 extern	u_long syn_cache_count;
    686 
    687 #ifdef MBUFTRACE
    688 extern	struct mowner tcp_rx_mowner;
    689 extern	struct mowner tcp_tx_mowner;
    690 extern	struct mowner tcp_mowner;
    691 #endif
    692 
    693 #define	TCPCTL_VARIABLES { \
    694 	{ 0 },					\
    695 	{ 1, 0, &tcp_do_rfc1323 },		\
    696 	{ 1, 0, &tcp_sendspace },		\
    697 	{ 1, 0, &tcp_recvspace },		\
    698 	{ 1, 0, &tcp_mssdflt },			\
    699 	{ 1, 0, &tcp_syn_cache_limit },		\
    700 	{ 1, 0, &tcp_syn_bucket_limit },	\
    701 	{ 0 },					\
    702 	{ 1, 0, &tcp_init_win },		\
    703 	{ 1, 0, &tcp_mss_ifmtu },		\
    704 	{ 1, 0, &tcp_do_sack },			\
    705 	{ 1, 0, &tcp_do_win_scale },		\
    706 	{ 1, 0, &tcp_do_timestamps },		\
    707 	{ 1, 0, &tcp_compat_42 },		\
    708 	{ 1, 0, &tcp_cwm },			\
    709 	{ 1, 0, &tcp_cwm_burstsize },		\
    710 	{ 1, 0, &tcp_ack_on_push },		\
    711 	{ 1, 0, &tcp_keepidle },		\
    712 	{ 1, 0, &tcp_keepintvl },		\
    713 	{ 1, 0, &tcp_keepcnt },			\
    714 	{ 1, 1, 0, PR_SLOWHZ },			\
    715 	{ 1, 0, &tcp_do_newreno },		\
    716 	{ 1, 0, &tcp_log_refused },		\
    717 	{ 0 },					\
    718 	{ 1, 0, &tcp_rst_ppslim },		\
    719 	{ 1, 0, &tcp_delack_ticks },		\
    720 	{ 1, 0, &tcp_init_win_local },		\
    721 	{ 1, 0, &tcp_ackdrop_ppslim },		\
    722 }
    723 
    724 #ifdef __NO_STRICT_ALIGNMENT
    725 #define	TCP_HDR_ALIGNED_P(th)	1
    726 #else
    727 #define	TCP_HDR_ALIGNED_P(th)	((((vaddr_t)(th)) & 3) == 0)
    728 #endif
    729 
    730 struct secasvar;
    731 
    732 int	 tcp_attach(struct socket *);
    733 void	 tcp_canceltimers(struct tcpcb *);
    734 int	 tcp_timers_invoking(struct tcpcb*);
    735 struct tcpcb *
    736 	 tcp_close(struct tcpcb *);
    737 int	 tcp_isdead(struct tcpcb *);
    738 #ifdef INET6
    739 void	 tcp6_ctlinput(int, struct sockaddr *, void *);
    740 #endif
    741 void	 *tcp_ctlinput(int, struct sockaddr *, void *);
    742 int	 tcp_ctloutput(int, struct socket *, int, int, struct mbuf **);
    743 struct tcpcb *
    744 	 tcp_disconnect(struct tcpcb *);
    745 struct tcpcb *
    746 	 tcp_drop(struct tcpcb *, int);
    747 #ifdef TCP_SIGNATURE
    748 int	 tcp_signature_apply(void *, caddr_t, u_int);
    749 struct secasvar *tcp_signature_getsav(struct mbuf *, struct tcphdr *);
    750 int	 tcp_signature(struct mbuf *, struct tcphdr *, int, struct secasvar *,
    751 	    char *);
    752 #endif
    753 int	 tcp_dooptions(struct tcpcb *, u_char *, int, struct tcphdr *,
    754 	    struct mbuf *, int, struct tcp_opt_info *);
    755 void	 tcp_drain(void);
    756 void	 tcp_established(struct tcpcb *);
    757 void	 tcp_init(void);
    758 #ifdef INET6
    759 int	 tcp6_input(struct mbuf **, int *, int);
    760 #endif
    761 void	 tcp_input(struct mbuf *, ...);
    762 u_long	 tcp_mss_to_advertise(const struct ifnet *, int);
    763 void	 tcp_mss_from_peer(struct tcpcb *, int);
    764 void	 tcp_tcpcb_template(void);
    765 struct tcpcb *
    766 	 tcp_newtcpcb(int, void *);
    767 void	 tcp_notify(struct inpcb *, int);
    768 #ifdef INET6
    769 void	 tcp6_notify(struct in6pcb *, int);
    770 #endif
    771 u_int	 tcp_optlen(struct tcpcb *);
    772 int	 tcp_output(struct tcpcb *);
    773 void	 tcp_pulloutofband(struct socket *,
    774 	    struct tcphdr *, struct mbuf *, int);
    775 void	 tcp_quench(struct inpcb *, int);
    776 #ifdef INET6
    777 void	 tcp6_quench(struct in6pcb *, int);
    778 #endif
    779 int	 tcp_reass(struct tcpcb *, struct tcphdr *, struct mbuf *, int *);
    780 int	 tcp_respond(struct tcpcb *, struct mbuf *, struct mbuf *,
    781 	    struct tcphdr *, tcp_seq, tcp_seq, int);
    782 void	 tcp_rmx_rtt(struct tcpcb *);
    783 void	 tcp_setpersist(struct tcpcb *);
    784 #ifdef TCP_SIGNATURE
    785 int	 tcp_signature_compute(struct mbuf *, struct tcphdr *, int, int,
    786 	    int, u_char *, u_int);
    787 #endif
    788 void	 tcp_slowtimo(void);
    789 struct mbuf *
    790 	 tcp_template(struct tcpcb *);
    791 void	 tcp_trace(int, int, struct tcpcb *, struct mbuf *, int);
    792 struct tcpcb *
    793 	 tcp_usrclosed(struct tcpcb *);
    794 int	 tcp_sysctl(int *, u_int, void *, size_t *, void *, size_t);
    795 int	 tcp_usrreq(struct socket *,
    796 	    int, struct mbuf *, struct mbuf *, struct mbuf *, struct proc *);
    797 void	 tcp_xmit_timer(struct tcpcb *, uint32_t);
    798 tcp_seq	 tcp_new_iss(struct tcpcb *, tcp_seq);
    799 tcp_seq  tcp_new_iss1(void *, void *, u_int16_t, u_int16_t, size_t,
    800 	    tcp_seq);
    801 
    802 int	 syn_cache_add(struct sockaddr *, struct sockaddr *,
    803 		struct tcphdr *, unsigned int, struct socket *,
    804 		struct mbuf *, u_char *, int, struct tcp_opt_info *);
    805 void	 syn_cache_unreach(struct sockaddr *, struct sockaddr *,
    806 	   struct tcphdr *);
    807 struct socket *syn_cache_get(struct sockaddr *, struct sockaddr *,
    808 		struct tcphdr *, unsigned int, unsigned int,
    809 		struct socket *so, struct mbuf *);
    810 void	 syn_cache_init(void);
    811 void	 syn_cache_insert(struct syn_cache *, struct tcpcb *);
    812 struct syn_cache *syn_cache_lookup(struct sockaddr *, struct sockaddr *,
    813 		struct syn_cache_head **);
    814 void	 syn_cache_reset(struct sockaddr *, struct sockaddr *,
    815 		struct tcphdr *);
    816 int	 syn_cache_respond(struct syn_cache *, struct mbuf *);
    817 void	 syn_cache_timer(void *);
    818 void	 syn_cache_cleanup(struct tcpcb *);
    819 
    820 int	tcp_newreno(struct tcpcb *, struct tcphdr *);
    821 #endif
    822 
    823 #endif /* _NETINET_TCP_VAR_H_ */
    824