Skip to content

Commit

Permalink
bpf: fix checksum in edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
hack3ric committed Oct 1, 2024
1 parent 90ce2f3 commit 99b9723
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bpf/egress.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static inline int mangle_data(struct __sk_buff* skb, __u16 offset, __be32* csum_
try_shot(bpf_skb_store_bytes(skb, skb->len - copy_len, buf + 1, copy_len, 0));

// Fix checksum when moved bytes does not align with u16 boundaries
if (copy_len == reserve_len && data_len % 2 != 0) {
if ((data_len <= copy_len && reserve_len % 2 != 0) || data_len % 2 != 0) {
__u32 l = min(round_to_mul(copy_len, 4), MAX_RESERVE_LEN);
*csum_diff = bpf_csum_diff((__be32*)(buf + 1), l, (__be32*)buf, l + 4, *csum_diff);
}
Expand Down
2 changes: 1 addition & 1 deletion bpf/ingress.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static inline int restore_data(struct xdp_md* xdp, __u16 offset, __u32 buf_len,
try_drop(bpf_xdp_store_bytes(xdp, offset - TCP_UDP_HEADER_DIFF, buf + 1, copy_len));

// Fix checksum when moved bytes does not align with u16 boundaries
if (copy_len == reserve_len && data_len % 2 != 0) {
if ((data_len <= copy_len && copy_len % 2 != 0) || data_len % 2 != 0) {
__u32 l = min(round_to_mul(copy_len, 4), MAX_RESERVE_LEN);
*csum_diff = bpf_csum_diff((__be32*)buf, l + 4, (__be32*)(buf + 1), l, *csum_diff);
}
Expand Down

0 comments on commit 99b9723

Please sign in to comment.