1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 
5 void test_skb_ctx(void)
6 {
7 	struct __sk_buff skb = {
8 		.cb[0] = 1,
9 		.cb[1] = 2,
10 		.cb[2] = 3,
11 		.cb[3] = 4,
12 		.cb[4] = 5,
13 		.priority = 6,
14 		.ingress_ifindex = 11,
15 		.ifindex = 1,
16 		.tstamp = 7,
17 		.wire_len = 100,
18 		.gso_segs = 8,
19 		.mark = 9,
20 		.gso_size = 10,
21 		.hwtstamp = 11,
22 	};
23 	struct bpf_prog_test_run_attr tattr = {
24 		.data_in = &pkt_v4,
25 		.data_size_in = sizeof(pkt_v4),
26 		.ctx_in = &skb,
27 		.ctx_size_in = sizeof(skb),
28 		.ctx_out = &skb,
29 		.ctx_size_out = sizeof(skb),
30 	};
31 	struct bpf_object *obj;
32 	int err;
33 	int i;
34 
35 	err = bpf_prog_test_load("./test_skb_ctx.o", BPF_PROG_TYPE_SCHED_CLS, &obj,
36 			    &tattr.prog_fd);
37 	if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno))
38 		return;
39 
40 	/* ctx_in != NULL, ctx_size_in == 0 */
41 
42 	tattr.ctx_size_in = 0;
43 	err = bpf_prog_test_run_xattr(&tattr);
44 	CHECK_ATTR(err == 0, "ctx_size_in", "err %d errno %d\n", err, errno);
45 	tattr.ctx_size_in = sizeof(skb);
46 
47 	/* ctx_out != NULL, ctx_size_out == 0 */
48 
49 	tattr.ctx_size_out = 0;
50 	err = bpf_prog_test_run_xattr(&tattr);
51 	CHECK_ATTR(err == 0, "ctx_size_out", "err %d errno %d\n", err, errno);
52 	tattr.ctx_size_out = sizeof(skb);
53 
54 	/* non-zero [len, tc_index] fields should be rejected*/
55 
56 	skb.len = 1;
57 	err = bpf_prog_test_run_xattr(&tattr);
58 	CHECK_ATTR(err == 0, "len", "err %d errno %d\n", err, errno);
59 	skb.len = 0;
60 
61 	skb.tc_index = 1;
62 	err = bpf_prog_test_run_xattr(&tattr);
63 	CHECK_ATTR(err == 0, "tc_index", "err %d errno %d\n", err, errno);
64 	skb.tc_index = 0;
65 
66 	/* non-zero [hash, sk] fields should be rejected */
67 
68 	skb.hash = 1;
69 	err = bpf_prog_test_run_xattr(&tattr);
70 	CHECK_ATTR(err == 0, "hash", "err %d errno %d\n", err, errno);
71 	skb.hash = 0;
72 
73 	skb.sk = (struct bpf_sock *)1;
74 	err = bpf_prog_test_run_xattr(&tattr);
75 	CHECK_ATTR(err == 0, "sk", "err %d errno %d\n", err, errno);
76 	skb.sk = 0;
77 
78 	err = bpf_prog_test_run_xattr(&tattr);
79 	CHECK_ATTR(err != 0 || tattr.retval,
80 		   "run",
81 		   "err %d errno %d retval %d\n",
82 		   err, errno, tattr.retval);
83 
84 	CHECK_ATTR(tattr.ctx_size_out != sizeof(skb),
85 		   "ctx_size_out",
86 		   "incorrect output size, want %zu have %u\n",
87 		   sizeof(skb), tattr.ctx_size_out);
88 
89 	for (i = 0; i < 5; i++)
90 		CHECK_ATTR(skb.cb[i] != i + 2,
91 			   "ctx_out_cb",
92 			   "skb->cb[i] == %d, expected %d\n",
93 			   skb.cb[i], i + 2);
94 	CHECK_ATTR(skb.priority != 7,
95 		   "ctx_out_priority",
96 		   "skb->priority == %d, expected %d\n",
97 		   skb.priority, 7);
98 	CHECK_ATTR(skb.ifindex != 1,
99 		   "ctx_out_ifindex",
100 		   "skb->ifindex == %d, expected %d\n",
101 		   skb.ifindex, 1);
102 	CHECK_ATTR(skb.ingress_ifindex != 11,
103 		   "ctx_out_ingress_ifindex",
104 		   "skb->ingress_ifindex == %d, expected %d\n",
105 		   skb.ingress_ifindex, 11);
106 	CHECK_ATTR(skb.tstamp != 8,
107 		   "ctx_out_tstamp",
108 		   "skb->tstamp == %lld, expected %d\n",
109 		   skb.tstamp, 8);
110 	CHECK_ATTR(skb.mark != 10,
111 		   "ctx_out_mark",
112 		   "skb->mark == %u, expected %d\n",
113 		   skb.mark, 10);
114 
115 	bpf_object__close(obj);
116 }
117