Created
March 17, 2015 15:01
-
-
Save lukego/7edf49dd7f28275a3273 to your computer and use it in GitHub Desktop.
L3/L4 checksum verification pseudo code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Return three values: | |
-- valid - is the header checksum valid (always true for IPv6) | |
-- offset - where the inner checksum starts, or nil if no more to checksum | |
-- initial - initial value for inner checksum ("pseudo header checksum") | |
function check_header (p) | |
if is_ipv4(p) then | |
-- check header checksum; | |
-- find start of tcp/udp header; | |
-- calculate pseudo header checksum | |
elseif is_ipv6(p) then | |
-- (no header checksum to check) | |
-- find start of tcp/udp header; | |
-- calculate pseudo header checksum | |
end | |
end | |
-- Return true if the header and data checksums of p are known to be correct. | |
function check_packet (p) | |
local valid, offset, pseudoheader_csum = check_header(p) | |
if not valid then return false end -- header was invalid | |
if not offset then return true end -- nothing more to check | |
return ipsum(p.data + offset, p.length - offset, pseudoheader_csum) == 0 | |
-- ^^^ if the checksum is okay then ipsum() will return 0. | |
-- That is because the packet includes its own checksum, in the | |
-- checksum field, and if we calculate the correct checksum then | |
-- these will "cancel out". | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment