Skip to content

Commit 440c575

Browse files
author
Alexei Starovoitov
committed
Merge branch 'Do not limit cb_flags when creating child sk'
Martin KaFai says: ==================== This set fixes an issue that the bpf_skops_init_child() unnecessarily limited the child sk from inheriting all bpf_sock_ops_cb_flags of the listen sk. It also adds a test to check that. ==================== Tested-by: Stanislav Fomichev <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 48ca624 + 96d46c5 commit 440c575

File tree

6 files changed

+22
-40
lines changed

6 files changed

+22
-40
lines changed

include/net/tcp.h

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,34 +2228,6 @@ int __tcp_bpf_recvmsg(struct sock *sk, struct sk_psock *psock,
22282228
#endif /* CONFIG_NET_SOCK_MSG */
22292229

22302230
#ifdef CONFIG_CGROUP_BPF
2231-
/* Copy the listen sk's HDR_OPT_CB flags to its child.
2232-
*
2233-
* During 3-Way-HandShake, the synack is usually sent from
2234-
* the listen sk with the HDR_OPT_CB flags set so that
2235-
* bpf-prog will be called to write the BPF hdr option.
2236-
*
2237-
* In fastopen, the child sk is used to send synack instead
2238-
* of the listen sk. Thus, inheriting the HDR_OPT_CB flags
2239-
* from the listen sk gives the bpf-prog a chance to write
2240-
* BPF hdr option in the synack pkt during fastopen.
2241-
*
2242-
* Both fastopen and non-fastopen child will inherit the
2243-
* HDR_OPT_CB flags to keep the bpf-prog having a consistent
2244-
* behavior when deciding to clear this cb flags (or not)
2245-
* during the PASSIVE_ESTABLISHED_CB.
2246-
*
2247-
* In the future, other cb flags could be inherited here also.
2248-
*/
2249-
static inline void bpf_skops_init_child(const struct sock *sk,
2250-
struct sock *child)
2251-
{
2252-
tcp_sk(child)->bpf_sock_ops_cb_flags =
2253-
tcp_sk(sk)->bpf_sock_ops_cb_flags &
2254-
(BPF_SOCK_OPS_PARSE_ALL_HDR_OPT_CB_FLAG |
2255-
BPF_SOCK_OPS_PARSE_UNKNOWN_HDR_OPT_CB_FLAG |
2256-
BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG);
2257-
}
2258-
22592231
static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops,
22602232
struct sk_buff *skb,
22612233
unsigned int end_offset)
@@ -2264,11 +2236,6 @@ static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops,
22642236
skops->skb_data_end = skb->data + end_offset;
22652237
}
22662238
#else
2267-
static inline void bpf_skops_init_child(const struct sock *sk,
2268-
struct sock *child)
2269-
{
2270-
}
2271-
22722239
static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops,
22732240
struct sk_buff *skb,
22742241
unsigned int end_offset)

