Skip to content

Commit

Permalink
tcp: fix TCP checksums
Browse files Browse the repository at this point in the history
1. Include the options field and padding when converting a TCP packet from
record to binary.

2. Include the length of the TCP payload in the checksum

Thanks to Garry Hodgson for reporting the bug!
  • Loading branch information
msantos committed Jul 7, 2013
1 parent 4d0b1df commit e1dd786
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/pkt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,17 @@ tcp(#tcp{
ackno = AckNo,
off = Off, cwr = CWR, ece = ECE, urg = URG, ack = ACK,
psh = PSH, rst = RST, syn = SYN, fin = FIN, win = Win,
sum = Sum, urp = Urp
sum = Sum, urp = Urp,
opt = Opt
}) ->
Pad = ((Off - 5) * 4 - byte_size(Opt)) * 8,
<<SPort:16, DPort:16,
SeqNo:32,
AckNo:32,
Off:4, 0:4, CWR:1, ECE:1, URG:1, ACK:1,
PSH:1, RST:1, SYN:1, FIN:1, Win:16,
Sum:16, Urp:16>>.
Sum:16, Urp:16,
Opt/binary, 0:Pad>>.

options(Offset, Payload) ->
N = (Offset-5)*4,
Expand Down Expand Up @@ -583,12 +586,12 @@ checksum([#ipv4{
} = TCPhdr,
Payload
]) ->
Len = Off * 4,
TCP = tcp(TCPhdr#tcp{sum = 0}),
Len = Off * 4 + byte_size(Payload),
Pad = case Len rem 2 of
0 -> 0;
1 -> 8
end,
TCP = tcp(TCPhdr#tcp{sum = 0}),
checksum(
<<SA1,SA2,SA3,SA4,
DA1,DA2,DA3,DA4,
Expand Down

0 comments on commit e1dd786

Please sign in to comment.