I am trying to redirect the traffic between NIC and WIFI. I am trying to forwards packets from eth0, to send even packets through wlan0 and odd packets through wlan1.
I have not been able to successfully redirect packets from one interface to another unless those interfaces are virtual (such as the ones created in the xdp-tutorial).
Is there a simple example anywhere of redirecting ingress packets from eth0 with MAC 28:f1:f1:f1:f1:f1 to wlan0 with MAC e4:f1:f1:f1:f1:f1? (example MACs) So that if I connect a second computer through the ethernet port (assuming correct routing) and ping 8.8.8.8 it sends the packets through wlan0?
I would appreciate any help in this regard.
EDIT:
The code I am using is the one from xdp-tutorial
Step by step setup:
# Mount map directory
sudo mount -t bpf bpf /sys/fs/bpf/
# Load the programs
sudo ./xdp_loader -d eth0 -S --filename xdp_prog_kern_03.o --progsec xdp_redirect_map -F
sudo ./xdp_loader -d wlan0 -S --filename xdp_prog_kern_03.o --progsec xdp_pass -F
# Set up redirect map
sudo ./xdp_prog_user -d eth0 -r wlan0 --src-mac 28:f1:f1:f1:f1:f1 --dest-mac e4:f1:f1:f1:f1:f1
The last command outputs:
map dir: /sys/fs/bpf/eth0
redirect from ifnum=5 to ifnum=3
forward: 28:f1:f1:f1:f1:f1 -> e4:f1:f1:f1:f1:f1
The relevant BPF code is
struct bpf_map_def SEC("maps") tx_port = {
.type = BPF_MAP_TYPE_DEVMAP,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = 256,
};
struct bpf_map_def SEC("maps") redirect_params = {
.type = BPF_MAP_TYPE_HASH,
.key_size = ETH_ALEN,
.value_size = ETH_ALEN,
.max_entries = 1,
};
SEC("xdp_redirect_map")
int xdp_redirect_map_func(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct hdr_cursor nh;
struct ethhdr *eth;
int eth_type;
int action = XDP_PASS;
unsigned char *dst;
/* These keep track of the next header type and iterator pointer */
nh.pos = data;
/* Parse Ethernet and IP/IPv6 headers */
eth_type = parse_ethhdr(&nh, data_end, ð);
if (eth_type == -1)
return xdp_stats_record_action(ctx, XDP_DROP);
/* Do we know where to redirect this packet? */
dst = bpf_map_lookup_elem(&redirect_params, eth->h_source);
if (!dst)
goto out;
/* Set a proper destination address */
memcpy(eth->h_dest, dst, ETH_ALEN);
action = bpf_redirect_map(&tx_port, 0, 0);
out:
return xdp_stats_record_action(ctx, action);
}
SEC("xdp_pass")
int xdp_pass_func(struct xdp_md *ctx)
{
return xdp_stats_record_action(ctx, XDP_PASS);
}
And the content of the maps is:
# Output of redirect_params
sudo bpftool map dump id 33
key: 28 f1 f1 f1 f1 f1 value: e4 f1 f1 f1 f1 f1
Found 1 element
# Output of tx_port
key: 00 00 00 00 value: 03 00 00 00
key:
01 00 00 00
value:
No such file or directory
# And then the No such file or directory repeats with every key