Skip to content

Commit

Permalink
complete udp checksum in xdp_patch_ports_func
Browse files Browse the repository at this point in the history
Signed-off-by: longguang.yue <bigclouds@163.com>
  • Loading branch information
bigclouds authored and tohojo committed Feb 22, 2024
1 parent 622945e commit b9253dd
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packet-solutions/xdp_prog_kern_02.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,34 @@ int xdp_patch_ports_func(struct xdp_md *ctx)
action = XDP_ABORTED;
goto out;
}

/*
* We need to update the packet checksum when modifying the header.
* RFC1071 contains an algorithm for in-place updating, which is what we use here
* since we're always just decrementing the port number. Another option would be
* to recompute the full checksum, like:
*
* struct udphdr udphdr_old;
* __u32 csum = udphdr->check;
* udphdr_old = *udphdr;
* udphdr->dest = bpf_htons(bpf_ntohs(udphdr->dest) - 1);
* csum = bpf_csum_diff((__be32 *)&udphdr_old, 4, (__be32 *)udphdr, 4, ~csum);
* udphdr->check = csum_fold_helper(csum);
*/

udphdr->dest = bpf_htons(bpf_ntohs(udphdr->dest) - 1);
udphdr->check += bpf_htons(1);
if (!udphdr->check)
udphdr->check += bpf_htons(1);
} else if (ip_type == IPPROTO_TCP) {
if (parse_tcphdr(&nh, data_end, &tcphdr) < 0) {
action = XDP_ABORTED;
goto out;
}
tcphdr->dest = bpf_htons(bpf_ntohs(tcphdr->dest) - 1);
tcphdr->check += bpf_htons(1);
if (!tcphdr->check)
tcphdr->check += bpf_htons(1);
}

out:
Expand Down

0 comments on commit b9253dd

Please sign in to comment.