xref: /openbmc/linux/tools/testing/selftests/bpf/test_sock_addr.c (revision a0ae2562c6c4b2721d9fddba63b7286c13517d9f)
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(int type, const struct sockaddr_storage *addr,
1002 			     socklen_t addr_len, int set_cmsg, int flags,
1003 			     int *syscall_err)
1004 {
1005 	union {
1006 		char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
1007 		struct cmsghdr align;
1008 	} control6;
1009 	union {
1010 		char buf[CMSG_SPACE(sizeof(struct in_pktinfo))];
1011 		struct cmsghdr align;
1012 	} control4;
1013 	struct msghdr hdr;
1014 	struct iovec iov;
1015 	char data = 'a';
1016 	int domain;
1017 	int fd = -1;
1018 
1019 	domain = addr->ss_family;
1020 
1021 	if (domain != AF_INET && domain != AF_INET6) {
1022 		log_err("Unsupported address family");
1023 		goto err;
1024 	}
1025 
1026 	fd = socket(domain, type, 0);
1027 	if (fd == -1) {
1028 		log_err("Failed to create client socket");
1029 		goto err;
1030 	}
1031 
1032 	memset(&iov, 0, sizeof(iov));
1033 	iov.iov_base = &data;
1034 	iov.iov_len = sizeof(data);
1035 
1036 	memset(&hdr, 0, sizeof(hdr));
1037 	hdr.msg_name = (void *)addr;
1038 	hdr.msg_namelen = addr_len;
1039 	hdr.msg_iov = &iov;
1040 	hdr.msg_iovlen = 1;
1041 
1042 	if (set_cmsg) {
1043 		if (domain == AF_INET) {
1044 			hdr.msg_control = &control4;
1045 			hdr.msg_controllen = sizeof(control4.buf);
1046 		} else if (domain == AF_INET6) {
1047 			hdr.msg_control = &control6;
1048 			hdr.msg_controllen = sizeof(control6.buf);
1049 		}
1050 		if (init_pktinfo(domain, CMSG_FIRSTHDR(&hdr))) {
1051 			log_err("Fail to init pktinfo");
1052 			goto err;
1053 		}
1054 	}
1055 
1056 	if (sendmsg(fd, &hdr, flags) != sizeof(data)) {
1057 		log_err("Fail to send message to server");
1058 		*syscall_err = errno;
1059 		goto err;
1060 	}
1061 
1062 	goto out;
1063 err:
1064 	close(fd);
1065 	fd = -1;
1066 out:
1067 	return fd;
1068 }
1069 
1070 static int fastconnect_to_server(const struct sockaddr_storage *addr,
1071 				 socklen_t addr_len)
1072 {
1073 	int sendmsg_err;
1074 
1075 	return sendmsg_to_server(SOCK_STREAM, addr, addr_len, /*set_cmsg*/0,
1076 				 MSG_FASTOPEN, &sendmsg_err);
1077 }
1078 
1079 static int recvmsg_from_client(int sockfd, struct sockaddr_storage *src_addr)
1080 {
1081 	struct timeval tv;
1082 	struct msghdr hdr;
1083 	struct iovec iov;
1084 	char data[64];
1085 	fd_set rfds;
1086 
1087 	FD_ZERO(&rfds);
1088 	FD_SET(sockfd, &rfds);
1089 
1090 	tv.tv_sec = 2;
1091 	tv.tv_usec = 0;
1092 
1093 	if (select(sockfd + 1, &rfds, NULL, NULL, &tv) <= 0 ||
1094 	    !FD_ISSET(sockfd, &rfds))
1095 		return -1;
1096 
1097 	memset(&iov, 0, sizeof(iov));
1098 	iov.iov_base = data;
1099 	iov.iov_len = sizeof(data);
1100 
1101 	memset(&hdr, 0, sizeof(hdr));
1102 	hdr.msg_name = src_addr;
1103 	hdr.msg_namelen = sizeof(struct sockaddr_storage);
1104 	hdr.msg_iov = &iov;
1105 	hdr.msg_iovlen = 1;
1106 
1107 	return recvmsg(sockfd, &hdr, 0);
1108 }
1109 
1110 static int init_addrs(const struct sock_addr_test *test,
1111 		      struct sockaddr_storage *requested_addr,
1112 		      struct sockaddr_storage *expected_addr,
1113 		      struct sockaddr_storage *expected_src_addr)
1114 {
1115 	socklen_t addr_len = sizeof(struct sockaddr_storage);
1116 
1117 	if (mk_sockaddr(test->domain, test->expected_ip, test->expected_port,
1118 			(struct sockaddr *)expected_addr, addr_len) == -1)
1119 		goto err;
1120 
1121 	if (mk_sockaddr(test->domain, test->requested_ip, test->requested_port,
1122 			(struct sockaddr *)requested_addr, addr_len) == -1)
1123 		goto err;
1124 
1125 	if (test->expected_src_ip &&
1126 	    mk_sockaddr(test->domain, test->expected_src_ip, 0,
1127 			(struct sockaddr *)expected_src_addr, addr_len) == -1)
1128 		goto err;
1129 
1130 	return 0;
1131 err:
1132 	return -1;
1133 }
1134 
1135 static int run_bind_test_case(const struct sock_addr_test *test)
1136 {
1137 	socklen_t addr_len = sizeof(struct sockaddr_storage);
1138 	struct sockaddr_storage requested_addr;
1139 	struct sockaddr_storage expected_addr;
1140 	int clientfd = -1;
1141 	int servfd = -1;
1142 	int err = 0;
1143 
1144 	if (init_addrs(test, &requested_addr, &expected_addr, NULL))
1145 		goto err;
1146 
1147 	servfd = start_server(test->type, &requested_addr, addr_len);
1148 	if (servfd == -1)
1149 		goto err;
1150 
1151 	if (cmp_local_addr(servfd, &expected_addr))
1152 		goto err;
1153 
1154 	/* Try to connect to server just in case */
1155 	clientfd = connect_to_server(test->type, &expected_addr, addr_len);
1156 	if (clientfd == -1)
1157 		goto err;
1158 
1159 	goto out;
1160 err:
1161 	err = -1;
1162 out:
1163 	close(clientfd);
1164 	close(servfd);
1165 	return err;
1166 }
1167 
1168 static int run_connect_test_case(const struct sock_addr_test *test)
1169 {
1170 	socklen_t addr_len = sizeof(struct sockaddr_storage);
1171 	struct sockaddr_storage expected_src_addr;
1172 	struct sockaddr_storage requested_addr;
1173 	struct sockaddr_storage expected_addr;
1174 	int clientfd = -1;
1175 	int servfd = -1;
1176 	int err = 0;
1177 
1178 	if (init_addrs(test, &requested_addr, &expected_addr,
1179 		       &expected_src_addr))
1180 		goto err;
1181 
1182 	/* Prepare server to connect to */
1183 	servfd = start_server(test->type, &expected_addr, addr_len);
1184 	if (servfd == -1)
1185 		goto err;
1186 
1187 	clientfd = connect_to_server(test->type, &requested_addr, addr_len);
1188 	if (clientfd == -1)
1189 		goto err;
1190 
1191 	/* Make sure src and dst addrs were overridden properly */
1192 	if (cmp_peer_addr(clientfd, &expected_addr))
1193 		goto err;
1194 
1195 	if (cmp_local_ip(clientfd, &expected_src_addr))
1196 		goto err;
1197 
1198 	if (test->type == SOCK_STREAM) {
1199 		/* Test TCP Fast Open scenario */
1200 		clientfd = fastconnect_to_server(&requested_addr, addr_len);
1201 		if (clientfd == -1)
1202 			goto err;
1203 
1204 		/* Make sure src and dst addrs were overridden properly */
1205 		if (cmp_peer_addr(clientfd, &expected_addr))
1206 			goto err;
1207 
1208 		if (cmp_local_ip(clientfd, &expected_src_addr))
1209 			goto err;
1210 	}
1211 
1212 	goto out;
1213 err:
1214 	err = -1;
1215 out:
1216 	close(clientfd);
1217 	close(servfd);
1218 	return err;
1219 }
1220 
1221 static int run_sendmsg_test_case(const struct sock_addr_test *test)
1222 {
1223 	socklen_t addr_len = sizeof(struct sockaddr_storage);
1224 	struct sockaddr_storage expected_src_addr;
1225 	struct sockaddr_storage requested_addr;
1226 	struct sockaddr_storage expected_addr;
1227 	struct sockaddr_storage real_src_addr;
1228 	int clientfd = -1;
1229 	int servfd = -1;
1230 	int set_cmsg;
1231 	int err = 0;
1232 
1233 	if (test->type != SOCK_DGRAM)
1234 		goto err;
1235 
1236 	if (init_addrs(test, &requested_addr, &expected_addr,
1237 		       &expected_src_addr))
1238 		goto err;
1239 
1240 	/* Prepare server to sendmsg to */
1241 	servfd = start_server(test->type, &expected_addr, addr_len);
1242 	if (servfd == -1)
1243 		goto err;
1244 
1245 	for (set_cmsg = 0; set_cmsg <= 1; ++set_cmsg) {
1246 		if (clientfd >= 0)
1247 			close(clientfd);
1248 
1249 		clientfd = sendmsg_to_server(test->type, &requested_addr,
1250 					     addr_len, set_cmsg, /*flags*/0,
1251 					     &err);
1252 		if (err)
1253 			goto out;
1254 		else if (clientfd == -1)
1255 			goto err;
1256 
1257 		/* Try to receive message on server instead of using
1258 		 * getpeername(2) on client socket, to check that client's
1259 		 * destination address was rewritten properly, since
1260 		 * getpeername(2) doesn't work with unconnected datagram
1261 		 * sockets.
1262 		 *
1263 		 * Get source address from recvmsg(2) as well to make sure
1264 		 * source was rewritten properly: getsockname(2) can't be used
1265 		 * since socket is unconnected and source defined for one
1266 		 * specific packet may differ from the one used by default and
1267 		 * returned by getsockname(2).
1268 		 */
1269 		if (recvmsg_from_client(servfd, &real_src_addr) == -1)
1270 			goto err;
1271 
1272 		if (cmp_addr(&real_src_addr, &expected_src_addr, /*cmp_port*/0))
1273 			goto err;
1274 	}
1275 
1276 	goto out;
1277 err:
1278 	err = -1;
1279 out:
1280 	close(clientfd);
1281 	close(servfd);
1282 	return err;
1283 }
1284 
1285 static int run_test_case(int cgfd, const struct sock_addr_test *test)
1286 {
1287 	int progfd = -1;
1288 	int err = 0;
1289 
1290 	printf("Test case: %s .. ", test->descr);
1291 
1292 	progfd = test->loadfn(test);
1293 	if (test->expected_result == LOAD_REJECT && progfd < 0)
1294 		goto out;
1295 	else if (test->expected_result == LOAD_REJECT || progfd < 0)
1296 		goto err;
1297 
1298 	err = bpf_prog_attach(progfd, cgfd, test->attach_type,
1299 			      BPF_F_ALLOW_OVERRIDE);
1300 	if (test->expected_result == ATTACH_REJECT && err) {
1301 		err = 0; /* error was expected, reset it */
1302 		goto out;
1303 	} else if (test->expected_result == ATTACH_REJECT || err) {
1304 		goto err;
1305 	}
1306 
1307 	switch (test->attach_type) {
1308 	case BPF_CGROUP_INET4_BIND:
1309 	case BPF_CGROUP_INET6_BIND:
1310 		err = run_bind_test_case(test);
1311 		break;
1312 	case BPF_CGROUP_INET4_CONNECT:
1313 	case BPF_CGROUP_INET6_CONNECT:
1314 		err = run_connect_test_case(test);
1315 		break;
1316 	case BPF_CGROUP_UDP4_SENDMSG:
1317 	case BPF_CGROUP_UDP6_SENDMSG:
1318 		err = run_sendmsg_test_case(test);
1319 		break;
1320 	default:
1321 		goto err;
1322 	}
1323 
1324 	if (test->expected_result == SYSCALL_EPERM && err == EPERM) {
1325 		err = 0; /* error was expected, reset it */
1326 		goto out;
1327 	}
1328 
1329 	if (test->expected_result == SYSCALL_ENOTSUPP && err == ENOTSUPP) {
1330 		err = 0; /* error was expected, reset it */
1331 		goto out;
1332 	}
1333 
1334 	if (err || test->expected_result != SUCCESS)
1335 		goto err;
1336 
1337 	goto out;
1338 err:
1339 	err = -1;
1340 out:
1341 	/* Detaching w/o checking return code: best effort attempt. */
1342 	if (progfd != -1)
1343 		bpf_prog_detach(cgfd, test->attach_type);
1344 	close(progfd);
1345 	printf("[%s]\n", err ? "FAIL" : "PASS");
1346 	return err;
1347 }
1348 
1349 static int run_tests(int cgfd)
1350 {
1351 	int passes = 0;
1352 	int fails = 0;
1353 	int i;
1354 
1355 	for (i = 0; i < ARRAY_SIZE(tests); ++i) {
1356 		if (run_test_case(cgfd, &tests[i]))
1357 			++fails;
1358 		else
1359 			++passes;
1360 	}
1361 	printf("Summary: %d PASSED, %d FAILED\n", passes, fails);
1362 	return fails ? -1 : 0;
1363 }
1364 
1365 int main(int argc, char **argv)
1366 {
1367 	int cgfd = -1;
1368 	int err = 0;
1369 
1370 	if (argc < 2) {
1371 		fprintf(stderr,
1372 			"%s has to be run via %s.sh. Skip direct run.\n",
1373 			argv[0], argv[0]);
1374 		exit(err);
1375 	}
1376 
1377 	if (setup_cgroup_environment())
1378 		goto err;
1379 
1380 	cgfd = create_and_get_cgroup(CG_PATH);
1381 	if (!cgfd)
1382 		goto err;
1383 
1384 	if (join_cgroup(CG_PATH))
1385 		goto err;
1386 
1387 	if (run_tests(cgfd))
1388 		goto err;
1389 
1390 	goto out;
1391 err:
1392 	err = -1;
1393 out:
1394 	close(cgfd);
1395 	cleanup_cgroup_environment();
1396 	return err;
1397 }
1398