Tc bpf packet forward to other device by updating the Checksum

1.2k views Asked by At

I want to forward the UDP/TCP packet to other devices connected to the same router. I just write the code which forwards the packet to other interfaces by checking the packet type and its payload. This is working fine, but when I try to forward the same packet to other devices I don't receive the packet on other devices. I updated the checksum by using helper functions and redirect to other devices is not working for me. Is TC_ACT_REDIRECT similar to XDP_TX?

Here is the piece of code (If the value of c is 1 it updates the destination address and forwards the packet to destination):

if (c == 1) {
    int ipaddr = htonl(3232260738);  // Dest: 192.168.98.130

    sum = bpf_csum_diff((void *)&old1_daddr, 4, (void *)&ipaddr, 4, 0);

    bpf_skb_store_bytes(skb, ETH_HLEN + offsetof(struct iphdr, daddr),
                        (void *)&ipaddr, 4, 0);
    bpf_l3_csum_replace(skb, IP_CSUM_OFFSET, 0, sum, 0);
    bpf_l4_csum_replace(skb, IP_CSUM_OFFSET1, 0, sum, BPF_F_PSEUDO_HDR);

    bpf_clone_redirect(skb, skb->ifindex, 0 );

    return TC_ACT_REDIRECT;
}

Here is the ingress tc command

sudo tc filter add dev ens33 ingress bpf da obj tcbpf1_kern.o sec classifier

with an above chunk of code, I can redirect the packet to virtual interface but not to the updated destination.

1

There are 1 answers

0
warrior On

if I am not wrong, you're missing the MAC address of your destination device. You need to update the MAC address as well. You are receiving the packet on a virtual machine because it has no mac address. Please check the mac address and your problem will be solved.