bpf: tcp: Support arbitrary SYN Cookie at TC.
Subsystems (9)
v2 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 is down, all the connections through it are also down. 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. Also, ISN contains WScale, SACK, and ECN, not in TS val. This is not to sacrifice any connection quality, where some customers turn off the timestamp option due to retro CVE.
After 3WHS, the proxy restores SYN and forwards it and ACK 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 and 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,
.snd_scale = send_scale, /* < 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);For details, please see each patch. Here's an overview:
patch 1 - 6 : Misc cleanup
patch 7, 8 : Factorise non-BPF SYN Cookie handling patch 9, 10 : Support arbitrary SYN Cookie with BPF
patch 11 : Selftest
[0]: https://lpc.events/event/17/contributions/1645/attachments/1350/2701/SYN_Proxy_at_Scale_with_BPF.pdf
Changes:
v2:
* 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 (11):
tcp: Clean up reverse xmas tree in cookie_v[46]_check().
tcp: Cache sock_net(sk) in cookie_v[46]_check().
tcp: Clean up goto labels in cookie_v[46]_check().
tcp: Don't pass cookie to __cookie_v[46]_check().
tcp: Don't initialise tp->tsoffset in tcp_get_cookie_sock().
tcp: Move TCP-AO bits from cookie_v[46]_check() to tcp_ao_syncookie().
tcp: Factorise cookie req initialisation.
tcp: Factorise non-BPF SYN Cookie handling.
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/linux/netfilter_ipv6.h | 8 +- include/net/inet6_hashtables.h | 14 +- include/net/inet_hashtables.h | 14 +- include/net/tcp.h | 49 +- include/net/tcp_ao.h | 6 +- net/core/filter.c | 111 +++- net/core/sock.c | 14 +- net/ipv4/syncookies.c | 273 +++++---- net/ipv4/tcp_ao.c | 16 +- net/ipv6/syncookies.c | 112 ++-- net/netfilter/nf_synproxy_core.c | 4 +- tools/testing/selftests/bpf/bpf_kfuncs.h | 10 + .../bpf/prog_tests/tcp_custom_syncookie.c | 163 +++++ .../selftests/bpf/progs/test_siphash.h | 64 ++ .../bpf/progs/test_tcp_custom_syncookie.c | 570 ++++++++++++++++++ .../bpf/progs/test_tcp_custom_syncookie.h | 161 +++++ 16 files changed, 1387 insertions(+), 202 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 v2 1/11 tcp: Clean up reverse xmas tree in cookie_v[46]_check().
We will grow and cut the xmas tree in cookie_v[46]_check(). This patch cleans it up to make later patches tidy.
--- net/ipv4/syncookies.c | 10 +++++----- net/ipv6/syncookies.c | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-)
net/ipv4/syncookies.c +5 -5
@@ -331,18 +331,18 @@ EXPORT_SYMBOL_GPL(cookie_tcp_reqsk_alloc);struct sock *cookie_v4_check (struct sock *sk,struct sk_buff *skb) {struct ip_options *opt = &TCP_SKB_CB (skb)->header.h4.opt; +const struct tcphdr *th =tcp_hdr (skb); +__u32 cookie =ntohl (th->ack_seq) -1 ;struct tcp_options_received tcp_opt; +struct tcp_sock *tp =tcp_sk (sk);struct inet_request_sock *ireq;struct tcp_request_sock *treq; -struct tcp_sock *tp =tcp_sk (sk); -const struct tcphdr *th =tcp_hdr (skb); -__u32 cookie =ntohl (th->ack_seq) -1 ; -struct sock *ret = sk;struct request_sock *req; +struct sock *ret = sk;int full_space, mss; +struct flowi4 fl4;struct rtable *rt;__u8 rcv_wscale; -struct flowi4 fl4;u32 tsoff =0 ;int l3index;
net/ipv6/syncookies.c +6 -6
@@ -127,17 +127,17 @@ EXPORT_SYMBOL_GPL(__cookie_v6_check);struct sock *cookie_v6_check (struct sock *sk,struct sk_buff *skb) { +const struct tcphdr *th =tcp_hdr (skb); +__u32 cookie =ntohl (th->ack_seq) -1 ; +struct ipv6_pinfo *np =inet6_sk (sk);struct tcp_options_received tcp_opt; +struct tcp_sock *tp =tcp_sk (sk);struct inet_request_sock *ireq;struct tcp_request_sock *treq; -struct ipv6_pinfo *np =inet6_sk (sk); -struct tcp_sock *tp =tcp_sk (sk); -const struct tcphdr *th =tcp_hdr (skb); -__u32 cookie =ntohl (th->ack_seq) -1 ; -struct sock *ret = sk;struct request_sock *req; -int full_space, mss;struct dst_entry *dst; +struct sock *ret = sk; +int full_space, mss;__u8 rcv_wscale;u32 tsoff =0 ;int l3index;
Signature · 1 line
2.30.2
PATCH v2 2/11 tcp: Cache sock_net(sk) in cookie_v[46]_check().
In reply to Kuniyuki Iwashimasock_net(sk) is used repeatedly in cookie_v[46]_check(). Let's cache it in a variable.
--- net/ipv4/syncookies.c | 21 +++++++++++---------- net/ipv6/syncookies.c | 19 ++++++++++--------- 2 files changed, 21 insertions(+), 19 deletions(-)
net/ipv4/syncookies.c +11 -10
@@ -336,6 +336,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)struct tcp_options_received tcp_opt;struct tcp_sock *tp =tcp_sk (sk);struct inet_request_sock *ireq; +struct net *net =sock_net (sk);struct tcp_request_sock *treq;struct request_sock *req;struct sock *ret = sk;
@@ -346,7 +347,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)u32 tsoff =0 ;int l3index; -if (!READ_ONCE (sock_net (sk)->ipv4.sysctl_tcp_syncookies) || +if (!READ_ONCE (net->ipv4.sysctl_tcp_syncookies) || !th->ack || th->rst) goto out;
@@ -355,24 +356,24 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) mss =__cookie_v4_check (ip_hdr (skb), th, cookie);if (mss ==0 ) { -__NET_INC_STATS (sock_net (sk),LINUX_MIB_SYNCOOKIESFAILED ); +__NET_INC_STATS (net,LINUX_MIB_SYNCOOKIESFAILED ); goto out; } -__NET_INC_STATS (sock_net (sk),LINUX_MIB_SYNCOOKIESRECV ); +__NET_INC_STATS (net,LINUX_MIB_SYNCOOKIESRECV );/* check for timestamp cookie support */ memset (&tcp_opt,0 ,sizeof (tcp_opt)); -tcp_parse_options (sock_net (sk), skb, &tcp_opt,0 ,NULL ); +tcp_parse_options (net, skb, &tcp_opt,0 ,NULL );if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) { - tsoff =secure_tcp_ts_off (sock_net (sk), + tsoff =secure_tcp_ts_off (net,ip_hdr (skb)->daddr,ip_hdr (skb)->saddr); tcp_opt.rcv_tsecr -= tsoff; } -if (!cookie_timestamp_decode (sock_net (sk), &tcp_opt)) +if (!cookie_timestamp_decode (net, &tcp_opt)) goto out; ret =NULL ;
@@ -406,13 +407,13 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) ireq->ir_iif =inet_request_bound_dev_if (sk, skb); - l3index =l3mdev_master_ifindex_by_index (sock_net (sk), ireq->ir_iif); + l3index =l3mdev_master_ifindex_by_index (net, ireq->ir_iif);tcp_ao_syncookie (sk, skb, treq,AF_INET , l3index);/* We throwed the options of the initial SYN away, so we hope * the ACK carries the same options again (see RFC1122 4.2.3.8) */ -RCU_INIT_POINTER (ireq->ireq_opt,tcp_v4_save_options (sock_net (sk), skb)); +RCU_INIT_POINTER (ireq->ireq_opt,tcp_v4_save_options (net, skb));if (security_inet_conn_request (sk, skb, req)) {reqsk_free (req);
@@ -433,7 +434,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) opt->srr ? opt->faddr : ireq->ir_rmt_addr, ireq->ir_loc_addr, th->source, th->dest, sk->sk_uid);security_req_classify_flow (req,flowi4_to_flowi_common (&fl4)); - rt =ip_route_output_key (sock_net (sk), &fl4); + rt =ip_route_output_key (net, &fl4);if (IS_ERR (rt)) {reqsk_free (req); goto out;
@@ -453,7 +454,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) dst_metric(&rt->dst, RTAX_INITRWND)); ireq->rcv_wscale = rcv_wscale; - ireq->ecn_ok =cookie_ecn_ok (&tcp_opt,sock_net (sk), &rt->dst); + ireq->ecn_ok =cookie_ecn_ok (&tcp_opt, net, &rt->dst); ret =tcp_get_cookie_sock (sk, skb, req, &rt->dst, tsoff);/* ip_queue_xmit() depends on our flow being setup
net/ipv6/syncookies.c +10 -9
@@ -133,6 +133,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)struct tcp_options_received tcp_opt;struct tcp_sock *tp =tcp_sk (sk);struct inet_request_sock *ireq; +struct net *net =sock_net (sk);struct tcp_request_sock *treq;struct request_sock *req;struct dst_entry *dst;
@@ -142,7 +143,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)u32 tsoff =0 ;int l3index; -if (!READ_ONCE (sock_net (sk)->ipv4.sysctl_tcp_syncookies) || +if (!READ_ONCE (net->ipv4.sysctl_tcp_syncookies) || !th->ack || th->rst) goto out;
@@ -151,24 +152,24 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) mss =__cookie_v6_check (ipv6_hdr (skb), th, cookie);if (mss ==0 ) { -__NET_INC_STATS (sock_net (sk),LINUX_MIB_SYNCOOKIESFAILED ); +__NET_INC_STATS (net,LINUX_MIB_SYNCOOKIESFAILED ); goto out; } -__NET_INC_STATS (sock_net (sk),LINUX_MIB_SYNCOOKIESRECV ); +__NET_INC_STATS (net,LINUX_MIB_SYNCOOKIESRECV );/* check for timestamp cookie support */ memset (&tcp_opt,0 ,sizeof (tcp_opt)); -tcp_parse_options (sock_net (sk), skb, &tcp_opt,0 ,NULL ); +tcp_parse_options (net, skb, &tcp_opt,0 ,NULL );if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) { - tsoff =secure_tcpv6_ts_off (sock_net (sk), + tsoff =secure_tcpv6_ts_off (net,ipv6_hdr (skb)->daddr.s6_addr32,ipv6_hdr (skb)->saddr.s6_addr32); tcp_opt.rcv_tsecr -= tsoff; } -if (!cookie_timestamp_decode (sock_net (sk), &tcp_opt)) +if (!cookie_timestamp_decode (net, &tcp_opt)) goto out; ret =NULL ;
@@ -217,7 +218,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) treq->ts_off =0 ; treq->txhash =net_tx_rndhash (); - l3index =l3mdev_master_ifindex_by_index (sock_net (sk), ireq->ir_iif); + l3index =l3mdev_master_ifindex_by_index (net, ireq->ir_iif);tcp_ao_syncookie (sk, skb, treq,AF_INET6 , l3index);if (IS_ENABLED (CONFIG_SMC ))
@@ -243,7 +244,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) fl6.flowi6_uid = sk->sk_uid;security_req_classify_flow (req,flowi6_to_flowi_common (&fl6)); - dst =ip6_dst_lookup_flow (sock_net (sk), sk, &fl6, final_p); + dst =ip6_dst_lookup_flow (net, sk, &fl6, final_p);if (IS_ERR (dst)) goto out_free; }
@@ -261,7 +262,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) dst_metric(dst, RTAX_INITRWND)); ireq->rcv_wscale = rcv_wscale; - ireq->ecn_ok =cookie_ecn_ok (&tcp_opt,sock_net (sk), dst); + ireq->ecn_ok =cookie_ecn_ok (&tcp_opt, net, dst); ret =tcp_get_cookie_sock (sk, skb, req, dst, tsoff); out:
Signature · 1 line
2.30.2
PATCH v2 3/11 tcp: Clean up goto labels in cookie_v[46]_check().
In reply to Kuniyuki IwashimaWe will support arbitrary SYN Cookie with BPF, and then reqsk will be preallocated before cookie_v[46]_check().
Depending on how validation fails, we send RST or just drop skb.
To make the error handling easier, let's clean up goto labels.
--- net/ipv4/syncookies.c | 22 +++++++++++----------- net/ipv6/syncookies.c | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-)
net/ipv4/syncookies.c +11 -11
@@ -376,11 +376,10 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)if (!cookie_timestamp_decode (net, &tcp_opt)) goto out; - ret =NULL ; req =cookie_tcp_reqsk_alloc (&tcp_request_sock_ops, &tcp_request_sock_ipv4_ops, sk, skb);if (!req) - goto out; + goto out_drop; ireq =inet_rsk (req); treq =tcp_rsk (req);
@@ -415,10 +414,8 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)*/ RCU_INIT_POINTER (ireq->ireq_opt,tcp_v4_save_options (net, skb)); -if (security_inet_conn_request (sk, skb, req)) { -reqsk_free (req); - goto out; - } +if (security_inet_conn_request (sk, skb, req)) + goto out_free; req->num_retrans =0 ;
@@ -435,10 +432,8 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) ireq->ir_loc_addr, th->source, th->dest, sk->sk_uid);security_req_classify_flow (req,flowi4_to_flowi_common (&fl4)); rt =ip_route_output_key (net, &fl4); -if (IS_ERR (rt)) { -reqsk_free (req); - goto out; - } +if (IS_ERR (rt)) + goto out_free;/* Try to redo what tcp_v4_send_synack did. */ req->rsk_window_clamp = tp->window_clamp ? :dst_metric (&rt->dst,RTAX_WINDOW );
@@ -462,5 +457,10 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)*/ if (ret)inet_sk (ret)->cork.fl.u.ip4 = fl4; -out:return ret; +out: +return ret; +out_free: +reqsk_free (req); +out_drop: +return NULL ; }
net/ipv6/syncookies.c +2 -2
@@ -172,11 +172,10 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)if (!cookie_timestamp_decode (net, &tcp_opt)) goto out; - ret =NULL ; req =cookie_tcp_reqsk_alloc (&tcp6_request_sock_ops, &tcp_request_sock_ipv6_ops, sk, skb);if (!req) - goto out; + goto out_drop; ireq =inet_rsk (req); treq =tcp_rsk (req);
@@ -269,5 +268,6 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)return ret; out_free:reqsk_free (req); +out_drop:return NULL ; }
Signature · 1 line
2.30.2
PATCH v2 4/11 tcp: Don't pass cookie to __cookie_v[46]_check().
In reply to Kuniyuki Iwashimatcp_hdr(skb) and SYN Cookie are passed to __cookie_v[46]_check(), but none of the callers passes cookie other than ntohl(th->ack_seq) - 1.
Let's fetch it in __cookie_v[46]_check() instead of passing the cookie over and over.
--- include/linux/netfilter_ipv6.h | 8 ++++---- include/net/tcp.h | 6 ++---- net/core/filter.c | 15 ++++----------- net/ipv4/syncookies.c | 15 ++++++++------- net/ipv6/syncookies.c | 15 ++++++++------- net/netfilter/nf_synproxy_core.c | 4 ++-- 6 files changed, 28 insertions(+), 35 deletions(-)
include/linux/netfilter_ipv6.h +4 -4
@@ -51,7 +51,7 @@ struct nf_ipv6_ops {u32 (*cookie_init_sequence)(const struct ipv6hdr *iph,const struct tcphdr *th,u16 *mssp);int (*cookie_v6_check)(const struct ipv6hdr *iph, -const struct tcphdr *th,__u32 cookie); +const struct tcphdr *th);#endif void (*route_input)(struct sk_buff *skb); int (*fragment)(struct net *net, struct sock *sk, struct sk_buff *skb,
@@ -179,16 +179,16 @@ static inline u32 nf_ipv6_cookie_init_sequence(const struct ipv6hdr *iph, }static inline int nf_cookie_v6_check (const struct ipv6hdr *iph, -const struct tcphdr *th,__u32 cookie) +const struct tcphdr *th) {#if IS_ENABLED (CONFIG_SYN_COOKIES )#if IS_MODULE (CONFIG_IPV6 )const struct nf_ipv6_ops *v6_ops =nf_get_ipv6_ops ();if (v6_ops) -return v6_ops->cookie_v6_check (iph, th, cookie); +return v6_ops->cookie_v6_check (iph, th);#elif IS_BUILTIN (CONFIG_IPV6 ) -return __cookie_v6_check (iph, th, cookie); +return __cookie_v6_check (iph, th);#endif #endif return 0 ;
include/net/tcp.h +2 -4
@@ -491,8 +491,7 @@ void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb);struct sock *tcp_get_cookie_sock (struct sock *sk,struct sk_buff *skb,struct request_sock *req,struct dst_entry *dst,u32 tsoff); -int __cookie_v4_check (const struct iphdr *iph,const struct tcphdr *th, -u32 cookie); +int __cookie_v4_check (const struct iphdr *iph,const struct tcphdr *th);struct sock *cookie_v4_check (struct sock *sk,struct sk_buff *skb); struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops, const struct tcp_request_sock_ops *af_ops,
@@ -586,8 +585,7 @@ bool cookie_ecn_ok(const struct tcp_options_received *opt, const struct net *net, const struct dst_entry *dst);/* From net/ipv6/syncookies.c */ -int __cookie_v6_check (const struct ipv6hdr *iph,const struct tcphdr *th, -u32 cookie); +int __cookie_v6_check (const struct ipv6hdr *iph,const struct tcphdr *th);struct sock *cookie_v6_check (struct sock *sk,struct sk_buff *skb); u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
net/core/filter.c +4 -11
@@ -7229,7 +7229,6 @@ BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len struct tcphdr *, th, u32, th_len) {#ifdef CONFIG_SYN_COOKIES -u32 cookie;int ret;if (unlikely (!sk || th_len <sizeof (*th)))
@@ -7251,8 +7250,6 @@ BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_lenif (tcp_synq_no_recent_overflow (sk))return -ENOENT ; - cookie =ntohl (th->ack_seq) -1 ; -/* Both struct iphdr and struct ipv6hdr have the version field at the * same offset so we can cast to the shorter header (struct iphdr). */
@@ -7261,7 +7258,7 @@ BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_lenif (sk->sk_family ==AF_INET6 &&ipv6_only_sock (sk))return -EINVAL ; - ret =__cookie_v4_check ((struct iphdr *)iph, th, cookie); + ret =__cookie_v4_check ((struct iphdr *)iph, th);break ;#if IS_BUILTIN (CONFIG_IPV6 )
@@ -7272,7 +7269,7 @@ BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_lenif (sk->sk_family !=AF_INET6 )return -EINVAL ; - ret =__cookie_v6_check ((struct ipv6hdr *)iph, th, cookie); + ret =__cookie_v6_check ((struct ipv6hdr *)iph, th);break ;#endif /* CONFIG_IPV6 */
@@ -7725,9 +7722,7 @@ static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv6_proto = { BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv4, struct iphdr *, iph, struct tcphdr *, th) { -u32 cookie =ntohl (th->ack_seq) -1 ; - -if (__cookie_v4_check (iph, th, cookie) >0 ) +if (__cookie_v4_check (iph, th) >0 )return 0 ;return -EACCES ;
@@ -7748,9 +7743,7 @@ BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv6, struct ipv6hdr *, iph, struct tcphdr *, th) {#if IS_BUILTIN (CONFIG_IPV6 ) -u32 cookie =ntohl (th->ack_seq) -1 ; - -if (__cookie_v6_check (iph, th, cookie) >0 ) +if (__cookie_v6_check (iph, th) >0 )return 0 ;return -EACCES ;
net/ipv4/syncookies.c +8 -7
@@ -189,12 +189,14 @@ __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mssp)* Check if a ack sequence number is a valid syncookie. * Return the decoded mss if it is, or 0 if not. */ -int __cookie_v4_check (const struct iphdr *iph,const struct tcphdr *th, -u32 cookie) +int __cookie_v4_check (const struct iphdr *iph,const struct tcphdr *th) { +__u32 cookie =ntohl (th->ack_seq) -1 ;__u32 seq =ntohl (th->seq) -1 ; -__u32 mssind =check_tcp_syn_cookie (cookie, iph->saddr, iph->daddr, - th->source, th->dest, seq); +__u32 mssind; + + mssind =check_tcp_syn_cookie (cookie, iph->saddr, iph->daddr, + th->source, th->dest, seq);return mssind <ARRAY_SIZE (msstab) ? msstab[mssind] :0 ; }
@@ -332,7 +334,6 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) {struct ip_options *opt = &TCP_SKB_CB (skb)->header.h4.opt;const struct tcphdr *th =tcp_hdr (skb); -__u32 cookie =ntohl (th->ack_seq) -1 ;struct tcp_options_received tcp_opt;struct tcp_sock *tp =tcp_sk (sk);struct inet_request_sock *ireq;
@@ -354,7 +355,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)if (tcp_synq_no_recent_overflow (sk)) goto out; - mss =__cookie_v4_check (ip_hdr (skb), th, cookie); + mss =__cookie_v4_check (ip_hdr (skb), th);if (mss ==0 ) {__NET_INC_STATS (net,LINUX_MIB_SYNCOOKIESFAILED ); goto out;
@@ -384,7 +385,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) ireq =inet_rsk (req); treq =tcp_rsk (req); treq->rcv_isn =ntohl (th->seq) -1 ; - treq->snt_isn = cookie; + treq->snt_isn =ntohl (th->ack_seq) -1 ; treq->ts_off =0 ; treq->txhash =net_tx_rndhash (); req->mss = mss;
net/ipv6/syncookies.c +8 -7
@@ -114,12 +114,14 @@ __u32 cookie_v6_init_sequence(const struct sk_buff *skb, __u16 *mssp)return __cookie_v6_init_sequence (iph, th, mssp); } -int __cookie_v6_check (const struct ipv6hdr *iph,const struct tcphdr *th, -__u32 cookie) +int __cookie_v6_check (const struct ipv6hdr *iph,const struct tcphdr *th) { +__u32 cookie =ntohl (th->ack_seq) -1 ;__u32 seq =ntohl (th->seq) -1 ; -__u32 mssind =check_tcp_syn_cookie (cookie, &iph->saddr, &iph->daddr, - th->source, th->dest, seq); +__u32 mssind; + + mssind =check_tcp_syn_cookie (cookie, &iph->saddr, &iph->daddr, + th->source, th->dest, seq);return mssind <ARRAY_SIZE (msstab) ? msstab[mssind] :0 ; }
@@ -128,7 +130,6 @@ EXPORT_SYMBOL_GPL(__cookie_v6_check);struct sock *cookie_v6_check (struct sock *sk,struct sk_buff *skb) {const struct tcphdr *th =tcp_hdr (skb); -__u32 cookie =ntohl (th->ack_seq) -1 ;struct ipv6_pinfo *np =inet6_sk (sk);struct tcp_options_received tcp_opt;struct tcp_sock *tp =tcp_sk (sk);
@@ -150,7 +151,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)if (tcp_synq_no_recent_overflow (sk)) goto out; - mss =__cookie_v6_check (ipv6_hdr (skb), th, cookie); + mss =__cookie_v6_check (ipv6_hdr (skb), th);if (mss ==0 ) {__NET_INC_STATS (net,LINUX_MIB_SYNCOOKIESFAILED ); goto out;
@@ -213,7 +214,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) req->ts_recent = tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval :0 ; treq->snt_synack =0 ; treq->rcv_isn =ntohl (th->seq) -1 ; - treq->snt_isn = cookie; + treq->snt_isn =ntohl (th->ack_seq) -1 ; treq->ts_off =0 ; treq->txhash =net_tx_rndhash ();
net/netfilter/nf_synproxy_core.c +2 -2
@@ -617,7 +617,7 @@ synproxy_recv_client_ack(struct net *net,struct synproxy_net *snet =synproxy_pernet (net);int mss; - mss =__cookie_v4_check (ip_hdr (skb), th,ntohl (th->ack_seq) -1 ); + mss =__cookie_v4_check (ip_hdr (skb), th);if (mss ==0 ) {this_cpu_inc (snet->stats->cookie_invalid);return false;
@@ -1034,7 +1034,7 @@ synproxy_recv_client_ack_ipv6(struct net *net,struct synproxy_net *snet =synproxy_pernet (net);int mss; - mss =nf_cookie_v6_check (ipv6_hdr (skb), th,ntohl (th->ack_seq) -1 ); + mss =nf_cookie_v6_check (ipv6_hdr (skb), th);if (mss ==0 ) {this_cpu_inc (snet->stats->cookie_invalid);return false;
Signature · 1 line
2.30.2
PATCH v2 5/11 tcp: Don't initialise tp->tsoffset in tcp_get_cookie_sock().
In reply to Kuniyuki IwashimaWhen we create a full socket from SYN Cookie, we initialise tcp_sk(sk)->tsoffset redundantly in tcp_get_cookie_sock() as the field is inherited from tcp_rsk(req)->ts_off.
cookie_v[46]_check
|- treq->ts_off = 0
`- tcp_get_cookie_sock
|- tcp_v[46]_syn_recv_sock
| `- tcp_create_openreq_child
| `- newtp->tsoffset = treq->ts_off
`- tcp_sk(child)->tsoffset = tsoffLet's initialise tcp_rsk(req)->ts_off with the correct offset and remove the second initialisation of tcp_sk(sk)->tsoffset.
--- include/net/tcp.h | 2 +- net/ipv4/syncookies.c | 7 +++---- net/ipv6/syncookies.c | 4 ++-- 3 files changed, 6 insertions(+), 7 deletions(-)
include/net/tcp.h +1 -1
@@ -490,7 +490,7 @@ void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb);/* From syncookies.c */ struct sock *tcp_get_cookie_sock (struct sock *sk,struct sk_buff *skb,struct request_sock *req, -struct dst_entry *dst,u32 tsoff); +struct dst_entry *dst);int __cookie_v4_check (const struct iphdr *iph,const struct tcphdr *th);struct sock *cookie_v4_check (struct sock *sk,struct sk_buff *skb); struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops,
net/ipv4/syncookies.c +3 -4
@@ -204,7 +204,7 @@ EXPORT_SYMBOL_GPL(__cookie_v4_check);struct sock *tcp_get_cookie_sock (struct sock *sk,struct sk_buff *skb,struct request_sock *req, -struct dst_entry *dst,u32 tsoff) +struct dst_entry *dst) {struct inet_connection_sock *icsk =inet_csk (sk);struct sock *child;
@@ -214,7 +214,6 @@ struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb, NULL, &own_req);if (child) {refcount_set (&req->rsk_refcnt,1 ); -tcp_sk (child)->tsoffset = tsoff;sock_rps_save_rxhash (child, skb);if (rsk_drop_req (req)) {
@@ -386,7 +385,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) treq =tcp_rsk (req); treq->rcv_isn =ntohl (th->seq) -1 ; treq->snt_isn =ntohl (th->ack_seq) -1 ; - treq->ts_off =0 ; + treq->ts_off = tsoff; treq->txhash =net_tx_rndhash (); req->mss = mss; ireq->ir_num =ntohs (th->dest);
@@ -452,7 +451,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) ireq->rcv_wscale = rcv_wscale; ireq->ecn_ok =cookie_ecn_ok (&tcp_opt, net, &rt->dst); - ret =tcp_get_cookie_sock (sk, skb, req, &rt->dst, tsoff); + ret =tcp_get_cookie_sock (sk, skb, req, &rt->dst);/* ip_queue_xmit() depends on our flow being setup * Normal sockets get it right from inet_csk_route_child_sock() */
net/ipv6/syncookies.c +2 -2
@@ -215,7 +215,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) treq->snt_synack =0 ; treq->rcv_isn =ntohl (th->seq) -1 ; treq->snt_isn =ntohl (th->ack_seq) -1 ; - treq->ts_off =0 ; + treq->ts_off = tsoff; treq->txhash =net_tx_rndhash (); l3index =l3mdev_master_ifindex_by_index (net, ireq->ir_iif);
@@ -264,7 +264,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) ireq->rcv_wscale = rcv_wscale; ireq->ecn_ok =cookie_ecn_ok (&tcp_opt, net, dst); - ret =tcp_get_cookie_sock (sk, skb, req, dst, tsoff); + ret =tcp_get_cookie_sock (sk, skb, req, dst); out:return ret; out_free:
Signature · 1 line
2.30.2
PATCH v2 6/11 tcp: Move TCP-AO bits from cookie_v[46]_check() to tcp_ao_syncookie().
In reply to Kuniyuki IwashimaWe initialise treq->af_specific in cookie_tcp_reqsk_alloc() so that we can look up a key later in tcp_create_openreq_child().
Initially, that change was added for MD5 by commit ba5a4fdd63ae ("tcp:
make sure treq->af_specific is initialized"), but it has not been used since commit d0f2b7a9ca0a ("tcp: Disable header prediction for MD5 flow.").
Now, treq->af_specific is used only by TCP-AO, so, we can move that initialisation into tcp_ao_syncookie().
In addition to that, l3index in cookie_v[46]_check() is only used for tcp_ao_syncookie(), so let's move it as well.
While at it, we move down tcp_ao_syncookie() in cookie_v4_check() so that it will be called after security_inet_conn_request() to make functions order consistent with cookie_v6_check().
--- include/net/tcp.h | 1 - include/net/tcp_ao.h | 6 ++---- net/ipv4/syncookies.c | 16 ++++------------ net/ipv4/tcp_ao.c | 16 ++++++++++++++-- net/ipv6/syncookies.c | 7 ++----- 5 files changed, 22 insertions(+), 24 deletions(-)
include/net/tcp.h +0 -1
@@ -494,7 +494,6 @@ struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,int __cookie_v4_check (const struct iphdr *iph,const struct tcphdr *th);struct sock *cookie_v4_check (struct sock *sk,struct sk_buff *skb);struct request_sock *cookie_tcp_reqsk_alloc (const struct request_sock_ops *ops, -const struct tcp_request_sock_ops *af_ops,struct sock *sk,struct sk_buff *skb);#ifdef CONFIG_SYN_COOKIES
include/net/tcp_ao.h +2 -4
@@ -265,8 +265,7 @@ void tcp_ao_established(struct sock *sk);void tcp_ao_finish_connect (struct sock *sk,struct sk_buff *skb);void tcp_ao_connect_init (struct sock *sk);void tcp_ao_syncookie (struct sock *sk,const struct sk_buff *skb, -struct tcp_request_sock *treq, -unsigned short int family,int l3index); +struct request_sock *req,unsigned short int family);#else /* CONFIG_TCP_AO */ static inline int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb,
@@ -277,8 +276,7 @@ static inline int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb, }static inline void tcp_ao_syncookie (struct sock *sk,const struct sk_buff *skb, -struct tcp_request_sock *treq, -unsigned short int family,int l3index) +struct request_sock *req,unsigned short int family) { }
net/ipv4/syncookies.c +4 -12
@@ -286,9 +286,7 @@ bool cookie_ecn_ok(const struct tcp_options_received *tcp_opt,EXPORT_SYMBOL (cookie_ecn_ok);struct request_sock *cookie_tcp_reqsk_alloc (const struct request_sock_ops *ops, -const struct tcp_request_sock_ops *af_ops, -struct sock *sk, -struct sk_buff *skb) +struct sock *sk,struct sk_buff *skb) {struct tcp_request_sock *treq;struct request_sock *req;
@@ -303,9 +301,6 @@ struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops, treq =tcp_rsk (req); -/* treq->af_specific might be used to perform TCP_MD5 lookup */ - treq->af_specific = af_ops; - treq->syn_tos =TCP_SKB_CB (skb)->ip_dsfield; treq->req_usec_ts = false;
@@ -345,7 +340,6 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)struct rtable *rt;__u8 rcv_wscale;u32 tsoff =0 ; -int l3index;if (!READ_ONCE (net->ipv4.sysctl_tcp_syncookies) || !th->ack || th->rst)
@@ -376,8 +370,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)if (!cookie_timestamp_decode (net, &tcp_opt)) goto out; - req =cookie_tcp_reqsk_alloc (&tcp_request_sock_ops, - &tcp_request_sock_ipv4_ops, sk, skb); + req =cookie_tcp_reqsk_alloc (&tcp_request_sock_ops, sk, skb);if (!req) goto out_drop;
@@ -406,9 +399,6 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) ireq->ir_iif =inet_request_bound_dev_if (sk, skb); - l3index =l3mdev_master_ifindex_by_index (net, ireq->ir_iif); -tcp_ao_syncookie (sk, skb, treq,AF_INET , l3index); -/* We throwed the options of the initial SYN away, so we hope * the ACK carries the same options again (see RFC1122 4.2.3.8) */
@@ -417,6 +407,8 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)if (security_inet_conn_request (sk, skb, req)) goto out_free; +tcp_ao_syncookie (sk, skb, req,AF_INET ); + req->num_retrans =0 ;/*
net/ipv4/tcp_ao.c +14 -2
@@ -844,18 +844,30 @@ static struct tcp_ao_key *tcp_ao_inbound_lookup(unsigned short int family, }void tcp_ao_syncookie (struct sock *sk,const struct sk_buff *skb, -struct tcp_request_sock *treq, -unsigned short int family,int l3index) +struct request_sock *req,unsigned short int family) { +struct tcp_request_sock *treq =tcp_rsk (req);const struct tcphdr *th =tcp_hdr (skb);const struct tcp_ao_hdr *aoh;struct tcp_ao_key *key; +int l3index; + +/* treq->af_specific is used to perform TCP_AO lookup +* in tcp_create_openreq_child(). +*/ +#if IS_ENABLED (CONFIG_IPV6 ) +if (family ==AF_INET6 ) + treq->af_specific = &tcp_request_sock_ipv6_ops; + else +#endif + treq->af_specific = &tcp_request_sock_ipv4_ops; treq->maclen =0 ;if (tcp_parse_auth_options (th,NULL , &aoh) || !aoh)return ; + l3index =l3mdev_master_ifindex_by_index (sock_net (sk),inet_rsk (req)->ir_iif); key =tcp_ao_inbound_lookup (family, sk, skb,-1 , aoh->keyid, l3index);if (!key)/* Key not found, continue without TCP-AO */
net/ipv6/syncookies.c +2 -5
@@ -142,7 +142,6 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)int full_space, mss;__u8 rcv_wscale;u32 tsoff =0 ; -int l3index;if (!READ_ONCE (net->ipv4.sysctl_tcp_syncookies) || !th->ack || th->rst)
@@ -173,8 +172,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)if (!cookie_timestamp_decode (net, &tcp_opt)) goto out; - req =cookie_tcp_reqsk_alloc (&tcp6_request_sock_ops, - &tcp_request_sock_ipv6_ops, sk, skb); + req =cookie_tcp_reqsk_alloc (&tcp6_request_sock_ops, sk, skb);if (!req) goto out_drop;
@@ -218,8 +216,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) treq->ts_off = tsoff; treq->txhash =net_tx_rndhash (); - l3index =l3mdev_master_ifindex_by_index (net, ireq->ir_iif); -tcp_ao_syncookie (sk, skb, treq,AF_INET6 , l3index); +tcp_ao_syncookie (sk, skb, req,AF_INET6 );if (IS_ENABLED (CONFIG_SMC )) ireq->smc_ok =0 ;
Signature · 1 line
2.30.2
PATCH v2 7/11 tcp: Factorise cookie req initialisation.
In reply to Kuniyuki IwashimaWe will support arbitrary SYN Cookie with BPF, and then some reqsk fields are initialised in kfunc, and others are done in cookie_v[46]_check().
This patch factorises the common part as cookie_tcp_reqsk_init() and calls it in cookie_tcp_reqsk_alloc() to minimise the discrepancy between cookie_v[46]_check().
--- net/ipv4/syncookies.c | 69 ++++++++++++++++++++++++------------------- net/ipv6/syncookies.c | 14 --------- 2 files changed, 38 insertions(+), 45 deletions(-)
net/ipv4/syncookies.c +38 -31
@@ -285,10 +285,44 @@ bool cookie_ecn_ok(const struct tcp_options_received *tcp_opt, }EXPORT_SYMBOL (cookie_ecn_ok); +static int cookie_tcp_reqsk_init (struct sock *sk,struct sk_buff *skb, +struct request_sock *req) +{ +struct inet_request_sock *ireq =inet_rsk (req); +struct tcp_request_sock *treq =tcp_rsk (req); +const struct tcphdr *th =tcp_hdr (skb); + + req->num_retrans =0 ; + + ireq->ir_num =ntohs (th->dest); + ireq->ir_rmt_port = th->source; + ireq->ir_iif =inet_request_bound_dev_if (sk, skb); + ireq->ir_mark =inet_request_mark (sk, skb); + +if (IS_ENABLED (CONFIG_SMC )) + ireq->smc_ok =0 ; + + treq->snt_synack =0 ; + treq->tfo_listener = false; + treq->txhash =net_tx_rndhash (); + treq->rcv_isn =ntohl (th->seq) -1 ; + treq->snt_isn =ntohl (th->ack_seq) -1 ; + treq->syn_tos =TCP_SKB_CB (skb)->ip_dsfield; + treq->syn_tos =TCP_SKB_CB (skb)->ip_dsfield; + treq->req_usec_ts = false; + +#if IS_ENABLED (CONFIG_MPTCP ) + treq->is_mptcp =sk_is_mptcp (sk); +if (treq->is_mptcp) +return mptcp_subflow_init_cookie_req (req, sk, skb); +#endif + +return 0 ; +} +struct request_sock *cookie_tcp_reqsk_alloc (const struct request_sock_ops *ops,struct sock *sk,struct sk_buff *skb) { -struct tcp_request_sock *treq;struct request_sock *req;if (sk_is_mptcp (sk))
@@ -299,22 +333,10 @@ struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops,if (!req)return NULL ; - treq =tcp_rsk (req); - - treq->syn_tos =TCP_SKB_CB (skb)->ip_dsfield; - treq->req_usec_ts = false; - -#if IS_ENABLED (CONFIG_MPTCP ) - treq->is_mptcp =sk_is_mptcp (sk); -if (treq->is_mptcp) { -int err =mptcp_subflow_init_cookie_req (req, sk, skb); - -if (err) { -reqsk_free (req); -return NULL ; - } +if (cookie_tcp_reqsk_init (sk, skb, req)) { +reqsk_free (req); +return NULL ; } -#endif return req; }
@@ -376,28 +398,15 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) ireq =inet_rsk (req); treq =tcp_rsk (req); - treq->rcv_isn =ntohl (th->seq) -1 ; - treq->snt_isn =ntohl (th->ack_seq) -1 ; treq->ts_off = tsoff; - treq->txhash =net_tx_rndhash (); req->mss = mss; - ireq->ir_num =ntohs (th->dest); - ireq->ir_rmt_port = th->source;sk_rcv_saddr_set (req_to_sk (req),ip_hdr (skb)->daddr);sk_daddr_set (req_to_sk (req),ip_hdr (skb)->saddr); - ireq->ir_mark =inet_request_mark (sk, skb); ireq->snd_wscale = tcp_opt.snd_wscale; ireq->sack_ok = tcp_opt.sack_ok; ireq->wscale_ok = tcp_opt.wscale_ok; ireq->tstamp_ok = tcp_opt.saw_tstamp; req->ts_recent = tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval :0 ; - treq->snt_synack =0 ; - treq->tfo_listener = false; - -if (IS_ENABLED (CONFIG_SMC )) - ireq->smc_ok =0 ; - - ireq->ir_iif =inet_request_bound_dev_if (sk, skb);/* We throwed the options of the initial SYN away, so we hope * the ACK carries the same options again (see RFC1122 4.2.3.8)
@@ -409,8 +418,6 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)tcp_ao_syncookie (sk, skb, req,AF_INET ); - req->num_retrans =0 ; -/* * We need to lookup the route here to get at the correct * window size. We should better make sure that the window size
net/ipv6/syncookies.c +0 -14
@@ -178,11 +178,8 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) ireq =inet_rsk (req); treq =tcp_rsk (req); - treq->tfo_listener = false; req->mss = mss; - ireq->ir_rmt_port = th->source; - ireq->ir_num =ntohs (th->dest); ireq->ir_v6_rmt_addr =ipv6_hdr (skb)->saddr; ireq->ir_v6_loc_addr =ipv6_hdr (skb)->daddr;
@@ -196,31 +193,20 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) ireq->pktopts = skb; } - ireq->ir_iif =inet_request_bound_dev_if (sk, skb);/* So that link locals have meaning */ if (!sk->sk_bound_dev_if &&ipv6_addr_type (&ireq->ir_v6_rmt_addr) &IPV6_ADDR_LINKLOCAL ) ireq->ir_iif =tcp_v6_iif (skb); - ireq->ir_mark =inet_request_mark (sk, skb); - - req->num_retrans =0 ; ireq->snd_wscale = tcp_opt.snd_wscale; ireq->sack_ok = tcp_opt.sack_ok; ireq->wscale_ok = tcp_opt.wscale_ok; ireq->tstamp_ok = tcp_opt.saw_tstamp; req->ts_recent = tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval :0 ; - treq->snt_synack =0 ; - treq->rcv_isn =ntohl (th->seq) -1 ; - treq->snt_isn =ntohl (th->ack_seq) -1 ; treq->ts_off = tsoff; - treq->txhash =net_tx_rndhash ();tcp_ao_syncookie (sk, skb, req,AF_INET6 ); -if (IS_ENABLED (CONFIG_SMC )) - ireq->smc_ok =0 ; -/* * We need to lookup the dst_entry to get the correct window size. * This is taken from tcp_v6_syn_recv_sock. Somebody please enlighten
Signature · 1 line
2.30.2
PATCH v2 8/11 tcp: Factorise non-BPF SYN Cookie handling.
In reply to Kuniyuki IwashimaWe will support arbitrary SYN Cookie with BPF, and then kfunc at TC will preallocate reqsk and initialise some fields that should not be overwritten later by cookie_v[46]_check().
To simplify the flow in cookie_v[46]_check(), we move such fields' initialisation to cookie_tcp_reqsk_alloc() and factorise non-BPF SYN Cookie handling into cookie_tcp_check(), where we validate the cookie and allocate reqsk, as done by kfunc later.
Note that we set ireq->ecn_ok in two steps, the latter of which will be shared by the BPF case. As cookie_ecn_ok() is one-liner, now it's inlined.
--- include/net/tcp.h | 13 ++++-- net/ipv4/syncookies.c | 106 +++++++++++++++++++++++------------------- net/ipv6/syncookies.c | 61 ++++++++++++------------ 3 files changed, 99 insertions(+), 81 deletions(-)
include/net/tcp.h +10 -3
@@ -494,7 +494,10 @@ struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,int __cookie_v4_check (const struct iphdr *iph,const struct tcphdr *th);struct sock *cookie_v4_check (struct sock *sk,struct sk_buff *skb);struct request_sock *cookie_tcp_reqsk_alloc (const struct request_sock_ops *ops, -struct sock *sk,struct sk_buff *skb); +struct sock *sk,struct sk_buff *skb, +struct tcp_options_received *tcp_opt, +int mss,u32 tsoff); +#ifdef CONFIG_SYN_COOKIES /* Syncookies use a monotonic timer which increments every 60 seconds.
@@ -580,8 +583,12 @@ __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mss);u64 cookie_init_timestamp (struct request_sock *req,u64 now);bool cookie_timestamp_decode (const struct net *net,struct tcp_options_received *opt); -bool cookie_ecn_ok (const struct tcp_options_received *opt, -const struct net *net,const struct dst_entry *dst); + +static inline bool cookie_ecn_ok (const struct net *net,const struct dst_entry *dst) +{ +return READ_ONCE (net->ipv4.sysctl_tcp_ecn) || +dst_feature (dst,RTAX_FEATURE_ECN ); +}/* From net/ipv6/syncookies.c */ int __cookie_v6_check (const struct ipv6hdr *iph,const struct tcphdr *th);
net/ipv4/syncookies.c +57 -49
154 lines not on this page · raw message
net/ipv6/syncookies.c +32 -29
97 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v2 9/11 bpf: tcp: Handle BPF SYN Cookie in cookie_v[46]_check().
In reply to Kuniyuki IwashimaWe 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 | 59 ++++++++++++++++++++++++++++++++++++++++++- net/ipv6/syncookies.c | 6 ++++- 3 files changed, 84 insertions(+), 2 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 +58 -1
71 lines not on this page · raw message
net/ipv6/syncookies.c +5 -1
@@ -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)
Signature · 1 line
2.30.2
PATCH v2 10/11 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,
.snd_scale = send_scale, /* < 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 inet6?_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 extend struct tcp_cookie_attributes in the future when we add a new attribute that is determined in 3WHS.
--- include/net/inet6_hashtables.h | 14 ++++- include/net/inet_hashtables.h | 14 ++++- include/net/tcp.h | 6 +++ net/core/filter.c | 96 +++++++++++++++++++++++++++++++++- net/core/sock.c | 14 ++++- 5 files changed, 138 insertions(+), 6 deletions(-)
include/net/inet6_hashtables.h +13 -1
@@ -116,9 +116,21 @@ struct sock *inet6_steal_sock(struct net *net, struct sk_buff *skb, int doff,if (!sk)return NULL ; -if (!prefetched || !sk_fullsock (sk)) +if (!prefetched)return sk; +if (sk->sk_state ==TCP_NEW_SYN_RECV ) { +if (inet_reqsk (sk)->syncookie) { + *refcounted = false; + skb->sk = sk; + skb->destructor = sock_pfree; +return inet_reqsk (sk)->rsk_listener; + } +return sk; + }else if (sk->sk_state ==TCP_TIME_WAIT ) { +return sk; + } +if (sk->sk_protocol ==IPPROTO_TCP ) {if (sk->sk_state !=TCP_LISTEN )return sk;
include/net/inet_hashtables.h +13 -1
@@ -462,9 +462,21 @@ struct sock *inet_steal_sock(struct net *net, struct sk_buff *skb, int doff,if (!sk)return NULL ; -if (!prefetched || !sk_fullsock (sk)) +if (!prefetched)return sk; +if (sk->sk_state ==TCP_NEW_SYN_RECV ) { +if (inet_reqsk (sk)->syncookie) { + *refcounted = false; + skb->sk = sk; + skb->destructor = sock_pfree; +return inet_reqsk (sk)->rsk_listener; + } +return sk; + }else if (sk->sk_state ==TCP_TIME_WAIT ) { +return sk; + } +if (sk->sk_protocol ==IPPROTO_TCP ) {if (sk->sk_state !=TCP_LISTEN )return sk;
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 +94 -2
120 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
kernel test robot
build botRe: [PATCH v2 bpf-next 10/11] bpf: tcp: Support arbitrary SYN Cookie.
kernel test robot noticed the following build warnings:
Hi Kuniyuki,
kernel test robot noticed the following build warnings:
[auto build test WARNING on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Kuniyuki-Iwashima/tcp-Clean-up-reverse-xmas-tree-in-cookie_v-46-_check/20231121-063036 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20231120222341.54776-11-kuniyu%40amazon.com patch subject: [PATCH v2 bpf-next 10/11] bpf: tcp: Support arbitrary SYN Cookie. config: arm-randconfig-001-20231121 (https://download.01.org/0day-ci/archive/20231121/[email protected]/config) compiler: arm-linux-gnueabi-gcc (GCC) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231121/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> net/core/filter.c:11812:48: warning: 'struct tcp_cookie_attributes' declared inside parameter list will not be visible outside of this definition or declaration
11812 | struct tcp_cookie_attributes *attr,
| ^~~~~~~~~~~~~~~~~~~~~ net/core/filter.c: In function 'bpf_sk_assign_tcp_reqsk':
net/core/filter.c:11821:31: error: invalid application of 'sizeof' to incomplete type 'struct tcp_cookie_attributes'
11821 | if (attr__sz != sizeof(*attr))
| ^
net/core/filter.c:11851:17: error: invalid use of undefined type 'struct tcp_cookie_attributes'
11851 | if (attr->tcp_opt.mss_clamp < min_mss) {
| ^~
net/core/filter.c:11856:17: error: invalid use of undefined type 'struct tcp_cookie_attributes'
11856 | if (attr->tcp_opt.wscale_ok &&
| ^~
net/core/filter.c:11857:17: error: invalid use of undefined type 'struct tcp_cookie_attributes'
11857 | attr->tcp_opt.snd_wscale > TCP_MAX_WSCALE) {
| ^~
net/core/filter.c:11875:24: error: invalid use of undefined type 'struct tcp_cookie_attributes'
11875 | req->mss = attr->tcp_opt.mss_clamp;
| ^~
net/core/filter.c:11877:32: error: invalid use of undefined type 'struct tcp_cookie_attributes'
11877 | ireq->snd_wscale = attr->tcp_opt.snd_wscale;
| ^~
net/core/filter.c:11878:31: error: invalid use of undefined type 'struct tcp_cookie_attributes'
11878 | ireq->wscale_ok = attr->tcp_opt.wscale_ok;
| ^~
net/core/filter.c:11879:31: error: invalid use of undefined type 'struct tcp_cookie_attributes'
11879 | ireq->tstamp_ok = attr->tcp_opt.tstamp_ok;
| ^~
net/core/filter.c:11880:29: error: invalid use of undefined type 'struct tcp_cookie_attributes'
11880 | ireq->sack_ok = attr->tcp_opt.sack_ok;
| ^~
net/core/filter.c:11881:28: error: invalid use of undefined type 'struct tcp_cookie_attributes'
11881 | ireq->ecn_ok = attr->ecn_ok;
| ^~
net/core/filter.c:11883:33: error: invalid use of undefined type 'struct tcp_cookie_attributes'
11883 | treq->req_usec_ts = attr->usec_ts_ok;
| ^~
vim +11812 net/core/filter.c
11810
11811 __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct sk_buff *skb, struct sock *sk,
11812 struct tcp_cookie_attributes *attr,
11813 int attr__sz)
11814 {
11815 const struct request_sock_ops *ops;
11816 struct inet_request_sock *ireq;
11817 struct tcp_request_sock *treq;
11818 struct request_sock *req;
11819 __u16 min_mss;
11820
11821 if (attr__sz != sizeof(*attr))
11822 return -EINVAL;
11823
11824 if (!sk)
11825 return -EINVAL;
11826
11827 if (!skb_at_tc_ingress(skb))
11828 return -EINVAL;
11829
11830 if (dev_net(skb->dev) != sock_net(sk))
11831 return -ENETUNREACH;
11832
11833 switch (skb->protocol) {
11834 case htons(ETH_P_IP):
11835 ops = &tcp_request_sock_ops;
11836 min_mss = 536;
11837 break;
11838 #if IS_BUILTIN(CONFIG_IPV6)
11839 case htons(ETH_P_IPV6):
11840 ops = &tcp6_request_sock_ops;
11841 min_mss = IPV6_MIN_MTU - 60;
11842 break;
11843 #endif
11844 default:
11845 return -EINVAL;
11846 }
11847
11848 if (sk->sk_type != SOCK_STREAM || sk->sk_state != TCP_LISTEN)
11849 return -EINVAL;
11850
11851 if (attr->tcp_opt.mss_clamp < min_mss) {
11852 __NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESFAILED);
11853 return -EINVAL;
11854 }
11855
11856 if (attr->tcp_opt.wscale_ok &&
11857 attr->tcp_opt.snd_wscale > TCP_MAX_WSCALE) {
11858 __NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESFAILED);
11859 return -EINVAL;
11860 }
11861
11862 if (sk_is_mptcp(sk))
11863 req = mptcp_subflow_reqsk_alloc(ops, sk, false);
11864 else
11865 req = inet_reqsk_alloc(ops, sk, false);
11866
11867 if (!req)
11868 return -ENOMEM;
11869
11870 ireq = inet_rsk(req);
11871 treq = tcp_rsk(req);
11872
11873 req->syncookie = 1;
11874 req->rsk_listener = sk;
11875 req->mss = attr->tcp_opt.mss_clamp;
11876
11877 ireq->snd_wscale = attr->tcp_opt.snd_wscale;
11878 ireq->wscale_ok = attr->tcp_opt.wscale_ok;
11879 ireq->tstamp_ok = attr->tcp_opt.tstamp_ok;
11880 ireq->sack_ok = attr->tcp_opt.sack_ok;
11881 ireq->ecn_ok = attr->ecn_ok;
11882
11883 treq->req_usec_ts = attr->usec_ts_ok;
11884
11885 skb_orphan(skb);
11886 skb->sk = req_to_sk(req);
11887 skb->destructor = sock_pfree;
11888
11889 return 0;
11890 }
11891
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v2 bpf-next 10/11] bpf: tcp: Support arbitrary SYN Cookie.
From: kernel test robot <[email protected]>
Date: Tue, 21 Nov 2023 13:06:23 +0800
Quoted 60 lines
Hi Kuniyuki,
kernel test robot noticed the following build warnings:
[auto build test WARNING on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Kuniyuki-Iwashima/tcp-Clean-up-reverse-xmas-tree-in-cookie_v-46-_check/20231121-063036 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20231120222341.54776-11-kuniyu%40amazon.com patch subject: [PATCH v2 bpf-next 10/11] bpf: tcp: Support arbitrary SYN Cookie. config: arm-randconfig-001-20231121 (https://download.01.org/0day-ci/archive/20231121/[email protected]/config) compiler: arm-linux-gnueabi-gcc (GCC) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231121/[email protected]/reproduce)If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/All warnings (new ones prefixed by >>):
Quoted 1 line
Quoted 1 line
net/core/filter.c:11812:48: warning: 'structtcp_cookie_attributes' declared inside parameter list will not be visible outside of this definition or declaration11812 | struct
tcp_cookie_attributes*attr,| ^~~~~~~~~~~~~~~~~~~~~
net/core/filter.c: In function 'bpf_sk_assign_tcp_reqsk':
net/core/filter.c:11821:31: error: invalid application of 'sizeof' to incomplete type 'structtcp_cookie_attributes'
11821 | if (attr__sz!=sizeof(*attr))
| ^
net/core/filter.c:11851:17: error: invalid use of undefined type 'structtcp_cookie_attributes'
11851 | if (attr->tcp_opt.mss_clamp<min_mss) {
| ^~
net/core/filter.c:11856:17: error: invalid use of undefined type 'structtcp_cookie_attributes'
11856 | if (attr->tcp_opt.wscale_ok&&
| ^~
net/core/filter.c:11857:17: error: invalid use of undefined type 'structtcp_cookie_attributes'
11857 |attr->tcp_opt.snd_wscale>TCP_MAX_WSCALE) {
| ^~
net/core/filter.c:11875:24: error: invalid use of undefined type 'structtcp_cookie_attributes'
11875 |req->mss=attr->tcp_opt.mss_clamp;
| ^~
net/core/filter.c:11877:32: error: invalid use of undefined type 'structtcp_cookie_attributes'
11877 |ireq->snd_wscale=attr->tcp_opt.snd_wscale;
| ^~
net/core/filter.c:11878:31: error: invalid use of undefined type 'structtcp_cookie_attributes'
11878 |ireq->wscale_ok=attr->tcp_opt.wscale_ok;
| ^~
net/core/filter.c:11879:31: error: invalid use of undefined type 'structtcp_cookie_attributes'
11879 |ireq->tstamp_ok=attr->tcp_opt.tstamp_ok;
| ^~
net/core/filter.c:11880:29: error: invalid use of undefined type 'structtcp_cookie_attributes'
11880 |ireq->sack_ok=attr->tcp_opt.sack_ok;
| ^~
net/core/filter.c:11881:28: error: invalid use of undefined type 'structtcp_cookie_attributes'
11881 |ireq->ecn_ok=attr->ecn_ok;
| ^~
net/core/filter.c:11883:33: error: invalid use of undefined type 'structtcp_cookie_attributes'
11883 |treq->req_usec_ts=attr->usec_ts_ok;
| ^~
Quoted 7 lines
vim +11812
net/core/filter.c11810
11811__bpf_kfuncint bpf_sk_assign_tcp_reqsk(structsk_buff*skb, struct sock *sk,
11812 structtcp_cookie_attributes*attr,
11813 intattr__sz)
11814 {
bpf_sk_assign_tcp_reqsk() needs to be guarded by CONFIG_SYN_COOKIE.
Will fix in v3.
Thanks!
---8<---
net/core/filter.c +2 -0
@@ -11808,6 +11808,7 @@ __bpf_kfunc int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern,return 0 ; } +#if IS_ENABLED (CONFIG_SYN_COOKIE ) __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct sk_buff *skb, struct sock *sk,struct tcp_cookie_attributes *attr,int attr__sz)
@@ -11888,6 +11889,7 @@ __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct sk_buff *skb, struct sock *sk, return 0; } +#endif __bpf_kfunc_end_defs ();
---8<---
kernel test robot
build botRe: [PATCH v2 bpf-next 10/11] bpf: tcp: Support arbitrary SYN Cookie.
In reply to Kuniyuki Iwashimakernel test robot noticed the following build errors:
Hi Kuniyuki,
kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Kuniyuki-Iwashima/tcp-Clean-up-reverse-xmas-tree-in-cookie_v-46-_check/20231121-063036 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20231120222341.54776-11-kuniyu%40amazon.com patch subject: [PATCH v2 bpf-next 10/11] bpf: tcp: Support arbitrary SYN Cookie. config: arm-spear3xx_defconfig (https://download.01.org/0day-ci/archive/20231121/[email protected]/config) compiler: clang version 16.0.4 (https://github.com/llvm/llvm-project.git ae42196bc493ffe877a7e3dff8be32035dea4d07) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231121/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from net/core/sock.c:142:
In file included from include/net/tcp.h:32:
>> include/net/inet_hashtables.h:472:22: error: use of undeclared identifier 'sock_pfree'
skb->destructor = sock_pfree;
^ 1 error generated.
--
In file included from net/core/filter.c:39:
In file included from include/linux/skmsg.h:13:
In file included from include/net/tcp.h:32:
>> include/net/inet_hashtables.h:472:22: error: use of undeclared identifier 'sock_pfree'
skb->destructor = sock_pfree;
^
net/core/filter.c:11812:13: warning: declaration of 'struct tcp_cookie_attributes' will not be visible outside of this function [-Wvisibility]
struct tcp_cookie_attributes *attr,
^
net/core/filter.c:11821:24: error: invalid application of 'sizeof' to an incomplete type 'struct tcp_cookie_attributes'
if (attr__sz != sizeof(*attr))
^~~~~~~
net/core/filter.c:11812:13: note: forward declaration of 'struct tcp_cookie_attributes'
struct tcp_cookie_attributes *attr,
^
net/core/filter.c:11851:10: error: incomplete definition of type 'struct tcp_cookie_attributes'
if (attr->tcp_opt.mss_clamp < min_mss) {
~~~~^
net/core/filter.c:11812:13: note: forward declaration of 'struct tcp_cookie_attributes'
struct tcp_cookie_attributes *attr,
^
net/core/filter.c:11856:10: error: incomplete definition of type 'struct tcp_cookie_attributes'
if (attr->tcp_opt.wscale_ok &&
~~~~^
net/core/filter.c:11812:13: note: forward declaration of 'struct tcp_cookie_attributes'
struct tcp_cookie_attributes *attr,
^
net/core/filter.c:11857:10: error: incomplete definition of type 'struct tcp_cookie_attributes'
attr->tcp_opt.snd_wscale > TCP_MAX_WSCALE) {
~~~~^
net/core/filter.c:11812:13: note: forward declaration of 'struct tcp_cookie_attributes'
struct tcp_cookie_attributes *attr,
^
net/core/filter.c:11875:17: error: incomplete definition of type 'struct tcp_cookie_attributes'
req->mss = attr->tcp_opt.mss_clamp;
~~~~^
net/core/filter.c:11812:13: note: forward declaration of 'struct tcp_cookie_attributes'
struct tcp_cookie_attributes *attr,
^
net/core/filter.c:11877:25: error: incomplete definition of type 'struct tcp_cookie_attributes'
ireq->snd_wscale = attr->tcp_opt.snd_wscale;
~~~~^
net/core/filter.c:11812:13: note: forward declaration of 'struct tcp_cookie_attributes'
struct tcp_cookie_attributes *attr,
^
net/core/filter.c:11878:24: error: incomplete definition of type 'struct tcp_cookie_attributes'
ireq->wscale_ok = attr->tcp_opt.wscale_ok;
~~~~^
net/core/filter.c:11812:13: note: forward declaration of 'struct tcp_cookie_attributes'
struct tcp_cookie_attributes *attr,
^
net/core/filter.c:11879:24: error: incomplete definition of type 'struct tcp_cookie_attributes'
ireq->tstamp_ok = attr->tcp_opt.tstamp_ok;
~~~~^
net/core/filter.c:11812:13: note: forward declaration of 'struct tcp_cookie_attributes'
struct tcp_cookie_attributes *attr,
^
net/core/filter.c:11880:22: error: incomplete definition of type 'struct tcp_cookie_attributes'
ireq->sack_ok = attr->tcp_opt.sack_ok;
~~~~^
net/core/filter.c:11812:13: note: forward declaration of 'struct tcp_cookie_attributes'
struct tcp_cookie_attributes *attr,
^
net/core/filter.c:11881:21: error: incomplete definition of type 'struct tcp_cookie_attributes'
ireq->ecn_ok = attr->ecn_ok;
~~~~^
net/core/filter.c:11812:13: note: forward declaration of 'struct tcp_cookie_attributes'
struct tcp_cookie_attributes *attr,
^
net/core/filter.c:11883:26: error: incomplete definition of type 'struct tcp_cookie_attributes'
treq->req_usec_ts = attr->usec_ts_ok;
~~~~^
net/core/filter.c:11812:13: note: forward declaration of 'struct tcp_cookie_attributes'
struct tcp_cookie_attributes *attr,
^
>> net/core/filter.c:11887:20: error: use of undeclared identifier 'sock_pfree'
skb->destructor = sock_pfree;
^1 warning and 13 errors generated.
vim +/sock_pfree +472 include/net/inet_hashtables.h
451
452 static inline
453 struct sock *inet_steal_sock(struct net *net, struct sk_buff *skb, int doff,
454 const __be32 saddr, const __be16 sport,
455 const __be32 daddr, const __be16 dport,
456 bool *refcounted, inet_ehashfn_t *ehashfn)
457 {
458 struct sock *sk, *reuse_sk;
459 bool prefetched;
460
461 sk = skb_steal_sock(skb, refcounted, &prefetched);
462 if (!sk)
463 return NULL;
464
465 if (!prefetched)
466 return sk;
467
468 if (sk->sk_state == TCP_NEW_SYN_RECV) {
469 if (inet_reqsk(sk)->syncookie) {
470 *refcounted = false;
471 skb->sk = sk;
> 472 skb->destructor = sock_pfree; 473 return inet_reqsk(sk)->rsk_listener;
474 }
475 return sk;
476 } else if (sk->sk_state == TCP_TIME_WAIT) {
477 return sk;
478 }
479
480 if (sk->sk_protocol == IPPROTO_TCP) {
481 if (sk->sk_state != TCP_LISTEN)
482 return sk;
483 } else if (sk->sk_protocol == IPPROTO_UDP) {
484 if (sk->sk_state != TCP_CLOSE)
485 return sk;
486 } else {
487 return sk;
488 }
489
490 reuse_sk = inet_lookup_reuseport(net, sk, skb, doff,
491 saddr, sport, daddr, ntohs(dport),
492 ehashfn);
493 if (!reuse_sk)
494 return sk;
495
496 /* We've chosen a new reuseport sock which is never refcounted. This
497 * implies that sk also isn't refcounted.
498 */
499 WARN_ON_ONCE(*refcounted);
500
501 return reuse_sk;
502 }
503
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v2 bpf-next 10/11] bpf: tcp: Support arbitrary SYN Cookie.
From: kernel test robot <[email protected]>
Date: Tue, 21 Nov 2023 13:17:40 +0800
Quoted 19 lines
Hi Kuniyuki,
kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Kuniyuki-Iwashima/tcp-Clean-up-reverse-xmas-tree-in-cookie_v-46-_check/20231121-063036 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20231120222341.54776-11-kuniyu%40amazon.com patch subject: [PATCH v2 bpf-next 10/11] bpf: tcp: Support arbitrary SYN Cookie. config: arm-spear3xx_defconfig (https://download.01.org/0day-ci/archive/20231121/[email protected]/config) compiler: clang version 16.0.4 (https://github.com/llvm/llvm-project.git ae42196bc493ffe877a7e3dff8be32035dea4d07) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231121/[email protected]/reproduce)If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
Quoted 7 lines
All errors (new ones prefixed by >>):
In file included from
net/core/sock.c:142:
In file included frominclude/net/tcp.h:32:Quoted 1 line
Quoted 1 line
include/net/inet_hashtables.h:472:22: error: use of undeclared identifier 'sock_pfree'skb->destructor = sock_pfree; ^
Ok, sock_pfree is available with CONFIG_INET.
I'll guard the req->syncookie part in inet6?_steal_sock() by CONFIG_SYN_COOKIE too.
---8<---
include/net/inet6_hashtables.h +2 -0
@@ -120,12 +120,14 @@ struct sock *inet6_steal_sock(struct net *net, struct sk_buff *skb, int doff,return sk;if (sk->sk_state ==TCP_NEW_SYN_RECV ) { +#if IS_ENABLED (CONFIG_SYN_COOKIE )if (inet_reqsk (sk)->syncookie) { *refcounted = false; skb->sk = sk; skb->destructor = sock_pfree;return inet_reqsk (sk)->rsk_listener; } +#endif return sk; }else if (sk->sk_state ==TCP_TIME_WAIT ) {return sk;
include/net/inet_hashtables.h +2 -0
@@ -466,12 +466,14 @@ struct sock *inet_steal_sock(struct net *net, struct sk_buff *skb, int doff,return sk;if (sk->sk_state ==TCP_NEW_SYN_RECV ) { +#if IS_ENABLED (CONFIG_SYN_COOKIE )if (inet_reqsk (sk)->syncookie) { *refcounted = false; skb->sk = sk; skb->destructor = sock_pfree;return inet_reqsk (sk)->rsk_listener; } +#endif return sk; }else if (sk->sk_state ==TCP_TIME_WAIT ) {return sk;
---8<---
kernel test robot
build botRe: [PATCH v2 bpf-next 10/11] bpf: tcp: Support arbitrary SYN Cookie.
In reply to Kuniyuki Iwashimakernel test robot noticed the following build errors:
Hi Kuniyuki,
kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Kuniyuki-Iwashima/tcp-Clean-up-reverse-xmas-tree-in-cookie_v-46-_check/20231121-063036 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20231120222341.54776-11-kuniyu%40amazon.com patch subject: [PATCH v2 bpf-next 10/11] bpf: tcp: Support arbitrary SYN Cookie. config: um-i386_defconfig (https://download.01.org/0day-ci/archive/20231121/[email protected]/config) compiler: gcc-7 (Ubuntu 7.5.0-6ubuntu2) 7.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231121/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
net/core/filter.c:11812:13: warning: 'struct tcp_cookie_attributes' declared inside parameter list will not be visible outside of this definition or declaration
struct tcp_cookie_attributes *attr,
^~~~~~~~~~~~~~~~~~~~~
net/core/filter.c: In function 'bpf_sk_assign_tcp_reqsk':
>> net/core/filter.c:11821:25: error: dereferencing pointer to incomplete type 'struct tcp_cookie_attributes'
if (attr__sz != sizeof(*attr))
^~~~~vim +11821 net/core/filter.c
11810
11811 __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct sk_buff *skb, struct sock *sk,
11812 struct tcp_cookie_attributes *attr,
11813 int attr__sz)
11814 {
11815 const struct request_sock_ops *ops;
11816 struct inet_request_sock *ireq;
11817 struct tcp_request_sock *treq;
11818 struct request_sock *req;
11819 __u16 min_mss;
11820
11821 if (attr__sz != sizeof(*attr))
11822 return -EINVAL;
11823
11824 if (!sk)
11825 return -EINVAL;
11826
11827 if (!skb_at_tc_ingress(skb))
11828 return -EINVAL;
11829
11830 if (dev_net(skb->dev) != sock_net(sk))
11831 return -ENETUNREACH;
11832
11833 switch (skb->protocol) {
11834 case htons(ETH_P_IP):
11835 ops = &tcp_request_sock_ops;
11836 min_mss = 536;
11837 break;
11838 #if IS_BUILTIN(CONFIG_IPV6)
11839 case htons(ETH_P_IPV6):
11840 ops = &tcp6_request_sock_ops;
11841 min_mss = IPV6_MIN_MTU - 60;
11842 break;
11843 #endif
11844 default:
11845 return -EINVAL;
11846 }
11847
11848 if (sk->sk_type != SOCK_STREAM || sk->sk_state != TCP_LISTEN)
11849 return -EINVAL;
11850
11851 if (attr->tcp_opt.mss_clamp < min_mss) {
11852 __NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESFAILED);
11853 return -EINVAL;
11854 }
11855
11856 if (attr->tcp_opt.wscale_ok &&
11857 attr->tcp_opt.snd_wscale > TCP_MAX_WSCALE) {
11858 __NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESFAILED);
11859 return -EINVAL;
11860 }
11861
11862 if (sk_is_mptcp(sk))
11863 req = mptcp_subflow_reqsk_alloc(ops, sk, false);
11864 else
11865 req = inet_reqsk_alloc(ops, sk, false);
11866
11867 if (!req)
11868 return -ENOMEM;
11869
11870 ireq = inet_rsk(req);
11871 treq = tcp_rsk(req);
11872
11873 req->syncookie = 1;
11874 req->rsk_listener = sk;
11875 req->mss = attr->tcp_opt.mss_clamp;
11876
11877 ireq->snd_wscale = attr->tcp_opt.snd_wscale;
11878 ireq->wscale_ok = attr->tcp_opt.wscale_ok;
11879 ireq->tstamp_ok = attr->tcp_opt.tstamp_ok;
11880 ireq->sack_ok = attr->tcp_opt.sack_ok;
11881 ireq->ecn_ok = attr->ecn_ok;
11882
11883 treq->req_usec_ts = attr->usec_ts_ok;
11884
11885 skb_orphan(skb);
11886 skb->sk = req_to_sk(req);
11887 skb->destructor = sock_pfree;
11888
11889 return 0;
11890 }
11891
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v2 11/11 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 |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 to 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 + .../bpf/prog_tests/tcp_custom_syncookie.c | 163 +++++ .../selftests/bpf/progs/test_siphash.h | 64 ++ .../bpf/progs/test_tcp_custom_syncookie.c | 570 ++++++++++++++++++ .../bpf/progs/test_tcp_custom_syncookie.h | 161 +++++ 5 files changed, 968 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/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 +570 -0
570 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h new file +161 -0
161 lines not on this page · raw message
Signature · 1 line
2.30.2