1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 #include <net/if.h>
5 #include "empty_skb.skel.h"
6 
7 void test_empty_skb(void)
8 {
9 	LIBBPF_OPTS(bpf_test_run_opts, tattr);
10 	struct empty_skb *bpf_obj = NULL;
11 	struct nstoken *tok = NULL;
12 	struct bpf_program *prog;
13 	char eth_hlen_pp[15];
14 	char eth_hlen[14];
15 	int veth_ifindex;
16 	int ipip_ifindex;
17 	int err;
18 	int i;
19 
20 	struct {
21 		const char *msg;
22 		const void *data_in;
23 		__u32 data_size_in;
24 		int *ifindex;
25 		int err;
26 		int ret;
27 		bool success_on_tc;
28 	} tests[] = {
29 		/* Empty packets are always rejected. */
30 
31 		{
32 			/* BPF_PROG_RUN ETH_HLEN size check */
33 			.msg = "veth empty ingress packet",
34 			.data_in = NULL,
35 			.data_size_in = 0,
36 			.ifindex = &veth_ifindex,
37 			.err = -EINVAL,
38 		},
39 		{
40 			/* BPF_PROG_RUN ETH_HLEN size check */
41 			.msg = "ipip empty ingress packet",
42 			.data_in = NULL,
43 			.data_size_in = 0,
44 			.ifindex = &ipip_ifindex,
45 			.err = -EINVAL,
46 		},
47 
48 		/* ETH_HLEN-sized packets:
49 		 * - can not be redirected at LWT_XMIT
50 		 * - can be redirected at TC to non-tunneling dest
51 		 */
52 
53 		{
54 			/* __bpf_redirect_common */
55 			.msg = "veth ETH_HLEN packet ingress",
56 			.data_in = eth_hlen,
57 			.data_size_in = sizeof(eth_hlen),
58 			.ifindex = &veth_ifindex,
59 			.ret = -ERANGE,
60 			.success_on_tc = true,
61 		},
62 		{
63 			/* __bpf_redirect_no_mac
64 			 *
65 			 * lwt: skb->len=0 <= skb_network_offset=0
66 			 * tc: skb->len=14 <= skb_network_offset=14
67 			 */
68 			.msg = "ipip ETH_HLEN packet ingress",
69 			.data_in = eth_hlen,
70 			.data_size_in = sizeof(eth_hlen),
71 			.ifindex = &ipip_ifindex,
72 			.ret = -ERANGE,
73 		},
74 
75 		/* ETH_HLEN+1-sized packet should be redirected. */
76 
77 		{
78 			.msg = "veth ETH_HLEN+1 packet ingress",
79 			.data_in = eth_hlen_pp,
80 			.data_size_in = sizeof(eth_hlen_pp),
81 			.ifindex = &veth_ifindex,
82 		},
83 		{
84 			.msg = "ipip ETH_HLEN+1 packet ingress",
85 			.data_in = eth_hlen_pp,
86 			.data_size_in = sizeof(eth_hlen_pp),
87 			.ifindex = &ipip_ifindex,
88 		},
89 	};
90 
91 	SYS(out, "ip netns add empty_skb");
92 	tok = open_netns("empty_skb");
93 	SYS(out, "ip link add veth0 type veth peer veth1");
94 	SYS(out, "ip link set dev veth0 up");
95 	SYS(out, "ip link set dev veth1 up");
96 	SYS(out, "ip addr add 10.0.0.1/8 dev veth0");
97 	SYS(out, "ip addr add 10.0.0.2/8 dev veth1");
98 	veth_ifindex = if_nametoindex("veth0");
99 
100 	SYS(out, "ip link add ipip0 type ipip local 10.0.0.1 remote 10.0.0.2");
101 	SYS(out, "ip link set ipip0 up");
102 	SYS(out, "ip addr add 192.168.1.1/16 dev ipip0");
103 	ipip_ifindex = if_nametoindex("ipip0");
104 
105 	bpf_obj = empty_skb__open_and_load();
106 	if (!ASSERT_OK_PTR(bpf_obj, "open skeleton"))
107 		goto out;
108 
109 	for (i = 0; i < ARRAY_SIZE(tests); i++) {
110 		bpf_object__for_each_program(prog, bpf_obj->obj) {
111 			char buf[128];
112 			bool at_tc = !strncmp(bpf_program__section_name(prog), "tc", 2);
113 
114 			tattr.data_in = tests[i].data_in;
115 			tattr.data_size_in = tests[i].data_size_in;
116 
117 			tattr.data_size_out = 0;
118 			bpf_obj->bss->ifindex = *tests[i].ifindex;
119 			bpf_obj->bss->ret = 0;
120 			err = bpf_prog_test_run_opts(bpf_program__fd(prog), &tattr);
121 			sprintf(buf, "err: %s [%s]", tests[i].msg, bpf_program__name(prog));
122 
123 			if (at_tc && tests[i].success_on_tc)
124 				ASSERT_GE(err, 0, buf);
125 			else
126 				ASSERT_EQ(err, tests[i].err, buf);
127 			sprintf(buf, "ret: %s [%s]", tests[i].msg, bpf_program__name(prog));
128 			if (at_tc && tests[i].success_on_tc)
129 				ASSERT_GE(bpf_obj->bss->ret, 0, buf);
130 			else
131 				ASSERT_EQ(bpf_obj->bss->ret, tests[i].ret, buf);
132 		}
133 	}
134 
135 out:
136 	if (bpf_obj)
137 		empty_skb__destroy(bpf_obj);
138 	if (tok)
139 		close_netns(tok);
140 	SYS_NOFAIL("ip netns del empty_skb");
141 }
142