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 #include "bpf_util.h" 24 25 #ifndef ENOTSUPP 26 # define ENOTSUPP 524 27 #endif 28 29 #define CG_PATH "/foo" 30 #define CONNECT4_PROG_PATH "./connect4_prog.o" 31 #define CONNECT6_PROG_PATH "./connect6_prog.o" 32 #define SENDMSG4_PROG_PATH "./sendmsg4_prog.o" 33 #define SENDMSG6_PROG_PATH "./sendmsg6_prog.o" 34 35 #define SERV4_IP "192.168.1.254" 36 #define SERV4_REWRITE_IP "127.0.0.1" 37 #define SRC4_IP "172.16.0.1" 38 #define SRC4_REWRITE_IP "127.0.0.4" 39 #define SERV4_PORT 4040 40 #define SERV4_REWRITE_PORT 4444 41 42 #define SERV6_IP "face:b00c:1234:5678::abcd" 43 #define SERV6_REWRITE_IP "::1" 44 #define SERV6_V4MAPPED_IP "::ffff:192.168.0.4" 45 #define SRC6_IP "::1" 46 #define SRC6_REWRITE_IP "::6" 47 #define WILDCARD6_IP "::" 48 #define SERV6_PORT 6060 49 #define SERV6_REWRITE_PORT 6666 50 51 #define INET_NTOP_BUF 40 52 53 struct sock_addr_test; 54 55 typedef int (*load_fn)(const struct sock_addr_test *test); 56 typedef int (*info_fn)(int, struct sockaddr *, socklen_t *); 57 58 char bpf_log_buf[BPF_LOG_BUF_SIZE]; 59 60 struct sock_addr_test { 61 const char *descr; 62 /* BPF prog properties */ 63 load_fn loadfn; 64 enum bpf_attach_type expected_attach_type; 65 enum bpf_attach_type attach_type; 66 /* Socket properties */ 67 int domain; 68 int type; 69 /* IP:port pairs for BPF prog to override */ 70 const char *requested_ip; 71 unsigned short requested_port; 72 const char *expected_ip; 73 unsigned short expected_port; 74 const char *expected_src_ip; 75 /* Expected test result */ 76 enum { 77 LOAD_REJECT, 78 ATTACH_REJECT, 79 SYSCALL_EPERM, 80 SYSCALL_ENOTSUPP, 81 SUCCESS, 82 } expected_result; 83 }; 84 85 static int bind4_prog_load(const struct sock_addr_test *test); 86 static int bind6_prog_load(const struct sock_addr_test *test); 87 static int connect4_prog_load(const struct sock_addr_test *test); 88 static int connect6_prog_load(const struct sock_addr_test *test); 89 static int sendmsg_allow_prog_load(const struct sock_addr_test *test); 90 static int sendmsg_deny_prog_load(const struct sock_addr_test *test); 91 static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test); 92 static int sendmsg4_rw_c_prog_load(const struct sock_addr_test *test); 93 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test); 94 static int sendmsg6_rw_c_prog_load(const struct sock_addr_test *test); 95 static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test *test); 96 static int sendmsg6_rw_wildcard_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: set dst IP = [::] (BSD'ism)", 470 sendmsg6_rw_wildcard_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 SUCCESS, 481 }, 482 { 483 "sendmsg6: preserve dst IP = [::] (BSD'ism)", 484 sendmsg_allow_prog_load, 485 BPF_CGROUP_UDP6_SENDMSG, 486 BPF_CGROUP_UDP6_SENDMSG, 487 AF_INET6, 488 SOCK_DGRAM, 489 WILDCARD6_IP, 490 SERV6_PORT, 491 SERV6_REWRITE_IP, 492 SERV6_PORT, 493 SRC6_IP, 494 SUCCESS, 495 }, 496 { 497 "sendmsg6: deny call", 498 sendmsg_deny_prog_load, 499 BPF_CGROUP_UDP6_SENDMSG, 500 BPF_CGROUP_UDP6_SENDMSG, 501 AF_INET6, 502 SOCK_DGRAM, 503 SERV6_IP, 504 SERV6_PORT, 505 SERV6_REWRITE_IP, 506 SERV6_REWRITE_PORT, 507 SRC6_REWRITE_IP, 508 SYSCALL_EPERM, 509 }, 510 }; 511 512 static int mk_sockaddr(int domain, const char *ip, unsigned short port, 513 struct sockaddr *addr, socklen_t addr_len) 514 { 515 struct sockaddr_in6 *addr6; 516 struct sockaddr_in *addr4; 517 518 if (domain != AF_INET && domain != AF_INET6) { 519 log_err("Unsupported address family"); 520 return -1; 521 } 522 523 memset(addr, 0, addr_len); 524 525 if (domain == AF_INET) { 526 if (addr_len < sizeof(struct sockaddr_in)) 527 return -1; 528 addr4 = (struct sockaddr_in *)addr; 529 addr4->sin_family = domain; 530 addr4->sin_port = htons(port); 531 if (inet_pton(domain, ip, (void *)&addr4->sin_addr) != 1) { 532 log_err("Invalid IPv4: %s", ip); 533 return -1; 534 } 535 } else if (domain == AF_INET6) { 536 if (addr_len < sizeof(struct sockaddr_in6)) 537 return -1; 538 addr6 = (struct sockaddr_in6 *)addr; 539 addr6->sin6_family = domain; 540 addr6->sin6_port = htons(port); 541 if (inet_pton(domain, ip, (void *)&addr6->sin6_addr) != 1) { 542 log_err("Invalid IPv6: %s", ip); 543 return -1; 544 } 545 } 546 547 return 0; 548 } 549 550 static int load_insns(const struct sock_addr_test *test, 551 const struct bpf_insn *insns, size_t insns_cnt) 552 { 553 struct bpf_load_program_attr load_attr; 554 int ret; 555 556 memset(&load_attr, 0, sizeof(struct bpf_load_program_attr)); 557 load_attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; 558 load_attr.expected_attach_type = test->expected_attach_type; 559 load_attr.insns = insns; 560 load_attr.insns_cnt = insns_cnt; 561 load_attr.license = "GPL"; 562 563 ret = bpf_load_program_xattr(&load_attr, bpf_log_buf, BPF_LOG_BUF_SIZE); 564 if (ret < 0 && test->expected_result != LOAD_REJECT) { 565 log_err(">>> Loading program error.\n" 566 ">>> Verifier output:\n%s\n-------\n", bpf_log_buf); 567 } 568 569 return ret; 570 } 571 572 /* [1] These testing programs try to read different context fields, including 573 * narrow loads of different sizes from user_ip4 and user_ip6, and write to 574 * those allowed to be overridden. 575 * 576 * [2] BPF_LD_IMM64 & BPF_JMP_REG are used below whenever there is a need to 577 * compare a register with unsigned 32bit integer. BPF_JMP_IMM can't be used 578 * in such cases since it accepts only _signed_ 32bit integer as IMM 579 * argument. Also note that BPF_LD_IMM64 contains 2 instructions what matters 580 * to count jumps properly. 581 */ 582 583 static int bind4_prog_load(const struct sock_addr_test *test) 584 { 585 union { 586 uint8_t u4_addr8[4]; 587 uint16_t u4_addr16[2]; 588 uint32_t u4_addr32; 589 } ip4; 590 struct sockaddr_in addr4_rw; 591 592 if (inet_pton(AF_INET, SERV4_IP, (void *)&ip4) != 1) { 593 log_err("Invalid IPv4: %s", SERV4_IP); 594 return -1; 595 } 596 597 if (mk_sockaddr(AF_INET, SERV4_REWRITE_IP, SERV4_REWRITE_PORT, 598 (struct sockaddr *)&addr4_rw, sizeof(addr4_rw)) == -1) 599 return -1; 600 601 /* See [1]. */ 602 struct bpf_insn insns[] = { 603 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), 604 605 /* if (sk.family == AF_INET && */ 606 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6, 607 offsetof(struct bpf_sock_addr, family)), 608 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, AF_INET, 24), 609 610 /* (sk.type == SOCK_DGRAM || sk.type == SOCK_STREAM) && */ 611 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6, 612 offsetof(struct bpf_sock_addr, type)), 613 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, SOCK_DGRAM, 1), 614 BPF_JMP_A(1), 615 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, SOCK_STREAM, 20), 616 617 /* 1st_byte_of_user_ip4 == expected && */ 618 BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_6, 619 offsetof(struct bpf_sock_addr, user_ip4)), 620 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr8[0], 18), 621 622 /* 2nd_byte_of_user_ip4 == expected && */ 623 BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_6, 624 offsetof(struct bpf_sock_addr, user_ip4) + 1), 625 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr8[1], 16), 626 627 /* 3rd_byte_of_user_ip4 == expected && */ 628 BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_6, 629 offsetof(struct bpf_sock_addr, user_ip4) + 2), 630 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr8[2], 14), 631 632 /* 4th_byte_of_user_ip4 == expected && */ 633 BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_6, 634 offsetof(struct bpf_sock_addr, user_ip4) + 3), 635 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr8[3], 12), 636 637 /* 1st_half_of_user_ip4 == expected && */ 638 BPF_LDX_MEM(BPF_H, BPF_REG_7, BPF_REG_6, 639 offsetof(struct bpf_sock_addr, user_ip4)), 640 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr16[0], 10), 641 642 /* 2nd_half_of_user_ip4 == expected && */ 643 BPF_LDX_MEM(BPF_H, BPF_REG_7, BPF_REG_6, 644 offsetof(struct bpf_sock_addr, user_ip4) + 2), 645 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr16[1], 8), 646 647 /* whole_user_ip4 == expected) { */ 648 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6, 649 offsetof(struct bpf_sock_addr, user_ip4)), 650 BPF_LD_IMM64(BPF_REG_8, ip4.u4_addr32), /* See [2]. */ 651 BPF_JMP_REG(BPF_JNE, BPF_REG_7, BPF_REG_8, 4), 652 653 /* user_ip4 = addr4_rw.sin_addr */ 654 BPF_MOV32_IMM(BPF_REG_7, addr4_rw.sin_addr.s_addr), 655 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7, 656 offsetof(struct bpf_sock_addr, user_ip4)), 657 658 /* user_port = addr4_rw.sin_port */ 659 BPF_MOV32_IMM(BPF_REG_7, addr4_rw.sin_port), 660 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7, 661 offsetof(struct bpf_sock_addr, user_port)), 662 /* } */ 663 664 /* return 1 */ 665 BPF_MOV64_IMM(BPF_REG_0, 1), 666 BPF_EXIT_INSN(), 667 }; 668 669 return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn)); 670 } 671 672 static int bind6_prog_load(const struct sock_addr_test *test) 673 { 674 struct sockaddr_in6 addr6_rw; 675 struct in6_addr ip6; 676 677 if (inet_pton(AF_INET6, SERV6_IP, (void *)&ip6) != 1) { 678 log_err("Invalid IPv6: %s", SERV6_IP); 679 return -1; 680 } 681 682 if (mk_sockaddr(AF_INET6, SERV6_REWRITE_IP, SERV6_REWRITE_PORT, 683 (struct sockaddr *)&addr6_rw, sizeof(addr6_rw)) == -1) 684 return -1; 685 686 /* See [1]. */ 687 struct bpf_insn insns[] = { 688 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), 689 690 /* if (sk.family == AF_INET6 && */ 691 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6, 692 offsetof(struct bpf_sock_addr, family)), 693 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, AF_INET6, 18), 694 695 /* 5th_byte_of_user_ip6 == expected && */ 696 BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_6, 697 offsetof(struct bpf_sock_addr, user_ip6[1])), 698 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip6.s6_addr[4], 16), 699 700 /* 3rd_half_of_user_ip6 == expected && */ 701 BPF_LDX_MEM(BPF_H, BPF_REG_7, BPF_REG_6, 702 offsetof(struct bpf_sock_addr, user_ip6[1])), 703 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip6.s6_addr16[2], 14), 704 705 /* last_word_of_user_ip6 == expected) { */ 706 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6, 707 offsetof(struct bpf_sock_addr, user_ip6[3])), 708 BPF_LD_IMM64(BPF_REG_8, ip6.s6_addr32[3]), /* See [2]. */ 709 BPF_JMP_REG(BPF_JNE, BPF_REG_7, BPF_REG_8, 10), 710 711 712 #define STORE_IPV6_WORD(N) \ 713 BPF_MOV32_IMM(BPF_REG_7, addr6_rw.sin6_addr.s6_addr32[N]), \ 714 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7, \ 715 offsetof(struct bpf_sock_addr, user_ip6[N])) 716 717 /* user_ip6 = addr6_rw.sin6_addr */ 718 STORE_IPV6_WORD(0), 719 STORE_IPV6_WORD(1), 720 STORE_IPV6_WORD(2), 721 STORE_IPV6_WORD(3), 722 723 /* user_port = addr6_rw.sin6_port */ 724 BPF_MOV32_IMM(BPF_REG_7, addr6_rw.sin6_port), 725 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7, 726 offsetof(struct bpf_sock_addr, user_port)), 727 728 /* } */ 729 730 /* return 1 */ 731 BPF_MOV64_IMM(BPF_REG_0, 1), 732 BPF_EXIT_INSN(), 733 }; 734 735 return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn)); 736 } 737 738 static int load_path(const struct sock_addr_test *test, const char *path) 739 { 740 struct bpf_prog_load_attr attr; 741 struct bpf_object *obj; 742 int prog_fd; 743 744 memset(&attr, 0, sizeof(struct bpf_prog_load_attr)); 745 attr.file = path; 746 attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; 747 attr.expected_attach_type = test->expected_attach_type; 748 749 if (bpf_prog_load_xattr(&attr, &obj, &prog_fd)) { 750 if (test->expected_result != LOAD_REJECT) 751 log_err(">>> Loading program (%s) error.\n", path); 752 return -1; 753 } 754 755 return prog_fd; 756 } 757 758 static int connect4_prog_load(const struct sock_addr_test *test) 759 { 760 return load_path(test, CONNECT4_PROG_PATH); 761 } 762 763 static int connect6_prog_load(const struct sock_addr_test *test) 764 { 765 return load_path(test, CONNECT6_PROG_PATH); 766 } 767 768 static int sendmsg_ret_only_prog_load(const struct sock_addr_test *test, 769 int32_t rc) 770 { 771 struct bpf_insn insns[] = { 772 /* return rc */ 773 BPF_MOV64_IMM(BPF_REG_0, rc), 774 BPF_EXIT_INSN(), 775 }; 776 return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn)); 777 } 778 779 static int sendmsg_allow_prog_load(const struct sock_addr_test *test) 780 { 781 return sendmsg_ret_only_prog_load(test, /*rc*/ 1); 782 } 783 784 static int sendmsg_deny_prog_load(const struct sock_addr_test *test) 785 { 786 return sendmsg_ret_only_prog_load(test, /*rc*/ 0); 787 } 788 789 static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test) 790 { 791 struct sockaddr_in dst4_rw_addr; 792 struct in_addr src4_rw_ip; 793 794 if (inet_pton(AF_INET, SRC4_REWRITE_IP, (void *)&src4_rw_ip) != 1) { 795 log_err("Invalid IPv4: %s", SRC4_REWRITE_IP); 796 return -1; 797 } 798 799 if (mk_sockaddr(AF_INET, SERV4_REWRITE_IP, SERV4_REWRITE_PORT, 800 (struct sockaddr *)&dst4_rw_addr, 801 sizeof(dst4_rw_addr)) == -1) 802 return -1; 803 804 struct bpf_insn insns[] = { 805 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), 806 807 /* if (sk.family == AF_INET && */ 808 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6, 809 offsetof(struct bpf_sock_addr, family)), 810 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, AF_INET, 8), 811 812 /* sk.type == SOCK_DGRAM) { */ 813 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6, 814 offsetof(struct bpf_sock_addr, type)), 815 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, SOCK_DGRAM, 6), 816 817 /* msg_src_ip4 = src4_rw_ip */ 818 BPF_MOV32_IMM(BPF_REG_7, src4_rw_ip.s_addr), 819 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7, 820 offsetof(struct bpf_sock_addr, msg_src_ip4)), 821 822 /* user_ip4 = dst4_rw_addr.sin_addr */ 823 BPF_MOV32_IMM(BPF_REG_7, dst4_rw_addr.sin_addr.s_addr), 824 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7, 825 offsetof(struct bpf_sock_addr, user_ip4)), 826 827 /* user_port = dst4_rw_addr.sin_port */ 828 BPF_MOV32_IMM(BPF_REG_7, dst4_rw_addr.sin_port), 829 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7, 830 offsetof(struct bpf_sock_addr, user_port)), 831 /* } */ 832 833 /* return 1 */ 834 BPF_MOV64_IMM(BPF_REG_0, 1), 835 BPF_EXIT_INSN(), 836 }; 837 838 return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn)); 839 } 840 841 static int sendmsg4_rw_c_prog_load(const struct sock_addr_test *test) 842 { 843 return load_path(test, SENDMSG4_PROG_PATH); 844 } 845 846 static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test *test, 847 const char *rw_dst_ip) 848 { 849 struct sockaddr_in6 dst6_rw_addr; 850 struct in6_addr src6_rw_ip; 851 852 if (inet_pton(AF_INET6, SRC6_REWRITE_IP, (void *)&src6_rw_ip) != 1) { 853 log_err("Invalid IPv6: %s", SRC6_REWRITE_IP); 854 return -1; 855 } 856 857 if (mk_sockaddr(AF_INET6, rw_dst_ip, SERV6_REWRITE_PORT, 858 (struct sockaddr *)&dst6_rw_addr, 859 sizeof(dst6_rw_addr)) == -1) 860 return -1; 861 862 struct bpf_insn insns[] = { 863 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), 864 865 /* if (sk.family == AF_INET6) { */ 866 BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6, 867 offsetof(struct bpf_sock_addr, family)), 868 BPF_JMP_IMM(BPF_JNE, BPF_REG_7, AF_INET6, 18), 869 870 #define STORE_IPV6_WORD_N(DST, SRC, N) \ 871 BPF_MOV32_IMM(BPF_REG_7, SRC[N]), \ 872 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7, \ 873 offsetof(struct bpf_sock_addr, DST[N])) 874 875 #define STORE_IPV6(DST, SRC) \ 876 STORE_IPV6_WORD_N(DST, SRC, 0), \ 877 STORE_IPV6_WORD_N(DST, SRC, 1), \ 878 STORE_IPV6_WORD_N(DST, SRC, 2), \ 879 STORE_IPV6_WORD_N(DST, SRC, 3) 880 881 STORE_IPV6(msg_src_ip6, src6_rw_ip.s6_addr32), 882 STORE_IPV6(user_ip6, dst6_rw_addr.sin6_addr.s6_addr32), 883 884 /* user_port = dst6_rw_addr.sin6_port */ 885 BPF_MOV32_IMM(BPF_REG_7, dst6_rw_addr.sin6_port), 886 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7, 887 offsetof(struct bpf_sock_addr, user_port)), 888 889 /* } */ 890 891 /* return 1 */ 892 BPF_MOV64_IMM(BPF_REG_0, 1), 893 BPF_EXIT_INSN(), 894 }; 895 896 return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn)); 897 } 898 899 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test) 900 { 901 return sendmsg6_rw_dst_asm_prog_load(test, SERV6_REWRITE_IP); 902 } 903 904 static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test *test) 905 { 906 return sendmsg6_rw_dst_asm_prog_load(test, SERV6_V4MAPPED_IP); 907 } 908 909 static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test *test) 910 { 911 return sendmsg6_rw_dst_asm_prog_load(test, WILDCARD6_IP); 912 } 913 914 static int sendmsg6_rw_c_prog_load(const struct sock_addr_test *test) 915 { 916 return load_path(test, SENDMSG6_PROG_PATH); 917 } 918 919 static int cmp_addr(const struct sockaddr_storage *addr1, 920 const struct sockaddr_storage *addr2, int cmp_port) 921 { 922 const struct sockaddr_in *four1, *four2; 923 const struct sockaddr_in6 *six1, *six2; 924 925 if (addr1->ss_family != addr2->ss_family) 926 return -1; 927 928 if (addr1->ss_family == AF_INET) { 929 four1 = (const struct sockaddr_in *)addr1; 930 four2 = (const struct sockaddr_in *)addr2; 931 return !((four1->sin_port == four2->sin_port || !cmp_port) && 932 four1->sin_addr.s_addr == four2->sin_addr.s_addr); 933 } else if (addr1->ss_family == AF_INET6) { 934 six1 = (const struct sockaddr_in6 *)addr1; 935 six2 = (const struct sockaddr_in6 *)addr2; 936 return !((six1->sin6_port == six2->sin6_port || !cmp_port) && 937 !memcmp(&six1->sin6_addr, &six2->sin6_addr, 938 sizeof(struct in6_addr))); 939 } 940 941 return -1; 942 } 943 944 static int cmp_sock_addr(info_fn fn, int sock1, 945 const struct sockaddr_storage *addr2, int cmp_port) 946 { 947 struct sockaddr_storage addr1; 948 socklen_t len1 = sizeof(addr1); 949 950 memset(&addr1, 0, len1); 951 if (fn(sock1, (struct sockaddr *)&addr1, (socklen_t *)&len1) != 0) 952 return -1; 953 954 return cmp_addr(&addr1, addr2, cmp_port); 955 } 956 957 static int cmp_local_ip(int sock1, const struct sockaddr_storage *addr2) 958 { 959 return cmp_sock_addr(getsockname, sock1, addr2, /*cmp_port*/ 0); 960 } 961 962 static int cmp_local_addr(int sock1, const struct sockaddr_storage *addr2) 963 { 964 return cmp_sock_addr(getsockname, sock1, addr2, /*cmp_port*/ 1); 965 } 966 967 static int cmp_peer_addr(int sock1, const struct sockaddr_storage *addr2) 968 { 969 return cmp_sock_addr(getpeername, sock1, addr2, /*cmp_port*/ 1); 970 } 971 972 static int start_server(int type, const struct sockaddr_storage *addr, 973 socklen_t addr_len) 974 { 975 int fd; 976 977 fd = socket(addr->ss_family, type, 0); 978 if (fd == -1) { 979 log_err("Failed to create server socket"); 980 goto out; 981 } 982 983 if (bind(fd, (const struct sockaddr *)addr, addr_len) == -1) { 984 log_err("Failed to bind server socket"); 985 goto close_out; 986 } 987 988 if (type == SOCK_STREAM) { 989 if (listen(fd, 128) == -1) { 990 log_err("Failed to listen on server socket"); 991 goto close_out; 992 } 993 } 994 995 goto out; 996 close_out: 997 close(fd); 998 fd = -1; 999 out: 1000 return fd; 1001 } 1002 1003 static int connect_to_server(int type, const struct sockaddr_storage *addr, 1004 socklen_t addr_len) 1005 { 1006 int domain; 1007 int fd = -1; 1008 1009 domain = addr->ss_family; 1010 1011 if (domain != AF_INET && domain != AF_INET6) { 1012 log_err("Unsupported address family"); 1013 goto err; 1014 } 1015 1016 fd = socket(domain, type, 0); 1017 if (fd == -1) { 1018 log_err("Failed to create client socket"); 1019 goto err; 1020 } 1021 1022 if (connect(fd, (const struct sockaddr *)addr, addr_len) == -1) { 1023 log_err("Fail to connect to server"); 1024 goto err; 1025 } 1026 1027 goto out; 1028 err: 1029 close(fd); 1030 fd = -1; 1031 out: 1032 return fd; 1033 } 1034 1035 int init_pktinfo(int domain, struct cmsghdr *cmsg) 1036 { 1037 struct in6_pktinfo *pktinfo6; 1038 struct in_pktinfo *pktinfo4; 1039 1040 if (domain == AF_INET) { 1041 cmsg->cmsg_level = SOL_IP; 1042 cmsg->cmsg_type = IP_PKTINFO; 1043 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); 1044 pktinfo4 = (struct in_pktinfo *)CMSG_DATA(cmsg); 1045 memset(pktinfo4, 0, sizeof(struct in_pktinfo)); 1046 if (inet_pton(domain, SRC4_IP, 1047 (void *)&pktinfo4->ipi_spec_dst) != 1) 1048 return -1; 1049 } else if (domain == AF_INET6) { 1050 cmsg->cmsg_level = SOL_IPV6; 1051 cmsg->cmsg_type = IPV6_PKTINFO; 1052 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); 1053 pktinfo6 = (struct in6_pktinfo *)CMSG_DATA(cmsg); 1054 memset(pktinfo6, 0, sizeof(struct in6_pktinfo)); 1055 if (inet_pton(domain, SRC6_IP, 1056 (void *)&pktinfo6->ipi6_addr) != 1) 1057 return -1; 1058 } else { 1059 return -1; 1060 } 1061 1062 return 0; 1063 } 1064 1065 static int sendmsg_to_server(int type, const struct sockaddr_storage *addr, 1066 socklen_t addr_len, int set_cmsg, int flags, 1067 int *syscall_err) 1068 { 1069 union { 1070 char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; 1071 struct cmsghdr align; 1072 } control6; 1073 union { 1074 char buf[CMSG_SPACE(sizeof(struct in_pktinfo))]; 1075 struct cmsghdr align; 1076 } control4; 1077 struct msghdr hdr; 1078 struct iovec iov; 1079 char data = 'a'; 1080 int domain; 1081 int fd = -1; 1082 1083 domain = addr->ss_family; 1084 1085 if (domain != AF_INET && domain != AF_INET6) { 1086 log_err("Unsupported address family"); 1087 goto err; 1088 } 1089 1090 fd = socket(domain, type, 0); 1091 if (fd == -1) { 1092 log_err("Failed to create client socket"); 1093 goto err; 1094 } 1095 1096 memset(&iov, 0, sizeof(iov)); 1097 iov.iov_base = &data; 1098 iov.iov_len = sizeof(data); 1099 1100 memset(&hdr, 0, sizeof(hdr)); 1101 hdr.msg_name = (void *)addr; 1102 hdr.msg_namelen = addr_len; 1103 hdr.msg_iov = &iov; 1104 hdr.msg_iovlen = 1; 1105 1106 if (set_cmsg) { 1107 if (domain == AF_INET) { 1108 hdr.msg_control = &control4; 1109 hdr.msg_controllen = sizeof(control4.buf); 1110 } else if (domain == AF_INET6) { 1111 hdr.msg_control = &control6; 1112 hdr.msg_controllen = sizeof(control6.buf); 1113 } 1114 if (init_pktinfo(domain, CMSG_FIRSTHDR(&hdr))) { 1115 log_err("Fail to init pktinfo"); 1116 goto err; 1117 } 1118 } 1119 1120 if (sendmsg(fd, &hdr, flags) != sizeof(data)) { 1121 log_err("Fail to send message to server"); 1122 *syscall_err = errno; 1123 goto err; 1124 } 1125 1126 goto out; 1127 err: 1128 close(fd); 1129 fd = -1; 1130 out: 1131 return fd; 1132 } 1133 1134 static int fastconnect_to_server(const struct sockaddr_storage *addr, 1135 socklen_t addr_len) 1136 { 1137 int sendmsg_err; 1138 1139 return sendmsg_to_server(SOCK_STREAM, addr, addr_len, /*set_cmsg*/0, 1140 MSG_FASTOPEN, &sendmsg_err); 1141 } 1142 1143 static int recvmsg_from_client(int sockfd, struct sockaddr_storage *src_addr) 1144 { 1145 struct timeval tv; 1146 struct msghdr hdr; 1147 struct iovec iov; 1148 char data[64]; 1149 fd_set rfds; 1150 1151 FD_ZERO(&rfds); 1152 FD_SET(sockfd, &rfds); 1153 1154 tv.tv_sec = 2; 1155 tv.tv_usec = 0; 1156 1157 if (select(sockfd + 1, &rfds, NULL, NULL, &tv) <= 0 || 1158 !FD_ISSET(sockfd, &rfds)) 1159 return -1; 1160 1161 memset(&iov, 0, sizeof(iov)); 1162 iov.iov_base = data; 1163 iov.iov_len = sizeof(data); 1164 1165 memset(&hdr, 0, sizeof(hdr)); 1166 hdr.msg_name = src_addr; 1167 hdr.msg_namelen = sizeof(struct sockaddr_storage); 1168 hdr.msg_iov = &iov; 1169 hdr.msg_iovlen = 1; 1170 1171 return recvmsg(sockfd, &hdr, 0); 1172 } 1173 1174 static int init_addrs(const struct sock_addr_test *test, 1175 struct sockaddr_storage *requested_addr, 1176 struct sockaddr_storage *expected_addr, 1177 struct sockaddr_storage *expected_src_addr) 1178 { 1179 socklen_t addr_len = sizeof(struct sockaddr_storage); 1180 1181 if (mk_sockaddr(test->domain, test->expected_ip, test->expected_port, 1182 (struct sockaddr *)expected_addr, addr_len) == -1) 1183 goto err; 1184 1185 if (mk_sockaddr(test->domain, test->requested_ip, test->requested_port, 1186 (struct sockaddr *)requested_addr, addr_len) == -1) 1187 goto err; 1188 1189 if (test->expected_src_ip && 1190 mk_sockaddr(test->domain, test->expected_src_ip, 0, 1191 (struct sockaddr *)expected_src_addr, addr_len) == -1) 1192 goto err; 1193 1194 return 0; 1195 err: 1196 return -1; 1197 } 1198 1199 static int run_bind_test_case(const struct sock_addr_test *test) 1200 { 1201 socklen_t addr_len = sizeof(struct sockaddr_storage); 1202 struct sockaddr_storage requested_addr; 1203 struct sockaddr_storage expected_addr; 1204 int clientfd = -1; 1205 int servfd = -1; 1206 int err = 0; 1207 1208 if (init_addrs(test, &requested_addr, &expected_addr, NULL)) 1209 goto err; 1210 1211 servfd = start_server(test->type, &requested_addr, addr_len); 1212 if (servfd == -1) 1213 goto err; 1214 1215 if (cmp_local_addr(servfd, &expected_addr)) 1216 goto err; 1217 1218 /* Try to connect to server just in case */ 1219 clientfd = connect_to_server(test->type, &expected_addr, addr_len); 1220 if (clientfd == -1) 1221 goto err; 1222 1223 goto out; 1224 err: 1225 err = -1; 1226 out: 1227 close(clientfd); 1228 close(servfd); 1229 return err; 1230 } 1231 1232 static int run_connect_test_case(const struct sock_addr_test *test) 1233 { 1234 socklen_t addr_len = sizeof(struct sockaddr_storage); 1235 struct sockaddr_storage expected_src_addr; 1236 struct sockaddr_storage requested_addr; 1237 struct sockaddr_storage expected_addr; 1238 int clientfd = -1; 1239 int servfd = -1; 1240 int err = 0; 1241 1242 if (init_addrs(test, &requested_addr, &expected_addr, 1243 &expected_src_addr)) 1244 goto err; 1245 1246 /* Prepare server to connect to */ 1247 servfd = start_server(test->type, &expected_addr, addr_len); 1248 if (servfd == -1) 1249 goto err; 1250 1251 clientfd = connect_to_server(test->type, &requested_addr, addr_len); 1252 if (clientfd == -1) 1253 goto err; 1254 1255 /* Make sure src and dst addrs were overridden properly */ 1256 if (cmp_peer_addr(clientfd, &expected_addr)) 1257 goto err; 1258 1259 if (cmp_local_ip(clientfd, &expected_src_addr)) 1260 goto err; 1261 1262 if (test->type == SOCK_STREAM) { 1263 /* Test TCP Fast Open scenario */ 1264 clientfd = fastconnect_to_server(&requested_addr, addr_len); 1265 if (clientfd == -1) 1266 goto err; 1267 1268 /* Make sure src and dst addrs were overridden properly */ 1269 if (cmp_peer_addr(clientfd, &expected_addr)) 1270 goto err; 1271 1272 if (cmp_local_ip(clientfd, &expected_src_addr)) 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_sendmsg_test_case(const struct sock_addr_test *test) 1286 { 1287 socklen_t addr_len = sizeof(struct sockaddr_storage); 1288 struct sockaddr_storage expected_src_addr; 1289 struct sockaddr_storage requested_addr; 1290 struct sockaddr_storage expected_addr; 1291 struct sockaddr_storage real_src_addr; 1292 int clientfd = -1; 1293 int servfd = -1; 1294 int set_cmsg; 1295 int err = 0; 1296 1297 if (test->type != SOCK_DGRAM) 1298 goto err; 1299 1300 if (init_addrs(test, &requested_addr, &expected_addr, 1301 &expected_src_addr)) 1302 goto err; 1303 1304 /* Prepare server to sendmsg to */ 1305 servfd = start_server(test->type, &expected_addr, addr_len); 1306 if (servfd == -1) 1307 goto err; 1308 1309 for (set_cmsg = 0; set_cmsg <= 1; ++set_cmsg) { 1310 if (clientfd >= 0) 1311 close(clientfd); 1312 1313 clientfd = sendmsg_to_server(test->type, &requested_addr, 1314 addr_len, set_cmsg, /*flags*/0, 1315 &err); 1316 if (err) 1317 goto out; 1318 else if (clientfd == -1) 1319 goto err; 1320 1321 /* Try to receive message on server instead of using 1322 * getpeername(2) on client socket, to check that client's 1323 * destination address was rewritten properly, since 1324 * getpeername(2) doesn't work with unconnected datagram 1325 * sockets. 1326 * 1327 * Get source address from recvmsg(2) as well to make sure 1328 * source was rewritten properly: getsockname(2) can't be used 1329 * since socket is unconnected and source defined for one 1330 * specific packet may differ from the one used by default and 1331 * returned by getsockname(2). 1332 */ 1333 if (recvmsg_from_client(servfd, &real_src_addr) == -1) 1334 goto err; 1335 1336 if (cmp_addr(&real_src_addr, &expected_src_addr, /*cmp_port*/0)) 1337 goto err; 1338 } 1339 1340 goto out; 1341 err: 1342 err = -1; 1343 out: 1344 close(clientfd); 1345 close(servfd); 1346 return err; 1347 } 1348 1349 static int run_test_case(int cgfd, const struct sock_addr_test *test) 1350 { 1351 int progfd = -1; 1352 int err = 0; 1353 1354 printf("Test case: %s .. ", test->descr); 1355 1356 progfd = test->loadfn(test); 1357 if (test->expected_result == LOAD_REJECT && progfd < 0) 1358 goto out; 1359 else if (test->expected_result == LOAD_REJECT || progfd < 0) 1360 goto err; 1361 1362 err = bpf_prog_attach(progfd, cgfd, test->attach_type, 1363 BPF_F_ALLOW_OVERRIDE); 1364 if (test->expected_result == ATTACH_REJECT && err) { 1365 err = 0; /* error was expected, reset it */ 1366 goto out; 1367 } else if (test->expected_result == ATTACH_REJECT || err) { 1368 goto err; 1369 } 1370 1371 switch (test->attach_type) { 1372 case BPF_CGROUP_INET4_BIND: 1373 case BPF_CGROUP_INET6_BIND: 1374 err = run_bind_test_case(test); 1375 break; 1376 case BPF_CGROUP_INET4_CONNECT: 1377 case BPF_CGROUP_INET6_CONNECT: 1378 err = run_connect_test_case(test); 1379 break; 1380 case BPF_CGROUP_UDP4_SENDMSG: 1381 case BPF_CGROUP_UDP6_SENDMSG: 1382 err = run_sendmsg_test_case(test); 1383 break; 1384 default: 1385 goto err; 1386 } 1387 1388 if (test->expected_result == SYSCALL_EPERM && err == EPERM) { 1389 err = 0; /* error was expected, reset it */ 1390 goto out; 1391 } 1392 1393 if (test->expected_result == SYSCALL_ENOTSUPP && err == ENOTSUPP) { 1394 err = 0; /* error was expected, reset it */ 1395 goto out; 1396 } 1397 1398 if (err || test->expected_result != SUCCESS) 1399 goto err; 1400 1401 goto out; 1402 err: 1403 err = -1; 1404 out: 1405 /* Detaching w/o checking return code: best effort attempt. */ 1406 if (progfd != -1) 1407 bpf_prog_detach(cgfd, test->attach_type); 1408 close(progfd); 1409 printf("[%s]\n", err ? "FAIL" : "PASS"); 1410 return err; 1411 } 1412 1413 static int run_tests(int cgfd) 1414 { 1415 int passes = 0; 1416 int fails = 0; 1417 int i; 1418 1419 for (i = 0; i < ARRAY_SIZE(tests); ++i) { 1420 if (run_test_case(cgfd, &tests[i])) 1421 ++fails; 1422 else 1423 ++passes; 1424 } 1425 printf("Summary: %d PASSED, %d FAILED\n", passes, fails); 1426 return fails ? -1 : 0; 1427 } 1428 1429 int main(int argc, char **argv) 1430 { 1431 int cgfd = -1; 1432 int err = 0; 1433 1434 if (argc < 2) { 1435 fprintf(stderr, 1436 "%s has to be run via %s.sh. Skip direct run.\n", 1437 argv[0], argv[0]); 1438 exit(err); 1439 } 1440 1441 if (setup_cgroup_environment()) 1442 goto err; 1443 1444 cgfd = create_and_get_cgroup(CG_PATH); 1445 if (cgfd < 0) 1446 goto err; 1447 1448 if (join_cgroup(CG_PATH)) 1449 goto err; 1450 1451 if (run_tests(cgfd)) 1452 goto err; 1453 1454 goto out; 1455 err: 1456 err = -1; 1457 out: 1458 close(cgfd); 1459 cleanup_cgroup_environment(); 1460 return err; 1461 } 1462