From patchwork Wed Jun 14 23:09:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Safonov X-Patchwork-Id: 108187 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp284116vqr; Wed, 14 Jun 2023 16:21:07 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5NL9Mv0w6wAq/NsTZ0CLWxcxP4PjWaaNjNnIXW1MNeojRLAAGP0ewL1B/5z8GNaDAmpyC4 X-Received: by 2002:a05:6808:152:b0:398:2d08:1143 with SMTP id h18-20020a056808015200b003982d081143mr11814690oie.35.1686784867319; Wed, 14 Jun 2023 16:21:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686784867; cv=none; d=google.com; s=arc-20160816; b=hC8hAhVSNXhnY8Ir7Dz3w6YwsleEAI9Nlk9yF2SuZJotpzlBMgFxx6MgxOgNKmJQNB 9VYQPYqOWF1aZf4Jb6RpjKWmHwMMt2g/WIkLPeZIbWoCqAo02GD/JBGCj+kVDXRIEIwM 7iz69YG4UxhDh2JOR5gf7ERtETrEM3iiYlFQrFALCx7iuGs/wfpixapJhDqjcZnpsT8g FEtiPwf9Xlpykc3A2cnf3FJwtu/0vC4AgsTD9jZmqrAma3DhBf0fvfB/NaEdksU7VCTj DDcwXRCK1E32Vjy+pP6wlJe1uZRFZ41k01dnDfT/EaGZy0uaHTFC1SzjaVaoraeCJk2P TjoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PDr+dBAvKpABVNSxI7WZDNnHeX6B4tB4KcN1ztOpo3o=; b=rIpFKRQFyXO9VJGOVoh2Kxi6wYcPSkEy1u92SV7fYjO+pvIYDznEjy/9PXyEbrwR7f H8EOEHTeyr76AGUA9M3uP3K1MX+Xcxn55YcMovoBroueqqjPLBq+O0TqcPOAK/xdd2LT biL2I5ujuy9tWc9qKvQpQECnmCR26NJ/gaOuMUuym9TH94mdAzkgnmODP/9uvEasavWN R0sN9jgJ1GnbOXt5rAjRS9e6vEAAwuL35cOWhlvs/DwQJtFbZLM1Bw9qzpkcud2a3VZ0 LNGRS6pY7j2aEMPIVGHRTiu6GPROoalrGTJ1JG1W21dIBNI3AxhKxOdgsytu5NCkao+p Of5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=google header.b=M0i+zI8j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=arista.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j7-20020a17090a734700b0025337db9e19si11670167pjs.156.2023.06.14.16.20.55; Wed, 14 Jun 2023 16:21:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@arista.com header.s=google header.b=M0i+zI8j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=arista.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233762AbjFNXLo (ORCPT + 99 others); Wed, 14 Jun 2023 19:11:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238478AbjFNXLA (ORCPT ); Wed, 14 Jun 2023 19:11:00 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97F55296E for ; Wed, 14 Jun 2023 16:10:21 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-3f8cc04c2adso12893675e9.3 for ; Wed, 14 Jun 2023 16:10:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1686784219; x=1689376219; 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=PDr+dBAvKpABVNSxI7WZDNnHeX6B4tB4KcN1ztOpo3o=; b=M0i+zI8jfsY76QgPkxl5rpL1he+vLiXtd9MMOtncdQXDW8a/ivk0uZbHWD+yct+Q/t 2ra3+Pmuti4Ivodf6bowKHWi4svbWiB5CowngUZd30Ab81clprrQA/DUy04acjg9iTdd 0KxzOtm6vKu1sQndR3l9KhZbGhdLLCGhOIiOKLECAp7SMW4b62sxPkgaFL8LVTmLbU4+ VUIU2suV569Jr0OWiYR73AX2yq2YlXRUj0Su0rlZx+azrKcu2gR3+venrtRw3pg9SDYn c38quC0Ks7y2zJVqNheMdq1iT2jD20piF8opJL17x2+UCkEEkRL62OBYvfnLxNXqQ8Sx AfMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686784219; x=1689376219; 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=PDr+dBAvKpABVNSxI7WZDNnHeX6B4tB4KcN1ztOpo3o=; b=KZSOz8uJelz9v0Gh0lfR59ll4Ionto9smI+7ifn2sWZTcuIeIw6GTTroU59k1JzajY SkJEac7JjjSVO9AJPmXJ8u02qgi9N8UlkEFcO9g1vn8XJ5yh/DSkL19+AnOvDevEhpCk f1JdIirBWUZbeg/G9fMsGvjSwRGSVQ5IS49SxTHkSK4/NShefoVFfYpw5M+B1TKEJKft 065UvS2jiEVgCvE6f7Oi7xojJU/nrz0Vh5sCsJIoGCMCYlhpa7DYDyfK5qjSRYV6SBtY uuceyu5F8VYkKU9cedWXACSmH1QSUGK3fElUMz0HVACRPYURteSTQXMACHa+vm/xakoc N84g== X-Gm-Message-State: AC+VfDyA1MgUWFyg6fgvtqtpHsoHAiktO8h+BoLtC4chT5s+CPThPZrr 9kOe/owlH9zeETyHWinywRcvHQ== X-Received: by 2002:a05:600c:2204:b0:3f6:692:5607 with SMTP id z4-20020a05600c220400b003f606925607mr13840835wml.40.1686784218990; Wed, 14 Jun 2023 16:10:18 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id s12-20020a7bc38c000000b003f7ba52eeccsm18725261wmj.7.2023.06.14.16.10.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jun 2023 16:10:18 -0700 (PDT) From: Dmitry Safonov To: David Ahern , Eric Dumazet , Paolo Abeni , Jakub Kicinski , "David S. Miller" Cc: linux-kernel@vger.kernel.org, Dmitry Safonov , Andy Lutomirski , Ard Biesheuvel , Bob Gilligan , Dan Carpenter , David Laight , Dmitry Safonov <0x7f454c46@gmail.com>, Donald Cassidy , Eric Biggers , "Eric W. Biederman" , Francesco Ruggeri , Herbert Xu , Hideaki YOSHIFUJI , Ivan Delalande , Leonard Crestez , Salam Noureddine , netdev@vger.kernel.org Subject: [PATCH v7 15/22] net/tcp: Add tcp_hash_fail() ratelimited logs Date: Thu, 15 Jun 2023 00:09:40 +0100 Message-Id: <20230614230947.3954084-16-dima@arista.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230614230947.3954084-1-dima@arista.com> References: <20230614230947.3954084-1-dima@arista.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768722128608102816?= X-GMAIL-MSGID: =?utf-8?q?1768722128608102816?= Add a helper for logging connection-detailed messages for failed TCP hash verification (both MD5 and AO). Co-developed-by: Francesco Ruggeri Signed-off-by: Francesco Ruggeri Co-developed-by: Salam Noureddine Signed-off-by: Salam Noureddine Signed-off-by: Dmitry Safonov --- include/net/tcp.h | 14 ++++++++++++-- include/net/tcp_ao.h | 29 +++++++++++++++++++++++++++++ net/ipv4/tcp.c | 23 +++++++++++++---------- net/ipv4/tcp_ao.c | 7 +++++++ 4 files changed, 61 insertions(+), 12 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index d09f994f7562..1d3cf13ae66b 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -2615,12 +2615,18 @@ tcp_inbound_hash(struct sock *sk, const struct request_sock *req, int l3index; /* Invalid option or two times meet any of auth options */ - if (tcp_parse_auth_options(th, &md5_location, &aoh)) + if (tcp_parse_auth_options(th, &md5_location, &aoh)) { + tcp_hash_fail("TCP segment has incorrect auth options set", + family, skb, ""); return SKB_DROP_REASON_TCP_AUTH_HDR; + } if (req) { if (tcp_rsk_used_ao(req) != !!aoh) { NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOBAD); + tcp_hash_fail("TCP connection can't start/end using TCP-AO", + family, skb, "%s", + !aoh ? "missing AO" : "AO signed"); return SKB_DROP_REASON_TCP_AOFAILURE; } } @@ -2637,10 +2643,14 @@ tcp_inbound_hash(struct sock *sk, const struct request_sock *req, * the last key is impossible to remove, so there's * always at least one current_key. */ - if (tcp_ao_required(sk, saddr, family, true)) + if (tcp_ao_required(sk, saddr, family, true)) { + tcp_hash_fail("AO hash is required, but not found", + family, skb, "L3 index %d", l3index); return SKB_DROP_REASON_TCP_AONOTFOUND; + } if (unlikely(tcp_md5_do_lookup(sk, l3index, saddr, family))) { NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND); + tcp_hash_fail("MD5 Hash not found", family, skb, ""); return SKB_DROP_REASON_TCP_MD5NOTFOUND; } return SKB_NOT_DROPPED_YET; diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index 9930e56e35be..5397ba6f84b6 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -105,6 +105,35 @@ struct tcp_ao_info { struct rcu_head rcu; }; +#define tcp_hash_fail(msg, family, skb, fmt, ...) \ +do { \ + const struct tcphdr *th = tcp_hdr(skb); \ + char hdr_flags[5] = {}; \ + char *f = hdr_flags; \ + \ + if (th->fin) \ + *f++ = 'F'; \ + if (th->syn) \ + *f++ = 'S'; \ + if (th->rst) \ + *f++ = 'R'; \ + if (th->ack) \ + *f++ = 'A'; \ + if (f != hdr_flags) \ + *f = ' '; \ + if (family == AF_INET) { \ + net_info_ratelimited("%s for (%pI4, %d)->(%pI4, %d) %s" fmt "\n", \ + msg, &ip_hdr(skb)->saddr, ntohs(th->source), \ + &ip_hdr(skb)->daddr, ntohs(th->dest), \ + hdr_flags, ##__VA_ARGS__); \ + } else { \ + net_info_ratelimited("%s for [%pI6c]:%u->[%pI6c]:%u %s" fmt "\n", \ + msg, &ipv6_hdr(skb)->saddr, ntohs(th->source), \ + &ipv6_hdr(skb)->daddr, ntohs(th->dest), \ + hdr_flags, ##__VA_ARGS__); \ + } \ +} while (0) + #ifdef CONFIG_TCP_AO /* TCP-AO structures and functions */ diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 20e16ada8d5d..dd1885d7a81f 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -4485,7 +4485,6 @@ tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb, * o MD5 hash and we're not expecting one. * o MD5 hash and its wrong. */ - const struct tcphdr *th = tcp_hdr(skb); const struct tcp_sock *tp = tcp_sk(sk); struct tcp_md5sig_key *key; int genhash; @@ -4495,6 +4494,7 @@ tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb, if (!key && hash_location) { NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED); + tcp_hash_fail("Unexpected MD5 Hash found", family, skb, ""); return SKB_DROP_REASON_TCP_MD5UNEXPECTED; } @@ -4510,16 +4510,19 @@ tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb, if (genhash || memcmp(hash_location, newhash, 16) != 0) { NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5FAILURE); if (family == AF_INET) { - net_info_ratelimited("MD5 Hash failed for (%pI4, %d)->(%pI4, %d)%s L3 index %d\n", - saddr, ntohs(th->source), - daddr, ntohs(th->dest), - genhash ? " tcp_v4_calc_md5_hash failed" - : "", l3index); + tcp_hash_fail("MD5 Hash failed", AF_INET, skb, "%s L3 index %d", + genhash ? "tcp_v4_calc_md5_hash failed" + : "", l3index); } else { - net_info_ratelimited("MD5 Hash %s for [%pI6c]:%u->[%pI6c]:%u L3 index %d\n", - genhash ? "failed" : "mismatch", - saddr, ntohs(th->source), - daddr, ntohs(th->dest), l3index); + if (genhash) { + tcp_hash_fail("MD5 Hash failed", + AF_INET6, skb, "L3 index %d", + l3index); + } else { + tcp_hash_fail("MD5 Hash mismatch", + AF_INET6, skb, "L3 index %d", + l3index); + } } return SKB_DROP_REASON_TCP_MD5FAILURE; } diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index 4c618d339009..1c358889ac10 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -741,6 +741,8 @@ tcp_ao_verify_hash(const struct sock *sk, const struct sk_buff *skb, NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOBAD); atomic64_inc(&info->counters.pkt_bad); atomic64_inc(&key->pkt_bad); + tcp_hash_fail("AO hash wrong length", family, skb, + "%u != %d", maclen, tcp_ao_maclen(key)); return SKB_DROP_REASON_TCP_AOFAILURE; } @@ -751,6 +753,7 @@ tcp_ao_verify_hash(const struct sock *sk, const struct sk_buff *skb, NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOBAD); atomic64_inc(&info->counters.pkt_bad); atomic64_inc(&key->pkt_bad); + tcp_hash_fail("AO hash mismatch", family, skb, ""); return SKB_DROP_REASON_TCP_AOFAILURE; } NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOGOOD); @@ -776,6 +779,8 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_buff *skb, info = rcu_dereference(tcp_sk(sk)->ao_info); if (!info) { NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOKEYNOTFOUND); + tcp_hash_fail("AO key not found", family, skb, + "keyid: %u", aoh->keyid); return SKB_DROP_REASON_TCP_AOUNEXPECTED; } @@ -874,6 +879,8 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_buff *skb, key_not_found: NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOKEYNOTFOUND); atomic64_inc(&info->counters.key_not_found); + tcp_hash_fail("Requested by the peer AO key id not found", + family, skb, ""); return SKB_DROP_REASON_TCP_AOKEYNOTFOUND; } EXPORT_SYMBOL_GPL(tcp_inbound_ao_hash);