Merged
Conversation
RFC768 states:
If the computed checksum is zero, it is transmitted as all ones (the
equivalent in one's complement arithmetic). An all zero transmitted
checksum value means that the transmitter generated no checksum.
Updated the serialization logic to correctly handle this. It isn't a big
problem for IPv4 where checksums are optional, but affects IPv6 which
has mandatory UDP checksums.
This commit unifies the checksum computation for different layers and splits it into a computation and folding step to make it more flexible.
This commit adds functions to validate checksums of individual layers or all layers of a packet. It also implements this functionality for all layers that use the internet checksum and already have some kind of checksum calculation implemented.
Contributor
|
looks good. need to do a bit more digging. originally wanted to suggest that we move the checksum to its own type rather than |
mosajjal
approved these changes
Sep 10, 2023
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Thank you for forking
google/gopacketand keeping it alive!Originally submitted here: google/gopacket#1057
With a commit from: google/gopacket#883
This PR adds a LayerWithChecksum interface for layers that have a checksum with a function to verify it and implements this function for all layers that use the internet checksum from RFC1071 and where the checksum computation was implemented (for SerializeTo).
It also adds a way to verify the checksums of all layers of a packet and retrieve a list of all found mismatches.
To implement this, I refactored the internet checksum calculation code, deduplicated it and made it a little bit more modular. There are now two functions: ComputeChecksum and FoldChecksum.
These split-up functions make it possible to verify checksums without having to zero-out the checksum that's already set in the packet. Instead, the checksum of the whole packet (including the existing checksum) is computed and then the existing checksum is subtracted, before it is folded. This way we don't need to copy the packet buffer and zero-out the checksum field, just to verify it.
The refactoring and checksum-verification are split up into two commits to make this easier to review.
For the UDP layer, there is a special case, because the UDP checksum is optional. That's why this PR is based on google/gopacket#883 and extends it to make the checksum verification succeed, if the existing UDP checksum was unset.