From patchwork Sun Feb 4 10:45:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13544626 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1E8D52206D for ; Sun, 4 Feb 2024 10:46:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707043586; cv=none; b=mHCFeZ7GJh/AZxb9eNYYYBe0NcUpbAHQ1Ohg0MYeXJDCHr9YYlIW+1ol0EUgIHk3YpwETjTuFZLEp60NIsMNxK/7DMNSXKt4dsO8kFjD3qRQYqvPt5EzjGnBqTjk+1JvYYWjDuls+/O9GhuVfHvo+F1QQ50I09yDtcILefqM7wk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707043586; c=relaxed/simple; bh=wC2c1rER5sPvbea8MZA733oMmJucQ4DxsCXxKt8skYw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dqlL72KRXg86TzFhKaSUQVVTTyI3Wz4FaTnpcr8gICX3l9XYXH44TbCNp2Rmk1mbx0krF7QVLb/7naA+JcyJImpcbgEWu4WuoAMUv2hlzB39sv3MlztN5mgFr+MzvEXjKKutaPRN0CSAiQuH9uPsHpzOVUUkQ87uxrt3Jd/XktY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=IIbGL5n7; arc=none smtp.client-ip=209.85.215.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IIbGL5n7" Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-5ce2aada130so3266445a12.1 for ; Sun, 04 Feb 2024 02:46:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707043584; x=1707648384; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=f5CtxJG7CfG2KPK2uOaftVBppm910I8+3fU0rCDi4aU=; b=IIbGL5n7J3+eh+qkBl2Ib+ZSJZSCfFrnX0Kzww+BqiZDIX+3CJbvyq6i9B/CIlgRH9 UdfnxndJWJWAiocEsq75Rwkb8jOKzdGJeok50bmGSb2poJBClfI30c3Y67n+5/9gsSo0 KFM0yzgCoCcs1gBrvxxcoxuxVmsQP2YSSaF6KrSPvbfGjbL0QH5e928J8hoF2kvJwiuX fA+aCpLX8CRCTCiN0S04TEMN5fRqG4IPAJo0ZtrERKbnPdI4pQJ9XiFQwZ4x9ihIA1a7 a9PHjG1nfkFAJHZS+WGlkCz6EImXk7qc7ugUo5Kg8tooSQR0qripMlZ2TR0zrFVZcXxN q0Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707043584; x=1707648384; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=f5CtxJG7CfG2KPK2uOaftVBppm910I8+3fU0rCDi4aU=; b=dCTKYHaDmh6ypVnt8peRRUeGkkCQ21UAzczKTb/vvqLW3SIgctNuvW2X9FcUPLtjjU 2LvytY6Psfa8f5lOvdy4nHDP4LhvMKnEeoWcvweS42qZ867ILr3MS/OC6k0s2S45zkk+ tdIHyQWAeiu/8i8ld6MFYJL/1r1ZmKPQ3J/oTJmdJ1dJBu+x7+5vVkbVUsClm/ohOx9x m6ITrsDljwZDHrNYZ7+9/0CkNdFAwdmwAQfqJzvbroGULAS4va8uuf2E3UBvbYVn2bTJ noMl7KiOBVza851iWKfpc86jFRop+Eu6SBGU6Ag8xMa2GkaSGqlU2kCAUv53LoUklINa mqMA== X-Gm-Message-State: AOJu0Yyae78ihOdoybeB6NO8Ekn67YecSXAO7SU2CiLz5Nu43YoNpK0i yOldPHEj1dKeZ2N4TYdm/OYXrHnBzs5D8fhxstlIFIiNmcixV2cB X-Google-Smtp-Source: AGHT+IFGU1HB840FmlQs6peTmSn+tBMBEcEViWj4Agtv31ottkRx6Kk+CJG7QY3ov2uR0TvH8vr3nQ== X-Received: by 2002:a05:6a00:1805:b0:6d9:bc67:8c6 with SMTP id y5-20020a056a00180500b006d9bc6708c6mr14076536pfa.8.1707043584231; Sun, 04 Feb 2024 02:46:24 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCX15DUQzSN2ldPe/fXg0MS/2rF54zciAjHtSE4RT13j8qeDhPDpVSsd73oBC/MkW0Gbl1s5SXV2NWD2HeN6pmPvUKl61rqasRPOrC+m/IMV8sRfgJrrKd368XUxi3TVIzIbsp/h1fnQJIsF6irfjJweA4u0dqrrjtfbQHqi4flCPp7iKSPOuaIgl5KXAdkZUsVektLwkfL86g/Wgyj0ZLuXpQk54HwoR7DItyWtGbQ= Received: from KERNELXING-MB0.tencent.com ([43.132.141.20]) by smtp.gmail.com with ESMTPSA id c14-20020aa7880e000000b006dd79bbcd11sm327099pfo.205.2024.02.04.02.46.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Feb 2024 02:46:23 -0800 (PST) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org Cc: netdev@vger.kernel.org, kerneljasonxing@gmail.com, Jason Xing Subject: [PATCH net-next 1/2] tcp: add more DROP REASONs in cookie check Date: Sun, 4 Feb 2024 18:45:59 +0800 Message-Id: <20240204104601.55760-2-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240204104601.55760-1-kerneljasonxing@gmail.com> References: <20240204104601.55760-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing Since the commit 8eba65fa5f06 ("net: tcp: use kfree_skb_reason() for tcp_v{4,6}_do_rcv()") introduced the drop reason mechanism, this function is always using NOT_SPECIFIED which is too general and unhelpful to us if we want to track this part. Signed-off-by: Jason Xing --- include/net/dropreason-core.h | 12 ++++++++++++ include/net/tcp.h | 3 ++- net/ipv4/syncookies.c | 18 ++++++++++++++---- net/ipv4/tcp_ipv4.c | 7 ++++--- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/include/net/dropreason-core.h b/include/net/dropreason-core.h index 6d3a20163260..85a19b883dee 100644 --- a/include/net/dropreason-core.h +++ b/include/net/dropreason-core.h @@ -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) \ @@ -107,6 +111,8 @@ 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 */ + 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 +249,8 @@ 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 */ + SKB_DROP_REASON_COOKIE_NOCHILD, /** * @SKB_DROP_REASON_TCP_ACK_UNSENT_DATA: TCP ACK for data we haven't * sent yet @@ -254,6 +262,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 +281,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) diff --git a/include/net/tcp.h b/include/net/tcp.h index 58e65af74ad1..e3b07d2790c4 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -492,7 +492,8 @@ struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb, struct request_sock *req, 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 sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb, + enum skb_drop_reason *reason); struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops, struct sock *sk, struct sk_buff *skb, struct tcp_options_received *tcp_opt, diff --git a/net/ipv4/syncookies.c b/net/ipv4/syncookies.c index be88bf586ff9..9febad3a3150 100644 --- a/net/ipv4/syncookies.c +++ b/net/ipv4/syncookies.c @@ -395,7 +395,8 @@ static struct request_sock *cookie_tcp_check(struct net *net, struct sock *sk, * Output is listener if incoming packet would not create a child * NULL if memory could not be allocated. */ -struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) +struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb, + enum skb_drop_reason *reason) { struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt; const struct tcphdr *th = tcp_hdr(skb); @@ -420,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) { + *reason = SKB_DROP_REASON_NO_REQSK_ALLOC; goto out_drop; + } ireq = inet_rsk(req); @@ -433,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)) { + *reason = SKB_DROP_REASON_SECURITY_HOOK; goto out_free; + } tcp_ao_syncookie(sk, skb, req, AF_INET); @@ -451,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)) { + *reason = SKB_DROP_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); @@ -477,6 +484,9 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) */ if (ret) inet_sk(ret)->cork.fl.u.ip4 = fl4; + else + *reason = SKB_DROP_REASON_COOKIE_NOCHILD; + out: return ret; out_free: diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 0c50c5a32b84..b63b0efa111d 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -1846,13 +1846,14 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, } EXPORT_SYMBOL(tcp_v4_syn_recv_sock); -static struct sock *tcp_v4_cookie_check(struct sock *sk, struct sk_buff *skb) +static struct sock *tcp_v4_cookie_check(struct sock *sk, struct sk_buff *skb, + enum skb_drop_reason *reason) { #ifdef CONFIG_SYN_COOKIES const struct tcphdr *th = tcp_hdr(skb); if (!th->syn) - sk = cookie_v4_check(sk, skb); + sk = cookie_v4_check(sk, skb, reason); #endif return sk; } @@ -1912,7 +1913,7 @@ int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb) goto csum_err; if (sk->sk_state == TCP_LISTEN) { - struct sock *nsk = tcp_v4_cookie_check(sk, skb); + struct sock *nsk = tcp_v4_cookie_check(sk, skb, &reason); if (!nsk) goto discard; From patchwork Sun Feb 4 10:46:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Xing X-Patchwork-Id: 13544628 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-oa1-f46.google.com (mail-oa1-f46.google.com [209.85.160.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF56C1B808 for ; Sun, 4 Feb 2024 10:46:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707043599; cv=none; b=KbfEintHeGOFHVu22Xg3nEmcSVVzoqJ15s/nNSYBWUq5//tk1AXLftl2XaA3jJpkn6RGuyjsRtQFY4d9Rrf6xaUCj7CC8oU5Gg7JpfIgCZH8rAUqZlySPdJstycVQUScoulAdXRKBsPmH7Xn2QEBgxKdQDZpXyOcebLfp6GqxC8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707043599; c=relaxed/simple; bh=21DKPToyP8vfOxzJeXudCfCjb+ST9k7OQoMZV7Zxu8A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=S4x/ss4PDoWt3MKNeNHxUdBt7bOAm9VZJ4aepn2+qkxi4iMcjRf8honp6BeasmDEP8nTl15WsqUZE1K1CYhBQNVN13VksRVbCVO9hVGliYDRT9LP1rlTVBdC+yHW7EdiMvOfynArGBxPfFmmLMCW5lO5VK2Y37oTqB6BWzXrcMQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=B9MI5cfN; arc=none smtp.client-ip=209.85.160.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="B9MI5cfN" Received: by mail-oa1-f46.google.com with SMTP id 586e51a60fabf-21433afcc53so2218090fac.3 for ; Sun, 04 Feb 2024 02:46:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707043597; x=1707648397; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+pjijQ99Q872W8sS4ZrH9wkRzDg9r3R+YgWYikG40R8=; b=B9MI5cfNW0w9LT59MQYK6ZVNJ9i3Wor+PnFYNrBkarL1UiiWfC9XpEXisgIOLDCoI3 Fr0zE3XK8xiw/dUiA5o5dtgde4Ewk5HQ316TjLWyevnhn5u3/rEGYvWAKhjMqVaoT8B8 lS1oOxJVDMg/GlQCSS036y7//JqBQ0tyhh7kyILLGrzFYjRCQFBFWzFCj0R9ojppqkvo cor0g29CkLltts1FVF718BAjaeUWgt/jbAPoYN9/flXn5wg+h1MylIvkPdNesGMmhG3/ tOF0F0wTNsYci9IYUwriJRWujx90ZGlyePLgSbOx5/7X9+7lkTg8N5j96sQAIUysZ3C5 TTBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707043597; x=1707648397; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+pjijQ99Q872W8sS4ZrH9wkRzDg9r3R+YgWYikG40R8=; b=r7Xm/YdTTvDnfq/IIYo0L2fYhs17Ib84s8lxJWTK/pkC2LRDTuqYaUs17tmM+pc2+l spmlX/QNHb33n4cDkRRGSDyV3plVi5N1wySxToDBd1fqfA7m3qCtrC0UsPsEcYeyGIsq yZU6kdqBDsRLamiEkcrrXLKBWUeDE1bcZ1MRw148ppP3qq0u75uuoqCgeIohlNBhOhfw Y5dx3X8uooR5rXj5fs2abeDpDY+coqDHVyDRasEsa+7Fvr88qwZ9d4yGtoqyhl/SwkzN 2R8udGOgwqDWNUnaAdY6Ma05j0afTsx+RJrUl0kOZiGYGvJ33BaQQcSEBnd6cBlEALAU oi1w== X-Gm-Message-State: AOJu0YxHZ/ObdaEac/R0XE14gDUKncrtVAUQuGjdx5+6gY07C1XJyeuM Os2Yy7JZwUxTxv/1QP+BdWHYmx1KdVB+ErYoaFpYXYVTGZRSI+i9x/7G1aDuYx47AA== X-Google-Smtp-Source: AGHT+IHtZsu0D1qdkhzlbbPCYqz/c51wNkF8Zv22O+sVBsaT7r2CfM95nQ9OgtHRI05RlfciODR6Yg== X-Received: by 2002:a05:6358:441e:b0:176:7d13:d70e with SMTP id z30-20020a056358441e00b001767d13d70emr7336596rwc.16.1707043596767; Sun, 04 Feb 2024 02:46:36 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCW457LEuL7PzXbp7Tl+eoMTNsrhK9kuMOc710LoQazd6wkuaeh8sE91k1S5+B0pAd6PN2V4jED4f5+yyHclacfQtT/AH9IItlTdsx203KiZc0jm2+YAl06ZZ45XN5EIZLfjzbeius+njZEtkv2UOlLaH6ZAcc24Lhnv1/nu1nB84unDMUMMSUNSgOQ2jT7hhVrLTqG44gRJQmS99kDwNsQOQ8g9bcG6SOYAwlViBr8= Received: from KERNELXING-MB0.tencent.com ([43.132.141.20]) by smtp.gmail.com with ESMTPSA id c14-20020aa7880e000000b006dd79bbcd11sm327099pfo.205.2024.02.04.02.46.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Feb 2024 02:46:36 -0800 (PST) From: Jason Xing To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, dsahern@kernel.org Cc: netdev@vger.kernel.org, kerneljasonxing@gmail.com, Jason Xing Subject: [PATCH net-next 2/2] tcp: add more DROP REASONS in child process Date: Sun, 4 Feb 2024 18:46:01 +0800 Message-Id: <20240204104601.55760-4-kerneljasonxing@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240204104601.55760-1-kerneljasonxing@gmail.com> References: <20240204104601.55760-1-kerneljasonxing@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jason Xing As the title said, add more reasons to narrow down the range about why the skb should be dropped. Signed-off-by: Jason Xing --- include/net/dropreason-core.h | 6 ++++++ include/net/tcp.h | 5 +++-- net/ipv4/tcp_input.c | 19 +++++++++++++++---- net/ipv4/tcp_ipv4.c | 6 +++--- net/ipv4/tcp_minisocks.c | 4 ++-- net/ipv6/tcp_ipv6.c | 6 +++--- 6 files changed, 32 insertions(+), 14 deletions(-) diff --git a/include/net/dropreason-core.h b/include/net/dropreason-core.h index 85a19b883dee..980fd4442b7c 100644 --- a/include/net/dropreason-core.h +++ b/include/net/dropreason-core.h @@ -31,6 +31,8 @@ FN(TCP_AOFAILURE) \ FN(SOCKET_BACKLOG) \ FN(TCP_FLAGS) \ + FN(TCP_CONNREQNOTACCEPTABLE) \ + FN(TCP_ABORTONDATA) \ FN(TCP_ZEROWINDOW) \ FN(TCP_OLD_DATA) \ FN(TCP_OVERWINDOW) \ @@ -203,6 +205,10 @@ enum skb_drop_reason { SKB_DROP_REASON_SOCKET_BACKLOG, /** @SKB_DROP_REASON_TCP_FLAGS: TCP flags invalid */ SKB_DROP_REASON_TCP_FLAGS, + /** @SKB_DROP_REASON_TCP_CONNREQNOTACCEPTABLE: con req not acceptable */ + SKB_DROP_REASON_TCP_CONNREQNOTACCEPTABLE, + /** @SKB_DROP_REASON_TCP_ABORTONDATA: abort on data */ + SKB_DROP_REASON_TCP_ABORTONDATA, /** * @SKB_DROP_REASON_TCP_ZEROWINDOW: TCP receive window size is zero, * see LINUX_MIB_TCPZEROWINDOWDROP diff --git a/include/net/tcp.h b/include/net/tcp.h index e3b07d2790c4..8c87170cb0b5 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -348,7 +348,8 @@ void tcp_wfree(struct sk_buff *skb); void tcp_write_timer_handler(struct sock *sk); void tcp_delack_timer_handler(struct sock *sk); int tcp_ioctl(struct sock *sk, int cmd, int *karg); -int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb); +int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb, + enum skb_drop_reason *drop_reason); void tcp_rcv_established(struct sock *sk, struct sk_buff *skb); void tcp_rcv_space_adjust(struct sock *sk); int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp); @@ -397,7 +398,7 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb, struct request_sock *req, bool fastopen, bool *lost_race); int tcp_child_process(struct sock *parent, struct sock *child, - struct sk_buff *skb); + struct sk_buff *skb, enum skb_drop_reason *reason); void tcp_enter_loss(struct sock *sk); void tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked, int newly_lost, int flag); void tcp_clear_retrans(struct tcp_sock *tp); diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 2d20edf652e6..bacb1140dab3 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -6616,7 +6616,8 @@ static void tcp_rcv_synrecv_state_fastopen(struct sock *sk) * address independent. */ -int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb) +int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb, + enum skb_drop_reason *drop_reason) { struct tcp_sock *tp = tcp_sk(sk); struct inet_connection_sock *icsk = inet_csk(sk); @@ -6632,8 +6633,10 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb) goto discard; case TCP_LISTEN: - if (th->ack) + if (th->ack) { + SKB_DR_SET(*drop_reason, TCP_FLAGS); return 1; + } if (th->rst) { SKB_DR_SET(reason, TCP_RESET); @@ -6653,8 +6656,10 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb) local_bh_enable(); rcu_read_unlock(); - if (!acceptable) + if (!acceptable) { + SKB_DR_SET(*drop_reason, TCP_CONNREQNOTACCEPTABLE); return 1; + } consume_skb(skb); return 0; } @@ -6704,8 +6709,11 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb) FLAG_NO_CHALLENGE_ACK); if ((int)reason <= 0) { - if (sk->sk_state == TCP_SYN_RECV) + if (sk->sk_state == TCP_SYN_RECV) { + if ((int)reason < 0) + *drop_reason = -reason; return 1; /* send one RST */ + } /* accept old ack during closing */ if ((int)reason < 0) { tcp_send_challenge_ack(sk); @@ -6781,6 +6789,7 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb) if (READ_ONCE(tp->linger2) < 0) { tcp_done(sk); NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA); + SKB_DR_SET(*drop_reason, TCP_ABORTONDATA); return 1; } if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq && @@ -6790,6 +6799,7 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb) tcp_fastopen_active_disable(sk); tcp_done(sk); NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA); + SKB_DR_SET(*drop_reason, TCP_ABORTONDATA); return 1; } @@ -6855,6 +6865,7 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb) after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) { NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA); tcp_reset(sk, skb); + SKB_DR_SET(*drop_reason, TCP_ABORTONDATA); return 1; } } diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index b63b0efa111d..7da62af0d890 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -1918,7 +1918,7 @@ int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb) if (!nsk) goto discard; if (nsk != sk) { - if (tcp_child_process(sk, nsk, skb)) { + if (tcp_child_process(sk, nsk, skb, &reason)) { rsk = nsk; goto reset; } @@ -1927,7 +1927,7 @@ int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb) } else sock_rps_save_rxhash(sk, skb); - if (tcp_rcv_state_process(sk, skb)) { + if (tcp_rcv_state_process(sk, skb, &reason)) { rsk = sk; goto reset; } @@ -2276,7 +2276,7 @@ int tcp_v4_rcv(struct sk_buff *skb) if (nsk == sk) { reqsk_put(req); tcp_v4_restore_cb(skb); - } else if (tcp_child_process(sk, nsk, skb)) { + } else if (tcp_child_process(sk, nsk, skb, &drop_reason)) { tcp_v4_send_reset(nsk, skb); goto discard_and_relse; } else { diff --git a/net/ipv4/tcp_minisocks.c b/net/ipv4/tcp_minisocks.c index 9e85f2a0bddd..49a88bf47b79 100644 --- a/net/ipv4/tcp_minisocks.c +++ b/net/ipv4/tcp_minisocks.c @@ -912,7 +912,7 @@ EXPORT_SYMBOL(tcp_check_req); */ int tcp_child_process(struct sock *parent, struct sock *child, - struct sk_buff *skb) + struct sk_buff *skb, enum skb_drop_reason *reason) __releases(&((child)->sk_lock.slock)) { int ret = 0; @@ -923,7 +923,7 @@ int tcp_child_process(struct sock *parent, struct sock *child, tcp_segs_in(tcp_sk(child), skb); if (!sock_owned_by_user(child)) { - ret = tcp_rcv_state_process(child, skb); + ret = tcp_rcv_state_process(child, skb, reason); /* Wakeup parent, send SIGIO */ if (state == TCP_SYN_RECV && child->sk_state != state) parent->sk_data_ready(parent); diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 57b25b1fc9d9..ced52f7aa5bb 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -1657,7 +1657,7 @@ int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb) goto discard; if (nsk != sk) { - if (tcp_child_process(sk, nsk, skb)) + if (tcp_child_process(sk, nsk, skb, &reason)) goto reset; if (opt_skb) __kfree_skb(opt_skb); @@ -1666,7 +1666,7 @@ int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb) } else sock_rps_save_rxhash(sk, skb); - if (tcp_rcv_state_process(sk, skb)) + if (tcp_rcv_state_process(sk, skb, &reason)) goto reset; if (opt_skb) goto ipv6_pktoptions; @@ -1856,7 +1856,7 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb) if (nsk == sk) { reqsk_put(req); tcp_v6_restore_cb(skb); - } else if (tcp_child_process(sk, nsk, skb)) { + } else if (tcp_child_process(sk, nsk, skb, &drop_reason)) { tcp_v6_send_reset(nsk, skb); goto discard_and_relse; } else {