我正在尝试使用ebpf
. 我想将传入的数据包重定向到不同的目的地。虽然我已经使用bpf_clone_redirect帮助函数将数据包重定向到真实/虚拟接口并且它工作正常。现在我想一次将数据包重定向到多个接口。这是我编写的用于重定向 1 数据包的代码。
eth->h_dest[0] = 0xb8;
eth->h_dest[1] = 0x27;
eth->h_dest[2] = 0xeb;
eth->h_dest[3] = 0x42;
eth->h_dest[4] = 0x77;
eth->h_dest[5] = 0x56;
new_port = ntohs( 5302) ;
udp->dest= new_port; //change the destination port
ipaddr = htonl(0xc0a8006c); // Dest: 192.168.98.108
iph->daddr = ipaddr; // Change the destination address
// Calculate the sum_diff for destination address and Port number
sum = bpf_csum_diff((void *)&old1_daddr , 4,(void *)&ipaddr,4 , 0);
sum1 = bpf_csum_diff((void *)&old_port , 4,(void *)&new_port,4 , 0);
ipaddr2 = htonl(0xc0a8006e); // Dest: 192.168.98.110
iph->daddr = ipaddr2; // Change the destination address
//L3 and L4 checksum Update
bpf_l3_csum_replace(skb, IP_CSUM_OFF, 0 , sum,0 );
bpf_l4_csum_replace(skb, UDP_CSUM_OFF , 0, sum +sum1, IS_PSEUDO | sizeof(ipaddr) );
bpf_clone_redirect(skb, skb->ifindex, 0 ); // Packet forward to the destination
//Repeat the same functionality
// Calculate the sum_diff for destination address and Port number
sum = bpf_csum_diff((void *)&old1_daddr , 4,(void *)&ipaddr2,4 , 0);
//L3 and L4 checksum Update
bpf_l3_csum_replace(skb, IP_CSUM_OFF, 0 , sum,0 );
bpf_l4_csum_replace(skb, UDP_CSUM_OFF , 0, sum, IS_PSEUDO | sizeof(ipaddr2) );
bpf_clone_redirect(skb, skb->ifindex, 0 ); // Packet forward to the destination
return TC_ACT_OK;
我面临更新数据包MAC地址的问题。使用上面的代码,我可以更改目标地址和端口号,但无法更新第二个克隆中的 mac 地址。