bpf: tcp: Support arbitrary SYN Cookie at TC.
Subsystems (8)
v4 cover bpf: tcp: Support arbitrary SYN Cookie at TC.
Under SYN Flood, the TCP stack generates SYN Cookie to remain stateless for the connection request until a valid ACK is responded to the SYN+ACK.
The cookie contains two kinds of host-specific bits, a timestamp and secrets, so only can it be validated by the generator. It means SYN Cookie consumes network resources between the client and the server; intermediate nodes must remember which nodes to route ACK for the cookie.
SYN Proxy reduces such unwanted resource allocation by handling 3WHS at the edge network. After SYN Proxy completes 3WHS, it forwards SYN to the backend server and completes another 3WHS. However, since the server's ISN differs from the cookie, the proxy must manage the ISN mappings and fix up SEQ/ACK numbers in every packet for each connection. If a proxy node goes down, all the connections through it are terminated. Keeping a state at proxy is painful from that perspective.
At AWS, we use a dirty hack to build truly stateless SYN Proxy at scale. Our SYN Proxy consists of the front proxy layer and the backend kernel module. (See slides of LPC2023 [0], p37 - p48)
The cookie that SYN Proxy generates differs from the kernel's cookie in that it contains a secret (called rolling salt) (i) shared by all the proxy nodes so that any node can validate ACK and (ii) updated periodically so that old cookies cannot be validated and we need not encode a timestamp for the cookie. Also, ISN contains WScale, SACK, and ECN, not in TS val. This is not to sacrifice any connection quality, where some customers turn off TCP timestamps option due to retro CVE.
After 3WHS, the proxy restores SYN, encapsulates ACK into SYN, and forward the TCP-in-TCP packet to the backend server. Our kernel module works at Netfilter input/output hooks and first feeds SYN to the TCP stack to initiate 3WHS. When the module is triggered for SYN+ACK, it looks up the corresponding request socket and overwrites tcp_rsk(req)->snt_isn with the proxy's cookie. Then, the module can complete 3WHS with the original ACK as is.
This way, our SYN Proxy does not manage the ISN mappings nor wait for SYN+ACK from the backend thus can remain stateless. It's working very well for high-bandwidth services like multiple Tbps, but we are looking for a way to drop the dirty hack and further optimise the sequences.
If we could validate an arbitrary SYN Cookie on the backend server with BPF, the proxy would need not restore SYN nor pass it. After validating ACK, the proxy node just needs to forward it, and then the server can do the lightweight validation (e.g. check if ACK came from proxy nodes, etc) and create a connection from the ACK.
This series adds a new kfunc available on TC to create a reqsk and configure it based on the argument populated from SYN Cookie.
Usage:
struct tcp_cookie_attributes attr = {
.tcp_opt = {
.mss_clamp = mss,
.wscale_ok = wscale_ok,
.rcv_scale = recv_scale, /* Server's WScale < 15 */
.snd_scale = send_scale, /* Client's WScale < 15 */
.tstamp_ok = tstamp_ok,
.sack_ok = sack_ok,
},
.ecn_ok = ecn_ok,
.usec_ts_ok = usec_ts_ok,
};
skc = bpf_skc_lookup_tcp(...);
sk = (struct sock *)bpf_skc_to_tcp_sock(skc);
bpf_sk_assign_tcp_reqsk(skb, sk, attr, sizeof(attr));
bpf_sk_release(skc);[0]: https://lpc.events/event/17/contributions/1645/attachments/1350/2701/SYN_Proxy_at_Scale_with_BPF.pdf
Changes:
v4:
* Patch 1 & 2
* s/CONFIG_SYN_COOKIE/CONFIG_SYN_COOKIES/
* Patch 1
* Don't set rcv_wscale for BPF SYN Cookie case.
* Patch 2
* Add test for tcp_opt.{unused,rcv_wscale} in kfunc
* Modify skb_steal_sock() to avoid resetting skb-sk
* Support SO_REUSEPORT lookup
* Patch 3
* Add CONFIG_SYN_COOKIES to Kconfig for CI
* Define BPF_F_CURRENT_NETNS
v3: https://lore.kernel.org/netdev/[email protected]/
* Guard kfunc and req->syncookie part in inet6?_steal_sock() with CONFIG_SYN_COOKIE
v2: https://lore.kernel.org/netdev/[email protected]/
* Drop SOCK_OPS and move SYN Cookie validation logic to TC with kfunc.
* Add cleanup patches to reduce discrepancy between cookie_v[46]_check()
v1: https://lore.kernel.org/bpf/[email protected]/
Kuniyuki Iwashima (3):
bpf: tcp: Handle BPF SYN Cookie in cookie_v[46]_check().
bpf: tcp: Support arbitrary SYN Cookie.
selftest: bpf: Test bpf_sk_assign_tcp_reqsk().
include/net/request_sock.h | 35 ++ include/net/sock.h | 25 - include/net/tcp.h | 27 + net/core/filter.c | 102 +++- net/core/sock.c | 14 +- net/ipv4/syncookies.c | 62 +- net/ipv6/syncookies.c | 9 +- tools/testing/selftests/bpf/bpf_kfuncs.h | 10 + tools/testing/selftests/bpf/config | 1 + .../bpf/prog_tests/tcp_custom_syncookie.c | 163 +++++ .../selftests/bpf/progs/test_siphash.h | 64 ++ .../bpf/progs/test_tcp_custom_syncookie.c | 573 ++++++++++++++++++ .../bpf/progs/test_tcp_custom_syncookie.h | 162 +++++ 13 files changed, 1214 insertions(+), 33 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/tcp_custom_syncookie.c create mode 100644 tools/testing/selftests/bpf/progs/test_siphash.h create mode 100644 tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c create mode 100644 tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h
Signature · 1 line
2.30.2
PATCH v4 1/3 bpf: tcp: Handle BPF SYN Cookie in cookie_v[46]_check().
We will support arbitrary SYN Cookie with BPF in the following patch.
If BPF prog validates ACK and kfunc allocates reqsk, it will be carried to cookie_[46]_check() as skb->sk. Then, we call cookie_bpf_check() to validate the configuration passed to kfunc.
First, we clear skb->sk, skb->destructor, and req->rsk_listener, which are needed not to hold refcnt for reqsk and the listener. See the following patch for details.
Then, we parse TCP options to check if tstamp_ok is discrepant. If it is invalid, we increment LINUX_MIB_SYNCOOKIESFAILED and send RST. If tstamp_ok is valid, we increment LINUX_MIB_SYNCOOKIESRECV.
After that, we check sack_ok and wscale_ok with corresponding sysctl knobs. If the test fails, we send RST but do not increment LINUX_MIB_SYNCOOKIESFAILED. This behaviour is the same with the non-BPF cookie handling in cookie_tcp_check().
Finally, we finish initialisation for the remaining fields with cookie_tcp_reqsk_init().
--- include/net/tcp.h | 21 +++++++++++++++ net/ipv4/syncookies.c | 62 +++++++++++++++++++++++++++++++++++++++++-- net/ipv6/syncookies.c | 9 +++++-- 3 files changed, 88 insertions(+), 4 deletions(-)
include/net/tcp.h +21 -0
@@ -590,6 +590,27 @@ static inline bool cookie_ecn_ok(const struct net *net, const struct dst_entry *dst_feature (dst,RTAX_FEATURE_ECN ); } +#if IS_ENABLED (CONFIG_BPF ) +static inline bool cookie_bpf_ok (struct sk_buff *skb) +{ +return skb->sk; +} + +struct request_sock *cookie_bpf_check (struct net *net,struct sock *sk, +struct sk_buff *skb); +#else +static inline bool cookie_bpf_ok (struct sk_buff *skb) +{ +return false; +} + +static inline struct request_sock *cookie_bpf_check (struct net *net,struct sock *sk, +struct sk_buff *skb) +{ +return NULL ; +} +#endif +/* From net/ipv6/syncookies.c */ int __cookie_v6_check (const struct ipv6hdr *iph,const struct tcphdr *th);struct sock *cookie_v6_check (struct sock *sk,struct sk_buff *skb);
net/ipv4/syncookies.c +60 -2
80 lines not on this page · raw message
net/ipv6/syncookies.c +7 -2
@@ -182,7 +182,11 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) !th->ack || th->rst) goto out; - req =cookie_tcp_check (net, sk, skb); +if (cookie_bpf_ok (skb)) + req =cookie_bpf_check (net, sk, skb); +else + req =cookie_tcp_check (net, sk, skb); +if (IS_ERR (req)) goto out;if (!req)
@@ -247,7 +251,8 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) ireq->wscale_ok, &rcv_wscale, dst_metric(dst, RTAX_INITRWND)); - ireq->rcv_wscale = rcv_wscale; +if (!req->syncookie) + ireq->rcv_wscale = rcv_wscale; ireq->ecn_ok &=cookie_ecn_ok (net, dst); ret =tcp_get_cookie_sock (sk, skb, req, dst);
Signature · 1 line
2.30.2
Martin KaFai Lau
reviewerRe: [PATCH v4 bpf-next 1/3] bpf: tcp: Handle BPF SYN Cookie in cookie_v[46]_check().
Quoted 14 lines · Kuniyuki Iwashima
net/ipv4/syncookies.c +6
-0@@ -304,6 +304,59 @@ static int cookie_tcp_reqsk_init(struct sock *sk, struct sk_buff *skb,return 0 ; } +#if IS_ENABLED (CONFIG_BPF ) +struct request_sock *cookie_bpf_check (struct net *net,struct sock *sk, +struct sk_buff *skb) +{ +struct request_sock *req =inet_reqsk (skb->sk); +struct inet_request_sock *ireq =inet_rsk (req);
net/ipv4/syncookies.c static int cookie_tcp_reqsk_init(struct sock *sk, struct sk_buff *skb, · line 322
Quoted 10 lines
net/ipv4/syncookies.c +10
-0+struct tcp_request_sock *treq =tcp_rsk (req); +struct tcp_options_received tcp_opt; +int ret; + + skb->sk =NULL ; + skb->destructor =NULL ; + req->rsk_listener =NULL ; + +memset (&tcp_opt,0 ,sizeof (tcp_opt)); +tcp_parse_options (net, skb, &tcp_opt,0 ,NULL );
In patch 2, the bpf prog is passing the tcp_opt to the kfunc. The selftest in patch 3 is also parsing the tcp-options.
The kernel parses the tcp-option here again to do some checking and req's member initialization. Can these checking and initialization be done in the bpf_sk_assign_tcp_reqsk() kfunc instead to avoid the double tcp-option parsing?
Quoted 36 lines
net/ipv4/syncookies.c +36
-0+ +if (ireq->tstamp_ok ^ tcp_opt.saw_tstamp) { +__NET_INC_STATS (net,LINUX_MIB_SYNCOOKIESFAILED ); + goto reset; + } + +__NET_INC_STATS (net,LINUX_MIB_SYNCOOKIESRECV ); + +if (ireq->tstamp_ok) { +if (!READ_ONCE (net->ipv4.sysctl_tcp_timestamps)) + goto reset; + + req->ts_recent = tcp_opt.rcv_tsval; + treq->ts_off = tcp_opt.rcv_tsecr -tcp_ns_to_ts (false,tcp_clock_ns ()); + } + +if (ireq->sack_ok && !READ_ONCE (net->ipv4.sysctl_tcp_sack)) + goto reset; + +if (ireq->wscale_ok && !READ_ONCE (net->ipv4.sysctl_tcp_window_scaling)) + goto reset; + + ret =cookie_tcp_reqsk_init (sk, skb, req); +if (ret) { +reqsk_free (req); + req =NULL ; + } + +return req; + +reset: +reqsk_free (req); + return ERR_PTR(-EINVAL); +} +EXPORT_SYMBOL_GPL (cookie_bpf_check); +#endif
Re: [PATCH v4 bpf-next 1/3] bpf: tcp: Handle BPF SYN Cookie in cookie_v[46]_check().
From: Martin KaFai Lau <[email protected]>
Date: Tue, 5 Dec 2023 16:19:20 -0800
Quoted 29 lines · Kuniyuki Iwashima
Quoted 14 lines
net/ipv4/syncookies.c +6
-0@@ -304,6 +304,59 @@ static int cookie_tcp_reqsk_init(struct sock *sk, struct sk_buff *skb,return 0 ; } +#if IS_ENABLED (CONFIG_BPF ) +struct request_sock *cookie_bpf_check (struct net *net,struct sock *sk, +struct sk_buff *skb) +{ +struct request_sock *req =inet_reqsk (skb->sk); +struct inet_request_sock *ireq =inet_rsk (req);net/ipv4/syncookies.c static int cookie_tcp_reqsk_init(struct sock *sk, struct sk_buff *skb, · line 322
Quoted 10 lines
net/ipv4/syncookies.c +10
-0+struct tcp_request_sock *treq =tcp_rsk (req); +struct tcp_options_received tcp_opt; +int ret; + + skb->sk =NULL ; + skb->destructor =NULL ; + req->rsk_listener =NULL ; + +memset (&tcp_opt,0 ,sizeof (tcp_opt)); +tcp_parse_options (net, skb, &tcp_opt,0 ,NULL );In patch 2, the bpf prog is passing the
tcp_optto the kfunc. The selftest in patch 3 is also parsing the tcp-options.
Quoted 3 lines
The kernel parses the tcp-option here again to do some checking and req's member initialization. Can these checking and initialization be done in the
bpf_sk_assign_tcp_reqsk()kfunc instead to avoid the double tcp-option parsing?
If TS is not used as a cookie storage, bpf prog need not parse it. OTOH, if a value is encoded into TS, bpf prog need to parse it. In that case, we cannot avoid parsing options in bpf prog.
The parsing here comes from my paranoia, so.. probably we can drop it and the first test below, and rely on bpf prog's tcp_opt, especially tstamp_ok, rcv_tsval, and rcv_tsecr ?
I placed other tests here to align with the normal cookie flow, but they can be moved to kfunc. However, initialisation assuems skb points to TCP header, so here would be better place, I think.
Quoted 37 lines
Quoted 37 lines
net/ipv4/syncookies.c +36
-0+ +if (ireq->tstamp_ok ^ tcp_opt.saw_tstamp) { +__NET_INC_STATS (net,LINUX_MIB_SYNCOOKIESFAILED ); + goto reset; + } + +__NET_INC_STATS (net,LINUX_MIB_SYNCOOKIESRECV ); + +if (ireq->tstamp_ok) { +if (!READ_ONCE (net->ipv4.sysctl_tcp_timestamps)) + goto reset; + + req->ts_recent = tcp_opt.rcv_tsval; + treq->ts_off = tcp_opt.rcv_tsecr -tcp_ns_to_ts (false,tcp_clock_ns ()); + } + +if (ireq->sack_ok && !READ_ONCE (net->ipv4.sysctl_tcp_sack)) + goto reset; + +if (ireq->wscale_ok && !READ_ONCE (net->ipv4.sysctl_tcp_window_scaling)) + goto reset; + + ret =cookie_tcp_reqsk_init (sk, skb, req); +if (ret) { +reqsk_free (req); + req =NULL ; + } + +return req; + +reset: +reqsk_free (req); + return ERR_PTR(-EINVAL); +} +EXPORT_SYMBOL_GPL (cookie_bpf_check); +#endif
Martin KaFai Lau
reviewerRe: [PATCH v4 bpf-next 1/3] bpf: tcp: Handle BPF SYN Cookie in cookie_v[46]_check().
Quoted 31 lines · Kuniyuki Iwashima
From: Martin KaFai Lau <[email protected]>
Date: Tue, 5 Dec 2023 16:19:20 -0800Quoted 29 lines · Kuniyuki Iwashima
Quoted 14 lines
net/ipv4/syncookies.c +6
-0@@ -304,6 +304,59 @@ static int cookie_tcp_reqsk_init(struct sock *sk, struct sk_buff *skb,return 0 ; } +#if IS_ENABLED (CONFIG_BPF ) +struct request_sock *cookie_bpf_check (struct net *net,struct sock *sk, +struct sk_buff *skb) +{ +struct request_sock *req =inet_reqsk (skb->sk); +struct inet_request_sock *ireq =inet_rsk (req);net/ipv4/syncookies.c static int cookie_tcp_reqsk_init(struct sock *sk, struct sk_buff *skb, · line 322
Quoted 10 lines
net/ipv4/syncookies.c +10
-0+struct tcp_request_sock *treq =tcp_rsk (req); +struct tcp_options_received tcp_opt; +int ret; + + skb->sk =NULL ; + skb->destructor =NULL ; + req->rsk_listener =NULL ; + +memset (&tcp_opt,0 ,sizeof (tcp_opt)); +tcp_parse_options (net, skb, &tcp_opt,0 ,NULL );In patch 2, the bpf prog is passing the
tcp_optto the kfunc. The selftest in patch 3 is also parsing the tcp-options.
Quoted 7 lines
Quoted 3 lines
The kernel parses the tcp-option here again to do some checking and req's member initialization. Can these checking and initialization be done in the
bpf_sk_assign_tcp_reqsk()kfunc instead to avoid the double tcp-option parsing?If TS is not used as a cookie storage, bpf prog need not parse it. OTOH, if a value is encoded into TS, bpf prog need to parse it. In that case, we cannot avoid parsing options in bpf prog.
If I read patch 2 correctly, the ireq->tstamp_ok is set by the kfunc, so I assume that the bpf prog has to parse the tcp-option.
Like the "if (ireq->tstamp_ok ^ tcp_opt.saw_tstamp)" test below, ireq->tstamp_ok will always be 0 if the bpf prog did not parse the tcp-option.
Quoted 4 lines
The parsing here comes from my paranoia, so.. probably we can drop it and the first test below, and rely on bpf prog's
tcp_opt, especiallytstamp_ok,rcv_tsval, andrcv_tsecr?
My preference is that it is clearer to allow the bpf prog to initialize all tcp_opt instead of only taking the tcp_opt.tstamp_ok from bpf_prog but ignore the tcp_opt.rcv_tsval/tsecr. The kfunc will then use the tcp_opt to initialize the req.
It is also better to detect the following error cases as much as possible in the kfunc instead of failing later in the tcp stack. e.g. checking the sysctl should be doable in the kfunc.
Quoted 43 lines
I placed other tests here to align with the normal cookie flow, but they can be moved to kfunc. However, initialisation assuems skb points to TCP header, so here would be better place, I think.
Quoted 37 lines
Quoted 37 lines
net/ipv4/syncookies.c +36
-0+ +if (ireq->tstamp_ok ^ tcp_opt.saw_tstamp) { +__NET_INC_STATS (net,LINUX_MIB_SYNCOOKIESFAILED ); + goto reset; + } + +__NET_INC_STATS (net,LINUX_MIB_SYNCOOKIESRECV ); + +if (ireq->tstamp_ok) { +if (!READ_ONCE (net->ipv4.sysctl_tcp_timestamps)) + goto reset; + + req->ts_recent = tcp_opt.rcv_tsval; + treq->ts_off = tcp_opt.rcv_tsecr -tcp_ns_to_ts (false,tcp_clock_ns ()); + } + +if (ireq->sack_ok && !READ_ONCE (net->ipv4.sysctl_tcp_sack)) + goto reset; + +if (ireq->wscale_ok && !READ_ONCE (net->ipv4.sysctl_tcp_window_scaling)) + goto reset; + + ret =cookie_tcp_reqsk_init (sk, skb, req); +if (ret) { +reqsk_free (req); + req =NULL ; + } + +return req; + +reset: +reqsk_free (req); + return ERR_PTR(-EINVAL); +} +EXPORT_SYMBOL_GPL (cookie_bpf_check); +#endif
Re: [PATCH v4 bpf-next 1/3] bpf: tcp: Handle BPF SYN Cookie in cookie_v[46]_check().
From: Martin KaFai Lau <[email protected]>
Date: Tue, 5 Dec 2023 19:11:17 -0800
Quoted 40 lines · Kuniyuki Iwashima
Quoted 31 lines
From: Martin KaFai Lau <[email protected]>
Date: Tue, 5 Dec 2023 16:19:20 -0800Quoted 29 lines · Kuniyuki Iwashima
Quoted 14 lines
net/ipv4/syncookies.c +6
-0@@ -304,6 +304,59 @@ static int cookie_tcp_reqsk_init(struct sock *sk, struct sk_buff *skb,return 0 ; } +#if IS_ENABLED (CONFIG_BPF ) +struct request_sock *cookie_bpf_check (struct net *net,struct sock *sk, +struct sk_buff *skb) +{ +struct request_sock *req =inet_reqsk (skb->sk); +struct inet_request_sock *ireq =inet_rsk (req);net/ipv4/syncookies.c static int cookie_tcp_reqsk_init(struct sock *sk, struct sk_buff *skb, · line 322
Quoted 10 lines
net/ipv4/syncookies.c +10
-0+struct tcp_request_sock *treq =tcp_rsk (req); +struct tcp_options_received tcp_opt; +int ret; + + skb->sk =NULL ; + skb->destructor =NULL ; + req->rsk_listener =NULL ; + +memset (&tcp_opt,0 ,sizeof (tcp_opt)); +tcp_parse_options (net, skb, &tcp_opt,0 ,NULL );In patch 2, the bpf prog is passing the
tcp_optto the kfunc. The selftest in patch 3 is also parsing the tcp-options.Quoted 7 lines
Quoted 3 lines
The kernel parses the tcp-option here again to do some checking and req's member initialization. Can these checking and initialization be done in the
bpf_sk_assign_tcp_reqsk()kfunc instead to avoid the double tcp-option parsing?If TS is not used as a cookie storage, bpf prog need not parse it. OTOH, if a value is encoded into TS, bpf prog need to parse it. In that case, we cannot avoid parsing options in bpf prog.
Quoted 5 lines
If I read patch 2 correctly, the
ireq->tstamp_okis set by the kfunc, so I assume that the bpf prog has to parse the tcp-option.Like the "if (
ireq->tstamp_ok^ tcp_opt.saw_tstamp)" test below,ireq->tstamp_okwill always be 0 if the bpf prog did not parse the tcp-option.
Ah sorry, I assumed TS bit was encoded in SYN as disabled. TCP option parsing is needed at least once for SYN, but we need not do so for SYN+ACK if TS bit is in ISN.
Quoted 10 lines
Quoted 5 lines
The parsing here comes from my paranoia, so.. probably we can drop it and the first test below, and rely on bpf prog's
tcp_opt, especiallytstamp_ok,rcv_tsval, andrcv_tsecr?My preference is that it is clearer to allow the bpf prog to initialize all
tcp_optinstead of only taking the tcp_opt.tstamp_ok frombpf_progbut ignore the tcp_opt.rcv_tsval/tsecr. The kfunc will then use thetcp_optto initialize the req.
I'll drop the option parsing in kernel and allow bpf prog to fully initialise tcp_opt.
Quoted 4 lines
It is also better to detect the following error cases as much as possible in the kfunc instead of failing later in the tcp stack. e.g. checking the sysctl should be doable in the kfunc.
Ok, I'll move the sysctl tests and ts_off init to kfunc.
Quoted 44 lines
Quoted 44 lines
I placed other tests here to align with the normal cookie flow, but they can be moved to kfunc. However, initialisation assuems skb points to TCP header, so here would be better place, I think.
Quoted 37 lines
Quoted 37 lines
net/ipv4/syncookies.c +36
-0+ +if (ireq->tstamp_ok ^ tcp_opt.saw_tstamp) { +__NET_INC_STATS (net,LINUX_MIB_SYNCOOKIESFAILED ); + goto reset; + } + +__NET_INC_STATS (net,LINUX_MIB_SYNCOOKIESRECV ); + +if (ireq->tstamp_ok) { +if (!READ_ONCE (net->ipv4.sysctl_tcp_timestamps)) + goto reset; + + req->ts_recent = tcp_opt.rcv_tsval; + treq->ts_off = tcp_opt.rcv_tsecr -tcp_ns_to_ts (false,tcp_clock_ns ()); + } + +if (ireq->sack_ok && !READ_ONCE (net->ipv4.sysctl_tcp_sack)) + goto reset; + +if (ireq->wscale_ok && !READ_ONCE (net->ipv4.sysctl_tcp_window_scaling)) + goto reset; + + ret =cookie_tcp_reqsk_init (sk, skb, req); +if (ret) { +reqsk_free (req); + req =NULL ; + } + +return req; + +reset: +reqsk_free (req); + return ERR_PTR(-EINVAL); +} +EXPORT_SYMBOL_GPL (cookie_bpf_check); +#endif
PATCH v4 2/3 bpf: tcp: Support arbitrary SYN Cookie.
In reply to Kuniyuki IwashimaThis patch adds a new kfunc available at TC hook to support arbitrary SYN Cookie.
The basic usage is as follows:
struct tcp_cookie_attributes attr = {
.tcp_opt = {
.mss_clamp = mss,
.wscale_ok = wscale_ok,
.rcv_scale = recv_scale, /* Server's WScale < 15 */
.snd_scale = send_scale, /* Client's WScale < 15 */
.tstamp_ok = tstamp_ok,
.sack_ok = sack_ok,
},
.ecn_ok = ecn_ok,
.usec_ts_ok = usec_ts_ok,
};
skc = bpf_skc_lookup_tcp(...);
sk = (struct sock *)bpf_skc_to_tcp_sock(skc);
bpf_sk_assign_tcp_reqsk(skb, sk, attr, sizeof(attr));
bpf_sk_release(skc);bpf_sk_assign_tcp_reqsk() takes skb, a listener sk, and struct tcp_cookie_attributes and allocates reqsk and configures it. Then, bpf_sk_assign_tcp_reqsk() links reqsk with skb and the listener.
The notable thing here is that we do not hold refcnt for both reqsk and listener. To differentiate that, we mark reqsk->syncookie, which is only used in TX for now. So, if reqsk->syncookie is 1 in RX, it means that the reqsk is allocated by kfunc.
When skb is freed, sock_pfree() checks if reqsk->syncookie is 1, and in that case, we set NULL to reqsk->rsk_listener before calling reqsk_free() as reqsk does not hold a refcnt of the listener.
When the TCP stack looks up a socket from the skb, we return inet_reqsk(skb->sk)->rsk_listener in skb_steal_sock(). However, we do not clear skb->sk and skb->destructor so that we can carry the reqsk to cookie_v[46]_check().
The refcnt of reqsk will finally be set to 1 in tcp_get_cookie_sock() after creating a full sk.
Note that we can use the unused bits in struct tcp_options_received and extend struct tcp_cookie_attributes in the future when we add a new attribute that is determined in 3WHS.
--- include/net/request_sock.h | 35 +++++++++++++ include/net/sock.h | 25 --------- include/net/tcp.h | 6 +++ net/core/filter.c | 102 ++++++++++++++++++++++++++++++++++++- net/core/sock.c | 14 ++++- 5 files changed, 153 insertions(+), 29 deletions(-)
include/net/request_sock.h +35 -0
@@ -83,6 +83,41 @@ static inline struct sock *req_to_sk(struct request_sock *req)return (struct sock *)req; } +/** +* skb_steal_sock - steal a socket from an sk_buff +* @skb: sk_buff to steal the socket from +* @refcounted: is set to true if the socket is reference-counted +* @prefetched: is set to true if the socket was assigned from bpf +*/ +static inline struct sock * +skb_steal_sock (struct sk_buff *skb,bool *refcounted,bool *prefetched) +{ +struct sock *sk = skb->sk; + +if (!skb->sk) { + *prefetched = false; + *refcounted = false; +return NULL ; + } + + *prefetched =skb_sk_is_prefetched (skb); +if (*prefetched) { +#if IS_ENABLED (CONFIG_SYN_COOKIES ) +if (sk->sk_state ==TCP_NEW_SYN_RECV &&inet_reqsk (sk)->syncookie) { + *refcounted = false; +return inet_reqsk (sk)->rsk_listener; + } +#endif + *refcounted =sk_is_refcounted (sk); + }else { + *refcounted = true; + } + + skb->destructor =NULL ; + skb->sk =NULL ; +return sk; +} +static inline struct request_sock *reqsk_alloc (const struct request_sock_ops *ops,struct sock *sk_listener,bool attach_listener)
include/net/sock.h +0 -25
@@ -2838,31 +2838,6 @@ sk_is_refcounted(struct sock *sk)return !sk_fullsock (sk) || !sock_flag (sk,SOCK_RCU_FREE ); } -/** -* skb_steal_sock - steal a socket from an sk_buff -* @skb: sk_buff to steal the socket from -* @refcounted: is set to true if the socket is reference-counted -* @prefetched: is set to true if the socket was assigned from bpf -*/ -static inline struct sock * -skb_steal_sock (struct sk_buff *skb,bool *refcounted,bool *prefetched) -{ -if (skb->sk) { -struct sock *sk = skb->sk; - - *refcounted = true; - *prefetched =skb_sk_is_prefetched (skb); -if (*prefetched) - *refcounted =sk_is_refcounted (sk); - skb->destructor =NULL ; - skb->sk =NULL ; -return sk; - } - *prefetched = false; - *refcounted = false; -return NULL ; -} -/* Checks if this SKB belongs to an HW offloaded socket * and whether any SW fallbacks are required based on dev. * Check decrypted mark in case skb_orphan() cleared socket.
include/net/tcp.h +6 -0
@@ -591,6 +591,12 @@ static inline bool cookie_ecn_ok(const struct net *net, const struct dst_entry * }#if IS_ENABLED (CONFIG_BPF ) +struct tcp_cookie_attributes { +struct tcp_options_received tcp_opt; +bool ecn_ok; +bool usec_ts_ok; +} __packed; +static inline bool cookie_bpf_ok (struct sk_buff *skb) {return skb->sk;
net/core/filter.c +100 -2
126 lines not on this page · raw message
net/core/sock.c +12 -2
@@ -2579,8 +2579,18 @@ EXPORT_SYMBOL(sock_efree);#ifdef CONFIG_INET void sock_pfree (struct sk_buff *skb) { -if (sk_is_refcounted (skb->sk)) -sock_gen_put (skb->sk); +struct sock *sk = skb->sk; + +if (!sk_is_refcounted (sk)) +return ; + +if (sk->sk_state ==TCP_NEW_SYN_RECV &&inet_reqsk (sk)->syncookie) { +inet_reqsk (sk)->rsk_listener =NULL ; +reqsk_free (inet_reqsk (sk)); +return ; + } + +sock_gen_put (sk); }EXPORT_SYMBOL (sock_pfree);#endif /* CONFIG_INET */
Signature · 1 line
2.30.2
Martin KaFai Lau
reviewerRe: [PATCH v4 bpf-next 2/3] bpf: tcp: Support arbitrary SYN Cookie.
Quoted 21 lines · Kuniyuki Iwashima
include/net/request_sock.h +13
-0@@ -83,6 +83,41 @@ static inline struct sock *req_to_sk(struct request_sock *req)return (struct sock *)req; } +/** +* skb_steal_sock - steal a socket from an sk_buff +* @skb: sk_buff to steal the socket from +* @refcounted: is set to true if the socket is reference-counted +* @prefetched: is set to true if the socket was assigned from bpf +*/ +static inline struct sock * +skb_steal_sock (struct sk_buff *skb,bool *refcounted,bool *prefetched) +{ +struct sock *sk = skb->sk; + +if (!skb->sk) { + *prefetched = false;
include/net/request_sock.h static inline struct sock *req_to_sk(struct request_sock *req) · line 108
Quoted 10 lines
include/net/request_sock.h +10
-0+ *refcounted = false; +return NULL ; + } + + *prefetched =skb_sk_is_prefetched (skb); +if (*prefetched) { +#if IS_ENABLED (CONFIG_SYN_COOKIES ) +if (sk->sk_state ==TCP_NEW_SYN_RECV &&inet_reqsk (sk)->syncookie) { + *refcounted = false; +return inet_reqsk (sk)->rsk_listener;
If it does not break later logic, I would set inet_reqsk(sk)->rsk_listener to NULL to avoid inconsistency when the later inet[6]_lookup_reuseport() selects another listener. skb_steal_sock() steals the inet_reqsk(sk)->rsk_listener in this sense.
Quoted 12 lines
include/net/request_sock.h +12
-0+ } +#endif + *refcounted =sk_is_refcounted (sk); + }else { + *refcounted = true; + } + + skb->destructor =NULL ; + skb->sk =NULL ; +return sk; +} +
[ ... ]
Quoted 42 lines
net/core/filter.c +34
-0@@ -11816,6 +11816,94 @@ __bpf_kfunc int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern,return 0 ; } + +__bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct sk_buff *skb, struct sock *sk, +struct tcp_cookie_attributes *attr, +int attr__sz) +{ +#if IS_ENABLED (CONFIG_SYN_COOKIES ) +const struct request_sock_ops *ops; +struct inet_request_sock *ireq; +struct tcp_request_sock *treq; +struct request_sock *req; +__u16 min_mss; + +if (attr__sz !=sizeof (*attr) || attr->tcp_opt.unused) +return -EINVAL ; + +if (!sk) +return -EINVAL ; + +if (!skb_at_tc_ingress (skb)) +return -EINVAL ; + +if (dev_net (skb->dev) !=sock_net (sk)) +return -ENETUNREACH ; + +switch (skb->protocol) { +case htons (ETH_P_IP ): + ops = &tcp_request_sock_ops; + min_mss =536 ; +break ; +#if IS_BUILTIN (CONFIG_IPV6 ) +case htons (ETH_P_IPV6 ): + ops = &tcp6_request_sock_ops; + min_mss =IPV6_MIN_MTU -60 ; +break ;
net/core/filter.c __bpf_kfunc int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern, · line 11862
Quoted 10 lines
net/core/filter.c +10
-0+#endif +default : +return -EINVAL ; + } + +if (sk->sk_type !=SOCK_STREAM || sk->sk_state !=TCP_LISTEN ) +return -EINVAL ; + +if (attr->tcp_opt.mss_clamp < min_mss) { +__NET_INC_STATS (sock_net (sk),LINUX_MIB_SYNCOOKIESFAILED );
hmm... this one I am not sure if the kfunc should decide what counted as SYNCOOKIESFAILED or not. Beside, the bpf prog should have already rejected the skb as part of its cookie validation logic. Thus, reaching here is more like a bpf prog's error instead.
I would leave the SYNCOOKIESFAILED usage for the kernel tcp layer only. The existing bpf_tcp_check_syncookie() helper does not increment SYNCOOKIESFAILED also.
net/core/filter.c __bpf_kfunc int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern, · line 11869
Quoted 7 lines
net/core/filter.c +7
-0+return -EINVAL ; + } + +if (attr->tcp_opt.wscale_ok && + (attr->tcp_opt.snd_wscale >TCP_MAX_WSCALE || + attr->tcp_opt.rcv_wscale >TCP_MAX_WSCALE )) { +__NET_INC_STATS (sock_net (sk),LINUX_MIB_SYNCOOKIESFAILED );
Same here.
Quoted 100 lines
net/core/filter.c +49
-2+return -EINVAL ; + } + +if (sk_is_mptcp (sk)) + req =mptcp_subflow_reqsk_alloc (ops, sk, false); +else + req =inet_reqsk_alloc (ops, sk, false); + +if (!req) +return -ENOMEM ; + + ireq =inet_rsk (req); + treq =tcp_rsk (req); + + req->syncookie =1 ; + req->rsk_listener = sk; + req->mss = attr->tcp_opt.mss_clamp; + + ireq->snd_wscale = attr->tcp_opt.snd_wscale; + ireq->rcv_wscale = attr->tcp_opt.rcv_wscale; + ireq->wscale_ok = attr->tcp_opt.wscale_ok; + ireq->tstamp_ok = attr->tcp_opt.tstamp_ok; + ireq->sack_ok = attr->tcp_opt.sack_ok; + ireq->ecn_ok = attr->ecn_ok; + + treq->req_usec_ts = attr->usec_ts_ok; + +skb_orphan (skb); + skb->sk =req_to_sk (req); + skb->destructor = sock_pfree; + +return 0 ; +#else +return -EOPNOTSUPP ; +#endif +} +__bpf_kfunc_end_defs (); int bpf_dynptr_from_skb_rdonly(struct sk_buff *skb, u64 flags,@@ -11844,6 +11932,10 @@ BTF_SET8_START(bpf_kfunc_check_set_sock_addr) BTF_ID_FLAGS(func, bpf_sock_addr_set_sun_path)BTF_SET8_END (bpf_kfunc_check_set_sock_addr) +BTF_SET8_START (bpf_kfunc_check_set_tcp_reqsk ) +BTF_ID_FLAGS (func, bpf_sk_assign_tcp_reqsk) +BTF_SET8_END (bpf_kfunc_check_set_tcp_reqsk) + static const struct btf_kfunc_id_set bpf_kfunc_set_skb = { .owner =THIS_MODULE , .set = &bpf_kfunc_check_set_skb,@@ -11859,6 +11951,11 @@ static const struct btf_kfunc_id_set bpf_kfunc_set_sock_addr = { .set = &bpf_kfunc_check_set_sock_addr, }; +static const struct btf_kfunc_id_set bpf_kfunc_set_tcp_reqsk = { + .owner =THIS_MODULE , + .set = &bpf_kfunc_check_set_tcp_reqsk, +}; +static int __init bpf_kfunc_init (void ) {int ret;@@ -11874,8 +11971,9 @@ static int __init bpf_kfunc_init(void) ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_LWT_SEG6LOCAL , &bpf_kfunc_set_skb); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_NETFILTER , &bpf_kfunc_set_skb); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_XDP , &bpf_kfunc_set_xdp); -return ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_CGROUP_SOCK_ADDR , - &bpf_kfunc_set_sock_addr); + ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_CGROUP_SOCK_ADDR , + &bpf_kfunc_set_sock_addr); +return ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SCHED_CLS , &bpf_kfunc_set_tcp_reqsk); }late_initcall (bpf_kfunc_init);net/core/sock.c +12
-2@@ -2579,8 +2579,18 @@ EXPORT_SYMBOL(sock_efree);#ifdef CONFIG_INET void sock_pfree (struct sk_buff *skb) { -if (sk_is_refcounted (skb->sk)) -sock_gen_put (skb->sk); +struct sock *sk = skb->sk; + +if (!sk_is_refcounted (sk)) +return ; + +if (sk->sk_state ==TCP_NEW_SYN_RECV &&inet_reqsk (sk)->syncookie) { +inet_reqsk (sk)->rsk_listener =NULL ; +reqsk_free (inet_reqsk (sk)); +return ; + } + +sock_gen_put (sk); }EXPORT_SYMBOL (sock_pfree);#endif /* CONFIG_INET */
Re: [PATCH v4 bpf-next 2/3] bpf: tcp: Support arbitrary SYN Cookie.
From: Martin KaFai Lau <[email protected]>
Date: Tue, 5 Dec 2023 17:20:53 -0800
Quoted 29 lines · Kuniyuki Iwashima
Quoted 28 lines
include/net/request_sock.h +20
-0@@ -83,6 +83,41 @@ static inline struct sock *req_to_sk(struct request_sock *req)return (struct sock *)req; } +/** +* skb_steal_sock - steal a socket from an sk_buff +* @skb: sk_buff to steal the socket from +* @refcounted: is set to true if the socket is reference-counted +* @prefetched: is set to true if the socket was assigned from bpf +*/ +static inline struct sock * +skb_steal_sock (struct sk_buff *skb,bool *refcounted,bool *prefetched) +{ +struct sock *sk = skb->sk; + +if (!skb->sk) { + *prefetched = false; + *refcounted = false; +return NULL ; + } + + *prefetched =skb_sk_is_prefetched (skb); +if (*prefetched) { +#if IS_ENABLED (CONFIG_SYN_COOKIES )
Quoted 8 lines
include/net/request_sock.h static inline struct sock *req_to_sk(struct request_sock *req) · line 108
Quoted 3 lines
include/net/request_sock.h +3
-0+if (sk->sk_state ==TCP_NEW_SYN_RECV &&inet_reqsk (sk)->syncookie) { + *refcounted = false; +return inet_reqsk (sk)->rsk_listener;If it does not break later logic, I would set inet_reqsk(sk)->rsk_listener to NULL to avoid inconsistency when the later inet[6]_lookup_reuseport() selects another listener.
skb_steal_sock()steals the inet_reqsk(sk)->rsk_listener in this sense.
That makes sense.
I'll clear rsk_listener in the next spin.
Quoted 66 lines
Quoted 14 lines
include/net/request_sock.h +12
-0+ } +#endif + *refcounted =sk_is_refcounted (sk); + }else { + *refcounted = true; + } + + skb->destructor =NULL ; + skb->sk =NULL ; +return sk; +} +[ ... ]
Quoted 49 lines
net/core/filter.c +41
-0@@ -11816,6 +11816,94 @@ __bpf_kfunc int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern,return 0 ; } + +__bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct sk_buff *skb, struct sock *sk, +struct tcp_cookie_attributes *attr, +int attr__sz) +{ +#if IS_ENABLED (CONFIG_SYN_COOKIES ) +const struct request_sock_ops *ops; +struct inet_request_sock *ireq; +struct tcp_request_sock *treq; +struct request_sock *req; +__u16 min_mss; + +if (attr__sz !=sizeof (*attr) || attr->tcp_opt.unused) +return -EINVAL ; + +if (!sk) +return -EINVAL ; + +if (!skb_at_tc_ingress (skb)) +return -EINVAL ; + +if (dev_net (skb->dev) !=sock_net (sk)) +return -ENETUNREACH ; + +switch (skb->protocol) { +case htons (ETH_P_IP ): + ops = &tcp_request_sock_ops; + min_mss =536 ; +break ; +#if IS_BUILTIN (CONFIG_IPV6 ) +case htons (ETH_P_IPV6 ): + ops = &tcp6_request_sock_ops; + min_mss =IPV6_MIN_MTU -60 ; +break ; +#endif +default : +return -EINVAL ; + } + +if (sk->sk_type !=SOCK_STREAM || sk->sk_state !=TCP_LISTEN ) +return -EINVAL ;
Quoted 8 lines
net/core/filter.c __bpf_kfunc int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern, · line 11862
Quoted 3 lines
net/core/filter.c +3
-0+ +if (attr->tcp_opt.mss_clamp < min_mss) { +__NET_INC_STATS (sock_net (sk),LINUX_MIB_SYNCOOKIESFAILED );hmm... this one I am not sure if the kfunc should decide what counted as SYNCOOKIESFAILED or not. Beside, the bpf prog should have already rejected the skb as part of its cookie validation logic. Thus, reaching here is more like a bpf prog's error instead.
Indeed.
Quoted 3 lines
I would leave the SYNCOOKIESFAILED usage for the kernel tcp layer only. The existing
bpf_tcp_check_syncookie()helper does not increment SYNCOOKIESFAILED also.
I'll remove __NET_INC_STATS()s from kfunc.
Thanks!
Quoted 111 lines
net/core/filter.c __bpf_kfunc int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern, · line 11869
Quoted 8 lines
net/core/filter.c +7
-0+return -EINVAL ; + } + +if (attr->tcp_opt.wscale_ok && + (attr->tcp_opt.snd_wscale >TCP_MAX_WSCALE || + attr->tcp_opt.rcv_wscale >TCP_MAX_WSCALE )) { +__NET_INC_STATS (sock_net (sk),LINUX_MIB_SYNCOOKIESFAILED );Same here.
Quoted 100 lines
net/core/filter.c +49
-2+return -EINVAL ; + } + +if (sk_is_mptcp (sk)) + req =mptcp_subflow_reqsk_alloc (ops, sk, false); +else + req =inet_reqsk_alloc (ops, sk, false); + +if (!req) +return -ENOMEM ; + + ireq =inet_rsk (req); + treq =tcp_rsk (req); + + req->syncookie =1 ; + req->rsk_listener = sk; + req->mss = attr->tcp_opt.mss_clamp; + + ireq->snd_wscale = attr->tcp_opt.snd_wscale; + ireq->rcv_wscale = attr->tcp_opt.rcv_wscale; + ireq->wscale_ok = attr->tcp_opt.wscale_ok; + ireq->tstamp_ok = attr->tcp_opt.tstamp_ok; + ireq->sack_ok = attr->tcp_opt.sack_ok; + ireq->ecn_ok = attr->ecn_ok; + + treq->req_usec_ts = attr->usec_ts_ok; + +skb_orphan (skb); + skb->sk =req_to_sk (req); + skb->destructor = sock_pfree; + +return 0 ; +#else +return -EOPNOTSUPP ; +#endif +} +__bpf_kfunc_end_defs (); int bpf_dynptr_from_skb_rdonly(struct sk_buff *skb, u64 flags,@@ -11844,6 +11932,10 @@ BTF_SET8_START(bpf_kfunc_check_set_sock_addr) BTF_ID_FLAGS(func, bpf_sock_addr_set_sun_path)BTF_SET8_END (bpf_kfunc_check_set_sock_addr) +BTF_SET8_START (bpf_kfunc_check_set_tcp_reqsk ) +BTF_ID_FLAGS (func, bpf_sk_assign_tcp_reqsk) +BTF_SET8_END (bpf_kfunc_check_set_tcp_reqsk) + static const struct btf_kfunc_id_set bpf_kfunc_set_skb = { .owner =THIS_MODULE , .set = &bpf_kfunc_check_set_skb,@@ -11859,6 +11951,11 @@ static const struct btf_kfunc_id_set bpf_kfunc_set_sock_addr = { .set = &bpf_kfunc_check_set_sock_addr, }; +static const struct btf_kfunc_id_set bpf_kfunc_set_tcp_reqsk = { + .owner =THIS_MODULE , + .set = &bpf_kfunc_check_set_tcp_reqsk, +}; +static int __init bpf_kfunc_init (void ) {int ret;@@ -11874,8 +11971,9 @@ static int __init bpf_kfunc_init(void) ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_LWT_SEG6LOCAL , &bpf_kfunc_set_skb); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_NETFILTER , &bpf_kfunc_set_skb); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_XDP , &bpf_kfunc_set_xdp); -return ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_CGROUP_SOCK_ADDR , - &bpf_kfunc_set_sock_addr); + ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_CGROUP_SOCK_ADDR , + &bpf_kfunc_set_sock_addr); +return ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SCHED_CLS , &bpf_kfunc_set_tcp_reqsk); }late_initcall (bpf_kfunc_init);net/core/sock.c +12
-2@@ -2579,8 +2579,18 @@ EXPORT_SYMBOL(sock_efree);#ifdef CONFIG_INET void sock_pfree (struct sk_buff *skb) { -if (sk_is_refcounted (skb->sk)) -sock_gen_put (skb->sk); +struct sock *sk = skb->sk; + +if (!sk_is_refcounted (sk)) +return ; + +if (sk->sk_state ==TCP_NEW_SYN_RECV &&inet_reqsk (sk)->syncookie) { +inet_reqsk (sk)->rsk_listener =NULL ; +reqsk_free (inet_reqsk (sk)); +return ; + } + +sock_gen_put (sk); }EXPORT_SYMBOL (sock_pfree);#endif /* CONFIG_INET */
PATCH v4 3/3 selftest: bpf: Test bpf_sk_assign_tcp_reqsk().
In reply to Kuniyuki IwashimaThis commit adds a sample selftest to demonstrate how we can use bpf_sk_assign_tcp_reqsk() as the backend of SYN Proxy.
The test creates IPv4/IPv6 x TCP/MPTCP connections and transfer messages over them on lo with BPF tc prog attached.
The tc prog will process SYN and returns SYN+ACK with the following ISN and TS. In a real use case, this part will be done by other hosts.
MSB LSB
ISN: | 31 ... 8 | 7 6 | 5 | 4 | 3 2 1 0 |
| Hash_1 | MSS | ECN | SACK | WScale | TS: | 31 ... 8 | 7 ... 0 |
| Random | Hash_2 |WScale in SYN is reused in SYN+ACK.
The client returns ACK, and tc prog will recalculate ISN and TS from ACK and validate SYN Cookie.
If it's valid, the prog calls kfunc to allocate a reqsk for skb and configure the reqsk based on the argument created from SYN Cookie.
Later, the reqsk will be processed in cookie_v[46]_check() to create a connection.
--- tools/testing/selftests/bpf/bpf_kfuncs.h | 10 + tools/testing/selftests/bpf/config | 1 + .../bpf/prog_tests/tcp_custom_syncookie.c | 163 +++++ .../selftests/bpf/progs/test_siphash.h | 64 ++ .../bpf/progs/test_tcp_custom_syncookie.c | 573 ++++++++++++++++++ .../bpf/progs/test_tcp_custom_syncookie.h | 162 +++++ 6 files changed, 973 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/tcp_custom_syncookie.c create mode 100644 tools/testing/selftests/bpf/progs/test_siphash.h create mode 100644 tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c create mode 100644 tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h
tools/testing/selftests/bpf/bpf_kfuncs.h +10 -0
@@ -51,6 +51,16 @@ extern int bpf_dynptr_clone(const struct bpf_dynptr *ptr, struct bpf_dynptr *clo extern int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern, const __u8 *sun_path, __u32 sun_path__sz) __ksym; +/* Description +* Allocate and configure a reqsk and link it with a listener and skb. +* Returns +* Error code +*/ +struct sock ; +struct tcp_cookie_attributes ; +extern int bpf_sk_assign_tcp_reqsk(struct __sk_buff *skb, struct sock *sk, + struct tcp_cookie_attributes *attr, int attr__sz) __ksym; +void *bpf_cast_to_kern_ctx (void *) __ksym;void *bpf_rdonly_cast (void *obj,__u32 btf_id) __ksym;
tools/testing/selftests/bpf/config +1 -0
@@ -81,6 +81,7 @@ CONFIG_NF_NAT=y CONFIG_RC_CORE=y CONFIG_SECURITY=y CONFIG_SECURITYFS=y +CONFIG_SYN_COOKIES=y CONFIG_TEST_BPF=m CONFIG_USERFAULTFD=y CONFIG_VSOCKETS=y
tools/testing/selftests/bpf/prog_tests/tcp_custom_syncookie.c new file +163 -0
163 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_siphash.h new file +64 -0
64 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c new file +573 -0
573 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h new file +162 -0
162 lines not on this page · raw message
Signature · 1 line
2.30.2
Re: [PATCH v4 bpf-next 3/3] selftest: bpf: Test bpf_sk_assign_tcp_reqsk().
Quoted 2 lines · Kuniyuki Iwashima
+static
__always_inlineinttcp_load_headers(struct tcp_syncookie *ctx)
...
Quoted 1 line
+static
__always_inlineinttcp_reload_headers(struct tcp_syncookie *ctx)
please remove __always_inline here and in all other places. The generated code will be much better == faster and the verifier should be able to understand it.
Re: [PATCH v4 bpf-next 3/3] selftest: bpf: Test bpf_sk_assign_tcp_reqsk().
From: Alexei Starovoitov <[email protected]>
Date: Mon, 4 Dec 2023 18:13:55 -0800
Quoted 11 lines · Kuniyuki Iwashima
Quoted 2 lines
+static
__always_inlineinttcp_load_headers(struct tcp_syncookie *ctx)...
Quoted 1 line
+static
__always_inlineinttcp_reload_headers(struct tcp_syncookie *ctx)please remove
__always_inlinehere and in all other places. The generated code will be much better == faster and the verifier should be able to understand it.
I confirmed the test worked without __always_inline. I'll fix it in v5.
Thanks!
Martin KaFai Lau
reviewerRe: [PATCH v4 bpf-next 3/3] selftest: bpf: Test bpf_sk_assign_tcp_reqsk().
In reply to Kuniyuki IwashimaQuoted 10 lines · Kuniyuki Iwashima
This commit adds a sample selftest to demonstrate how we can use
bpf_sk_assign_tcp_reqsk()as the backend of SYN Proxy.The test creates IPv4/IPv6 x TCP/MPTCP connections and transfer messages over them on lo with BPF tc prog attached.
The tc prog will process SYN and returns SYN+ACK with the following ISN and TS. In a real use case, this part will be done by other hosts.
Quoted 6 lines
MSB LSB
ISN: | 31 ... 8 | 7 6 | 5 | 4 | 3 2 1 0 | | Hash_1 | MSS | ECN | SACK | WScale |TS: | 31 ... 8 | 7 ... 0 | | Random | Hash_2 |
Thanks for the details. It is helpful.
Quoted 12 lines
WScale in SYN is reused in SYN+ACK.
The client returns ACK, and tc prog will recalculate ISN and TS from ACK and validate SYN Cookie.
If it's valid, the prog calls kfunc to allocate a reqsk for skb and configure the reqsk based on the argument created from SYN Cookie.
Later, the reqsk will be processed in cookie_v[46]_check() to create a connection.
[ ... ]
Quoted 11 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c new file +11
-0+SEC("tc") +int tcp_custom_syncookie (struct __sk_buff *skb) +{ +struct tcp_syncookie ctx = { + .skb = skb, + }; + +if (tcp_load_headers (&ctx)) +return TC_ACT_OK ; + +if (ctx.tcp->rst)
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c line 570
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c new file +10
-0+return TC_ACT_OK ; + +if (ctx.tcp->syn) { +if (ctx.tcp->ack) +return TC_ACT_OK ; + +return tcp_handle_syn (&ctx); + } + +return tcp_handle_ack (&ctx);
It may be useful to ensure tcp_handle_{syn,ack} is executed instead of the kernel doing the regular syncookie. A global variable (bool or counter) can be used by the prog_tests to check.
Quoted 25 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c new file +3
-0+} + +char _license[] SEC("license") = "GPL";tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h new file +16
-0@@ -0,0 +1,162 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright Amazon.com Inc. or its affiliates. */ + +#ifndef _TEST_TCP_SYNCOOKIE_H +#define _TEST_TCP_SYNCOOKIE_H + +#define TC_ACT_OK 0 +#define TC_ACT_SHOT 2 + +#define ETH_ALEN 6 +#define ETH_P_IP 0x0800 +#define ETH_P_IPV6 0x86DD + +#define NEXTHDR_TCP 6 + +#define TCPOPT_NOP 1
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h line 26
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h new file +10
-0+#define TCPOPT_EOL 0 +#define TCPOPT_MSS 2 +#define TCPOPT_WINDOW 3 +#define TCPOPT_TIMESTAMP 8 +#define TCPOPT_SACK_PERM 4 + +#define TCPOLEN_MSS 4 +#define TCPOLEN_WINDOW 3 +#define TCPOLEN_TIMESTAMP 10 +#define TCPOLEN_SACK_PERM 2
Some of the above is already in the bpf_tracing_net.h. Move the non-existing ones to bpf_tracing_net.h also.
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h line 27
Quoted 1 line
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h new file +1
-0+#define BPF_F_CURRENT_NETNS (-1)
This should be already in the vmlinux.h
Quoted 124 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h new file +124
-0+ +#define __packed __attribute__((__packed__)) +#define __force + +#define ARRAY_SIZE (arr) (sizeof(arr) / sizeof((arr)[0])) + +#define swap (a, b) \ + do { \ + typeof(a) __tmp = (a); \ + (a) = (b); \ + (b) = __tmp; \ + } while (0) + +#define swap_array (a, b) \ + do { \ + typeof(a) __tmp[sizeof(a)]; \ + __builtin_memcpy(__tmp, a, sizeof(a)); \ + __builtin_memcpy(a, b, sizeof(a)); \ + __builtin_memcpy(b, __tmp, sizeof(a)); \ + } while (0) + +/* asm-generic/unaligned.h */ +#define __get_unaligned_t (type, ptr) ({ \ + const struct { type x; } __packed * __pptr = (typeof(__pptr))(ptr); \ + __pptr->x; \ +}) + +#define get_unaligned (ptr) __get_unaligned_t(typeof(*(ptr)), (ptr)) + +static inline u16 get_unaligned_be16 (const void *p) +{ +return bpf_ntohs (__get_unaligned_t (__be16, p)); +} + +static inline u32 get_unaligned_be32 (const void *p) +{ +return bpf_ntohl (__get_unaligned_t (__be32, p)); +} + +/* lib/checksum.c */ +static inline u32 from64to32 (u64 x) +{ +/* add up 32-bit and 32-bit for 32+c bit */ + x = (x &0xffffffff ) + (x >>32 ); +/* add up carry.. */ + x = (x &0xffffffff ) + (x >>32 ); +return (u32 )x; +} + +static inline __wsum csum_tcpudp_nofold (__be32 saddr,__be32 daddr, +__u32 len,__u8 proto,__wsum sum) +{ +unsigned long long s = (__force u32 )sum; + + s += (__force u32 )saddr; + s += (__force u32 )daddr; +#ifdef __BIG_ENDIAN + s += proto + len; +#else + s += (proto + len) <<8 ; +#endif +return (__force __wsum )from64to32 (s); +} + +/* asm-generic/checksum.h */ +static inline __sum16 csum_fold (__wsum csum) +{ +u32 sum = (__force u32 )csum; + + sum = (sum &0xffff ) + (sum >>16 ); + sum = (sum &0xffff ) + (sum >>16 ); +return (__force __sum16 )~sum; +} + +static inline __sum16 csum_tcpudp_magic (__be32 saddr,__be32 daddr,__u32 len, +__u8 proto,__wsum sum) +{ +return csum_fold (csum_tcpudp_nofold (saddr, daddr, len, proto, sum)); +} + +/* net/ipv6/ip6_checksum.c */ +static inline __sum16 csum_ipv6_magic (const struct in6_addr *saddr, +const struct in6_addr *daddr, +__u32 len,__u8 proto,__wsum csum) +{ +int carry; +__u32 ulen; +__u32 uproto; +__u32 sum = (__force u32 )csum; + + sum += (__force u32 )saddr->in6_u.u6_addr32[0 ]; + carry = (sum < (__force u32 )saddr->in6_u.u6_addr32[0 ]); + sum += carry; + + sum += (__force u32 )saddr->in6_u.u6_addr32[1 ]; + carry = (sum < (__force u32 )saddr->in6_u.u6_addr32[1 ]); + sum += carry; + + sum += (__force u32 )saddr->in6_u.u6_addr32[2 ]; + carry = (sum < (__force u32 )saddr->in6_u.u6_addr32[2 ]); + sum += carry; + + sum += (__force u32 )saddr->in6_u.u6_addr32[3 ]; + carry = (sum < (__force u32 )saddr->in6_u.u6_addr32[3 ]); + sum += carry; + + sum += (__force u32 )daddr->in6_u.u6_addr32[0 ]; + carry = (sum < (__force u32 )daddr->in6_u.u6_addr32[0 ]); + sum += carry; + + sum += (__force u32 )daddr->in6_u.u6_addr32[1 ]; + carry = (sum < (__force u32 )daddr->in6_u.u6_addr32[1 ]); + sum += carry; + + sum += (__force u32 )daddr->in6_u.u6_addr32[2 ]; + carry = (sum < (__force u32 )daddr->in6_u.u6_addr32[2 ]); + sum += carry; + + sum += (__force u32 )daddr->in6_u.u6_addr32[3 ]; + carry = (sum < (__force u32 )daddr->in6_u.u6_addr32[3 ]); + sum += carry; + + ulen = (__force u32 )bpf_htonl ((__u32 )len); + sum += ulen;
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h line 161
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h new file +10
-0+ carry = (sum < ulen); + sum += carry; + + uproto = (__force u32 )bpf_htonl (proto); + sum += uproto; + carry = (sum < uproto); + sum += carry; + +return csum_fold ((__force __wsum )sum); +}
The above helpers are useful for other tests, so make sense to stay in test_tcp_custom_syncookie.h. e.g. In the future, some of the duplicated helpers in xdp_synproxy_kern.c can be removed.
Quoted 1 line
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h new file +1
-0+#endif
Re: [PATCH v4 bpf-next 3/3] selftest: bpf: Test bpf_sk_assign_tcp_reqsk().
From: Martin KaFai Lau <[email protected]>
Date: Tue, 5 Dec 2023 22:39:33 -0800
Quoted 52 lines · Kuniyuki Iwashima
Quoted 10 lines
This commit adds a sample selftest to demonstrate how we can use
bpf_sk_assign_tcp_reqsk()as the backend of SYN Proxy.The test creates IPv4/IPv6 x TCP/MPTCP connections and transfer messages over them on lo with BPF tc prog attached.
The tc prog will process SYN and returns SYN+ACK with the following ISN and TS. In a real use case, this part will be done by other hosts.
Quoted 6 lines
MSB LSB
ISN: | 31 ... 8 | 7 6 | 5 | 4 | 3 2 1 0 | | Hash_1 | MSS | ECN | SACK | WScale |TS: | 31 ... 8 | 7 ... 0 | | Random | Hash_2 |Thanks for the details. It is helpful.
Quoted 12 lines
WScale in SYN is reused in SYN+ACK.
The client returns ACK, and tc prog will recalculate ISN and TS from ACK and validate SYN Cookie.
If it's valid, the prog calls kfunc to allocate a reqsk for skb and configure the reqsk based on the argument created from SYN Cookie.
Later, the reqsk will be processed in cookie_v[46]_check() to create a connection.
[ ... ]
Quoted 17 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c new file +17
-0+SEC("tc") +int tcp_custom_syncookie (struct __sk_buff *skb) +{ +struct tcp_syncookie ctx = { + .skb = skb, + }; + +if (tcp_load_headers (&ctx)) +return TC_ACT_OK ; + +if (ctx.tcp->rst) +return TC_ACT_OK ; + +if (ctx.tcp->syn) { +if (ctx.tcp->ack) +return TC_ACT_OK ; +
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c line 570
Quoted 4 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c new file +4
-0+return tcp_handle_syn (&ctx); + } + +return tcp_handle_ack (&ctx);It may be useful to ensure tcp_handle_{syn,ack} is executed instead of the kernel doing the regular syncookie. A global variable (bool or counter) can be used by the
prog_teststo check.
Sure, will add that check.
Quoted 31 lines
Quoted 31 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c new file +3
-0+} + +char _license[] SEC("license") = "GPL";tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h new file +21
-0@@ -0,0 +1,162 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright Amazon.com Inc. or its affiliates. */ + +#ifndef _TEST_TCP_SYNCOOKIE_H +#define _TEST_TCP_SYNCOOKIE_H + +#define TC_ACT_OK 0 +#define TC_ACT_SHOT 2 + +#define ETH_ALEN 6 +#define ETH_P_IP 0x0800 +#define ETH_P_IPV6 0x86DD + +#define NEXTHDR_TCP 6 + +#define TCPOPT_NOP 1 +#define TCPOPT_EOL 0 +#define TCPOPT_MSS 2 +#define TCPOPT_WINDOW 3 +#define TCPOPT_TIMESTAMP 8 +#define TCPOPT_SACK_PERM 4
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h line 26
Quoted 5 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h new file +5
-0+ +#define TCPOLEN_MSS 4 +#define TCPOLEN_WINDOW 3 +#define TCPOLEN_TIMESTAMP 10 +#define TCPOLEN_SACK_PERM 2Some of the above is already in the
bpf_tracing_net.h. Move the non-existing ones tobpf_tracing_net.halso.
Will move it.
Quoted 4 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h line 27
Quoted 2 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h new file +1
-0+#define BPF_F_CURRENT_NETNS (-1)This should be already in the
vmlinux.h
I thought so, but the kernel robot complained ...
https://lore.kernel.org/bpf/[email protected]/
Quoted 131 lines
Quoted 131 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h new file +130
-0+ +#define __packed __attribute__((__packed__)) +#define __force + +#define ARRAY_SIZE (arr) (sizeof(arr) / sizeof((arr)[0])) + +#define swap (a, b) \ + do { \ + typeof(a) __tmp = (a); \ + (a) = (b); \ + (b) = __tmp; \ + } while (0) + +#define swap_array (a, b) \ + do { \ + typeof(a) __tmp[sizeof(a)]; \ + __builtin_memcpy(__tmp, a, sizeof(a)); \ + __builtin_memcpy(a, b, sizeof(a)); \ + __builtin_memcpy(b, __tmp, sizeof(a)); \ + } while (0) + +/* asm-generic/unaligned.h */ +#define __get_unaligned_t (type, ptr) ({ \ + const struct { type x; } __packed * __pptr = (typeof(__pptr))(ptr); \ + __pptr->x; \ +}) + +#define get_unaligned (ptr) __get_unaligned_t(typeof(*(ptr)), (ptr)) + +static inline u16 get_unaligned_be16 (const void *p) +{ +return bpf_ntohs (__get_unaligned_t (__be16, p)); +} + +static inline u32 get_unaligned_be32 (const void *p) +{ +return bpf_ntohl (__get_unaligned_t (__be32, p)); +} + +/* lib/checksum.c */ +static inline u32 from64to32 (u64 x) +{ +/* add up 32-bit and 32-bit for 32+c bit */ + x = (x &0xffffffff ) + (x >>32 ); +/* add up carry.. */ + x = (x &0xffffffff ) + (x >>32 ); +return (u32 )x; +} + +static inline __wsum csum_tcpudp_nofold (__be32 saddr,__be32 daddr, +__u32 len,__u8 proto,__wsum sum) +{ +unsigned long long s = (__force u32 )sum; + + s += (__force u32 )saddr; + s += (__force u32 )daddr; +#ifdef __BIG_ENDIAN + s += proto + len; +#else + s += (proto + len) <<8 ; +#endif +return (__force __wsum )from64to32 (s); +} + +/* asm-generic/checksum.h */ +static inline __sum16 csum_fold (__wsum csum) +{ +u32 sum = (__force u32 )csum; + + sum = (sum &0xffff ) + (sum >>16 ); + sum = (sum &0xffff ) + (sum >>16 ); +return (__force __sum16 )~sum; +} + +static inline __sum16 csum_tcpudp_magic (__be32 saddr,__be32 daddr,__u32 len, +__u8 proto,__wsum sum) +{ +return csum_fold (csum_tcpudp_nofold (saddr, daddr, len, proto, sum)); +} + +/* net/ipv6/ip6_checksum.c */ +static inline __sum16 csum_ipv6_magic (const struct in6_addr *saddr, +const struct in6_addr *daddr, +__u32 len,__u8 proto,__wsum csum) +{ +int carry; +__u32 ulen; +__u32 uproto; +__u32 sum = (__force u32 )csum; + + sum += (__force u32 )saddr->in6_u.u6_addr32[0 ]; + carry = (sum < (__force u32 )saddr->in6_u.u6_addr32[0 ]); + sum += carry; + + sum += (__force u32 )saddr->in6_u.u6_addr32[1 ]; + carry = (sum < (__force u32 )saddr->in6_u.u6_addr32[1 ]); + sum += carry; + + sum += (__force u32 )saddr->in6_u.u6_addr32[2 ]; + carry = (sum < (__force u32 )saddr->in6_u.u6_addr32[2 ]); + sum += carry; + + sum += (__force u32 )saddr->in6_u.u6_addr32[3 ]; + carry = (sum < (__force u32 )saddr->in6_u.u6_addr32[3 ]); + sum += carry; + + sum += (__force u32 )daddr->in6_u.u6_addr32[0 ]; + carry = (sum < (__force u32 )daddr->in6_u.u6_addr32[0 ]); + sum += carry; + + sum += (__force u32 )daddr->in6_u.u6_addr32[1 ]; + carry = (sum < (__force u32 )daddr->in6_u.u6_addr32[1 ]); + sum += carry; + + sum += (__force u32 )daddr->in6_u.u6_addr32[2 ]; + carry = (sum < (__force u32 )daddr->in6_u.u6_addr32[2 ]); + sum += carry; + + sum += (__force u32 )daddr->in6_u.u6_addr32[3 ]; + carry = (sum < (__force u32 )daddr->in6_u.u6_addr32[3 ]); + sum += carry; + + ulen = (__force u32 )bpf_htonl ((__u32 )len); + sum += ulen; + carry = (sum < ulen); + sum += carry; + + uproto = (__force u32 )bpf_htonl (proto); + sum += uproto; + carry = (sum < uproto);
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h line 161
Quoted 4 lines
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h new file +4
-0+ sum += carry; + +return csum_fold ((__force __wsum )sum); +}The above helpers are useful for other tests, so make sense to stay in
test_tcp_custom_syncookie.h. e.g. In the future, some of the duplicated helpers inxdp_synproxy_kern.ccan be removed.
Will post a followup patch after this series was merged.
Thanks!
Martin KaFai Lau
reviewerRe: [PATCH v4 bpf-next 3/3] selftest: bpf: Test bpf_sk_assign_tcp_reqsk().
Quoted 4 lines · Kuniyuki Iwashima
Quoted 2 lines
Quoted 1 line
+#define
BPF_F_CURRENT_NETNS(-1)This should be already in the
vmlinux.hI thought so, but the kernel robot complained ...
https://lore.kernel.org/bpf/[email protected]/
may be the BPF_F_CURRENT_NETNS is not used in the net/core/filter.c and then not get into the vmlinux's btf...
just pass -1 for now.