As requested by Daniel, we need to add a selftest to cover bpf_skb_change_tail() cases in skb_verdict. Here we test trimming, growing and error cases, and validate its expected return values and the expected sizes of the payload. Signed-off-by: Cong Wang <cong.wang@bytedance.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/bpf/20241213034057.246437-3-xiyou.wangcong@gmail.com
40 lines
980 B
C
40 lines
980 B
C
// SPDX-License-Identifier: GPL-2.0
|
|
/* Copyright (c) 2024 ByteDance */
|
|
#include <linux/bpf.h>
|
|
#include <bpf/bpf_helpers.h>
|
|
|
|
struct {
|
|
__uint(type, BPF_MAP_TYPE_SOCKMAP);
|
|
__uint(max_entries, 1);
|
|
__type(key, int);
|
|
__type(value, int);
|
|
} sock_map_rx SEC(".maps");
|
|
|
|
long change_tail_ret = 1;
|
|
|
|
SEC("sk_skb")
|
|
int prog_skb_verdict(struct __sk_buff *skb)
|
|
{
|
|
char *data, *data_end;
|
|
|
|
bpf_skb_pull_data(skb, 1);
|
|
data = (char *)(unsigned long)skb->data;
|
|
data_end = (char *)(unsigned long)skb->data_end;
|
|
|
|
if (data + 1 > data_end)
|
|
return SK_PASS;
|
|
|
|
if (data[0] == 'T') { /* Trim the packet */
|
|
change_tail_ret = bpf_skb_change_tail(skb, skb->len - 1, 0);
|
|
return SK_PASS;
|
|
} else if (data[0] == 'G') { /* Grow the packet */
|
|
change_tail_ret = bpf_skb_change_tail(skb, skb->len + 1, 0);
|
|
return SK_PASS;
|
|
} else if (data[0] == 'E') { /* Error */
|
|
change_tail_ret = bpf_skb_change_tail(skb, 65535, 0);
|
|
return SK_PASS;
|
|
}
|
|
return SK_PASS;
|
|
}
|
|
|
|
char _license[] SEC("license") = "GPL";
|