2017-09-25 02:25:53 +02:00
|
|
|
#include <linux/bpf.h>
|
|
|
|
#include <linux/if_ether.h>
|
|
|
|
#include <linux/pkt_cls.h>
|
|
|
|
|
2020-01-20 14:06:45 +01:00
|
|
|
#include <bpf/bpf_helpers.h>
|
2017-09-25 02:25:53 +02:00
|
|
|
|
2025-03-05 21:34:36 +00:00
|
|
|
#define META_SIZE 32
|
|
|
|
|
2017-09-25 02:25:53 +02:00
|
|
|
#define ctx_ptr(ctx, mem) (void *)(unsigned long)ctx->mem
|
|
|
|
|
2025-03-05 21:34:36 +00:00
|
|
|
/* Demonstrates how metadata can be passed from an XDP program to a TC program
|
|
|
|
* using bpf_xdp_adjust_meta.
|
|
|
|
* For the sake of testing the metadata support in drivers, the XDP program uses
|
|
|
|
* a fixed-size payload after the Ethernet header as metadata. The TC program
|
|
|
|
* copies the metadata it receives into a map so it can be checked from
|
|
|
|
* userspace.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct {
|
|
|
|
__uint(type, BPF_MAP_TYPE_ARRAY);
|
|
|
|
__uint(max_entries, 1);
|
|
|
|
__type(key, __u32);
|
|
|
|
__uint(value_size, META_SIZE);
|
|
|
|
} test_result SEC(".maps");
|
|
|
|
|
2024-12-13 16:06:20 +01:00
|
|
|
SEC("tc")
|
2017-09-25 02:25:53 +02:00
|
|
|
int ing_cls(struct __sk_buff *ctx)
|
|
|
|
{
|
2025-03-05 21:34:36 +00:00
|
|
|
__u8 *data, *data_meta;
|
|
|
|
__u32 key = 0;
|
2017-09-25 02:25:53 +02:00
|
|
|
|
|
|
|
data_meta = ctx_ptr(ctx, data_meta);
|
|
|
|
data = ctx_ptr(ctx, data);
|
|
|
|
|
2025-03-05 21:34:36 +00:00
|
|
|
if (data_meta + META_SIZE > data)
|
2017-09-25 02:25:53 +02:00
|
|
|
return TC_ACT_SHOT;
|
|
|
|
|
2025-03-05 21:34:36 +00:00
|
|
|
bpf_map_update_elem(&test_result, &key, data_meta, BPF_ANY);
|
2017-09-25 02:25:53 +02:00
|
|
|
|
2025-03-05 21:34:36 +00:00
|
|
|
return TC_ACT_SHOT;
|
2017-09-25 02:25:53 +02:00
|
|
|
}
|
|
|
|
|
2024-12-13 16:06:20 +01:00
|
|
|
SEC("xdp")
|
2017-09-25 02:25:53 +02:00
|
|
|
int ing_xdp(struct xdp_md *ctx)
|
|
|
|
{
|
2025-03-05 21:34:36 +00:00
|
|
|
__u8 *data, *data_meta, *data_end, *payload;
|
|
|
|
struct ethhdr *eth;
|
2017-09-25 02:25:53 +02:00
|
|
|
int ret;
|
|
|
|
|
2025-03-05 21:34:36 +00:00
|
|
|
ret = bpf_xdp_adjust_meta(ctx, -META_SIZE);
|
2017-09-25 02:25:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return XDP_DROP;
|
|
|
|
|
|
|
|
data_meta = ctx_ptr(ctx, data_meta);
|
|
|
|
data_end = ctx_ptr(ctx, data_end);
|
|
|
|
data = ctx_ptr(ctx, data);
|
|
|
|
|
2025-03-05 21:34:36 +00:00
|
|
|
eth = (struct ethhdr *)data;
|
|
|
|
payload = data + sizeof(struct ethhdr);
|
|
|
|
|
|
|
|
if (payload + META_SIZE > data_end ||
|
|
|
|
data_meta + META_SIZE > data)
|
|
|
|
return XDP_DROP;
|
|
|
|
|
|
|
|
/* The Linux networking stack may send other packets on the test
|
|
|
|
* interface that interfere with the test. Just drop them.
|
|
|
|
* The test packets can be recognized by their ethertype of zero.
|
|
|
|
*/
|
|
|
|
if (eth->h_proto != 0)
|
2017-09-25 02:25:53 +02:00
|
|
|
return XDP_DROP;
|
|
|
|
|
2025-03-05 21:34:36 +00:00
|
|
|
__builtin_memcpy(data_meta, payload, META_SIZE);
|
2017-09-25 02:25:53 +02:00
|
|
|
return XDP_PASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
char _license[] SEC("license") = "GPL";
|