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

Fixing L4 offset for IP packets with Options field #41

Closed
wants to merge 26 commits into from
Closed

Fixing L4 offset for IP packets with Options field #41

wants to merge 26 commits into from

Conversation

evershalik
Copy link

@evershalik evershalik commented Apr 1, 2023

This PR fixed #30

Problem Statement:
The current implementation of the codebase does not handle IP and TCP packets with a header length greater than 20 bytes correctly. This issue is particularly problematic for packets containing options, which increase the header length beyond 20 bytes.

Approach:
To address this issue, we have taken the following approaches:

Firstly, I have modified the code to use the l4_offset variable(IP Header Length) instead of sizeof(iphdr struct) when adding to the iph pointer. This ensures that the code correctly traverses to the end of the IP header( or the start of the transport layer header), even in cases where the header length is greater than 20 bytes.

Secondly, I have modified the code to use the data_offset variable(TCP Header Length) instead of sizeof(tcphdr struct) when adding to the tcph pointer. This ensures that the code correctly traverses to the end of the TCP header (or start of the payload), even in cases where the header length is greater than 20 bytes.

By implementing these changes, I can ensure that IP and TCP packets with options are handled correctly, which improves the reliability and stability of the system.

struct iphdr *iph = (struct iphdr *)(data + sizeof(struct ethhdr));
if (iph + 1 > data_end)
__u8 l4_offset = iph->ihl * 4; // ip header length
Copy link

Choose a reason for hiding this comment

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

Endianness is handled in the definition of 'struct iphdr'. However please have a test-case to verify that the value for IHL is read correctly from the packet.

Copy link
Collaborator

@dthaler dthaler Apr 19, 2023

Choose a reason for hiding this comment

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

What sort of check did you have in mind? I am not sure an additional one is necessary beyond what line 281 checks.

Copy link

@aka320 aka320 left a comment

Choose a reason for hiding this comment

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

  1. The comments given are applicable for all the eBPF applications. Please fix in all files even though its not explicitly commented in that specific file.

  2. Please include the tests results (different values of IHL including negative cases like invalid IHL value)

  3. Add a Commit log message with a short description of RootCause (pointing to the source Issue) & Details of the Fix

@evershalik
Copy link
Author

evershalik commented Apr 5, 2023

  1. I have gone ahead and fixed the issue, as you suggested.
  2. I have added a commit log message above that provides a short description of the root cause of the issue (as pointed out in the source issue), along with details of the fix that has been implemented. This should help with tracking the changes and understanding the reasons for the changes made.
  3. Regarding the test results, it may take some time to complete these tests and ensure that everything is working as expected.
    Thank you for your feedback.

@evershalik evershalik requested a review from aka320 April 6, 2023 06:25
@aka320
Copy link

aka320 commented Apr 12, 2023

  1. You can remove the diagrams in the Description. Text is enough.
  2. Next time when you address review comments, do not overwrite the previous commit. It doesnt give option to review the additional changes made for the review comments.

@evershalik
Copy link
Author

Thank you for your feedback.
I have removed the diagrams in the Description.
And I apologize for overwriting the previous commit. I will take care of this in the future.

@aka320
Copy link

aka320 commented Apr 12, 2023

Changes look good. Please update once testing is done.

Copy link
Collaborator

@dthaler dthaler left a comment

Choose a reason for hiding this comment

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

This code isn't safe because it doesn't check whether the packet is IPv4 before trying to interpret the IPv4 header.


/* Check if its valid ip packet */
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/* Check if its valid ip packet */
/* Check if it's a valid IPv4 packet */


/* Check if its valid ip packet */
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/* Check if its valid ip packet */
/* Check if it's a valid IPv4 packet */

return XDP_PASS;

/* Ignore other than TCP packets */
if (iph->protocol != IPPROTO_TCP)
return XDP_PASS;

struct tcphdr *tcph = (struct tcphdr *)((unsigned char *)iph + l4_offset);
__u16 data_offset = tcph->doff * 4; // tcp header length

/* Check if its valid tcp packet */
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/* Check if its valid tcp packet */
/* Check if it's a valid TCP packet */

@evershalik evershalik requested a review from dthaler April 20, 2023 09:04
make defconfig

- name: Clone kernel function repository
uses: actions/checkout@v2
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
uses: actions/checkout@v2
uses: actions/checkout@b0e28b5ac45a892f91e7d036f8200cf5ed489415

use hash instead of version. I see some programs use version and others use hash, we should be consistent at always using hash.

@@ -0,0 +1,104 @@
# Copyright Contributors to the L3AF Project.
# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
Copy link
Collaborator

Choose a reason for hiding this comment

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

is there a reason the codeql file needs to have GPL in the SPDX?


# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@04df1262e6247151b5ac09cd2c303ac36ad3f62b

make defconfig

- name: Clone eBPF-Package-Repository
uses: actions/checkout@v2
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
uses: actions/checkout@v2
uses: actions/checkout@b0e28b5ac45a892f91e7d036f8200cf5ed489415

buildscript.sh Outdated
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
# Copyright Contributors to the L3AF Project.
# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any reason GPL is needed in the build script SPDX for all programs?

}

if (allow_all_src_ports == true && allow_all_dst_ports == true) {
return bpf_clone_redirect(skb, *ifindex, 0); // __bpf_rx_skb
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the comment on the end for? Avoid putting Linux-only things in comments where possible.

__u8 data[4];
};

