[En-Nut-Discussion] [PATCH] Implement TCP sequence number wraparound

Marti Raudsepp marti at voicecom.ee
Wed Mar 24 15:22:20 CET 2010


Hello list,

It seems that neither Ethernut 4.8 nor 4.9 currently attempt to handle
TCP sequence number wraparound. This means that each TCP connection is
limited to transmitting/receiving 4 GB of data in the best case.
Technically it's in violation of the TCP spec.

Transmitting at the maximum rate of our application (1.10 MB/s both
ways by our measurements) means that we can hit this limit in just 1
hour.

Further, since the initial TX sequence number is initialized from
NutGetMillis(), the maximum TCP session lifetime approaches 0 as
uptime approaches 49.71 days. Nut/OS has no control at all over RX
sequence numbers which are assigned by the other endpoint.

So I went through every sequence number comparison in the TCP code and
changed it. My initial approach was to change all sequence number
values to signed int32_t and change comparisons of style "a > b" into
"a - b > 0", but I figured it might be confusing to people, because
sequence numbers are usually represented as positive numbers and the
two comparisons would seem equivalent mathematically.

In the end I settled for adding two macros to tcputil.h:

#define SeqIsBetween(x, low, high) \
  ((uint32_t)(x - low) <= (uint32_t)(high - low))

#define SeqIsAfter(x, low) \
  ((int32_t)(x - low) > 0)

SeqIsBetween is my replacement for the IsInLimits function; the macro
is very simple, so a function seemed like overkill.

The patch applies cleanly to both 4.8.6 and 4.9.8, but I have only
tested with 4.8.6 on AT91SAM7X. So far I have tested wraparound in the
common case (no packet loss). I feel confident that I haven't broken
anything that used to work.

However, testing wraparound around packet loss/reordering edge cases
is pretty complicated, and Nut/OS's short TCP window doesn't help.
I'll see what I can do with Linux network emulator.

----
Hereby I submit 3 patches for review:

01_tcp_seqno_wraparound.patch:
Implement handling of TCP sequence number wraparound. This is the
actual patch that I described above.

02_remove_bogus_todo.patch:
This just removes a bogus TODO item that has already been implemented.

03_tcp_ntohl_to_htonl.patch:
I found a few places in the TCP code where the "htonl" macro was used
insteasd of "ntohl". Since these operations are intechangeable, it's a
coding style fix only.


Regards,
Marti Raudsepp
voicecom.ee


More information about the En-Nut-Discussion mailing list