Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp310470rwd; Wed, 14 Jun 2023 16:35:24 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4epkVZ6B4JQUNre9fCYAGDlp/AxgSWeA3Y39FnrKYayrbAdh0xMnOtgznk2bBxZFZKz/M2 X-Received: by 2002:a17:907:a4e:b0:969:fc68:fa9a with SMTP id be14-20020a1709070a4e00b00969fc68fa9amr17353146ejc.40.1686785723652; Wed, 14 Jun 2023 16:35:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686785723; cv=none; d=google.com; s=arc-20160816; b=dS+o4p1DAqY8CozTvLITVLUaoMKtBDmVSAX/4BRmVFSOsWtJYB5S/uPwFfuGPZglZO +FKQ9Ph3FsXJGxEkrgzjkQCee8+vEz13eimxwoMougEqHgHYwgysIIw4rAwv7mjGjoT4 K8DvHMDL5V/iK5201aG6nazaRl2UOp8bx/r2fRCy3MH5cKStWr7ghi9+YyTmddxk6lTy 6PE+v779qcEjCnj7fhH7EztiEbVwld9L2wSeou9iNe9VjXgChZ8Ltj3jRPdT8cOiW0D4 5Wugz1+HOIv8prQ65/IL1RX0G8KKT5M/Vuc+cy76vBmjheKZfDwV/7TIb2Q8Dff2I+lr JfOw== 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=Gq3+23upRKD5uMZN30YbyseaUz2qJh28nrfHnPjZvEE=; b=LVOH/PKZ9fxXwsFbjvXxeN4Et5V1AVUfvld1PT95XvhnJ+R4wGH3dto2gwm1oTqtAt vVdPbuYMD920UvJ+P2cPLhSMu5q9X6uVo7V23vsFaIeenSqvKn7G9C585MTqyhCPIuos 6Zkei6JuOl0YjZCaFzxmCEVJ5cmack3yhl1KMD7EcGsD+ap72DmTVc1NeTfnxH7dxYTH oqESVlAdnTZrgaZcGctv6/0/Bc6JyN6TlYIYv6cfrH2XbyK/3IhaMfx8fAj6h4u3zYkT 9zP9QZqeySHo5R4KIkIa6NGGzJ2JW6ILvDzJDujlAimLS4YAS6s1rCZyEu9UrrA4TgG4 xj1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=google header.b=bc18+aW7; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h9-20020a170906260900b0096f8626d592si7372398ejc.921.2023.06.14.16.34.58; Wed, 14 Jun 2023 16:35:23 -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=bc18+aW7; 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 S236068AbjFNXMV (ORCPT + 99 others); Wed, 14 Jun 2023 19:12:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238477AbjFNXLl (ORCPT ); Wed, 14 Jun 2023 19:11:41 -0400 Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9A7E271F for ; Wed, 14 Jun 2023 16:10:57 -0700 (PDT) Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-4f122ff663eso9540731e87.2 for ; Wed, 14 Jun 2023 16:10:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1686784227; x=1689376227; 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=Gq3+23upRKD5uMZN30YbyseaUz2qJh28nrfHnPjZvEE=; b=bc18+aW7qX/yHBJcnO/+SvuF5tymxFnxONUPVcnEBvLldoAT03I7jheaAxtL3l9QO+ Wr9JRZlREi5irINBxe6iFBt5au70J4/3aa3dZ1HNbFAr0AnkAMBCVW9bNu83wSNq+02h zHwpw9ohavvGq66TT9qjSX32bdFGdLAgCnPOohZzYlMuW+fjPCI/4TI6OW/1qRM6R/YX aGSFP4Wyem3fHYpIa9AcrO48ZxnWvYod5oBVg/Y1aZGmqVXzFAdoV9PbTZSS1SVZhali lcY7h+vPvc9vP63HiNQnbEvJ17QtuIgobQJcypJCb+QtKkEG3TdCZSl4iTVJRvqsbRcI yS2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686784227; x=1689376227; 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=Gq3+23upRKD5uMZN30YbyseaUz2qJh28nrfHnPjZvEE=; b=hJNMEwW9ceDpVEjI8MWVxM1pNE11nt84BVDFQXb78FfnYSLOB4KQP8oLor07PN4T9U 7DphozSFhotEFBtumJcaGc9nsE8xSSnxNFoTyttvUZWwE6U+7eOWvUtgJ5QUdy39KaPH QABG+5IEHsucqO4SxQDkJ8QNWypc1E9hHPE5NkdFeod8k5LZdYZhmYc4446Cqu07/Y40 SajGWHVYPoKhqN3puWtO/gb1+OudDvB/eyKXxbCVclrWqIz3TqshiFomTH/cDkdkLymB 3En6BKauS0vrAeAl/IGgffUM+QO10Gfwv09mLWbb91/pv8Qf62DodvYlZtrrtJ9vOhO1 jgZg== X-Gm-Message-State: AC+VfDxoaIFWFMhI5LzEBgx43B2b9RP+4sDmmS9l8l5UoRq5dAwleXiK YrkKZJOZnjsQvYff1xDFpZcSbQ== X-Received: by 2002:a05:6512:3283:b0:4f7:5fa3:8b37 with SMTP id p3-20020a056512328300b004f75fa38b37mr3550662lfe.32.1686784226800; Wed, 14 Jun 2023 16:10:26 -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.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jun 2023 16:10:26 -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 20/22] net/tcp: Add static_key for TCP-AO Date: Thu, 15 Jun 2023 00:09:45 +0100 Message-Id: <20230614230947.3954084-21-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 Content-Transfer-Encoding: 8bit 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 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 | 23 +++++++++++++++++++++++ net/ipv4/tcp_input.c | 42 ++++++++++++++++++++++++++++-------------- 4 files changed, 56 insertions(+), 14 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index 1d3cf13ae66b..6060513ab83d 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -2585,6 +2585,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 49402458b69d..714a46e30f3f 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -138,6 +138,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 3799432a386a..2c4a31d8f177 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -17,6 +17,9 @@ #include #include +DEFINE_STATIC_KEY_DEFERRED_FALSE(tcp_ao_needed, HZ); +EXPORT_SYMBOL_GPL(tcp_ao_needed); + int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx, unsigned int len) { @@ -58,6 +61,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 @@ -198,6 +204,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) @@ -292,6 +301,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) @@ -1107,6 +1117,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); @@ -1523,6 +1538,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); } @@ -1788,6 +1807,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 eed3f7631b4b..c0c18b05fd1c 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -3524,17 +3524,14 @@ 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) { @@ -3543,20 +3540,27 @@ static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack) ao->snd_sne_seq = ack; } #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; } -/* If we update tp->rcv_nxt, also update tp->bytes_received */ -static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq) +static void tcp_rcv_sne_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 + 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) { @@ -3565,6 +3569,16 @@ static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq) ao->rcv_sne_seq = seq; } #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; + + sock_owned_by_me((struct sock *)tp); + tp->bytes_received += delta; + tcp_rcv_sne_update(tp, seq); WRITE_ONCE(tp->rcv_nxt, seq); } -- 2.40.0