@@ -24,6 +24,7 @@ enum sockopt_test_error {
2424static struct sockopt_test {
2525 const char * descr ;
2626 const struct bpf_insn insns [64 ];
27+ enum bpf_prog_type prog_type ;
2728 enum bpf_attach_type attach_type ;
2829 enum bpf_attach_type expected_attach_type ;
2930
@@ -928,9 +929,40 @@ static struct sockopt_test {
928929
929930 .error = EPERM_SETSOCKOPT ,
930931 },
932+
933+ /* ==================== prog_type ==================== */
934+
935+ {
936+ .descr = "can attach only BPF_CGROUP_SETSOCKOP" ,
937+ .insns = {
938+ /* return 1 */
939+ BPF_MOV64_IMM (BPF_REG_0 , 1 ),
940+ BPF_EXIT_INSN (),
941+
942+ },
943+ .prog_type = BPF_PROG_TYPE_CGROUP_SKB ,
944+ .attach_type = BPF_CGROUP_SETSOCKOPT ,
945+ .expected_attach_type = 0 ,
946+ .error = DENY_ATTACH ,
947+ },
948+
949+ {
950+ .descr = "can attach only BPF_CGROUP_GETSOCKOP" ,
951+ .insns = {
952+ /* return 1 */
953+ BPF_MOV64_IMM (BPF_REG_0 , 1 ),
954+ BPF_EXIT_INSN (),
955+
956+ },
957+ .prog_type = BPF_PROG_TYPE_CGROUP_SKB ,
958+ .attach_type = BPF_CGROUP_GETSOCKOPT ,
959+ .expected_attach_type = 0 ,
960+ .error = DENY_ATTACH ,
961+ },
931962};
932963
933964static int load_prog (const struct bpf_insn * insns ,
965+ enum bpf_prog_type prog_type ,
934966 enum bpf_attach_type expected_attach_type )
935967{
936968 LIBBPF_OPTS (bpf_prog_load_opts , opts ,
@@ -947,7 +979,7 @@ static int load_prog(const struct bpf_insn *insns,
947979 }
948980 insns_cnt ++ ;
949981
950- fd = bpf_prog_load (BPF_PROG_TYPE_CGROUP_SOCKOPT , NULL , "GPL" , insns , insns_cnt , & opts );
982+ fd = bpf_prog_load (prog_type , NULL , "GPL" , insns , insns_cnt , & opts );
951983 if (verbose && fd < 0 )
952984 fprintf (stderr , "%s\n" , bpf_log_buf );
953985
@@ -1036,13 +1068,18 @@ static int call_getsockopt(bool use_io_uring, int fd, int level, int optname,
10361068 return getsockopt (fd , level , optname , optval , optlen );
10371069}
10381070
1039- static int run_test (int cgroup_fd , struct sockopt_test * test , bool use_io_uring )
1071+ static int run_test (int cgroup_fd , struct sockopt_test * test , bool use_io_uring ,
1072+ bool use_link )
10401073{
1041- int sock_fd , err , prog_fd ;
1074+ int prog_type = BPF_PROG_TYPE_CGROUP_SOCKOPT ;
1075+ int sock_fd , err , prog_fd , link_fd = -1 ;
10421076 void * optval = NULL ;
10431077 int ret = 0 ;
10441078
1045- prog_fd = load_prog (test -> insns , test -> expected_attach_type );
1079+ if (test -> prog_type )
1080+ prog_type = test -> prog_type ;
1081+
1082+ prog_fd = load_prog (test -> insns , prog_type , test -> expected_attach_type );
10461083 if (prog_fd < 0 ) {
10471084 if (test -> error == DENY_LOAD )
10481085 return 0 ;
@@ -1051,7 +1088,12 @@ static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring)
10511088 return -1 ;
10521089 }
10531090
1054- err = bpf_prog_attach (prog_fd , cgroup_fd , test -> attach_type , 0 );
1091+ if (use_link ) {
1092+ err = bpf_link_create (prog_fd , cgroup_fd , test -> attach_type , NULL );
1093+ link_fd = err ;
1094+ } else {
1095+ err = bpf_prog_attach (prog_fd , cgroup_fd , test -> attach_type , 0 );
1096+ }
10551097 if (err < 0 ) {
10561098 if (test -> error == DENY_ATTACH )
10571099 goto close_prog_fd ;
@@ -1142,7 +1184,12 @@ static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring)
11421184close_sock_fd :
11431185 close (sock_fd );
11441186detach_prog :
1145- bpf_prog_detach2 (prog_fd , cgroup_fd , test -> attach_type );
1187+ if (use_link ) {
1188+ if (link_fd >= 0 )
1189+ close (link_fd );
1190+ } else {
1191+ bpf_prog_detach2 (prog_fd , cgroup_fd , test -> attach_type );
1192+ }
11461193close_prog_fd :
11471194 close (prog_fd );
11481195 return ret ;
@@ -1160,10 +1207,12 @@ void test_sockopt(void)
11601207 if (!test__start_subtest (tests [i ].descr ))
11611208 continue ;
11621209
1163- ASSERT_OK (run_test (cgroup_fd , & tests [i ], false),
1210+ ASSERT_OK (run_test (cgroup_fd , & tests [i ], false, false),
1211+ tests [i ].descr );
1212+ ASSERT_OK (run_test (cgroup_fd , & tests [i ], false, true),
11641213 tests [i ].descr );
11651214 if (tests [i ].io_uring_support )
1166- ASSERT_OK (run_test (cgroup_fd , & tests [i ], true),
1215+ ASSERT_OK (run_test (cgroup_fd , & tests [i ], true, false ),
11671216 tests [i ].descr );
11681217 }
11691218
0 commit comments