From patchwork Wed Jul 19 20:26:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Safonov X-Patchwork-Id: 122813 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2692535vqt; Wed, 19 Jul 2023 13:33:13 -0700 (PDT) X-Google-Smtp-Source: APBJJlGxFFcfgv8EmbaiLHACzQY5FutxXFJbSWIIMU31nIgVjFq6THDsHrDFLcvQ6Et5IcGfBEx+ X-Received: by 2002:aa7:ccd2:0:b0:51e:166a:ac7f with SMTP id y18-20020aa7ccd2000000b0051e166aac7fmr343669edt.28.1689798793457; Wed, 19 Jul 2023 13:33:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689798793; cv=none; d=google.com; s=arc-20160816; b=AG1gwd7STC8WxiQT+hxx4cbA/xI28vuQ8Nf3TCfRPJVDoOR9Gidc/nvbaDy6ks42/3 hFGAs/oopZas/HLmn2ATaDY12X+U6hK5S5x5Xa9BAuN5wW50st+ZdnI6HFt/yoYC7XGO fI8yr5vOA78L3QrsiOPF0hGV5QW8yxkV/yfqnT8mfuRqfbemRUfsg2kqpeDQvUjNmdcC cAgXVUAkyFBQKJSg88YUiN+DmN6X5D5L+ADTK7LWwLzdQ6ZLiIayYytnJRTJnnF4ab0a 5/o0UzgGDAMAEQz9I4S6nBi9CcMaxJHmCnBBml14JwbrEZxOsfLkBQAv7O2/dmG52u2v tDFA== 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=hQpABcM7sS37o8YoY/DwDlpuOOS6tJvDIdhxnUe3kYE=; fh=+1v2wpWL3SwC67MevmNFbsOkJ0jlpe0KMhkxFTuop+s=; b=lOFPjCEC9GwR2V91U9ixbGjuyrHh3V4LAZtGIw4LzDP9b8fpT2B1tq9B0DuZi01XC0 rBd1DJ9iRT0JNpxgq3ilefgAnRY04n1KFQOHvf99HjCYDjJKxzv7ttH6Y8+Go3JtEvqt vX3e7jyipX/23PvNTVZafUrmTKewHy5pj/ZAMvsyIHQlvGB6ZajarkGJNrsguHQsEVBg trf4SPgGIBfD11+P4QZiHnmBQikNX1L5l1gsz0i8hyTq0Brlucgsn1A9UXnpiB/Vhhz5 w3kje79Ep7VDUfW+GCOOWsuxY1Ww00JVY3XOGnKFNsyeEE8gb7+GC6YeVRwSrZiDMbQD qtUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=google header.b=APNfnCTg; 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 d24-20020a056402001800b00521d83e8d9esi45247edu.150.2023.07.19.13.32.49; Wed, 19 Jul 2023 13:33:13 -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=APNfnCTg; 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 S231502AbjGSU3A (ORCPT + 99 others); Wed, 19 Jul 2023 16:29:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231130AbjGSU2Z (ORCPT ); Wed, 19 Jul 2023 16:28:25 -0400 Received: from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com [IPv6:2a00:1450:4864:20::12c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD27C2D65 for ; Wed, 19 Jul 2023 13:27:25 -0700 (PDT) Received: by mail-lf1-x12c.google.com with SMTP id 2adb3069b0e04-4fbc0314a7bso16504e87.2 for ; Wed, 19 Jul 2023 13:27:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1689798435; x=1692390435; 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=hQpABcM7sS37o8YoY/DwDlpuOOS6tJvDIdhxnUe3kYE=; b=APNfnCTg3peg2X2VpDJakvam7cSGTNJkUnEZa77PX3rAFJ8AUx8x1xCzeY4AT5uGTe Q/CDQXiX7Aziu1zm8c3o+DISgLv1ID2zXjxs6Wy/E7ekhgZoPAN5QwpuM5hidFhdXvea txuvQR6YJ2a+stD0ewHYedVEEcdHSV15ddDFq0zY6myeAdjS8+2ddjJIFD029Nr6sw/z Vm/8K87jer0SjtGgk0rDih1+CO04XR1PzCJ6AszqWoB4anzKTEOKqjbRAYT0p9uBoH0z 6qJjlj5XmAmEjVnHwTg6Jvufm5ZYmOTtjMCCjytF23kW6U+tl5jU3w9oEx2uuSJ+qr9g BWLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689798435; x=1692390435; 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=hQpABcM7sS37o8YoY/DwDlpuOOS6tJvDIdhxnUe3kYE=; b=BCOpLH3TwX+onY17CaM5PmYhYdnj0bHvZTrXJNa0Aa2HLQaW0aM/Irb/yd4zcaViZ0 QMRP4oZLCx5dPyxuoVsnTveEeDcK1BsR/ExOI+nk5XChySlzO4Ln3aTCUn/DJPe/UMWi FUXo6IGcYlqc8GG1KrJ8ynmztI9yv3SXPleAOv/P/JXivuw/mDKu4bLI8HdAQBvOJL6B f67X8EY5H0GQ/mOthkTBg23p9WHAkkwOr2nNBqdrtQN6OUmRXmjfQfw5Bhsg4c+LHAYK wBbcbaYqnwB1WRLACAYyYkgYTZhNMaDIx6Ouh3qs1+qtwa7/EXebHzSrB/t+9gqXhGO3 we+g== X-Gm-Message-State: ABy/qLZV+QMhIco91O/eZ64dnNCmZwB344coBCoxhCt1BMJ5Dz9+U3n8 FJW2Fsfv5Pi48IiBjZIw3amQ4g== X-Received: by 2002:a19:6512:0:b0:4fb:7d73:d097 with SMTP id z18-20020a196512000000b004fb7d73d097mr88792lfb.39.1689798435347; Wed, 19 Jul 2023 13:27:15 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id 3-20020a05600c234300b003fc04d13242sm2432319wmq.0.2023.07.19.13.27.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jul 2023 13:27: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 , Salam Noureddine , "Tetreault, Francois" , netdev@vger.kernel.org Subject: [PATCH v8 22/23] net/tcp: Add TCP_AO_REPAIR Date: Wed, 19 Jul 2023 21:26:27 +0100 Message-ID: <20230719202631.472019-23-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230719202631.472019-1-dima@arista.com> References: <20230719202631.472019-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: 1771882459812224728 X-GMAIL-MSGID: 1771882459812224728 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 --- include/net/tcp_ao.h | 14 ++++++ include/uapi/linux/tcp.h | 8 ++++ net/ipv4/tcp.c | 24 ++++++++--- net/ipv4/tcp_ao.c | 92 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 127 insertions(+), 11 deletions(-) diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index b56b49a98266..0027f4b4683d 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -196,6 +196,8 @@ void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp); bool tcp_ao_ignore_icmp(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, @@ -317,6 +319,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 979ff960fddb..624198fa170d 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 @@ -445,6 +446,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 f329a88b3ede..581748a0fd64 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -3605,20 +3605,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 @@ -4285,6 +4293,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 3490b590bff0..70ff7822f57a 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -1463,6 +1463,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) @@ -1647,11 +1657,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) { @@ -1899,6 +1911,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; @@ -2281,3 +2295,73 @@ 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(ao)) + return PTR_ERR(ao); + if (!ao) { + rcu_read_unlock(); + return -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; +}