From patchwork Fri Jul 21 16:19:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Safonov X-Patchwork-Id: 123958 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp320550vqg; Fri, 21 Jul 2023 09:24:45 -0700 (PDT) X-Google-Smtp-Source: APBJJlEE16488RWvr4T5mwdqUzbMueo1fC5DoKwi3aA4wXFwzHW1BsRm4JbZz+/QeAJMysRoBLDS X-Received: by 2002:a05:6a20:4d8:b0:137:23a2:2b3c with SMTP id 24-20020a056a2004d800b0013723a22b3cmr2604014pzd.49.1689956685506; Fri, 21 Jul 2023 09:24:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689956685; cv=none; d=google.com; s=arc-20160816; b=VYtGCpYvI4h5GKSeDdnKFJ3R7ASeNfNqs6TlqaC4TRjjtdNGos/wXm18aMeyZ3fDWi Qja6t1jItqa4OeiMJDdRfW2JEH+WgEszCLRHb+3DETpjcjRf0brFQjmDwpucnI5j2Ruy n4WZ7XJ3qhrTopxV1RUT/U921aEPs9NOnthKScD+HdhxekaWdcixwwT7nT54nsrXRlQG ZDwZcSIHE7wvW9NwsbgKTNW0ALs0klsPTJTVHANYRkpOemD2q0UPgwotxT7/LMzI+3La WNfpT9ItBELws6bNhV/FAmE4BtsijulE637Xlfqn5xJCJ+wIN0GwKVN6upTA2i8s/NwE ItkQ== 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=YBrGKffyjTkycCwd+lM5fHU406znDgFk9HFzWebHaZ0=; fh=+1v2wpWL3SwC67MevmNFbsOkJ0jlpe0KMhkxFTuop+s=; b=XEeIZk4v/V0l8VgukhlIOMo3VqDUSBcpxt8gONrRP5iCfFIH0+f/wR2HIQkdLyKohj Z1QknF2OanRntJ5+z7Uqhxmi7hAYgEKNd1l9dbVxYYOWanuFM/yGz7aJVxpueznofBxj TgEmTXoXF4ysF8u92UpoKni1C6oS3rrxQq96Hrai9YN05gSVtM3I4d9eB9IMZLnjxhZW uPikFUnXDhzNEJH63l99lrJpvx3J/AX4LbwFlFTr/KuqpkzOOzN6BMAuyMUOLcewxicq 7hqI3K2ziTNiRBAmsoBzYWthbqqJwpL6jkCmLxB/Vw+bgBeDYhH2s/H2ClncwkRhWUG+ ekfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=google header.b=Lx137Wrv; 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 k11-20020a634b4b000000b0055ba8a7dea6si3201308pgl.519.2023.07.21.09.24.30; Fri, 21 Jul 2023 09:24:45 -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=Lx137Wrv; 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 S232798AbjGUQXh (ORCPT + 99 others); Fri, 21 Jul 2023 12:23:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229736AbjGUQWv (ORCPT ); Fri, 21 Jul 2023 12:22:51 -0400 Received: from mail-lj1-x232.google.com (mail-lj1-x232.google.com [IPv6:2a00:1450:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3F1A4C09 for ; Fri, 21 Jul 2023 09:20:04 -0700 (PDT) Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2b6f97c7115so31215781fa.2 for ; Fri, 21 Jul 2023 09:20:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1689956399; x=1690561199; 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=YBrGKffyjTkycCwd+lM5fHU406znDgFk9HFzWebHaZ0=; b=Lx137Wrvlvz2vXyuCyanT1JclkSyIH6Ua7N8+WJIzWgBG9j4nuMTEiW1uYbGuvuKxr LRhH7jK0RS6Sab/xbwQaIRZmtOzHrBSU5vooO1LeA1b/b2TJUdogJUTEtAz1IJmo+Bo9 K0OFNxifUjinMWcnRhWhwfxmtMBegJWkGZn+uQFTgjahdr8oWebzSxj+F1xyOF5jjc2K Xijla5UyNRfH5vSTY3Izyi6UAL9oenXPLOSIyM0HLIzGQO8LSY/K9ConuNcBi/eW5veU cBTuEGFaKkDh8bhxjxZPMp2lavVk0UinVy0G/kn3Teo7VJ8ntndZCzc+4k7385/N901F oXFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689956399; x=1690561199; 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=YBrGKffyjTkycCwd+lM5fHU406znDgFk9HFzWebHaZ0=; b=clcQBNzG7DqdAtaPaNA+vi4XJWIZA00uwl0zA5GncI8gVAnFYXGoifUDhDzrvqJHQH xOU8TOz03Nty1zZkToe17KaVB+ZXWX0plPUQCSytELZgcfeo44RAAl+0DfrZNwKgi6H2 91jhx1cvQNHqBxYprNDAkJVy52Z3vLxsZ71Plomx/T9KHDRrr4oXnYK2aTEjMC8067W+ O9rZE65jd1XsX0SkQ8tptlaqqxRScO3jHG4bSz9egydVKl2/72HY0vvBX7WkDyV43m/z qfaFWiweGzOlh6/Yzkl6jWhKQM9dx1PkSAKlg3XbQg2bNWvx1P/ChjPCM1/SdgrhJ6W9 +gvg== X-Gm-Message-State: ABy/qLYVoQpJ6/J7D3jEei8aIvJvEYrNH12w9clhLSRAZwaOfOX89JJ6 o+UZsi/8fzUfnQiNEq/UFLyklQ== X-Received: by 2002:a2e:9810:0:b0:2b7:31a:9d7c with SMTP id a16-20020a2e9810000000b002b7031a9d7cmr2070328ljj.33.1689956399324; Fri, 21 Jul 2023 09:19:59 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id l19-20020a7bc453000000b003fbc681c8d1sm6390210wmi.36.2023.07.21.09.19.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Jul 2023 09:19:58 -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 , "Gaillardetz, Dominik" , Herbert Xu , Hideaki YOSHIFUJI , Ivan Delalande , Leonard Crestez , Salam Noureddine , "Tetreault, Francois" , netdev@vger.kernel.org Subject: [PATCH v8.1 net-next 20/23] net/tcp: Add static_key for TCP-AO Date: Fri, 21 Jul 2023 17:19:11 +0100 Message-ID: <20230721161916.542667-21-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230721161916.542667-1-dima@arista.com> References: <20230721161916.542667-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_BLOCKED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=ham 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: INBOX X-GMAIL-THRID: 1772048021345221288 X-GMAIL-MSGID: 1772048021345221288 Similarly to TCP-MD5, add a static key to TCP-AO that is patched out when there are no keys on a machine and dynamically enabled with the first setsockopt(TCP_AO) adds a key on any socket. The static key is as well dynamically disabled later when the socket is destructed. The lifetime of enabled static key here is the same as ao_info: it is enabled on allocation, passed over from full socket to twsk and destructed when ao_info is scheduled for destruction. Signed-off-by: Dmitry Safonov --- include/net/tcp.h | 3 +++ include/net/tcp_ao.h | 2 ++ net/ipv4/tcp_ao.c | 22 +++++++++++++++++++++ net/ipv4/tcp_input.c | 46 +++++++++++++++++++++++++++++--------------- 4 files changed, 57 insertions(+), 16 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index 50636ee7ee2f..4df3ee6e2722 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -2611,6 +2611,9 @@ static inline bool tcp_ao_required(struct sock *sk, const void *saddr, struct tcp_ao_info *ao_info; struct tcp_ao_key *ao_key; + if (!static_branch_unlikely(&tcp_ao_needed.key)) + return false; + ao_info = rcu_dereference_check(tcp_sk(sk)->ao_info, lockdep_sock_is_held(sk)); if (!ao_info) diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index 9209343878e1..e6d849d7179e 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -152,6 +152,8 @@ do { \ #ifdef CONFIG_TCP_AO /* TCP-AO structures and functions */ +#include +extern struct static_key_false_deferred tcp_ao_needed; struct tcp4_ao_context { __be32 saddr; diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index 567aa9400f7d..54b35ee1edf1 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -17,6 +17,8 @@ #include #include +DEFINE_STATIC_KEY_DEFERRED_FALSE(tcp_ao_needed, HZ); + int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx, unsigned int len, struct tcp_sigpool *hp) { @@ -50,6 +52,9 @@ bool tcp_ao_ignore_icmp(struct sock *sk, int type, int code) struct tcp_ao_info *ao; bool ignore_icmp = false; + if (!static_branch_unlikely(&tcp_ao_needed.key)) + return false; + /* RFC5925, 7.8: * >> A TCP-AO implementation MUST default to ignore incoming ICMPv4 * messages of Type 3 (destination unreachable), Codes 2-4 (protocol @@ -188,6 +193,9 @@ static struct tcp_ao_key *__tcp_ao_do_lookup(const struct sock *sk, struct tcp_ao_key *key; struct tcp_ao_info *ao; + if (!static_branch_unlikely(&tcp_ao_needed.key)) + return NULL; + ao = rcu_dereference_check(tcp_sk(sk)->ao_info, lockdep_sock_is_held(sk)); if (!ao) @@ -281,6 +289,7 @@ void tcp_ao_destroy_sock(struct sock *sk, bool twsk) } kfree_rcu(ao, rcu); + static_branch_slow_dec_deferred(&tcp_ao_needed); } void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp) @@ -1142,6 +1151,11 @@ int tcp_ao_copy_all_matching(const struct sock *sk, struct sock *newsk, goto free_and_exit; } + if (!static_key_fast_inc_not_disabled(&tcp_ao_needed.key.key)) { + ret = -EUSERS; + goto free_and_exit; + } + key_head = rcu_dereference(hlist_first_rcu(&new_ao->head)); first_key = hlist_entry_safe(key_head, struct tcp_ao_key, node); @@ -1585,6 +1599,10 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned short int family, tcp_ao_link_mkt(ao_info, key); if (first) { + if (!static_branch_inc(&tcp_ao_needed.key)) { + ret = -EUSERS; + goto err_free_sock; + } sk_gso_disable(sk); rcu_assign_pointer(tcp_sk(sk)->ao_info, ao_info); } @@ -1850,6 +1868,10 @@ static int tcp_ao_info_cmd(struct sock *sk, unsigned short int family, if (new_rnext) WRITE_ONCE(ao_info->rnext_key, new_rnext); if (first) { + if (!static_branch_inc(&tcp_ao_needed.key)) { + err = -EUSERS; + goto out; + } sk_gso_disable(sk); rcu_assign_pointer(tcp_sk(sk)->ao_info, ao_info); } diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 4b37e82ab612..777f5f5f721d 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -3528,41 +3528,55 @@ static inline bool tcp_may_update_window(const struct tcp_sock *tp, (ack_seq == tp->snd_wl1 && nwin > tp->snd_wnd); } -/* If we update tp->snd_una, also update tp->bytes_acked */ -static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack) +static void tcp_snd_sne_update(struct tcp_sock *tp, u32 ack) { - u32 delta = ack - tp->snd_una; #ifdef CONFIG_TCP_AO struct tcp_ao_info *ao; -#endif - sock_owned_by_me((struct sock *)tp); - tp->bytes_acked += delta; -#ifdef CONFIG_TCP_AO + if (!static_branch_unlikely(&tcp_ao_needed.key)) + return; + ao = rcu_dereference_protected(tp->ao_info, lockdep_sock_is_held((struct sock *)tp)); if (ao && ack < tp->snd_una) ao->snd_sne++; #endif +} + +/* If we update tp->snd_una, also update tp->bytes_acked */ +static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack) +{ + u32 delta = ack - tp->snd_una; + + sock_owned_by_me((struct sock *)tp); + tp->bytes_acked += delta; + tcp_snd_sne_update(tp, ack); tp->snd_una = ack; } +static void tcp_rcv_sne_update(struct tcp_sock *tp, u32 seq) +{ +#ifdef CONFIG_TCP_AO + struct tcp_ao_info *ao; + + if (!static_branch_unlikely(&tcp_ao_needed.key)) + return; + + ao = rcu_dereference_protected(tp->ao_info, + lockdep_sock_is_held((struct sock *)tp)); + if (ao && seq < tp->rcv_nxt) + ao->rcv_sne++; +#endif +} + /* If we update tp->rcv_nxt, also update tp->bytes_received */ static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq) { u32 delta = seq - tp->rcv_nxt; -#ifdef CONFIG_TCP_AO - struct tcp_ao_info *ao; -#endif sock_owned_by_me((struct sock *)tp); tp->bytes_received += delta; -#ifdef CONFIG_TCP_AO - ao = rcu_dereference_protected(tp->ao_info, - lockdep_sock_is_held((struct sock *)tp)); - if (ao && seq < tp->rcv_nxt) - ao->rcv_sne++; -#endif + tcp_rcv_sne_update(tp, seq); WRITE_ONCE(tp->rcv_nxt, seq); }