Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IP fragmentation checks for big-endian CPUs #2919

Merged
merged 1 commit into from
Oct 4, 2024
Merged

Fix IP fragmentation checks for big-endian CPUs #2919

merged 1 commit into from
Oct 4, 2024

Conversation

cpq
Copy link
Member

@cpq cpq commented Oct 1, 2024

No description provided.

ip->ver = 0x45; // Version 4, header length 5 words
ip->frag = 0x40; // Don't fragment
ip->ver = 0x45; // Version 4, header length 5 words
ip->frag = mg_htons(0x40); // Don't fragment
Copy link
Collaborator

@robertc2000 robertc2000 Oct 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be mg_htons(0x4000) instead.

We want to set the first byte at the &ip->frag offset.

Thus, on a little endian mg_htons(0x4000) results in 0x0040, which means the first byte will be written with value 0x40.

On a big endian system instead, mg_htons(0x4000) would still be 0x4000, which means the first byte will be written with 0x40.

In both cases, we set the 'don't fragment' bit correctly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, fixed

@@ -784,7 +784,8 @@ static void rx_tcp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
}

static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) {
if (pkt->ip->frag & IP_MORE_FRAGS_MSK || pkt->ip->frag & IP_FRAG_OFFSET_MSK) {
uint16_t frag = mg_ntohs(pkt->ip->frag);
if (frag & IP_MORE_FRAGS_MSK || frag & IP_FRAG_OFFSET_MSK) {
Copy link
Collaborator

@robertc2000 robertc2000 Oct 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The masks should also be in big endian order.

#define IP_FRAG_OFFSET_MSK 0x1FFF
#define IP_MORE_FRAGS_MSK 0x2000

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@scaprile scaprile merged commit 4be8aa0 into master Oct 4, 2024
73 of 76 checks passed
@scaprile scaprile deleted the frag branch October 4, 2024 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants