Skip to content

Commit 4a58963

Browse files
Cong Wangborkmann
authored andcommitted
selftests/bpf: Test bpf_skb_change_tail() in TC ingress
Similarly to the previous test, we also need a test case to cover positive offsets as well, TC is an excellent hook for this. Signed-off-by: Cong Wang <cong.wang@bytedance.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Tested-by: Zijian Zhang <zijianzhang@bytedance.com> Acked-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/bpf/20241213034057.246437-5-xiyou.wangcong@gmail.com
1 parent 472759c commit 4a58963

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <error.h>
3+
#include <test_progs.h>
4+
#include <linux/pkt_cls.h>
5+
6+
#include "test_tc_change_tail.skel.h"
7+
#include "socket_helpers.h"
8+
9+
#define LO_IFINDEX 1
10+
11+
void test_tc_change_tail(void)
12+
{
13+
LIBBPF_OPTS(bpf_tcx_opts, tcx_opts);
14+
struct test_tc_change_tail *skel = NULL;
15+
struct bpf_link *link;
16+
int c1, p1;
17+
char buf[2];
18+
int ret;
19+
20+
skel = test_tc_change_tail__open_and_load();
21+
if (!ASSERT_OK_PTR(skel, "test_tc_change_tail__open_and_load"))
22+
return;
23+
24+
link = bpf_program__attach_tcx(skel->progs.change_tail, LO_IFINDEX,
25+
&tcx_opts);
26+
if (!ASSERT_OK_PTR(link, "bpf_program__attach_tcx"))
27+
goto destroy;
28+
29+
skel->links.change_tail = link;
30+
ret = create_pair(AF_INET, SOCK_DGRAM, &c1, &p1);
31+
if (!ASSERT_OK(ret, "create_pair"))
32+
goto destroy;
33+
34+
ret = xsend(p1, "Tr", 2, 0);
35+
ASSERT_EQ(ret, 2, "xsend(p1)");
36+
ret = recv(c1, buf, 2, 0);
37+
ASSERT_EQ(ret, 2, "recv(c1)");
38+
ASSERT_EQ(skel->data->change_tail_ret, 0, "change_tail_ret");
39+
40+
ret = xsend(p1, "G", 1, 0);
41+
ASSERT_EQ(ret, 1, "xsend(p1)");
42+
ret = recv(c1, buf, 2, 0);
43+
ASSERT_EQ(ret, 1, "recv(c1)");
44+
ASSERT_EQ(skel->data->change_tail_ret, 0, "change_tail_ret");
45+
46+
ret = xsend(p1, "E", 1, 0);
47+
ASSERT_EQ(ret, 1, "xsend(p1)");
48+
ret = recv(c1, buf, 1, 0);
49+
ASSERT_EQ(ret, 1, "recv(c1)");
50+
ASSERT_EQ(skel->data->change_tail_ret, -EINVAL, "change_tail_ret");
51+
52+
ret = xsend(p1, "Z", 1, 0);
53+
ASSERT_EQ(ret, 1, "xsend(p1)");
54+
ret = recv(c1, buf, 1, 0);
55+
ASSERT_EQ(ret, 1, "recv(c1)");
56+
ASSERT_EQ(skel->data->change_tail_ret, -EINVAL, "change_tail_ret");
57+
58+
close(c1);
59+
close(p1);
60+
destroy:
61+
test_tc_change_tail__destroy(skel);
62+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/bpf.h>
3+
#include <bpf/bpf_helpers.h>
4+
#include <linux/if_ether.h>
5+
#include <linux/in.h>
6+
#include <linux/ip.h>
7+
#include <linux/udp.h>
8+
#include <linux/pkt_cls.h>
9+
10+
long change_tail_ret = 1;
11+
12+
static __always_inline struct iphdr *parse_ip_header(struct __sk_buff *skb, int *ip_proto)
13+
{
14+
void *data_end = (void *)(long)skb->data_end;
15+
void *data = (void *)(long)skb->data;
16+
struct ethhdr *eth = data;
17+
struct iphdr *iph;
18+
19+
/* Verify Ethernet header */
20+
if ((void *)(data + sizeof(*eth)) > data_end)
21+
return NULL;
22+
23+
/* Skip Ethernet header to get to IP header */
24+
iph = (void *)(data + sizeof(struct ethhdr));
25+
26+
/* Verify IP header */
27+
if ((void *)(data + sizeof(struct ethhdr) + sizeof(*iph)) > data_end)
28+
return NULL;
29+
30+
/* Basic IP header validation */
31+
if (iph->version != 4) /* Only support IPv4 */
32+
return NULL;
33+
34+
if (iph->ihl < 5) /* Minimum IP header length */
35+
return NULL;
36+
37+
*ip_proto = iph->protocol;
38+
return iph;
39+
}
40+
41+
static __always_inline struct udphdr *parse_udp_header(struct __sk_buff *skb, struct iphdr *iph)
42+
{
43+
void *data_end = (void *)(long)skb->data_end;
44+
void *hdr = (void *)iph;
45+
struct udphdr *udp;
46+
47+
/* Calculate UDP header position */
48+
udp = hdr + (iph->ihl * 4);
49+
hdr = (void *)udp;
50+
51+
/* Verify UDP header bounds */
52+
if ((void *)(hdr + sizeof(*udp)) > data_end)
53+
return NULL;
54+
55+
return udp;
56+
}
57+
58+
SEC("tc/ingress")
59+
int change_tail(struct __sk_buff *skb)
60+
{
61+
int len = skb->len;
62+
struct udphdr *udp;
63+
struct iphdr *iph;
64+
void *data_end;
65+
char *payload;
66+
int ip_proto;
67+
68+
bpf_skb_pull_data(skb, len);
69+
70+
data_end = (void *)(long)skb->data_end;
71+
iph = parse_ip_header(skb, &ip_proto);
72+
if (!iph)
73+
return TCX_PASS;
74+
75+
if (ip_proto != IPPROTO_UDP)
76+
return TCX_PASS;
77+
78+
udp = parse_udp_header(skb, iph);
79+
if (!udp)
80+
return TCX_PASS;
81+
82+
payload = (char *)udp + (sizeof(struct udphdr));
83+
if (payload + 1 > (char *)data_end)
84+
return TCX_PASS;
85+
86+
if (payload[0] == 'T') { /* Trim the packet */
87+
change_tail_ret = bpf_skb_change_tail(skb, len - 1, 0);
88+
if (!change_tail_ret)
89+
bpf_skb_change_tail(skb, len, 0);
90+
return TCX_PASS;
91+
} else if (payload[0] == 'G') { /* Grow the packet */
92+
change_tail_ret = bpf_skb_change_tail(skb, len + 1, 0);
93+
if (!change_tail_ret)
94+
bpf_skb_change_tail(skb, len, 0);
95+
return TCX_PASS;
96+
} else if (payload[0] == 'E') { /* Error */
97+
change_tail_ret = bpf_skb_change_tail(skb, 65535, 0);
98+
return TCX_PASS;
99+
} else if (payload[0] == 'Z') { /* Zero */
100+
change_tail_ret = bpf_skb_change_tail(skb, 0, 0);
101+
return TCX_PASS;
102+
}
103+
return TCX_DROP;
104+
}
105+
106+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)