/* TODO: Describe what this PIN_GLOBAL_NS value 2 means???
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this TODO be done before approving the PR?

int *ifany;
int iface_key = 1;

/* Lookup what ifindex to redirect packets to */
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/* Lookup what ifindex to redirect packets to */
/* Look up what ifindex to redirect packets to */

Comment on lines 167 to 172
l7_off = l4_off + sizeof(struct tcphdr); // L7 (e.g. HTTP) header offset
} else if (iph->protocol == IPPROTO_UDP) {
l7_off = l4_off + sizeof(struct udphdr); // L7 (e.g. HTTP) header offset
} else if (iph->protocol == IPPROTO_ICMP) {
l7_off =
l4_off + sizeof(struct icmphdr); // L7 (e.g. HTTP) header offset
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
l7_off = l4_off + sizeof(struct tcphdr); // L7 (e.g. HTTP) header offset
} else if (iph->protocol == IPPROTO_UDP) {
l7_off = l4_off + sizeof(struct udphdr); // L7 (e.g. HTTP) header offset
} else if (iph->protocol == IPPROTO_ICMP) {
l7_off =
l4_off + sizeof(struct icmphdr); // L7 (e.g. HTTP) header offset
l7_off = l4_off + sizeof(struct tcphdr); // L7 (e.g., HTTP) header offset
} else if (iph->protocol == IPPROTO_UDP) {
l7_off = l4_off + sizeof(struct udphdr); // L7 (e.g., HTTP) header offset
} else if (iph->protocol == IPPROTO_ICMP) {
l7_off =
l4_off + sizeof(struct icmphdr); // L7 (e.g., HTTP) header offset

Comment on lines 196 to 197
// 1th bit True: allow all/any source port
// 2th bit True: allow all/any destination port
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// 1th bit True: allow all/any source port
// 2th bit True: allow all/any destination port
// 1st bit True: allow all/any source port
// 2nd bit True: allow all/any destination port

@dthaler dthaler dismissed their stale review April 20, 2023 16:59

Updated

@evershalik
Copy link
Author

evershalik commented Apr 21, 2023

Sorry for this. I have synced the forked repository by mistake due to which commits made by other people are also showing in this PR. What should I do now to revert this?

sanfern and others added 9 commits June 18, 2023 13:02
Signed-off-by: Santhosh Fernandes <[email protected]>
Signed-off-by: evershalik <[email protected]>
He's moved on to other things.

Signed-off-by: evershalik <[email protected]>
Signed-off-by: Santhosh Fernandes <[email protected]>
Signed-off-by: evershalik <[email protected]>
Signed-off-by: Santhosh Fernandes <[email protected]>
Signed-off-by: evershalik <[email protected]>
Signed-off-by: Dave Thaler <[email protected]>
Signed-off-by: evershalik <[email protected]>
Signed-off-by: Dave Thaler <[email protected]>
Signed-off-by: evershalik <[email protected]>
Signed-off-by: Dave Thaler <[email protected]>
Signed-off-by: evershalik <[email protected]>
dthaler and others added 17 commits June 18, 2023 13:02
Signed-off-by: Dave Thaler <[email protected]>
Signed-off-by: evershalik <[email protected]>
Signed-off-by: Dave Thaler <[email protected]>
Signed-off-by: evershalik <[email protected]>
Signed-off-by: Dave Thaler <[email protected]>
Signed-off-by: evershalik <[email protected]>
Signed-off-by: evershalik <[email protected]>
* Adding Traffic Mirroring

Signed-off-by: Jay Sheth <[email protected]>

* Fixing Readme

Signed-off-by: Jay Sheth <[email protected]>

* Removing unnecessary comments and code

Signed-off-by: Jay Sheth <[email protected]>

* Removing unnecessary files

Signed-off-by: Jay Sheth <[email protected]>

* Minor Fixes

Signed-off-by: [email protected] <[email protected]>

---------

Signed-off-by: Jay Sheth <[email protected]>
Signed-off-by: [email protected] <[email protected]>
Signed-off-by: evershalik <[email protected]>
Signed-off-by: sferna1 <[email protected]>
Signed-off-by: evershalik <[email protected]>
Signed-off-by: evershalik <[email protected]>
Signed-off-by: Santhosh Fernandes <[email protected]>
Signed-off-by: sanfern <[email protected]>

Signed-off-by: SHANKAR <[email protected]>
Signed-off-by: evershalik <[email protected]>
Signed-off-by: Santhosh Fernandes <[email protected]>
Signed-off-by: sferna1 <[email protected]>

Signed-off-by: SHANKAR <[email protected]>
Signed-off-by: evershalik <[email protected]>
Signed-off-by: sanfern <[email protected]>

Signed-off-by: SHANKAR <[email protected]>
Signed-off-by: evershalik <[email protected]>
Signed-off-by: Santhosh Fernandes <[email protected]>
Signed-off-by: sanfern <[email protected]>

Signed-off-by: SHANKAR <[email protected]>
Signed-off-by: evershalik <[email protected]>
Signed-off-by: SHANKAR <[email protected]>

Signed-off-by: evershalik <[email protected]>
Signed-off-by: SHANKAR <[email protected]>
Signed-off-by: evershalik <[email protected]>
Signed-off-by: evershalik <[email protected]>
@evershalik evershalik closed this Jun 18, 2023
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.

Incorrect L4 offset for IP packets with Options field
6 participants