1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 Facebook
3 #include <test_progs.h>
4 
5 static int duration = 0;
6 
7 struct sec_name_test {
8 	const char sec_name[32];
9 	struct {
10 		int rc;
11 		enum bpf_prog_type prog_type;
12 		enum bpf_attach_type expected_attach_type;
13 	} expected_load;
14 	struct {
15 		int rc;
16 		enum bpf_attach_type attach_type;
17 	} expected_attach;
18 };
19 
20 static struct sec_name_test tests[] = {
21 	{"InvAliD", {-ESRCH, 0, 0}, {-EINVAL, 0} },
22 	{"cgroup", {-ESRCH, 0, 0}, {-EINVAL, 0} },
23 	{"socket", {0, BPF_PROG_TYPE_SOCKET_FILTER, 0}, {-EINVAL, 0} },
24 	{"kprobe/", {0, BPF_PROG_TYPE_KPROBE, 0}, {-EINVAL, 0} },
25 	{"uprobe/", {0, BPF_PROG_TYPE_KPROBE, 0}, {-EINVAL, 0} },
26 	{"kretprobe/", {0, BPF_PROG_TYPE_KPROBE, 0}, {-EINVAL, 0} },
27 	{"uretprobe/", {0, BPF_PROG_TYPE_KPROBE, 0}, {-EINVAL, 0} },
28 	{"classifier", {0, BPF_PROG_TYPE_SCHED_CLS, 0}, {-EINVAL, 0} },
29 	{"action", {0, BPF_PROG_TYPE_SCHED_ACT, 0}, {-EINVAL, 0} },
30 	{"tracepoint/", {0, BPF_PROG_TYPE_TRACEPOINT, 0}, {-EINVAL, 0} },
31 	{"tp/", {0, BPF_PROG_TYPE_TRACEPOINT, 0}, {-EINVAL, 0} },
32 	{
33 		"raw_tracepoint/",
34 		{0, BPF_PROG_TYPE_RAW_TRACEPOINT, 0},
35 		{-EINVAL, 0},
36 	},
37 	{"raw_tp/", {0, BPF_PROG_TYPE_RAW_TRACEPOINT, 0}, {-EINVAL, 0} },
38 	{"xdp", {0, BPF_PROG_TYPE_XDP, 0}, {-EINVAL, 0} },
39 	{"perf_event", {0, BPF_PROG_TYPE_PERF_EVENT, 0}, {-EINVAL, 0} },
40 	{"lwt_in", {0, BPF_PROG_TYPE_LWT_IN, 0}, {-EINVAL, 0} },
41 	{"lwt_out", {0, BPF_PROG_TYPE_LWT_OUT, 0}, {-EINVAL, 0} },
42 	{"lwt_xmit", {0, BPF_PROG_TYPE_LWT_XMIT, 0}, {-EINVAL, 0} },
43 	{"lwt_seg6local", {0, BPF_PROG_TYPE_LWT_SEG6LOCAL, 0}, {-EINVAL, 0} },
44 	{
45 		"cgroup_skb/ingress",
46 		{0, BPF_PROG_TYPE_CGROUP_SKB, 0},
47 		{0, BPF_CGROUP_INET_INGRESS},
48 	},
49 	{
50 		"cgroup_skb/egress",
51 		{0, BPF_PROG_TYPE_CGROUP_SKB, 0},
52 		{0, BPF_CGROUP_INET_EGRESS},
53 	},
54 	{"cgroup/skb", {0, BPF_PROG_TYPE_CGROUP_SKB, 0}, {-EINVAL, 0} },
55 	{
56 		"cgroup/sock",
57 		{0, BPF_PROG_TYPE_CGROUP_SOCK, 0},
58 		{0, BPF_CGROUP_INET_SOCK_CREATE},
59 	},
60 	{
61 		"cgroup/post_bind4",
62 		{0, BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET4_POST_BIND},
63 		{0, BPF_CGROUP_INET4_POST_BIND},
64 	},
65 	{
66 		"cgroup/post_bind6",
67 		{0, BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET6_POST_BIND},
68 		{0, BPF_CGROUP_INET6_POST_BIND},
69 	},
70 	{
71 		"cgroup/dev",
72 		{0, BPF_PROG_TYPE_CGROUP_DEVICE, 0},
73 		{0, BPF_CGROUP_DEVICE},
74 	},
75 	{"sockops", {0, BPF_PROG_TYPE_SOCK_OPS, 0}, {0, BPF_CGROUP_SOCK_OPS} },
76 	{
77 		"sk_skb/stream_parser",
78 		{0, BPF_PROG_TYPE_SK_SKB, 0},
79 		{0, BPF_SK_SKB_STREAM_PARSER},
80 	},
81 	{
82 		"sk_skb/stream_verdict",
83 		{0, BPF_PROG_TYPE_SK_SKB, 0},
84 		{0, BPF_SK_SKB_STREAM_VERDICT},
85 	},
86 	{"sk_skb", {0, BPF_PROG_TYPE_SK_SKB, 0}, {-EINVAL, 0} },
87 	{"sk_msg", {0, BPF_PROG_TYPE_SK_MSG, 0}, {0, BPF_SK_MSG_VERDICT} },
88 	{"lirc_mode2", {0, BPF_PROG_TYPE_LIRC_MODE2, 0}, {0, BPF_LIRC_MODE2} },
89 	{
90 		"flow_dissector",
91 		{0, BPF_PROG_TYPE_FLOW_DISSECTOR, 0},
92 		{0, BPF_FLOW_DISSECTOR},
93 	},
94 	{
95 		"cgroup/bind4",
96 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND},
97 		{0, BPF_CGROUP_INET4_BIND},
98 	},
99 	{
100 		"cgroup/bind6",
101 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_BIND},
102 		{0, BPF_CGROUP_INET6_BIND},
103 	},
104 	{
105 		"cgroup/connect4",
106 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_CONNECT},
107 		{0, BPF_CGROUP_INET4_CONNECT},
108 	},
109 	{
110 		"cgroup/connect6",
111 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_CONNECT},
112 		{0, BPF_CGROUP_INET6_CONNECT},
113 	},
114 	{
115 		"cgroup/sendmsg4",
116 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_SENDMSG},
117 		{0, BPF_CGROUP_UDP4_SENDMSG},
118 	},
119 	{
120 		"cgroup/sendmsg6",
121 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_SENDMSG},
122 		{0, BPF_CGROUP_UDP6_SENDMSG},
123 	},
124 	{
125 		"cgroup/recvmsg4",
126 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_RECVMSG},
127 		{0, BPF_CGROUP_UDP4_RECVMSG},
128 	},
129 	{
130 		"cgroup/recvmsg6",
131 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_RECVMSG},
132 		{0, BPF_CGROUP_UDP6_RECVMSG},
133 	},
134 	{
135 		"cgroup/sysctl",
136 		{0, BPF_PROG_TYPE_CGROUP_SYSCTL, BPF_CGROUP_SYSCTL},
137 		{0, BPF_CGROUP_SYSCTL},
138 	},
139 	{
140 		"cgroup/getsockopt",
141 		{0, BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT},
142 		{0, BPF_CGROUP_GETSOCKOPT},
143 	},
144 	{
145 		"cgroup/setsockopt",
146 		{0, BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT},
147 		{0, BPF_CGROUP_SETSOCKOPT},
148 	},
149 };
150 
151 static void test_prog_type_by_name(const struct sec_name_test *test)
152 {
153 	enum bpf_attach_type expected_attach_type;
154 	enum bpf_prog_type prog_type;
155 	int rc;
156 
157 	rc = libbpf_prog_type_by_name(test->sec_name, &prog_type,
158 				      &expected_attach_type);
159 
160 	CHECK(rc != test->expected_load.rc, "check_code",
161 	      "prog: unexpected rc=%d for %s", rc, test->sec_name);
162 
163 	if (rc)
164 		return;
165 
166 	CHECK(prog_type != test->expected_load.prog_type, "check_prog_type",
167 	      "prog: unexpected prog_type=%d for %s",
168 	      prog_type, test->sec_name);
169 
170 	CHECK(expected_attach_type != test->expected_load.expected_attach_type,
171 	      "check_attach_type", "prog: unexpected expected_attach_type=%d for %s",
172 	      expected_attach_type, test->sec_name);
173 }
174 
175 static void test_attach_type_by_name(const struct sec_name_test *test)
176 {
177 	enum bpf_attach_type attach_type;
178 	int rc;
179 
180 	rc = libbpf_attach_type_by_name(test->sec_name, &attach_type);
181 
182 	CHECK(rc != test->expected_attach.rc, "check_ret",
183 	      "attach: unexpected rc=%d for %s", rc, test->sec_name);
184 
185 	if (rc)
186 		return;
187 
188 	CHECK(attach_type != test->expected_attach.attach_type,
189 	      "check_attach_type", "attach: unexpected attach_type=%d for %s",
190 	      attach_type, test->sec_name);
191 }
192 
193 void test_section_names(void)
194 {
195 	int i;
196 
197 	for (i = 0; i < ARRAY_SIZE(tests); ++i) {
198 		struct sec_name_test *test = &tests[i];
199 
200 		test_prog_type_by_name(test);
201 		test_attach_type_by_name(test);
202 	}
203 }
204