1 | /*
|
---|
2 | * Copyright (c) 2011 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup tcp
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file Sequence number computations
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <assert.h>
|
---|
38 | #include <bool.h>
|
---|
39 | #include <sys/types.h>
|
---|
40 | #include "seq_no.h"
|
---|
41 | #include "tcp_type.h"
|
---|
42 |
|
---|
43 | /** a <= b < c modulo sequence space */
|
---|
44 | static bool seq_no_le_lt(uint32_t a, uint32_t b, uint32_t c)
|
---|
45 | {
|
---|
46 | if (a <= c) {
|
---|
47 | return (a <= b) && (b < c);
|
---|
48 | } else {
|
---|
49 | return (b < c) || (a <= b);
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | /** a < b <= c modulo sequence space */
|
---|
54 | static bool seq_no_lt_le(uint32_t a, uint32_t b, uint32_t c)
|
---|
55 | {
|
---|
56 | if (a <= c) {
|
---|
57 | return (a < b) && (b <= c);
|
---|
58 | } else {
|
---|
59 | return (b <= c) || (a < b);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | /** Determine wheter ack is acceptable (new acknowledgement) */
|
---|
64 | bool seq_no_ack_acceptable(tcp_conn_t *conn, uint32_t seg_ack)
|
---|
65 | {
|
---|
66 | /* SND.UNA < SEG.ACK <= SND.NXT */
|
---|
67 | return seq_no_lt_le(conn->snd_una, seg_ack, conn->snd_nxt);
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** Determine wheter ack is duplicate.
|
---|
71 | *
|
---|
72 | * ACK is duplicate if it refers to a sequence number that has
|
---|
73 | * aleady been acked (SEG.ACK <= SND.UNA).
|
---|
74 | */
|
---|
75 | bool seq_no_ack_duplicate(tcp_conn_t *conn, uint32_t seg_ack)
|
---|
76 | {
|
---|
77 | uint32_t diff;
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * There does not seem to be a three-point comparison
|
---|
81 | * equivalent of SEG.ACK < SND.UNA. Thus we do it
|
---|
82 | * on a best-effort basis, based on the difference.
|
---|
83 | * [-2^31, 0) means less-than, 0 means equal, [0, 2^31)
|
---|
84 | * means greater-than. Less-than or equal means duplicate.
|
---|
85 | */
|
---|
86 | diff = seg_ack - conn->snd_una;
|
---|
87 | return diff == 0 || (diff & (0x1 << 31)) != 0;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /** Determine if sequence number is in receive window. */
|
---|
91 | bool seq_no_in_rcv_wnd(tcp_conn_t *conn, uint32_t sn)
|
---|
92 | {
|
---|
93 | return seq_no_le_lt(conn->rcv_nxt, sn, conn->rcv_nxt + conn->rcv_wnd);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Determine segment has new window update.
|
---|
97 | *
|
---|
98 | * Window update is new if either SND.WL1 < SEG.SEQ or
|
---|
99 | * (SND.WL1 = SEG.SEQ and SND.WL2 <= SEG.ACK).
|
---|
100 | */
|
---|
101 | bool seq_no_new_wnd_update(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
102 | {
|
---|
103 | bool n_seq, n_ack;
|
---|
104 |
|
---|
105 | assert(seq_no_segment_acceptable(conn, seg));
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * We make use of the fact that the peer should not ACK anything
|
---|
109 | * beyond our send window (we surely haven't sent that yet)
|
---|
110 | * as we should have filtered those acks out.
|
---|
111 | * We use SND.UNA+SND.WND as the third point of comparison.
|
---|
112 | */
|
---|
113 |
|
---|
114 | n_seq = seq_no_lt_le(conn->snd_wl1, seg->seq,
|
---|
115 | conn->snd_una + conn->snd_wnd);
|
---|
116 |
|
---|
117 | n_ack = conn->snd_wl1 == seg->seq &&
|
---|
118 | seq_no_le_lt(conn->snd_wl2, seg->ack,
|
---|
119 | conn->snd_una + conn->snd_wnd + 1);
|
---|
120 |
|
---|
121 | return n_seq || n_ack;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /** Determine if segment is ready for processing.
|
---|
125 | *
|
---|
126 | * Assuming segment is acceptable, a segment is ready if it intersects
|
---|
127 | * RCV.NXT, that is we can process it immediately.
|
---|
128 | */
|
---|
129 | bool seq_no_segment_ready(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
130 | {
|
---|
131 | assert(seq_no_segment_acceptable(conn, seg));
|
---|
132 |
|
---|
133 | return seq_no_le_lt(seg->seq, conn->rcv_nxt, seg->seq + seg->len + 1);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /** Determine whether segment is fully acked */
|
---|
137 | bool seq_no_segment_acked(tcp_conn_t *conn, tcp_segment_t *seg, uint32_t ack)
|
---|
138 | {
|
---|
139 | assert(seg->len > 0);
|
---|
140 | return seq_no_lt_le(seg->seq, seg->seq + seg->len, ack);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /** Determine whether initial SYN is acked */
|
---|
144 | bool seq_no_syn_acked(tcp_conn_t *conn)
|
---|
145 | {
|
---|
146 | return seq_no_lt_le(conn->iss, conn->snd_una, conn->snd_nxt);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /** Determine whether segment overlaps the receive window */
|
---|
150 | bool seq_no_segment_acceptable(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
151 | {
|
---|
152 | bool b_in, e_in;
|
---|
153 |
|
---|
154 | b_in = seq_no_le_lt(conn->rcv_nxt, seg->seq, conn->rcv_nxt
|
---|
155 | + conn->rcv_wnd);
|
---|
156 |
|
---|
157 | e_in = seq_no_le_lt(conn->rcv_nxt, seg->seq + seg->len - 1,
|
---|
158 | conn->rcv_nxt + conn->rcv_wnd);
|
---|
159 |
|
---|
160 | if (seg->len == 0 && conn->rcv_wnd == 0) {
|
---|
161 | return seg->seq == conn->rcv_nxt;
|
---|
162 | } else if (seg->len == 0 && conn->rcv_wnd != 0) {
|
---|
163 | return b_in;
|
---|
164 | } else if (seg->len > 0 && conn->rcv_wnd == 0) {
|
---|
165 | return false;
|
---|
166 | } else {
|
---|
167 | return b_in || e_in;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | /** Determine size that control bits occupy in sequence space. */
|
---|
172 | uint32_t seq_no_control_len(tcp_control_t ctrl)
|
---|
173 | {
|
---|
174 | uint32_t len = 0;
|
---|
175 |
|
---|
176 | if ((ctrl & CTL_SYN) != 0)
|
---|
177 | ++len;
|
---|
178 |
|
---|
179 | if ((ctrl & CTL_FIN) != 0)
|
---|
180 | ++len;
|
---|
181 |
|
---|
182 | return len;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /** Calculate the amount of trim needed to fit segment in receive window. */
|
---|
186 | void seq_no_seg_trim_calc(tcp_conn_t *conn, tcp_segment_t *seg,
|
---|
187 | uint32_t *left, uint32_t *right)
|
---|
188 | {
|
---|
189 | assert(seq_no_segment_acceptable(conn, seg));
|
---|
190 |
|
---|
191 | /*
|
---|
192 | * If RCV.NXT is between SEG.SEQ and RCV.NXT+RCV.WND, then
|
---|
193 | * left trim amount is positive
|
---|
194 | */
|
---|
195 | if (seq_no_lt_le(seg->seq, conn->rcv_nxt,
|
---|
196 | conn->rcv_nxt + conn->rcv_wnd)) {
|
---|
197 | *left = conn->rcv_nxt - seg->seq;
|
---|
198 | } else {
|
---|
199 | *left = 0;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /*
|
---|
203 | * If SEG.SEQ+SEG.LEN is between SEG.SEQ and RCV.NXT+RCV.WND,
|
---|
204 | * then right trim is zero.
|
---|
205 | */
|
---|
206 | if (seq_no_lt_le(seg->seq - 1, seg->seq + seg->len,
|
---|
207 | conn->rcv_nxt + conn->rcv_wnd)) {
|
---|
208 | *right = 0;
|
---|
209 | } else {
|
---|
210 | *right = (seg->seq + seg->len) -
|
---|
211 | (conn->rcv_nxt + conn->rcv_wnd);
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | /** Segment order comparison.
|
---|
216 | *
|
---|
217 | * Compare sequence order of two acceptable segments.
|
---|
218 | *
|
---|
219 | * @param conn Connection
|
---|
220 | * @param sa Segment A
|
---|
221 | * @param sb Segment B
|
---|
222 | *
|
---|
223 | * @return -1, 0, 1, resp. if A < B, A == B, A > B in terms
|
---|
224 | * of sequence order of the beginning of the segment.
|
---|
225 | */
|
---|
226 | int seq_no_seg_cmp(tcp_conn_t *conn, tcp_segment_t *sa, tcp_segment_t *sb)
|
---|
227 | {
|
---|
228 | assert(seq_no_segment_acceptable(conn, sa));
|
---|
229 | assert(seq_no_segment_acceptable(conn, sb));
|
---|
230 |
|
---|
231 | if (seq_no_lt_le(sa->seq, sb->seq, conn->rcv_nxt + conn->rcv_wnd))
|
---|
232 | return -1;
|
---|
233 |
|
---|
234 | if (seq_no_lt_le(sb->seq, sa->seq, conn->rcv_nxt + conn->rcv_wnd))
|
---|
235 | return +1;
|
---|
236 |
|
---|
237 | assert(sa->seq == sb->seq);
|
---|
238 | return 0;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * @}
|
---|
243 | */
|
---|