| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include <stdbool.h> |
| 8 |
|
| 9 |
#include <linux/bpf.h> |
| 10 |
#include <linux/types.h> |
| 11 |
|
| 12 |
#include <bpf/bpf_endian.h> |
| 13 |
#include <bpf/bpf_helpers.h> |
| 14 |
|
| 15 |
#include <xdp/xdp_helpers.h> |
| 16 |
|
| 17 |
#include "net_def.h" |
| 18 |
|
| 19 |
struct { |
| 20 |
__uint(type, BPF_MAP_TYPE_DEVMAP); |
| 21 |
__uint(key_size, sizeof(int)); |
| 22 |
__uint(value_size, sizeof(int)); |
| 23 |
__uint(max_entries, 2); |
| 24 |
} veth_map SEC(".maps"); |
| 25 |
|
| 26 |
struct { |
| 27 |
__uint(priority, 10); |
| 28 |
__uint(XDP_PASS, 1); |
| 29 |
} XDP_RUN_CONFIG(xdp_sock_prog); |
| 30 |
|
| 31 |
SEC("xdp_sock") |
| 32 |
int xdp_sock_prog(struct xdp_md *ctx) |
| 33 |
{ |
| 34 |
void *data_end = (void *)(long)ctx->data_end; |
| 35 |
void *data = (void *)(long)ctx->data; |
| 36 |
struct vlan_ethernet_header *veth; |
| 37 |
bool redirect = false; |
| 38 |
void *p = data; |
| 39 |
int if_key; |
| 40 |
|
| 41 |
veth = p; |
| 42 |
if ((void *)(veth + 1) > data_end) |
| 43 |
return XDP_PASS; |
| 44 |
p += sizeof(*veth); |
| 45 |
|
| 46 |
|
| 47 |
if (veth->vlan_proto != bpf_htons(ETH_P_8021Q)) |
| 48 |
return XDP_PASS; |
| 49 |
|
| 50 |
|
| 51 |
if (veth->vlan_encapsulated_proto != bpf_htons(ETH_P_PROFINET_RT)) |
| 52 |
return XDP_PASS; |
| 53 |
|
| 54 |
|
| 55 |
if ((bpf_ntohs(veth->vlantci) & VLAN_ID_MASK) == 100) { |
| 56 |
if_key = 0; |
| 57 |
redirect = true; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
if ((bpf_ntohs(veth->vlantci) & VLAN_ID_MASK) == 200) { |
| 62 |
if_key = 1; |
| 63 |
redirect = true; |
| 64 |
} |
| 65 |
|
| 66 |
if (!redirect) |
| 67 |
return XDP_PASS; |
| 68 |
|
| 69 |
|
| 70 |
if (bpf_map_lookup_elem(&veth_map, &if_key)) |
| 71 |
return bpf_redirect_map(&veth_map, if_key, 0); |
| 72 |
|
| 73 |
return XDP_PASS; |
| 74 |
} |
| 75 |
|
| 76 |
char _license[] SEC("license") = "Dual BSD/GPL"; |