Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp337280rdb; Mon, 18 Sep 2023 17:35:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE+BFS9cvbodHYoezN5GUHT3DaKJKuZYS2+rMQ14ALnL/PSzF0OcO5fylL9o3Vm/E/Cen+W X-Received: by 2002:a05:6a20:e115:b0:128:ffb7:dcfe with SMTP id kr21-20020a056a20e11500b00128ffb7dcfemr1537845pzb.1.1695083731878; Mon, 18 Sep 2023 17:35:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695083731; cv=none; d=google.com; s=arc-20160816; b=lcPpV0z3aQJnT6lW2+19gMb7KQWJlST5Vn5zHdCxhLXVjFSWuXjyPCJ4hjOdFe0f6D X7XERqIdL4/2CHXg16GwmvjfhbOXUICGWtWDOWzoL4WgqblzZP4pQfpp0ebrAKOyazRF vnsuzvv8ZBS4Hc6xQp909PDdLuSYP7j8v9pk/e7Xw42pH3thLGU36TBirrQSfaZJMeH/ taW0yC6J0Fy2o7DS59Nj6BGf9pSgcbq36m7msTGNzOKRLGouYvmsLU60X/dHu08QvDpK 6ssNHlb151KfJATnqwcPSAYBC3gAzwoGG9hUM9hcZGv2GIW4JctB2mTTGhE9jrYEwyUX 5U9g== 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=1CBNKjDAvF0Yu5CQhKp6AGPHKjMDHCa24yq0eTRYb6Y=; fh=58X8Twod/aefd4Yph/F0FPyIOMSYi96Sqi5HmPAz0To=; b=tr8AvslTxsdicf067rA/FI5q/hR7s3ir/Q4VXAvIp3D+G4rdiyuUoSx9ODm8uLS6Fg s+XoddXHXIeDzooRo0ZbyzGaCHyw+AHuhlWmpc7AkPy5yjsmZ5Em0egD+slQONw5o5wa x+IbZDIzUIRUWzQT07CNBfVQbOSajeag1mT1MigXXc54XWp1XseEo/P+zlCcMEmnOwAP SBQqYAv+3tm70x19MMe6Tekhewl3wp9BBL7dG1Vk0FSJHgdni/zQ1w2zrzMH51sba63y rEf/X1XUahmICqqNsV5KiKDc6wWGVurNx0cT+kGq6LFVB8hNrax5a5vGcX7znWPUGyZi /c0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=google header.b=WKa3T8KV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id s205-20020a632cd6000000b0054fe295d487si8575539pgs.434.2023.09.18.17.35.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 17:35:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@arista.com header.s=google header.b=WKa3T8KV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id BFCAA802093C; Mon, 18 Sep 2023 12:03:05 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230314AbjIRTCo (ORCPT + 99 others); Mon, 18 Sep 2023 15:02:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230052AbjIRTB7 (ORCPT ); Mon, 18 Sep 2023 15:01:59 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5D82192 for ; Mon, 18 Sep 2023 12:01:18 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-3ff1c397405so52799315e9.3 for ; Mon, 18 Sep 2023 12:01:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1695063676; x=1695668476; 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=1CBNKjDAvF0Yu5CQhKp6AGPHKjMDHCa24yq0eTRYb6Y=; b=WKa3T8KV3W9iNqfwx2RmUSfpn6jys2l3nNDLn3xQ9jm58v3QcIQvK449nRVeYBTGl2 yAnm0CEBNHOpoDbHQnbzclPdGWCpRXQgmByndLv0p3bgNhgOz4VROLDtvNVcxfVKu50Q m2Nty3Gg/6zLdDqp25xOSS9nge/56W5seyAiYxO4xBSXHnOYLakb+VOYujR/vqP+XlsU fLEUpKPc3Ra0rKQOBuJ5KU+eno6FMO01cQ4VqavDj2W0vSSKa3IKQ3ucCAyacQxcypYy /g1u3maK7J9dsckgk4nuFktNSERVZ20rwYA7PktXCX+nEoos8nBPk/rUAczfcFzpNxf+ CTFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695063676; x=1695668476; 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=1CBNKjDAvF0Yu5CQhKp6AGPHKjMDHCa24yq0eTRYb6Y=; b=I7cgD9+22fizPR1znx0eTxsPrA0BFU4CZVGDBxAfw+iU0BInmAaW56zeDCLrD1qKzX /5xRgmtnMS2zdrbeO4oHveZbKhSiFiFlNG6cHEcz0OV0FPAxW0rVrj46dOcnVXHEZjt2 Bcc9tHPIayDcE6hvec+P8Llxj9t3he3osKL8NEiJ9ms37/r3moMt9ocyQkbqwdNDVlwg t3YqgmfPSf5pxvbAs3iMLl0Ns/LTdsLRUaLUmBoSZNmRZZIPWhMmHXMRWohxmD1o5gcT UI7dXBwbg9xR+nSEsG7IVOyqc2MVjOl78NHqI0YbRds+kgTW+85QrXy7LIvZnzAX4dV2 z2Ug== X-Gm-Message-State: AOJu0YxV51oxLi3k+P3FCEjTY+3BpbL+/OSuKXTQjb3MFHKRECN7N66d nvgIuxl6f0mYmt1d3Myu+9tf618sEXOZkaVXRi0= X-Received: by 2002:a05:600c:247:b0:402:d72:bee5 with SMTP id 7-20020a05600c024700b004020d72bee5mr9287137wmj.21.1695063676143; Mon, 18 Sep 2023 12:01:16 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id n9-20020a05600c3b8900b004047ac770d1sm10762707wms.8.2023.09.18.12.01.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 12:01:15 -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 , "Nassiri, Mohammad" , Salam Noureddine , Simon Horman , "Tetreault, Francois" , netdev@vger.kernel.org Subject: [PATCH v12 net-next 22/23] net/tcp: Add TCP_AO_REPAIR Date: Mon, 18 Sep 2023 20:00:20 +0100 Message-ID: <20230918190027.613430-23-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230918190027.613430-1-dima@arista.com> References: <20230918190027.613430-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_BLOCKED,SPF_HELO_NONE,SPF_NONE 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 18 Sep 2023 12:03:06 -0700 (PDT) Add TCP_AO_REPAIR setsockopt(), getsockopt(). They let a user to repair TCP-AO ISNs/SNEs. Also let the user hack around when (tp->repair) is on and add ao_info on a socket in any supported state. As SNEs now can be read/written at any moment, use WRITE_ONCE()/READ_ONCE() to set/read them. Signed-off-by: Dmitry Safonov Acked-by: David Ahern --- include/net/tcp_ao.h | 14 +++++++ include/uapi/linux/tcp.h | 8 ++++ net/ipv4/tcp.c | 24 +++++++---- net/ipv4/tcp_ao.c | 90 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 125 insertions(+), 11 deletions(-) diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index da04efa1aaa5..dfdd980c5d87 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -197,6 +197,8 @@ void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp); bool tcp_ao_ignore_icmp(const struct sock *sk, int type, int code); int tcp_ao_get_mkts(struct sock *sk, sockptr_t optval, sockptr_t optlen); int tcp_ao_get_sock_info(struct sock *sk, sockptr_t optval, sockptr_t optlen); +int tcp_ao_get_repair(struct sock *sk, sockptr_t optval, sockptr_t optlen); +int tcp_ao_set_repair(struct sock *sk, sockptr_t optval, unsigned int optlen); enum skb_drop_reason tcp_inbound_ao_hash(struct sock *sk, const struct sk_buff *skb, unsigned short int family, const struct request_sock *req, int l3index, @@ -315,6 +317,18 @@ static inline int tcp_ao_get_sock_info(struct sock *sk, sockptr_t optval, sockpt { return -ENOPROTOOPT; } + +static inline int tcp_ao_get_repair(struct sock *sk, + sockptr_t optval, sockptr_t optlen) +{ + return -ENOPROTOOPT; +} + +static inline int tcp_ao_set_repair(struct sock *sk, + sockptr_t optval, unsigned int optlen) +{ + return -ENOPROTOOPT; +} #endif #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) diff --git a/include/uapi/linux/tcp.h b/include/uapi/linux/tcp.h index 25d62ecb9532..3440f8bbab46 100644 --- a/include/uapi/linux/tcp.h +++ b/include/uapi/linux/tcp.h @@ -133,6 +133,7 @@ enum { #define TCP_AO_DEL_KEY 39 /* Delete MKT */ #define TCP_AO_INFO 40 /* Set/list TCP-AO per-socket options */ #define TCP_AO_GET_KEYS 41 /* List MKT(s) */ +#define TCP_AO_REPAIR 42 /* Get/Set SNEs and ISNs */ #define TCP_REPAIR_ON 1 #define TCP_REPAIR_OFF 0 @@ -457,6 +458,13 @@ struct tcp_ao_getsockopt { /* getsockopt(TCP_AO_GET_KEYS) */ __u64 pkt_bad; /* out: segments that failed verification */ } __attribute__((aligned(8))); +struct tcp_ao_repair { /* {s,g}etsockopt(TCP_AO_REPAIR) */ + __be32 snt_isn; + __be32 rcv_isn; + __u32 snd_sne; + __u32 rcv_sne; +} __attribute__((aligned(8))); + /* setsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, ...) */ #define TCP_RECEIVE_ZEROCOPY_FLAG_TLB_CLEAN_HINT 0x1 diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index d745086bf1a8..7263cd609cfb 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -3597,20 +3597,28 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname, __tcp_sock_set_quickack(sk, val); break; + case TCP_AO_REPAIR: + err = tcp_ao_set_repair(sk, optval, optlen); + break; #ifdef CONFIG_TCP_AO case TCP_AO_ADD_KEY: case TCP_AO_DEL_KEY: case TCP_AO_INFO: { /* If this is the first TCP-AO setsockopt() on the socket, - * sk_state has to be LISTEN or CLOSE + * sk_state has to be LISTEN or CLOSE. Allow TCP_REPAIR + * in any state. */ - if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) || - rcu_dereference_protected(tcp_sk(sk)->ao_info, + if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) + goto ao_parse; + if (rcu_dereference_protected(tcp_sk(sk)->ao_info, lockdep_sock_is_held(sk))) - err = tp->af_specific->ao_parse(sk, optname, optval, - optlen); - else - err = -EISCONN; + goto ao_parse; + if (tp->repair) + goto ao_parse; + err = -EISCONN; + break; +ao_parse: + err = tp->af_specific->ao_parse(sk, optname, optval, optlen); break; } #endif @@ -4277,6 +4285,8 @@ int do_tcp_getsockopt(struct sock *sk, int level, return err; } #endif + case TCP_AO_REPAIR: + return tcp_ao_get_repair(sk, optval, optlen); case TCP_AO_GET_KEYS: case TCP_AO_INFO: { int err; diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index 522d3c84ac96..8a5c2824a9b4 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -1439,6 +1439,16 @@ static struct tcp_ao_info *setsockopt_ao_info(struct sock *sk) return ERR_PTR(-ESOCKTNOSUPPORT); } +static struct tcp_ao_info *getsockopt_ao_info(struct sock *sk) +{ + if (sk_fullsock(sk)) + return rcu_dereference(tcp_sk(sk)->ao_info); + else if (sk->sk_state == TCP_TIME_WAIT) + return rcu_dereference(tcp_twsk(sk)->ao_info); + + return ERR_PTR(-ESOCKTNOSUPPORT); +} + #define TCP_AO_KEYF_ALL (TCP_AO_KEYF_IFINDEX | TCP_AO_KEYF_EXCLUDE_OPT) #define TCP_AO_GET_KEYF_VALID (TCP_AO_KEYF_IFINDEX) @@ -1620,11 +1630,13 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned short int family, if (ret < 0) goto err_free_sock; - /* Change this condition if we allow adding keys in states - * like close_wait, syn_sent or fin_wait... - */ - if (sk->sk_state == TCP_ESTABLISHED) + if (!((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))) { tcp_ao_cache_traffic_keys(sk, ao_info, key); + if (first) { + ao_info->current_key = key; + ao_info->rnext_key = key; + } + } tcp_ao_link_mkt(ao_info, key); if (first) { @@ -1875,6 +1887,8 @@ static int tcp_ao_info_cmd(struct sock *sk, unsigned short int family, if (IS_ERR(ao_info)) return PTR_ERR(ao_info); if (!ao_info) { + if (!((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))) + return -EINVAL; ao_info = tcp_ao_alloc_info(GFP_KERNEL); if (!ao_info) return -ENOMEM; @@ -2257,3 +2271,71 @@ int tcp_ao_get_sock_info(struct sock *sk, sockptr_t optval, sockptr_t optlen) return 0; } +int tcp_ao_set_repair(struct sock *sk, sockptr_t optval, unsigned int optlen) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct tcp_ao_repair cmd; + struct tcp_ao_key *key; + struct tcp_ao_info *ao; + int err; + + if (optlen < sizeof(cmd)) + return -EINVAL; + + err = copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen); + if (err) + return err; + + if (!tp->repair) + return -EPERM; + + ao = setsockopt_ao_info(sk); + if (IS_ERR(ao)) + return PTR_ERR(ao); + if (!ao) + return -ENOENT; + + WRITE_ONCE(ao->lisn, cmd.snt_isn); + WRITE_ONCE(ao->risn, cmd.rcv_isn); + WRITE_ONCE(ao->snd_sne, cmd.snd_sne); + WRITE_ONCE(ao->rcv_sne, cmd.rcv_sne); + + hlist_for_each_entry_rcu(key, &ao->head, node) + tcp_ao_cache_traffic_keys(sk, ao, key); + + return 0; +} + +int tcp_ao_get_repair(struct sock *sk, sockptr_t optval, sockptr_t optlen) +{ + struct tcp_sock *tp = tcp_sk(sk); + struct tcp_ao_repair opt; + struct tcp_ao_info *ao; + int len; + + if (copy_from_sockptr(&len, optlen, sizeof(int))) + return -EFAULT; + + if (len <= 0) + return -EINVAL; + + if (!tp->repair) + return -EPERM; + + rcu_read_lock(); + ao = getsockopt_ao_info(sk); + if (IS_ERR_OR_NULL(ao)) { + rcu_read_unlock(); + return ao ? PTR_ERR(ao) : -ENOENT; + } + + opt.snt_isn = ao->lisn; + opt.rcv_isn = ao->risn; + opt.snd_sne = READ_ONCE(ao->snd_sne); + opt.rcv_sne = READ_ONCE(ao->rcv_sne); + rcu_read_unlock(); + + if (copy_to_sockptr(optval, &opt, min_t(int, len, sizeof(opt)))) + return -EFAULT; + return 0; +} -- 2.41.0