introduce drop reasons for cookie check
Subsystems (3)
v3 cover introduce drop reasons for cookie check
From: Jason Xing <[email protected]>
When I was debugging the reason about why the skb should be dropped in syn cookie mode, I found out that this NOT_SPECIFIED reason is too general. Thus I decided to refine it.
Previously I submitted one patch serie which is too large/risky, so I split that one serie into two seires. For another one, I will submit later.
Summary
1. introduce all the dropreasons we need, [1/5] patch.
2. use new dropreasons in ipv4 cookie check, [2/5],[3/5] patch.
3. use new dropreasons ipv6 cookie check, [4/5],[5/5] patch.
v3:
1. Split that patch into some smaller ones as suggested by Eric.
v2:
1. change the title of 2/2 patch.
2. fix some warnings checkpatch tool showed before.
3. use return value instead of adding more parameters suggested by Eric.
Jason Xing (5):
tcp: add dropreasons definitions and prepare for cookie check
tcp: directly drop skb in cookie check for ipv4
tcp: use drop reasons in cookie check for ipv4
tcp: directly drop skb in cookie check for ipv6
tcp: use drop reasons in cookie check for ipv6
include/net/dropreason-core.h | 21 +++++++++++++++++++++ net/ipv4/syncookies.c | 20 ++++++++++++++++---- net/ipv4/tcp_ipv4.c | 2 +- net/ipv6/syncookies.c | 18 +++++++++++++++--- net/ipv6/tcp_ipv6.c | 7 +++++-- 5 files changed, 58 insertions(+), 10 deletions(-)
Signature · 1 line
2.37.3
PATCH v3 1/5 tcp: add dropreasons definitions and prepare for cookie check
From: Jason Xing <[email protected]>
Only add five drop reasons to detect the condition of skb dropped in cookie check for later use.
--- include/net/dropreason-core.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
include/net/dropreason-core.h +21 -0
@@ -6,6 +6,7 @@#define DEFINE_DROP_REASON (FN , FNe) \ FN(NOT_SPECIFIED) \ FN(NO_SOCKET) \ + FN(NO_REQSK_ALLOC) \ FN(PKT_TOO_SMALL) \ FN(TCP_CSUM) \ FN(SOCKET_FILTER) \
@@ -43,10 +44,12 @@ FN(TCP_FASTOPEN) \ FN(TCP_OLD_ACK) \ FN(TCP_TOO_OLD_ACK) \ + FN(COOKIE_NOCHILD) \ FN(TCP_ACK_UNSENT_DATA) \ FN(TCP_OFO_QUEUE_PRUNE) \ FN(TCP_OFO_DROP) \ FN(IP_OUTNOROUTES) \ + FN(IP_ROUTEOUTPUTKEY) \ FN(BPF_CGROUP_EGRESS) \ FN(IPV6DISABLED) \ FN(NEIGH_CREATEFAIL) \
@@ -54,6 +57,7 @@ FN(NEIGH_QUEUEFULL) \ FN(NEIGH_DEAD) \ FN(TC_EGRESS) \ + FN(SECURITY_HOOK) \ FN(QDISC_DROP) \ FN(CPU_BACKLOG) \ FN(XDP) \
@@ -71,6 +75,7 @@ FN(TAP_TXFILTER) \ FN(ICMP_CSUM) \ FN(INVALID_PROTO) \ + FN(INVALID_DST) \ FN(IP_INADDRERRORS) \ FN(IP_INNOROUTES) \ FN(PKT_TOO_BIG) \
@@ -107,6 +112,11 @@ enum skb_drop_reason {SKB_DROP_REASON_NOT_SPECIFIED ,/** @SKB_DROP_REASON_NO_SOCKET: socket not found */ SKB_DROP_REASON_NO_SOCKET , +/** +* @SKB_DROP_REASON_NO_REQSK_ALLOC: request socket allocation failed +* probably because of no available memory for now +*/ +SKB_DROP_REASON_NO_REQSK_ALLOC ,/** @SKB_DROP_REASON_PKT_TOO_SMALL: packet size is too small */ SKB_DROP_REASON_PKT_TOO_SMALL,/** @SKB_DROP_REASON_TCP_CSUM: TCP checksum error */
@@ -243,6 +253,11 @@ enum skb_drop_reason {SKB_DROP_REASON_TCP_OLD_ACK ,/** @SKB_DROP_REASON_TCP_TOO_OLD_ACK: TCP ACK is too old */ SKB_DROP_REASON_TCP_TOO_OLD_ACK , +/** +* @SKB_DROP_REASON_COOKIE_NOCHILD: no child socket in cookie mode +* reason could be the failure of child socket allocation +*/ + SKB_DROP_REASON_COOKIE_NOCHILD,/** * @SKB_DROP_REASON_TCP_ACK_UNSENT_DATA: TCP ACK for data we haven't * sent yet
@@ -254,6 +269,8 @@ enum skb_drop_reason {SKB_DROP_REASON_TCP_OFO_DROP ,/** @SKB_DROP_REASON_IP_OUTNOROUTES: route lookup failed */ SKB_DROP_REASON_IP_OUTNOROUTES , +/** @SKB_DROP_REASON_IP_ROUTEOUTPUTKEY: route output key failed */ + SKB_DROP_REASON_IP_ROUTEOUTPUTKEY,/** * @SKB_DROP_REASON_BPF_CGROUP_EGRESS: dropped by BPF_PROG_TYPE_CGROUP_SKB * eBPF program
@@ -271,6 +288,8 @@ enum skb_drop_reason {SKB_DROP_REASON_NEIGH_DEAD ,/** @SKB_DROP_REASON_TC_EGRESS: dropped in TC egress HOOK */ SKB_DROP_REASON_TC_EGRESS , +/** @SKB_DROP_REASON_SECURITY_HOOK: dropped due to security HOOK */ + SKB_DROP_REASON_SECURITY_HOOK,/** * @SKB_DROP_REASON_QDISC_DROP: dropped by qdisc when packet outputting ( * failed to enqueue to current qdisc)
@@ -333,6 +352,8 @@ enum skb_drop_reason {* such as a broadcasts ICMP_TIMESTAMP */ SKB_DROP_REASON_INVALID_PROTO , +/** @SKB_DROP_REASON_INVALIDDST: look-up dst entry error */ + SKB_DROP_REASON_INVALID_DST,/** * @SKB_DROP_REASON_IP_INADDRERRORS: host unreachable, corresponding to * IPSTATS_MIB_INADDRERRORS
Signature · 1 line
2.37.3
Jakub Kicinski
maintainerRe: [PATCH net-next v3 1/5] tcp: add dropreasons definitions and prepare for cookie check
include/net/dropreason-core.h enum skb_drop_reason { · line 356
Quoted 2 lines · On Mon, 12 Feb 2024 13:25:09 +0800 Jason Xing wrote:
include/net/dropreason-core.h +2
-0+/** @SKB_DROP_REASON_INVALIDDST: look-up dst entry error */ + SKB_DROP_REASON_INVALID_DST,
The name is misspelled in kdoc.
Signature · 1 line
pw-bot: cr
Re: [PATCH net-next v3 1/5] tcp: add dropreasons definitions and prepare for cookie check
Quoted 6 lines · Jakub Kicinski
include/net/dropreason-core.h enum skb_drop_reason { · line 356
Quoted 2 lines · On Mon, 12 Feb 2024 13:25:09 +0800 Jason Xing wrote:
include/net/dropreason-core.h +2
-0+/** @SKB_DROP_REASON_INVALIDDST: look-up dst entry error */ + SKB_DROP_REASON_INVALID_DST,The name is misspelled in kdoc.
Thanks for correcting the error. I'll update it soon :)
Thanks,
Jason
Quoted 2 lines
--
pw-bot: cr
PATCH v3 2/5 tcp: directly drop skb in cookie check for ipv4
In reply to Jason XingFrom: Jason Xing <[email protected]>
Only move the skb drop from tcp_v4_do_rcv() to cookie_v4_check() itself, no other changes made. It can help us refine the specific drop reasons later.
--- net/ipv4/syncookies.c | 4 ++++ net/ipv4/tcp_ipv4.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-)
net/ipv4/syncookies.c +4 -0
@@ -408,6 +408,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)struct rtable *rt;__u8 rcv_wscale;int full_space; +SKB_DR (reason);if (!READ_ONCE (net->ipv4.sysctl_tcp_syncookies) || !th->ack || th->rst)
@@ -477,10 +478,13 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)*/ if (ret)inet_sk (ret)->cork.fl.u.ip4 = fl4; +else + goto out_drop; out:return ret; out_free:reqsk_free (req); out_drop: +kfree_skb_reason (skb, reason);return NULL ; }
net/ipv4/tcp_ipv4.c +1 -1
@@ -1915,7 +1915,7 @@ int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)struct sock *nsk =tcp_v4_cookie_check (sk, skb);if (!nsk) - goto discard; +return 0 ;if (nsk != sk) {if (tcp_child_process (sk, nsk, skb)) { rsk = nsk;
Signature · 1 line
2.37.3
PATCH v3 3/5 tcp: use drop reasons in cookie check for ipv4
In reply to Jason XingFrom: Jason Xing <[email protected]>
Now it's time to use the prepared definitions to refine this part. Four reasons used might enough for now, I think.
--- net/ipv4/syncookies.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
net/ipv4/syncookies.c +13 -5
@@ -421,8 +421,10 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)if (IS_ERR (req)) goto out; } -if (!req) +if (!req) { +SKB_DR_SET (reason,NO_REQSK_ALLOC ); goto out_drop; + } ireq =inet_rsk (req);
@@ -434,8 +436,10 @@ 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)) +if (security_inet_conn_request (sk, skb, req)) { +SKB_DR_SET (reason,SECURITY_HOOK ); goto out_free; + }tcp_ao_syncookie (sk, skb, req,AF_INET );
@@ -452,8 +456,10 @@ 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)) +if (IS_ERR (rt)) { +SKB_DR_SET (reason,IP_ROUTEOUTPUTKEY ); 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 );
@@ -476,10 +482,12 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)/* ip_queue_xmit() depends on our flow being setup * Normal sockets get it right from inet_csk_route_child_sock() */ -if (ret) +if (ret) {inet_sk (ret)->cork.fl.u.ip4 = fl4; -else + }else { +SKB_DR_SET (reason,COOKIE_NOCHILD ); goto out_drop; + } out:return ret; out_free:
Signature · 1 line
2.37.3
PATCH v3 4/5 tcp: directly drop skb in cookie check for ipv6
In reply to Jason XingFrom: Jason Xing <[email protected]>
Like previous patch does, only moving skb drop logical code to cookie_v6_check() for later refinement.
--- net/ipv6/syncookies.c | 4 ++++ net/ipv6/tcp_ipv6.c | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-)
net/ipv6/syncookies.c +4 -0
@@ -177,6 +177,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)struct sock *ret = sk;__u8 rcv_wscale;int full_space; +SKB_DR (reason);if (!READ_ONCE (net->ipv4.sysctl_tcp_syncookies) || !th->ack || th->rst)
@@ -256,10 +257,13 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) ireq->ecn_ok &=cookie_ecn_ok (net, dst); ret =tcp_get_cookie_sock (sk, skb, req, dst); +if (!ret) + goto out_drop; out:return ret; out_free:reqsk_free (req); out_drop: +kfree_skb_reason (skb, reason);return NULL ; }
net/ipv6/tcp_ipv6.c +5 -2
@@ -1653,8 +1653,11 @@ int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)if (sk->sk_state ==TCP_LISTEN ) {struct sock *nsk =tcp_v6_cookie_check (sk, skb); -if (!nsk) - goto discard; +if (!nsk) { +if (opt_skb) +__kfree_skb (opt_skb); +return 0 ; + }if (nsk != sk) {if (tcp_child_process (sk, nsk, skb))
Signature · 1 line
2.37.3
PATCH v3 5/5 tcp: use drop reasons in cookie check for ipv6
In reply to Jason XingFrom: Jason Xing <[email protected]>
Like what I did to ipv4 mode, refine this part: adding more drop reasons for better tracing.
--- net/ipv6/syncookies.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
net/ipv6/syncookies.c +12 -4
@@ -190,16 +190,20 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)if (IS_ERR (req)) goto out; } -if (!req) +if (!req) { +SKB_DR_SET (reason,NO_REQSK_ALLOC ); goto out_drop; + } ireq =inet_rsk (req); ireq->ir_v6_rmt_addr =ipv6_hdr (skb)->saddr; ireq->ir_v6_loc_addr =ipv6_hdr (skb)->daddr; -if (security_inet_conn_request (sk, skb, req)) +if (security_inet_conn_request (sk, skb, req)) { +SKB_DR_SET (reason,SECURITY_HOOK ); goto out_free; + } if (ipv6_opt_accepted(sk, skb, &TCP_SKB_CB(skb)->header.h6) || np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
@@ -236,8 +240,10 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)security_req_classify_flow (req,flowi6_to_flowi_common (&fl6)); dst =ip6_dst_lookup_flow (net, sk, &fl6, final_p); -if (IS_ERR (dst)) +if (IS_ERR (dst)) { +SKB_DR_SET (reason,INVALID_DST ); goto out_free; + } } req->rsk_window_clamp = tp->window_clamp ? :dst_metric (dst,RTAX_WINDOW );
@@ -257,8 +263,10 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) ireq->ecn_ok &=cookie_ecn_ok (net, dst); ret =tcp_get_cookie_sock (sk, skb, req, dst); -if (!ret) +if (!ret) { +SKB_DR_SET (reason,COOKIE_NOCHILD ); goto out_drop; + } out:return ret; out_free:
Signature · 1 line
2.37.3