Skip to content

Commit

Permalink
TCP/UDP checksum for port translation
Browse files Browse the repository at this point in the history
  • Loading branch information
phudtran committed Sep 23, 2020
1 parent 6ae482a commit 175dbbe
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/xdp/trn_kern.h
Original file line number Diff line number Diff line change
Expand Up @@ -447,20 +447,17 @@ static inline void trn_set_src_dst_port(struct transit_packet *pkt, __u16 sport,
pkt->inner_tcp->source = sport;
pkt->inner_tcp->dest = dport;
// Compute csum
bpf_debug("1 Modified Inner TCP Ports src: %u, dest: %u, csum: 0x%x\n",
bpf_ntohs(pkt->inner_tcp->source),
bpf_ntohs(pkt->inner_tcp->dest), pkt->inner_tcp->check);
if (old_dport != dport) {
__u64 cs = pkt->inner_tcp->check;
trn_update_l4_csum_port(&cs, old_dport, dport);
pkt->inner_tcp->check = (__u16)cs;
pkt->inner_tcp->check = cs - bpf_htons(256);
}
if (old_sport != sport) {
__u64 cs = pkt->inner_tcp->check;
trn_update_l4_csum_port(&cs, old_sport, sport);
pkt->inner_tcp->check = (__u16)cs;
pkt->inner_tcp->check = cs - bpf_htons(256);
}
bpf_debug("2 Modified Inner TCP Ports src: %u, dest: %u, csum: 0x%x\n",
bpf_debug("Modified Inner TCP Ports src: %u, dest: %u, csum: 0x%x\n",
bpf_ntohs(pkt->inner_tcp->source),
bpf_ntohs(pkt->inner_tcp->dest), pkt->inner_tcp->check);
} else if (pkt->inner_ipv4_tuple.protocol == IPPROTO_UDP) {
Expand All @@ -475,17 +472,16 @@ static inline void trn_set_src_dst_port(struct transit_packet *pkt, __u16 sport,
if (old_dport != dport) {
__u64 cs = pkt->inner_udp->check;
trn_update_l4_csum_port(&cs, old_dport, dport);
pkt->inner_udp->check = (__u16)cs;
pkt->inner_udp->check = cs - bpf_htons(256);
}
if (old_sport != sport) {
__u64 cs = pkt->inner_udp->check;
trn_update_l4_csum_port(&cs, old_sport, sport);
pkt->inner_udp->check = (__u16)cs;
pkt->inner_udp->check = cs - bpf_htons(256);
}

bpf_debug("Modified Inner UDP Ports src: %u, dest: %u\n",
bpf_debug("Modified Inner UDP Ports src: %u, dest: %u, csum: 0x%x\n",
bpf_ntohs(pkt->inner_udp->source),
bpf_ntohs(pkt->inner_udp->dest));
bpf_ntohs(pkt->inner_udp->dest), pkt->inner_udp->check);
} else {
return;
}
Expand Down

0 comments on commit 175dbbe

Please sign in to comment.