| 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 segment has new window update.
|
|---|
| 91 | *
|
|---|
| 92 | * Window update is new if either SND.WL1 < SEG.SEQ or
|
|---|
| 93 | * (SND.WL1 = SEG.SEQ and SND.WL2 <= SEG.ACK).
|
|---|
| 94 | */
|
|---|
| 95 | bool seq_no_new_wnd_update(tcp_conn_t *conn, tcp_segment_t *seg)
|
|---|
| 96 | {
|
|---|
| 97 | bool n_seq, n_ack;
|
|---|
| 98 |
|
|---|
| 99 | assert(seq_no_segment_acceptable(conn, seg));
|
|---|
| 100 |
|
|---|
| 101 | /*
|
|---|
| 102 | * We make use of the fact that the peer should not ACK anything
|
|---|
| 103 | * beyond our send window (we surely haven't sent that yet)
|
|---|
| 104 | * as we should have filtered those acks out.
|
|---|
| 105 | * We use SND.UNA+SND.WND as the third point of comparison.
|
|---|
| 106 | */
|
|---|
| 107 |
|
|---|
| 108 | n_seq = seq_no_lt_le(conn->snd_wl1, seg->seq,
|
|---|
| 109 | conn->snd_una + conn->snd_wnd);
|
|---|
| 110 |
|
|---|
| 111 | n_ack = conn->snd_wl1 == seg->seq &&
|
|---|
| 112 | seq_no_le_lt(conn->snd_wl2, seg->ack,
|
|---|
| 113 | conn->snd_una + conn->snd_wnd + 1);
|
|---|
| 114 |
|
|---|
| 115 | return n_seq || n_ack;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /** Determine if segment is ready for processing.
|
|---|
| 119 | *
|
|---|
| 120 | * Assuming segment is acceptable, a segment is ready if it intersects
|
|---|
| 121 | * RCV.NXT, that is we can process it immediately.
|
|---|
| 122 | */
|
|---|
| 123 | bool seq_no_segment_ready(tcp_conn_t *conn, tcp_segment_t *seg)
|
|---|
| 124 | {
|
|---|
| 125 | assert(seq_no_segment_acceptable(conn, seg));
|
|---|
| 126 |
|
|---|
| 127 | return seq_no_le_lt(seg->seq, conn->rcv_nxt, seg->seq + seg->len + 1);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /** Determine whether segment is fully acked */
|
|---|
| 131 | bool seq_no_segment_acked(tcp_conn_t *conn, tcp_segment_t *seg, uint32_t ack)
|
|---|
| 132 | {
|
|---|
| 133 | assert(seg->len > 0);
|
|---|
| 134 | return seq_no_lt_le(seg->seq, seg->seq + seg->len, ack);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /** Determine whether initial SYN is acked */
|
|---|
| 138 | bool seq_no_syn_acked(tcp_conn_t *conn)
|
|---|
| 139 | {
|
|---|
| 140 | return seq_no_lt_le(conn->iss, conn->snd_una, conn->snd_nxt);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /** Determine whether segment overlaps the receive window */
|
|---|
| 144 | bool seq_no_segment_acceptable(tcp_conn_t *conn, tcp_segment_t *seg)
|
|---|
| 145 | {
|
|---|
| 146 | bool b_in, e_in;
|
|---|
| 147 |
|
|---|
| 148 | b_in = seq_no_le_lt(conn->rcv_nxt, seg->seq, conn->rcv_nxt
|
|---|
| 149 | + conn->rcv_wnd);
|
|---|
| 150 |
|
|---|
| 151 | e_in = seq_no_le_lt(conn->rcv_nxt, seg->seq + seg->len - 1,
|
|---|
| 152 | conn->rcv_nxt + conn->rcv_wnd);
|
|---|
| 153 |
|
|---|
| 154 | if (seg->len == 0 && conn->rcv_wnd == 0) {
|
|---|
| 155 | return seg->seq == conn->rcv_nxt;
|
|---|
| 156 | } else if (seg->len == 0 && conn->rcv_wnd != 0) {
|
|---|
| 157 | return b_in;
|
|---|
| 158 | } else if (seg->len > 0 && conn->rcv_wnd == 0) {
|
|---|
| 159 | return false;
|
|---|
| 160 | } else {
|
|---|
| 161 | return b_in || e_in;
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /** Determine size that control bits occupy in sequence space. */
|
|---|
| 166 | uint32_t seq_no_control_len(tcp_control_t ctrl)
|
|---|
| 167 | {
|
|---|
| 168 | uint32_t len = 0;
|
|---|
| 169 |
|
|---|
| 170 | if ((ctrl & CTL_SYN) != 0)
|
|---|
| 171 | ++len;
|
|---|
| 172 |
|
|---|
| 173 | if ((ctrl & CTL_FIN) != 0)
|
|---|
| 174 | ++len;
|
|---|
| 175 |
|
|---|
| 176 | return len;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /** Calculate the amount of trim needed to fit segment in receive window. */
|
|---|
| 180 | void seq_no_seg_trim_calc(tcp_conn_t *conn, tcp_segment_t *seg,
|
|---|
| 181 | uint32_t *left, uint32_t *right)
|
|---|
| 182 | {
|
|---|
| 183 | assert(seq_no_segment_acceptable(conn, seg));
|
|---|
| 184 |
|
|---|
| 185 | /*
|
|---|
| 186 | * If RCV.NXT is between SEG.SEQ and RCV.NXT+RCV.WND, then
|
|---|
| 187 | * left trim amount is positive
|
|---|
| 188 | */
|
|---|
| 189 | if (seq_no_lt_le(seg->seq, conn->rcv_nxt,
|
|---|
| 190 | conn->rcv_nxt + conn->rcv_wnd)) {
|
|---|
| 191 | *left = conn->rcv_nxt - seg->seq;
|
|---|
| 192 | } else {
|
|---|
| 193 | *left = 0;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /*
|
|---|
| 197 | * If SEG.SEQ+SEG.LEN is between SEG.SEQ and RCV.NXT+RCV.WND,
|
|---|
| 198 | * then right trim is zero.
|
|---|
| 199 | */
|
|---|
| 200 | if (seq_no_lt_le(seg->seq - 1, seg->seq + seg->len,
|
|---|
| 201 | conn->rcv_nxt + conn->rcv_wnd)) {
|
|---|
| 202 | *right = 0;
|
|---|
| 203 | } else {
|
|---|
| 204 | *right = (seg->seq + seg->len) -
|
|---|
| 205 | (conn->rcv_nxt + conn->rcv_wnd);
|
|---|
| 206 | }
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /** Segment order comparison.
|
|---|
| 210 | *
|
|---|
| 211 | * Compare sequence order of two acceptable segments.
|
|---|
| 212 | *
|
|---|
| 213 | * @param conn Connection
|
|---|
| 214 | * @param sa Segment A
|
|---|
| 215 | * @param sb Segment B
|
|---|
| 216 | *
|
|---|
| 217 | * @return -1, 0, 1, resp. if A < B, A == B, A > B in terms
|
|---|
| 218 | * of sequence order of the beginning of the segment.
|
|---|
| 219 | */
|
|---|
| 220 | int seq_no_seg_cmp(tcp_conn_t *conn, tcp_segment_t *sa, tcp_segment_t *sb)
|
|---|
| 221 | {
|
|---|
| 222 | assert(seq_no_segment_acceptable(conn, sa));
|
|---|
| 223 | assert(seq_no_segment_acceptable(conn, sb));
|
|---|
| 224 |
|
|---|
| 225 | if (seq_no_lt_le(sa->seq, sb->seq, conn->rcv_nxt + conn->rcv_wnd))
|
|---|
| 226 | return -1;
|
|---|
| 227 |
|
|---|
| 228 | if (seq_no_lt_le(sb->seq, sa->seq, conn->rcv_nxt + conn->rcv_wnd))
|
|---|
| 229 | return +1;
|
|---|
| 230 |
|
|---|
| 231 | assert(sa->seq == sb->seq);
|
|---|
| 232 | return 0;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | /**
|
|---|
| 236 | * @}
|
|---|
| 237 | */
|
|---|