net/ipv4/tcp_minisocks.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,6 @@ struct sock *tcp_create_openreq_child(const struct sock *sk,
548548
newtp->fastopen_req = NULL;
549549
RCU_INIT_POINTER(newtp->fastopen_rsk, NULL);
550550

551-
bpf_skops_init_child(sk, newsk);
552551
tcp_bpf_clone(sk, newsk);
553552

554553
__TCP_INC_STATS(sock_net(sk), TCP_MIB_PASSIVEOPENS);

tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,19 @@ static int check_error_linum(const struct sk_fds *sk_fds)
264264

265265
static void check_hdr_and_close_fds(struct sk_fds *sk_fds)
266266
{
267+
const __u32 expected_inherit_cb_flags =
268+
BPF_SOCK_OPS_PARSE_UNKNOWN_HDR_OPT_CB_FLAG |
269+
BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG |
270+
BPF_SOCK_OPS_STATE_CB_FLAG;
271+
267272
if (sk_fds_shutdown(sk_fds))
268273
goto check_linum;
269274

275+
if (CHECK(expected_inherit_cb_flags != skel->bss->inherit_cb_flags,
276+
"Unexpected inherit_cb_flags", "0x%x != 0x%x\n",
277+
skel->bss->inherit_cb_flags, expected_inherit_cb_flags))
278+
goto check_linum;
279+
270280
if (check_hdr_stg(&exp_passive_hdr_stg, sk_fds->passive_fd,
271281
"passive_hdr_stg"))
272282
goto check_linum;
@@ -321,6 +331,8 @@ static void reset_test(void)
321331
memset(&skel->bss->active_estab_in, 0, optsize);
322332
memset(&skel->bss->active_fin_in, 0, optsize);
323333

334+
skel->bss->inherit_cb_flags = 0;
335+
324336
skel->data->test_kind = TCPOPT_EXP;
325337
skel->data->test_magic = 0xeB9F;
326338

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,10 @@ int misc_estab(struct bpf_sock_ops *skops)
304304
passive_lport_n = __bpf_htons(passive_lport_h);
305305
bpf_setsockopt(skops, SOL_TCP, TCP_SAVE_SYN,
306306
&true_val, sizeof(true_val));
307-
set_hdr_cb_flags(skops);
307+
set_hdr_cb_flags(skops, 0);
308308
break;
309309
case BPF_SOCK_OPS_TCP_CONNECT_CB:
310-
set_hdr_cb_flags(skops);
310+
set_hdr_cb_flags(skops, 0);
311311
break;
312312
case BPF_SOCK_OPS_PARSE_HDR_OPT_CB:
313313
return handle_parse_hdr(skops);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
__u8 test_kind = TCPOPT_EXP;
2323
__u16 test_magic = 0xeB9F;
24+
__u32 inherit_cb_flags = 0;
2425

2526
struct bpf_test_option passive_synack_out = {};
2627
struct bpf_test_option passive_fin_out = {};
@@ -467,6 +468,8 @@ static int handle_passive_estab(struct bpf_sock_ops *skops)
467468
struct tcphdr *th;
468469
int err;
469470

471+
inherit_cb_flags = skops->bpf_sock_ops_cb_flags;
472+
470473
err = load_option(skops, &passive_estab_in, true);
471474
if (err == -ENOENT) {
472475
/* saved_syn is not found. It was in syncookie mode.
@@ -600,10 +603,10 @@ int estab(struct bpf_sock_ops *skops)
600603
case BPF_SOCK_OPS_TCP_LISTEN_CB:
601604
bpf_setsockopt(skops, SOL_TCP, TCP_SAVE_SYN,
602605
&true_val, sizeof(true_val));
603-
set_hdr_cb_flags(skops);
606+
set_hdr_cb_flags(skops, BPF_SOCK_OPS_STATE_CB_FLAG);
604607
break;
605608
case BPF_SOCK_OPS_TCP_CONNECT_CB:
606-
set_hdr_cb_flags(skops);
609+
set_hdr_cb_flags(skops, 0);
607610
break;
608611
case BPF_SOCK_OPS_PARSE_HDR_OPT_CB:
609612
return handle_parse_hdr(skops);

tools/testing/selftests/bpf/test_tcp_hdr_options.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,13 @@ static inline void clear_hdr_cb_flags(struct bpf_sock_ops *skops)
110110
BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG));
111111
}
112112

113-
static inline void set_hdr_cb_flags(struct bpf_sock_ops *skops)
113+
static inline void set_hdr_cb_flags(struct bpf_sock_ops *skops, __u32 extra)
114114
{
115115
bpf_sock_ops_cb_flags_set(skops,
116116
skops->bpf_sock_ops_cb_flags |
117117
BPF_SOCK_OPS_PARSE_UNKNOWN_HDR_OPT_CB_FLAG |
118-
BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG);
118+
BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG |
119+
extra);
119120
}
120121
static inline void
121122
clear_parse_all_hdr_cb_flags(struct bpf_sock_ops *skops)

0 commit comments

Comments
 (0)