Skip to content

Commit 0645f7e

Browse files
rdnaAlexei Starovoitov
authored andcommitted
selftests/bpf: Test narrow loads for bpf_sock_addr.user_port
Test 1,2,4-byte loads from bpf_sock_addr.user_port in sock_addr programs. Signed-off-by: Andrey Ignatov <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/bpf/e5c734a58cca4041ab30cb5471e644246f8cdb5a.1589420814.git.rdna@fb.com
1 parent 7aebfa1 commit 0645f7e

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

tools/testing/selftests/bpf/test_sock_addr.c

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -677,14 +677,16 @@ static int bind4_prog_load(const struct sock_addr_test *test)
677677
uint8_t u4_addr8[4];
678678
uint16_t u4_addr16[2];
679679
uint32_t u4_addr32;
680-
} ip4;
680+
} ip4, port;
681681
struct sockaddr_in addr4_rw;
682682

683683
if (inet_pton(AF_INET, SERV4_IP, (void *)&ip4) != 1) {
684684
log_err("Invalid IPv4: %s", SERV4_IP);
685685
return -1;
686686
}
687687

688+
port.u4_addr32 = htons(SERV4_PORT);
689+
688690
if (mk_sockaddr(AF_INET, SERV4_REWRITE_IP, SERV4_REWRITE_PORT,
689691
(struct sockaddr *)&addr4_rw, sizeof(addr4_rw)) == -1)
690692
return -1;
@@ -696,49 +698,65 @@ static int bind4_prog_load(const struct sock_addr_test *test)
696698
/* if (sk.family == AF_INET && */
697699
BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
698700
offsetof(struct bpf_sock_addr, family)),
699-
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, AF_INET, 24),
701+
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, AF_INET, 32),
700702

701703
/* (sk.type == SOCK_DGRAM || sk.type == SOCK_STREAM) && */
702704
BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
703705
offsetof(struct bpf_sock_addr, type)),
704706
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, SOCK_DGRAM, 1),
705707
BPF_JMP_A(1),
706-
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, SOCK_STREAM, 20),
708+
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, SOCK_STREAM, 28),
707709

708710
/* 1st_byte_of_user_ip4 == expected && */
709711
BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_6,
710712
offsetof(struct bpf_sock_addr, user_ip4)),
711-
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr8[0], 18),
713+
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr8[0], 26),
712714

713715
/* 2nd_byte_of_user_ip4 == expected && */
714716
BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_6,
715717
offsetof(struct bpf_sock_addr, user_ip4) + 1),
716-
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr8[1], 16),
718+
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr8[1], 24),
717719

718720
/* 3rd_byte_of_user_ip4 == expected && */
719721
BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_6,
720722
offsetof(struct bpf_sock_addr, user_ip4) + 2),
721-
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr8[2], 14),
723+
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr8[2], 22),
722724

723725
/* 4th_byte_of_user_ip4 == expected && */
724726
BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_6,
725727
offsetof(struct bpf_sock_addr, user_ip4) + 3),
726-
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr8[3], 12),
728+
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr8[3], 20),
727729

728730
/* 1st_half_of_user_ip4 == expected && */
729731
BPF_LDX_MEM(BPF_H, BPF_REG_7, BPF_REG_6,
730732
offsetof(struct bpf_sock_addr, user_ip4)),
731-
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr16[0], 10),
733+
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr16[0], 18),
732734

733735
/* 2nd_half_of_user_ip4 == expected && */
734736
BPF_LDX_MEM(BPF_H, BPF_REG_7, BPF_REG_6,
735737
offsetof(struct bpf_sock_addr, user_ip4) + 2),
736-
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr16[1], 8),
738+
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, ip4.u4_addr16[1], 16),
737739

738-
/* whole_user_ip4 == expected) { */
740+
/* whole_user_ip4 == expected && */
739741
BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
740742
offsetof(struct bpf_sock_addr, user_ip4)),
741743
BPF_LD_IMM64(BPF_REG_8, ip4.u4_addr32), /* See [2]. */
744+
BPF_JMP_REG(BPF_JNE, BPF_REG_7, BPF_REG_8, 12),
745+
746+
/* 1st_byte_of_user_port == expected && */
747+
BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_6,
748+
offsetof(struct bpf_sock_addr, user_port)),
749+
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, port.u4_addr8[0], 10),
750+
751+
/* 1st_half_of_user_port == expected && */
752+
BPF_LDX_MEM(BPF_H, BPF_REG_7, BPF_REG_6,
753+
offsetof(struct bpf_sock_addr, user_port)),
754+
BPF_JMP_IMM(BPF_JNE, BPF_REG_7, port.u4_addr16[0], 8),
755+
756+
/* user_port == expected) { */
757+
BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
758+
offsetof(struct bpf_sock_addr, user_port)),
759+
BPF_LD_IMM64(BPF_REG_8, port.u4_addr32), /* See [2]. */
742760
BPF_JMP_REG(BPF_JNE, BPF_REG_7, BPF_REG_8, 4),
743761

744762
/* user_ip4 = addr4_rw.sin_addr */

0 commit comments

Comments
 (0)