level-ip icon indicating copy to clipboard operation
level-ip copied to clipboard

Sequence number comparisons need to be done "modulo"

Open ambrop72 opened this issue 7 years ago • 0 comments

Sequence numbers must not be compared for inequality "naively", since they are designed to wrap around. For example there is an issue here when checking if an incoming segment has a valid sequence number: https://github.com/saminiir/level-ip/blob/c1950ea0e0f9feceb5602432f1751b8ce71c4952/src/tcp_input.c#L106

Consider the case tcb->rcv_nxt=4294967290, tcb->rcv_wnd=20, th->seq=5. This is an acceptable segment (it is 11 more than tcb->rcv_nxt, so it is within the receive window), but your code will decide it is not acceptable.

One way to fix this specific part of code is like this:

uint32_t seq_relative_to_rcv_nxt = (uint32_t)(th->seq - tcb->rcv_nxt);
if (seq_relative_to_rcv_nxt > tcb->rcv_wnd) {
    // invalid segment
}

Note that the conversion of the result of the subtraction to uint32_t is imperative to ensure modulo reduction.

ambrop72 avatar Nov 16 '17 20:11 ambrop72