how to forward packet between NIC and WIFI using XDP

Viewed 1798

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, &eth);                                                    
        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
1 Answers

As of date, Linux Kernel 5.5 houses bpf_prog_run_xdp function call for TUN and NIC drivers.

drivers/net/tun.c
drivers/net/ethernet
/broadcom/bnxt/bnxt_xdp.c, line 144
cavium/thunder/nicvf_main.c, line 559
freescale/dpaa2/dpaa2-eth.c, line 305
intel/i40e/i40e_txrx.c, line 2212
intel/ice/ice_txrx.c, line 442
ixgbe/ixgbe_main.c, line 2213
marvell/mvneta.c, line 2099
mellanox/mlx4/en_rx.c, line 785
mellanox/mlx5/core/en/xdp.c, line 141
netronome/nfp/nfp_net_common.c, line 1917
qlogic/qede/qede_fp.c, line 1075
sfc/rx.c, line 700
socionext/netsec.c, line 890
ti/cpsw_priv.c, line 1337

hence as discussed in comments there looks like no XDP calls to wifi driver natively.

Note: the Linux kernel version, custom driver, or results of ip or 'bpftool` are not shared to debug. Also I am unable to find XDP code change for allowing 802MAC headers too.

Related