18b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project/*
28b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * Copyright (c) 1982, 1986, 1993
38b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project *	The Regents of the University of California.  All rights reserved.
48b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project *
58b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * Redistribution and use in source and binary forms, with or without
68b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * modification, are permitted provided that the following conditions
78b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * are met:
88b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * 1. Redistributions of source code must retain the above copyright
98b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
108b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
118b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
128b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project *    documentation and/or other materials provided with the distribution.
135d8f37ad78fc66901af50c762029a501561f3b23David 'Digit' Turner * 3. Neither the name of the University nor the names of its contributors
148b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project *    may be used to endorse or promote products derived from this software
158b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project *    without specific prior written permission.
168b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project *
178b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
188b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
218b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
238b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
258b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * SUCH DAMAGE.
288b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project *
298b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project *	@(#)tcp.h	8.1 (Berkeley) 6/10/93
308b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * tcp.h,v 1.3 1994/08/21 05:27:34 paul Exp
318b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project */
328b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
338b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#ifndef _TCP_H_
348b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define _TCP_H_
358b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
368b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Projecttypedef	u_int32_t	tcp_seq;
378b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
388b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define      PR_SLOWHZ       2               /* 2 slow timeouts per second (approx) */
398b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define      PR_FASTHZ       5               /* 5 fast timeouts per second (not important) */
408b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
418b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Projectextern struct socket *tcp_last_so;
428b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
438b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCP_SNDSPACE 8192
448b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCP_RCVSPACE 8192
458b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
468b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project/*
478b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * TCP header.
488b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * Per RFC 793, September, 1981.
498b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project */
508b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Projectstruct tcphdr {
515d8f37ad78fc66901af50c762029a501561f3b23David 'Digit' Turner	u_int16_t	th_sport;		/* source port */
525d8f37ad78fc66901af50c762029a501561f3b23David 'Digit' Turner	u_int16_t	th_dport;		/* destination port */
538b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project	tcp_seq	th_seq;			/* sequence number */
548b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project	tcp_seq	th_ack;			/* acknowledgement number */
5520894ae3fa98f82da925fbeb72e616eef509758aDavid 'Digit' Turner#ifdef HOST_WORDS_BIGENDIAN
568b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project	u_int	th_off:4,		/* data offset */
578b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project		th_x2:4;		/* (unused) */
588b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#else
598b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project	u_int	th_x2:4,		/* (unused) */
608b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project		th_off:4;		/* data offset */
618b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#endif
628b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project	u_int8_t	th_flags;
638b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define	TH_FIN	0x01
648b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define	TH_SYN	0x02
658b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define	TH_RST	0x04
668b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define	TH_PUSH	0x08
678b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define	TH_ACK	0x10
688b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define	TH_URG	0x20
698b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project	u_int16_t	th_win;			/* window */
708b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project	u_int16_t	th_sum;			/* checksum */
718b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project	u_int16_t	th_urp;			/* urgent pointer */
728b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project};
738b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
748b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "tcp_var.h"
758b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
768b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define	TCPOPT_EOL		0
778b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define	TCPOPT_NOP		1
788b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define	TCPOPT_MAXSEG		2
798b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define    TCPOLEN_MAXSEG		4
808b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPOPT_WINDOW		3
818b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define    TCPOLEN_WINDOW		3
828b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPOPT_SACK_PERMITTED	4		/* Experimental */
838b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define    TCPOLEN_SACK_PERMITTED	2
848b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPOPT_SACK		5		/* Experimental */
858b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPOPT_TIMESTAMP	8
868b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define    TCPOLEN_TIMESTAMP		10
878b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define    TCPOLEN_TSTAMP_APPA		(TCPOLEN_TIMESTAMP+2) /* appendix A */
888b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
898b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPOPT_TSTAMP_HDR	\
908b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    (TCPOPT_NOP<<24|TCPOPT_NOP<<16|TCPOPT_TIMESTAMP<<8|TCPOLEN_TIMESTAMP)
918b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
928b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project/*
938b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * Default maximum segment size for TCP.
948b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * With an IP MSS of 576, this is 536,
958b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * but 512 is probably more convenient.
968b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * This should be defined as MIN(512, IP_MSS - sizeof (struct tcpiphdr)).
978b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project *
988b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * We make this 1460 because we only care about Ethernet in the qemu context.
998b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project */
1008b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define	TCP_MSS	1460
1018b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1028b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define	TCP_MAXWIN	65535	/* largest value for (unscaled) window */
1038b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1048b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCP_MAX_WINSHIFT	14	/* maximum window shift */
1058b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1068b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project/*
1078b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * User-settable options (used with setsockopt).
1085d8f37ad78fc66901af50c762029a501561f3b23David 'Digit' Turner *
1095d8f37ad78fc66901af50c762029a501561f3b23David 'Digit' Turner * We don't use the system headers on unix because we have conflicting
1105d8f37ad78fc66901af50c762029a501561f3b23David 'Digit' Turner * local structures. We can't avoid the system definitions on Windows,
1115d8f37ad78fc66901af50c762029a501561f3b23David 'Digit' Turner * so we undefine them.
1128b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project */
1135d8f37ad78fc66901af50c762029a501561f3b23David 'Digit' Turner#undef TCP_NODELAY
1148b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define	TCP_NODELAY	0x01	/* don't delay send to coalesce packets */
1155d8f37ad78fc66901af50c762029a501561f3b23David 'Digit' Turner#undef TCP_MAXSEG
1168b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project/* #define	TCP_MAXSEG	0x02 */	/* set maximum segment size */
1178b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1188b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project/*
1198b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * TCP FSM state definitions.
1208b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * Per RFC793, September, 1981.
1218b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project */
1228b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1238b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCP_NSTATES     11
1248b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1258b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPS_CLOSED             0       /* closed */
1268b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPS_LISTEN             1       /* listening for connection */
1278b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPS_SYN_SENT           2       /* active, have sent syn */
1288b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPS_SYN_RECEIVED       3       /* have send and received syn */
1298b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project/* states < TCPS_ESTABLISHED are those where connections not established */
1308b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPS_ESTABLISHED        4       /* established */
1318b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPS_CLOSE_WAIT         5       /* rcvd fin, waiting for close */
1328b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project/* states > TCPS_CLOSE_WAIT are those where user has closed */
1338b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPS_FIN_WAIT_1         6       /* have closed, sent fin */
1348b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPS_CLOSING            7       /* closed xchd FIN; await FIN ACK */
1358b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPS_LAST_ACK           8       /* had fin and close; await FIN ACK */
1368b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project/* states > TCPS_CLOSE_WAIT && < TCPS_FIN_WAIT_2 await ACK of FIN */
1378b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPS_FIN_WAIT_2         9       /* have closed, fin is acked */
1388b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPS_TIME_WAIT          10      /* in 2*msl quiet wait after close */
1398b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1408b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPS_HAVERCVDSYN(s)     ((s) >= TCPS_SYN_RECEIVED)
1418b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPS_HAVEESTABLISHED(s) ((s) >= TCPS_ESTABLISHED)
1428b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCPS_HAVERCVDFIN(s)     ((s) >= TCPS_TIME_WAIT)
1438b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1448b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project/*
1458b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * TCP sequence numbers are 32 bit integers operated
1468b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * on with modular arithmetic.  These macros can be
1478b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * used to compare such integers.
1488b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project */
1498b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define SEQ_LT(a,b)     ((int)((a)-(b)) < 0)
1508b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define SEQ_LEQ(a,b)    ((int)((a)-(b)) <= 0)
1518b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define SEQ_GT(a,b)     ((int)((a)-(b)) > 0)
1528b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define SEQ_GEQ(a,b)    ((int)((a)-(b)) >= 0)
1538b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1548b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project/*
1558b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * Macros to initialize tcp sequence numbers for
1568b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * send and receive from initial send and receive
1578b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * sequence numbers.
1588b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project */
1598b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define tcp_rcvseqinit(tp) \
1608b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project     (tp)->rcv_adv = (tp)->rcv_nxt = (tp)->irs + 1
1618b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1628b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define tcp_sendseqinit(tp) \
1638b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    (tp)->snd_una = (tp)->snd_nxt = (tp)->snd_max = (tp)->snd_up = (tp)->iss
1648b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1658b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TCP_ISSINCR     (125*1024)      /* increment for tcp_iss each second */
1668b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1678b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Projectextern tcp_seq tcp_iss;                /* tcp initial send seq # */
1688b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1695d8f37ad78fc66901af50c762029a501561f3b23David 'Digit' Turnerextern const char * const tcpstates[];
1708b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1718b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#endif
172