1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 Facebook
3 
4 #define _GNU_SOURCE
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 
10 #include <arpa/inet.h>
11 #include <netinet/in.h>
12 #include <sys/types.h>
13 #include <sys/select.h>
14 #include <sys/socket.h>
15 
16 #include <linux/filter.h>
17 
18 #include <bpf/bpf.h>
19 #include <bpf/libbpf.h>
20 
21 #include "cgroup_helpers.h"
22 #include "bpf_rlimit.h"
23 
24 #ifndef ENOTSUPP
25 # define ENOTSUPP 524
26 #endif
27 
28 #ifndef ARRAY_SIZE
29 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
30 #endif
31 
32 #define CG_PATH	"/foo"
33 #define CONNECT4_PROG_PATH	"./connect4_prog.o"
34 #define CONNECT6_PROG_PATH	"./connect6_prog.o"
35 #define SENDMSG4_PROG_PATH	"./sendmsg4_prog.o"
36 #define SENDMSG6_PROG_PATH	"./sendmsg6_prog.o"
37 
38 #define SERV4_IP		"192.168.1.254"
39 #define SERV4_REWRITE_IP	"127.0.0.1"
40 #define SRC4_IP			"172.16.0.1"
41 #define SRC4_REWRITE_IP		"127.0.0.4"
42 #define SERV4_PORT		4040
43 #define SERV4_REWRITE_PORT	4444
44 
45 #define SERV6_IP		"face:b00c:1234:5678::abcd"
46 #define SERV6_REWRITE_IP	"::1"
47 #define SERV6_V4MAPPED_IP	"::ffff:192.168.0.4"
48 #define SRC6_IP			"::1"
49 #define SRC6_REWRITE_IP		"::6"
50 #define SERV6_PORT		6060
51 #define SERV6_REWRITE_PORT	6666
52 
53 #define INET_NTOP_BUF	40
54 
55 struct sock_addr_test;
56 
57 typedef int (*load_fn)(const struct sock_addr_test *test);
58 typedef int (*info_fn)(int, struct sockaddr *, socklen_t *);
59 
60 char bpf_log_buf[BPF_LOG_BUF_SIZE];
61 
62 struct sock_addr_test {
63 	const char *descr;
64 	/* BPF prog properties */
65 	load_fn loadfn;
66 	enum bpf_attach_type expected_attach_type;
67 	enum bpf_attach_type attach_type;
68 	/* Socket properties */
69 	int domain;
70 	int type;
71 	/* IP:port pairs for BPF prog to override */
72 	const char *requested_ip;
73 	unsigned short requested_port;
74 	const char *expected_ip;
75 	unsigned short expected_port;
76 	const char *expected_src_ip;
77 	/* Expected test result */
78 	enum {
79 		LOAD_REJECT,
80 		ATTACH_REJECT,
81 		SYSCALL_EPERM,
82 		SYSCALL_ENOTSUPP,
83 		SUCCESS,
84 	} expected_result;
85 };
86 
87 static int bind4_prog_load(const struct sock_addr_test *test);
88 static int bind6_prog_load(const struct sock_addr_test *test);
89 static int connect4_prog_load(const struct sock_addr_test *test);
90 static int connect6_prog_load(const struct sock_addr_test *test);
91 static int sendmsg_deny_prog_load(const struct sock_addr_test *test);
92 static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test);
93 static int sendmsg4_rw_c_prog_load(const struct sock_addr_test *test);
94 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test);
95 static int sendmsg6_rw_c_prog_load(const struct sock_addr_test *test);
96 static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test *test);
97 
98 static struct sock_addr_test tests[] = {
99 	/* bind */
100 	{
101 		"bind4: load prog with wrong expected attach type",
102 		bind4_prog_load,
103 		BPF_CGROUP_INET6_BIND,
104 		BPF_CGROUP_INET4_BIND,
105 		AF_INET,
106 		SOCK_STREAM,
107 		NULL,
108 		0,
109 		NULL,
110 		0,
111 		NULL,
112 		LOAD_REJECT,
113 	},
114 	{
115 		"bind4: attach prog with wrong attach type",
116 		bind4_prog_load,
117 		BPF_CGROUP_INET4_BIND,
118 		BPF_CGROUP_INET6_BIND,
119 		AF_INET,
120 		SOCK_STREAM,
121 		NULL,
122 		0,
123 		NULL,
124 		0,
125 		NULL,
126 		ATTACH_REJECT,
127 	},
128 	{
129 		"bind4: rewrite IP & TCP port in",
130 		bind4_prog_load,
131 		BPF_CGROUP_INET4_BIND,
132 		BPF_CGROUP_INET4_BIND,
133 		AF_INET,
134 		SOCK_STREAM,
135 		SERV4_IP,
136 		SERV4_PORT,
137 		SERV4_REWRITE_IP,
138 		SERV4_REWRITE_PORT,
139 		NULL,
140 		SUCCESS,
141 	},
142 	{
143 		"bind4: rewrite IP & UDP port in",
144 		bind4_prog_load,
145 		BPF_CGROUP_INET4_BIND,
146 		BPF_CGROUP_INET4_BIND,
147 		AF_INET,
148 		SOCK_DGRAM,
149 		SERV4_IP,
150 		SERV4_PORT,
151 		SERV4_REWRITE_IP,
152 		SERV4_REWRITE_PORT,
153 		NULL,
154 		SUCCESS,
155 	},
156 	{
157 		"bind6: load prog with wrong expected attach type",
158 		bind6_prog_load,
159 		BPF_CGROUP_INET4_BIND,
160 		BPF_CGROUP_INET6_BIND,
161 		AF_INET6,
162 		SOCK_STREAM,
163 		NULL,
164 		0,
165 		NULL,
166 		0,
167 		NULL,
168 		LOAD_REJECT,
169 	},
170 	{
171 		"bind6: attach prog with wrong attach type",
172 		bind6_prog_load,
173 		BPF_CGROUP_INET6_BIND,
174 		BPF_CGROUP_INET4_BIND,
175 		AF_INET,
176 		SOCK_STREAM,
177 		NULL,
178 		0,
179 		NULL,
180 		0,
181 		NULL,
182 		ATTACH_REJECT,
183 	},
184 	{
185 		"bind6: rewrite IP & TCP port in",
186 		bind6_prog_load,
187 		BPF_CGROUP_INET6_BIND,
188 		BPF_CGROUP_INET6_BIND,
189 		AF_INET6,
190 		SOCK_STREAM,
191 		SERV6_IP,
192 		SERV6_PORT,
193 		SERV6_REWRITE_IP,
194 		SERV6_REWRITE_PORT,
195 		NULL,
196 		SUCCESS,
197 	},
198 	{
199 		"bind6: rewrite IP & UDP port in",
200 		bind6_prog_load,
201 		BPF_CGROUP_INET6_BIND,
202 		BPF_CGROUP_INET6_BIND,
203 		AF_INET6,
204 		SOCK_DGRAM,
205 		SERV6_IP,
206 		SERV6_PORT,
207 		SERV6_REWRITE_IP,
208 		SERV6_REWRITE_PORT,
209 		NULL,
210 		SUCCESS,
211 	},
212 
213 	/* connect */
214 	{
215 		"connect4: load prog with wrong expected attach type",
216 		connect4_prog_load,
217 		BPF_CGROUP_INET6_CONNECT,
218 		BPF_CGROUP_INET4_CONNECT,
219 		AF_INET,
220 		SOCK_STREAM,
221 		NULL,
222 		0,
223 		NULL,
224 		0,
225 		NULL,
226 		LOAD_REJECT,
227 	},
228 	{
229 		"connect4: attach prog with wrong attach type",
230 		connect4_prog_load,
231 		BPF_CGROUP_INET4_CONNECT,
232 		BPF_CGROUP_INET6_CONNECT,
233 		AF_INET,
234 		SOCK_STREAM,
235 		NULL,
236 		0,
237 		NULL,
238 		0,
239 		NULL,
240 		ATTACH_REJECT,
241 	},
242 	{
243 		"connect4: rewrite IP & TCP port",
244 		connect4_prog_load,
245 		BPF_CGROUP_INET4_CONNECT,
246 		BPF_CGROUP_INET4_CONNECT,
247 		AF_INET,
248 		SOCK_STREAM,
249 		SERV4_IP,
250 		SERV4_PORT,
251 		SERV4_REWRITE_IP,
252 		SERV4_REWRITE_PORT,
253 		SRC4_REWRITE_IP,
254 		SUCCESS,
255 	},
256 	{
257 		"connect4: rewrite IP & UDP port",
258 		connect4_prog_load,
259 		BPF_CGROUP_INET4_CONNECT,
260 		BPF_CGROUP_INET4_CONNECT,
261 		AF_INET,
262 		SOCK_DGRAM,
263 		SERV4_IP,
264 		SERV4_PORT,
265 		SERV4_REWRITE_IP,
266 		SERV4_REWRITE_PORT,
267 		SRC4_REWRITE_IP,
268 		SUCCESS,
269 	},
270 	{
271 		"connect6: load prog with wrong expected attach type",
272 		connect6_prog_load,
273 		BPF_CGROUP_INET4_CONNECT,
274 		BPF_CGROUP_INET6_CONNECT,
275 		AF_INET6,
276 		SOCK_STREAM,
277 		NULL,
278 		0,
279 		NULL,
280 		0,
281 		NULL,
282 		LOAD_REJECT,
283 	},
284 	{
285 		"connect6: attach prog with wrong attach type",
286 		connect6_prog_load,
287 		BPF_CGROUP_INET6_CONNECT,
288 		BPF_CGROUP_INET4_CONNECT,
289 		AF_INET,
290 		SOCK_STREAM,
291 		NULL,
292 		0,
293 		NULL,
294 		0,
295 		NULL,
296 		ATTACH_REJECT,
297 	},
298 	{
299 		"connect6: rewrite IP & TCP port",
300 		connect6_prog_load,
301 		BPF_CGROUP_INET6_CONNECT,
302 		BPF_CGROUP_INET6_CONNECT,
303 		AF_INET6,
304 		SOCK_STREAM,
305 		SERV6_IP,
306 		SERV6_PORT,
307 		SERV6_REWRITE_IP,
308 		SERV6_REWRITE_PORT,
309 		SRC6_REWRITE_IP,
310 		SUCCESS,
311 	},
312 	{
313 		"connect6: rewrite IP & UDP port",
314 		connect6_prog_load,
315 		BPF_CGROUP_INET6_CONNECT,
316 		BPF_CGROUP_INET6_CONNECT,
317 		AF_INET6,
318 		SOCK_DGRAM,
319 		SERV6_IP,
320 		SERV6_PORT,
321 		SERV6_REWRITE_IP,
322 		SERV6_REWRITE_PORT,
323 		SRC6_REWRITE_IP,
324 		SUCCESS,
325 	},
326 
327 	/* sendmsg */
328 	{
329 		"sendmsg4: load prog with wrong expected attach type",
330 		sendmsg4_rw_asm_prog_load,
331 		BPF_CGROUP_UDP6_SENDMSG,
332 		BPF_CGROUP_UDP4_SENDMSG,
333 		AF_INET,
334 		SOCK_DGRAM,
335 		NULL,
336 		0,
337 		NULL,
338 		0,
339 		NULL,
340 		LOAD_REJECT,
341 	},
342 	{
343 		"sendmsg4: attach prog with wrong attach type",
344 		sendmsg4_rw_asm_prog_load,
345 		BPF_CGROUP_UDP4_SENDMSG,
346 		BPF_CGROUP_UDP6_SENDMSG,
347 		AF_INET,
348 		SOCK_DGRAM,
349 		NULL,
350 		0,
351 		NULL,
352 		0,
353 		NULL,
354 		ATTACH_REJECT,
355 	},
356 	{
357 		"sendmsg4: rewrite IP & port (asm)",
358 		sendmsg4_rw_asm_prog_load,
359 		BPF_CGROUP_UDP4_SENDMSG,
360 		BPF_CGROUP_UDP4_SENDMSG,
361 		AF_INET,
362 		SOCK_DGRAM,
363 		SERV4_IP,
364 		SERV4_PORT,
365 		SERV4_REWRITE_IP,
366 		SERV4_REWRITE_PORT,
367 		SRC4_REWRITE_IP,
368 		SUCCESS,
369 	},
370 	{
371 		"sendmsg4: rewrite IP & port (C)",
372 		sendmsg4_rw_c_prog_load,
373 		BPF_CGROUP_UDP4_SENDMSG,
374 		BPF_CGROUP_UDP4_SENDMSG,
375 		AF_INET,
376 		SOCK_DGRAM,
377 		SERV4_IP,
378 		SERV4_PORT,
379 		SERV4_REWRITE_IP,
380 		SERV4_REWRITE_PORT,
381 		SRC4_REWRITE_IP,
382 		SUCCESS,
383 	},
384 	{
385 		"sendmsg4: deny call",
386 		sendmsg_deny_prog_load,
387 		BPF_CGROUP_UDP4_SENDMSG,
388 		BPF_CGROUP_UDP4_SENDMSG,
389 		AF_INET,
390 		SOCK_DGRAM,
391 		SERV4_IP,
392 		SERV4_PORT,
393 		SERV4_REWRITE_IP,
394 		SERV4_REWRITE_PORT,
395 		SRC4_REWRITE_IP,
396 		SYSCALL_EPERM,
397 	},
398 	{
399 		"sendmsg6: load prog with wrong expected attach type",
400 		sendmsg6_rw_asm_prog_load,
401 		BPF_CGROUP_UDP4_SENDMSG,
402 		BPF_CGROUP_UDP6_SENDMSG,
403 		AF_INET6,
404 		SOCK_DGRAM,
405 		NULL,
406 		0,
407 		NULL,
408 		0,
409 		NULL,
410 		LOAD_REJECT,
411 	},
412 	{
413 		"sendmsg6: attach prog with wrong attach type",
414 		sendmsg6_rw_asm_prog_load,
415 		BPF_CGROUP_UDP6_SENDMSG,
416 		BPF_CGROUP_UDP4_SENDMSG,
417 		AF_INET6,
418 		SOCK_DGRAM,
419 		NULL,
420 		0,
421 		NULL,
422 		0,
423 		NULL,
424 		ATTACH_REJECT,
425 	},
426 	{
427 		"sendmsg6: rewrite IP & port (asm)",
428 		sendmsg6_rw_asm_prog_load,
429 		BPF_CGROUP_UDP6_SENDMSG,
430 		BPF_CGROUP_UDP6_SENDMSG,
431 		AF_INET6,
432 		SOCK_DGRAM,
433 		SERV6_IP,
434 		SERV6_PORT,
435 		SERV6_REWRITE_IP,
436 		SERV6_REWRITE_PORT,
437 		SRC6_REWRITE_IP,
438 		SUCCESS,
439 	},
440 	{
441 		"sendmsg6: rewrite IP & port (C)",
442 		sendmsg6_rw_c_prog_load,
443 		BPF_CGROUP_UDP6_SENDMSG,
444 		BPF_CGROUP_UDP6_SENDMSG,
445 		AF_INET6,
446 		SOCK_DGRAM,
447 		SERV6_IP,
448 		SERV6_PORT,
449 		SERV6_REWRITE_IP,
450 		SERV6_REWRITE_PORT,
451 		SRC6_REWRITE_IP,
452 		SUCCESS,
453 	},
454 	{
455 		"sendmsg6: IPv4-mapped IPv6",
456 		sendmsg6_rw_v4mapped_prog_load,
457 		BPF_CGROUP_UDP6_SENDMSG,
458 		BPF_CGROUP_UDP6_SENDMSG,
459 		AF_INET6,
460 		SOCK_DGRAM,
461 		SERV6_IP,
462 		SERV6_PORT,
463 		SERV6_REWRITE_IP,
464 		SERV6_REWRITE_PORT,
465 		SRC6_REWRITE_IP,
466 		SYSCALL_ENOTSUPP,
467 	},
468 	{
469 		"sendmsg6: deny call",
470 		sendmsg_deny_prog_load,
471 		BPF_CGROUP_UDP6_SENDMSG,
472 		BPF_CGROUP_UDP6_SENDMSG,
473 		AF_INET6,
474 		SOCK_DGRAM,
475 		SERV6_IP,
476 		SERV6_PORT,
477 		SERV6_REWRITE_IP,
478 		SERV6_REWRITE_PORT,
479 		SRC6_REWRITE_IP,
480 		SYSCALL_EPERM,
481 	},
482 };
483 
484 static int mk_sockaddr(int domain, const char *ip, unsigned short port,
485 		       struct sockaddr *addr, socklen_t addr_len)
486 {
487 	struct sockaddr_in6 *addr6;
488 	struct sockaddr_in *addr4;
489 
490 	if (domain != AF_INET && domain != AF_INET6) {
491 		log_err("Unsupported address family");
492 		return -1;
493 	}
494 
495 	memset(addr, 0, addr_len);
496 
497 	if (domain == AF_INET) {
498 		if (addr_len < sizeof(struct sockaddr_in))
499 			return -1;
500 		addr4 = (struct sockaddr_in *)addr;
501 		addr4->sin_family = domain;
502 		addr4->sin_port = htons(port);
503 		if (inet_pton(domain, ip, (void *)&addr4->sin_addr) != 1) {
504 			log_err("Invalid IPv4: %s", ip);
505 			return -1;
506 		}
507 	} else if (domain == AF_INET6) {
508 		if (addr_len < sizeof(struct sockaddr_in6))
509 			return -1;
510 		addr6 = (struct sockaddr_in6 *)addr;
511 		addr6->sin6_family = domain;
512 		addr6->sin6_port = htons(port);
513 		if (inet_pton(domain, ip, (void *)&addr6->sin6_addr) != 1) {
514 			log_err("Invalid IPv6: %s", ip);
515 			return -1;
516 		}
517 	}
518 
519 	return 0;
520 }
521 
522 static int load_insns(const struct sock_addr_test *test,
523 		      const struct bpf_insn *insns, size_t insns_cnt)
524 {
525 	struct bpf_load_program_attr load_attr;
526 	int ret;
527 
528 	memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
529 	load_attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
530 	load_attr.expected_attach_type = test->expected_attach_type;
531 	load_attr.insns = insns;
532 	load_attr.insns_cnt = insns_cnt;
533 	load_attr.license = "GPL";
534 
535 	ret = bpf_load_program_xattr(&load_attr, bpf_log_buf, BPF_LOG_BUF_SIZE);
536 	if (ret < 0 && test->expected_result != LOAD_REJECT) {
537 		log_err(">>> Loading program error.\n"
538 			">>> Verifier output:\n%s\n-------\n", bpf_log_buf);
539 	}
540 
541 	return ret;
542 }
543 
544 /* [1] These testing programs try to read different context fields, including
545  * narrow loads of different sizes from user_ip4 and user_ip6, and write to
546  * those allowed to be overridden.
547  *
548  * [2] BPF_LD_IMM64 & BPF_JMP_REG are used below whenever there is a need to
549  * compare a register with unsigned 32bit integer. BPF_JMP_IMM can't be used
550  * in such cases since it accepts only _signed_ 32bit integer as IMM
551  * argument. Also note that BPF_LD_IMM64 contains 2 instructions what matters
552  * to count jumps properly.
553  */
554 
555 static int bind4_prog_load(const struct sock_addr_test *test)
556 {
557 	union {
558 		uint8_t u4_addr8[4];
559 		uint16_t u4_addr16[2];
560 		uint32_t u4_addr32;
561 	} ip4;
562 	struct sockaddr_in addr4_rw;
563 
564 	if (inet_pton(AF_INET, SERV4_IP, (void *)&ip4) != 1) {
565 		log_err("Invalid IPv4: %s", SERV4_IP);
566 		return -1;
567 	}
568 
569 	if (mk_sockaddr(AF_INET, SERV4_REWRITE_IP, SERV4_REWRITE_PORT,
570 			(struct sockaddr *)&addr4_rw, sizeof(addr4_rw)) == -1)
571 		return -1;
572 
573 	/* See [1]. */
574 	struct bpf_insn insns[] = {
575 		BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
576 
577 		/* if (sk.family == AF_INET && */
578 		BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
579 			    offsetof(struct bpf_sock_addr, family)),
580 		BPF_JMP_IMM(BPF_JNE, BPF_REG_7, AF_INET, 16),
581 
582 		/*     (sk.type == SOCK_DGRAM || sk.type == SOCK_STREAM) && */
583 		BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
584 			    offsetof(struct bpf_sock_addr, type)),
585 		BPF_JMP_IMM(BPF_JNE, BPF_REG_7, SOCK_DGRAM, 1),
586 		BPF_JMP_A(1),
587 		BPF_JMP_IMM(BPF_JNE, BPF_REG_7, SOCK_STREAM, 12),
588 
589 		/*     1st_byte_of_user_ip4 == expected && */
590 		BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_6,
591 			    offsetof(struct bpf_sock_addr, user_ip4)),
592 		BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr8[0], 10),
593 
594 		/*     1st_half_of_user_ip4 == expected && */
595 		BPF_LDX_MEM(BPF_H, BPF_REG_7, BPF_REG_6,
596 			    offsetof(struct bpf_sock_addr, user_ip4)),
597 		BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr16[0], 8),
598 
599 		/*     whole_user_ip4 == expected) { */
600 		BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
601 			    offsetof(struct bpf_sock_addr, user_ip4)),
602 		BPF_LD_IMM64(BPF_REG_8, ip4.u4_addr32), /* See [2]. */
603 		BPF_JMP_REG(BPF_JNE, BPF_REG_7, BPF_REG_8, 4),
604 
605 		/*      user_ip4 = addr4_rw.sin_addr */
606 		BPF_MOV32_IMM(BPF_REG_7, addr4_rw.sin_addr.s_addr),
607 		BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7,
608 			    offsetof(struct bpf_sock_addr, user_ip4)),
609 
610 		/*      user_port = addr4_rw.sin_port */
611 		BPF_MOV32_IMM(BPF_REG_7, addr4_rw.sin_port),
612 		BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7,
613 			    offsetof(struct bpf_sock_addr, user_port)),
614 		/* } */
615 
616 		/* return 1 */
617 		BPF_MOV64_IMM(BPF_REG_0, 1),
618 		BPF_EXIT_INSN(),
619 	};
620 
621 	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
622 }
623 
624 static int bind6_prog_load(const struct sock_addr_test *test)
625 {
626 	struct sockaddr_in6 addr6_rw;
627 	struct in6_addr ip6;
628 
629 	if (inet_pton(AF_INET6, SERV6_IP, (void *)&ip6) != 1) {
630 		log_err("Invalid IPv6: %s", SERV6_IP);
631 		return -1;
632 	}
633 
634 	if (mk_sockaddr(AF_INET6, SERV6_REWRITE_IP, SERV6_REWRITE_PORT,
635 			(struct sockaddr *)&addr6_rw, sizeof(addr6_rw)) == -1)
636 		return -1;
637 
638 	/* See [1]. */
639 	struct bpf_insn insns[] = {
640 		BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
641 
642 		/* if (sk.family == AF_INET6 && */
643 		BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
644 			    offsetof(struct bpf_sock_addr, family)),
645 		BPF_JMP_IMM(BPF_JNE, BPF_REG_7, AF_INET6, 18),
646 
647 		/*            5th_byte_of_user_ip6 == expected && */
648 		BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_6,
649 			    offsetof(struct bpf_sock_addr, user_ip6[1])),
650 		BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip6.s6_addr[4], 16),
651 
652 		/*            3rd_half_of_user_ip6 == expected && */
653 		BPF_LDX_MEM(BPF_H, BPF_REG_7, BPF_REG_6,
654 			    offsetof(struct bpf_sock_addr, user_ip6[1])),
655 		BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip6.s6_addr16[2], 14),
656 
657 		/*            last_word_of_user_ip6 == expected) { */
658 		BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
659 			    offsetof(struct bpf_sock_addr, user_ip6[3])),
660 		BPF_LD_IMM64(BPF_REG_8, ip6.s6_addr32[3]),  /* See [2]. */
661 		BPF_JMP_REG(BPF_JNE, BPF_REG_7, BPF_REG_8, 10),
662 
663 
664 #define STORE_IPV6_WORD(N)						       \
665 		BPF_MOV32_IMM(BPF_REG_7, addr6_rw.sin6_addr.s6_addr32[N]),     \
666 		BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7,		       \
667 			    offsetof(struct bpf_sock_addr, user_ip6[N]))
668 
669 		/*      user_ip6 = addr6_rw.sin6_addr */
670 		STORE_IPV6_WORD(0),
671 		STORE_IPV6_WORD(1),
672 		STORE_IPV6_WORD(2),
673 		STORE_IPV6_WORD(3),
674 
675 		/*      user_port = addr6_rw.sin6_port */
676 		BPF_MOV32_IMM(BPF_REG_7, addr6_rw.sin6_port),
677 		BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7,
678 			    offsetof(struct bpf_sock_addr, user_port)),
679 
680 		/* } */
681 
682 		/* return 1 */
683 		BPF_MOV64_IMM(BPF_REG_0, 1),
684 		BPF_EXIT_INSN(),
685 	};
686 
687 	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
688 }
689 
690 static int load_path(const struct sock_addr_test *test, const char *path)
691 {
692 	struct bpf_prog_load_attr attr;
693 	struct bpf_object *obj;
694 	int prog_fd;
695 
696 	memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
697 	attr.file = path;
698 	attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
699 	attr.expected_attach_type = test->expected_attach_type;
700 
701 	if (bpf_prog_load_xattr(&attr, &obj, &prog_fd)) {
702 		if (test->expected_result != LOAD_REJECT)
703 			log_err(">>> Loading program (%s) error.\n", path);
704 		return -1;
705 	}
706 
707 	return prog_fd;
708 }
709 
710 static int connect4_prog_load(const struct sock_addr_test *test)
711 {
712 	return load_path(test, CONNECT4_PROG_PATH);
713 }
714 
715 static int connect6_prog_load(const struct sock_addr_test *test)
716 {
717 	return load_path(test, CONNECT6_PROG_PATH);
718 }
719 
720 static int sendmsg_deny_prog_load(const struct sock_addr_test *test)
721 {
722 	struct bpf_insn insns[] = {
723 		/* return 0 */
724 		BPF_MOV64_IMM(BPF_REG_0, 0),
725 		BPF_EXIT_INSN(),
726 	};
727 	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
728 }
729 
730 static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test)
731 {
732 	struct sockaddr_in dst4_rw_addr;
733 	struct in_addr src4_rw_ip;
734 
735 	if (inet_pton(AF_INET, SRC4_REWRITE_IP, (void *)&src4_rw_ip) != 1) {
736 		log_err("Invalid IPv4: %s", SRC4_REWRITE_IP);
737 		return -1;
738 	}
739 
740 	if (mk_sockaddr(AF_INET, SERV4_REWRITE_IP, SERV4_REWRITE_PORT,
741 			(struct sockaddr *)&dst4_rw_addr,
742 			sizeof(dst4_rw_addr)) == -1)
743 		return -1;
744 
745 	struct bpf_insn insns[] = {
746 		BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
747 
748 		/* if (sk.family == AF_INET && */
749 		BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
750 			    offsetof(struct bpf_sock_addr, family)),
751 		BPF_JMP_IMM(BPF_JNE, BPF_REG_7, AF_INET, 8),
752 
753 		/*     sk.type == SOCK_DGRAM)  { */
754 		BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
755 			    offsetof(struct bpf_sock_addr, type)),
756 		BPF_JMP_IMM(BPF_JNE, BPF_REG_7, SOCK_DGRAM, 6),
757 
758 		/*      msg_src_ip4 = src4_rw_ip */
759 		BPF_MOV32_IMM(BPF_REG_7, src4_rw_ip.s_addr),
760 		BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7,
761 			    offsetof(struct bpf_sock_addr, msg_src_ip4)),
762 
763 		/*      user_ip4 = dst4_rw_addr.sin_addr */
764 		BPF_MOV32_IMM(BPF_REG_7, dst4_rw_addr.sin_addr.s_addr),
765 		BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7,
766 			    offsetof(struct bpf_sock_addr, user_ip4)),
767 
768 		/*      user_port = dst4_rw_addr.sin_port */
769 		BPF_MOV32_IMM(BPF_REG_7, dst4_rw_addr.sin_port),
770 		BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7,
771 			    offsetof(struct bpf_sock_addr, user_port)),
772 		/* } */
773 
774 		/* return 1 */
775 		BPF_MOV64_IMM(BPF_REG_0, 1),
776 		BPF_EXIT_INSN(),
777 	};
778 
779 	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
780 }
781 
782 static int sendmsg4_rw_c_prog_load(const struct sock_addr_test *test)
783 {
784 	return load_path(test, SENDMSG4_PROG_PATH);
785 }
786 
787 static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test *test,
788 					 const char *rw_dst_ip)
789 {
790 	struct sockaddr_in6 dst6_rw_addr;
791 	struct in6_addr src6_rw_ip;
792 
793 	if (inet_pton(AF_INET6, SRC6_REWRITE_IP, (void *)&src6_rw_ip) != 1) {
794 		log_err("Invalid IPv6: %s", SRC6_REWRITE_IP);
795 		return -1;
796 	}
797 
798 	if (mk_sockaddr(AF_INET6, rw_dst_ip, SERV6_REWRITE_PORT,
799 			(struct sockaddr *)&dst6_rw_addr,
800 			sizeof(dst6_rw_addr)) == -1)
801 		return -1;
802 
803 	struct bpf_insn insns[] = {
804 		BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
805 
806 		/* if (sk.family == AF_INET6) { */
807 		BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
808 			    offsetof(struct bpf_sock_addr, family)),
809 		BPF_JMP_IMM(BPF_JNE, BPF_REG_7, AF_INET6, 18),
810 
811 #define STORE_IPV6_WORD_N(DST, SRC, N)					       \
812 		BPF_MOV32_IMM(BPF_REG_7, SRC[N]),			       \
813 		BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7,		       \
814 			    offsetof(struct bpf_sock_addr, DST[N]))
815 
816 #define STORE_IPV6(DST, SRC)						       \
817 		STORE_IPV6_WORD_N(DST, SRC, 0),				       \
818 		STORE_IPV6_WORD_N(DST, SRC, 1),				       \
819 		STORE_IPV6_WORD_N(DST, SRC, 2),				       \
820 		STORE_IPV6_WORD_N(DST, SRC, 3)
821 
822 		STORE_IPV6(msg_src_ip6, src6_rw_ip.s6_addr32),
823 		STORE_IPV6(user_ip6, dst6_rw_addr.sin6_addr.s6_addr32),
824 
825 		/*      user_port = dst6_rw_addr.sin6_port */
826 		BPF_MOV32_IMM(BPF_REG_7, dst6_rw_addr.sin6_port),
827 		BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7,
828 			    offsetof(struct bpf_sock_addr, user_port)),
829 
830 		/* } */
831 
832 		/* return 1 */
833 		BPF_MOV64_IMM(BPF_REG_0, 1),
834 		BPF_EXIT_INSN(),
835 	};
836 
837 	return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
838 }
839 
840 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test)
841 {
842 	return sendmsg6_rw_dst_asm_prog_load(test, SERV6_REWRITE_IP);
843 }
844 
845 static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test *test)
846 {
847 	return sendmsg6_rw_dst_asm_prog_load(test, SERV6_V4MAPPED_IP);
848 }
849 
850 static int sendmsg6_rw_c_prog_load(const struct sock_addr_test *test)
851 {
852 	return load_path(test, SENDMSG6_PROG_PATH);
853 }
854 
855 static int cmp_addr(const struct sockaddr_storage *addr1,
856 		    const struct sockaddr_storage *addr2, int cmp_port)
857 {
858 	const struct sockaddr_in *four1, *four2;
859 	const struct sockaddr_in6 *six1, *six2;
860 
861 	if (addr1->ss_family != addr2->ss_family)
862 		return -1;
863 
864 	if (addr1->ss_family == AF_INET) {
865 		four1 = (const struct sockaddr_in *)addr1;
866 		four2 = (const struct sockaddr_in *)addr2;
867 		return !((four1->sin_port == four2->sin_port || !cmp_port) &&
868 			 four1->sin_addr.s_addr == four2->sin_addr.s_addr);
869 	} else if (addr1->ss_family == AF_INET6) {
870 		six1 = (const struct sockaddr_in6 *)addr1;
871 		six2 = (const struct sockaddr_in6 *)addr2;
872 		return !((six1->sin6_port == six2->sin6_port || !cmp_port) &&
873 			 !memcmp(&six1->sin6_addr, &six2->sin6_addr,
874 				 sizeof(struct in6_addr)));
875 	}
876 
877 	return -1;
878 }
879 
880 static int cmp_sock_addr(info_fn fn, int sock1,
881 			 const struct sockaddr_storage *addr2, int cmp_port)
882 {
883 	struct sockaddr_storage addr1;
884 	socklen_t len1 = sizeof(addr1);
885 
886 	memset(&addr1, 0, len1);
887 	if (fn(sock1, (struct sockaddr *)&addr1, (socklen_t *)&len1) != 0)
888 		return -1;
889 
890 	return cmp_addr(&addr1, addr2, cmp_port);
891 }
892 
893 static int cmp_local_ip(int sock1, const struct sockaddr_storage *addr2)
894 {
895 	return cmp_sock_addr(getsockname, sock1, addr2, /*cmp_port*/ 0);
896 }
897 
898 static int cmp_local_addr(int sock1, const struct sockaddr_storage *addr2)
899 {
900 	return cmp_sock_addr(getsockname, sock1, addr2, /*cmp_port*/ 1);
901 }
902 
903 static int cmp_peer_addr(int sock1, const struct sockaddr_storage *addr2)
904 {
905 	return cmp_sock_addr(getpeername, sock1, addr2, /*cmp_port*/ 1);
906 }
907 
908 static int start_server(int type, const struct sockaddr_storage *addr,
909 			socklen_t addr_len)
910 {
911 	int fd;
912 
913 	fd = socket(addr->ss_family, type, 0);
914 	if (fd == -1) {
915 		log_err("Failed to create server socket");
916 		goto out;
917 	}
918 
919 	if (bind(fd, (const struct sockaddr *)addr, addr_len) == -1) {
920 		log_err("Failed to bind server socket");
921 		goto close_out;
922 	}
923 
924 	if (type == SOCK_STREAM) {
925 		if (listen(fd, 128) == -1) {
926 			log_err("Failed to listen on server socket");
927 			goto close_out;
928 		}
929 	}
930 
931 	goto out;
932 close_out:
933 	close(fd);
934 	fd = -1;
935 out:
936 	return fd;
937 }
938 
939 static int connect_to_server(int type, const struct sockaddr_storage *addr,
940 			     socklen_t addr_len)
941 {
942 	int domain;
943 	int fd = -1;
944 
945 	domain = addr->ss_family;
946 
947 	if (domain != AF_INET && domain != AF_INET6) {
948 		log_err("Unsupported address family");
949 		goto err;
950 	}
951 
952 	fd = socket(domain, type, 0);
953 	if (fd == -1) {
954 		log_err("Failed to create client socket");
955 		goto err;
956 	}
957 
958 	if (connect(fd, (const struct sockaddr *)addr, addr_len) == -1) {
959 		log_err("Fail to connect to server");
960 		goto err;
961 	}
962 
963 	goto out;
964 err:
965 	close(fd);
966 	fd = -1;
967 out:
968 	return fd;
969 }
970 
971 int init_pktinfo(int domain, struct cmsghdr *cmsg)
972 {
973 	struct in6_pktinfo *pktinfo6;
974 	struct in_pktinfo *pktinfo4;
975 
976 	if (domain == AF_INET) {
977 		cmsg->cmsg_level = SOL_IP;
978 		cmsg->cmsg_type = IP_PKTINFO;
979 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
980 		pktinfo4 = (struct in_pktinfo *)CMSG_DATA(cmsg);
981 		memset(pktinfo4, 0, sizeof(struct in_pktinfo));
982 		if (inet_pton(domain, SRC4_IP,
983 			      (void *)&pktinfo4->ipi_spec_dst) != 1)
984 			return -1;
985 	} else if (domain == AF_INET6) {
986 		cmsg->cmsg_level = SOL_IPV6;
987 		cmsg->cmsg_type = IPV6_PKTINFO;
988 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
989 		pktinfo6 = (struct in6_pktinfo *)CMSG_DATA(cmsg);
990 		memset(pktinfo6, 0, sizeof(struct in6_pktinfo));
991 		if (inet_pton(domain, SRC6_IP,
992 			      (void *)&pktinfo6->ipi6_addr) != 1)
993 			return -1;
994 	} else {
995 		return -1;
996 	}
997 
998 	return 0;
999 }
1000 
1001 static int sendmsg_to_server(const struct sockaddr_storage *addr,
1002 			     socklen_t addr_len, int set_cmsg, int *syscall_err)
1003 {
1004 	union {
1005 		char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
1006 		struct cmsghdr align;
1007 	} control6;
1008 	union {
1009 		char buf[CMSG_SPACE(sizeof(struct in_pktinfo))];
1010 		struct cmsghdr align;
1011 	} control4;
1012 	struct msghdr hdr;
1013 	struct iovec iov;
1014 	char data = 'a';
1015 	int domain;
1016 	int fd = -1;
1017 
1018 	domain = addr->ss_family;
1019 
1020 	if (domain != AF_INET && domain != AF_INET6) {
1021 		log_err("Unsupported address family");
1022 		goto err;
1023 	}
1024 
1025 	fd = socket(domain, SOCK_DGRAM, 0);
1026 	if (fd == -1) {
1027 		log_err("Failed to create client socket");
1028 		goto err;
1029 	}
1030 
1031 	memset(&iov, 0, sizeof(iov));
1032 	iov.iov_base = &data;
1033 	iov.iov_len = sizeof(data);
1034 
1035 	memset(&hdr, 0, sizeof(hdr));
1036 	hdr.msg_name = (void *)addr;
1037 	hdr.msg_namelen = addr_len;
1038 	hdr.msg_iov = &iov;
1039 	hdr.msg_iovlen = 1;
1040 
1041 	if (set_cmsg) {
1042 		if (domain == AF_INET) {
1043 			hdr.msg_control = &control4;
1044 			hdr.msg_controllen = sizeof(control4.buf);
1045 		} else if (domain == AF_INET6) {
1046 			hdr.msg_control = &control6;
1047 			hdr.msg_controllen = sizeof(control6.buf);
1048 		}
1049 		if (init_pktinfo(domain, CMSG_FIRSTHDR(&hdr))) {
1050 			log_err("Fail to init pktinfo");
1051 			goto err;
1052 		}
1053 	}
1054 
1055 	if (sendmsg(fd, &hdr, 0) != sizeof(data)) {
1056 		log_err("Fail to send message to server");
1057 		*syscall_err = errno;
1058 		goto err;
1059 	}
1060 
1061 	goto out;
1062 err:
1063 	close(fd);
1064 	fd = -1;
1065 out:
1066 	return fd;
1067 }
1068 
1069 static int recvmsg_from_client(int sockfd, struct sockaddr_storage *src_addr)
1070 {
1071 	struct timeval tv;
1072 	struct msghdr hdr;
1073 	struct iovec iov;
1074 	char data[64];
1075 	fd_set rfds;
1076 
1077 	FD_ZERO(&rfds);
1078 	FD_SET(sockfd, &rfds);
1079 
1080 	tv.tv_sec = 2;
1081 	tv.tv_usec = 0;
1082 
1083 	if (select(sockfd + 1, &rfds, NULL, NULL, &tv) <= 0 ||
1084 	    !FD_ISSET(sockfd, &rfds))
1085 		return -1;
1086 
1087 	memset(&iov, 0, sizeof(iov));
1088 	iov.iov_base = data;
1089 	iov.iov_len = sizeof(data);
1090 
1091 	memset(&hdr, 0, sizeof(hdr));
1092 	hdr.msg_name = src_addr;
1093 	hdr.msg_namelen = sizeof(struct sockaddr_storage);
1094 	hdr.msg_iov = &iov;
1095 	hdr.msg_iovlen = 1;
1096 
1097 	return recvmsg(sockfd, &hdr, 0);
1098 }
1099 
1100 static int init_addrs(const struct sock_addr_test *test,
1101 		      struct sockaddr_storage *requested_addr,
1102 		      struct sockaddr_storage *expected_addr,
1103 		      struct sockaddr_storage *expected_src_addr)
1104 {
1105 	socklen_t addr_len = sizeof(struct sockaddr_storage);
1106 
1107 	if (mk_sockaddr(test->domain, test->expected_ip, test->expected_port,
1108 			(struct sockaddr *)expected_addr, addr_len) == -1)
1109 		goto err;
1110 
1111 	if (mk_sockaddr(test->domain, test->requested_ip, test->requested_port,
1112 			(struct sockaddr *)requested_addr, addr_len) == -1)
1113 		goto err;
1114 
1115 	if (test->expected_src_ip &&
1116 	    mk_sockaddr(test->domain, test->expected_src_ip, 0,
1117 			(struct sockaddr *)expected_src_addr, addr_len) == -1)
1118 		goto err;
1119 
1120 	return 0;
1121 err:
1122 	return -1;
1123 }
1124 
1125 static int run_bind_test_case(const struct sock_addr_test *test)
1126 {
1127 	socklen_t addr_len = sizeof(struct sockaddr_storage);
1128 	struct sockaddr_storage requested_addr;
1129 	struct sockaddr_storage expected_addr;
1130 	int clientfd = -1;
1131 	int servfd = -1;
1132 	int err = 0;
1133 
1134 	if (init_addrs(test, &requested_addr, &expected_addr, NULL))
1135 		goto err;
1136 
1137 	servfd = start_server(test->type, &requested_addr, addr_len);
1138 	if (servfd == -1)
1139 		goto err;
1140 
1141 	if (cmp_local_addr(servfd, &expected_addr))
1142 		goto err;
1143 
1144 	/* Try to connect to server just in case */
1145 	clientfd = connect_to_server(test->type, &expected_addr, addr_len);
1146 	if (clientfd == -1)
1147 		goto err;
1148 
1149 	goto out;
1150 err:
1151 	err = -1;
1152 out:
1153 	close(clientfd);
1154 	close(servfd);
1155 	return err;
1156 }
1157 
1158 static int run_connect_test_case(const struct sock_addr_test *test)
1159 {
1160 	socklen_t addr_len = sizeof(struct sockaddr_storage);
1161 	struct sockaddr_storage expected_src_addr;
1162 	struct sockaddr_storage requested_addr;
1163 	struct sockaddr_storage expected_addr;
1164 	int clientfd = -1;
1165 	int servfd = -1;
1166 	int err = 0;
1167 
1168 	if (init_addrs(test, &requested_addr, &expected_addr,
1169 		       &expected_src_addr))
1170 		goto err;
1171 
1172 	/* Prepare server to connect to */
1173 	servfd = start_server(test->type, &expected_addr, addr_len);
1174 	if (servfd == -1)
1175 		goto err;
1176 
1177 	clientfd = connect_to_server(test->type, &requested_addr, addr_len);
1178 	if (clientfd == -1)
1179 		goto err;
1180 
1181 	/* Make sure src and dst addrs were overridden properly */
1182 	if (cmp_peer_addr(clientfd, &expected_addr))
1183 		goto err;
1184 
1185 	if (cmp_local_ip(clientfd, &expected_src_addr))
1186 		goto err;
1187 
1188 	goto out;
1189 err:
1190 	err = -1;
1191 out:
1192 	close(clientfd);
1193 	close(servfd);
1194 	return err;
1195 }
1196 
1197 static int run_sendmsg_test_case(const struct sock_addr_test *test)
1198 {
1199 	socklen_t addr_len = sizeof(struct sockaddr_storage);
1200 	struct sockaddr_storage expected_src_addr;
1201 	struct sockaddr_storage requested_addr;
1202 	struct sockaddr_storage expected_addr;
1203 	struct sockaddr_storage real_src_addr;
1204 	int clientfd = -1;
1205 	int servfd = -1;
1206 	int set_cmsg;
1207 	int err = 0;
1208 
1209 	if (test->type != SOCK_DGRAM)
1210 		goto err;
1211 
1212 	if (init_addrs(test, &requested_addr, &expected_addr,
1213 		       &expected_src_addr))
1214 		goto err;
1215 
1216 	/* Prepare server to sendmsg to */
1217 	servfd = start_server(test->type, &expected_addr, addr_len);
1218 	if (servfd == -1)
1219 		goto err;
1220 
1221 	for (set_cmsg = 0; set_cmsg <= 1; ++set_cmsg) {
1222 		if (clientfd >= 0)
1223 			close(clientfd);
1224 
1225 		clientfd = sendmsg_to_server(&requested_addr, addr_len,
1226 					     set_cmsg, &err);
1227 		if (err)
1228 			goto out;
1229 		else if (clientfd == -1)
1230 			goto err;
1231 
1232 		/* Try to receive message on server instead of using
1233 		 * getpeername(2) on client socket, to check that client's
1234 		 * destination address was rewritten properly, since
1235 		 * getpeername(2) doesn't work with unconnected datagram
1236 		 * sockets.
1237 		 *
1238 		 * Get source address from recvmsg(2) as well to make sure
1239 		 * source was rewritten properly: getsockname(2) can't be used
1240 		 * since socket is unconnected and source defined for one
1241 		 * specific packet may differ from the one used by default and
1242 		 * returned by getsockname(2).
1243 		 */
1244 		if (recvmsg_from_client(servfd, &real_src_addr) == -1)
1245 			goto err;
1246 
1247 		if (cmp_addr(&real_src_addr, &expected_src_addr, /*cmp_port*/0))
1248 			goto err;
1249 	}
1250 
1251 	goto out;
1252 err:
1253 	err = -1;
1254 out:
1255 	close(clientfd);
1256 	close(servfd);
1257 	return err;
1258 }
1259 
1260 static int run_test_case(int cgfd, const struct sock_addr_test *test)
1261 {
1262 	int progfd = -1;
1263 	int err = 0;
1264 
1265 	printf("Test case: %s .. ", test->descr);
1266 
1267 	progfd = test->loadfn(test);
1268 	if (test->expected_result == LOAD_REJECT && progfd < 0)
1269 		goto out;
1270 	else if (test->expected_result == LOAD_REJECT || progfd < 0)
1271 		goto err;
1272 
1273 	err = bpf_prog_attach(progfd, cgfd, test->attach_type,
1274 			      BPF_F_ALLOW_OVERRIDE);
1275 	if (test->expected_result == ATTACH_REJECT && err) {
1276 		err = 0; /* error was expected, reset it */
1277 		goto out;
1278 	} else if (test->expected_result == ATTACH_REJECT || err) {
1279 		goto err;
1280 	}
1281 
1282 	switch (test->attach_type) {
1283 	case BPF_CGROUP_INET4_BIND:
1284 	case BPF_CGROUP_INET6_BIND:
1285 		err = run_bind_test_case(test);
1286 		break;
1287 	case BPF_CGROUP_INET4_CONNECT:
1288 	case BPF_CGROUP_INET6_CONNECT:
1289 		err = run_connect_test_case(test);
1290 		break;
1291 	case BPF_CGROUP_UDP4_SENDMSG:
1292 	case BPF_CGROUP_UDP6_SENDMSG:
1293 		err = run_sendmsg_test_case(test);
1294 		break;
1295 	default:
1296 		goto err;
1297 	}
1298 
1299 	if (test->expected_result == SYSCALL_EPERM && err == EPERM) {
1300 		err = 0; /* error was expected, reset it */
1301 		goto out;
1302 	}
1303 
1304 	if (test->expected_result == SYSCALL_ENOTSUPP && err == ENOTSUPP) {
1305 		err = 0; /* error was expected, reset it */
1306 		goto out;
1307 	}
1308 
1309 	if (err || test->expected_result != SUCCESS)
1310 		goto err;
1311 
1312 	goto out;
1313 err:
1314 	err = -1;
1315 out:
1316 	/* Detaching w/o checking return code: best effort attempt. */
1317 	if (progfd != -1)
1318 		bpf_prog_detach(cgfd, test->attach_type);
1319 	close(progfd);
1320 	printf("[%s]\n", err ? "FAIL" : "PASS");
1321 	return err;
1322 }
1323 
1324 static int run_tests(int cgfd)
1325 {
1326 	int passes = 0;
1327 	int fails = 0;
1328 	int i;
1329 
1330 	for (i = 0; i < ARRAY_SIZE(tests); ++i) {
1331 		if (run_test_case(cgfd, &tests[i]))
1332 			++fails;
1333 		else
1334 			++passes;
1335 	}
1336 	printf("Summary: %d PASSED, %d FAILED\n", passes, fails);
1337 	return fails ? -1 : 0;
1338 }
1339 
1340 int main(int argc, char **argv)
1341 {
1342 	int cgfd = -1;
1343 	int err = 0;
1344 
1345 	if (argc < 2) {
1346 		fprintf(stderr,
1347 			"%s has to be run via %s.sh. Skip direct run.\n",
1348 			argv[0], argv[0]);
1349 		exit(err);
1350 	}
1351 
1352 	if (setup_cgroup_environment())
1353 		goto err;
1354 
1355 	cgfd = create_and_get_cgroup(CG_PATH);
1356 	if (!cgfd)
1357 		goto err;
1358 
1359 	if (join_cgroup(CG_PATH))
1360 		goto err;
1361 
1362 	if (run_tests(cgfd))
1363 		goto err;
1364 
1365 	goto out;
1366 err:
1367 	err = -1;
1368 out:
1369 	close(cgfd);
1370 	cleanup_cgroup_environment();
1371 	return err;
1372 }
1373