Skip to content

Commit

Permalink
Fix misleading use of bpf_ntohl
Browse files Browse the repository at this point in the history
`local_port` in `struct bpf_sock_ops` and `struct sk_msg_md` is stored in
host byte order. The use of `bpf_ntohl` is kind of misleading.

Signed-off-by: Wang Zhen <lazybetrayer@gmail.com>
  • Loading branch information
lazybetrayer authored and joestringer committed Mar 22, 2023
1 parent 29b29fe commit 522004b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bpf/sockops/bpf_redir.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static __always_inline void sk_msg_extract4_key(const struct sk_msg_md *msg,
key->sip4 = msg->local_ip4;
key->family = ENDPOINT_KEY_IPV4;

key->sport = (bpf_ntohl(msg->local_port) >> 16);
key->sport = (bpf_htonl(msg->local_port) >> 16);
/* clang-7.1 or higher seems to think it can do a 16-bit read here
* which unfortunately most kernels (as of October 2019) do not
* support, which leads to verifier failures. Insert a READ_ONCE
Expand Down
2 changes: 1 addition & 1 deletion bpf/sockops/bpf_sockops.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static __always_inline void sk_extract4_key(const struct bpf_sock_ops *ops,
key->sip4 = ops->local_ip4;
key->family = ENDPOINT_KEY_IPV4;

key->sport = (bpf_ntohl(ops->local_port) >> 16);
key->sport = (bpf_htonl(ops->local_port) >> 16);
/* clang-7.1 or higher seems to think it can do a 16-bit read here
* which unfortunately most kernels (as of October 2019) do not
* support, which leads to verifier failures. Insert a READ_ONCE
Expand Down

0 comments on commit 522004b

Please sign in to comment.