Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | \ ========== Copyright Header Begin ========================================== |
2 | \ | |
3 | \ Hypervisor Software File: tcp-h.fth | |
4 | \ | |
5 | \ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | \ | |
7 | \ - Do no alter or remove copyright notices | |
8 | \ | |
9 | \ - Redistribution and use of this software in source and binary forms, with | |
10 | \ or without modification, are permitted provided that the following | |
11 | \ conditions are met: | |
12 | \ | |
13 | \ - Redistribution of source code must retain the above copyright notice, | |
14 | \ this list of conditions and the following disclaimer. | |
15 | \ | |
16 | \ - Redistribution in binary form must reproduce the above copyright notice, | |
17 | \ this list of conditions and the following disclaimer in the | |
18 | \ documentation and/or other materials provided with the distribution. | |
19 | \ | |
20 | \ Neither the name of Sun Microsystems, Inc. or the names of contributors | |
21 | \ may be used to endorse or promote products derived from this software | |
22 | \ without specific prior written permission. | |
23 | \ | |
24 | \ This software is provided "AS IS," without a warranty of any kind. | |
25 | \ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, | |
26 | \ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A | |
27 | \ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN | |
28 | \ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR | |
29 | \ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR | |
30 | \ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN | |
31 | \ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR | |
32 | \ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE | |
33 | \ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, | |
34 | \ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF | |
35 | \ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | |
36 | \ | |
37 | \ You acknowledge that this software is not designed, licensed or | |
38 | \ intended for use in the design, construction, operation or maintenance of | |
39 | \ any nuclear facility. | |
40 | \ | |
41 | \ ========== Copyright Header End ============================================ | |
42 | id: @(#)tcp-h.fth 1.1 04/09/07 | |
43 | purpose: | |
44 | copyright: Copyright 2004 Sun Microsystems, Inc. All Rights Reserved | |
45 | copyright: Use is subject to license terms. | |
46 | ||
47 | headerless | |
48 | ||
49 | struct | |
50 | /ip-header field >tcp-iphdr \ IP header, no options | |
51 | /w field >tcp-sport \ Source port | |
52 | /w field >tcp-dport \ Destination port | |
53 | /l field >tcp-seq \ Sequence number | |
54 | /l field >tcp-ack \ Acknowledgement number | |
55 | /c field >tcp-offset \ Data offset (4 bits), Rsvd (4 bits) | |
56 | /c field >tcp-flags \ Flags | |
57 | /w field >tcp-window \ Advertised window | |
58 | /w field >tcp-cksum \ Checksum | |
59 | /w field >tcp-urgptr \ Urgent pointer | |
60 | 0 field >tcp-options \ options | |
61 | constant /tcpip-header | |
62 | ||
63 | d# 20 constant /tcp-header | |
64 | ||
65 | \ TCP flags (Control bits) | |
66 | 1 constant TH_FIN \ No more data from sender | |
67 | 2 constant TH_SYN \ Synchronize sequence numbers | |
68 | h# 04 constant TH_RST \ Reset connection | |
69 | h# 08 constant TH_PSH \ Push function | |
70 | h# 10 constant TH_ACK \ Ack# is valid | |
71 | h# 20 constant TH_URG \ Urgent pointer is valid | |
72 | ||
73 | \ TCP options | |
74 | 0 constant TCPOPT_EOL \ End of Option list | |
75 | 1 constant TCPOPT_NOP \ NOOP | |
76 | 2 constant TCPOPT_MSS \ Maximum segment size | |
77 | ||
78 | \ TCP FSM states | |
79 | 0 constant TCPS_CLOSED \ Closed | |
80 | 1 constant TCPS_LISTEN \ Listening for connection | |
81 | 2 constant TCPS_SYN_SENT \ Sent SYN (active open) | |
82 | 3 constant TCPS_SYN_RCVD \ Sent and received SYN, awaiting ACK | |
83 | d# 4 constant TCPS_ESTABLISHED \ Established (Data transfer) | |
84 | d# 5 constant TCPS_CLOSE_WAIT \ Received FIN; waiting for close | |
85 | d# 6 constant TCPS_FIN_WAIT_1 \ Have closed; Sent FIN | |
86 | d# 7 constant TCPS_CLOSING \ Simultaneous close, awaiting FIN ACK | |
87 | d# 8 constant TCPS_LAST_ACK \ Passive close, awaiting FIN ACK | |
88 | d# 9 constant TCPS_FIN_WAIT_2 \ Active close, FIN is ACKed | |
89 | d# 10 constant TCPS_TIME_WAIT \ In 2MSL state after active close | |
90 | ||
91 | d# 536 constant TCP_DEFAULT_MSS | |
92 | ||
93 | : tcp-hlen@ ( pkt -- n ) >tcp-offset c@ h# f0 and 2 >> ; | |
94 | : tcp-hlen! ( n pkt -- ) swap 2 << h# f0 and swap >tcp-offset c! ; | |
95 | ||
96 | : tcpip-hlen@ ( pkt -- n ) tcp-hlen@ /ip-header + ; | |
97 | ||
98 | : tcp-flags@ ( pkt -- flags ) >tcp-flags c@ ; | |
99 | : tcp-flags! ( flags pkt -- ) >tcp-flags c! ; | |
100 | ||
101 | \ Routines to check TCP flags | |
102 | : is-tcpfin? ( pkt -- flag ) tcp-flags@ TH_FIN and ; | |
103 | : is-tcpsyn? ( pkt -- flag ) tcp-flags@ TH_SYN and ; | |
104 | : is-tcprst? ( pkt -- flag ) tcp-flags@ TH_RST and ; | |
105 | : is-tcppsh? ( pkt -- flag ) tcp-flags@ TH_PSH and ; | |
106 | : is-tcpack? ( pkt -- flag ) tcp-flags@ TH_ACK and ; | |
107 | ||
108 | : tcp-clear-flags ( pkt flags -- ) | |
109 | invert over tcp-flags@ and swap tcp-flags! | |
110 | ; | |
111 | ||
112 | : seg-ack@ ( pkt -- ack# ) >tcp-ack ntohl@ ; | |
113 | : seg-seq@ ( pkt -- seq# ) >tcp-seq ntohl@ ; | |
114 | : seg-wnd@ ( pkt -- n ) >tcp-window ntohw@ ; | |
115 | ||
116 | \ Size of data in the segment. SYN and FIN are not counted here. | |
117 | : seg-datalen@ ( pkt -- n ) | |
118 | dup ippkt>payload nip swap tcp-hlen@ - | |
119 | ; | |
120 | ||
121 | \ Segment length, including SYN and FIN. | |
122 | : seg-len@ ( pkt -- n ) | |
123 | dup seg-datalen@ ( pkt datalen ) | |
124 | over is-tcpsyn? if 1+ then ( pkt datalen' ) | |
125 | swap is-tcpfin? if 1+ then ( seg.len ) | |
126 | ; | |
127 | ||
128 | \ Last sequence number occupied by a segment | |
129 | : seg-lastseq@ ( pkt -- n ) dup seg-seq@ swap seg-len@ + 1- ; | |
130 | ||
131 | \ Length of options in packet | |
132 | : tcp-optlen@ ( pkt -- n ) tcpip-hlen@ 0 >tcp-options - ; | |
133 | ||
134 | \ TCP sequence number comparison routines | |
135 | : seq< ( s1 s2 -- flag ) - 0< ; | |
136 | : seq> ( s1 s2 -- flag ) - 0> ; | |
137 | : seq<= ( s1 s2 -- flag ) - 0<= ; | |
138 | : seq>= ( s1 s2 -- flag ) - 0>= ; | |
139 | : seq= ( s1 s2 -- flag ) - 0= ; | |
140 | : seq<> ( s1 s2 -- flag ) - 0<> ; | |
141 | ||
142 | : seq-within ( n s1 s2 -- flag ) | |
143 | >r over seq<= swap r> seq< and | |
144 | ; | |
145 | ||
146 | headers |