Skip to content

Commit 63cc8e2

Browse files
committed
Merge branch 'bpf-fix-sock-field-tests'
Jakub Sitnicki says: ==================== I think we have reached a consensus [1] on how the test for the 4-byte load from bpf_sock->dst_port and bpf_sk_lookup->remote_port should look, so here goes v3. I will submit a separate set of patches for bpf_sk_lookup->remote_port tests. This series has been tested on x86_64 and s390 on top of recent bpf-next - ad13baf ("selftests/bpf: Test subprog jit when toggle bpf_jit_harden repeatedly"). [1] https://lore.kernel.org/bpf/[email protected]/ v2 -> v3: - Split what was previously patch 2 which was doing two things - Use BPF_TCP_* constants (Martin) - Treat the result of 4-byte load from dst_port as a 16-bit value (Martin) - Typo fixup and some rewording in patch 4 description v1 -> v2: - Limit read_sk_dst_port only to client traffic (patch 2) - Make read_sk_dst_port pass on litte- and big-endian (patch 3) v1: https://lore.kernel.org/bpf/[email protected]/ v2: https://lore.kernel.org/bpf/[email protected]/ ==================== Signed-off-by: Daniel Borkmann <[email protected]>
2 parents 6091197 + deb5940 commit 63cc8e2

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

tools/testing/selftests/bpf/progs/test_sock_fields.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static void tpcpy(struct bpf_tcp_sock *dst,
114114

115115
#define RET_LOG() ({ \
116116
linum = __LINE__; \
117-
bpf_map_update_elem(&linum_map, &linum_idx, &linum, BPF_NOEXIST); \
117+
bpf_map_update_elem(&linum_map, &linum_idx, &linum, BPF_ANY); \
118118
return CG_OK; \
119119
})
120120

@@ -134,11 +134,11 @@ int egress_read_sock_fields(struct __sk_buff *skb)
134134
if (!sk)
135135
RET_LOG();
136136

137-
/* Not the testing egress traffic or
138-
* TCP_LISTEN (10) socket will be copied at the ingress side.
137+
/* Not testing the egress traffic or the listening socket,
138+
* which are covered by the cgroup_skb/ingress test program.
139139
*/
140140
if (sk->family != AF_INET6 || !is_loopback6(sk->src_ip6) ||
141-
sk->state == 10)
141+
sk->state == BPF_TCP_LISTEN)
142142
return CG_OK;
143143

144144
if (sk->src_port == bpf_ntohs(srv_sa6.sin6_port)) {
@@ -232,8 +232,8 @@ int ingress_read_sock_fields(struct __sk_buff *skb)
232232
sk->src_port != bpf_ntohs(srv_sa6.sin6_port))
233233
return CG_OK;
234234

235-
/* Only interested in TCP_LISTEN */
236-
if (sk->state != 10)
235+
/* Only interested in the listening socket */
236+
if (sk->state != BPF_TCP_LISTEN)
237237
return CG_OK;
238238

239239
/* It must be a fullsock for cgroup_skb/ingress prog */
@@ -251,10 +251,16 @@ int ingress_read_sock_fields(struct __sk_buff *skb)
251251
return CG_OK;
252252
}
253253

254+
/*
255+
* NOTE: 4-byte load from bpf_sock at dst_port offset is quirky. It
256+
* gets rewritten by the access converter to a 2-byte load for
257+
* backward compatibility. Treating the load result as a be16 value
258+
* makes the code portable across little- and big-endian platforms.
259+
*/
254260
static __noinline bool sk_dst_port__load_word(struct bpf_sock *sk)
255261
{
256262
__u32 *word = (__u32 *)&sk->dst_port;
257-
return word[0] == bpf_htonl(0xcafe0000);
263+
return word[0] == bpf_htons(0xcafe);
258264
}
259265

260266
static __noinline bool sk_dst_port__load_half(struct bpf_sock *sk)
@@ -281,6 +287,10 @@ int read_sk_dst_port(struct __sk_buff *skb)
281287
if (!sk)
282288
RET_LOG();
283289

290+
/* Ignore everything but the SYN from the client socket */
291+
if (sk->state != BPF_TCP_SYN_SENT)
292+
return CG_OK;
293+
284294
if (!sk_dst_port__load_word(sk))
285295
RET_LOG();
286296
if (!sk_dst_port__load_half(sk))

0 commit comments

Comments
 (0)