From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 65D2BC001E0 for ; Tue, 15 Aug 2023 19:15:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239853AbjHOTPW (ORCPT ); Tue, 15 Aug 2023 15:15:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239797AbjHOTPL (ORCPT ); Tue, 15 Aug 2023 15:15:11 -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 969F2B8 for ; Tue, 15 Aug 2023 12:15:06 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-3fe4ad22e36so53930075e9.2 for ; Tue, 15 Aug 2023 12:15:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126905; x=1692731705; 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=mL96PD4XhFx0atT2U9OnbcX/4XS9s+UWHWujPekL1nY=; b=i4uoNNjbhCO6KQg/TKkbLPTNqfVS/Xn3DwNhD+EH6Sih7SS2fkQVKmUyT1DNMy3eVz bCc3PCw2IWmH6VbmdY0Z07sgPdw7s/7hsJ3ZMF+v5o0YqtmORwE5voqai4q2wGk86nMJ GY5AKgmVrympbrqW4TmodgJQlaA87B3TU9GWnuhZHv6oQwfxlc0Sy0foqL6LedQYBvTk J/nj3tiT+SxqZh96B2klW6t2LCllOxKu8j9CT3O6pEnc+hjSxV0z2v7ZTo4S2b2HkoiC VBg9ovBO5THnq7aNV9opQJ8DpLpZ/1AbV4fYoazfS1tAwbx+d0sa4IVVHjEWy1s5Ehdk 06Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126905; x=1692731705; 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=mL96PD4XhFx0atT2U9OnbcX/4XS9s+UWHWujPekL1nY=; b=THCnRrwco3REcCLbifGxdJySDqVDsZNBakWrZ+yacQK6le70yw+d8eoSc87rJBnhuf n3Ic4xTid/148xPlhJ1E0HBO/6n5dc2ZFX8yf56t0/gpoEaO8bDCNbbbfej+3RYcI/Rj nAuXlzUCv9FLiH5h9BBvmCNfPatYRQSCO7Wa6RQnjpdbqHTLrsLxfP6lWsxkrh5g/h8l GCT/1E83mv1nLwuQENmEN2W69Vy4Qbr+hMBKjxBUsiv+fxx2yDsfDZIt7IrIf113Mpjs UbdCqOttpQ1eGO+pbFSshQ0yqUXY6WW7rlB7J/XMNgB4jruaCxalNjYEtxJYYKomC5NQ UfYA== X-Gm-Message-State: AOJu0Yyo5NvB1eaJ1YsJK8YSsBwCUQm4weMcg0JKvdjgCEFKcGpRrjwI 4iYvybhHoh5/x4mm7UcCxQ+Xgw== X-Google-Smtp-Source: AGHT+IFwJK93e3MF5kZvUtP7AL/10ezmVYysvgOe3oI5LX8fP2SF/LtR/X08tEcz+Apcl/vfDsh8KQ== X-Received: by 2002:a7b:c38d:0:b0:3fe:179a:9eef with SMTP id s13-20020a7bc38d000000b003fe179a9eefmr9604961wmj.40.1692126904879; Tue, 15 Aug 2023 12:15:04 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:04 -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, Steen Hegelund Subject: [PATCH v10 net-next 01/23] net/tcp: Prepare tcp_md5sig_pool for TCP-AO Date: Tue, 15 Aug 2023 20:14:30 +0100 Message-ID: <20230815191455.1872316-2-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" TCP-AO, similarly to TCP-MD5, needs to allocate tfms on a slow-path, which is setsockopt() and use crypto ahash requests on fast paths, which are RX/TX softirqs. Also, it needs a temporary/scratch buffer for preparing the hash. Rework tcp_md5sig_pool in order to support other hashing algorithms than MD5. It will make it possible to share pre-allocated crypto_ahash descriptors and scratch area between all TCP hash users. Internally tcp_sigpool calls crypto_clone_ahash() API over pre-allocated crypto ahash tfm. Kudos to Herbert, who provided this new crypto API. I was a little concerned over GFP_ATOMIC allocations of ahash and crypto_request in RX/TX (see tcp_sigpool_start()), so I benchmarked both "backends" with different algorithms, using patched version of iperf3[2]. On my laptop with i7-7600U @ 2.80GHz: clone-tfm per-CPU-requests TCP-MD5 2.25 Gbits/sec 2.30 Gbits/sec TCP-AO(hmac(sha1)) 2.53 Gbits/sec 2.54 Gbits/sec TCP-AO(hmac(sha512)) 1.67 Gbits/sec 1.64 Gbits/sec TCP-AO(hmac(sha384)) 1.77 Gbits/sec 1.80 Gbits/sec TCP-AO(hmac(sha224)) 1.29 Gbits/sec 1.30 Gbits/sec TCP-AO(hmac(sha3-512)) 481 Mbits/sec 480 Mbits/sec TCP-AO(hmac(md5)) 2.07 Gbits/sec 2.12 Gbits/sec TCP-AO(hmac(rmd160)) 1.01 Gbits/sec 995 Mbits/sec TCP-AO(cmac(aes128)) [not supporetd yet] 2.11 Gbits/sec So, it seems that my concerns don't have strong grounds and per-CPU crypto_request allocation can be dropped/removed from tcp_sigpool once ciphers get crypto_clone_ahash() support. [1]: https://lore.kernel.org/all/ZDefxOq6Ax0JeTRH@gondor.apana.org.au/T/#u [2]: https://github.com/0x7f454c46/iperf/tree/tcp-md5-ao Signed-off-by: Dmitry Safonov Reviewed-by: Steen Hegelund Acked-by: David Ahern --- include/net/tcp.h | 50 ++++-- net/ipv4/Kconfig | 4 + net/ipv4/Makefile | 1 + net/ipv4/tcp.c | 145 +++------------- net/ipv4/tcp_ipv4.c | 97 ++++++----- net/ipv4/tcp_minisocks.c | 21 ++- net/ipv4/tcp_sigpool.c | 358 +++++++++++++++++++++++++++++++++++++++ net/ipv6/tcp_ipv6.c | 60 +++---- 8 files changed, 525 insertions(+), 211 deletions(-) create mode 100644 net/ipv4/tcp_sigpool.c diff --git a/include/net/tcp.h b/include/net/tcp.h index 6d77c08d83b7..506138e69643 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -1700,12 +1700,39 @@ union tcp_md5sum_block { #endif }; =20 -/* - pool: digest algorithm, hash description and scratch buffer */ -struct tcp_md5sig_pool { - struct ahash_request *md5_req; - void *scratch; +/* + * struct tcp_sigpool - per-CPU pool of ahash_requests + * @scratch: per-CPU temporary area, that can be used between + * tcp_sigpool_start() and tcp_sigpool_end() to perform + * crypto request + * @req: pre-allocated ahash request + */ +struct tcp_sigpool { + void *scratch; + struct ahash_request *req; }; =20 +int tcp_sigpool_alloc_ahash(const char *alg, size_t scratch_size); +void tcp_sigpool_get(unsigned int id); +void tcp_sigpool_release(unsigned int id); +int tcp_sigpool_hash_skb_data(struct tcp_sigpool *hp, + const struct sk_buff *skb, + unsigned int header_len); + +/** + * tcp_sigpool_start - disable bh and start using tcp_sigpool_ahash + * @id: tcp_sigpool that was previously allocated by tcp_sigpool_alloc_aha= sh() + * @c: returned tcp_sigpool for usage (uninitialized on failure) + * + * Returns 0 on success, error otherwise. + */ +int tcp_sigpool_start(unsigned int id, struct tcp_sigpool *c); +/** + * tcp_sigpool_end - enable bh and stop using tcp_sigpool + * @c: tcp_sigpool context that was returned by tcp_sigpool_start() + */ +void tcp_sigpool_end(struct tcp_sigpool *c); +size_t tcp_sigpool_algo(unsigned int id, char *buf, size_t buf_len); /* - functions */ int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key, const struct sock *sk, const struct sk_buff *skb); @@ -1761,17 +1788,12 @@ tcp_inbound_md5_hash(const struct sock *sk, const s= truct sk_buff *skb, #define tcp_twsk_md5_key(twsk) NULL #endif =20 -bool tcp_alloc_md5sig_pool(void); +int tcp_md5_alloc_sigpool(void); +void tcp_md5_release_sigpool(void); +void tcp_md5_add_sigpool(void); +extern int tcp_md5_sigpool_id; =20 -struct tcp_md5sig_pool *tcp_get_md5sig_pool(void); -static inline void tcp_put_md5sig_pool(void) -{ - local_bh_enable(); -} - -int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, const struct sk_buff *, - unsigned int header_len); -int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, +int tcp_md5_hash_key(struct tcp_sigpool *hp, const struct tcp_md5sig_key *key); =20 /* From tcp_fastopen.c */ diff --git a/net/ipv4/Kconfig b/net/ipv4/Kconfig index 2dfb12230f08..89e2ab023272 100644 --- a/net/ipv4/Kconfig +++ b/net/ipv4/Kconfig @@ -741,10 +741,14 @@ config DEFAULT_TCP_CONG default "bbr" if DEFAULT_BBR default "cubic" =20 +config TCP_SIGPOOL + tristate + config TCP_MD5SIG bool "TCP: MD5 Signature Option support (RFC2385)" select CRYPTO select CRYPTO_MD5 + select TCP_SIGPOOL help RFC2385 specifies a method of giving MD5 protection to TCP sessions. Its main (only?) use is to protect BGP sessions between core routers diff --git a/net/ipv4/Makefile b/net/ipv4/Makefile index b18ba8ef93ad..cd760793cfcb 100644 --- a/net/ipv4/Makefile +++ b/net/ipv4/Makefile @@ -62,6 +62,7 @@ obj-$(CONFIG_TCP_CONG_SCALABLE) +=3D tcp_scalable.o obj-$(CONFIG_TCP_CONG_LP) +=3D tcp_lp.o obj-$(CONFIG_TCP_CONG_YEAH) +=3D tcp_yeah.o obj-$(CONFIG_TCP_CONG_ILLINOIS) +=3D tcp_illinois.o +obj-$(CONFIG_TCP_SIGPOOL) +=3D tcp_sigpool.o obj-$(CONFIG_NET_SOCK_MSG) +=3D tcp_bpf.o obj-$(CONFIG_BPF_SYSCALL) +=3D udp_bpf.o obj-$(CONFIG_NETLABEL) +=3D cipso_ipv4.o diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 4fbc7ff8c53c..c6b0113ccc79 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -4290,141 +4290,52 @@ int tcp_getsockopt(struct sock *sk, int level, int= optname, char __user *optval, EXPORT_SYMBOL(tcp_getsockopt); =20 #ifdef CONFIG_TCP_MD5SIG -static DEFINE_PER_CPU(struct tcp_md5sig_pool, tcp_md5sig_pool); -static DEFINE_MUTEX(tcp_md5sig_mutex); -static bool tcp_md5sig_pool_populated =3D false; +int tcp_md5_sigpool_id =3D -1; +EXPORT_SYMBOL_GPL(tcp_md5_sigpool_id); =20 -static void __tcp_alloc_md5sig_pool(void) +int tcp_md5_alloc_sigpool(void) { - struct crypto_ahash *hash; - int cpu; + size_t scratch_size; + int ret; =20 - hash =3D crypto_alloc_ahash("md5", 0, CRYPTO_ALG_ASYNC); - if (IS_ERR(hash)) - return; - - for_each_possible_cpu(cpu) { - void *scratch =3D per_cpu(tcp_md5sig_pool, cpu).scratch; - struct ahash_request *req; - - if (!scratch) { - scratch =3D kmalloc_node(sizeof(union tcp_md5sum_block) + - sizeof(struct tcphdr), - GFP_KERNEL, - cpu_to_node(cpu)); - if (!scratch) - return; - per_cpu(tcp_md5sig_pool, cpu).scratch =3D scratch; - } - if (per_cpu(tcp_md5sig_pool, cpu).md5_req) - continue; - - req =3D ahash_request_alloc(hash, GFP_KERNEL); - if (!req) - return; - - ahash_request_set_callback(req, 0, NULL, NULL); - - per_cpu(tcp_md5sig_pool, cpu).md5_req =3D req; + scratch_size =3D sizeof(union tcp_md5sum_block) + sizeof(struct tcphdr); + ret =3D tcp_sigpool_alloc_ahash("md5", scratch_size); + if (ret >=3D 0) { + /* As long as any md5 sigpool was allocated, the return + * id would stay the same. Re-write the id only for the case + * when previously all MD5 keys were deleted and this call + * allocates the first MD5 key, which may return a different + * sigpool id than was used previously. + */ + WRITE_ONCE(tcp_md5_sigpool_id, ret); /* Avoids the compiler potentially = being smart here */ + return 0; } - /* before setting tcp_md5sig_pool_populated, we must commit all writes - * to memory. See smp_rmb() in tcp_get_md5sig_pool() - */ - smp_wmb(); - /* Paired with READ_ONCE() from tcp_alloc_md5sig_pool() - * and tcp_get_md5sig_pool(). - */ - WRITE_ONCE(tcp_md5sig_pool_populated, true); + return ret; } =20 -bool tcp_alloc_md5sig_pool(void) +void tcp_md5_release_sigpool(void) { - /* Paired with WRITE_ONCE() from __tcp_alloc_md5sig_pool() */ - if (unlikely(!READ_ONCE(tcp_md5sig_pool_populated))) { - mutex_lock(&tcp_md5sig_mutex); - - if (!tcp_md5sig_pool_populated) - __tcp_alloc_md5sig_pool(); - - mutex_unlock(&tcp_md5sig_mutex); - } - /* Paired with WRITE_ONCE() from __tcp_alloc_md5sig_pool() */ - return READ_ONCE(tcp_md5sig_pool_populated); + tcp_sigpool_release(READ_ONCE(tcp_md5_sigpool_id)); } -EXPORT_SYMBOL(tcp_alloc_md5sig_pool); =20 - -/** - * tcp_get_md5sig_pool - get md5sig_pool for this user - * - * We use percpu structure, so if we succeed, we exit with preemption - * and BH disabled, to make sure another thread or softirq handling - * wont try to get same context. - */ -struct tcp_md5sig_pool *tcp_get_md5sig_pool(void) +void tcp_md5_add_sigpool(void) { - local_bh_disable(); - - /* Paired with WRITE_ONCE() from __tcp_alloc_md5sig_pool() */ - if (READ_ONCE(tcp_md5sig_pool_populated)) { - /* coupled with smp_wmb() in __tcp_alloc_md5sig_pool() */ - smp_rmb(); - return this_cpu_ptr(&tcp_md5sig_pool); - } - local_bh_enable(); - return NULL; + tcp_sigpool_get(READ_ONCE(tcp_md5_sigpool_id)); } -EXPORT_SYMBOL(tcp_get_md5sig_pool); =20 -int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *hp, - const struct sk_buff *skb, unsigned int header_len) -{ - struct scatterlist sg; - const struct tcphdr *tp =3D tcp_hdr(skb); - struct ahash_request *req =3D hp->md5_req; - unsigned int i; - const unsigned int head_data_len =3D skb_headlen(skb) > header_len ? - skb_headlen(skb) - header_len : 0; - const struct skb_shared_info *shi =3D skb_shinfo(skb); - struct sk_buff *frag_iter; - - sg_init_table(&sg, 1); - - sg_set_buf(&sg, ((u8 *) tp) + header_len, head_data_len); - ahash_request_set_crypt(req, &sg, NULL, head_data_len); - if (crypto_ahash_update(req)) - return 1; - - for (i =3D 0; i < shi->nr_frags; ++i) { - const skb_frag_t *f =3D &shi->frags[i]; - unsigned int offset =3D skb_frag_off(f); - struct page *page =3D skb_frag_page(f) + (offset >> PAGE_SHIFT); - - sg_set_page(&sg, page, skb_frag_size(f), - offset_in_page(offset)); - ahash_request_set_crypt(req, &sg, NULL, skb_frag_size(f)); - if (crypto_ahash_update(req)) - return 1; - } - - skb_walk_frags(skb, frag_iter) - if (tcp_md5_hash_skb_data(hp, frag_iter, 0)) - return 1; - - return 0; -} -EXPORT_SYMBOL(tcp_md5_hash_skb_data); - -int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, const struct tcp_md5sig_k= ey *key) +int tcp_md5_hash_key(struct tcp_sigpool *hp, + const struct tcp_md5sig_key *key) { u8 keylen =3D READ_ONCE(key->keylen); /* paired with WRITE_ONCE() in tcp_= md5_do_add */ struct scatterlist sg; =20 sg_init_one(&sg, key->key, keylen); - ahash_request_set_crypt(hp->md5_req, &sg, NULL, keylen); + ahash_request_set_crypt(hp->req, &sg, NULL, keylen); =20 - /* We use data_race() because tcp_md5_do_add() might change key->key unde= r us */ - return data_race(crypto_ahash_update(hp->md5_req)); + /* We use data_race() because tcp_md5_do_add() might change + * key->key under us + */ + return data_race(crypto_ahash_update(hp->req)); } EXPORT_SYMBOL(tcp_md5_hash_key); =20 diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 5b18a048f613..d3d7a03002f6 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -1220,10 +1220,6 @@ static int __tcp_md5_do_add(struct sock *sk, const u= nion tcp_md5_addr *addr, key =3D sock_kmalloc(sk, sizeof(*key), gfp | __GFP_ZERO); if (!key) return -ENOMEM; - if (!tcp_alloc_md5sig_pool()) { - sock_kfree_s(sk, key, sizeof(*key)); - return -ENOMEM; - } =20 memcpy(key->key, newkey, newkeylen); key->keylen =3D newkeylen; @@ -1245,15 +1241,21 @@ int tcp_md5_do_add(struct sock *sk, const union tcp= _md5_addr *addr, struct tcp_sock *tp =3D tcp_sk(sk); =20 if (!rcu_dereference_protected(tp->md5sig_info, lockdep_sock_is_held(sk))= ) { - if (tcp_md5sig_info_add(sk, GFP_KERNEL)) + if (tcp_md5_alloc_sigpool()) return -ENOMEM; =20 + if (tcp_md5sig_info_add(sk, GFP_KERNEL)) { + tcp_md5_release_sigpool(); + return -ENOMEM; + } + if (!static_branch_inc(&tcp_md5_needed.key)) { struct tcp_md5sig_info *md5sig; =20 md5sig =3D rcu_dereference_protected(tp->md5sig_info, lockdep_sock_is_h= eld(sk)); rcu_assign_pointer(tp->md5sig_info, NULL); kfree_rcu(md5sig, rcu); + tcp_md5_release_sigpool(); return -EUSERS; } } @@ -1270,8 +1272,12 @@ int tcp_md5_key_copy(struct sock *sk, const union tc= p_md5_addr *addr, struct tcp_sock *tp =3D tcp_sk(sk); =20 if (!rcu_dereference_protected(tp->md5sig_info, lockdep_sock_is_held(sk))= ) { - if (tcp_md5sig_info_add(sk, sk_gfp_mask(sk, GFP_ATOMIC))) + tcp_md5_add_sigpool(); + + if (tcp_md5sig_info_add(sk, sk_gfp_mask(sk, GFP_ATOMIC))) { + tcp_md5_release_sigpool(); return -ENOMEM; + } =20 if (!static_key_fast_inc_not_disabled(&tcp_md5_needed.key.key)) { struct tcp_md5sig_info *md5sig; @@ -1280,6 +1286,7 @@ int tcp_md5_key_copy(struct sock *sk, const union tcp= _md5_addr *addr, net_warn_ratelimited("Too many TCP-MD5 keys in the system\n"); rcu_assign_pointer(tp->md5sig_info, NULL); kfree_rcu(md5sig, rcu); + tcp_md5_release_sigpool(); return -EUSERS; } } @@ -1379,7 +1386,7 @@ static int tcp_v4_parse_md5_keys(struct sock *sk, int= optname, cmd.tcpm_key, cmd.tcpm_keylen); } =20 -static int tcp_v4_md5_hash_headers(struct tcp_md5sig_pool *hp, +static int tcp_v4_md5_hash_headers(struct tcp_sigpool *hp, __be32 daddr, __be32 saddr, const struct tcphdr *th, int nbytes) { @@ -1399,38 +1406,35 @@ static int tcp_v4_md5_hash_headers(struct tcp_md5si= g_pool *hp, _th->check =3D 0; =20 sg_init_one(&sg, bp, sizeof(*bp) + sizeof(*th)); - ahash_request_set_crypt(hp->md5_req, &sg, NULL, + ahash_request_set_crypt(hp->req, &sg, NULL, sizeof(*bp) + sizeof(*th)); - return crypto_ahash_update(hp->md5_req); + return crypto_ahash_update(hp->req); } =20 static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key= *key, __be32 daddr, __be32 saddr, const struct tcphdr *th) { - struct tcp_md5sig_pool *hp; - struct ahash_request *req; + struct tcp_sigpool hp; =20 - hp =3D tcp_get_md5sig_pool(); - if (!hp) - goto clear_hash_noput; - req =3D hp->md5_req; + if (tcp_sigpool_start(tcp_md5_sigpool_id, &hp)) + goto clear_hash_nostart; =20 - if (crypto_ahash_init(req)) + if (crypto_ahash_init(hp.req)) goto clear_hash; - if (tcp_v4_md5_hash_headers(hp, daddr, saddr, th, th->doff << 2)) + if (tcp_v4_md5_hash_headers(&hp, daddr, saddr, th, th->doff << 2)) goto clear_hash; - if (tcp_md5_hash_key(hp, key)) + if (tcp_md5_hash_key(&hp, key)) goto clear_hash; - ahash_request_set_crypt(req, NULL, md5_hash, 0); - if (crypto_ahash_final(req)) + ahash_request_set_crypt(hp.req, NULL, md5_hash, 0); + if (crypto_ahash_final(hp.req)) goto clear_hash; =20 - tcp_put_md5sig_pool(); + tcp_sigpool_end(&hp); return 0; =20 clear_hash: - tcp_put_md5sig_pool(); -clear_hash_noput: + tcp_sigpool_end(&hp); +clear_hash_nostart: memset(md5_hash, 0, 16); return 1; } @@ -1439,9 +1443,8 @@ int tcp_v4_md5_hash_skb(char *md5_hash, const struct = tcp_md5sig_key *key, const struct sock *sk, const struct sk_buff *skb) { - struct tcp_md5sig_pool *hp; - struct ahash_request *req; const struct tcphdr *th =3D tcp_hdr(skb); + struct tcp_sigpool hp; __be32 saddr, daddr; =20 if (sk) { /* valid for establish/request sockets */ @@ -1453,30 +1456,28 @@ int tcp_v4_md5_hash_skb(char *md5_hash, const struc= t tcp_md5sig_key *key, daddr =3D iph->daddr; } =20 - hp =3D tcp_get_md5sig_pool(); - if (!hp) - goto clear_hash_noput; - req =3D hp->md5_req; + if (tcp_sigpool_start(tcp_md5_sigpool_id, &hp)) + goto clear_hash_nostart; =20 - if (crypto_ahash_init(req)) + if (crypto_ahash_init(hp.req)) goto clear_hash; =20 - if (tcp_v4_md5_hash_headers(hp, daddr, saddr, th, skb->len)) + if (tcp_v4_md5_hash_headers(&hp, daddr, saddr, th, skb->len)) goto clear_hash; - if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2)) + if (tcp_sigpool_hash_skb_data(&hp, skb, th->doff << 2)) goto clear_hash; - if (tcp_md5_hash_key(hp, key)) + if (tcp_md5_hash_key(&hp, key)) goto clear_hash; - ahash_request_set_crypt(req, NULL, md5_hash, 0); - if (crypto_ahash_final(req)) + ahash_request_set_crypt(hp.req, NULL, md5_hash, 0); + if (crypto_ahash_final(hp.req)) goto clear_hash; =20 - tcp_put_md5sig_pool(); + tcp_sigpool_end(&hp); return 0; =20 clear_hash: - tcp_put_md5sig_pool(); -clear_hash_noput: + tcp_sigpool_end(&hp); +clear_hash_nostart: memset(md5_hash, 0, 16); return 1; } @@ -2294,6 +2295,18 @@ static int tcp_v4_init_sock(struct sock *sk) return 0; } =20 +#ifdef CONFIG_TCP_MD5SIG +static void tcp_md5sig_info_free_rcu(struct rcu_head *head) +{ + struct tcp_md5sig_info *md5sig; + + md5sig =3D container_of(head, struct tcp_md5sig_info, rcu); + kfree(md5sig); + static_branch_slow_dec_deferred(&tcp_md5_needed); + tcp_md5_release_sigpool(); +} +#endif + void tcp_v4_destroy_sock(struct sock *sk) { struct tcp_sock *tp =3D tcp_sk(sk); @@ -2318,10 +2331,12 @@ void tcp_v4_destroy_sock(struct sock *sk) #ifdef CONFIG_TCP_MD5SIG /* Clean up the MD5 key list, if any */ if (tp->md5sig_info) { + struct tcp_md5sig_info *md5sig; + + md5sig =3D rcu_dereference_protected(tp->md5sig_info, 1); tcp_clear_md5_list(sk); - kfree_rcu(rcu_dereference_protected(tp->md5sig_info, 1), rcu); - tp->md5sig_info =3D NULL; - static_branch_slow_dec_deferred(&tcp_md5_needed); + call_rcu(&md5sig->rcu, tcp_md5sig_info_free_rcu); + rcu_assign_pointer(tp->md5sig_info, NULL); } #endif =20 diff --git a/net/ipv4/tcp_minisocks.c b/net/ipv4/tcp_minisocks.c index 13ee12983c42..2e6c94133746 100644 --- a/net/ipv4/tcp_minisocks.c +++ b/net/ipv4/tcp_minisocks.c @@ -261,10 +261,9 @@ static void tcp_time_wait_init(struct sock *sk, struct= tcp_timewait_sock *tcptw) tcptw->tw_md5_key =3D kmemdup(key, sizeof(*key), GFP_ATOMIC); if (!tcptw->tw_md5_key) return; - if (!tcp_alloc_md5sig_pool()) - goto out_free; if (!static_key_fast_inc_not_disabled(&tcp_md5_needed.key.key)) goto out_free; + tcp_md5_add_sigpool(); } return; out_free: @@ -349,16 +348,26 @@ void tcp_time_wait(struct sock *sk, int state, int ti= meo) } EXPORT_SYMBOL(tcp_time_wait); =20 +#ifdef CONFIG_TCP_MD5SIG +static void tcp_md5_twsk_free_rcu(struct rcu_head *head) +{ + struct tcp_md5sig_key *key; + + key =3D container_of(head, struct tcp_md5sig_key, rcu); + kfree(key); + static_branch_slow_dec_deferred(&tcp_md5_needed); + tcp_md5_release_sigpool(); +} +#endif + void tcp_twsk_destructor(struct sock *sk) { #ifdef CONFIG_TCP_MD5SIG if (static_branch_unlikely(&tcp_md5_needed.key)) { struct tcp_timewait_sock *twsk =3D tcp_twsk(sk); =20 - if (twsk->tw_md5_key) { - kfree_rcu(twsk->tw_md5_key, rcu); - static_branch_slow_dec_deferred(&tcp_md5_needed); - } + if (twsk->tw_md5_key) + call_rcu(&twsk->tw_md5_key->rcu, tcp_md5_twsk_free_rcu); } #endif } diff --git a/net/ipv4/tcp_sigpool.c b/net/ipv4/tcp_sigpool.c new file mode 100644 index 000000000000..65a8eaae2fec --- /dev/null +++ b/net/ipv4/tcp_sigpool.c @@ -0,0 +1,358 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include +#include +#include +#include +#include +#include + +static size_t __scratch_size; +static DEFINE_PER_CPU(void __rcu *, sigpool_scratch); + +struct sigpool_entry { + struct crypto_ahash *hash; + const char *alg; + struct kref kref; + uint16_t needs_key:1, + reserved:15; +}; + +#define CPOOL_SIZE (PAGE_SIZE / sizeof(struct sigpool_entry)) +static struct sigpool_entry cpool[CPOOL_SIZE]; +static unsigned int cpool_populated; +static DEFINE_MUTEX(cpool_mutex); + +/* Slow-path */ +struct scratches_to_free { + struct rcu_head rcu; + unsigned int cnt; + void *scratches[]; +}; + +static void free_old_scratches(struct rcu_head *head) +{ + struct scratches_to_free *stf; + + stf =3D container_of(head, struct scratches_to_free, rcu); + while (stf->cnt--) + kfree(stf->scratches[stf->cnt]); + kfree(stf); +} + +/** + * sigpool_reserve_scratch - re-allocates scratch buffer, slow-path + * @size: request size for the scratch/temp buffer + */ +static int sigpool_reserve_scratch(size_t size) +{ + struct scratches_to_free *stf; + size_t stf_sz =3D struct_size(stf, scratches, num_possible_cpus()); + int cpu, err =3D 0; + + lockdep_assert_held(&cpool_mutex); + if (__scratch_size >=3D size) + return 0; + + stf =3D kmalloc(stf_sz, GFP_KERNEL); + if (!stf) + return -ENOMEM; + stf->cnt =3D 0; + + size =3D max(size, __scratch_size); + cpus_read_lock(); + for_each_possible_cpu(cpu) { + void *scratch, *old_scratch; + + scratch =3D kmalloc_node(size, GFP_KERNEL, cpu_to_node(cpu)); + if (!scratch) { + err =3D -ENOMEM; + break; + } + + old_scratch =3D rcu_replace_pointer(per_cpu(sigpool_scratch, cpu), + scratch, lockdep_is_held(&cpool_mutex)); + if (!cpu_online(cpu) || !old_scratch) { + kfree(old_scratch); + continue; + } + stf->scratches[stf->cnt++] =3D old_scratch; + } + cpus_read_unlock(); + if (!err) + __scratch_size =3D size; + + call_rcu(&stf->rcu, free_old_scratches); + return err; +} + +static void sigpool_scratch_free(void) +{ + int cpu; + + for_each_possible_cpu(cpu) + kfree(rcu_replace_pointer(per_cpu(sigpool_scratch, cpu), + NULL, lockdep_is_held(&cpool_mutex))); + __scratch_size =3D 0; +} + +static int __cpool_try_clone(struct crypto_ahash *hash) +{ + struct crypto_ahash *tmp; + + tmp =3D crypto_clone_ahash(hash); + if (IS_ERR(tmp)) + return PTR_ERR(tmp); + + crypto_free_ahash(tmp); + return 0; +} + +static int __cpool_alloc_ahash(struct sigpool_entry *e, const char *alg) +{ + struct crypto_ahash *cpu0_hash; + int ret; + + e->alg =3D kstrdup(alg, GFP_KERNEL); + if (!e->alg) + return -ENOMEM; + + cpu0_hash =3D crypto_alloc_ahash(alg, 0, CRYPTO_ALG_ASYNC); + if (IS_ERR(cpu0_hash)) { + ret =3D PTR_ERR(cpu0_hash); + goto out_free_alg; + } + + e->needs_key =3D crypto_ahash_get_flags(cpu0_hash) & CRYPTO_TFM_NEED_KEY; + + ret =3D __cpool_try_clone(cpu0_hash); + if (ret) + goto out_free_cpu0_hash; + e->hash =3D cpu0_hash; + kref_init(&e->kref); + return 0; + +out_free_cpu0_hash: + crypto_free_ahash(cpu0_hash); +out_free_alg: + kfree(e->alg); + e->alg =3D NULL; + return ret; +} + +/** + * tcp_sigpool_alloc_ahash - allocates pool for ahash requests + * @alg: name of async hash algorithm + * @scratch_size: reserve a tcp_sigpool::scratch buffer of this size + */ +int tcp_sigpool_alloc_ahash(const char *alg, size_t scratch_size) +{ + int i, ret; + + /* slow-path */ + mutex_lock(&cpool_mutex); + ret =3D sigpool_reserve_scratch(scratch_size); + if (ret) + goto out; + for (i =3D 0; i < cpool_populated; i++) { + if (!cpool[i].alg) + continue; + if (strcmp(cpool[i].alg, alg)) + continue; + + if (kref_read(&cpool[i].kref) > 0) + kref_get(&cpool[i].kref); + else + kref_init(&cpool[i].kref); + ret =3D i; + goto out; + } + + for (i =3D 0; i < cpool_populated; i++) { + if (!cpool[i].alg) + break; + } + if (i >=3D CPOOL_SIZE) { + ret =3D -ENOSPC; + goto out; + } + + ret =3D __cpool_alloc_ahash(&cpool[i], alg); + if (!ret) { + ret =3D i; + if (i =3D=3D cpool_populated) + cpool_populated++; + } +out: + mutex_unlock(&cpool_mutex); + return ret; +} +EXPORT_SYMBOL_GPL(tcp_sigpool_alloc_ahash); + +static void __cpool_free_entry(struct sigpool_entry *e) +{ + crypto_free_ahash(e->hash); + kfree(e->alg); + memset(e, 0, sizeof(*e)); +} + +static void cpool_cleanup_work_cb(struct work_struct *work) +{ + bool free_scratch =3D true; + unsigned int i; + + mutex_lock(&cpool_mutex); + for (i =3D 0; i < cpool_populated; i++) { + if (kref_read(&cpool[i].kref) > 0) { + free_scratch =3D false; + continue; + } + if (!cpool[i].alg) + continue; + __cpool_free_entry(&cpool[i]); + } + if (free_scratch) + sigpool_scratch_free(); + mutex_unlock(&cpool_mutex); +} + +static DECLARE_WORK(cpool_cleanup_work, cpool_cleanup_work_cb); +static void cpool_schedule_cleanup(struct kref *kref) +{ + schedule_work(&cpool_cleanup_work); +} + +/** + * tcp_sigpool_release - decreases number of users for a pool. If it was + * the last user of the pool, releases any memory that was consumed. + * @id: tcp_sigpool that was previously allocated by tcp_sigpool_alloc_aha= sh() + */ +void tcp_sigpool_release(unsigned int id) +{ + if (WARN_ON_ONCE(id > cpool_populated || !cpool[id].alg)) + return; + + /* slow-path */ + kref_put(&cpool[id].kref, cpool_schedule_cleanup); +} +EXPORT_SYMBOL_GPL(tcp_sigpool_release); + +/** + * tcp_sigpool_get - increases number of users (refcounter) for a pool + * @id: tcp_sigpool that was previously allocated by tcp_sigpool_alloc_aha= sh() + */ +void tcp_sigpool_get(unsigned int id) +{ + if (WARN_ON_ONCE(id > cpool_populated || !cpool[id].alg)) + return; + kref_get(&cpool[id].kref); +} +EXPORT_SYMBOL_GPL(tcp_sigpool_get); + +int tcp_sigpool_start(unsigned int id, struct tcp_sigpool *c) __cond_acqui= res(RCU_BH) +{ + struct crypto_ahash *hash; + + rcu_read_lock_bh(); + if (WARN_ON_ONCE(id > cpool_populated || !cpool[id].alg)) { + rcu_read_unlock_bh(); + return -EINVAL; + } + + hash =3D crypto_clone_ahash(cpool[id].hash); + if (IS_ERR(hash)) { + rcu_read_unlock_bh(); + return PTR_ERR(hash); + } + + c->req =3D ahash_request_alloc(hash, GFP_ATOMIC); + if (!c->req) { + crypto_free_ahash(hash); + rcu_read_unlock_bh(); + return -ENOMEM; + } + ahash_request_set_callback(c->req, 0, NULL, NULL); + + /* Pairs with tcp_sigpool_reserve_scratch(), scratch area is + * valid (allocated) until tcp_sigpool_end(). + */ + c->scratch =3D rcu_dereference_bh(*this_cpu_ptr(&sigpool_scratch)); + return 0; +} +EXPORT_SYMBOL_GPL(tcp_sigpool_start); + +void tcp_sigpool_end(struct tcp_sigpool *c) __releases(RCU_BH) +{ + struct crypto_ahash *hash =3D crypto_ahash_reqtfm(c->req); + + rcu_read_unlock_bh(); + ahash_request_free(c->req); + crypto_free_ahash(hash); +} +EXPORT_SYMBOL_GPL(tcp_sigpool_end); + +/** + * tcp_sigpool_algo - return algorithm of tcp_sigpool + * @id: tcp_sigpool that was previously allocated by tcp_sigpool_alloc_aha= sh() + * @buf: buffer to return name of algorithm + * @buf_len: size of @buf + */ +size_t tcp_sigpool_algo(unsigned int id, char *buf, size_t buf_len) +{ + if (WARN_ON_ONCE(id > cpool_populated || !cpool[id].alg)) + return -EINVAL; + + return strscpy(buf, cpool[id].alg, buf_len); +} +EXPORT_SYMBOL_GPL(tcp_sigpool_algo); + +/** + * tcp_sigpool_hash_skb_data - hash data in skb with initialized tcp_sigpo= ol + * @hp: tcp_sigpool pointer + * @skb: buffer to add sign for + * @header_len: TCP header length for this segment + */ +int tcp_sigpool_hash_skb_data(struct tcp_sigpool *hp, + const struct sk_buff *skb, + unsigned int header_len) +{ + const unsigned int head_data_len =3D skb_headlen(skb) > header_len ? + skb_headlen(skb) - header_len : 0; + const struct skb_shared_info *shi =3D skb_shinfo(skb); + const struct tcphdr *tp =3D tcp_hdr(skb); + struct ahash_request *req =3D hp->req; + struct sk_buff *frag_iter; + struct scatterlist sg; + unsigned int i; + + sg_init_table(&sg, 1); + + sg_set_buf(&sg, ((u8 *)tp) + header_len, head_data_len); + ahash_request_set_crypt(req, &sg, NULL, head_data_len); + if (crypto_ahash_update(req)) + return 1; + + for (i =3D 0; i < shi->nr_frags; ++i) { + const skb_frag_t *f =3D &shi->frags[i]; + unsigned int offset =3D skb_frag_off(f); + struct page *page; + + page =3D skb_frag_page(f) + (offset >> PAGE_SHIFT); + sg_set_page(&sg, page, skb_frag_size(f), offset_in_page(offset)); + ahash_request_set_crypt(req, &sg, NULL, skb_frag_size(f)); + if (crypto_ahash_update(req)) + return 1; + } + + skb_walk_frags(skb, frag_iter) + if (tcp_sigpool_hash_skb_data(hp, frag_iter, 0)) + return 1; + + return 0; +} +EXPORT_SYMBOL(tcp_sigpool_hash_skb_data); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Per-CPU pool of crypto requests"); diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 3a88545a265d..5a06bcfd6cd1 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -670,7 +670,7 @@ static int tcp_v6_parse_md5_keys(struct sock *sk, int o= ptname, cmd.tcpm_key, cmd.tcpm_keylen); } =20 -static int tcp_v6_md5_hash_headers(struct tcp_md5sig_pool *hp, +static int tcp_v6_md5_hash_headers(struct tcp_sigpool *hp, const struct in6_addr *daddr, const struct in6_addr *saddr, const struct tcphdr *th, int nbytes) @@ -691,39 +691,36 @@ static int tcp_v6_md5_hash_headers(struct tcp_md5sig_= pool *hp, _th->check =3D 0; =20 sg_init_one(&sg, bp, sizeof(*bp) + sizeof(*th)); - ahash_request_set_crypt(hp->md5_req, &sg, NULL, + ahash_request_set_crypt(hp->req, &sg, NULL, sizeof(*bp) + sizeof(*th)); - return crypto_ahash_update(hp->md5_req); + return crypto_ahash_update(hp->req); } =20 static int tcp_v6_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key= *key, const struct in6_addr *daddr, struct in6_addr *saddr, const struct tcphdr *th) { - struct tcp_md5sig_pool *hp; - struct ahash_request *req; + struct tcp_sigpool hp; =20 - hp =3D tcp_get_md5sig_pool(); - if (!hp) - goto clear_hash_noput; - req =3D hp->md5_req; + if (tcp_sigpool_start(tcp_md5_sigpool_id, &hp)) + goto clear_hash_nostart; =20 - if (crypto_ahash_init(req)) + if (crypto_ahash_init(hp.req)) goto clear_hash; - if (tcp_v6_md5_hash_headers(hp, daddr, saddr, th, th->doff << 2)) + if (tcp_v6_md5_hash_headers(&hp, daddr, saddr, th, th->doff << 2)) goto clear_hash; - if (tcp_md5_hash_key(hp, key)) + if (tcp_md5_hash_key(&hp, key)) goto clear_hash; - ahash_request_set_crypt(req, NULL, md5_hash, 0); - if (crypto_ahash_final(req)) + ahash_request_set_crypt(hp.req, NULL, md5_hash, 0); + if (crypto_ahash_final(hp.req)) goto clear_hash; =20 - tcp_put_md5sig_pool(); + tcp_sigpool_end(&hp); return 0; =20 clear_hash: - tcp_put_md5sig_pool(); -clear_hash_noput: + tcp_sigpool_end(&hp); +clear_hash_nostart: memset(md5_hash, 0, 16); return 1; } @@ -733,10 +730,9 @@ static int tcp_v6_md5_hash_skb(char *md5_hash, const struct sock *sk, const struct sk_buff *skb) { - const struct in6_addr *saddr, *daddr; - struct tcp_md5sig_pool *hp; - struct ahash_request *req; const struct tcphdr *th =3D tcp_hdr(skb); + const struct in6_addr *saddr, *daddr; + struct tcp_sigpool hp; =20 if (sk) { /* valid for establish/request sockets */ saddr =3D &sk->sk_v6_rcv_saddr; @@ -747,30 +743,28 @@ static int tcp_v6_md5_hash_skb(char *md5_hash, daddr =3D &ip6h->daddr; } =20 - hp =3D tcp_get_md5sig_pool(); - if (!hp) - goto clear_hash_noput; - req =3D hp->md5_req; + if (tcp_sigpool_start(tcp_md5_sigpool_id, &hp)) + goto clear_hash_nostart; =20 - if (crypto_ahash_init(req)) + if (crypto_ahash_init(hp.req)) goto clear_hash; =20 - if (tcp_v6_md5_hash_headers(hp, daddr, saddr, th, skb->len)) + if (tcp_v6_md5_hash_headers(&hp, daddr, saddr, th, skb->len)) goto clear_hash; - if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2)) + if (tcp_sigpool_hash_skb_data(&hp, skb, th->doff << 2)) goto clear_hash; - if (tcp_md5_hash_key(hp, key)) + if (tcp_md5_hash_key(&hp, key)) goto clear_hash; - ahash_request_set_crypt(req, NULL, md5_hash, 0); - if (crypto_ahash_final(req)) + ahash_request_set_crypt(hp.req, NULL, md5_hash, 0); + if (crypto_ahash_final(hp.req)) goto clear_hash; =20 - tcp_put_md5sig_pool(); + tcp_sigpool_end(&hp); return 0; =20 clear_hash: - tcp_put_md5sig_pool(); -clear_hash_noput: + tcp_sigpool_end(&hp); +clear_hash_nostart: memset(md5_hash, 0, 16); return 1; } --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55A91C04A6A for ; Tue, 15 Aug 2023 19:15:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239821AbjHOTPV (ORCPT ); Tue, 15 Aug 2023 15:15:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239792AbjHOTPJ (ORCPT ); Tue, 15 Aug 2023 15:15:09 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2CF1DE7A for ; Tue, 15 Aug 2023 12:15:08 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-3fe45481edfso58171785e9.1 for ; Tue, 15 Aug 2023 12:15:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126906; x=1692731706; 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=++mOHR9kyok4cfnjD2Hl5YBH3xAQRNzAiCB83liHnRU=; b=IG/iDR5YWtTUj60DcNvVwoiYx0KiUDHQuQn+dSA8PIE7k2UXCOo6COy93udjxmCG1i 1aitmhUnqZISF5P672/xDMjdS30S2JwsLtvH2NrV/IV2U3qGaOsIw27AOruT1nIrXUiA reBdGwJC94FaBGywRZv0d7dAH1CVleSy8/+LAF8iUmBgihdCetE6XwjCob+EqpFLMBYU XuaHPMlOaMZsNdHiqsKXVkOooPEaeXU7m1gML9YczH9LXizj+MMdG48DS8XEiQvUZZkg c2c/D8DhRZ4cNfJQIrhIK1PbwdOKkl0Bi3f9SjH7s8QTzpI25MMwg+eQggsWonpw9XZE uK5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126906; x=1692731706; 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=++mOHR9kyok4cfnjD2Hl5YBH3xAQRNzAiCB83liHnRU=; b=ect9cM5GHna4ftEWguphI95rAabJOPIj1uGdwryY3AF72JiW2Ii7FCuv91z6fapEnt zcJsFeShF2fZlggFhn3oDZ37uPLj1OHsFOlN2FIdiT+V2X+2QIB0iTbXkbo7IdtdaM4Y Un50RgPr0EWYjBQ4I3WqzGV5TzRJcyRMZR+6yctMHQoo9TNbprORopkmjlA8AfYtwDdq jc8NqbQEqzzqtHQQ4nbnwXfDeyPt0kOCzaCk9FsTswiZF/K4C7t8CmrKMmkME+1PrtvQ avmFymTzMiR2RCB1qERHDqRQYCM6kWgfHUUfQ8VaggQa/BVEwLpV/LkD0LtSz+giwSH2 0sxA== X-Gm-Message-State: AOJu0YwRf4QwxFbg0fBFtrULt1Ngb3ir3MMUGhlm/5U3saiJrdFXQi5Y DceZgMZRGMG6aCEUfUjjbOCTOw== X-Google-Smtp-Source: AGHT+IHdaW30upwe4AHIRGqUlj1FDEfsrOHRRSWpPaeXXTjW0W/+DHo5ojlwI70c7ObYfTxjeIZ3eQ== X-Received: by 2002:a05:600c:c7:b0:3fe:1cac:37d5 with SMTP id u7-20020a05600c00c700b003fe1cac37d5mr10899701wmm.4.1692126906630; Tue, 15 Aug 2023 12:15:06 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:06 -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 v10 net-next 02/23] net/tcp: Add TCP-AO config and structures Date: Tue, 15 Aug 2023 20:14:31 +0100 Message-ID: <20230815191455.1872316-3-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Introduce new kernel config option and common structures as well as helpers to be used by TCP-AO code. 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 Acked-by: David Ahern --- include/linux/tcp.h | 9 +++- include/net/tcp.h | 8 +--- include/net/tcp_ao.h | 90 ++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/tcp.h | 2 + net/ipv4/Kconfig | 13 ++++++ 5 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 include/net/tcp_ao.h diff --git a/include/linux/tcp.h b/include/linux/tcp.h index 3c5efeeb024f..fc98c7d63360 100644 --- a/include/linux/tcp.h +++ b/include/linux/tcp.h @@ -437,13 +437,18 @@ struct tcp_sock { bool syn_smc; /* SYN includes SMC */ #endif =20 -#ifdef CONFIG_TCP_MD5SIG -/* TCP AF-Specific parts; only used by MD5 Signature support so far */ +#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) +/* TCP AF-Specific parts; only used by TCP-AO/MD5 Signature support so far= */ const struct tcp_sock_af_ops *af_specific; =20 +#ifdef CONFIG_TCP_MD5SIG /* TCP MD5 Signature Option information */ struct tcp_md5sig_info __rcu *md5sig_info; #endif +#ifdef CONFIG_TCP_AO + struct tcp_ao_info __rcu *ao_info; +#endif +#endif =20 /* TCP fastopen related information */ struct tcp_fastopen_request *fastopen_req; diff --git a/include/net/tcp.h b/include/net/tcp.h index 506138e69643..9ea8fa4406de 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -1651,12 +1652,7 @@ static inline void tcp_clear_all_retrans_hints(struc= t tcp_sock *tp) tp->retransmit_skb_hint =3D NULL; } =20 -union tcp_md5_addr { - struct in_addr a4; -#if IS_ENABLED(CONFIG_IPV6) - struct in6_addr a6; -#endif -}; +#define tcp_md5_addr tcp_ao_addr =20 /* - key database */ struct tcp_md5sig_key { diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h new file mode 100644 index 000000000000..af76e1c47bea --- /dev/null +++ b/include/net/tcp_ao.h @@ -0,0 +1,90 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#ifndef _TCP_AO_H +#define _TCP_AO_H + +#define TCP_AO_KEY_ALIGN 1 +#define __tcp_ao_key_align __aligned(TCP_AO_KEY_ALIGN) + +union tcp_ao_addr { + struct in_addr a4; +#if IS_ENABLED(CONFIG_IPV6) + struct in6_addr a6; +#endif +}; + +struct tcp_ao_hdr { + u8 kind; + u8 length; + u8 keyid; + u8 rnext_keyid; +}; + +struct tcp_ao_key { + struct hlist_node node; + union tcp_ao_addr addr; + u8 key[TCP_AO_MAXKEYLEN] __tcp_ao_key_align; + unsigned int tcp_sigpool_id; + unsigned int digest_size; + u8 prefixlen; + u8 family; + u8 keylen; + u8 keyflags; + u8 sndid; + u8 rcvid; + u8 maclen; + struct rcu_head rcu; + u8 traffic_keys[]; +}; + +static inline u8 *rcv_other_key(struct tcp_ao_key *key) +{ + return key->traffic_keys; +} + +static inline u8 *snd_other_key(struct tcp_ao_key *key) +{ + return key->traffic_keys + key->digest_size; +} + +static inline int tcp_ao_maclen(const struct tcp_ao_key *key) +{ + return key->maclen; +} + +static inline int tcp_ao_len(const struct tcp_ao_key *key) +{ + return tcp_ao_maclen(key) + sizeof(struct tcp_ao_hdr); +} + +static inline unsigned int tcp_ao_digest_size(struct tcp_ao_key *key) +{ + return key->digest_size; +} + +static inline int tcp_ao_sizeof_key(const struct tcp_ao_key *key) +{ + return sizeof(struct tcp_ao_key) + (key->digest_size << 1); +} + +struct tcp_ao_info { + /* List of tcp_ao_key's */ + struct hlist_head head; + /* current_key and rnext_key aren't maintained on listen sockets. + * Their purpose is to cache keys on established connections, + * saving needless lookups. Never dereference any of them from + * listen sockets. + * ::current_key may change in RX to the key that was requested by + * the peer, please use READ_ONCE()/WRITE_ONCE() in order to avoid + * load/store tearing. + * Do the same for ::rnext_key, if you don't hold socket lock + * (it's changed only by userspace request in setsockopt()). + */ + struct tcp_ao_key *current_key; + struct tcp_ao_key *rnext_key; + u32 flags; + __be32 lisn; + __be32 risn; + struct rcu_head rcu; +}; + +#endif /* _TCP_AO_H */ diff --git a/include/uapi/linux/tcp.h b/include/uapi/linux/tcp.h index 879eeb0a084b..5655bfe28b8d 100644 --- a/include/uapi/linux/tcp.h +++ b/include/uapi/linux/tcp.h @@ -348,6 +348,8 @@ struct tcp_diag_md5sig { __u8 tcpm_key[TCP_MD5SIG_MAXKEYLEN]; }; =20 +#define TCP_AO_MAXKEYLEN 80 + /* setsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, ...) */ =20 #define TCP_RECEIVE_ZEROCOPY_FLAG_TLB_CLEAN_HINT 0x1 diff --git a/net/ipv4/Kconfig b/net/ipv4/Kconfig index 89e2ab023272..8e94ed7c56a0 100644 --- a/net/ipv4/Kconfig +++ b/net/ipv4/Kconfig @@ -744,6 +744,19 @@ config DEFAULT_TCP_CONG config TCP_SIGPOOL tristate =20 +config TCP_AO + bool "TCP: Authentication Option (RFC5925)" + select CRYPTO + select TCP_SIGPOOL + depends on 64BIT && IPV6 !=3D m # seq-number extension needs WRITE_ONCE(u= 64) + help + TCP-AO specifies the use of stronger Message Authentication Codes (MACs= ), + protects against replays for long-lived TCP connections, and + provides more details on the association of security with TCP + connections than TCP MD5 (See RFC5925) + + If unsure, say N. + config TCP_MD5SIG bool "TCP: MD5 Signature Option support (RFC2385)" select CRYPTO --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 75F4AC04E69 for ; Tue, 15 Aug 2023 19:15:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239862AbjHOTPX (ORCPT ); Tue, 15 Aug 2023 15:15:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239843AbjHOTPO (ORCPT ); Tue, 15 Aug 2023 15:15:14 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3530BD1 for ; Tue, 15 Aug 2023 12:15:10 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-3fe2d218eedso54102605e9.0 for ; Tue, 15 Aug 2023 12:15:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126908; x=1692731708; 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=xv0VrkedrSk1tGUn9kvjkqMBFRftlG/hPlIiRNEmeRU=; b=TdpMHIILMZt/VlSUbZP3TbqnbKLohkhzEs7XRsaCfE0wQvXPxuCHiQpkWpll8a9Xae +1chPnyMpDORrDv1wIFG5DK8wOuCGp3rLi+261Fl11YMlhIwQx32PYbj5RUJ7EYJupXl fFNQgwG4UErxMP9b6x15B+4d9PwWH5np/9U7opIgMGCGrnxY+LponQElNa1iNrPNtKGt crbTGmjtLPUDuP4olORbhH9FrCyCtXyJebpimG6b/4l0Qas46hgvsoyeZb+w6mF90u4B UIez7hOXchRVe22QDa86Myp3BrAhVXgH9uYMhpsf5aP6Ob6jrfBOfgHGnpxiD2TuF295 wfTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126908; x=1692731708; 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=xv0VrkedrSk1tGUn9kvjkqMBFRftlG/hPlIiRNEmeRU=; b=BCZLFb0Pkjru7qQlichOtuOmqmK6XYXS0VuE+qCx3UbRCd2KsVkVzvYnGznNSsWTQ0 zWOvQJo9ADM2Yjc95e5z+buo/LG0O3wqwyZh7hewsTmsj5We6cIf05Yrv8eN+PvaxCE1 6DFLKbR9+08eMZ/HmaFahGpG9h2x1RlL5vr76UvaCjTazmftMTxv0OP7VcKiuGoKyJr9 SrJ3jRVwY7qLIsiqPXCmrIckKf5PN2qharbpYkUjSxB/rx/ELRC5ETnIGgvxll4CdML1 DFfBLtl01Ibr0Xl4jdck9s0iJqNVR3odCbt93MgF59oATPl9nXHSUGpUYKBQ90T+93XR v17A== X-Gm-Message-State: AOJu0Yxiack6TDaSFdS6erSaqgS4f45H/c0FaUolQfJTBd63swUso6m9 LJclg5OP7ZgEyqfXMPQKtncmyA== X-Google-Smtp-Source: AGHT+IH1JNSl2oU0GHS6F+1VGt9MGAA/1rUOTXeQjdV819ck0xsQiUtKis7/+p19ccZJV8h8Ye+8FA== X-Received: by 2002:a05:600c:ad2:b0:3fe:1923:2c3 with SMTP id c18-20020a05600c0ad200b003fe192302c3mr9864842wmr.30.1692126908472; Tue, 15 Aug 2023 12:15:08 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:07 -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 v10 net-next 03/23] net/tcp: Introduce TCP_AO setsockopt()s Date: Tue, 15 Aug 2023 20:14:32 +0100 Message-ID: <20230815191455.1872316-4-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add 3 setsockopt()s: 1. TCP_AO_ADD_KEY to add a new Master Key Tuple (MKT) on a socket 2. TCP_AO_DEL_KEY to delete present MKT from a socket 3. TCP_AO_INFO to change flags, Current_key/RNext_key on a TCP-AO sk Userspace has to introduce keys on every socket it wants to use TCP-AO option on, similarly to TCP_MD5SIG/TCP_MD5SIG_EXT. RFC5925 prohibits definition of MKTs that would match the same peer, so do sanity checks on the data provided by userspace. Be as conservative as possible, including refusal of defining MKT on an established connection with no AO, removing the key in-use and etc. (1) and (2) are to be used by userspace key manager to add/remove keys. (3) main purpose is to set RNext_key, which (as prescribed by RFC5925) is the KeyID that will be requested in TCP-AO header from the peer to sign their segments with. At this moment the life of ao_info ends in tcp_v4_destroy_sock(). 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 Acked-by: David Ahern --- include/linux/sockptr.h | 23 ++ include/net/tcp.h | 3 + include/net/tcp_ao.h | 17 +- include/uapi/linux/tcp.h | 46 +++ net/ipv4/Makefile | 1 + net/ipv4/tcp.c | 17 + net/ipv4/tcp_ao.c | 794 +++++++++++++++++++++++++++++++++++++++ net/ipv4/tcp_ipv4.c | 10 +- net/ipv6/Makefile | 1 + net/ipv6/tcp_ao.c | 19 + net/ipv6/tcp_ipv6.c | 39 +- 11 files changed, 952 insertions(+), 18 deletions(-) create mode 100644 net/ipv4/tcp_ao.c create mode 100644 net/ipv6/tcp_ao.c diff --git a/include/linux/sockptr.h b/include/linux/sockptr.h index bae5e2369b4f..307961b41541 100644 --- a/include/linux/sockptr.h +++ b/include/linux/sockptr.h @@ -55,6 +55,29 @@ static inline int copy_from_sockptr(void *dst, sockptr_t= src, size_t size) return copy_from_sockptr_offset(dst, src, 0, size); } =20 +static inline int copy_struct_from_sockptr(void *dst, size_t ksize, + sockptr_t src, size_t usize) +{ + size_t size =3D min(ksize, usize); + size_t rest =3D max(ksize, usize) - size; + + if (!sockptr_is_kernel(src)) + return copy_struct_from_user(dst, ksize, src.user, size); + + if (usize < ksize) { + memset(dst + size, 0, rest); + } else if (usize > ksize) { + char *p =3D src.kernel; + + while (rest--) { + if (*p++) + return -E2BIG; + } + } + memcpy(dst, src.kernel, size); + return 0; +} + static inline int copy_to_sockptr_offset(sockptr_t dst, size_t offset, const void *src, size_t size) { diff --git a/include/net/tcp.h b/include/net/tcp.h index 9ea8fa4406de..6161e6bde100 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -2134,6 +2134,9 @@ struct tcp_sock_af_ops { sockptr_t optval, int optlen); #endif +#ifdef CONFIG_TCP_AO + int (*ao_parse)(struct sock *sk, int optname, sockptr_t optval, int optle= n); +#endif }; =20 struct tcp_request_sock_ops { diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index af76e1c47bea..a81e40fd255a 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -81,10 +81,25 @@ struct tcp_ao_info { */ struct tcp_ao_key *current_key; struct tcp_ao_key *rnext_key; - u32 flags; + u32 ao_required :1, + __unused :31; __be32 lisn; __be32 risn; struct rcu_head rcu; }; =20 +#ifdef CONFIG_TCP_AO +int tcp_parse_ao(struct sock *sk, int cmd, unsigned short int family, + sockptr_t optval, int optlen); +void tcp_ao_destroy_sock(struct sock *sk); +/* ipv4 specific functions */ +int tcp_v4_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen= ); +/* ipv6 specific functions */ +int tcp_v6_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen= ); +#else +static inline void tcp_ao_destroy_sock(struct sock *sk) +{ +} +#endif + #endif /* _TCP_AO_H */ diff --git a/include/uapi/linux/tcp.h b/include/uapi/linux/tcp.h index 5655bfe28b8d..250e0ce2cc38 100644 --- a/include/uapi/linux/tcp.h +++ b/include/uapi/linux/tcp.h @@ -129,6 +129,9 @@ enum { =20 #define TCP_TX_DELAY 37 /* delay outgoing packets by XX usec */ =20 +#define TCP_AO_ADD_KEY 38 /* Add/Set MKT */ +#define TCP_AO_DEL_KEY 39 /* Delete MKT */ +#define TCP_AO_INFO 40 /* Modify TCP-AO per-socket options */ =20 #define TCP_REPAIR_ON 1 #define TCP_REPAIR_OFF 0 @@ -350,6 +353,49 @@ struct tcp_diag_md5sig { =20 #define TCP_AO_MAXKEYLEN 80 =20 +#define TCP_AO_KEYF_IFINDEX (1 << 0) /* L3 ifindex for VRF */ + +struct tcp_ao_add { /* setsockopt(TCP_AO_ADD_KEY) */ + struct __kernel_sockaddr_storage addr; /* peer's address for the key */ + char alg_name[64]; /* crypto hash algorithm to use */ + __s32 ifindex; /* L3 dev index for VRF */ + __u32 set_current :1, /* set key as Current_key at once */ + set_rnext :1, /* request it from peer with RNext_key */ + reserved :30; /* must be 0 */ + __u16 reserved2; /* padding, must be 0 */ + __u8 prefix; /* peer's address prefix */ + __u8 sndid; /* SendID for outgoing segments */ + __u8 rcvid; /* RecvID to match for incoming seg */ + __u8 maclen; /* length of authentication code (hash) */ + __u8 keyflags; /* see TCP_AO_KEYF_ */ + __u8 keylen; /* length of ::key */ + __u8 key[TCP_AO_MAXKEYLEN]; +} __attribute__((aligned(8))); + +struct tcp_ao_del { /* setsockopt(TCP_AO_DEL_KEY) */ + struct __kernel_sockaddr_storage addr; /* peer's address for the key */ + __s32 ifindex; /* L3 dev index for VRF */ + __u32 set_current :1, /* corresponding ::current_key */ + set_rnext :1, /* corresponding ::rnext */ + reserved :30; /* must be 0 */ + __u16 reserved2; /* padding, must be 0 */ + __u8 prefix; /* peer's address prefix */ + __u8 sndid; /* SendID for outgoing segments */ + __u8 rcvid; /* RecvID to match for incoming seg */ + __u8 current_key; /* KeyID to set as Current_key */ + __u8 rnext; /* KeyID to set as Rnext_key */ + __u8 keyflags; /* see TCP_AO_KEYF_ */ +} __attribute__((aligned(8))); + +struct tcp_ao_info_opt { /* setsockopt(TCP_AO_INFO) */ + __u32 set_current :1, /* corresponding ::current_key */ + set_rnext :1, /* corresponding ::rnext */ + ao_required :1, /* don't accept non-AO connects */ + reserved :29; /* must be 0 */ + __u8 current_key; /* KeyID to set as Current_key */ + __u8 rnext; /* KeyID to set as Rnext_key */ +} __attribute__((aligned(8))); + /* setsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, ...) */ =20 #define TCP_RECEIVE_ZEROCOPY_FLAG_TLB_CLEAN_HINT 0x1 diff --git a/net/ipv4/Makefile b/net/ipv4/Makefile index cd760793cfcb..e144a02a6a61 100644 --- a/net/ipv4/Makefile +++ b/net/ipv4/Makefile @@ -69,6 +69,7 @@ obj-$(CONFIG_NETLABEL) +=3D cipso_ipv4.o =20 obj-$(CONFIG_XFRM) +=3D xfrm4_policy.o xfrm4_state.o xfrm4_input.o \ xfrm4_output.o xfrm4_protocol.o +obj-$(CONFIG_TCP_AO) +=3D tcp_ao.o =20 ifeq ($(CONFIG_BPF_JIT),y) obj-$(CONFIG_BPF_SYSCALL) +=3D bpf_tcp_ca.o diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index c6b0113ccc79..4d0536ff75d8 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -3598,6 +3598,23 @@ int do_tcp_setsockopt(struct sock *sk, int level, in= t optname, __tcp_sock_set_quickack(sk, val); break; =20 +#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 + */ + if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) || + rcu_dereference_protected(tcp_sk(sk)->ao_info, + lockdep_sock_is_held(sk))) + err =3D tp->af_specific->ao_parse(sk, optname, optval, + optlen); + else + err =3D -EISCONN; + break; + } +#endif #ifdef CONFIG_TCP_MD5SIG case TCP_MD5SIG: case TCP_MD5SIG_EXT: diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c new file mode 100644 index 000000000000..9121f1eeb224 --- /dev/null +++ b/net/ipv4/tcp_ao.c @@ -0,0 +1,794 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * INET An implementation of the TCP Authentication Option (TCP-AO). + * See RFC5925. + * + * Authors: Dmitry Safonov + * Francesco Ruggeri + * Salam Noureddine + */ +#define pr_fmt(fmt) "TCP: " fmt + +#include +#include +#include + +#include +#include + +/* Optimized version of tcp_ao_do_lookup(): only for sockets for which + * it's known that the keys in ao_info are matching peer's + * family/address/VRF/etc. + */ +static struct tcp_ao_key *tcp_ao_established_key(struct tcp_ao_info *ao, + int sndid, int rcvid) +{ + struct tcp_ao_key *key; + + hlist_for_each_entry_rcu(key, &ao->head, node) { + if ((sndid >=3D 0 && key->sndid !=3D sndid) || + (rcvid >=3D 0 && key->rcvid !=3D rcvid)) + continue; + return key; + } + + return NULL; +} + +static int ipv4_prefix_cmp(const struct in_addr *addr1, + const struct in_addr *addr2, + unsigned int prefixlen) +{ + __be32 mask =3D inet_make_mask(prefixlen); + __be32 a1 =3D addr1->s_addr & mask; + __be32 a2 =3D addr2->s_addr & mask; + + if (a1 =3D=3D a2) + return 0; + return memcmp(&a1, &a2, sizeof(a1)); +} + +static int __tcp_ao_key_cmp(const struct tcp_ao_key *key, + const union tcp_ao_addr *addr, u8 prefixlen, + int family, int sndid, int rcvid) +{ + if (sndid >=3D 0 && key->sndid !=3D sndid) + return (key->sndid > sndid) ? 1 : -1; + if (rcvid >=3D 0 && key->rcvid !=3D rcvid) + return (key->rcvid > rcvid) ? 1 : -1; + + if (family =3D=3D AF_UNSPEC) + return 0; + if (key->family !=3D family) + return (key->family > family) ? 1 : -1; + + if (family =3D=3D AF_INET) { + if (ntohl(key->addr.a4.s_addr) =3D=3D INADDR_ANY) + return 0; + if (ntohl(addr->a4.s_addr) =3D=3D INADDR_ANY) + return 0; + return ipv4_prefix_cmp(&key->addr.a4, &addr->a4, prefixlen); +#if IS_ENABLED(CONFIG_IPV6) + } else { + if (ipv6_addr_any(&key->addr.a6) || ipv6_addr_any(&addr->a6)) + return 0; + if (ipv6_prefix_equal(&key->addr.a6, &addr->a6, prefixlen)) + return 0; + return memcmp(&key->addr.a6, &addr->a6, sizeof(addr->a6)); +#endif + } + return -1; +} + +static int tcp_ao_key_cmp(const struct tcp_ao_key *key, + const union tcp_ao_addr *addr, u8 prefixlen, + int family, int sndid, int rcvid) +{ +#if IS_ENABLED(CONFIG_IPV6) + if (family =3D=3D AF_INET6 && ipv6_addr_v4mapped(&addr->a6)) { + __be32 addr4 =3D addr->a6.s6_addr32[3]; + + return __tcp_ao_key_cmp(key, (union tcp_ao_addr *)&addr4, + prefixlen, AF_INET, sndid, rcvid); + } +#endif + return __tcp_ao_key_cmp(key, addr, prefixlen, family, sndid, rcvid); +} + +static struct tcp_ao_key *__tcp_ao_do_lookup(const struct sock *sk, + const union tcp_ao_addr *addr, int family, u8 prefix, + int sndid, int rcvid) +{ + struct tcp_ao_key *key; + struct tcp_ao_info *ao; + + ao =3D rcu_dereference_check(tcp_sk(sk)->ao_info, + lockdep_sock_is_held(sk)); + if (!ao) + return NULL; + + hlist_for_each_entry_rcu(key, &ao->head, node) { + u8 prefixlen =3D min(prefix, key->prefixlen); + + if (!tcp_ao_key_cmp(key, addr, prefixlen, family, sndid, rcvid)) + return key; + } + return NULL; +} + +static struct tcp_ao_info *tcp_ao_alloc_info(gfp_t flags) +{ + struct tcp_ao_info *ao; + + ao =3D kzalloc(sizeof(*ao), flags); + if (!ao) + return NULL; + INIT_HLIST_HEAD(&ao->head); + + return ao; +} + +static void tcp_ao_link_mkt(struct tcp_ao_info *ao, struct tcp_ao_key *mkt) +{ + hlist_add_head_rcu(&mkt->node, &ao->head); +} + +static void tcp_ao_key_free_rcu(struct rcu_head *head) +{ + struct tcp_ao_key *key =3D container_of(head, struct tcp_ao_key, rcu); + + tcp_sigpool_release(key->tcp_sigpool_id); + kfree(key); +} + +void tcp_ao_destroy_sock(struct sock *sk) +{ + struct tcp_ao_info *ao; + struct tcp_ao_key *key; + struct hlist_node *n; + + ao =3D rcu_dereference_protected(tcp_sk(sk)->ao_info, 1); + tcp_sk(sk)->ao_info =3D NULL; + + if (!ao) + return; + + hlist_for_each_entry_safe(key, n, &ao->head, node) { + hlist_del_rcu(&key->node); + atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc); + call_rcu(&key->rcu, tcp_ao_key_free_rcu); + } + + kfree_rcu(ao, rcu); +} + +static bool tcp_ao_can_set_current_rnext(struct sock *sk) +{ + /* There aren't current/rnext keys on TCP_LISTEN sockets */ + if (sk->sk_state =3D=3D TCP_LISTEN) + return false; + return true; +} + +static int tcp_ao_verify_ipv4(struct sock *sk, struct tcp_ao_add *cmd, + union tcp_ao_addr **addr) +{ + struct sockaddr_in *sin =3D (struct sockaddr_in *)&cmd->addr; + struct inet_sock *inet =3D inet_sk(sk); + + if (sin->sin_family !=3D AF_INET) + return -EINVAL; + + /* Currently matching is not performed on port (or port ranges) */ + if (sin->sin_port !=3D 0) + return -EINVAL; + + /* Check prefix and trailing 0's in addr */ + if (cmd->prefix !=3D 0) { + __be32 mask; + + if (ntohl(sin->sin_addr.s_addr) =3D=3D INADDR_ANY) + return -EINVAL; + if (cmd->prefix > 32) + return -EINVAL; + + mask =3D inet_make_mask(cmd->prefix); + if (sin->sin_addr.s_addr & ~mask) + return -EINVAL; + + /* Check that MKT address is consistent with socket */ + if (ntohl(inet->inet_daddr) !=3D INADDR_ANY && + (inet->inet_daddr & mask) !=3D sin->sin_addr.s_addr) + return -EINVAL; + } else { + if (ntohl(sin->sin_addr.s_addr) !=3D INADDR_ANY) + return -EINVAL; + } + + *addr =3D (union tcp_ao_addr *)&sin->sin_addr; + return 0; +} + +static int tcp_ao_parse_crypto(struct tcp_ao_add *cmd, struct tcp_ao_key *= key) +{ + unsigned int syn_tcp_option_space; + bool is_kdf_aes_128_cmac =3D false; + struct crypto_ahash *tfm; + struct tcp_sigpool hp; + void *tmp_key =3D NULL; + int err; + + /* RFC5926, 3.1.1.2. KDF_AES_128_CMAC */ + if (!strcmp("cmac(aes128)", cmd->alg_name)) { + strscpy(cmd->alg_name, "cmac(aes)", sizeof(cmd->alg_name)); + is_kdf_aes_128_cmac =3D (cmd->keylen !=3D 16); + tmp_key =3D kmalloc(cmd->keylen, GFP_KERNEL); + if (!tmp_key) + return -ENOMEM; + } + + key->maclen =3D cmd->maclen ?: 12; /* 12 is the default in RFC5925 */ + + /* Check: maclen + tcp-ao header <=3D (MAX_TCP_OPTION_SPACE - mss + * - tstamp - wscale - sackperm), + * see tcp_syn_options(), tcp_synack_options(), commit 33ad798c924b. + * + * In order to allow D-SACK with TCP-AO, the header size should be: + * (MAX_TCP_OPTION_SPACE - TCPOLEN_TSTAMP_ALIGNED + * - TCPOLEN_SACK_BASE_ALIGNED + * - 2 * TCPOLEN_SACK_PERBLOCK) =3D 8 (maclen =3D 4), + * see tcp_established_options(). + * + * RFC5925, 2.2: + * Typical MACs are 96-128 bits (12-16 bytes), but any length + * that fits in the header of the segment being authenticated + * is allowed. + * + * RFC5925, 7.6: + * TCP-AO continues to consume 16 bytes in non-SYN segments, + * leaving a total of 24 bytes for other options, of which + * the timestamp consumes 10. This leaves 14 bytes, of which 10 + * are used for a single SACK block. When two SACK blocks are used, + * such as to handle D-SACK, a smaller TCP-AO MAC would be required + * to make room for the additional SACK block (i.e., to leave 18 + * bytes for the D-SACK variant of the SACK option) [RFC2883]. + * Note that D-SACK is not supportable in TCP MD5 in the presence + * of timestamps, because TCP MD5=E2=80=99s MAC length is fixed and too + * large to leave sufficient option space. + */ + syn_tcp_option_space =3D MAX_TCP_OPTION_SPACE; + syn_tcp_option_space -=3D TCPOLEN_TSTAMP_ALIGNED; + syn_tcp_option_space -=3D TCPOLEN_WSCALE_ALIGNED; + syn_tcp_option_space -=3D TCPOLEN_SACKPERM_ALIGNED; + if (tcp_ao_len(key) > syn_tcp_option_space) { + err =3D -EMSGSIZE; + goto err_kfree; + } + + key->keylen =3D cmd->keylen; + memcpy(key->key, cmd->key, cmd->keylen); + + err =3D tcp_sigpool_start(key->tcp_sigpool_id, &hp); + if (err) + goto err_kfree; + + tfm =3D crypto_ahash_reqtfm(hp.req); + if (is_kdf_aes_128_cmac) { + void *scratch =3D hp.scratch; + struct scatterlist sg; + + memcpy(tmp_key, cmd->key, cmd->keylen); + sg_init_one(&sg, tmp_key, cmd->keylen); + + /* Using zero-key of 16 bytes as described in RFC5926 */ + memset(scratch, 0, 16); + err =3D crypto_ahash_setkey(tfm, scratch, 16); + if (err) + goto err_pool_end; + + err =3D crypto_ahash_init(hp.req); + if (err) + goto err_pool_end; + + ahash_request_set_crypt(hp.req, &sg, key->key, cmd->keylen); + err =3D crypto_ahash_update(hp.req); + if (err) + goto err_pool_end; + + err |=3D crypto_ahash_final(hp.req); + if (err) + goto err_pool_end; + key->keylen =3D 16; + } + + err =3D crypto_ahash_setkey(tfm, key->key, key->keylen); + if (err) + goto err_pool_end; + + tcp_sigpool_end(&hp); + kfree(tmp_key); + + if (tcp_ao_maclen(key) > key->digest_size) + return -EINVAL; + + return 0; + +err_pool_end: + tcp_sigpool_end(&hp); +err_kfree: + kfree(tmp_key); + return err; +} + +#if IS_ENABLED(CONFIG_IPV6) +static int tcp_ao_verify_ipv6(struct sock *sk, struct tcp_ao_add *cmd, + union tcp_ao_addr **paddr, + unsigned short int *family) +{ + struct sockaddr_in6 *sin6 =3D (struct sockaddr_in6 *)&cmd->addr; + struct in6_addr *addr =3D &sin6->sin6_addr; + u8 prefix =3D cmd->prefix; + + if (sin6->sin6_family !=3D AF_INET6) + return -EINVAL; + + /* Currently matching is not performed on port (or port ranges) */ + if (sin6->sin6_port !=3D 0) + return -EINVAL; + + /* Check prefix and trailing 0's in addr */ + if (cmd->prefix !=3D 0 && ipv6_addr_v4mapped(addr)) { + __be32 addr4 =3D addr->s6_addr32[3]; + __be32 mask; + + if (prefix > 32 || ntohl(addr4) =3D=3D INADDR_ANY) + return -EINVAL; + + mask =3D inet_make_mask(prefix); + if (addr4 & ~mask) + return -EINVAL; + + /* Check that MKT address is consistent with socket */ + if (!ipv6_addr_any(&sk->sk_v6_daddr)) { + __be32 daddr4 =3D sk->sk_v6_daddr.s6_addr32[3]; + + if (!ipv6_addr_v4mapped(&sk->sk_v6_daddr)) + return -EINVAL; + if ((daddr4 & mask) !=3D addr4) + return -EINVAL; + } + + *paddr =3D (union tcp_ao_addr *)&addr->s6_addr32[3]; + *family =3D AF_INET; + return 0; + } else if (cmd->prefix !=3D 0) { + struct in6_addr pfx; + + if (ipv6_addr_any(addr) || prefix > 128) + return -EINVAL; + + ipv6_addr_prefix(&pfx, addr, prefix); + if (ipv6_addr_cmp(&pfx, addr)) + return -EINVAL; + + /* Check that MKT address is consistent with socket */ + if (!ipv6_addr_any(&sk->sk_v6_daddr) && + !ipv6_prefix_equal(&sk->sk_v6_daddr, addr, prefix)) + + return -EINVAL; + } else { + if (!ipv6_addr_any(addr)) + return -EINVAL; + } + + *paddr =3D (union tcp_ao_addr *)addr; + return 0; +} +#else +static int tcp_ao_verify_ipv6(struct sock *sk, struct tcp_ao_add *cmd, + union tcp_ao_addr **paddr, + unsigned short int *family) +{ + return -EOPNOTSUPP; +} +#endif + +static struct tcp_ao_info *setsockopt_ao_info(struct sock *sk) +{ + if (sk_fullsock(sk)) { + return rcu_dereference_protected(tcp_sk(sk)->ao_info, + lockdep_sock_is_held(sk)); + } + return ERR_PTR(-ESOCKTNOSUPPORT); +} + +#define TCP_AO_KEYF_ALL (0) + +static struct tcp_ao_key *tcp_ao_key_alloc(struct sock *sk, + struct tcp_ao_add *cmd) +{ + const char *algo =3D cmd->alg_name; + unsigned int digest_size; + struct crypto_ahash *tfm; + struct tcp_ao_key *key; + struct tcp_sigpool hp; + int err, pool_id; + size_t size; + + /* Force null-termination of alg_name */ + cmd->alg_name[ARRAY_SIZE(cmd->alg_name) - 1] =3D '\0'; + + /* RFC5926, 3.1.1.2. KDF_AES_128_CMAC */ + if (!strcmp("cmac(aes128)", algo)) + algo =3D "cmac(aes)"; + + /* Full TCP header (th->doff << 2) should fit into scratch area, + * see tcp_ao_hash_header(). + */ + pool_id =3D tcp_sigpool_alloc_ahash(algo, 60); + if (pool_id < 0) + return ERR_PTR(pool_id); + + err =3D tcp_sigpool_start(pool_id, &hp); + if (err) + goto err_free_pool; + + tfm =3D crypto_ahash_reqtfm(hp.req); + if (crypto_ahash_alignmask(tfm) > TCP_AO_KEY_ALIGN) { + err =3D -EOPNOTSUPP; + goto err_pool_end; + } + digest_size =3D crypto_ahash_digestsize(tfm); + tcp_sigpool_end(&hp); + + size =3D sizeof(struct tcp_ao_key) + (digest_size << 1); + key =3D sock_kmalloc(sk, size, GFP_KERNEL); + if (!key) { + err =3D -ENOMEM; + goto err_free_pool; + } + + key->tcp_sigpool_id =3D pool_id; + key->digest_size =3D digest_size; + return key; + +err_pool_end: + tcp_sigpool_end(&hp); +err_free_pool: + tcp_sigpool_release(pool_id); + return ERR_PTR(err); +} + +static int tcp_ao_add_cmd(struct sock *sk, unsigned short int family, + sockptr_t optval, int optlen) +{ + struct tcp_ao_info *ao_info; + union tcp_ao_addr *addr; + struct tcp_ao_key *key; + struct tcp_ao_add cmd; + bool first =3D false; + int ret; + + if (optlen < sizeof(cmd)) + return -EINVAL; + + ret =3D copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen); + if (ret) + return ret; + + if (cmd.keylen > TCP_AO_MAXKEYLEN) + return -EINVAL; + + if (cmd.reserved !=3D 0 || cmd.reserved2 !=3D 0) + return -EINVAL; + + if (family =3D=3D AF_INET) + ret =3D tcp_ao_verify_ipv4(sk, &cmd, &addr); + else + ret =3D tcp_ao_verify_ipv6(sk, &cmd, &addr, &family); + if (ret) + return ret; + + if (cmd.keyflags & ~TCP_AO_KEYF_ALL) + return -EINVAL; + + if (cmd.set_current || cmd.set_rnext) { + if (!tcp_ao_can_set_current_rnext(sk)) + return -EINVAL; + } + + ao_info =3D setsockopt_ao_info(sk); + if (IS_ERR(ao_info)) + return PTR_ERR(ao_info); + + if (!ao_info) { + ao_info =3D tcp_ao_alloc_info(GFP_KERNEL); + if (!ao_info) + return -ENOMEM; + first =3D true; + } else { + /* Check that neither RecvID nor SendID match any + * existing key for the peer, RFC5925 3.1: + * > The IDs of MKTs MUST NOT overlap where their + * > TCP connection identifiers overlap. + */ + if (__tcp_ao_do_lookup(sk, addr, family, + cmd.prefix, -1, cmd.rcvid)) + return -EEXIST; + if (__tcp_ao_do_lookup(sk, addr, family, + cmd.prefix, cmd.sndid, -1)) + return -EEXIST; + } + + key =3D tcp_ao_key_alloc(sk, &cmd); + if (IS_ERR(key)) { + ret =3D PTR_ERR(key); + goto err_free_ao; + } + + INIT_HLIST_NODE(&key->node); + memcpy(&key->addr, addr, (family =3D=3D AF_INET) ? sizeof(struct in_addr)= : + sizeof(struct in6_addr)); + key->prefixlen =3D cmd.prefix; + key->family =3D family; + key->keyflags =3D cmd.keyflags; + key->sndid =3D cmd.sndid; + key->rcvid =3D cmd.rcvid; + + ret =3D tcp_ao_parse_crypto(&cmd, key); + if (ret < 0) + goto err_free_sock; + + tcp_ao_link_mkt(ao_info, key); + if (first) { + sk_gso_disable(sk); + rcu_assign_pointer(tcp_sk(sk)->ao_info, ao_info); + } + + if (cmd.set_current) + WRITE_ONCE(ao_info->current_key, key); + if (cmd.set_rnext) + WRITE_ONCE(ao_info->rnext_key, key); + return 0; + +err_free_sock: + atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc); + tcp_sigpool_release(key->tcp_sigpool_id); + kfree(key); +err_free_ao: + if (first) + kfree(ao_info); + return ret; +} + +static int tcp_ao_delete_key(struct sock *sk, struct tcp_ao_info *ao_info, + struct tcp_ao_key *key, + struct tcp_ao_key *new_current, + struct tcp_ao_key *new_rnext) +{ + int err; + + hlist_del_rcu(&key->node); + + /* At this moment another CPU could have looked this key up + * while it was unlinked from the list. Wait for RCU grace period, + * after which the key is off-list and can't be looked up again; + * the rx path [just before RCU came] might have used it and set it + * as current_key (very unlikely). + */ + synchronize_rcu(); + if (new_current) + WRITE_ONCE(ao_info->current_key, new_current); + if (new_rnext) + WRITE_ONCE(ao_info->rnext_key, new_rnext); + + if (unlikely(READ_ONCE(ao_info->current_key) =3D=3D key || + READ_ONCE(ao_info->rnext_key) =3D=3D key)) { + err =3D -EBUSY; + goto add_key; + } + + atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc); + call_rcu(&key->rcu, tcp_ao_key_free_rcu); + + return 0; +add_key: + hlist_add_head_rcu(&key->node, &ao_info->head); + return err; +} + +static int tcp_ao_del_cmd(struct sock *sk, unsigned short int family, + sockptr_t optval, int optlen) +{ + struct tcp_ao_key *key, *new_current =3D NULL, *new_rnext =3D NULL; + struct tcp_ao_info *ao_info; + union tcp_ao_addr *addr; + struct tcp_ao_del cmd; + int addr_len; + __u8 prefix; + u16 port; + int err; + + if (optlen < sizeof(cmd)) + return -EINVAL; + + err =3D copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen); + if (err) + return err; + + if (cmd.reserved !=3D 0 || cmd.reserved2 !=3D 0) + return -EINVAL; + + if (cmd.set_current || cmd.set_rnext) { + if (!tcp_ao_can_set_current_rnext(sk)) + return -EINVAL; + } + + ao_info =3D setsockopt_ao_info(sk); + if (IS_ERR(ao_info)) + return PTR_ERR(ao_info); + if (!ao_info) + return -ENOENT; + + /* For sockets in TCP_CLOSED it's possible set keys that aren't + * matching the future peer (address/VRF/etc), + * tcp_ao_connect_init() will choose a correct matching MKT + * if there's any. + */ + if (cmd.set_current) { + new_current =3D tcp_ao_established_key(ao_info, cmd.current_key, -1); + if (!new_current) + return -ENOENT; + } + if (cmd.set_rnext) { + new_rnext =3D tcp_ao_established_key(ao_info, -1, cmd.rnext); + if (!new_rnext) + return -ENOENT; + } + + if (family =3D=3D AF_INET) { + struct sockaddr_in *sin =3D (struct sockaddr_in *)&cmd.addr; + + addr =3D (union tcp_ao_addr *)&sin->sin_addr; + addr_len =3D sizeof(struct in_addr); + port =3D ntohs(sin->sin_port); + } else { + struct sockaddr_in6 *sin6 =3D (struct sockaddr_in6 *)&cmd.addr; + struct in6_addr *addr6 =3D &sin6->sin6_addr; + + if (ipv6_addr_v4mapped(addr6)) { + addr =3D (union tcp_ao_addr *)&addr6->s6_addr32[3]; + addr_len =3D sizeof(struct in_addr); + family =3D AF_INET; + } else { + addr =3D (union tcp_ao_addr *)addr6; + addr_len =3D sizeof(struct in6_addr); + } + port =3D ntohs(sin6->sin6_port); + } + prefix =3D cmd.prefix; + + /* Currently matching is not performed on port (or port ranges) */ + if (port !=3D 0) + return -EINVAL; + + /* We could choose random present key here for current/rnext + * but that's less predictable. Let's be strict and don't + * allow removing a key that's in use. RFC5925 doesn't + * specify how-to coordinate key removal, but says: + * "It is presumed that an MKT affecting a particular + * connection cannot be destroyed during an active connection" + */ + hlist_for_each_entry_rcu(key, &ao_info->head, node) { + if (cmd.sndid !=3D key->sndid || + cmd.rcvid !=3D key->rcvid) + continue; + + if (family !=3D key->family || + prefix !=3D key->prefixlen || + memcmp(addr, &key->addr, addr_len)) + continue; + + if (key =3D=3D new_current || key =3D=3D new_rnext) + continue; + + return tcp_ao_delete_key(sk, ao_info, key, + new_current, new_rnext); + } + return -ENOENT; +} + +static int tcp_ao_info_cmd(struct sock *sk, unsigned short int family, + sockptr_t optval, int optlen) +{ + struct tcp_ao_key *new_current =3D NULL, *new_rnext =3D NULL; + struct tcp_ao_info *ao_info; + struct tcp_ao_info_opt cmd; + bool first =3D false; + int err; + + if (optlen < sizeof(cmd)) + return -EINVAL; + + err =3D copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen); + if (err) + return err; + + if (cmd.set_current || cmd.set_rnext) { + if (!tcp_ao_can_set_current_rnext(sk)) + return -EINVAL; + } + + if (cmd.reserved !=3D 0) + return -EINVAL; + + ao_info =3D setsockopt_ao_info(sk); + if (IS_ERR(ao_info)) + return PTR_ERR(ao_info); + if (!ao_info) { + ao_info =3D tcp_ao_alloc_info(GFP_KERNEL); + if (!ao_info) + return -ENOMEM; + first =3D true; + } + + /* For sockets in TCP_CLOSED it's possible set keys that aren't + * matching the future peer (address/port/VRF/etc), + * tcp_ao_connect_init() will choose a correct matching MKT + * if there's any. + */ + if (cmd.set_current) { + new_current =3D tcp_ao_established_key(ao_info, cmd.current_key, -1); + if (!new_current) { + err =3D -ENOENT; + goto out; + } + } + if (cmd.set_rnext) { + new_rnext =3D tcp_ao_established_key(ao_info, -1, cmd.rnext); + if (!new_rnext) { + err =3D -ENOENT; + goto out; + } + } + + ao_info->ao_required =3D cmd.ao_required; + if (new_current) + WRITE_ONCE(ao_info->current_key, new_current); + if (new_rnext) + WRITE_ONCE(ao_info->rnext_key, new_rnext); + if (first) { + sk_gso_disable(sk); + rcu_assign_pointer(tcp_sk(sk)->ao_info, ao_info); + } + return 0; +out: + if (first) + kfree(ao_info); + return err; +} + +int tcp_parse_ao(struct sock *sk, int cmd, unsigned short int family, + sockptr_t optval, int optlen) +{ + if (WARN_ON_ONCE(family !=3D AF_INET && family !=3D AF_INET6)) + return -EAFNOSUPPORT; + + switch (cmd) { + case TCP_AO_ADD_KEY: + return tcp_ao_add_cmd(sk, family, optval, optlen); + case TCP_AO_DEL_KEY: + return tcp_ao_del_cmd(sk, family, optval, optlen); + case TCP_AO_INFO: + return tcp_ao_info_cmd(sk, family, optval, optlen); + default: + WARN_ON_ONCE(1); + return -EINVAL; + } +} + +int tcp_v4_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen) +{ + return tcp_parse_ao(sk, cmd, AF_INET, optval, optlen); +} + diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index d3d7a03002f6..ce10df35fedb 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -2269,11 +2269,16 @@ const struct inet_connection_sock_af_ops ipv4_speci= fic =3D { }; EXPORT_SYMBOL(ipv4_specific); =20 -#ifdef CONFIG_TCP_MD5SIG +#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) static const struct tcp_sock_af_ops tcp_sock_ipv4_specific =3D { +#ifdef CONFIG_TCP_MD5SIG .md5_lookup =3D tcp_v4_md5_lookup, .calc_md5_hash =3D tcp_v4_md5_hash_skb, .md5_parse =3D tcp_v4_parse_md5_keys, +#endif +#ifdef CONFIG_TCP_AO + .ao_parse =3D tcp_v4_parse_ao, +#endif }; #endif =20 @@ -2288,7 +2293,7 @@ static int tcp_v4_init_sock(struct sock *sk) =20 icsk->icsk_af_ops =3D &ipv4_specific; =20 -#ifdef CONFIG_TCP_MD5SIG +#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) tcp_sk(sk)->af_specific =3D &tcp_sock_ipv4_specific; #endif =20 @@ -2339,6 +2344,7 @@ void tcp_v4_destroy_sock(struct sock *sk) rcu_assign_pointer(tp->md5sig_info, NULL); } #endif + tcp_ao_destroy_sock(sk); =20 /* Clean up a referenced TCP bind bucket. */ if (inet_csk(sk)->icsk_bind_hash) diff --git a/net/ipv6/Makefile b/net/ipv6/Makefile index 3036a45e8a1e..d283c59df4c1 100644 --- a/net/ipv6/Makefile +++ b/net/ipv6/Makefile @@ -52,4 +52,5 @@ obj-$(subst m,y,$(CONFIG_IPV6)) +=3D inet6_hashtables.o ifneq ($(CONFIG_IPV6),) obj-$(CONFIG_NET_UDP_TUNNEL) +=3D ip6_udp_tunnel.o obj-y +=3D mcast_snoop.o +obj-$(CONFIG_TCP_AO) +=3D tcp_ao.o endif diff --git a/net/ipv6/tcp_ao.c b/net/ipv6/tcp_ao.c new file mode 100644 index 000000000000..049ddbabe049 --- /dev/null +++ b/net/ipv6/tcp_ao.c @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * INET An implementation of the TCP Authentication Option (TCP-AO). + * See RFC5925. + * + * Authors: Dmitry Safonov + * Francesco Ruggeri + * Salam Noureddine + */ +#include + +#include +#include + +int tcp_v6_parse_ao(struct sock *sk, int cmd, + sockptr_t optval, int optlen) +{ + return tcp_parse_ao(sk, cmd, AF_INET6, optval, optlen); +} diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 5a06bcfd6cd1..21e2dc011b23 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -76,16 +76,9 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_do_rcv(struct sock *s= k, struct sk_buff *skb); =20 static const struct inet_connection_sock_af_ops ipv6_mapped; const struct inet_connection_sock_af_ops ipv6_specific; -#ifdef CONFIG_TCP_MD5SIG +#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) static const struct tcp_sock_af_ops tcp_sock_ipv6_specific; static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific; -#else -static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(const struct sock *sk, - const struct in6_addr *addr, - int l3index) -{ - return NULL; -} #endif =20 /* Helper returning the inet6 address from a given tcp socket. @@ -239,7 +232,7 @@ static int tcp_v6_connect(struct sock *sk, struct socka= ddr *uaddr, if (sk_is_mptcp(sk)) mptcpv6_handle_mapped(sk, true); sk->sk_backlog_rcv =3D tcp_v4_do_rcv; -#ifdef CONFIG_TCP_MD5SIG +#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) tp->af_specific =3D &tcp_sock_ipv6_mapped_specific; #endif =20 @@ -252,7 +245,7 @@ static int tcp_v6_connect(struct sock *sk, struct socka= ddr *uaddr, if (sk_is_mptcp(sk)) mptcpv6_handle_mapped(sk, false); sk->sk_backlog_rcv =3D tcp_v6_do_rcv; -#ifdef CONFIG_TCP_MD5SIG +#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) tp->af_specific =3D &tcp_sock_ipv6_specific; #endif goto failure; @@ -768,7 +761,13 @@ static int tcp_v6_md5_hash_skb(char *md5_hash, memset(md5_hash, 0, 16); return 1; } - +#else /* CONFIG_TCP_MD5SIG */ +static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(const struct sock *sk, + const struct in6_addr *addr, + int l3index) +{ + return NULL; +} #endif =20 static void tcp_v6_init_req(struct request_sock *req, @@ -1229,7 +1228,7 @@ static struct sock *tcp_v6_syn_recv_sock(const struct= sock *sk, struct sk_buff * if (sk_is_mptcp(newsk)) mptcpv6_handle_mapped(newsk, true); newsk->sk_backlog_rcv =3D tcp_v4_do_rcv; -#ifdef CONFIG_TCP_MD5SIG +#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) newtp->af_specific =3D &tcp_sock_ipv6_mapped_specific; #endif =20 @@ -1893,11 +1892,16 @@ const struct inet_connection_sock_af_ops ipv6_speci= fic =3D { .mtu_reduced =3D tcp_v6_mtu_reduced, }; =20 -#ifdef CONFIG_TCP_MD5SIG +#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) static const struct tcp_sock_af_ops tcp_sock_ipv6_specific =3D { +#ifdef CONFIG_TCP_MD5SIG .md5_lookup =3D tcp_v6_md5_lookup, .calc_md5_hash =3D tcp_v6_md5_hash_skb, .md5_parse =3D tcp_v6_parse_md5_keys, +#endif +#ifdef CONFIG_TCP_AO + .ao_parse =3D tcp_v6_parse_ao, +#endif }; #endif =20 @@ -1919,11 +1923,16 @@ static const struct inet_connection_sock_af_ops ipv= 6_mapped =3D { .mtu_reduced =3D tcp_v4_mtu_reduced, }; =20 -#ifdef CONFIG_TCP_MD5SIG +#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific =3D { +#ifdef CONFIG_TCP_MD5SIG .md5_lookup =3D tcp_v4_md5_lookup, .calc_md5_hash =3D tcp_v4_md5_hash_skb, .md5_parse =3D tcp_v6_parse_md5_keys, +#endif +#ifdef CONFIG_TCP_AO + .ao_parse =3D tcp_v6_parse_ao, +#endif }; #endif =20 @@ -1938,7 +1947,7 @@ static int tcp_v6_init_sock(struct sock *sk) =20 icsk->icsk_af_ops =3D &ipv6_specific; =20 -#ifdef CONFIG_TCP_MD5SIG +#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) tcp_sk(sk)->af_specific =3D &tcp_sock_ipv6_specific; #endif =20 --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 874E5C04A94 for ; Tue, 15 Aug 2023 19:15:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239877AbjHOTPZ (ORCPT ); Tue, 15 Aug 2023 15:15:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239844AbjHOTPP (ORCPT ); Tue, 15 Aug 2023 15:15:15 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D153510E for ; Tue, 15 Aug 2023 12:15:11 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-3fe8a1591c7so42645255e9.3 for ; Tue, 15 Aug 2023 12:15:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126910; x=1692731710; 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=5h39+84yx1FeO5RAPJWkwK9jh7nkeHfkG0lSpj+E58E=; b=E97b8e2QBkWiEd+jUq3VP+U8judZhZh9i4n4SXdXTUDwQmeHmCfrsB93ztp7Zbx4cD uat4UykPKdNCaOQMo4Z7zb051QJQp4P75RjpvVw4B4V9OJ+NDj3MO2YVzSsgB0Ta/7Su HpdAbkp1xC1nh+u5EXM8+I/AgSltZvWkUO9VyPXCeAn1dp/v1WL9iREBfAaRb9pQ+OTI kdqHyP644MZHq9nNGk720V8A0qsAgDlIGyAP6mCKT8a5qaboRR9JxPsa+lAA48dIRHCN zFh2VF7hbnaBVYU9YYf+6W9J9gtTUJ2K1+fswkq0x8OsIqQOxSSoA3zchSyzjBexIgXR 9Z3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126910; x=1692731710; 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=5h39+84yx1FeO5RAPJWkwK9jh7nkeHfkG0lSpj+E58E=; b=lDX5zWUXliHuw2riVZDtOd1eEYLKESD6wu1zdHrMgef23KseqYj8Z3WdfhEPX9IGca 2t4tkeds8K9UdsBBHcOdB4wI5xF+f1gq2xV7WZ4YqdGSVRnkxOwdAgW0nlxHlVo2XhdN NN0kOoGZcQnpRWipcDPuN2IBDy9E+zykF9oVhuG5a3p5ngpD57gELhHJPBjerGo97/dQ JOlFEIaZqjsmIm2xh52BXC8d19uYqSExNH4gwTsYIX7kANUYUUYf7xv0SY22zo9rJpkc pFGqZ2Yt/Dn32VoswZPppuz3SVpwv1ARK0IoLcchF93acQQAyWKIp6KVonxEq0/s+DIG /5Gg== X-Gm-Message-State: AOJu0Yxn8R+GMGLGmipDyTpPsBxQhEs/Bkf2vb9R0vwxBUBRhyajlcrZ xlWQeR/rCyC8kWe3yPrFytVpgQ== X-Google-Smtp-Source: AGHT+IEWt82zGek4luN2JN8eozUgiEXlbklNJ1RHFJF7ev0unSjAMvfAiSIPVpF25EuX7k+qpV1+vw== X-Received: by 2002:a05:600c:2149:b0:3fa:71db:56ff with SMTP id v9-20020a05600c214900b003fa71db56ffmr9585463wml.40.1692126910280; Tue, 15 Aug 2023 12:15:10 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:09 -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 v10 net-next 04/23] net/tcp: Prevent TCP-MD5 with TCP-AO being set Date: Tue, 15 Aug 2023 20:14:33 +0100 Message-ID: <20230815191455.1872316-5-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Be as conservative as possible: if there is TCP-MD5 key for a given peer regardless of L3 interface - don't allow setting TCP-AO key for the same peer. According to RFC5925, TCP-AO is supposed to replace TCP-MD5 and there can't be any switch between both on any connected tuple. Later it can be relaxed, if there's a use, but in the beginning restrict any intersection. Note: it's still should be possible to set both TCP-MD5 and TCP-AO keys on a listening socket for *different* peers. 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 Acked-by: David Ahern --- include/net/tcp.h | 43 +++++++++++++++++++++++++++++++++++++-- include/net/tcp_ao.h | 13 ++++++++++++ net/ipv4/tcp_ao.c | 47 +++++++++++++++++++++++++++++++++++++++++++ net/ipv4/tcp_ipv4.c | 14 ++++++++++--- net/ipv4/tcp_output.c | 47 +++++++++++++++++++++++++++++++++++++++++++ net/ipv6/tcp_ao.c | 17 ++++++++++++++++ net/ipv6/tcp_ipv6.c | 26 ++++++++++++++++++++---- 7 files changed, 198 insertions(+), 9 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index 6161e6bde100..b1c0dcfd45e8 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -1741,6 +1741,7 @@ int tcp_md5_key_copy(struct sock *sk, const union tcp= _md5_addr *addr, =20 int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr, int family, u8 prefixlen, int l3index, u8 flags); +void tcp_clear_md5_list(struct sock *sk); struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk, const struct sock *addr_sk); =20 @@ -1749,14 +1750,23 @@ struct tcp_md5sig_key *tcp_v4_md5_lookup(const stru= ct sock *sk, extern struct static_key_false_deferred tcp_md5_needed; struct tcp_md5sig_key *__tcp_md5_do_lookup(const struct sock *sk, int l3in= dex, const union tcp_md5_addr *addr, - int family); + int family, bool any_l3index); static inline struct tcp_md5sig_key * tcp_md5_do_lookup(const struct sock *sk, int l3index, const union tcp_md5_addr *addr, int family) { if (!static_branch_unlikely(&tcp_md5_needed.key)) return NULL; - return __tcp_md5_do_lookup(sk, l3index, addr, family); + return __tcp_md5_do_lookup(sk, l3index, addr, family, false); +} + +static inline struct tcp_md5sig_key * +tcp_md5_do_lookup_any_l3index(const struct sock *sk, + const union tcp_md5_addr *addr, int family) +{ + if (!static_branch_unlikely(&tcp_md5_needed.key)) + return NULL; + return __tcp_md5_do_lookup(sk, 0, addr, family, true); } =20 enum skb_drop_reason @@ -1774,6 +1784,13 @@ tcp_md5_do_lookup(const struct sock *sk, int l3index, return NULL; } =20 +static inline struct tcp_md5sig_key * +tcp_md5_do_lookup_any_l3index(const struct sock *sk, + const union tcp_md5_addr *addr, int family) +{ + return NULL; +} + static inline enum skb_drop_reason tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb, const void *saddr, const void *daddr, @@ -2136,6 +2153,9 @@ struct tcp_sock_af_ops { #endif #ifdef CONFIG_TCP_AO int (*ao_parse)(struct sock *sk, int optname, sockptr_t optval, int optle= n); + struct tcp_ao_key *(*ao_lookup)(const struct sock *sk, + struct sock *addr_sk, + int sndid, int rcvid); #endif }; =20 @@ -2547,4 +2567,23 @@ static inline u64 tcp_transmit_time(const struct soc= k *sk) return 0; } =20 +static inline bool tcp_ao_required(struct sock *sk, const void *saddr, + int family) +{ +#ifdef CONFIG_TCP_AO + struct tcp_ao_info *ao_info; + struct tcp_ao_key *ao_key; + + ao_info =3D rcu_dereference_check(tcp_sk(sk)->ao_info, + lockdep_sock_is_held(sk)); + if (!ao_info) + return false; + + ao_key =3D tcp_ao_do_lookup(sk, saddr, family, -1, -1); + if (ao_info->ao_required || ao_key) + return true; +#endif + return false; +} + #endif /* _TCP_H */ diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index a81e40fd255a..3c7f576376f9 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -92,11 +92,24 @@ struct tcp_ao_info { int tcp_parse_ao(struct sock *sk, int cmd, unsigned short int family, sockptr_t optval, int optlen); void tcp_ao_destroy_sock(struct sock *sk); +struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, + const union tcp_ao_addr *addr, + int family, int sndid, int rcvid); /* ipv4 specific functions */ int tcp_v4_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen= ); +struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *ad= dr_sk, + int sndid, int rcvid); /* ipv6 specific functions */ int tcp_v6_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen= ); +struct tcp_ao_key *tcp_v6_ao_lookup(const struct sock *sk, + struct sock *addr_sk, int sndid, int rcvid); #else +static inline struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, + const union tcp_ao_addr *addr, int family, int sndid, int rcvid) +{ + return NULL; +} + static inline void tcp_ao_destroy_sock(struct sock *sk) { } diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index 9121f1eeb224..0a530076abdc 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -116,6 +116,13 @@ static struct tcp_ao_key *__tcp_ao_do_lookup(const str= uct sock *sk, return NULL; } =20 +struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, + const union tcp_ao_addr *addr, + int family, int sndid, int rcvid) +{ + return __tcp_ao_do_lookup(sk, addr, family, U8_MAX, sndid, rcvid); +} + static struct tcp_ao_info *tcp_ao_alloc_info(gfp_t flags) { struct tcp_ao_info *ao; @@ -162,6 +169,14 @@ void tcp_ao_destroy_sock(struct sock *sk) kfree_rcu(ao, rcu); } =20 +struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *ad= dr_sk, + int sndid, int rcvid) +{ + union tcp_ao_addr *addr =3D (union tcp_ao_addr *)&addr_sk->sk_daddr; + + return tcp_ao_do_lookup(sk, addr, AF_INET, sndid, rcvid); +} + static bool tcp_ao_can_set_current_rnext(struct sock *sk) { /* There aren't current/rnext keys on TCP_LISTEN sockets */ @@ -497,6 +512,10 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned sh= ort int family, return -EINVAL; } =20 + /* Don't allow keys for peers that have a matching TCP-MD5 key */ + if (tcp_md5_do_lookup_any_l3index(sk, addr, family)) + return -EKEYREJECTED; + ao_info =3D setsockopt_ao_info(sk); if (IS_ERR(ao_info)) return PTR_ERR(ao_info); @@ -698,6 +717,31 @@ static int tcp_ao_del_cmd(struct sock *sk, unsigned sh= ort int family, return -ENOENT; } =20 +/* cmd.ao_required makes a socket TCP-AO only. + * Don't allow any md5 keys for any l3intf on the socket together with it. + * Restricting it early in setsockopt() removes a check for + * ao_info->ao_required on inbound tcp segment fast-path. + */ +static int tcp_ao_required_verify(struct sock *sk) +{ +#ifdef CONFIG_TCP_MD5SIG + const struct tcp_md5sig_info *md5sig; + + if (!static_branch_unlikely(&tcp_md5_needed.key)) + return 0; + + md5sig =3D rcu_dereference_check(tcp_sk(sk)->md5sig_info, + lockdep_sock_is_held(sk)); + if (!md5sig) + return 0; + + if (rcu_dereference_check(hlist_first_rcu(&md5sig->head), + lockdep_sock_is_held(sk))) + return 1; +#endif + return 0; +} + static int tcp_ao_info_cmd(struct sock *sk, unsigned short int family, sockptr_t optval, int optlen) { @@ -732,6 +776,9 @@ static int tcp_ao_info_cmd(struct sock *sk, unsigned sh= ort int family, first =3D true; } =20 + if (cmd.ao_required && tcp_ao_required_verify(sk)) + return -EKEYREJECTED; + /* For sockets in TCP_CLOSED it's possible set keys that aren't * matching the future peer (address/port/VRF/etc), * tcp_ao_connect_init() will choose a correct matching MKT diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index ce10df35fedb..354a26b19baf 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -1081,7 +1081,7 @@ static bool better_md5_match(struct tcp_md5sig_key *o= ld, struct tcp_md5sig_key * /* Find the Key structure for an address. */ struct tcp_md5sig_key *__tcp_md5_do_lookup(const struct sock *sk, int l3in= dex, const union tcp_md5_addr *addr, - int family) + int family, bool any_l3index) { const struct tcp_sock *tp =3D tcp_sk(sk); struct tcp_md5sig_key *key; @@ -1100,7 +1100,8 @@ struct tcp_md5sig_key *__tcp_md5_do_lookup(const stru= ct sock *sk, int l3index, lockdep_sock_is_held(sk)) { if (key->family !=3D family) continue; - if (key->flags & TCP_MD5SIG_FLAG_IFINDEX && key->l3index !=3D l3index) + if (!any_l3index && key->flags & TCP_MD5SIG_FLAG_IFINDEX && + key->l3index !=3D l3index) continue; if (family =3D=3D AF_INET) { mask =3D inet_make_mask(key->prefixlen); @@ -1312,7 +1313,7 @@ int tcp_md5_do_del(struct sock *sk, const union tcp_m= d5_addr *addr, int family, } EXPORT_SYMBOL(tcp_md5_do_del); =20 -static void tcp_clear_md5_list(struct sock *sk) +void tcp_clear_md5_list(struct sock *sk) { struct tcp_sock *tp =3D tcp_sk(sk); struct tcp_md5sig_key *key; @@ -1382,6 +1383,12 @@ static int tcp_v4_parse_md5_keys(struct sock *sk, in= t optname, if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN) return -EINVAL; =20 + /* Don't allow keys for peers that have a matching TCP-AO key. + * See the comment in tcp_ao_add_cmd() + */ + if (tcp_ao_required(sk, addr, AF_INET)) + return -EKEYREJECTED; + return tcp_md5_do_add(sk, addr, AF_INET, prefixlen, l3index, flags, cmd.tcpm_key, cmd.tcpm_keylen); } @@ -2277,6 +2284,7 @@ static const struct tcp_sock_af_ops tcp_sock_ipv4_spe= cific =3D { .md5_parse =3D tcp_v4_parse_md5_keys, #endif #ifdef CONFIG_TCP_AO + .ao_lookup =3D tcp_v4_ao_lookup, .ao_parse =3D tcp_v4_parse_ao, #endif }; diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 769a558159ee..680b5b660be2 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -3931,6 +3931,53 @@ int tcp_connect(struct sock *sk) =20 tcp_call_bpf(sk, BPF_SOCK_OPS_TCP_CONNECT_CB, 0, NULL); =20 +#if defined(CONFIG_TCP_MD5SIG) && defined(CONFIG_TCP_AO) + /* Has to be checked late, after setting daddr/saddr/ops. + * Return error if the peer has both a md5 and a tcp-ao key + * configured as this is ambiguous. + */ + if (unlikely(rcu_dereference_protected(tp->md5sig_info, + lockdep_sock_is_held(sk)))) { + bool needs_ao =3D !!tp->af_specific->ao_lookup(sk, sk, -1, -1); + bool needs_md5 =3D !!tp->af_specific->md5_lookup(sk, sk); + struct tcp_ao_info *ao_info; + + ao_info =3D rcu_dereference_check(tp->ao_info, + lockdep_sock_is_held(sk)); + if (ao_info) { + /* This is an extra check: tcp_ao_required() in + * tcp_v{4,6}_parse_md5_keys() should prevent adding + * md5 keys on ao_required socket. + */ + needs_ao |=3D ao_info->ao_required; + WARN_ON_ONCE(ao_info->ao_required && needs_md5); + } + if (needs_md5 && needs_ao) + return -EKEYREJECTED; + + /* If we have a matching md5 key and no matching tcp-ao key + * then free up ao_info if allocated. + */ + if (needs_md5) { + tcp_ao_destroy_sock(sk); + } else if (needs_ao) { + tcp_clear_md5_list(sk); + kfree(rcu_replace_pointer(tp->md5sig_info, NULL, + lockdep_sock_is_held(sk))); + } + } +#endif +#ifdef CONFIG_TCP_AO + if (unlikely(rcu_dereference_protected(tp->ao_info, + lockdep_sock_is_held(sk)))) { + /* Don't allow connecting if ao is configured but no + * matching key is found. + */ + if (!tp->af_specific->ao_lookup(sk, sk, -1, -1)) + return -EKEYREJECTED; + } +#endif + if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk)) return -EHOSTUNREACH; /* Routing failure or similar. */ =20 diff --git a/net/ipv6/tcp_ao.c b/net/ipv6/tcp_ao.c index 049ddbabe049..0640acaee67b 100644 --- a/net/ipv6/tcp_ao.c +++ b/net/ipv6/tcp_ao.c @@ -12,6 +12,23 @@ #include #include =20 +static struct tcp_ao_key *tcp_v6_ao_do_lookup(const struct sock *sk, + const struct in6_addr *addr, + int sndid, int rcvid) +{ + return tcp_ao_do_lookup(sk, (union tcp_ao_addr *)addr, AF_INET6, + sndid, rcvid); +} + +struct tcp_ao_key *tcp_v6_ao_lookup(const struct sock *sk, + struct sock *addr_sk, + int sndid, int rcvid) +{ + struct in6_addr *addr =3D &addr_sk->sk_v6_daddr; + + return tcp_v6_ao_do_lookup(sk, addr, sndid, rcvid); +} + int tcp_v6_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen) { diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 21e2dc011b23..53d64ea0938d 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -599,6 +599,7 @@ static int tcp_v6_parse_md5_keys(struct sock *sk, int o= ptname, { struct tcp_md5sig cmd; struct sockaddr_in6 *sin6 =3D (struct sockaddr_in6 *)&cmd.tcpm_addr; + union tcp_ao_addr *addr; int l3index =3D 0; u8 prefixlen; u8 flags; @@ -653,13 +654,28 @@ static int tcp_v6_parse_md5_keys(struct sock *sk, int= optname, if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN) return -EINVAL; =20 - if (ipv6_addr_v4mapped(&sin6->sin6_addr)) - return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin6->sin6_addr.s6_addr= 32[3], + if (ipv6_addr_v4mapped(&sin6->sin6_addr)) { + addr =3D (union tcp_md5_addr *)&sin6->sin6_addr.s6_addr32[3]; + + /* Don't allow keys for peers that have a matching TCP-AO key. + * See the comment in tcp_ao_add_cmd() + */ + if (tcp_ao_required(sk, addr, AF_INET)) + return -EKEYREJECTED; + return tcp_md5_do_add(sk, addr, AF_INET, prefixlen, l3index, flags, cmd.tcpm_key, cmd.tcpm_keylen); + } =20 - return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin6->sin6_addr, - AF_INET6, prefixlen, l3index, flags, + addr =3D (union tcp_md5_addr *)&sin6->sin6_addr; + + /* Don't allow keys for peers that have a matching TCP-AO key. + * See the comment in tcp_ao_add_cmd() + */ + if (tcp_ao_required(sk, addr, AF_INET6)) + return -EKEYREJECTED; + + return tcp_md5_do_add(sk, addr, AF_INET6, prefixlen, l3index, flags, cmd.tcpm_key, cmd.tcpm_keylen); } =20 @@ -1900,6 +1916,7 @@ static const struct tcp_sock_af_ops tcp_sock_ipv6_spe= cific =3D { .md5_parse =3D tcp_v6_parse_md5_keys, #endif #ifdef CONFIG_TCP_AO + .ao_lookup =3D tcp_v6_ao_lookup, .ao_parse =3D tcp_v6_parse_ao, #endif }; @@ -1931,6 +1948,7 @@ static const struct tcp_sock_af_ops tcp_sock_ipv6_map= ped_specific =3D { .md5_parse =3D tcp_v6_parse_md5_keys, #endif #ifdef CONFIG_TCP_AO + .ao_lookup =3D tcp_v6_ao_lookup, .ao_parse =3D tcp_v6_parse_ao, #endif }; --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 99720C04FE0 for ; Tue, 15 Aug 2023 19:15:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239885AbjHOTP0 (ORCPT ); Tue, 15 Aug 2023 15:15:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239845AbjHOTPR (ORCPT ); Tue, 15 Aug 2023 15:15:17 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7FA3C138 for ; Tue, 15 Aug 2023 12:15:13 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-3fe2d218eedso54103085e9.0 for ; Tue, 15 Aug 2023 12:15:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126912; x=1692731712; 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=5vEdThb2bsvpcFsrGmRTPHlFtLDRPs5NqgdOd9MVMx0=; b=kaSDb1SVlMJCgX9C91UMGC4w6tH1p/7Dmdo339HRE22UjxUMCHFX8SjrI09EyolfJc oWjSGOzqbq2vpR7L9jUUw8IG8tNO3cOlTcPV/W5ucz1tMiOtY+c5OBq9yBNqtZ9NGQ9E iWEWm+ZstghUoBibLaATXsS1Pmwrf5w2rKN7TFcvslxw8pDn89TNYL1HzXXfRNZKG8Cz cxHhKTW3swdp84BOoJMrwHuqk88GbwNMoj6xWfhFhSKum6MxkHtksGH7pnEdZnva0sAJ Tl9AXLR+juqo85N0mSaLiIt75LvVt2zihrz/Xt2xl2VQmpAcn0TjAodDP0idagXRCmR+ 9Ydg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126912; x=1692731712; 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=5vEdThb2bsvpcFsrGmRTPHlFtLDRPs5NqgdOd9MVMx0=; b=LFV781OPyvQYfTrGPbcTWjCzFPRI4rWoTd9J28mGYEf1rJ7yFqCQeTWpyoIEgJGjD3 IN3J8SivfQgIqtWNWaQ7DYXpE4rDGzgjeSX88Jm7T83HL0TCEvfGbX3RGkbWs3QoU/7s +Ij4SpQe7bIfjDpnVxXX5STQlwjNalBBtMnpe6CfDWdhFIV53TXL4YX1gty7vjZtH0TA v+HmfqGBtZ207GbbgaK8GZ2t3ad12L5OmlF566y8Lx7i38T5dXYCktjxSmJPf/d0OnRC cACp02DdTPASwktrfVHLZMNiN1qiZ4WmOU3yijjNE2Z3QbGuHgcVGf3N4/enLeFn0ddT 8GYA== X-Gm-Message-State: AOJu0YxckQK3eOrA4GIFW/oC86nPhKTJ9O9sjo8RjwNOMjJJo7MgFrot 3tnMmINpJmZVanq6Nw6UIc410w== X-Google-Smtp-Source: AGHT+IFMw1sg6aSUdNARsfMfsieIwo8twGLUVO7LeQsH9FgcLbKt/WunZ//bYQHp45pndO/43vDzoQ== X-Received: by 2002:a7b:c045:0:b0:3fe:1d34:908 with SMTP id u5-20020a7bc045000000b003fe1d340908mr10449806wmc.17.1692126911985; Tue, 15 Aug 2023 12:15:11 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:11 -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 v10 net-next 05/23] net/tcp: Calculate TCP-AO traffic keys Date: Tue, 15 Aug 2023 20:14:34 +0100 Message-ID: <20230815191455.1872316-6-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add traffic key calculation the way it's described in RFC5926. Wire it up to tcp_finish_connect() and cache the new keys straight away on already established TCP connections. 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 Acked-by: David Ahern --- include/net/tcp.h | 3 + include/net/tcp_ao.h | 46 +++++++++- net/ipv4/tcp_ao.c | 190 ++++++++++++++++++++++++++++++++++++++++++ net/ipv4/tcp_input.c | 1 + net/ipv4/tcp_ipv4.c | 1 + net/ipv4/tcp_output.c | 2 + net/ipv6/tcp_ao.c | 50 +++++++++++ net/ipv6/tcp_ipv6.c | 1 + 8 files changed, 292 insertions(+), 2 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index b1c0dcfd45e8..0ac0cc4186d8 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -2156,6 +2156,9 @@ struct tcp_sock_af_ops { struct tcp_ao_key *(*ao_lookup)(const struct sock *sk, struct sock *addr_sk, int sndid, int rcvid); + int (*ao_calc_key_sk)(struct tcp_ao_key *mkt, u8 *key, + const struct sock *sk, + __be32 sisn, __be32 disn, bool send); #endif }; =20 diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index 3c7f576376f9..7e0ce45d9934 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -89,8 +89,32 @@ struct tcp_ao_info { }; =20 #ifdef CONFIG_TCP_AO +/* TCP-AO structures and functions */ + +struct tcp4_ao_context { + __be32 saddr; + __be32 daddr; + __be16 sport; + __be16 dport; + __be32 sisn; + __be32 disn; +}; + +struct tcp6_ao_context { + struct in6_addr saddr; + struct in6_addr daddr; + __be16 sport; + __be16 dport; + __be32 sisn; + __be32 disn; +}; + +struct tcp_sigpool; + int tcp_parse_ao(struct sock *sk, int cmd, unsigned short int family, sockptr_t optval, int optlen); +int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx, + unsigned int len, struct tcp_sigpool *hp); void tcp_ao_destroy_sock(struct sock *sk); struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, const union tcp_ao_addr *addr, @@ -99,11 +123,21 @@ struct tcp_ao_key *tcp_ao_do_lookup(const struct sock = *sk, int tcp_v4_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen= ); struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *ad= dr_sk, int sndid, int rcvid); +int tcp_v4_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key, + const struct sock *sk, + __be32 sisn, __be32 disn, bool send); /* ipv6 specific functions */ -int tcp_v6_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen= ); +int tcp_v6_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key, + const struct sock *sk, __be32 sisn, + __be32 disn, bool send); struct tcp_ao_key *tcp_v6_ao_lookup(const struct sock *sk, struct sock *addr_sk, int sndid, int rcvid); -#else +int tcp_v6_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen= ); +void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *skb); +void tcp_ao_connect_init(struct sock *sk); + +#else /* CONFIG_TCP_AO */ + static inline struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, const union tcp_ao_addr *addr, int family, int sndid, int rcvid) { @@ -113,6 +147,14 @@ static inline struct tcp_ao_key *tcp_ao_do_lookup(cons= t struct sock *sk, static inline void tcp_ao_destroy_sock(struct sock *sk) { } + +static inline void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *= skb) +{ +} + +static inline void tcp_ao_connect_init(struct sock *sk) +{ +} #endif =20 #endif /* _TCP_AO_H */ diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index 0a530076abdc..fc7a6aa1936e 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -16,6 +16,34 @@ #include #include =20 +int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx, + unsigned int len, struct tcp_sigpool *hp) +{ + struct scatterlist sg; + int ret; + + if (crypto_ahash_setkey(crypto_ahash_reqtfm(hp->req), + mkt->key, mkt->keylen)) + goto clear_hash; + + ret =3D crypto_ahash_init(hp->req); + if (ret) + goto clear_hash; + + sg_init_one(&sg, ctx, len); + ahash_request_set_crypt(hp->req, &sg, key, len); + crypto_ahash_update(hp->req); + + ret =3D crypto_ahash_final(hp->req); + if (ret) + goto clear_hash; + + return 0; +clear_hash: + memset(key, 0, tcp_ao_digest_size(mkt)); + return 1; +} + /* Optimized version of tcp_ao_do_lookup(): only for sockets for which * it's known that the keys in ao_info are matching peer's * family/address/VRF/etc. @@ -169,6 +197,71 @@ void tcp_ao_destroy_sock(struct sock *sk) kfree_rcu(ao, rcu); } =20 +/* 4 tuple and ISNs are expected in NBO */ +static int tcp_v4_ao_calc_key(struct tcp_ao_key *mkt, u8 *key, + __be32 saddr, __be32 daddr, + __be16 sport, __be16 dport, + __be32 sisn, __be32 disn) +{ + /* See RFC5926 3.1.1 */ + struct kdf_input_block { + u8 counter; + u8 label[6]; + struct tcp4_ao_context ctx; + __be16 outlen; + } __packed * tmp; + struct tcp_sigpool hp; + int err; + + err =3D tcp_sigpool_start(mkt->tcp_sigpool_id, &hp); + if (err) + return err; + + tmp =3D hp.scratch; + tmp->counter =3D 1; + memcpy(tmp->label, "TCP-AO", 6); + tmp->ctx.saddr =3D saddr; + tmp->ctx.daddr =3D daddr; + tmp->ctx.sport =3D sport; + tmp->ctx.dport =3D dport; + tmp->ctx.sisn =3D sisn; + tmp->ctx.disn =3D disn; + tmp->outlen =3D htons(tcp_ao_digest_size(mkt) * 8); /* in bits */ + + err =3D tcp_ao_calc_traffic_key(mkt, key, tmp, sizeof(*tmp), &hp); + tcp_sigpool_end(&hp); + + return err; +} + +int tcp_v4_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key, + const struct sock *sk, + __be32 sisn, __be32 disn, bool send) +{ + if (send) + return tcp_v4_ao_calc_key(mkt, key, sk->sk_rcv_saddr, + sk->sk_daddr, htons(sk->sk_num), + sk->sk_dport, sisn, disn); + else + return tcp_v4_ao_calc_key(mkt, key, sk->sk_daddr, + sk->sk_rcv_saddr, sk->sk_dport, + htons(sk->sk_num), disn, sisn); +} + +static int tcp_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key, + const struct sock *sk, + __be32 sisn, __be32 disn, bool send) +{ + if (mkt->family =3D=3D AF_INET) + return tcp_v4_ao_calc_key_sk(mkt, key, sk, sisn, disn, send); +#if IS_ENABLED(CONFIG_IPV6) + else if (mkt->family =3D=3D AF_INET6) + return tcp_v6_ao_calc_key_sk(mkt, key, sk, sisn, disn, send); +#endif + else + return -EOPNOTSUPP; +} + struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *ad= dr_sk, int sndid, int rcvid) { @@ -177,6 +270,97 @@ struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock = *sk, struct sock *addr_sk, return tcp_ao_do_lookup(sk, addr, AF_INET, sndid, rcvid); } =20 +static int tcp_ao_cache_traffic_keys(const struct sock *sk, + struct tcp_ao_info *ao, + struct tcp_ao_key *ao_key) +{ + u8 *traffic_key =3D snd_other_key(ao_key); + int ret; + + ret =3D tcp_ao_calc_key_sk(ao_key, traffic_key, sk, + ao->lisn, ao->risn, true); + if (ret) + return ret; + + traffic_key =3D rcv_other_key(ao_key); + ret =3D tcp_ao_calc_key_sk(ao_key, traffic_key, sk, + ao->lisn, ao->risn, false); + return ret; +} + +void tcp_ao_connect_init(struct sock *sk) +{ + struct tcp_sock *tp =3D tcp_sk(sk); + struct tcp_ao_info *ao_info; + union tcp_ao_addr *addr; + struct tcp_ao_key *key; + int family; + + ao_info =3D rcu_dereference_protected(tp->ao_info, + lockdep_sock_is_held(sk)); + if (!ao_info) + return; + + /* Remove all keys that don't match the peer */ + family =3D sk->sk_family; + if (family =3D=3D AF_INET) + addr =3D (union tcp_ao_addr *)&sk->sk_daddr; +#if IS_ENABLED(CONFIG_IPV6) + else if (family =3D=3D AF_INET6) + addr =3D (union tcp_ao_addr *)&sk->sk_v6_daddr; +#endif + else + return; + + hlist_for_each_entry_rcu(key, &ao_info->head, node) { + if (!tcp_ao_key_cmp(key, addr, key->prefixlen, family, -1, -1)) + continue; + + if (key =3D=3D ao_info->current_key) + ao_info->current_key =3D NULL; + if (key =3D=3D ao_info->rnext_key) + ao_info->rnext_key =3D NULL; + hlist_del_rcu(&key->node); + tcp_sigpool_release(key->tcp_sigpool_id); + atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc); + kfree_rcu(key, rcu); + } + + key =3D tp->af_specific->ao_lookup(sk, sk, -1, -1); + if (key) { + /* if current_key or rnext_key were not provided, + * use the first key matching the peer + */ + if (!ao_info->current_key) + ao_info->current_key =3D key; + if (!ao_info->rnext_key) + ao_info->rnext_key =3D key; + tp->tcp_header_len +=3D tcp_ao_len(key); + + ao_info->lisn =3D htonl(tp->write_seq); + } else { + /* TODO: probably, it should fail to connect() here */ + rcu_assign_pointer(tp->ao_info, NULL); + kfree(ao_info); + } +} + +void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *skb) +{ + struct tcp_ao_info *ao; + struct tcp_ao_key *key; + + ao =3D rcu_dereference_protected(tcp_sk(sk)->ao_info, + lockdep_sock_is_held(sk)); + if (!ao) + return; + + ao->risn =3D tcp_hdr(skb)->seq; + + hlist_for_each_entry_rcu(key, &ao->head, node) + tcp_ao_cache_traffic_keys(sk, ao, key); +} + static bool tcp_ao_can_set_current_rnext(struct sock *sk) { /* There aren't current/rnext keys on TCP_LISTEN sockets */ @@ -558,6 +742,12 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned sh= ort int family, if (ret < 0) goto err_free_sock; =20 + /* Change this condition if we allow adding keys in states + * like close_wait, syn_sent or fin_wait... + */ + if (sk->sk_state =3D=3D TCP_ESTABLISHED) + tcp_ao_cache_traffic_keys(sk, ao_info, key); + tcp_ao_link_mkt(ao_info, key); if (first) { sk_gso_disable(sk); diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index d34d52fdfdb1..0df208d6fa44 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -6087,6 +6087,7 @@ void tcp_finish_connect(struct sock *sk, struct sk_bu= ff *skb) struct tcp_sock *tp =3D tcp_sk(sk); struct inet_connection_sock *icsk =3D inet_csk(sk); =20 + tcp_ao_finish_connect(sk, skb); tcp_set_state(sk, TCP_ESTABLISHED); icsk->icsk_ack.lrcvtime =3D tcp_jiffies32; =20 diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 354a26b19baf..f5699c6fa007 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -2286,6 +2286,7 @@ static const struct tcp_sock_af_ops tcp_sock_ipv4_spe= cific =3D { #ifdef CONFIG_TCP_AO .ao_lookup =3D tcp_v4_ao_lookup, .ao_parse =3D tcp_v4_parse_ao, + .ao_calc_key_sk =3D tcp_v4_ao_calc_key_sk, #endif }; #endif diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 680b5b660be2..895d902686fa 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -3749,6 +3749,8 @@ static void tcp_connect_init(struct sock *sk) if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_timestamps)) tp->tcp_header_len +=3D TCPOLEN_TSTAMP_ALIGNED; =20 + tcp_ao_connect_init(sk); + /* If user gave his TCP_MAXSEG, record it to clamp */ if (tp->rx_opt.user_mss) tp->rx_opt.mss_clamp =3D tp->rx_opt.user_mss; diff --git a/net/ipv6/tcp_ao.c b/net/ipv6/tcp_ao.c index 0640acaee67b..9ab594fadbd9 100644 --- a/net/ipv6/tcp_ao.c +++ b/net/ipv6/tcp_ao.c @@ -12,6 +12,56 @@ #include #include =20 +static int tcp_v6_ao_calc_key(struct tcp_ao_key *mkt, u8 *key, + const struct in6_addr *saddr, + const struct in6_addr *daddr, + __be16 sport, __be16 dport, + __be32 sisn, __be32 disn) +{ + struct kdf_input_block { + u8 counter; + u8 label[6]; + struct tcp6_ao_context ctx; + __be16 outlen; + } __packed * tmp; + struct tcp_sigpool hp; + int err; + + err =3D tcp_sigpool_start(mkt->tcp_sigpool_id, &hp); + if (err) + return err; + + tmp =3D hp.scratch; + tmp->counter =3D 1; + memcpy(tmp->label, "TCP-AO", 6); + tmp->ctx.saddr =3D *saddr; + tmp->ctx.daddr =3D *daddr; + tmp->ctx.sport =3D sport; + tmp->ctx.dport =3D dport; + tmp->ctx.sisn =3D sisn; + tmp->ctx.disn =3D disn; + tmp->outlen =3D htons(tcp_ao_digest_size(mkt) * 8); /* in bits */ + + err =3D tcp_ao_calc_traffic_key(mkt, key, tmp, sizeof(*tmp), &hp); + tcp_sigpool_end(&hp); + + return err; +} + +int tcp_v6_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key, + const struct sock *sk, __be32 sisn, + __be32 disn, bool send) +{ + if (send) + return tcp_v6_ao_calc_key(mkt, key, &sk->sk_v6_rcv_saddr, + &sk->sk_v6_daddr, htons(sk->sk_num), + sk->sk_dport, sisn, disn); + else + return tcp_v6_ao_calc_key(mkt, key, &sk->sk_v6_daddr, + &sk->sk_v6_rcv_saddr, sk->sk_dport, + htons(sk->sk_num), disn, sisn); +} + static struct tcp_ao_key *tcp_v6_ao_do_lookup(const struct sock *sk, const struct in6_addr *addr, int sndid, int rcvid) diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 53d64ea0938d..70a3f12a94a5 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -1918,6 +1918,7 @@ static const struct tcp_sock_af_ops tcp_sock_ipv6_spe= cific =3D { #ifdef CONFIG_TCP_AO .ao_lookup =3D tcp_v6_ao_lookup, .ao_parse =3D tcp_v6_parse_ao, + .ao_calc_key_sk =3D tcp_v6_ao_calc_key_sk, #endif }; #endif --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 84326C41513 for ; Tue, 15 Aug 2023 19:16:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239847AbjHOTPv (ORCPT ); Tue, 15 Aug 2023 15:15:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239726AbjHOTPT (ORCPT ); Tue, 15 Aug 2023 15:15:19 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5381EBF for ; Tue, 15 Aug 2023 12:15:15 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-317716a4622so5056791f8f.1 for ; Tue, 15 Aug 2023 12:15:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126914; x=1692731714; 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=6wBpyiT2TTYpfM0wN/1R7IgMWlCOkIymLolYlN6bvb4=; b=BJTF5Vdd/h0DAJYO/DjjzA/Z5C6apg7+lBVYoGDirq1qqhwGodNkB81urVnfrH3qjQ QV1qIgy9gRMxo27fzABSWC9yFMpKcaPK+e4kE98ZG9k5eue/VZa+k8/RheiDJnKJaqLp YVQ364TEvTqhJVrGve7VXQWxBNyE77sG/2IYvJ0zyDI1wjr29ND23x5ErYrf7f2yEKPI 8JPrlzNQPry5OFrslNd4hL7eTt2KKMt84zRw1ZNZJNc4W0BNHUd2Qz9Gpt0apNdWwxQ/ R1PfMdp4/1d+V6Ji+VdPlOxiLAmpMsNEU25CVDb0JbzCNPUsWJZiroI4HOaTwocaUaSA avlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126914; x=1692731714; 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=6wBpyiT2TTYpfM0wN/1R7IgMWlCOkIymLolYlN6bvb4=; b=Gkg8nyJhyvC9e442/k9UEOePfbqx3IkF7ZFcvA43I7zjFTJERh5wu1kILH3lxeHsW1 FtSIOyhB7dgiFDxfkK8tc8RkXdiLZl23EVqy7WeARul4c9VtASEFrISqAleN75BG2cJ/ baOAgICc/3qxcEQdjx9fp8Ddl2hCYetkT5tRtDV2a+jFE/I7aNVTfzF4tBqEgB37qeBT TJYCAAupGkKVcr1Rcoh7WRF4ax6JXpqIe4fUGSY8uAW2apMAkmPhfaNEdhLlLZ/Fqupi T1aWdD4QTJIyZgPoZYM5oXGPxo9naZWanKpaWixIMTHuKD3uMtSg5yeZ2kFAZ1v4lcEm bvfQ== X-Gm-Message-State: AOJu0Yy8z0hX4fndAMY/43j2FMtOWE022Wsem0bxF0AiF33olOY/ux5W L4AD0Hu8eQ/YcK4Mytz98h/u7w== X-Google-Smtp-Source: AGHT+IFy5ETn1M9HcV9gRbqbGtZ22EPtXqyeVa3nmehU1On12O+1/jOtvK1AR/xlOVeHukCnTJEBCw== X-Received: by 2002:adf:f590:0:b0:317:5ddb:a8bf with SMTP id f16-20020adff590000000b003175ddba8bfmr8898438wro.29.1692126913728; Tue, 15 Aug 2023 12:15:13 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:13 -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 v10 net-next 06/23] net/tcp: Add TCP-AO sign to outgoing packets Date: Tue, 15 Aug 2023 20:14:35 +0100 Message-ID: <20230815191455.1872316-7-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Using precalculated traffic keys, sign TCP segments as prescribed by RFC5925. Per RFC, TCP header options are included in sign calculation: "The TCP header, by default including options, and where the TCP checksum and TCP-AO MAC fields are set to zero, all in network- byte order." (5.1.3) tcp_ao_hash_header() has exclude_options parameter to optionally exclude TCP header from hash calculation, as described in RFC5925 (9.1), this is needed for interaction with middleboxes that may change "some TCP options". This is wired up to AO key flags and setsockopt() later. Similarly to TCP-MD5 hash TCP segment fragments. From this moment a user can start sending TCP-AO signed segments with one of crypto ahash algorithms from supported by Linux kernel. It can have a user-specified MAC length, to either save TCP option header space or provide higher protection using a longer signature. The inbound segments are not yet verified, TCP-AO option is ignored and they are accepted. 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 Acked-by: David Ahern --- include/net/tcp.h | 4 + include/net/tcp_ao.h | 13 ++++ net/ipv4/tcp_ao.c | 165 ++++++++++++++++++++++++++++++++++++++++++ net/ipv4/tcp_ipv4.c | 1 + net/ipv4/tcp_output.c | 111 +++++++++++++++++++++++++--- net/ipv6/tcp_ao.c | 28 +++++++ net/ipv6/tcp_ipv6.c | 2 + 7 files changed, 315 insertions(+), 9 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index 0ac0cc4186d8..73e6730fc3e6 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -186,6 +186,7 @@ void tcp_time_wait(struct sock *sk, int state, int time= o); #define TCPOPT_SACK 5 /* SACK Block */ #define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */ #define TCPOPT_MD5SIG 19 /* MD5 Signature (RFC2385) */ +#define TCPOPT_AO 29 /* Authentication Option (RFC5925) */ #define TCPOPT_MPTCP 30 /* Multipath TCP (RFC6824) */ #define TCPOPT_FASTOPEN 34 /* Fast open (RFC7413) */ #define TCPOPT_EXP 254 /* Experimental */ @@ -2159,6 +2160,9 @@ struct tcp_sock_af_ops { int (*ao_calc_key_sk)(struct tcp_ao_key *mkt, u8 *key, const struct sock *sk, __be32 sisn, __be32 disn, bool send); + int (*calc_ao_hash)(char *location, struct tcp_ao_key *ao, + const struct sock *sk, const struct sk_buff *skb, + const u8 *tkey, int hash_offset, u32 sne); #endif }; =20 diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index 7e0ce45d9934..cd85d292f78e 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -111,6 +111,10 @@ struct tcp6_ao_context { =20 struct tcp_sigpool; =20 +int tcp_ao_hash_skb(unsigned short int family, + char *ao_hash, struct tcp_ao_key *key, + const struct sock *sk, const struct sk_buff *skb, + const u8 *tkey, int hash_offset, u32 sne); int tcp_parse_ao(struct sock *sk, int cmd, unsigned short int family, sockptr_t optval, int optlen); int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx, @@ -126,12 +130,21 @@ struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock= *sk, struct sock *addr_sk, int tcp_v4_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key, const struct sock *sk, __be32 sisn, __be32 disn, bool send); +int tcp_v4_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key, + const struct sock *sk, const struct sk_buff *skb, + const u8 *tkey, int hash_offset, u32 sne); /* ipv6 specific functions */ +int tcp_v6_ao_hash_pseudoheader(struct tcp_sigpool *hp, + const struct in6_addr *daddr, + const struct in6_addr *saddr, int nbytes); int tcp_v6_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key, const struct sock *sk, __be32 sisn, __be32 disn, bool send); struct tcp_ao_key *tcp_v6_ao_lookup(const struct sock *sk, struct sock *addr_sk, int sndid, int rcvid); +int tcp_v6_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key, + const struct sock *sk, const struct sk_buff *skb, + const u8 *tkey, int hash_offset, u32 sne); int tcp_v6_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen= ); void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *skb); void tcp_ao_connect_init(struct sock *sk); diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index fc7a6aa1936e..d10daee79fe6 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -262,6 +262,171 @@ static int tcp_ao_calc_key_sk(struct tcp_ao_key *mkt,= u8 *key, return -EOPNOTSUPP; } =20 +static int tcp_v4_ao_hash_pseudoheader(struct tcp_sigpool *hp, + __be32 daddr, __be32 saddr, + int nbytes) +{ + struct tcp4_pseudohdr *bp; + struct scatterlist sg; + + bp =3D hp->scratch; + bp->saddr =3D saddr; + bp->daddr =3D daddr; + bp->pad =3D 0; + bp->protocol =3D IPPROTO_TCP; + bp->len =3D cpu_to_be16(nbytes); + + sg_init_one(&sg, bp, sizeof(*bp)); + ahash_request_set_crypt(hp->req, &sg, NULL, sizeof(*bp)); + return crypto_ahash_update(hp->req); +} + +static int tcp_ao_hash_pseudoheader(unsigned short int family, + const struct sock *sk, + const struct sk_buff *skb, + struct tcp_sigpool *hp, int nbytes) +{ + const struct tcphdr *th =3D tcp_hdr(skb); + + /* TODO: Can we rely on checksum being zero to mean outbound pkt? */ + if (!th->check) { + if (family =3D=3D AF_INET) + return tcp_v4_ao_hash_pseudoheader(hp, sk->sk_daddr, + sk->sk_rcv_saddr, skb->len); +#if IS_ENABLED(CONFIG_IPV6) + else if (family =3D=3D AF_INET6) + return tcp_v6_ao_hash_pseudoheader(hp, &sk->sk_v6_daddr, + &sk->sk_v6_rcv_saddr, skb->len); +#endif + else + return -EAFNOSUPPORT; + } + + if (family =3D=3D AF_INET) { + const struct iphdr *iph =3D ip_hdr(skb); + + return tcp_v4_ao_hash_pseudoheader(hp, iph->daddr, + iph->saddr, skb->len); +#if IS_ENABLED(CONFIG_IPV6) + } else if (family =3D=3D AF_INET6) { + const struct ipv6hdr *iph =3D ipv6_hdr(skb); + + return tcp_v6_ao_hash_pseudoheader(hp, &iph->daddr, + &iph->saddr, skb->len); +#endif + } + return -EAFNOSUPPORT; +} + +/* tcp_ao_hash_sne(struct tcp_sigpool *hp) + * @hp - used for hashing + * @sne - sne value + */ +static int tcp_ao_hash_sne(struct tcp_sigpool *hp, u32 sne) +{ + struct scatterlist sg; + __be32 *bp; + + bp =3D (__be32 *)hp->scratch; + *bp =3D htonl(sne); + + sg_init_one(&sg, bp, sizeof(*bp)); + ahash_request_set_crypt(hp->req, &sg, NULL, sizeof(*bp)); + return crypto_ahash_update(hp->req); +} + +static int tcp_ao_hash_header(struct tcp_sigpool *hp, + const struct tcphdr *th, + bool exclude_options, u8 *hash, + int hash_offset, int hash_len) +{ + int err, len =3D th->doff << 2; + struct scatterlist sg; + u8 *hdr =3D hp->scratch; + + /* We are not allowed to change tcphdr, make a local copy */ + if (exclude_options) { + len =3D sizeof(*th) + sizeof(struct tcp_ao_hdr) + hash_len; + memcpy(hdr, th, sizeof(*th)); + memcpy(hdr + sizeof(*th), + (u8 *)th + hash_offset - sizeof(struct tcp_ao_hdr), + sizeof(struct tcp_ao_hdr)); + memset(hdr + sizeof(*th) + sizeof(struct tcp_ao_hdr), + 0, hash_len); + ((struct tcphdr *)hdr)->check =3D 0; + } else { + len =3D th->doff << 2; + memcpy(hdr, th, len); + /* zero out tcp-ao hash */ + ((struct tcphdr *)hdr)->check =3D 0; + memset(hdr + hash_offset, 0, hash_len); + } + + sg_init_one(&sg, hdr, len); + ahash_request_set_crypt(hp->req, &sg, NULL, len); + err =3D crypto_ahash_update(hp->req); + WARN_ON_ONCE(err !=3D 0); + return err; +} + +int tcp_ao_hash_skb(unsigned short int family, + char *ao_hash, struct tcp_ao_key *key, + const struct sock *sk, const struct sk_buff *skb, + const u8 *tkey, int hash_offset, u32 sne) +{ + const struct tcphdr *th =3D tcp_hdr(skb); + int tkey_len =3D tcp_ao_digest_size(key); + struct tcp_sigpool hp; + void *hash_buf =3D NULL; + + hash_buf =3D kmalloc(tkey_len, GFP_ATOMIC); + if (!hash_buf) + goto clear_hash_noput; + + if (tcp_sigpool_start(key->tcp_sigpool_id, &hp)) + goto clear_hash_noput; + + if (crypto_ahash_setkey(crypto_ahash_reqtfm(hp.req), tkey, tkey_len)) + goto clear_hash; + + /* For now use sha1 by default. Depends on alg in tcp_ao_key */ + if (crypto_ahash_init(hp.req)) + goto clear_hash; + + if (tcp_ao_hash_sne(&hp, sne)) + goto clear_hash; + if (tcp_ao_hash_pseudoheader(family, sk, skb, &hp, skb->len)) + goto clear_hash; + if (tcp_ao_hash_header(&hp, th, false, + ao_hash, hash_offset, tcp_ao_maclen(key))) + goto clear_hash; + if (tcp_sigpool_hash_skb_data(&hp, skb, th->doff << 2)) + goto clear_hash; + ahash_request_set_crypt(hp.req, NULL, hash_buf, 0); + if (crypto_ahash_final(hp.req)) + goto clear_hash; + + memcpy(ao_hash, hash_buf, tcp_ao_maclen(key)); + tcp_sigpool_end(&hp); + kfree(hash_buf); + return 0; + +clear_hash: + tcp_sigpool_end(&hp); +clear_hash_noput: + memset(ao_hash, 0, tcp_ao_maclen(key)); + kfree(hash_buf); + return 1; +} + +int tcp_v4_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key, + const struct sock *sk, const struct sk_buff *skb, + const u8 *tkey, int hash_offset, u32 sne) +{ + return tcp_ao_hash_skb(AF_INET, ao_hash, key, sk, skb, + tkey, hash_offset, sne); +} + struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *ad= dr_sk, int sndid, int rcvid) { diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index f5699c6fa007..0750e8160a54 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -2285,6 +2285,7 @@ static const struct tcp_sock_af_ops tcp_sock_ipv4_spe= cific =3D { #endif #ifdef CONFIG_TCP_AO .ao_lookup =3D tcp_v4_ao_lookup, + .calc_ao_hash =3D tcp_v4_ao_hash_skb, .ao_parse =3D tcp_v4_parse_ao, .ao_calc_key_sk =3D tcp_v4_ao_calc_key_sk, #endif diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 895d902686fa..5804f50bb747 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -423,6 +423,7 @@ static inline bool tcp_urg_mode(const struct tcp_sock *= tp) #define OPTION_FAST_OPEN_COOKIE BIT(8) #define OPTION_SMC BIT(9) #define OPTION_MPTCP BIT(10) +#define OPTION_AO BIT(11) =20 static void smc_options_write(__be32 *ptr, u16 *options) { @@ -615,7 +616,8 @@ static void bpf_skops_write_hdr_opt(struct sock *sk, st= ruct sk_buff *skb, * (but it may well be that other scenarios fail similarly). */ static void tcp_options_write(struct tcphdr *th, struct tcp_sock *tp, - struct tcp_out_options *opts) + struct tcp_out_options *opts, + struct tcp_ao_key *ao_key) { __be32 *ptr =3D (__be32 *)(th + 1); u16 options =3D opts->options; /* mungable copy */ @@ -627,7 +629,33 @@ static void tcp_options_write(struct tcphdr *th, struc= t tcp_sock *tp, opts->hash_location =3D (__u8 *)ptr; ptr +=3D 4; } +#ifdef CONFIG_TCP_AO + if (unlikely(OPTION_AO & options)) { + struct tcp_ao_key *rnext_key; + struct tcp_ao_info *ao_info; + u8 maclen; =20 + if (WARN_ON_ONCE(!ao_key)) + goto out_ao; + ao_info =3D rcu_dereference_check(tp->ao_info, + lockdep_sock_is_held(&tp->inet_conn.icsk_inet.sk)); + rnext_key =3D READ_ONCE(ao_info->rnext_key); + if (WARN_ON_ONCE(!rnext_key)) + goto out_ao; + maclen =3D tcp_ao_maclen(ao_key); + *ptr++ =3D htonl((TCPOPT_AO << 24) | + (tcp_ao_len(ao_key) << 16) | + (ao_key->sndid << 8) | + (rnext_key->rcvid)); + opts->hash_location =3D (__u8 *)ptr; + ptr +=3D maclen / sizeof(*ptr); + if (unlikely(maclen % sizeof(*ptr))) { + memset(ptr, TCPOPT_NOP, sizeof(*ptr)); + ptr++; + } + } +out_ao: +#endif if (unlikely(opts->mss)) { *ptr++ =3D htonl((TCPOPT_MSS << 24) | (TCPOLEN_MSS << 16) | @@ -768,7 +796,8 @@ static void mptcp_set_option_cond(const struct request_= sock *req, */ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb, struct tcp_out_options *opts, - struct tcp_md5sig_key **md5) + struct tcp_md5sig_key **md5, + struct tcp_ao_key *ao_key) { struct tcp_sock *tp =3D tcp_sk(sk); unsigned int remaining =3D MAX_TCP_OPTION_SPACE; @@ -785,6 +814,12 @@ static unsigned int tcp_syn_options(struct sock *sk, s= truct sk_buff *skb, } } #endif +#ifdef CONFIG_TCP_AO + if (ao_key) { + opts->options |=3D OPTION_AO; + remaining -=3D tcp_ao_len(ao_key); + } +#endif =20 /* We always get an MSS option. The option bytes which will be seen in * normal data packets should timestamps be used, must be in the MSS @@ -922,7 +957,8 @@ static unsigned int tcp_synack_options(const struct soc= k *sk, */ static unsigned int tcp_established_options(struct sock *sk, struct sk_buf= f *skb, struct tcp_out_options *opts, - struct tcp_md5sig_key **md5) + struct tcp_md5sig_key **md5, + struct tcp_ao_key *ao_key) { struct tcp_sock *tp =3D tcp_sk(sk); unsigned int size =3D 0; @@ -941,6 +977,12 @@ static unsigned int tcp_established_options(struct soc= k *sk, struct sk_buff *skb } } #endif +#ifdef CONFIG_TCP_AO + if (ao_key) { + opts->options |=3D OPTION_AO; + size +=3D tcp_ao_len(ao_key); + } +#endif =20 if (likely(tp->rx_opt.tstamp_ok)) { opts->options |=3D OPTION_TS; @@ -1245,6 +1287,7 @@ static int __tcp_transmit_skb(struct sock *sk, struct= sk_buff *skb, int clone_it, gfp_t gfp_mask, u32 rcv_nxt) { const struct inet_connection_sock *icsk =3D inet_csk(sk); + struct tcp_ao_key *ao_key =3D NULL; struct inet_sock *inet; struct tcp_sock *tp; struct tcp_skb_cb *tcb; @@ -1252,6 +1295,9 @@ static int __tcp_transmit_skb(struct sock *sk, struct= sk_buff *skb, unsigned int tcp_options_size, tcp_header_size; struct sk_buff *oskb =3D NULL; struct tcp_md5sig_key *md5; +#ifdef CONFIG_TCP_AO + struct tcp_ao_info *ao; +#endif struct tcphdr *th; u64 prior_wstamp; int err; @@ -1283,11 +1329,17 @@ static int __tcp_transmit_skb(struct sock *sk, stru= ct sk_buff *skb, tcb =3D TCP_SKB_CB(skb); memset(&opts, 0, sizeof(opts)); =20 +#ifdef CONFIG_TCP_AO + ao =3D rcu_dereference_protected(tcp_sk(sk)->ao_info, + lockdep_sock_is_held(sk)); + if (ao) + ao_key =3D READ_ONCE(ao->current_key); +#endif if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) { - tcp_options_size =3D tcp_syn_options(sk, skb, &opts, &md5); + tcp_options_size =3D tcp_syn_options(sk, skb, &opts, &md5, ao_key); } else { tcp_options_size =3D tcp_established_options(sk, skb, &opts, - &md5); + &md5, ao_key); /* Force a PSH flag on all (GSO) packets to expedite GRO flush * at receiver : This slightly improve GRO performance. * Note that we do not force the PSH flag for non GSO packets, @@ -1361,7 +1413,7 @@ static int __tcp_transmit_skb(struct sock *sk, struct= sk_buff *skb, th->window =3D htons(min(tp->rcv_wnd, 65535U)); } =20 - tcp_options_write(th, tp, &opts); + tcp_options_write(th, tp, &opts, ao_key); =20 #ifdef CONFIG_TCP_MD5SIG /* Calculate the MD5 hash, as we have all we need now */ @@ -1371,6 +1423,34 @@ static int __tcp_transmit_skb(struct sock *sk, struc= t sk_buff *skb, md5, sk, skb); } #endif +#ifdef CONFIG_TCP_AO + if (ao) { + u8 *traffic_key; + void *tkey_buf =3D NULL; + __be32 disn; + + sk_gso_disable(sk); + if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) { + if (tcb->tcp_flags & TCPHDR_ACK) + disn =3D ao->risn; + else + disn =3D 0; + + tkey_buf =3D kmalloc(tcp_ao_digest_size(ao_key), GFP_ATOMIC); + if (!tkey_buf) + return -ENOMEM; + traffic_key =3D tkey_buf; + tp->af_specific->ao_calc_key_sk(ao_key, traffic_key, + sk, ao->lisn, disn, true); + } else { + traffic_key =3D snd_other_key(ao_key); + } + tp->af_specific->calc_ao_hash(opts.hash_location, ao_key, sk, skb, + traffic_key, + opts.hash_location - (u8 *)th, 0); + kfree(tkey_buf); + } +#endif =20 /* BPF prog is the last one writing header option */ bpf_skops_write_hdr_opt(sk, skb, NULL, NULL, 0, &opts); @@ -1818,10 +1898,14 @@ unsigned int tcp_current_mss(struct sock *sk) { const struct tcp_sock *tp =3D tcp_sk(sk); const struct dst_entry *dst =3D __sk_dst_get(sk); + struct tcp_ao_key *ao_key =3D NULL; u32 mss_now; unsigned int header_len; struct tcp_out_options opts; struct tcp_md5sig_key *md5; +#ifdef CONFIG_TCP_AO + struct tcp_ao_info *ao_info; +#endif =20 mss_now =3D tp->mss_cache; =20 @@ -1830,8 +1914,17 @@ unsigned int tcp_current_mss(struct sock *sk) if (mtu !=3D inet_csk(sk)->icsk_pmtu_cookie) mss_now =3D tcp_sync_mss(sk, mtu); } - - header_len =3D tcp_established_options(sk, NULL, &opts, &md5) + +#ifdef CONFIG_TCP_AO + ao_info =3D rcu_dereference_check(tp->ao_info, lockdep_sock_is_held(sk)); + if (ao_info) + /* TODO: verify if we can access current_key or we need to pass + * it from every caller of tcp_current_mss instead. The reason + * is that the current_key pointer can change asynchronously + * from the rx path. + */ + ao_key =3D READ_ONCE(ao_info->current_key); +#endif + header_len =3D tcp_established_options(sk, NULL, &opts, &md5, ao_key) + sizeof(struct tcphdr); /* The mss_cache is sized based on tp->tcp_header_len, which assumes * some common options. If this is an odd packet (because we have SACK @@ -3693,7 +3786,7 @@ struct sk_buff *tcp_make_synack(const struct sock *sk= , struct dst_entry *dst, =20 /* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */ th->window =3D htons(min(req->rsk_rcv_wnd, 65535U)); - tcp_options_write(th, NULL, &opts); + tcp_options_write(th, NULL, &opts, NULL); th->doff =3D (tcp_header_size >> 2); TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTSEGS); =20 diff --git a/net/ipv6/tcp_ao.c b/net/ipv6/tcp_ao.c index 9ab594fadbd9..d08735b6f3c5 100644 --- a/net/ipv6/tcp_ao.c +++ b/net/ipv6/tcp_ao.c @@ -7,6 +7,7 @@ * Francesco Ruggeri * Salam Noureddine */ +#include #include =20 #include @@ -79,6 +80,33 @@ struct tcp_ao_key *tcp_v6_ao_lookup(const struct sock *s= k, return tcp_v6_ao_do_lookup(sk, addr, sndid, rcvid); } =20 +int tcp_v6_ao_hash_pseudoheader(struct tcp_sigpool *hp, + const struct in6_addr *daddr, + const struct in6_addr *saddr, int nbytes) +{ + struct tcp6_pseudohdr *bp; + struct scatterlist sg; + + bp =3D hp->scratch; + /* 1. TCP pseudo-header (RFC2460) */ + bp->saddr =3D *saddr; + bp->daddr =3D *daddr; + bp->len =3D cpu_to_be32(nbytes); + bp->protocol =3D cpu_to_be32(IPPROTO_TCP); + + sg_init_one(&sg, bp, sizeof(*bp)); + ahash_request_set_crypt(hp->req, &sg, NULL, sizeof(*bp)); + return crypto_ahash_update(hp->req); +} + +int tcp_v6_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key, + const struct sock *sk, const struct sk_buff *skb, + const u8 *tkey, int hash_offset, u32 sne) +{ + return tcp_ao_hash_skb(AF_INET6, ao_hash, key, sk, skb, tkey, + hash_offset, sne); +} + int tcp_v6_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen) { diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 70a3f12a94a5..146b586839f5 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -1917,6 +1917,7 @@ static const struct tcp_sock_af_ops tcp_sock_ipv6_spe= cific =3D { #endif #ifdef CONFIG_TCP_AO .ao_lookup =3D tcp_v6_ao_lookup, + .calc_ao_hash =3D tcp_v6_ao_hash_skb, .ao_parse =3D tcp_v6_parse_ao, .ao_calc_key_sk =3D tcp_v6_ao_calc_key_sk, #endif @@ -1950,6 +1951,7 @@ static const struct tcp_sock_af_ops tcp_sock_ipv6_map= ped_specific =3D { #endif #ifdef CONFIG_TCP_AO .ao_lookup =3D tcp_v6_ao_lookup, + .calc_ao_hash =3D tcp_v4_ao_hash_skb, .ao_parse =3D tcp_v6_parse_ao, #endif }; --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9ABA0C04A6A for ; Tue, 15 Aug 2023 19:16:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239895AbjHOTP4 (ORCPT ); Tue, 15 Aug 2023 15:15:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239781AbjHOTPT (ORCPT ); Tue, 15 Aug 2023 15:15:19 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0392410EC for ; Tue, 15 Aug 2023 12:15:17 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-3fe4cdb72b9so52264065e9.0 for ; Tue, 15 Aug 2023 12:15:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126915; x=1692731715; 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=D6J1bByRjPegO+0wzvVIFtgk8o6BL03TlwNO7VElBHs=; b=dwtkCf7sn0gW+w3Mg0lD7mXxHhvIeccEDX0FbuKYpPEfk0bTrO4Z32XFPEv5VzUKtX QzDzMDc/fjEn3Rm1LNvPazIXaUH95HLGq8DVrUQ3DjfTJLJQ2mu/PDkGj07F6yucP6fI eTOTKf7nECHQiy0fQ9wsLWVFbfJtS7x8oW9ZELg/JCXAxppvXiPKEnqrnLDzLdkuf0Wv /QqecOGM+HWCcCOdDAnSGDdr0prRBKrMsObD0Mh95QonlQUIl16DtXYXwrsRnOEry67B y7jGs6fnvpYltD3uEMo1wC9QQyRVWao5DJ7jzQsRExvX7YWDxuetVhB+KdroW6CCYGN7 Dtvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126915; x=1692731715; 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=D6J1bByRjPegO+0wzvVIFtgk8o6BL03TlwNO7VElBHs=; b=YsovI14rRLVPnClHopjVhaTBv2MUT6GG9tnE0ynOyKUsmCiI4ieebrSRbpojjCazbL X6re95FJ5WaXxBjCga32MJBvjNLYEwTc9cPAjFsGKEyowxYTWnTUWIEEFJ997ZvZiy3F H8cb58Nt1VCiEYYdZ4NgDkOvZRiazGtg4sJ36EjigHLYbv7cPAEqdg9+sRGIkQEe/zTK mwihDuHuJwNrJ2gFWOihEE+SaknY43iVtfG0Op4dDgNlkd3wA+QiBPevhFixvF/gNIbG vLI1BJQYTE5wa5RTtSaxeVLVfQlKKQ6q3UvMvijITTDjlIMPHzDBmyeUQ5Uiqe6Tsh8p +oyA== X-Gm-Message-State: AOJu0YxWDpFV0WHM41/vvVG4yBC9ymMckhciT6b9QEi8+O8X9m3NZNQu vF80VjN8oR6mfcrpxyR3Szh6/Q== X-Google-Smtp-Source: AGHT+IGd5EEt5Ul+bWrOPb4Ft4ckTz9spPYn3NOT3cT1OT3djo6auZzFXEaBU1j+iR5BHJeoyIJOYA== X-Received: by 2002:a05:600c:280b:b0:3fe:16c8:65fa with SMTP id m11-20020a05600c280b00b003fe16c865famr10643645wmb.4.1692126915490; Tue, 15 Aug 2023 12:15:15 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15: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 v10 net-next 07/23] net/tcp: Add tcp_parse_auth_options() Date: Tue, 15 Aug 2023 20:14:36 +0100 Message-ID: <20230815191455.1872316-8-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Introduce a helper that: (1) shares the common code with TCP-MD5 header options parsing (2) looks for hash signature only once for both TCP-MD5 and TCP-AO (3) fails with -EEXIST if any TCP sign option is present twice, see RFC5925 (2.2): ">> A single TCP segment MUST NOT have more than one TCP-AO in its options sequence. When multiple TCP-AOs appear, TCP MUST discard the segment." 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 Acked-by: David Ahern --- include/net/dropreason-core.h | 6 ++++++ include/net/tcp.h | 24 ++++++++++++++++++++- include/net/tcp_ao.h | 17 ++++++++++++++- net/ipv4/tcp.c | 3 ++- net/ipv4/tcp_input.c | 39 ++++++++++++++++++++++++++--------- net/ipv4/tcp_ipv4.c | 15 +++++++++----- net/ipv6/tcp_ipv6.c | 11 ++++++---- 7 files changed, 93 insertions(+), 22 deletions(-) diff --git a/include/net/dropreason-core.h b/include/net/dropreason-core.h index f291a3b0f9e5..05255bffa2c8 100644 --- a/include/net/dropreason-core.h +++ b/include/net/dropreason-core.h @@ -20,6 +20,7 @@ FN(IP_NOPROTO) \ FN(SOCKET_RCVBUFF) \ FN(PROTO_MEM) \ + FN(TCP_AUTH_HDR) \ FN(TCP_MD5NOTFOUND) \ FN(TCP_MD5UNEXPECTED) \ FN(TCP_MD5FAILURE) \ @@ -140,6 +141,11 @@ enum skb_drop_reason { * drop out of udp_memory_allocated. */ SKB_DROP_REASON_PROTO_MEM, + /** + * @SKB_DROP_REASON_TCP_AUTH_HDR: TCP-MD5 or TCP-AO hashes are met + * twice or set incorrectly. + */ + SKB_DROP_REASON_TCP_AUTH_HDR, /** * @SKB_DROP_REASON_TCP_MD5NOTFOUND: no MD5 hash and one expected, * corresponding to LINUX_MIB_TCPMD5NOTFOUND diff --git a/include/net/tcp.h b/include/net/tcp.h index 73e6730fc3e6..a09b5b53a834 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -427,7 +427,6 @@ int tcp_mmap(struct file *file, struct socket *sock, void tcp_parse_options(const struct net *net, const struct sk_buff *skb, struct tcp_options_received *opt_rx, int estab, struct tcp_fastopen_cookie *foc); -const u8 *tcp_parse_md5sig_option(const struct tcphdr *th); =20 /* * BPF SKB-less helpers @@ -2574,6 +2573,29 @@ static inline u64 tcp_transmit_time(const struct soc= k *sk) return 0; } =20 +static inline int tcp_parse_auth_options(const struct tcphdr *th, + const u8 **md5_hash, const struct tcp_ao_hdr **aoh) +{ + const u8 *md5_tmp, *ao_tmp; + int ret; + + ret =3D tcp_do_parse_auth_options(th, &md5_tmp, &ao_tmp); + if (ret) + return ret; + + if (md5_hash) + *md5_hash =3D md5_tmp; + + if (aoh) { + if (!ao_tmp) + *aoh =3D NULL; + else + *aoh =3D (struct tcp_ao_hdr *)(ao_tmp - 2); + } + + return 0; +} + static inline bool tcp_ao_required(struct sock *sk, const void *saddr, int family) { diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index cd85d292f78e..e685ad9db949 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -148,7 +148,9 @@ int tcp_v6_ao_hash_skb(char *ao_hash, struct tcp_ao_key= *key, int tcp_v6_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen= ); void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *skb); void tcp_ao_connect_init(struct sock *sk); - +void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb, + struct tcp_request_sock *treq, + unsigned short int family); #else /* CONFIG_TCP_AO */ =20 static inline struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, @@ -170,4 +172,17 @@ static inline void tcp_ao_connect_init(struct sock *sk) } #endif =20 +#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) +int tcp_do_parse_auth_options(const struct tcphdr *th, + const u8 **md5_hash, const u8 **ao_hash); +#else +static inline int tcp_do_parse_auth_options(const struct tcphdr *th, + const u8 **md5_hash, const u8 **ao_hash) +{ + *md5_hash =3D NULL; + *ao_hash =3D NULL; + return 0; +} +#endif + #endif /* _TCP_AO_H */ diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 4d0536ff75d8..9a6a8b79dad4 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -4383,7 +4383,8 @@ tcp_inbound_md5_hash(const struct sock *sk, const str= uct sk_buff *skb, l3index =3D sdif ? dif : 0; =20 hash_expected =3D tcp_md5_do_lookup(sk, l3index, saddr, family); - hash_location =3D tcp_parse_md5sig_option(th); + if (tcp_parse_auth_options(th, &hash_location, NULL)) + return SKB_DROP_REASON_TCP_AUTH_HDR; =20 /* We've parsed the options - do we have a hash? */ if (!hash_expected && !hash_location) diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 0df208d6fa44..de6d55f46aa8 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -4211,39 +4211,58 @@ static bool tcp_fast_parse_options(const struct net= *net, return true; } =20 -#ifdef CONFIG_TCP_MD5SIG +#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) /* - * Parse MD5 Signature option + * Parse Signature options */ -const u8 *tcp_parse_md5sig_option(const struct tcphdr *th) +int tcp_do_parse_auth_options(const struct tcphdr *th, + const u8 **md5_hash, const u8 **ao_hash) { int length =3D (th->doff << 2) - sizeof(*th); const u8 *ptr =3D (const u8 *)(th + 1); + unsigned int minlen =3D TCPOLEN_MD5SIG; + + if (IS_ENABLED(CONFIG_TCP_AO)) + minlen =3D sizeof(struct tcp_ao_hdr) + 1; + + *md5_hash =3D NULL; + *ao_hash =3D NULL; =20 /* If not enough data remaining, we can short cut */ - while (length >=3D TCPOLEN_MD5SIG) { + while (length >=3D minlen) { int opcode =3D *ptr++; int opsize; =20 switch (opcode) { case TCPOPT_EOL: - return NULL; + return 0; case TCPOPT_NOP: length--; continue; default: opsize =3D *ptr++; if (opsize < 2 || opsize > length) - return NULL; - if (opcode =3D=3D TCPOPT_MD5SIG) - return opsize =3D=3D TCPOLEN_MD5SIG ? ptr : NULL; + return -EINVAL; + if (opcode =3D=3D TCPOPT_MD5SIG) { + if (opsize !=3D TCPOLEN_MD5SIG) + return -EINVAL; + if (unlikely(*md5_hash || *ao_hash)) + return -EEXIST; + *md5_hash =3D ptr; + } else if (opcode =3D=3D TCPOPT_AO) { + if (opsize <=3D sizeof(struct tcp_ao_hdr)) + return -EINVAL; + if (unlikely(*md5_hash || *ao_hash)) + return -EEXIST; + *ao_hash =3D ptr; + } } ptr +=3D opsize - 2; length -=3D opsize; } - return NULL; + return 0; } -EXPORT_SYMBOL(tcp_parse_md5sig_option); +EXPORT_SYMBOL(tcp_do_parse_auth_options); #endif =20 /* Sorry, PAWS as specified is broken wrt. pure-ACKs -DaveM diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 0750e8160a54..31169971cc56 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -670,7 +670,9 @@ EXPORT_SYMBOL(tcp_v4_send_check); * Exception: precedence violation. We do not implement it in any case. */ =20 -#ifdef CONFIG_TCP_MD5SIG +#ifdef CONFIG_TCP_AO +#define OPTION_BYTES MAX_TCP_OPTION_SPACE +#elif defined(CONFIG_TCP_MD5SIG) #define OPTION_BYTES TCPOLEN_MD5SIG_ALIGNED #else #define OPTION_BYTES sizeof(__be32) @@ -685,8 +687,8 @@ static void tcp_v4_send_reset(const struct sock *sk, st= ruct sk_buff *skb) } rep; struct ip_reply_arg arg; #ifdef CONFIG_TCP_MD5SIG + const __u8 *md5_hash_location =3D NULL; struct tcp_md5sig_key *key =3D NULL; - const __u8 *hash_location =3D NULL; unsigned char newhash[16]; int genhash; struct sock *sk1 =3D NULL; @@ -727,8 +729,11 @@ static void tcp_v4_send_reset(const struct sock *sk, s= truct sk_buff *skb) =20 net =3D sk ? sock_net(sk) : dev_net(skb_dst(skb)->dev); #ifdef CONFIG_TCP_MD5SIG + /* Invalid TCP option size or twice included auth */ + if (tcp_parse_auth_options(tcp_hdr(skb), &md5_hash_location, NULL)) + return; + rcu_read_lock(); - hash_location =3D tcp_parse_md5sig_option(th); if (sk && sk_fullsock(sk)) { const union tcp_md5_addr *addr; int l3index; @@ -739,7 +744,7 @@ static void tcp_v4_send_reset(const struct sock *sk, st= ruct sk_buff *skb) l3index =3D tcp_v4_sdif(skb) ? inet_iif(skb) : 0; addr =3D (union tcp_md5_addr *)&ip_hdr(skb)->saddr; key =3D tcp_md5_do_lookup(sk, l3index, addr, AF_INET); - } else if (hash_location) { + } else if (md5_hash_location) { const union tcp_md5_addr *addr; int sdif =3D tcp_v4_sdif(skb); int dif =3D inet_iif(skb); @@ -771,7 +776,7 @@ static void tcp_v4_send_reset(const struct sock *sk, st= ruct sk_buff *skb) =20 =20 genhash =3D tcp_v4_md5_hash_skb(newhash, key, NULL, skb); - if (genhash || memcmp(hash_location, newhash, 16) !=3D 0) + if (genhash || memcmp(md5_hash_location, newhash, 16) !=3D 0) goto out; =20 } diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 146b586839f5..5c1880d5f5bc 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -989,7 +989,7 @@ static void tcp_v6_send_reset(const struct sock *sk, st= ruct sk_buff *skb) u32 seq =3D 0, ack_seq =3D 0; struct tcp_md5sig_key *key =3D NULL; #ifdef CONFIG_TCP_MD5SIG - const __u8 *hash_location =3D NULL; + const __u8 *md5_hash_location =3D NULL; unsigned char newhash[16]; int genhash; struct sock *sk1 =3D NULL; @@ -1011,8 +1011,11 @@ static void tcp_v6_send_reset(const struct sock *sk,= struct sk_buff *skb) =20 net =3D sk ? sock_net(sk) : dev_net(skb_dst(skb)->dev); #ifdef CONFIG_TCP_MD5SIG + /* Invalid TCP option size or twice included auth */ + if (tcp_parse_auth_options(th, &md5_hash_location, NULL)) + return; + rcu_read_lock(); - hash_location =3D tcp_parse_md5sig_option(th); if (sk && sk_fullsock(sk)) { int l3index; =20 @@ -1021,7 +1024,7 @@ static void tcp_v6_send_reset(const struct sock *sk, = struct sk_buff *skb) */ l3index =3D tcp_v6_sdif(skb) ? tcp_v6_iif_l3_slave(skb) : 0; key =3D tcp_v6_md5_do_lookup(sk, &ipv6h->saddr, l3index); - } else if (hash_location) { + } else if (md5_hash_location) { int dif =3D tcp_v6_iif_l3_slave(skb); int sdif =3D tcp_v6_sdif(skb); int l3index; @@ -1050,7 +1053,7 @@ static void tcp_v6_send_reset(const struct sock *sk, = struct sk_buff *skb) goto out; =20 genhash =3D tcp_v6_md5_hash_skb(newhash, key, NULL, skb); - if (genhash || memcmp(hash_location, newhash, 16) !=3D 0) + if (genhash || memcmp(md5_hash_location, newhash, 16) !=3D 0) goto out; } #endif --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB2B4C04A94 for ; Tue, 15 Aug 2023 19:16:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239933AbjHOTP7 (ORCPT ); Tue, 15 Aug 2023 15:15:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239855AbjHOTPW (ORCPT ); Tue, 15 Aug 2023 15:15:22 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ECA43B8 for ; Tue, 15 Aug 2023 12:15:18 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-319779f0347so1956468f8f.1 for ; Tue, 15 Aug 2023 12:15:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126917; x=1692731717; 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=t9gzfhjgp1wlG3rKDBto+cSreKv/hJcxglDoyD+2vns=; b=KbDkloYFx9MPW56OVEdViiB2b9oHF4n7GUCB5OXedA9Ao2lKx/x47LWgiwSyMH9ngG revO5Vb5OsJgX7KktcGl11nzZGjEIL2hgyj5E7UI9QS6zker0RLCbU9mZhn5cO2AOOsp 82NZz/R9gLSp+ejLMUegEgFUn2Z65UhcAJ2euiCT1UCY7+SQbJfw/gs6mJ/ebCgGpCXt 1RHH3m0u/RzC+AjfYPVK/oVzDLNg3WzrwCth77ZgySxpzR26NQ4ONu8DifITnJJAMOfN squ4iB0p4tjax5BKa0DCwRIs1nO8pBZ1+hoVKY/iij4rIzl3CBAK3BWQX16BMcdXIzqX KGZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126917; x=1692731717; 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=t9gzfhjgp1wlG3rKDBto+cSreKv/hJcxglDoyD+2vns=; b=ZZ6fZMWK3k26vFD62vAP69TC7fvQtal9divBMKwT4Clieo0fUi0Q15zIbNGio0dADe LUCAabFUTdWrkvcMSqgNARsEKTnYGdiShOW7gMXEbB7zrkrypNLmmdJ0VtAWm5guzqgX VxLz/vCRfv8hpnTwZ3jwAI67IpdoNQHqIVQ+uGLWWZELGr9SBLf2MUEBnlBpeflbYJ+l wgtsorvu//nPU4rn4qKnEYIhCHTfrnBe1mNv4ooQVZrkBrD83HZS3aNUmLKfER1bw2Zn 5XtZiaudryGv/WshY4jf9jRu5rC8asSkhagBsRfDjtCLFWpWjaYgbR8alCc3juPN3O/y cq9g== X-Gm-Message-State: AOJu0YzmcL47Oyyp3JOlg5T7abgZDn+V92EeXhTptwZ180/RQva5dsIJ eyJz8k1GU1iL6pmoTbjYpIUjJQ== X-Google-Smtp-Source: AGHT+IFAaRq3KNxvrbLHHqfr2cv7G92RzsNJe+KYFtw3OTY5/IDypQv8y7YMxqV+tNZ6qLk3m7Jw9Q== X-Received: by 2002:a5d:4c86:0:b0:31a:d31:dbf9 with SMTP id z6-20020a5d4c86000000b0031a0d31dbf9mr115299wrs.49.1692126917425; Tue, 15 Aug 2023 12:15:17 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:16 -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 v10 net-next 08/23] net/tcp: Add AO sign to RST packets Date: Tue, 15 Aug 2023 20:14:37 +0100 Message-ID: <20230815191455.1872316-9-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Wire up sending resets to TCP-AO hashing. 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 Acked-by: David Ahern --- include/net/tcp_ao.h | 12 +++++ net/ipv4/tcp_ao.c | 104 ++++++++++++++++++++++++++++++++++++++++++- net/ipv4/tcp_ipv4.c | 69 ++++++++++++++++++++++------ net/ipv6/tcp_ipv6.c | 70 ++++++++++++++++++++++------- 4 files changed, 225 insertions(+), 30 deletions(-) diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index e685ad9db949..67f997aabd9c 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -117,12 +117,24 @@ int tcp_ao_hash_skb(unsigned short int family, const u8 *tkey, int hash_offset, u32 sne); int tcp_parse_ao(struct sock *sk, int cmd, unsigned short int family, sockptr_t optval, int optlen); +struct tcp_ao_key *tcp_ao_established_key(struct tcp_ao_info *ao, + int sndid, int rcvid); int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx, unsigned int len, struct tcp_sigpool *hp); void tcp_ao_destroy_sock(struct sock *sk); struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, const union tcp_ao_addr *addr, int family, int sndid, int rcvid); +int tcp_ao_hash_hdr(unsigned short family, char *ao_hash, + struct tcp_ao_key *key, const u8 *tkey, + const union tcp_ao_addr *daddr, + const union tcp_ao_addr *saddr, + const struct tcphdr *th, u32 sne); +int tcp_ao_prepare_reset(const struct sock *sk, struct sk_buff *skb, + const struct tcp_ao_hdr *aoh, int l3index, u32 seq, + struct tcp_ao_key **key, char **traffic_key, + bool *allocated_traffic_key, u8 *keyid, u32 *sne); + /* ipv4 specific functions */ int tcp_v4_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen= ); struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *ad= dr_sk, diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index d10daee79fe6..81ce4fe546f2 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -48,8 +48,8 @@ int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *k= ey, void *ctx, * it's known that the keys in ao_info are matching peer's * family/address/VRF/etc. */ -static struct tcp_ao_key *tcp_ao_established_key(struct tcp_ao_info *ao, - int sndid, int rcvid) +struct tcp_ao_key *tcp_ao_established_key(struct tcp_ao_info *ao, + int sndid, int rcvid) { struct tcp_ao_key *key; =20 @@ -369,6 +369,66 @@ static int tcp_ao_hash_header(struct tcp_sigpool *hp, return err; } =20 +int tcp_ao_hash_hdr(unsigned short int family, char *ao_hash, + struct tcp_ao_key *key, const u8 *tkey, + const union tcp_ao_addr *daddr, + const union tcp_ao_addr *saddr, + const struct tcphdr *th, u32 sne) +{ + int tkey_len =3D tcp_ao_digest_size(key); + int hash_offset =3D ao_hash - (char *)th; + struct tcp_sigpool hp; + void *hash_buf =3D NULL; + + hash_buf =3D kmalloc(tkey_len, GFP_ATOMIC); + if (!hash_buf) + goto clear_hash_noput; + + if (tcp_sigpool_start(key->tcp_sigpool_id, &hp)) + goto clear_hash_noput; + + if (crypto_ahash_setkey(crypto_ahash_reqtfm(hp.req), tkey, tkey_len)) + goto clear_hash; + + if (crypto_ahash_init(hp.req)) + goto clear_hash; + + if (tcp_ao_hash_sne(&hp, sne)) + goto clear_hash; + if (family =3D=3D AF_INET) { + if (tcp_v4_ao_hash_pseudoheader(&hp, daddr->a4.s_addr, + saddr->a4.s_addr, th->doff * 4)) + goto clear_hash; +#if IS_ENABLED(CONFIG_IPV6) + } else if (family =3D=3D AF_INET6) { + if (tcp_v6_ao_hash_pseudoheader(&hp, &daddr->a6, + &saddr->a6, th->doff * 4)) + goto clear_hash; +#endif + } else { + WARN_ON_ONCE(1); + goto clear_hash; + } + if (tcp_ao_hash_header(&hp, th, false, + ao_hash, hash_offset, tcp_ao_maclen(key))) + goto clear_hash; + ahash_request_set_crypt(hp.req, NULL, hash_buf, 0); + if (crypto_ahash_final(hp.req)) + goto clear_hash; + + memcpy(ao_hash, hash_buf, tcp_ao_maclen(key)); + tcp_sigpool_end(&hp); + kfree(hash_buf); + return 0; + +clear_hash: + tcp_sigpool_end(&hp); +clear_hash_noput: + memset(ao_hash, 0, tcp_ao_maclen(key)); + kfree(hash_buf); + return 1; +} + int tcp_ao_hash_skb(unsigned short int family, char *ao_hash, struct tcp_ao_key *key, const struct sock *sk, const struct sk_buff *skb, @@ -435,6 +495,46 @@ struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock = *sk, struct sock *addr_sk, return tcp_ao_do_lookup(sk, addr, AF_INET, sndid, rcvid); } =20 +int tcp_ao_prepare_reset(const struct sock *sk, struct sk_buff *skb, + const struct tcp_ao_hdr *aoh, int l3index, u32 seq, + struct tcp_ao_key **key, char **traffic_key, + bool *allocated_traffic_key, u8 *keyid, u32 *sne) +{ + struct tcp_ao_info *ao_info; + + *allocated_traffic_key =3D false; + /* If there's no socket - than initial sisn/disn are unknown. + * Drop the segment. RFC5925 (7.7) advises to require graceful + * restart [RFC4724]. Alternatively, the RFC5925 advises to + * save/restore traffic keys before/after reboot. + * Linux TCP-AO support provides TCP_AO_ADD_KEY and TCP_AO_REPAIR + * options to restore a socket post-reboot. + */ + if (!sk) + return -ENOTCONN; + + if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV)) { + return -1; + } else { + struct tcp_ao_key *rnext_key; + + if (sk->sk_state =3D=3D TCP_TIME_WAIT) + return -1; + ao_info =3D rcu_dereference(tcp_sk(sk)->ao_info); + if (!ao_info) + return -ENOENT; + + *key =3D tcp_ao_established_key(ao_info, aoh->rnext_keyid, -1); + if (!*key) + return -ENOENT; + *traffic_key =3D snd_other_key(*key); + rnext_key =3D READ_ONCE(ao_info->rnext_key); + *keyid =3D rnext_key->rcvid; + *sne =3D 0; + } + return 0; +} + static int tcp_ao_cache_traffic_keys(const struct sock *sk, struct tcp_ao_info *ao, struct tcp_ao_key *ao_key) diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 31169971cc56..f07a12f478d4 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -657,6 +657,52 @@ void tcp_v4_send_check(struct sock *sk, struct sk_buff= *skb) } EXPORT_SYMBOL(tcp_v4_send_check); =20 +#define REPLY_OPTIONS_LEN (MAX_TCP_OPTION_SPACE / sizeof(__be32)) + +static bool tcp_v4_ao_sign_reset(const struct sock *sk, struct sk_buff *sk= b, + const struct tcp_ao_hdr *aoh, + struct ip_reply_arg *arg, struct tcphdr *reply, + __be32 reply_options[REPLY_OPTIONS_LEN]) +{ +#ifdef CONFIG_TCP_AO + int sdif =3D tcp_v4_sdif(skb); + int dif =3D inet_iif(skb); + int l3index =3D sdif ? dif : 0; + bool allocated_traffic_key; + struct tcp_ao_key *key; + char *traffic_key; + bool drop =3D true; + u32 ao_sne =3D 0; + u8 keyid; + + rcu_read_lock(); + if (tcp_ao_prepare_reset(sk, skb, aoh, l3index, reply->seq, + &key, &traffic_key, &allocated_traffic_key, + &keyid, &ao_sne)) + goto out; + + reply_options[0] =3D htonl((TCPOPT_AO << 24) | (tcp_ao_len(key) << 16) | + (aoh->rnext_keyid << 8) | keyid); + arg->iov[0].iov_len +=3D round_up(tcp_ao_len(key), 4); + reply->doff =3D arg->iov[0].iov_len / 4; + + if (tcp_ao_hash_hdr(AF_INET, (char *)&reply_options[1], + key, traffic_key, + (union tcp_ao_addr *)&ip_hdr(skb)->saddr, + (union tcp_ao_addr *)&ip_hdr(skb)->daddr, + reply, ao_sne)) + goto out; + drop =3D false; +out: + rcu_read_unlock(); + if (allocated_traffic_key) + kfree(traffic_key); + return drop; +#else + return true; +#endif +} + /* * This routine will send an RST to the other tcp. * @@ -670,28 +716,21 @@ EXPORT_SYMBOL(tcp_v4_send_check); * Exception: precedence violation. We do not implement it in any case. */ =20 -#ifdef CONFIG_TCP_AO -#define OPTION_BYTES MAX_TCP_OPTION_SPACE -#elif defined(CONFIG_TCP_MD5SIG) -#define OPTION_BYTES TCPOLEN_MD5SIG_ALIGNED -#else -#define OPTION_BYTES sizeof(__be32) -#endif - static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb) { const struct tcphdr *th =3D tcp_hdr(skb); struct { struct tcphdr th; - __be32 opt[OPTION_BYTES / sizeof(__be32)]; + __be32 opt[REPLY_OPTIONS_LEN]; } rep; + const __u8 *md5_hash_location =3D NULL; + const struct tcp_ao_hdr *aoh; struct ip_reply_arg arg; #ifdef CONFIG_TCP_MD5SIG - const __u8 *md5_hash_location =3D NULL; struct tcp_md5sig_key *key =3D NULL; unsigned char newhash[16]; - int genhash; struct sock *sk1 =3D NULL; + int genhash; #endif u64 transmit_time =3D 0; struct sock *ctl_sk; @@ -728,11 +767,15 @@ static void tcp_v4_send_reset(const struct sock *sk, = struct sk_buff *skb) arg.iov[0].iov_len =3D sizeof(rep.th); =20 net =3D sk ? sock_net(sk) : dev_net(skb_dst(skb)->dev); -#ifdef CONFIG_TCP_MD5SIG + /* Invalid TCP option size or twice included auth */ - if (tcp_parse_auth_options(tcp_hdr(skb), &md5_hash_location, NULL)) + if (tcp_parse_auth_options(tcp_hdr(skb), &md5_hash_location, &aoh)) return; =20 + if (aoh && tcp_v4_ao_sign_reset(sk, skb, aoh, &arg, &rep.th, rep.opt)) + return; + +#ifdef CONFIG_TCP_MD5SIG rcu_read_lock(); if (sk && sk_fullsock(sk)) { const union tcp_md5_addr *addr; diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 5c1880d5f5bc..08b016d5c1d9 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -854,7 +854,9 @@ const struct tcp_request_sock_ops tcp_request_sock_ipv6= _ops =3D { static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *sk= b, u32 seq, u32 ack, u32 win, u32 tsval, u32 tsecr, int oif, struct tcp_md5sig_key *key, int rst, - u8 tclass, __be32 label, u32 priority, u32 txhash) + u8 tclass, __be32 label, u32 priority, u32 txhash, + struct tcp_ao_key *ao_key, char *tkey, + u8 rcv_next, u32 ao_sne) { const struct tcphdr *th =3D tcp_hdr(skb); struct tcphdr *t1; @@ -873,6 +875,13 @@ static void tcp_v6_send_response(const struct sock *sk= , struct sk_buff *skb, u32 if (key) tot_len +=3D TCPOLEN_MD5SIG_ALIGNED; #endif +#ifdef CONFIG_TCP_AO + if (ao_key) + tot_len +=3D tcp_ao_len(ao_key); +#endif +#if defined(CONFIG_TCP_MD5SIG) && defined(CONFIG_TCP_AO) + WARN_ON_ONCE(key && ao_key); +#endif =20 #ifdef CONFIG_MPTCP if (rst && !key) { @@ -924,6 +933,17 @@ static void tcp_v6_send_response(const struct sock *sk= , struct sk_buff *skb, u32 &ipv6_hdr(skb)->daddr, t1); } #endif +#ifdef CONFIG_TCP_AO + if (ao_key) { + *topt++ =3D htonl((TCPOPT_AO << 24) | (tcp_ao_len(ao_key) << 16) | + (ao_key->sndid << 8) | (rcv_next)); + + tcp_ao_hash_hdr(AF_INET6, (char *)topt, ao_key, tkey, + (union tcp_ao_addr *)&ipv6_hdr(skb)->saddr, + (union tcp_ao_addr *)&ipv6_hdr(skb)->daddr, + t1, ao_sne); + } +#endif =20 memset(&fl6, 0, sizeof(fl6)); fl6.daddr =3D ipv6_hdr(skb)->saddr; @@ -986,19 +1006,24 @@ static void tcp_v6_send_reset(const struct sock *sk,= struct sk_buff *skb) { const struct tcphdr *th =3D tcp_hdr(skb); struct ipv6hdr *ipv6h =3D ipv6_hdr(skb); - u32 seq =3D 0, ack_seq =3D 0; - struct tcp_md5sig_key *key =3D NULL; -#ifdef CONFIG_TCP_MD5SIG const __u8 *md5_hash_location =3D NULL; + u32 seq =3D 0, ack_seq =3D 0, ao_sne =3D 0; + bool allocated_traffic_key =3D false; + struct tcp_md5sig_key *key =3D NULL; + struct tcp_ao_key *ao_key =3D NULL; + const struct tcp_ao_hdr *aoh; + char *traffic_key =3D NULL; + __be32 label =3D 0; + u32 priority =3D 0; + struct net *net; + u8 rcv_next =3D 0; + u32 txhash =3D 0; + int oif =3D 0; +#ifdef CONFIG_TCP_MD5SIG unsigned char newhash[16]; int genhash; struct sock *sk1 =3D NULL; #endif - __be32 label =3D 0; - u32 priority =3D 0; - struct net *net; - u32 txhash =3D 0; - int oif =3D 0; =20 if (th->rst) return; @@ -1010,12 +1035,11 @@ static void tcp_v6_send_reset(const struct sock *sk= , struct sk_buff *skb) return; =20 net =3D sk ? sock_net(sk) : dev_net(skb_dst(skb)->dev); -#ifdef CONFIG_TCP_MD5SIG /* Invalid TCP option size or twice included auth */ - if (tcp_parse_auth_options(th, &md5_hash_location, NULL)) + if (tcp_parse_auth_options(th, &md5_hash_location, &aoh)) return; - rcu_read_lock(); +#ifdef CONFIG_TCP_MD5SIG if (sk && sk_fullsock(sk)) { int l3index; =20 @@ -1064,6 +1088,19 @@ static void tcp_v6_send_reset(const struct sock *sk,= struct sk_buff *skb) ack_seq =3D ntohl(th->seq) + th->syn + th->fin + skb->len - (th->doff << 2); =20 +#ifdef CONFIG_TCP_AO + if (aoh) { + int l3index; + + l3index =3D tcp_v6_sdif(skb) ? tcp_v6_iif_l3_slave(skb) : 0; + if (tcp_ao_prepare_reset(sk, skb, aoh, l3index, htonl(seq), + &ao_key, &traffic_key, + &allocated_traffic_key, + &rcv_next, &ao_sne)) + goto out; + } +#endif + if (sk) { oif =3D sk->sk_bound_dev_if; if (sk_fullsock(sk)) { @@ -1086,10 +1123,13 @@ static void tcp_v6_send_reset(const struct sock *sk= , struct sk_buff *skb) } =20 tcp_v6_send_response(sk, skb, seq, ack_seq, 0, 0, 0, oif, key, 1, - ipv6_get_dsfield(ipv6h), label, priority, txhash); + ipv6_get_dsfield(ipv6h), label, priority, txhash, + ao_key, traffic_key, rcv_next, ao_sne); =20 -#ifdef CONFIG_TCP_MD5SIG +#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) out: + if (allocated_traffic_key) + kfree(traffic_key); rcu_read_unlock(); #endif } @@ -1100,7 +1140,7 @@ static void tcp_v6_send_ack(const struct sock *sk, st= ruct sk_buff *skb, u32 seq, __be32 label, u32 priority, u32 txhash) { tcp_v6_send_response(sk, skb, seq, ack, win, tsval, tsecr, oif, key, 0, - tclass, label, priority, txhash); + tclass, label, priority, txhash, NULL, NULL, 0, 0); } =20 static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb) --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BB4E0C001E0 for ; Tue, 15 Aug 2023 19:16:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239939AbjHOTQC (ORCPT ); Tue, 15 Aug 2023 15:16:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45852 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239863AbjHOTPY (ORCPT ); Tue, 15 Aug 2023 15:15:24 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2429E7A for ; Tue, 15 Aug 2023 12:15:20 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-3fe426b8583so58055875e9.2 for ; Tue, 15 Aug 2023 12:15:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126919; x=1692731719; 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=SpwqBytQo1wqgHf7DRmZwhPYBAlp+/5uHT9jtf3+gYE=; b=d4g5SaPPMpKgfilo/aaLmCkHQWc+nOkRQOtLdZMULnsC2AmD3KnAozPYJHEROlwiXZ zNjO+dj+gJ5zoXEh/QswmmcsqzcTaZdL6rU9PldONp4D1a1kkuUYLqOt/AM3TnCA3rsm DqE7FE0uAPtMAG0vpcfcj88eHm5fRm2bnxKY4SSX5agEpx5BTUdq0vREp9W+8ftgpBUh QhEr/tTvqgqYpRmO2nG3c36EqK/J9FCiCidDCbn+1MGnDyDyqZ3B04XK6JLTcbzuhtzS O2206MCmnmBMfLR75wbJUONeG/8cTieZscUGu+txPq3cYpmb9Uoj4Kuifk8bvNWVTtZW j6HQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126919; x=1692731719; 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=SpwqBytQo1wqgHf7DRmZwhPYBAlp+/5uHT9jtf3+gYE=; b=Zz1FQu0ui9KpVYKk0s3BEFYB8dNc7TNKBARhzchbyrSgvYzDP5wicFfw40WwZget9d 1HlY5rHjbfJHotILlnAFDUU/+iNdhNd0KTVooI9HpxShHkI4T3TPXfcMJV3ZZE/hzzxn jSBitB/rHLFA9QfR0E0Wecz8nVuXL53se6id4EDfogmwrgEO3XyxIUbZxGWcPSmp+Kt3 /RWWtDymJIQ2gqvlJjUO8ZchYPh6gRpPj/vcl9CVYcz3BELo6IPEZTDUxWoGPh6HtFE0 0iR94b5SYBrpBuW9Me1pgCGziBvswNmLAN9mdUYdrpJ1CGWFXgCeqWASjVCqU3G75vV1 USmw== X-Gm-Message-State: AOJu0YwRRkZ5zJKZOQdoi9OLM9J5LCEohU2+wrcGPr1HV72wpBPrhk1Y in3IA9ijzmR9qWLH/aK/LLcWkg== X-Google-Smtp-Source: AGHT+IGTFZIpLZiX6wBO6AE1yazDJTXDo8/Jkns8emKo/1bRC89D2bUIwrHBJtlHT96pp0j60jtltg== X-Received: by 2002:a7b:c5c5:0:b0:3fe:a5b:e504 with SMTP id n5-20020a7bc5c5000000b003fe0a5be504mr10444460wmk.22.1692126919169; Tue, 15 Aug 2023 12:15:19 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15: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 , "Gaillardetz, Dominik" , Herbert Xu , Hideaki YOSHIFUJI , Ivan Delalande , Leonard Crestez , "Nassiri, Mohammad" , Salam Noureddine , Simon Horman , "Tetreault, Francois" , netdev@vger.kernel.org Subject: [PATCH v10 net-next 09/23] net/tcp: Add TCP-AO sign to twsk Date: Tue, 15 Aug 2023 20:14:38 +0100 Message-ID: <20230815191455.1872316-10-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add support for sockets in time-wait state. ao_info as well as all keys are inherited on transition to time-wait socket. The lifetime of ao_info is now protected by ref counter, so that tcp_ao_destroy_sock() will destruct it only when the last user is gone. 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 Acked-by: David Ahern --- include/linux/tcp.h | 3 ++ include/net/tcp_ao.h | 13 +++++++-- net/ipv4/tcp_ao.c | 48 +++++++++++++++++++++++++------ net/ipv4/tcp_ipv4.c | 61 ++++++++++++++++++++++++++++++++++++---- net/ipv4/tcp_minisocks.c | 4 ++- net/ipv4/tcp_output.c | 2 +- net/ipv6/tcp_ipv6.c | 40 ++++++++++++++++++++++++-- 7 files changed, 149 insertions(+), 22 deletions(-) diff --git a/include/linux/tcp.h b/include/linux/tcp.h index fc98c7d63360..4050077cb39e 100644 --- a/include/linux/tcp.h +++ b/include/linux/tcp.h @@ -502,6 +502,9 @@ struct tcp_timewait_sock { #ifdef CONFIG_TCP_MD5SIG struct tcp_md5sig_key *tw_md5_key; #endif +#ifdef CONFIG_TCP_AO + struct tcp_ao_info __rcu *ao_info; +#endif }; =20 static inline struct tcp_timewait_sock *tcp_twsk(const struct sock *sk) diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index 67f997aabd9c..ab9163bae48d 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -85,6 +85,7 @@ struct tcp_ao_info { __unused :31; __be32 lisn; __be32 risn; + atomic_t refcnt; /* Protects twsk destruction */ struct rcu_head rcu; }; =20 @@ -121,7 +122,8 @@ struct tcp_ao_key *tcp_ao_established_key(struct tcp_ao= _info *ao, int sndid, int rcvid); int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx, unsigned int len, struct tcp_sigpool *hp); -void tcp_ao_destroy_sock(struct sock *sk); +void tcp_ao_destroy_sock(struct sock *sk, bool twsk); +void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp= ); struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, const union tcp_ao_addr *addr, int family, int sndid, int rcvid); @@ -131,7 +133,7 @@ int tcp_ao_hash_hdr(unsigned short family, char *ao_has= h, const union tcp_ao_addr *saddr, const struct tcphdr *th, u32 sne); int tcp_ao_prepare_reset(const struct sock *sk, struct sk_buff *skb, - const struct tcp_ao_hdr *aoh, int l3index, u32 seq, + const struct tcp_ao_hdr *aoh, int l3index, __be32 seq, struct tcp_ao_key **key, char **traffic_key, bool *allocated_traffic_key, u8 *keyid, u32 *sne); =20 @@ -171,7 +173,7 @@ static inline struct tcp_ao_key *tcp_ao_do_lookup(const= struct sock *sk, return NULL; } =20 -static inline void tcp_ao_destroy_sock(struct sock *sk) +static inline void tcp_ao_destroy_sock(struct sock *sk, bool twsk) { } =20 @@ -179,6 +181,11 @@ static inline void tcp_ao_finish_connect(struct sock *= sk, struct sk_buff *skb) { } =20 +static inline void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, + struct tcp_sock *tp) +{ +} + static inline void tcp_ao_connect_init(struct sock *sk) { } diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index 81ce4fe546f2..93072a5ada5c 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -159,6 +159,7 @@ static struct tcp_ao_info *tcp_ao_alloc_info(gfp_t flag= s) if (!ao) return NULL; INIT_HLIST_HEAD(&ao->head); + atomic_set(&ao->refcnt, 1); =20 return ao; } @@ -176,27 +177,54 @@ static void tcp_ao_key_free_rcu(struct rcu_head *head) kfree(key); } =20 -void tcp_ao_destroy_sock(struct sock *sk) +void tcp_ao_destroy_sock(struct sock *sk, bool twsk) { struct tcp_ao_info *ao; struct tcp_ao_key *key; struct hlist_node *n; =20 - ao =3D rcu_dereference_protected(tcp_sk(sk)->ao_info, 1); - tcp_sk(sk)->ao_info =3D NULL; + if (twsk) { + ao =3D rcu_dereference_protected(tcp_twsk(sk)->ao_info, 1); + tcp_twsk(sk)->ao_info =3D NULL; + } else { + ao =3D rcu_dereference_protected(tcp_sk(sk)->ao_info, 1); + tcp_sk(sk)->ao_info =3D NULL; + } =20 - if (!ao) + if (!ao || !atomic_dec_and_test(&ao->refcnt)) return; =20 hlist_for_each_entry_safe(key, n, &ao->head, node) { hlist_del_rcu(&key->node); - atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc); + if (!twsk) + atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc); call_rcu(&key->rcu, tcp_ao_key_free_rcu); } =20 kfree_rcu(ao, rcu); } =20 +void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp) +{ + struct tcp_ao_info *ao_info =3D rcu_dereference_protected(tp->ao_info, 1); + + if (ao_info) { + struct tcp_ao_key *key; + struct hlist_node *n; + int omem =3D 0; + + hlist_for_each_entry_safe(key, n, &ao_info->head, node) { + omem +=3D tcp_ao_sizeof_key(key); + } + + atomic_inc(&ao_info->refcnt); + atomic_sub(omem, &(((struct sock *)tp)->sk_omem_alloc)); + rcu_assign_pointer(tcptw->ao_info, ao_info); + } else { + tcptw->ao_info =3D NULL; + } +} + /* 4 tuple and ISNs are expected in NBO */ static int tcp_v4_ao_calc_key(struct tcp_ao_key *mkt, u8 *key, __be32 saddr, __be32 daddr, @@ -496,7 +524,7 @@ struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *= sk, struct sock *addr_sk, } =20 int tcp_ao_prepare_reset(const struct sock *sk, struct sk_buff *skb, - const struct tcp_ao_hdr *aoh, int l3index, u32 seq, + const struct tcp_ao_hdr *aoh, int l3index, __be32 seq, struct tcp_ao_key **key, char **traffic_key, bool *allocated_traffic_key, u8 *keyid, u32 *sne) { @@ -519,8 +547,9 @@ int tcp_ao_prepare_reset(const struct sock *sk, struct = sk_buff *skb, struct tcp_ao_key *rnext_key; =20 if (sk->sk_state =3D=3D TCP_TIME_WAIT) - return -1; - ao_info =3D rcu_dereference(tcp_sk(sk)->ao_info); + ao_info =3D rcu_dereference(tcp_twsk(sk)->ao_info); + else + ao_info =3D rcu_dereference(tcp_sk(sk)->ao_info); if (!ao_info) return -ENOENT; =20 @@ -862,6 +891,9 @@ static struct tcp_ao_info *setsockopt_ao_info(struct so= ck *sk) if (sk_fullsock(sk)) { return rcu_dereference_protected(tcp_sk(sk)->ao_info, lockdep_sock_is_held(sk)); + } else if (sk->sk_state =3D=3D TCP_TIME_WAIT) { + return rcu_dereference_protected(tcp_twsk(sk)->ao_info, + lockdep_sock_is_held(sk)); } return ERR_PTR(-ESOCKTNOSUPPORT); } diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index f07a12f478d4..6e9f6a334240 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -912,16 +912,16 @@ static void tcp_v4_send_ack(const struct sock *sk, struct sk_buff *skb, u32 seq, u32 ack, u32 win, u32 tsval, u32 tsecr, int oif, struct tcp_md5sig_key *key, + struct tcp_ao_key *ao_key, + u8 *traffic_key, + u8 rcv_next, + u32 ao_sne, int reply_flags, u8 tos, u32 txhash) { const struct tcphdr *th =3D tcp_hdr(skb); struct { struct tcphdr th; - __be32 opt[(TCPOLEN_TSTAMP_ALIGNED >> 2) -#ifdef CONFIG_TCP_MD5SIG - + (TCPOLEN_MD5SIG_ALIGNED >> 2) -#endif - ]; + __be32 opt[(MAX_TCP_OPTION_SPACE >> 2)]; } rep; struct net *net =3D sock_net(sk); struct ip_reply_arg arg; @@ -966,6 +966,24 @@ static void tcp_v4_send_ack(const struct sock *sk, key, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr, &rep.th); } +#endif +#ifdef CONFIG_TCP_AO + if (ao_key) { + int offset =3D (tsecr) ? 3 : 0; + + rep.opt[offset++] =3D htonl((TCPOPT_AO << 24) | + (tcp_ao_len(ao_key) << 16) | + (ao_key->sndid << 8) | rcv_next); + arg.iov[0].iov_len +=3D round_up(tcp_ao_len(ao_key), 4); + rep.th.doff =3D arg.iov[0].iov_len / 4; + + tcp_ao_hash_hdr(AF_INET, (char *)&rep.opt[offset], + ao_key, traffic_key, + (union tcp_ao_addr *)&ip_hdr(skb)->saddr, + (union tcp_ao_addr *)&ip_hdr(skb)->daddr, + &rep.th, ao_sne); + } + WARN_ON_ONCE(key && ao_key); #endif arg.flags =3D reply_flags; arg.csum =3D csum_tcpudp_nofold(ip_hdr(skb)->daddr, @@ -999,6 +1017,32 @@ static void tcp_v4_timewait_ack(struct sock *sk, stru= ct sk_buff *skb) { struct inet_timewait_sock *tw =3D inet_twsk(sk); struct tcp_timewait_sock *tcptw =3D tcp_twsk(sk); + struct tcp_ao_key *ao_key =3D NULL; + u8 *traffic_key =3D NULL; + u8 rcv_next =3D 0; + u32 ao_sne =3D 0; +#ifdef CONFIG_TCP_AO + struct tcp_ao_info *ao_info; + + /* FIXME: the segment to-be-acked is not verified yet */ + ao_info =3D rcu_dereference(tcptw->ao_info); + if (ao_info) { + const struct tcp_ao_hdr *aoh; + + if (tcp_parse_auth_options(tcp_hdr(skb), NULL, &aoh)) + goto out; /* something is wrong with the sign */ + + if (aoh) + ao_key =3D tcp_ao_established_key(ao_info, aoh->rnext_keyid, -1); + } + if (ao_key) { + struct tcp_ao_key *rnext_key; + + traffic_key =3D snd_other_key(ao_key); + rnext_key =3D READ_ONCE(ao_info->rnext_key); + rcv_next =3D rnext_key->rcvid; + } +#endif =20 tcp_v4_send_ack(sk, skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt, @@ -1007,11 +1051,15 @@ static void tcp_v4_timewait_ack(struct sock *sk, st= ruct sk_buff *skb) tcptw->tw_ts_recent, tw->tw_bound_dev_if, tcp_twsk_md5_key(tcptw), + ao_key, traffic_key, rcv_next, ao_sne, tw->tw_transparent ? IP_REPLY_ARG_NOSRCCHECK : 0, tw->tw_tos, tw->tw_txhash ); =20 +#ifdef CONFIG_TCP_AO +out: +#endif inet_twsk_put(tw); } =20 @@ -1041,6 +1089,7 @@ static void tcp_v4_reqsk_send_ack(const struct sock *= sk, struct sk_buff *skb, READ_ONCE(req->ts_recent), 0, tcp_md5_do_lookup(sk, l3index, addr, AF_INET), + NULL, NULL, 0, 0, inet_rsk(req)->no_srccheck ? IP_REPLY_ARG_NOSRCCHECK : 0, ip_hdr(skb)->tos, READ_ONCE(tcp_rsk(req)->txhash)); @@ -2402,7 +2451,7 @@ void tcp_v4_destroy_sock(struct sock *sk) rcu_assign_pointer(tp->md5sig_info, NULL); } #endif - tcp_ao_destroy_sock(sk); + tcp_ao_destroy_sock(sk, false); =20 /* Clean up a referenced TCP bind bucket. */ if (inet_csk(sk)->icsk_bind_hash) diff --git a/net/ipv4/tcp_minisocks.c b/net/ipv4/tcp_minisocks.c index 2e6c94133746..d2bddd0099ec 100644 --- a/net/ipv4/tcp_minisocks.c +++ b/net/ipv4/tcp_minisocks.c @@ -279,7 +279,7 @@ static void tcp_time_wait_init(struct sock *sk, struct = tcp_timewait_sock *tcptw) void tcp_time_wait(struct sock *sk, int state, int timeo) { const struct inet_connection_sock *icsk =3D inet_csk(sk); - const struct tcp_sock *tp =3D tcp_sk(sk); + struct tcp_sock *tp =3D tcp_sk(sk); struct net *net =3D sock_net(sk); struct inet_timewait_sock *tw; =20 @@ -316,6 +316,7 @@ void tcp_time_wait(struct sock *sk, int state, int time= o) #endif =20 tcp_time_wait_init(sk, tcptw); + tcp_ao_time_wait(tcptw, tp); =20 /* Get the TIME_WAIT timeout firing. */ if (timeo < rto) @@ -370,6 +371,7 @@ void tcp_twsk_destructor(struct sock *sk) call_rcu(&twsk->tw_md5_key->rcu, tcp_md5_twsk_free_rcu); } #endif + tcp_ao_destroy_sock(sk, true); } EXPORT_SYMBOL_GPL(tcp_twsk_destructor); =20 diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 5804f50bb747..83516d6dc17a 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -4054,7 +4054,7 @@ int tcp_connect(struct sock *sk) * then free up ao_info if allocated. */ if (needs_md5) { - tcp_ao_destroy_sock(sk); + tcp_ao_destroy_sock(sk, false); } else if (needs_ao) { tcp_clear_md5_list(sk); kfree(rcu_replace_pointer(tp->md5sig_info, NULL, diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 08b016d5c1d9..8dfa0959e403 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -1137,24 +1137,57 @@ static void tcp_v6_send_reset(const struct sock *sk= , struct sk_buff *skb) static void tcp_v6_send_ack(const struct sock *sk, struct sk_buff *skb, u3= 2 seq, u32 ack, u32 win, u32 tsval, u32 tsecr, int oif, struct tcp_md5sig_key *key, u8 tclass, - __be32 label, u32 priority, u32 txhash) + __be32 label, u32 priority, u32 txhash, + struct tcp_ao_key *ao_key, char *tkey, + u8 rcv_next, u32 ao_sne) { tcp_v6_send_response(sk, skb, seq, ack, win, tsval, tsecr, oif, key, 0, - tclass, label, priority, txhash, NULL, NULL, 0, 0); + tclass, label, priority, txhash, + ao_key, tkey, rcv_next, ao_sne); } =20 static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb) { struct inet_timewait_sock *tw =3D inet_twsk(sk); struct tcp_timewait_sock *tcptw =3D tcp_twsk(sk); + struct tcp_ao_key *ao_key =3D NULL; + u8 *traffic_key =3D NULL; + u8 rcv_next =3D 0; + u32 ao_sne =3D 0; +#ifdef CONFIG_TCP_AO + struct tcp_ao_info *ao_info; + + /* FIXME: the segment to-be-acked is not verified yet */ + ao_info =3D rcu_dereference(tcptw->ao_info); + if (ao_info) { + const struct tcp_ao_hdr *aoh; + + /* Invalid TCP option size or twice included auth */ + if (tcp_parse_auth_options(tcp_hdr(skb), NULL, &aoh)) + goto out; + if (aoh) + ao_key =3D tcp_ao_established_key(ao_info, aoh->rnext_keyid, -1); + } + if (ao_key) { + struct tcp_ao_key *rnext_key; + + traffic_key =3D snd_other_key(ao_key); + /* rcv_next switches to our rcv_next */ + rnext_key =3D READ_ONCE(ao_info->rnext_key); + rcv_next =3D rnext_key->rcvid; + } +#endif =20 tcp_v6_send_ack(sk, skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt, tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale, tcp_time_stamp_raw() + tcptw->tw_ts_offset, tcptw->tw_ts_recent, tw->tw_bound_dev_if, tcp_twsk_md5_key(tcptw), tw->tw_tclass, cpu_to_be32(tw->tw_flowlabel), tw->tw_priority, - tw->tw_txhash); + tw->tw_txhash, ao_key, traffic_key, rcv_next, ao_sne); =20 +#ifdef CONFIG_TCP_AO +out: +#endif inet_twsk_put(tw); } =20 @@ -1183,6 +1216,7 @@ static void tcp_v6_reqsk_send_ack(const struct sock *= sk, struct sk_buff *skb, ipv6_get_dsfield(ipv6_hdr(skb)), 0, READ_ONCE(sk->sk_priority), READ_ONCE(tcp_rsk(req)->txhash)); + NULL, NULL, 0, 0); } =20 =20 --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CD878C04FDF for ; Tue, 15 Aug 2023 19:16:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239964AbjHOTQF (ORCPT ); Tue, 15 Aug 2023 15:16:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239892AbjHOTP2 (ORCPT ); Tue, 15 Aug 2023 15:15:28 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98B2FB8 for ; Tue, 15 Aug 2023 12:15:22 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-3fe45481edfso58174105e9.1 for ; Tue, 15 Aug 2023 12:15:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126921; x=1692731721; 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=jgtj56tZ1qrlDI4mq9pikOvGP+25MkAmHr3TCJGvN3A=; b=VgWCkMohTF7X21SxCPrQWeUIxABJYWPATdglPp9DDnB0Ks4UnY0RqmQmFHovTz2bOa BkoDno53WNjdgNdsRaNCopaziZ/ccjEQO73YLRd5i+rF0CzhCoPSVEYAGRR78ykrwT/r 2onB1O4vxUz0ePkaph2LCbeSwVZ/c1bIXgMzfAdCgpOVvZFDE/SAh7eI36nOlNjxDPXg JJhJ8EO8NtNNTiwbjHQO6oUmp0XdX6kbWceX1bC2GsTOxSQ8u300xAxWafCWYVhPm4W1 kpvw8YP/7qx5gT03MqcdKyvARyZukdfj8ekxubdCZbUA18kMeG6+BMLb0FJgo+LLjam/ a9LQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126921; x=1692731721; 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=jgtj56tZ1qrlDI4mq9pikOvGP+25MkAmHr3TCJGvN3A=; b=EsfIYPaMugjx5UbH3TkovpYpXXYblJXLXPu3Vd1C4T5SaMvB7DAPLKlwmBxL6xi13G ir0LdgvlSR49NQIgOzAsGYSOlvnQZV+58MuZp7tpgpwahm0Oz9F27l9d2PzV/msvvxCl PSdRLMNQVfntt3CkjYmBzDMrCdZ5vSxmuyqM1cdT9nLrlWoMr21P2yvivWy4TK6Pt9fq Pwcnjf41wXVNqOrJoaPnSBMbptuxjcL30xBdT9bgpkpvoGJx8jG0jrnyYM7sewv+sQKJ wZW4Ha0S6ex5OuijYEahuwP12+7dCVbc3p4xLzQ+YR9Nprbb78VocMA9kqQey1MFgZZX NOIg== X-Gm-Message-State: AOJu0YwXGFbeiPNgA1iwgy3+1TtLCBjKwKYby0f1ozdTjcxh6+Id5nvC Dc/MHCTnQFvyYVK1uASotGQCGw== X-Google-Smtp-Source: AGHT+IFyCk9uyJ93SbXBE/1AmtQqgTKkEtEPxlWsk991EJJTEVSdCGOKcqyteaJxi5nW3kd4L/PGEA== X-Received: by 2002:a05:600c:2055:b0:3fe:46ea:62c6 with SMTP id p21-20020a05600c205500b003fe46ea62c6mr10430420wmg.21.1692126920961; Tue, 15 Aug 2023 12:15:20 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:20 -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 v10 net-next 10/23] net/tcp: Wire TCP-AO to request sockets Date: Tue, 15 Aug 2023 20:14:39 +0100 Message-ID: <20230815191455.1872316-11-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Now when the new request socket is created from the listening socket, it's recorded what MKT was used by the peer. tcp_rsk_used_ao() is a new helper for checking if TCP-AO option was used to create the request socket. tcp_ao_copy_all_matching() will copy all keys that match the peer on the request socket, as well as preparing them for the usage (creating traffic keys). 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 Acked-by: David Ahern --- include/linux/tcp.h | 18 +++ include/net/tcp.h | 6 + include/net/tcp_ao.h | 24 ++++ net/ipv4/syncookies.c | 2 + net/ipv4/tcp_ao.c | 238 ++++++++++++++++++++++++++++++++++++++- net/ipv4/tcp_input.c | 15 +++ net/ipv4/tcp_ipv4.c | 63 ++++++++++- net/ipv4/tcp_minisocks.c | 10 ++ net/ipv4/tcp_output.c | 39 ++++--- net/ipv6/syncookies.c | 2 + net/ipv6/tcp_ao.c | 38 ++++++- net/ipv6/tcp_ipv6.c | 77 +++++++++++-- 12 files changed, 497 insertions(+), 35 deletions(-) diff --git a/include/linux/tcp.h b/include/linux/tcp.h index 4050077cb39e..fb3b43c52dd5 100644 --- a/include/linux/tcp.h +++ b/include/linux/tcp.h @@ -165,6 +165,11 @@ struct tcp_request_sock { * after data-in-SYN. */ u8 syn_tos; +#ifdef CONFIG_TCP_AO + u8 ao_keyid; + u8 ao_rcv_next; + u8 maclen; +#endif }; =20 static inline struct tcp_request_sock *tcp_rsk(const struct request_sock *= req) @@ -172,6 +177,19 @@ static inline struct tcp_request_sock *tcp_rsk(const s= truct request_sock *req) return (struct tcp_request_sock *)req; } =20 +static inline bool tcp_rsk_used_ao(const struct request_sock *req) +{ + /* The real length of MAC is saved in the request socket, + * signing anything with zero-length makes no sense, so here is + * a little hack.. + */ +#ifndef CONFIG_TCP_AO + return false; +#else + return tcp_rsk(req)->maclen !=3D 0; +#endif +} + #define TCP_RMEM_TO_WIN_SCALE 8 =20 struct tcp_sock { diff --git a/include/net/tcp.h b/include/net/tcp.h index a09b5b53a834..710d75b23b77 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -2175,6 +2175,12 @@ struct tcp_request_sock_ops { const struct sock *sk, const struct sk_buff *skb); #endif +#ifdef CONFIG_TCP_AO + struct tcp_ao_key *(*ao_lookup)(const struct sock *sk, + struct request_sock *req, + int sndid, int rcvid); + int (*ao_calc_key)(struct tcp_ao_key *mkt, u8 *key, struct request_sock *= sk); +#endif #ifdef CONFIG_SYN_COOKIES __u32 (*cookie_init_seq)(const struct sk_buff *skb, __u16 *mss); diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index ab9163bae48d..409646be90c3 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -120,6 +120,9 @@ int tcp_parse_ao(struct sock *sk, int cmd, unsigned sho= rt int family, sockptr_t optval, int optlen); struct tcp_ao_key *tcp_ao_established_key(struct tcp_ao_info *ao, int sndid, int rcvid); +int tcp_ao_copy_all_matching(const struct sock *sk, struct sock *newsk, + struct request_sock *req, struct sk_buff *skb, + int family); int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx, unsigned int len, struct tcp_sigpool *hp); void tcp_ao_destroy_sock(struct sock *sk, bool twsk); @@ -144,6 +147,11 @@ struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock = *sk, struct sock *addr_sk, int tcp_v4_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key, const struct sock *sk, __be32 sisn, __be32 disn, bool send); +int tcp_v4_ao_calc_key_rsk(struct tcp_ao_key *mkt, u8 *key, + struct request_sock *req); +struct tcp_ao_key *tcp_v4_ao_lookup_rsk(const struct sock *sk, + struct request_sock *req, + int sndid, int rcvid); int tcp_v4_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key, const struct sock *sk, const struct sk_buff *skb, const u8 *tkey, int hash_offset, u32 sne); @@ -151,11 +159,21 @@ int tcp_v4_ao_hash_skb(char *ao_hash, struct tcp_ao_k= ey *key, int tcp_v6_ao_hash_pseudoheader(struct tcp_sigpool *hp, const struct in6_addr *daddr, const struct in6_addr *saddr, int nbytes); +int tcp_v6_ao_calc_key_skb(struct tcp_ao_key *mkt, u8 *key, + const struct sk_buff *skb, __be32 sisn, __be32 disn); int tcp_v6_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key, const struct sock *sk, __be32 sisn, __be32 disn, bool send); +int tcp_v6_ao_calc_key_rsk(struct tcp_ao_key *mkt, u8 *key, + struct request_sock *req); +struct tcp_ao_key *tcp_v6_ao_do_lookup(const struct sock *sk, + const struct in6_addr *addr, + int sndid, int rcvid); struct tcp_ao_key *tcp_v6_ao_lookup(const struct sock *sk, struct sock *addr_sk, int sndid, int rcvid); +struct tcp_ao_key *tcp_v6_ao_lookup_rsk(const struct sock *sk, + struct request_sock *req, + int sndid, int rcvid); int tcp_v6_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key, const struct sock *sk, const struct sk_buff *skb, const u8 *tkey, int hash_offset, u32 sne); @@ -167,6 +185,12 @@ void tcp_ao_syncookie(struct sock *sk, const struct sk= _buff *skb, unsigned short int family); #else /* CONFIG_TCP_AO */ =20 +static inline void tcp_ao_syncookie(struct sock *sk, const struct sk_buff = *skb, + struct tcp_request_sock *treq, + unsigned short int family) +{ +} + static inline struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, const union tcp_ao_addr *addr, int family, int sndid, int rcvid) { diff --git a/net/ipv4/syncookies.c b/net/ipv4/syncookies.c index dc478a0574cb..23fca22bc992 100644 --- a/net/ipv4/syncookies.c +++ b/net/ipv4/syncookies.c @@ -394,6 +394,8 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk= _buff *skb) treq->snt_synack =3D 0; treq->tfo_listener =3D false; =20 + tcp_ao_syncookie(sk, skb, treq, AF_INET); + if (IS_ENABLED(CONFIG_SMC)) ireq->smc_ok =3D 0; =20 diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index 93072a5ada5c..94ca87240c17 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -169,6 +169,23 @@ static void tcp_ao_link_mkt(struct tcp_ao_info *ao, st= ruct tcp_ao_key *mkt) hlist_add_head_rcu(&mkt->node, &ao->head); } =20 +static struct tcp_ao_key *tcp_ao_copy_key(struct sock *sk, + struct tcp_ao_key *key) +{ + struct tcp_ao_key *new_key; + + new_key =3D sock_kmalloc(sk, tcp_ao_sizeof_key(key), + GFP_ATOMIC); + if (!new_key) + return NULL; + + *new_key =3D *key; + INIT_HLIST_NODE(&new_key->node); + tcp_sigpool_get(new_key->tcp_sigpool_id); + + return new_key; +} + static void tcp_ao_key_free_rcu(struct rcu_head *head) { struct tcp_ao_key *key =3D container_of(head, struct tcp_ao_key, rcu); @@ -290,6 +307,42 @@ static int tcp_ao_calc_key_sk(struct tcp_ao_key *mkt, = u8 *key, return -EOPNOTSUPP; } =20 +int tcp_v4_ao_calc_key_rsk(struct tcp_ao_key *mkt, u8 *key, + struct request_sock *req) +{ + struct inet_request_sock *ireq =3D inet_rsk(req); + + return tcp_v4_ao_calc_key(mkt, key, + ireq->ir_loc_addr, ireq->ir_rmt_addr, + htons(ireq->ir_num), ireq->ir_rmt_port, + htonl(tcp_rsk(req)->snt_isn), + htonl(tcp_rsk(req)->rcv_isn)); +} + +static int tcp_v4_ao_calc_key_skb(struct tcp_ao_key *mkt, u8 *key, + const struct sk_buff *skb, + __be32 sisn, __be32 disn) +{ + const struct iphdr *iph =3D ip_hdr(skb); + const struct tcphdr *th =3D tcp_hdr(skb); + + return tcp_v4_ao_calc_key(mkt, key, iph->saddr, iph->daddr, + th->source, th->dest, sisn, disn); +} + +static int tcp_ao_calc_key_skb(struct tcp_ao_key *mkt, u8 *key, + const struct sk_buff *skb, + __be32 sisn, __be32 disn, int family) +{ + if (family =3D=3D AF_INET) + return tcp_v4_ao_calc_key_skb(mkt, key, skb, sisn, disn); +#if IS_ENABLED(CONFIG_IPV6) + else if (family =3D=3D AF_INET6) + return tcp_v6_ao_calc_key_skb(mkt, key, skb, sisn, disn); +#endif + return -EAFNOSUPPORT; +} + static int tcp_v4_ao_hash_pseudoheader(struct tcp_sigpool *hp, __be32 daddr, __be32 saddr, int nbytes) @@ -515,6 +568,16 @@ int tcp_v4_ao_hash_skb(char *ao_hash, struct tcp_ao_ke= y *key, tkey, hash_offset, sne); } =20 +struct tcp_ao_key *tcp_v4_ao_lookup_rsk(const struct sock *sk, + struct request_sock *req, + int sndid, int rcvid) +{ + union tcp_ao_addr *addr =3D + (union tcp_ao_addr *)&inet_rsk(req)->ir_rmt_addr; + + return tcp_ao_do_lookup(sk, addr, AF_INET, sndid, rcvid); +} + struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *ad= dr_sk, int sndid, int rcvid) { @@ -528,6 +591,7 @@ int tcp_ao_prepare_reset(const struct sock *sk, struct = sk_buff *skb, struct tcp_ao_key **key, char **traffic_key, bool *allocated_traffic_key, u8 *keyid, u32 *sne) { + const struct tcphdr *th =3D tcp_hdr(skb); struct tcp_ao_info *ao_info; =20 *allocated_traffic_key =3D false; @@ -542,7 +606,45 @@ int tcp_ao_prepare_reset(const struct sock *sk, struct= sk_buff *skb, return -ENOTCONN; =20 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV)) { - return -1; + union tcp_ao_addr *addr; + unsigned int family; + __be32 disn, sisn; + + if (sk->sk_state =3D=3D TCP_NEW_SYN_RECV) { + struct request_sock *req =3D inet_reqsk(sk); + + sisn =3D htonl(tcp_rsk(req)->rcv_isn); + disn =3D htonl(tcp_rsk(req)->snt_isn); + *sne =3D 0; + } else { + sisn =3D th->seq; + disn =3D 0; + } + if (IS_ENABLED(CONFIG_IPV6) && sk->sk_family =3D=3D AF_INET6) + addr =3D (union tcp_md5_addr *)&ipv6_hdr(skb)->saddr; + else + addr =3D (union tcp_md5_addr *)&ip_hdr(skb)->saddr; + family =3D sk->sk_family; +#if IS_ENABLED(CONFIG_IPV6) + if (family =3D=3D AF_INET6 && ipv6_addr_v4mapped(&sk->sk_v6_daddr)) + family =3D AF_INET; +#endif + + sk =3D sk_const_to_full_sk(sk); + ao_info =3D rcu_dereference(tcp_sk(sk)->ao_info); + if (!ao_info) + return -ENOENT; + *key =3D tcp_ao_do_lookup(sk, addr, family, -1, aoh->rnext_keyid); + if (!*key) + return -ENOENT; + *traffic_key =3D kmalloc(tcp_ao_digest_size(*key), GFP_ATOMIC); + if (!*traffic_key) + return -ENOMEM; + *allocated_traffic_key =3D true; + if (tcp_ao_calc_key_skb(*key, *traffic_key, skb, + sisn, disn, family)) + return -1; + *keyid =3D (*key)->rcvid; } else { struct tcp_ao_key *rnext_key; =20 @@ -564,6 +666,46 @@ int tcp_ao_prepare_reset(const struct sock *sk, struct= sk_buff *skb, return 0; } =20 +static struct tcp_ao_key *tcp_ao_inbound_lookup(unsigned short int family, + const struct sock *sk, const struct sk_buff *skb, + int sndid, int rcvid) +{ + if (family =3D=3D AF_INET) { + const struct iphdr *iph =3D ip_hdr(skb); + + return tcp_ao_do_lookup(sk, (union tcp_ao_addr *)&iph->saddr, + AF_INET, sndid, rcvid); + } else { + const struct ipv6hdr *iph =3D ipv6_hdr(skb); + + return tcp_ao_do_lookup(sk, (union tcp_ao_addr *)&iph->saddr, + AF_INET6, sndid, rcvid); + } +} + +void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb, + struct tcp_request_sock *treq, + unsigned short int family) +{ + const struct tcphdr *th =3D tcp_hdr(skb); + const struct tcp_ao_hdr *aoh; + struct tcp_ao_key *key; + + treq->maclen =3D 0; + + if (tcp_parse_auth_options(th, NULL, &aoh) || !aoh) + return; + + key =3D tcp_ao_inbound_lookup(family, sk, skb, -1, aoh->keyid); + if (!key) + /* Key not found, continue without TCP-AO */ + return; + + treq->ao_rcv_next =3D aoh->keyid; + treq->ao_keyid =3D aoh->rnext_keyid; + treq->maclen =3D tcp_ao_maclen(key); +} + static int tcp_ao_cache_traffic_keys(const struct sock *sk, struct tcp_ao_info *ao, struct tcp_ao_key *ao_key) @@ -655,6 +797,100 @@ void tcp_ao_finish_connect(struct sock *sk, struct sk= _buff *skb) tcp_ao_cache_traffic_keys(sk, ao, key); } =20 +int tcp_ao_copy_all_matching(const struct sock *sk, struct sock *newsk, + struct request_sock *req, struct sk_buff *skb, + int family) +{ + struct tcp_ao_key *key, *new_key, *first_key; + struct tcp_ao_info *new_ao, *ao; + struct hlist_node *key_head; + union tcp_ao_addr *addr; + bool match =3D false; + int ret =3D -ENOMEM; + + ao =3D rcu_dereference(tcp_sk(sk)->ao_info); + if (!ao) + return 0; + + /* New socket without TCP-AO on it */ + if (!tcp_rsk_used_ao(req)) + return 0; + + new_ao =3D tcp_ao_alloc_info(GFP_ATOMIC); + if (!new_ao) + return -ENOMEM; + new_ao->lisn =3D htonl(tcp_rsk(req)->snt_isn); + new_ao->risn =3D htonl(tcp_rsk(req)->rcv_isn); + new_ao->ao_required =3D ao->ao_required; + + if (family =3D=3D AF_INET) { + addr =3D (union tcp_ao_addr *)&newsk->sk_daddr; +#if IS_ENABLED(CONFIG_IPV6) + } else if (family =3D=3D AF_INET6) { + addr =3D (union tcp_ao_addr *)&newsk->sk_v6_daddr; +#endif + } else { + ret =3D -EAFNOSUPPORT; + goto free_ao; + } + + hlist_for_each_entry_rcu(key, &ao->head, node) { + if (tcp_ao_key_cmp(key, addr, key->prefixlen, family, -1, -1)) + continue; + + new_key =3D tcp_ao_copy_key(newsk, key); + if (!new_key) + goto free_and_exit; + + tcp_ao_cache_traffic_keys(newsk, new_ao, new_key); + tcp_ao_link_mkt(new_ao, new_key); + match =3D true; + } + + if (!match) { + /* RFC5925 (7.4.1) specifies that the TCP-AO status + * of a connection is determined on the initial SYN. + * At this point the connection was TCP-AO enabled, so + * it can't switch to being unsigned if peer's key + * disappears on the listening socket. + */ + ret =3D -EKEYREJECTED; + goto free_and_exit; + } + + key_head =3D rcu_dereference(hlist_first_rcu(&new_ao->head)); + first_key =3D hlist_entry_safe(key_head, struct tcp_ao_key, node); + + key =3D tcp_ao_established_key(new_ao, tcp_rsk(req)->ao_keyid, -1); + if (key) + new_ao->current_key =3D key; + else + new_ao->current_key =3D first_key; + + /* set rnext_key */ + key =3D tcp_ao_established_key(new_ao, -1, tcp_rsk(req)->ao_rcv_next); + if (key) + new_ao->rnext_key =3D key; + else + new_ao->rnext_key =3D first_key; + + sk_gso_disable(newsk); + rcu_assign_pointer(tcp_sk(newsk)->ao_info, new_ao); + + return 0; + +free_and_exit: + hlist_for_each_entry_safe(key, key_head, &new_ao->head, node) { + hlist_del(&key->node); + tcp_sigpool_release(key->tcp_sigpool_id); + atomic_sub(tcp_ao_sizeof_key(key), &newsk->sk_omem_alloc); + kfree(key); + } +free_ao: + kfree(new_ao); + return ret; +} + static bool tcp_ao_can_set_current_rnext(struct sock *sk) { /* There aren't current/rnext keys on TCP_LISTEN sockets */ diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index de6d55f46aa8..acf9aa9ce0a0 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -6978,6 +6978,10 @@ int tcp_conn_request(struct request_sock_ops *rsk_op= s, struct flowi fl; u8 syncookies; =20 +#ifdef CONFIG_TCP_AO + const struct tcp_ao_hdr *aoh; +#endif + syncookies =3D READ_ONCE(net->ipv4.sysctl_tcp_syncookies); =20 /* TW buckets are converted to open requests without @@ -7063,6 +7067,17 @@ int tcp_conn_request(struct request_sock_ops *rsk_op= s, inet_rsk(req)->ecn_ok =3D 0; } =20 +#ifdef CONFIG_TCP_AO + if (tcp_parse_auth_options(tcp_hdr(skb), NULL, &aoh)) + goto drop_and_release; /* Invalid TCP options */ + if (aoh) { + tcp_rsk(req)->maclen =3D aoh->length - sizeof(struct tcp_ao_hdr); + tcp_rsk(req)->ao_rcv_next =3D aoh->keyid; + tcp_rsk(req)->ao_keyid =3D aoh->rnext_keyid; + } else { + tcp_rsk(req)->maclen =3D 0; + } +#endif tcp_rsk(req)->snt_isn =3D isn; tcp_rsk(req)->txhash =3D net_tx_rndhash(); tcp_rsk(req)->syn_tos =3D TCP_SKB_CB(skb)->ip_dsfield; diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 6e9f6a334240..968692f76fa2 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -1066,33 +1066,76 @@ static void tcp_v4_timewait_ack(struct sock *sk, st= ruct sk_buff *skb) static void tcp_v4_reqsk_send_ack(const struct sock *sk, struct sk_buff *s= kb, struct request_sock *req) { + struct tcp_md5sig_key *md5_key =3D NULL; + struct tcp_ao_key *ao_key =3D NULL; const union tcp_md5_addr *addr; - int l3index; + u8 *traffic_key =3D NULL; + u8 keyid =3D 0; +#ifdef CONFIG_TCP_AO + const struct tcp_ao_hdr *aoh; +#endif =20 /* sk->sk_state =3D=3D TCP_LISTEN -> for regular TCP_SYN_RECV * sk->sk_state =3D=3D TCP_SYN_RECV -> for Fast Open. */ u32 seq =3D (sk->sk_state =3D=3D TCP_LISTEN) ? tcp_rsk(req)->snt_isn + 1 : tcp_sk(sk)->snd_nxt; + addr =3D (union tcp_md5_addr *)&ip_hdr(skb)->saddr; =20 + if (tcp_rsk_used_ao(req)) { +#ifdef CONFIG_TCP_AO + /* Invalid TCP option size or twice included auth */ + if (tcp_parse_auth_options(tcp_hdr(skb), NULL, &aoh)) + return; + + if (!aoh) + return; + + ao_key =3D tcp_ao_do_lookup(sk, addr, AF_INET, aoh->rnext_keyid, -1); + if (unlikely(!ao_key)) { + /* Send ACK with any matching MKT for the peer */ + ao_key =3D tcp_ao_do_lookup(sk, addr, AF_INET, -1, -1); + /* Matching key disappeared (user removed the key?) + * let the handshake timeout. + */ + if (!ao_key) { + net_info_ratelimited("TCP-AO key for (%pI4, %d)->(%pI4, %d) suddenly d= isappeared, won't ACK new connection\n", + addr, + ntohs(tcp_hdr(skb)->source), + &ip_hdr(skb)->daddr, + ntohs(tcp_hdr(skb)->dest)); + return; + } + } + traffic_key =3D kmalloc(tcp_ao_digest_size(ao_key), GFP_ATOMIC); + if (!traffic_key) + return; + + keyid =3D aoh->keyid; + tcp_v4_ao_calc_key_rsk(ao_key, traffic_key, req); +#endif + } else { + int l3index; + + l3index =3D tcp_v4_sdif(skb) ? inet_iif(skb) : 0; + md5_key =3D tcp_md5_do_lookup(sk, l3index, addr, AF_INET); + } /* RFC 7323 2.3 * The window field (SEG.WND) of every outgoing segment, with the * exception of segments, MUST be right-shifted by * Rcv.Wind.Shift bits: */ - addr =3D (union tcp_md5_addr *)&ip_hdr(skb)->saddr; - l3index =3D tcp_v4_sdif(skb) ? inet_iif(skb) : 0; tcp_v4_send_ack(sk, skb, seq, tcp_rsk(req)->rcv_nxt, req->rsk_rcv_wnd >> inet_rsk(req)->rcv_wscale, tcp_time_stamp_raw() + tcp_rsk(req)->ts_off, READ_ONCE(req->ts_recent), 0, - tcp_md5_do_lookup(sk, l3index, addr, AF_INET), - NULL, NULL, 0, 0, + md5_key, ao_key, traffic_key, keyid, 0, inet_rsk(req)->no_srccheck ? IP_REPLY_ARG_NOSRCCHECK : 0, ip_hdr(skb)->tos, READ_ONCE(tcp_rsk(req)->txhash)); + kfree(traffic_key); } =20 /* @@ -1630,6 +1673,10 @@ const struct tcp_request_sock_ops tcp_request_sock_i= pv4_ops =3D { .req_md5_lookup =3D tcp_v4_md5_lookup, .calc_md5_hash =3D tcp_v4_md5_hash_skb, #endif +#ifdef CONFIG_TCP_AO + .ao_lookup =3D tcp_v4_ao_lookup_rsk, + .ao_calc_key =3D tcp_v4_ao_calc_key_rsk, +#endif #ifdef CONFIG_SYN_COOKIES .cookie_init_seq =3D cookie_v4_init_sequence, #endif @@ -1731,12 +1778,16 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock= *sk, struct sk_buff *skb, /* Copy over the MD5 key from the original socket */ addr =3D (union tcp_md5_addr *)&newinet->inet_daddr; key =3D tcp_md5_do_lookup(sk, l3index, addr, AF_INET); - if (key) { + if (key && !tcp_rsk_used_ao(req)) { if (tcp_md5_key_copy(newsk, addr, AF_INET, 32, l3index, key)) goto put_and_exit; sk_gso_disable(newsk); } #endif +#ifdef CONFIG_TCP_AO + if (tcp_ao_copy_all_matching(sk, newsk, req, skb, AF_INET)) + goto put_and_exit; /* OOM, release back memory */ +#endif =20 if (__inet_inherit_port(sk, newsk) < 0) goto put_and_exit; diff --git a/net/ipv4/tcp_minisocks.c b/net/ipv4/tcp_minisocks.c index d2bddd0099ec..c9360fbcd0b7 100644 --- a/net/ipv4/tcp_minisocks.c +++ b/net/ipv4/tcp_minisocks.c @@ -506,6 +506,9 @@ struct sock *tcp_create_openreq_child(const struct sock= *sk, const struct tcp_sock *oldtp; struct tcp_sock *newtp; u32 seq; +#ifdef CONFIG_TCP_AO + struct tcp_ao_key *ao_key; +#endif =20 if (!newsk) return NULL; @@ -582,6 +585,13 @@ struct sock *tcp_create_openreq_child(const struct soc= k *sk, #ifdef CONFIG_TCP_MD5SIG newtp->md5sig_info =3D NULL; /*XXX*/ #endif +#ifdef CONFIG_TCP_AO + newtp->ao_info =3D NULL; + ao_key =3D treq->af_specific->ao_lookup(sk, req, + tcp_rsk(req)->ao_keyid, -1); + if (ao_key) + newtp->tcp_header_len +=3D tcp_ao_len(ao_key); + #endif if (skb->len >=3D TCP_MSS_DEFAULT + newtp->tcp_header_len) newicsk->icsk_ack.last_seg_size =3D skb->len - newtp->tcp_header_len; newtp->rx_opt.mss_clamp =3D req->mss; diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 83516d6dc17a..a9e45b13720f 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -616,6 +616,7 @@ static void bpf_skops_write_hdr_opt(struct sock *sk, st= ruct sk_buff *skb, * (but it may well be that other scenarios fail similarly). */ static void tcp_options_write(struct tcphdr *th, struct tcp_sock *tp, + const struct tcp_request_sock *tcprsk, struct tcp_out_options *opts, struct tcp_ao_key *ao_key) { @@ -631,22 +632,34 @@ static void tcp_options_write(struct tcphdr *th, stru= ct tcp_sock *tp, } #ifdef CONFIG_TCP_AO if (unlikely(OPTION_AO & options)) { - struct tcp_ao_key *rnext_key; - struct tcp_ao_info *ao_info; u8 maclen; =20 if (WARN_ON_ONCE(!ao_key)) goto out_ao; - ao_info =3D rcu_dereference_check(tp->ao_info, - lockdep_sock_is_held(&tp->inet_conn.icsk_inet.sk)); - rnext_key =3D READ_ONCE(ao_info->rnext_key); - if (WARN_ON_ONCE(!rnext_key)) - goto out_ao; maclen =3D tcp_ao_maclen(ao_key); - *ptr++ =3D htonl((TCPOPT_AO << 24) | - (tcp_ao_len(ao_key) << 16) | - (ao_key->sndid << 8) | - (rnext_key->rcvid)); + + if (tcprsk) { + u8 aolen =3D maclen + sizeof(struct tcp_ao_hdr); + + *ptr++ =3D htonl((TCPOPT_AO << 24) | (aolen << 16) | + (tcprsk->ao_keyid << 8) | + (tcprsk->ao_rcv_next)); + } else if (WARN_ON_ONCE(!tp)) { + goto out_ao; + } else { + struct tcp_ao_key *rnext_key; + struct tcp_ao_info *ao_info; + + ao_info =3D rcu_dereference_check(tp->ao_info, + lockdep_sock_is_held(&tp->inet_conn.icsk_inet.sk)); + rnext_key =3D READ_ONCE(ao_info->rnext_key); + if (WARN_ON_ONCE(!rnext_key)) + goto out_ao; + *ptr++ =3D htonl((TCPOPT_AO << 24) | + (tcp_ao_len(ao_key) << 16) | + (ao_key->sndid << 8) | + (rnext_key->rcvid)); + } opts->hash_location =3D (__u8 *)ptr; ptr +=3D maclen / sizeof(*ptr); if (unlikely(maclen % sizeof(*ptr))) { @@ -1413,7 +1426,7 @@ static int __tcp_transmit_skb(struct sock *sk, struct= sk_buff *skb, th->window =3D htons(min(tp->rcv_wnd, 65535U)); } =20 - tcp_options_write(th, tp, &opts, ao_key); + tcp_options_write(th, tp, NULL, &opts, ao_key); =20 #ifdef CONFIG_TCP_MD5SIG /* Calculate the MD5 hash, as we have all we need now */ @@ -3786,7 +3799,7 @@ struct sk_buff *tcp_make_synack(const struct sock *sk= , struct dst_entry *dst, =20 /* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */ th->window =3D htons(min(req->rsk_rcv_wnd, 65535U)); - tcp_options_write(th, NULL, &opts, NULL); + tcp_options_write(th, NULL, NULL, &opts, NULL); th->doff =3D (tcp_header_size >> 2); TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTSEGS); =20 diff --git a/net/ipv6/syncookies.c b/net/ipv6/syncookies.c index 5014aa663452..ad7a8caa7b2a 100644 --- a/net/ipv6/syncookies.c +++ b/net/ipv6/syncookies.c @@ -214,6 +214,8 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk= _buff *skb) treq->snt_isn =3D cookie; treq->ts_off =3D 0; treq->txhash =3D net_tx_rndhash(); + tcp_ao_syncookie(sk, skb, treq, AF_INET6); + if (IS_ENABLED(CONFIG_SMC)) ireq->smc_ok =3D 0; =20 diff --git a/net/ipv6/tcp_ao.c b/net/ipv6/tcp_ao.c index d08735b6f3c5..c9a6fa84f6ce 100644 --- a/net/ipv6/tcp_ao.c +++ b/net/ipv6/tcp_ao.c @@ -49,6 +49,17 @@ static int tcp_v6_ao_calc_key(struct tcp_ao_key *mkt, u8= *key, return err; } =20 +int tcp_v6_ao_calc_key_skb(struct tcp_ao_key *mkt, u8 *key, + const struct sk_buff *skb, + __be32 sisn, __be32 disn) +{ + const struct ipv6hdr *iph =3D ipv6_hdr(skb); + const struct tcphdr *th =3D tcp_hdr(skb); + + return tcp_v6_ao_calc_key(mkt, key, &iph->saddr, &iph->daddr, + th->source, th->dest, sisn, disn); +} + int tcp_v6_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key, const struct sock *sk, __be32 sisn, __be32 disn, bool send) @@ -63,9 +74,21 @@ int tcp_v6_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *ke= y, htons(sk->sk_num), disn, sisn); } =20 -static struct tcp_ao_key *tcp_v6_ao_do_lookup(const struct sock *sk, - const struct in6_addr *addr, - int sndid, int rcvid) +int tcp_v6_ao_calc_key_rsk(struct tcp_ao_key *mkt, u8 *key, + struct request_sock *req) +{ + struct inet_request_sock *ireq =3D inet_rsk(req); + + return tcp_v6_ao_calc_key(mkt, key, + &ireq->ir_v6_loc_addr, &ireq->ir_v6_rmt_addr, + htons(ireq->ir_num), ireq->ir_rmt_port, + htonl(tcp_rsk(req)->snt_isn), + htonl(tcp_rsk(req)->rcv_isn)); +} + +struct tcp_ao_key *tcp_v6_ao_do_lookup(const struct sock *sk, + const struct in6_addr *addr, + int sndid, int rcvid) { return tcp_ao_do_lookup(sk, (union tcp_ao_addr *)addr, AF_INET6, sndid, rcvid); @@ -80,6 +103,15 @@ struct tcp_ao_key *tcp_v6_ao_lookup(const struct sock *= sk, return tcp_v6_ao_do_lookup(sk, addr, sndid, rcvid); } =20 +struct tcp_ao_key *tcp_v6_ao_lookup_rsk(const struct sock *sk, + struct request_sock *req, + int sndid, int rcvid) +{ + struct in6_addr *addr =3D &inet_rsk(req)->ir_v6_rmt_addr; + + return tcp_v6_ao_do_lookup(sk, addr, sndid, rcvid); +} + int tcp_v6_ao_hash_pseudoheader(struct tcp_sigpool *hp, const struct in6_addr *daddr, const struct in6_addr *saddr, int nbytes) diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 8dfa0959e403..7178e8bab922 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -842,6 +842,10 @@ const struct tcp_request_sock_ops tcp_request_sock_ipv= 6_ops =3D { .req_md5_lookup =3D tcp_v6_md5_lookup, .calc_md5_hash =3D tcp_v6_md5_hash_skb, #endif +#ifdef CONFIG_TCP_AO + .ao_lookup =3D tcp_v6_ao_lookup_rsk, + .ao_calc_key =3D tcp_v6_ao_calc_key_rsk, +#endif #ifdef CONFIG_SYN_COOKIES .cookie_init_seq =3D cookie_v6_init_sequence, #endif @@ -1194,9 +1198,51 @@ static void tcp_v6_timewait_ack(struct sock *sk, str= uct sk_buff *skb) static void tcp_v6_reqsk_send_ack(const struct sock *sk, struct sk_buff *s= kb, struct request_sock *req) { + struct tcp_md5sig_key *md5_key =3D NULL; + const struct in6_addr *addr; + u8 *traffic_key =3D NULL; int l3index; =20 l3index =3D tcp_v6_sdif(skb) ? tcp_v6_iif_l3_slave(skb) : 0; + addr =3D &ipv6_hdr(skb)->saddr; + + if (tcp_rsk_used_ao(req)) { +#ifdef CONFIG_TCP_AO + struct tcp_ao_key *ao_key =3D NULL; + const struct tcp_ao_hdr *aoh; + u8 keyid =3D 0; + + /* Invalid TCP option size or twice included auth */ + if (tcp_parse_auth_options(tcp_hdr(skb), NULL, &aoh)) + return; + if (!aoh) + return; + ao_key =3D tcp_v6_ao_do_lookup(sk, addr, aoh->rnext_keyid, -1); + if (unlikely(!ao_key)) { + /* Send ACK with any matching MKT for the peer */ + ao_key =3D tcp_v6_ao_do_lookup(sk, addr, -1, -1); + /* Matching key disappeared (user removed the key?) + * let the handshake timeout. + */ + if (!ao_key) { + net_info_ratelimited("TCP-AO key for (%pI6, %d)->(%pI6, %d) suddenly d= isappeared, won't ACK new connection\n", + addr, + ntohs(tcp_hdr(skb)->source), + &ipv6_hdr(skb)->daddr, + ntohs(tcp_hdr(skb)->dest)); + return; + } + } + traffic_key =3D kmalloc(tcp_ao_digest_size(ao_key), GFP_ATOMIC); + if (!traffic_key) + return; + + keyid =3D aoh->keyid; + tcp_v6_ao_calc_key_rsk(ao_key, traffic_key, req); +#endif + } else { + md5_key =3D tcp_v6_md5_do_lookup(sk, addr, l3index); + } =20 /* sk->sk_state =3D=3D TCP_LISTEN -> for regular TCP_SYN_RECV * sk->sk_state =3D=3D TCP_SYN_RECV -> for Fast Open. @@ -1212,11 +1258,11 @@ static void tcp_v6_reqsk_send_ack(const struct sock= *sk, struct sk_buff *skb, req->rsk_rcv_wnd >> inet_rsk(req)->rcv_wscale, tcp_time_stamp_raw() + tcp_rsk(req)->ts_off, READ_ONCE(req->ts_recent), sk->sk_bound_dev_if, - tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->saddr, l3index), + md5_key, ipv6_get_dsfield(ipv6_hdr(skb)), 0, READ_ONCE(sk->sk_priority), - READ_ONCE(tcp_rsk(req)->txhash)); - NULL, NULL, 0, 0); + READ_ONCE(tcp_rsk(req)->txhash), NULL, NULL, 0, 0); + kfree(traffic_key); } =20 =20 @@ -1446,19 +1492,26 @@ static struct sock *tcp_v6_syn_recv_sock(const stru= ct sock *sk, struct sk_buff * #ifdef CONFIG_TCP_MD5SIG l3index =3D l3mdev_master_ifindex_by_index(sock_net(sk), ireq->ir_iif); =20 - /* Copy over the MD5 key from the original socket */ - key =3D tcp_v6_md5_do_lookup(sk, &newsk->sk_v6_daddr, l3index); - if (key) { - const union tcp_md5_addr *addr; + if (!tcp_rsk_used_ao(req)) { + /* Copy over the MD5 key from the original socket */ + key =3D tcp_v6_md5_do_lookup(sk, &newsk->sk_v6_daddr, l3index); + if (key) { + const union tcp_md5_addr *addr; =20 - addr =3D (union tcp_md5_addr *)&newsk->sk_v6_daddr; - if (tcp_md5_key_copy(newsk, addr, AF_INET6, 128, l3index, key)) { - inet_csk_prepare_forced_close(newsk); - tcp_done(newsk); - goto out; + addr =3D (union tcp_md5_addr *)&newsk->sk_v6_daddr; + if (tcp_md5_key_copy(newsk, addr, AF_INET6, 128, l3index, key)) { + inet_csk_prepare_forced_close(newsk); + tcp_done(newsk); + goto out; + } } } #endif +#ifdef CONFIG_TCP_AO + /* Copy over tcp_ao_info if any */ + if (tcp_ao_copy_all_matching(sk, newsk, req, skb, AF_INET6)) + goto out; /* OOM */ +#endif =20 if (__inet_inherit_port(sk, newsk) < 0) { inet_csk_prepare_forced_close(newsk); --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC95CC04E69 for ; Tue, 15 Aug 2023 19:16:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239969AbjHOTQI (ORCPT ); Tue, 15 Aug 2023 15:16:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239904AbjHOTPa (ORCPT ); Tue, 15 Aug 2023 15:15:30 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 637281BF0 for ; Tue, 15 Aug 2023 12:15:24 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-3fe5695b180so52040315e9.2 for ; Tue, 15 Aug 2023 12:15:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126923; x=1692731723; 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=GDe4NGW77Pk9axgi6DKh/Q/v7MepQ5FMtjjS8FY61/w=; b=KisPezGO8VXBjMTc9X7sdlRbbJ6A4rc7aL3pR858urPoH5TJ1LuMjsGpQs5cAmwrXu PBtiYEgpszr8UPw5ZinyaRC7LlBVCrUEtMAQi7t3bWTjF9JpkFAiPumPOZvgPLX11eTk Yp7fqUiF0DphFIF7L9RabrzE032BxrrBBS0PqXnSuhBQk35k55EOhODUuuQK8yZAVJp5 6mDzAqxDhmpTMeWXZPg+hGC82CRVYCTCNGhbpwNfn9wDn6EgjqN2DDoDF/r6wEOACEAP 7+yoa/wYSQfR86zHZGmVQqDblJhwR4iDSKR9oEJaYTCCDuM28AS4bShSYA7ZFgAGg++D kFBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126923; x=1692731723; 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=GDe4NGW77Pk9axgi6DKh/Q/v7MepQ5FMtjjS8FY61/w=; b=T+ZPN8z8lQRViiMR7GGiLFsEXSVQhypoeljhgUB4FchCQz3WTWqi2Cqg8rY1N6eEol pKH/qTuXhnYw7DSG6aN1vHgganEz5YKbO9GiVzIhUeu85VhdXsa0/x1jJVNIMCu+Q3Od UxzrlRWkxUjjw1bNULgJ9ziB7V0O+KpMKj6RD6IUjyIUbTdsNrnIHdU+WX/Eqkzzf7ZJ /tnjYnHfkfEH0Z87LiiY0gsIMz6MQ3JFqoEU4TlJjEjf2d42kItn43mXHm9oO8uNJoxb I5bqRE6YK6sVlr12KOC5i4Y/e7PTpiqEeplAHIkrPtWxk4rL4UBQXRNwNuZI9GodMtIT OPsg== X-Gm-Message-State: AOJu0Yxv8aLV/dENT/1c23HmLNqEieXOZz54lNV57+IBRyjly1n6uCIP mLxFXmmHZWsSg2pAwPt10iWyDQ== X-Google-Smtp-Source: AGHT+IFhl5GRyilhO/dePoGolCvGj4DTb8YDmEJuKLRqrL9lBz1d4uLx7Yj65Bk5wO/Svw12Bk/iOA== X-Received: by 2002:adf:e90c:0:b0:317:9537:d73f with SMTP id f12-20020adfe90c000000b003179537d73fmr9895921wrm.30.1692126922734; Tue, 15 Aug 2023 12:15:22 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:22 -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 v10 net-next 11/23] net/tcp: Sign SYN-ACK segments with TCP-AO Date: Tue, 15 Aug 2023 20:14:40 +0100 Message-ID: <20230815191455.1872316-12-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Similarly to RST segments, wire SYN-ACKs to TCP-AO. tcp_rsk_used_ao() is handy here to check if the request socket used AO and needs a signature on the outgoing segments. 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 Acked-by: David Ahern --- include/net/tcp.h | 3 +++ include/net/tcp_ao.h | 6 +++++ net/ipv4/tcp_ao.c | 22 +++++++++++++++++ net/ipv4/tcp_ipv4.c | 1 + net/ipv4/tcp_output.c | 57 ++++++++++++++++++++++++++++++++++++++----- net/ipv6/tcp_ao.c | 22 +++++++++++++++++ net/ipv6/tcp_ipv6.c | 1 + 7 files changed, 106 insertions(+), 6 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index 710d75b23b77..97fbe14a1275 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -2180,6 +2180,9 @@ struct tcp_request_sock_ops { struct request_sock *req, int sndid, int rcvid); int (*ao_calc_key)(struct tcp_ao_key *mkt, u8 *key, struct request_sock *= sk); + int (*ao_synack_hash)(char *ao_hash, struct tcp_ao_key *mkt, + struct request_sock *req, const struct sk_buff *skb, + int hash_offset, u32 sne); #endif #ifdef CONFIG_SYN_COOKIES __u32 (*cookie_init_seq)(const struct sk_buff *skb, diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index 409646be90c3..ead52113591e 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -144,6 +144,9 @@ int tcp_ao_prepare_reset(const struct sock *sk, struct = sk_buff *skb, int tcp_v4_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen= ); struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *ad= dr_sk, int sndid, int rcvid); +int tcp_v4_ao_synack_hash(char *ao_hash, struct tcp_ao_key *mkt, + struct request_sock *req, const struct sk_buff *skb, + int hash_offset, u32 sne); int tcp_v4_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key, const struct sock *sk, __be32 sisn, __be32 disn, bool send); @@ -178,6 +181,9 @@ int tcp_v6_ao_hash_skb(char *ao_hash, struct tcp_ao_key= *key, const struct sock *sk, const struct sk_buff *skb, const u8 *tkey, int hash_offset, u32 sne); int tcp_v6_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen= ); +int tcp_v6_ao_synack_hash(char *ao_hash, struct tcp_ao_key *ao_key, + struct request_sock *req, const struct sk_buff *skb, + int hash_offset, u32 sne); void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *skb); void tcp_ao_connect_init(struct sock *sk); void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb, diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index 94ca87240c17..a3f26a2ab690 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -568,6 +568,28 @@ int tcp_v4_ao_hash_skb(char *ao_hash, struct tcp_ao_ke= y *key, tkey, hash_offset, sne); } =20 +int tcp_v4_ao_synack_hash(char *ao_hash, struct tcp_ao_key *ao_key, + struct request_sock *req, const struct sk_buff *skb, + int hash_offset, u32 sne) +{ + void *hash_buf =3D NULL; + int err; + + hash_buf =3D kmalloc(tcp_ao_digest_size(ao_key), GFP_ATOMIC); + if (!hash_buf) + return -ENOMEM; + + err =3D tcp_v4_ao_calc_key_rsk(ao_key, hash_buf, req); + if (err) + goto out; + + err =3D tcp_ao_hash_skb(AF_INET, ao_hash, ao_key, req_to_sk(req), skb, + hash_buf, hash_offset, sne); +out: + kfree(hash_buf); + return err; +} + struct tcp_ao_key *tcp_v4_ao_lookup_rsk(const struct sock *sk, struct request_sock *req, int sndid, int rcvid) diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 968692f76fa2..c7487f30af42 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -1676,6 +1676,7 @@ const struct tcp_request_sock_ops tcp_request_sock_ip= v4_ops =3D { #ifdef CONFIG_TCP_AO .ao_lookup =3D tcp_v4_ao_lookup_rsk, .ao_calc_key =3D tcp_v4_ao_calc_key_rsk, + .ao_synack_hash =3D tcp_v4_ao_synack_hash, #endif #ifdef CONFIG_SYN_COOKIES .cookie_init_seq =3D cookie_v4_init_sequence, diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index a9e45b13720f..72c0c0746ce4 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -900,6 +900,7 @@ static unsigned int tcp_synack_options(const struct soc= k *sk, unsigned int mss, struct sk_buff *skb, struct tcp_out_options *opts, const struct tcp_md5sig_key *md5, + const struct tcp_ao_key *ao, struct tcp_fastopen_cookie *foc, enum tcp_synack_type synack_type, struct sk_buff *syn_skb) @@ -921,6 +922,14 @@ static unsigned int tcp_synack_options(const struct so= ck *sk, ireq->tstamp_ok &=3D !ireq->sack_ok; } #endif +#ifdef CONFIG_TCP_AO + if (ao) { + opts->options |=3D OPTION_AO; + remaining -=3D tcp_ao_len(ao); + ireq->tstamp_ok &=3D !ireq->sack_ok; + } +#endif + WARN_ON_ONCE(md5 && ao); =20 /* We always send an MSS option. */ opts->mss =3D mss; @@ -3720,6 +3729,7 @@ struct sk_buff *tcp_make_synack(const struct sock *sk= , struct dst_entry *dst, struct inet_request_sock *ireq =3D inet_rsk(req); const struct tcp_sock *tp =3D tcp_sk(sk); struct tcp_md5sig_key *md5 =3D NULL; + struct tcp_ao_key *ao_key =3D NULL; struct tcp_out_options opts; struct sk_buff *skb; int tcp_header_size; @@ -3770,16 +3780,43 @@ struct sk_buff *tcp_make_synack(const struct sock *= sk, struct dst_entry *dst, tcp_rsk(req)->snt_synack =3D tcp_skb_timestamp_us(skb); } =20 -#ifdef CONFIG_TCP_MD5SIG +#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) rcu_read_lock(); - md5 =3D tcp_rsk(req)->af_specific->req_md5_lookup(sk, req_to_sk(req)); #endif + if (tcp_rsk_used_ao(req)) { +#ifdef CONFIG_TCP_AO + u8 maclen =3D tcp_rsk(req)->maclen; + u8 keyid =3D tcp_rsk(req)->ao_keyid; + + ao_key =3D tcp_sk(sk)->af_specific->ao_lookup(sk, req_to_sk(req), + keyid, -1); + /* If there is no matching key - avoid sending anything, + * especially usigned segments. It could try harder and lookup + * for another peer-matching key, but the peer has requested + * ao_keyid (RFC5925 RNextKeyID), so let's keep it simple here. + */ + if (unlikely(!ao_key || tcp_ao_maclen(ao_key) !=3D maclen)) { + rcu_read_unlock(); + skb_dst_drop(skb); + kfree_skb(skb); + net_warn_ratelimited("TCP-AO: the keyid %u with maclen %u|%u from SYN p= acket is not present - not sending SYNACK\n", + keyid, maclen, + ao_key ? tcp_ao_maclen(ao_key) : 0); + return NULL; + } +#endif + } else { +#ifdef CONFIG_TCP_MD5SIG + md5 =3D tcp_rsk(req)->af_specific->req_md5_lookup(sk, + req_to_sk(req)); +#endif + } skb_set_hash(skb, READ_ONCE(tcp_rsk(req)->txhash), PKT_HASH_TYPE_L4); /* bpf program will be interested in the tcp_flags */ TCP_SKB_CB(skb)->tcp_flags =3D TCPHDR_SYN | TCPHDR_ACK; tcp_header_size =3D tcp_synack_options(sk, req, mss, skb, &opts, md5, - foc, synack_type, - syn_skb) + sizeof(*th); + ao_key, foc, synack_type, syn_skb) + + sizeof(*th); =20 skb_push(skb, tcp_header_size); skb_reset_transport_header(skb); @@ -3799,7 +3836,7 @@ struct sk_buff *tcp_make_synack(const struct sock *sk= , struct dst_entry *dst, =20 /* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */ th->window =3D htons(min(req->rsk_rcv_wnd, 65535U)); - tcp_options_write(th, NULL, NULL, &opts, NULL); + tcp_options_write(th, NULL, tcp_rsk(req), &opts, ao_key); th->doff =3D (tcp_header_size >> 2); TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTSEGS); =20 @@ -3807,7 +3844,15 @@ struct sk_buff *tcp_make_synack(const struct sock *s= k, struct dst_entry *dst, /* Okay, we have all we need - do the md5 hash if needed */ if (md5) tcp_rsk(req)->af_specific->calc_md5_hash(opts.hash_location, - md5, req_to_sk(req), skb); + md5, req_to_sk(req), skb); +#endif +#ifdef CONFIG_TCP_AO + if (ao_key) + tcp_rsk(req)->af_specific->ao_synack_hash(opts.hash_location, + ao_key, req, skb, + opts.hash_location - (u8 *)th, 0); +#endif +#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) rcu_read_unlock(); #endif =20 diff --git a/net/ipv6/tcp_ao.c b/net/ipv6/tcp_ao.c index c9a6fa84f6ce..99753e12c08c 100644 --- a/net/ipv6/tcp_ao.c +++ b/net/ipv6/tcp_ao.c @@ -144,3 +144,25 @@ int tcp_v6_parse_ao(struct sock *sk, int cmd, { return tcp_parse_ao(sk, cmd, AF_INET6, optval, optlen); } + +int tcp_v6_ao_synack_hash(char *ao_hash, struct tcp_ao_key *ao_key, + struct request_sock *req, const struct sk_buff *skb, + int hash_offset, u32 sne) +{ + void *hash_buf =3D NULL; + int err; + + hash_buf =3D kmalloc(tcp_ao_digest_size(ao_key), GFP_ATOMIC); + if (!hash_buf) + return -ENOMEM; + + err =3D tcp_v6_ao_calc_key_rsk(ao_key, hash_buf, req); + if (err) + goto out; + + err =3D tcp_ao_hash_skb(AF_INET6, ao_hash, ao_key, req_to_sk(req), skb, + hash_buf, hash_offset, sne); +out: + kfree(hash_buf); + return err; +} diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 7178e8bab922..016871c6520f 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -845,6 +845,7 @@ const struct tcp_request_sock_ops tcp_request_sock_ipv6= _ops =3D { #ifdef CONFIG_TCP_AO .ao_lookup =3D tcp_v6_ao_lookup_rsk, .ao_calc_key =3D tcp_v6_ao_calc_key_rsk, + .ao_synack_hash =3D tcp_v6_ao_synack_hash, #endif #ifdef CONFIG_SYN_COOKIES .cookie_init_seq =3D cookie_v6_init_sequence, --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47BA3C07E8A for ; Tue, 15 Aug 2023 19:16:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240033AbjHOTQR (ORCPT ); Tue, 15 Aug 2023 15:16:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239911AbjHOTPc (ORCPT ); Tue, 15 Aug 2023 15:15:32 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 458471BD1 for ; Tue, 15 Aug 2023 12:15:26 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-3fea0640d88so17743965e9.2 for ; Tue, 15 Aug 2023 12:15:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126925; x=1692731725; 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=vOlcysZrpAjT7Ee49AooJ0NVHkyLglp2SFthUViu/to=; b=c6gfzNBcXybInSHWAw5VhgpnDTNdA2qesSnfeBMdrQ3N6unv1V/YTeoa5ChWVjjtAD v/QIBgPkzKEoTsCX8YwBmTuQtRZ5jsbFcf92wDnrbpSqY76YttFoM23uyLv3ALHykLdf k7tTtYRJp5Ppt7swiNUMM5/LMiVV7kh8mPDYdzlur2DVZOVNB4yE0/CQ6z0WgscPgq65 oFskZ8v58BJbrk2nUei9N+n3GxR17Tgnsw2QOCl1krnPhl9PP/oM7l19njemmaXozDj5 JwOSz5Of2LqgXRR5miQKer65fM2rWJPAdQqGjvxhCEtOJEpEnkzskQRjS5tEIfpZLfUC u9QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126925; x=1692731725; 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=vOlcysZrpAjT7Ee49AooJ0NVHkyLglp2SFthUViu/to=; b=YApAuU8YMpXkan9stJ9StiAHq07nSqGtRU1jpAgM1flwiCbBFBFjLKi3CKhtgoJsOr HoK/fiAY5OdZs+Z3V07kCvXQA/UQhtNt6qew2STTVB/xdHQKzHPEXfbd2v+Oe27kTVdz hOYioqnqTeCI3QREPt/Unt0WMaftrWYalkerEO6e7q9+CwYJKx8GuaFQOLqjoASU6sGe 8s9q+V2mU1Xc2AQkq1qLaoFF6ZbQ9OJ5OXAwbCrRTCHH2INlngqZFDWJUIZiAlCQek5Z PtnAbeHe2EzTvuY4WY1YkYfHDXwLHnvz4aLp13KooDFPq7oSPFszWZbVXOSY1ro5S/IM L/OA== X-Gm-Message-State: AOJu0YyezhaGFvBjheuZ3ewWtoEyZ8SSmHUHyUijC/BRUXwd6QLkc51L z5pnWxksUnkXaM31aqdiN8cdfA== X-Google-Smtp-Source: AGHT+IHqdXuplhxcYyyVHLbBNO/pKky5VNKhNp6K4UW3PGDiAAoH509PB/Q/Otk5FB1qVz6vNrZ1YQ== X-Received: by 2002:a05:600c:280b:b0:3fe:16c8:65fa with SMTP id m11-20020a05600c280b00b003fe16c865famr10643904wmb.4.1692126924584; Tue, 15 Aug 2023 12:15:24 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:24 -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 v10 net-next 12/23] net/tcp: Verify inbound TCP-AO signed segments Date: Tue, 15 Aug 2023 20:14:41 +0100 Message-ID: <20230815191455.1872316-13-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Now there is a common function to verify signature on TCP segments: tcp_inbound_hash(). It has checks for all possible cross-interactions with MD5 signs as well as with unsigned segments. The rules from RFC5925 are: (1) Any TCP segment can have at max only one signature. (2) TCP connections can't switch between using TCP-MD5 and TCP-AO. (3) TCP-AO connections can't stop using AO, as well as unsigned connections can't suddenly start using 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 Acked-by: David Ahern --- include/net/dropreason-core.h | 17 ++++ include/net/tcp.h | 53 ++++++++++++- include/net/tcp_ao.h | 15 ++++ net/ipv4/tcp.c | 39 ++-------- net/ipv4/tcp_ao.c | 143 ++++++++++++++++++++++++++++++++++ net/ipv4/tcp_ipv4.c | 10 +-- net/ipv6/tcp_ao.c | 9 ++- net/ipv6/tcp_ipv6.c | 11 +-- 8 files changed, 250 insertions(+), 47 deletions(-) diff --git a/include/net/dropreason-core.h b/include/net/dropreason-core.h index 05255bffa2c8..1de089ab73fd 100644 --- a/include/net/dropreason-core.h +++ b/include/net/dropreason-core.h @@ -24,6 +24,10 @@ FN(TCP_MD5NOTFOUND) \ FN(TCP_MD5UNEXPECTED) \ FN(TCP_MD5FAILURE) \ + FN(TCP_AONOTFOUND) \ + FN(TCP_AOUNEXPECTED) \ + FN(TCP_AOKEYNOTFOUND) \ + FN(TCP_AOFAILURE) \ FN(SOCKET_BACKLOG) \ FN(TCP_FLAGS) \ FN(TCP_ZEROWINDOW) \ @@ -161,6 +165,19 @@ enum skb_drop_reason { * to LINUX_MIB_TCPMD5FAILURE */ SKB_DROP_REASON_TCP_MD5FAILURE, + /** + * @SKB_DROP_REASON_TCP_AONOTFOUND: no TCP-AO hash and one was expected + */ + SKB_DROP_REASON_TCP_AONOTFOUND, + /** + * @SKB_DROP_REASON_TCP_AOUNEXPECTED: TCP-AO hash is present and it + * was not expected. + */ + SKB_DROP_REASON_TCP_AOUNEXPECTED, + /** @SKB_DROP_REASON_TCP_AOKEYNOTFOUND: TCP-AO key is unknown */ + SKB_DROP_REASON_TCP_AOKEYNOTFOUND, + /** @SKB_DROP_REASON_TCP_AOFAILURE: TCP-AO hash is wrong */ + SKB_DROP_REASON_TCP_AOFAILURE, /** * @SKB_DROP_REASON_SOCKET_BACKLOG: failed to add skb to socket backlog ( * see LINUX_MIB_TCPBACKLOGDROP) diff --git a/include/net/tcp.h b/include/net/tcp.h index 97fbe14a1275..684d6757e26f 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -1772,7 +1772,7 @@ tcp_md5_do_lookup_any_l3index(const struct sock *sk, enum skb_drop_reason tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb, const void *saddr, const void *daddr, - int family, int dif, int sdif); + int family, int l3index, const __u8 *hash_location); =20 =20 #define tcp_twsk_md5_key(twsk) ((twsk)->tw_md5_key) @@ -1794,7 +1794,7 @@ tcp_md5_do_lookup_any_l3index(const struct sock *sk, static inline enum skb_drop_reason tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb, const void *saddr, const void *daddr, - int family, int dif, int sdif) + int family, int l3index, const __u8 *hash_location) { return SKB_NOT_DROPPED_YET; } @@ -2624,4 +2624,53 @@ static inline bool tcp_ao_required(struct sock *sk, = const void *saddr, return false; } =20 +/* Called with rcu_read_lock() */ +static inline enum skb_drop_reason +tcp_inbound_hash(struct sock *sk, const struct request_sock *req, + const struct sk_buff *skb, + const void *saddr, const void *daddr, + int family, int dif, int sdif) +{ + const struct tcphdr *th =3D tcp_hdr(skb); + const struct tcp_ao_hdr *aoh; + const __u8 *md5_location; + int l3index; + + /* Invalid option or two times meet any of auth options */ + if (tcp_parse_auth_options(th, &md5_location, &aoh)) + return SKB_DROP_REASON_TCP_AUTH_HDR; + + if (req) { + if (tcp_rsk_used_ao(req) !=3D !!aoh) + return SKB_DROP_REASON_TCP_AOFAILURE; + } + + /* sdif set, means packet ingressed via a device + * in an L3 domain and dif is set to the l3mdev + */ + l3index =3D sdif ? dif : 0; + + /* Fast path: unsigned segments */ + if (likely(!md5_location && !aoh)) { + /* Drop if there's TCP-MD5 or TCP-AO key with any rcvid/sndid + * for the remote peer. On TCP-AO established connection + * the last key is impossible to remove, so there's + * always at least one current_key. + */ + if (tcp_ao_required(sk, saddr, family)) + 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); + return SKB_DROP_REASON_TCP_MD5NOTFOUND; + } + return SKB_NOT_DROPPED_YET; + } + + if (aoh) + return tcp_inbound_ao_hash(sk, skb, family, req, aoh); + + return tcp_inbound_md5_hash(sk, skb, saddr, daddr, family, + l3index, md5_location); +} + #endif /* _TCP_H */ diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index ead52113591e..e6cf7b1e74db 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -112,6 +112,10 @@ struct tcp6_ao_context { =20 struct tcp_sigpool; =20 +#define TCP_AO_ESTABLISHED (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_W= AIT2 | \ + TCPF_CLOSE | TCPF_CLOSE_WAIT | \ + TCPF_LAST_ACK | TCPF_CLOSING) + int tcp_ao_hash_skb(unsigned short int family, char *ao_hash, struct tcp_ao_key *key, const struct sock *sk, const struct sk_buff *skb, @@ -127,6 +131,10 @@ int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8= *key, void *ctx, unsigned int len, struct tcp_sigpool *hp); void tcp_ao_destroy_sock(struct sock *sk, bool twsk); void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp= ); +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, + const struct tcp_ao_hdr *aoh); struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, const union tcp_ao_addr *addr, int family, int sndid, int rcvid); @@ -197,6 +205,13 @@ static inline void tcp_ao_syncookie(struct sock *sk, c= onst struct sk_buff *skb, { } =20 +static inline 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, const struct tcp_ao_hdr *aoh) +{ + return SKB_NOT_DROPPED_YET; +} + static inline struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, const union tcp_ao_addr *addr, int family, int sndid, int rcvid) { diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 9a6a8b79dad4..4a83857fa4ea 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -4360,42 +4360,23 @@ EXPORT_SYMBOL(tcp_md5_hash_key); enum skb_drop_reason tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb, const void *saddr, const void *daddr, - int family, int dif, int sdif) + int family, int l3index, const __u8 *hash_location) { - /* - * This gets called for each TCP segment that arrives - * so we want to be efficient. + /* This gets called for each TCP segment that has TCP-MD5 option. * We have 3 drop cases: * o No MD5 hash and one expected. * o MD5 hash and we're not expecting one. * o MD5 hash and its wrong. */ - const __u8 *hash_location =3D NULL; - struct tcp_md5sig_key *hash_expected; const struct tcphdr *th =3D tcp_hdr(skb); const struct tcp_sock *tp =3D tcp_sk(sk); - int genhash, l3index; + struct tcp_md5sig_key *key; u8 newhash[16]; + int genhash; =20 - /* sdif set, means packet ingressed via a device - * in an L3 domain and dif is set to the l3mdev - */ - l3index =3D sdif ? dif : 0; + key =3D tcp_md5_do_lookup(sk, l3index, saddr, family); =20 - hash_expected =3D tcp_md5_do_lookup(sk, l3index, saddr, family); - if (tcp_parse_auth_options(th, &hash_location, NULL)) - return SKB_DROP_REASON_TCP_AUTH_HDR; - - /* We've parsed the options - do we have a hash? */ - if (!hash_expected && !hash_location) - return SKB_NOT_DROPPED_YET; - - if (hash_expected && !hash_location) { - NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND); - return SKB_DROP_REASON_TCP_MD5NOTFOUND; - } - - if (!hash_expected && hash_location) { + if (!key && hash_location) { NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED); return SKB_DROP_REASON_TCP_MD5UNEXPECTED; } @@ -4405,14 +4386,10 @@ tcp_inbound_md5_hash(const struct sock *sk, const s= truct sk_buff *skb, * IPv4-mapped case. */ if (family =3D=3D AF_INET) - genhash =3D tcp_v4_md5_hash_skb(newhash, - hash_expected, - NULL, skb); + genhash =3D tcp_v4_md5_hash_skb(newhash, key, NULL, skb); else - genhash =3D tp->af_specific->calc_md5_hash(newhash, - hash_expected, + genhash =3D tp->af_specific->calc_md5_hash(newhash, key, NULL, skb); - if (genhash || memcmp(hash_location, newhash, 16) !=3D 0) { NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5FAILURE); if (family =3D=3D AF_INET) { diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index a3f26a2ab690..d7686586c9c4 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -728,6 +728,149 @@ void tcp_ao_syncookie(struct sock *sk, const struct s= k_buff *skb, treq->maclen =3D tcp_ao_maclen(key); } =20 +static enum skb_drop_reason +tcp_ao_verify_hash(const struct sock *sk, const struct sk_buff *skb, + unsigned short int family, struct tcp_ao_info *info, + const struct tcp_ao_hdr *aoh, struct tcp_ao_key *key, + u8 *traffic_key, u8 *phash, u32 sne) +{ + u8 maclen =3D aoh->length - sizeof(struct tcp_ao_hdr); + const struct tcphdr *th =3D tcp_hdr(skb); + void *hash_buf =3D NULL; + + if (maclen !=3D tcp_ao_maclen(key)) + return SKB_DROP_REASON_TCP_AOFAILURE; + + hash_buf =3D kmalloc(tcp_ao_digest_size(key), GFP_ATOMIC); + if (!hash_buf) + return SKB_DROP_REASON_NOT_SPECIFIED; + + /* XXX: make it per-AF callback? */ + tcp_ao_hash_skb(family, hash_buf, key, sk, skb, traffic_key, + (phash - (u8 *)th), sne); + if (memcmp(phash, hash_buf, maclen)) { + kfree(hash_buf); + return SKB_DROP_REASON_TCP_AOFAILURE; + } + kfree(hash_buf); + return SKB_NOT_DROPPED_YET; +} + +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, + const struct tcp_ao_hdr *aoh) +{ + const struct tcphdr *th =3D tcp_hdr(skb); + u8 *phash =3D (u8 *)(aoh + 1); /* hash goes just after the header */ + struct tcp_ao_info *info; + enum skb_drop_reason ret; + struct tcp_ao_key *key; + __be32 sisn, disn; + u8 *traffic_key; + u32 sne =3D 0; + + info =3D rcu_dereference(tcp_sk(sk)->ao_info); + if (!info) + return SKB_DROP_REASON_TCP_AOUNEXPECTED; + + if (unlikely(th->syn)) { + sisn =3D th->seq; + disn =3D 0; + } + + /* Fast-path */ + /* TODO: fix fastopen and simultaneous open (TCPF_SYN_RECV) */ + if (likely((1 << sk->sk_state) & (TCP_AO_ESTABLISHED | TCPF_SYN_RECV))) { + enum skb_drop_reason err; + struct tcp_ao_key *current_key; + + /* Check if this socket's rnext_key matches the keyid in the + * packet. If not we lookup the key based on the keyid + * matching the rcvid in the mkt. + */ + key =3D READ_ONCE(info->rnext_key); + if (key->rcvid !=3D aoh->keyid) { + key =3D tcp_ao_established_key(info, -1, aoh->keyid); + if (!key) + goto key_not_found; + } + + /* Delayed retransmitted SYN */ + if (unlikely(th->syn && !th->ack)) + goto verify_hash; + + sne =3D 0; + /* Established socket, traffic key are cached */ + traffic_key =3D rcv_other_key(key); + err =3D tcp_ao_verify_hash(sk, skb, family, info, aoh, key, + traffic_key, phash, sne); + if (err) + return err; + current_key =3D READ_ONCE(info->current_key); + /* Key rotation: the peer asks us to use new key (RNext) */ + if (unlikely(aoh->rnext_keyid !=3D current_key->sndid)) { + /* If the key is not found we do nothing. */ + key =3D tcp_ao_established_key(info, aoh->rnext_keyid, -1); + if (key) + /* pairs with tcp_ao_del_cmd */ + WRITE_ONCE(info->current_key, key); + } + return SKB_NOT_DROPPED_YET; + } + + /* Lookup key based on peer address and keyid. + * current_key and rnext_key must not be used on tcp listen + * sockets as otherwise: + * - request sockets would race on those key pointers + * - tcp_ao_del_cmd() allows async key removal + */ + key =3D tcp_ao_inbound_lookup(family, sk, skb, -1, aoh->keyid); + if (!key) + goto key_not_found; + + if (th->syn && !th->ack) + goto verify_hash; + + if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV)) { + /* Make the initial syn the likely case here */ + if (unlikely(req)) { + sne =3D 0; + sisn =3D htonl(tcp_rsk(req)->rcv_isn); + disn =3D htonl(tcp_rsk(req)->snt_isn); + } else if (unlikely(th->ack && !th->syn)) { + /* Possible syncookie packet */ + sisn =3D htonl(ntohl(th->seq) - 1); + disn =3D htonl(ntohl(th->ack_seq) - 1); + sne =3D 0; + } else if (unlikely(!th->syn)) { + /* no way to figure out initial sisn/disn - drop */ + return SKB_DROP_REASON_TCP_FLAGS; + } + } else if (sk->sk_state =3D=3D TCP_SYN_SENT) { + disn =3D info->lisn; + if (th->syn || th->rst) + sisn =3D th->seq; + else + sisn =3D info->risn; + } else { + WARN_ONCE(1, "TCP-AO: Unexpected sk_state %d", sk->sk_state); + return SKB_DROP_REASON_TCP_AOFAILURE; + } +verify_hash: + traffic_key =3D kmalloc(tcp_ao_digest_size(key), GFP_ATOMIC); + if (!traffic_key) + return SKB_DROP_REASON_NOT_SPECIFIED; + tcp_ao_calc_key_skb(key, traffic_key, skb, sisn, disn, family); + ret =3D tcp_ao_verify_hash(sk, skb, family, info, aoh, key, + traffic_key, phash, sne); + kfree(traffic_key); + return ret; + +key_not_found: + return SKB_DROP_REASON_TCP_AOKEYNOTFOUND; +} + static int tcp_ao_cache_traffic_keys(const struct sock *sk, struct tcp_ao_info *ao, struct tcp_ao_key *ao_key) diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index c7487f30af42..d15bdaf2dff1 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -2198,9 +2198,9 @@ int tcp_v4_rcv(struct sk_buff *skb) if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) drop_reason =3D SKB_DROP_REASON_XFRM_POLICY; else - drop_reason =3D tcp_inbound_md5_hash(sk, skb, - &iph->saddr, &iph->daddr, - AF_INET, dif, sdif); + drop_reason =3D tcp_inbound_hash(sk, req, skb, + &iph->saddr, &iph->daddr, + AF_INET, dif, sdif); if (unlikely(drop_reason)) { sk_drops_add(sk, skb); reqsk_put(req); @@ -2277,8 +2277,8 @@ int tcp_v4_rcv(struct sk_buff *skb) goto discard_and_relse; } =20 - drop_reason =3D tcp_inbound_md5_hash(sk, skb, &iph->saddr, - &iph->daddr, AF_INET, dif, sdif); + drop_reason =3D tcp_inbound_hash(sk, NULL, skb, &iph->saddr, &iph->daddr, + AF_INET, dif, sdif); if (drop_reason) goto discard_and_relse; =20 diff --git a/net/ipv6/tcp_ao.c b/net/ipv6/tcp_ao.c index 99753e12c08c..8b04611c9078 100644 --- a/net/ipv6/tcp_ao.c +++ b/net/ipv6/tcp_ao.c @@ -53,11 +53,12 @@ int tcp_v6_ao_calc_key_skb(struct tcp_ao_key *mkt, u8 *= key, const struct sk_buff *skb, __be32 sisn, __be32 disn) { - const struct ipv6hdr *iph =3D ipv6_hdr(skb); - const struct tcphdr *th =3D tcp_hdr(skb); + const struct ipv6hdr *iph =3D ipv6_hdr(skb); + const struct tcphdr *th =3D tcp_hdr(skb); =20 - return tcp_v6_ao_calc_key(mkt, key, &iph->saddr, &iph->daddr, - th->source, th->dest, sisn, disn); + return tcp_v6_ao_calc_key(mkt, key, &iph->saddr, + &iph->daddr, th->source, + th->dest, sisn, disn); } =20 int tcp_v6_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key, diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 016871c6520f..bd4422064923 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -1780,9 +1780,9 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff= *skb) struct sock *nsk; =20 sk =3D req->rsk_listener; - drop_reason =3D tcp_inbound_md5_hash(sk, skb, - &hdr->saddr, &hdr->daddr, - AF_INET6, dif, sdif); + drop_reason =3D tcp_inbound_hash(sk, req, skb, + &hdr->saddr, &hdr->daddr, + AF_INET6, dif, sdif); if (drop_reason) { sk_drops_add(sk, skb); reqsk_put(req); @@ -1855,8 +1855,8 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff= *skb) goto discard_and_relse; } =20 - drop_reason =3D tcp_inbound_md5_hash(sk, skb, &hdr->saddr, &hdr->daddr, - AF_INET6, dif, sdif); + drop_reason =3D tcp_inbound_hash(sk, NULL, skb, &hdr->saddr, &hdr->daddr, + AF_INET6, dif, sdif); if (drop_reason) goto discard_and_relse; =20 @@ -2084,6 +2084,7 @@ static const struct tcp_sock_af_ops tcp_sock_ipv6_map= ped_specific =3D { .ao_lookup =3D tcp_v6_ao_lookup, .calc_ao_hash =3D tcp_v4_ao_hash_skb, .ao_parse =3D tcp_v6_parse_ao, + .ao_calc_key_sk =3D tcp_v4_ao_calc_key_sk, #endif }; #endif --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 386AEC04FE1 for ; Tue, 15 Aug 2023 19:16:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239997AbjHOTQP (ORCPT ); Tue, 15 Aug 2023 15:16:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239908AbjHOTPc (ORCPT ); Tue, 15 Aug 2023 15:15:32 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5E8DBF for ; Tue, 15 Aug 2023 12:15:27 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-3fe24dd8898so53945665e9.2 for ; Tue, 15 Aug 2023 12:15:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126926; x=1692731726; 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=f90hW5TIK4p5Lu5wkUgxZ3d6w0bTu/yMRfeKuzd3/hY=; b=C2Yi2FAQdSdL1rXhCbLCKVFWt2VHfGNf1ooTG/6DulC8ikTs+rx+N2cgB4jEqGiqD+ VaDMr+HJlp5JyPUkbRhGYDHXml5WRa10Va6BZwmVkVKxMPaN+X71N+LhfC+D/x83ninQ Pzba7gO3aXRl/8fRuf3WR64Z/g/gxHWaxlGsGcdWeQ1eE8aqRqPKtqpP1n9cS+35Oyb2 ahPsTvESteTUAXyixc+M+3IVhOB0cB1TC1V00JfRaFUnES2xMDyzdAezY9lcSpIItKjC C4cb9BFjm5zY2/eMJR2Cv4SH10DNWp1rqci0jCHpi65qvMPVSs4Mjr57AGTg21Y6OPim /d/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126926; x=1692731726; 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=f90hW5TIK4p5Lu5wkUgxZ3d6w0bTu/yMRfeKuzd3/hY=; b=GxmMCt4IbtnpUBOYUDUD0ByqfWfYBfI4+itFqgMygGdgG9jJMq2mZh/+1vR9e+QYba D0050BiwBMWsnJwgPtFG48gSR+w9NJVqPjBwOv/e3ltt4G0zHzjE9zilJ845t/Q5tCeu SPgUzVt9vRCYqxf7C3N8oa0MQ33tKw0OwMMko94bPwFiTE6xJBaBGcVJs9nTp5GnBLN1 W50ri51wSQDsng4G0pLnETHHg/z9zmibVQGHTjDCwWhSumConlJKNEUtJ8iG8hnctLP9 IRtaPVlevDJ9unzmxmYCFbf3Ec0Qb1blRuXBqIumCT04qGMTerW4kn4mEaNXAQdFbp6q ZaCg== X-Gm-Message-State: AOJu0YyYwS3lqykRBY77L0o0H7VlkRb2PoQ/lzNmMpj70VpmdIA+o+MH V4DA1YypanitsHwSCkncAbTzIw== X-Google-Smtp-Source: AGHT+IE2bhBzltgdY5fipIAjZ3h4cAIXx6unNpEOaG8cPVWu+UqhaY40zWiyZsHXljyW4fgt+JqClw== X-Received: by 2002:a7b:cc85:0:b0:3fe:108d:7f88 with SMTP id p5-20020a7bcc85000000b003fe108d7f88mr9905605wma.36.1692126926284; Tue, 15 Aug 2023 12:15:26 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:25 -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 v10 net-next 13/23] net/tcp: Add TCP-AO segments counters Date: Tue, 15 Aug 2023 20:14:42 +0100 Message-ID: <20230815191455.1872316-14-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Introduce segment counters that are useful for troubleshooting/debugging as well as for writing tests. Now there are global snmp counters as well as per-socket and per-key. 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 Acked-by: David Ahern --- include/net/dropreason-core.h | 15 +++++++++++---- include/net/tcp.h | 15 +++++++++++---- include/net/tcp_ao.h | 10 ++++++++++ include/uapi/linux/snmp.h | 4 ++++ include/uapi/linux/tcp.h | 8 +++++++- net/ipv4/proc.c | 4 ++++ net/ipv4/tcp_ao.c | 30 +++++++++++++++++++++++++++--- net/ipv4/tcp_ipv4.c | 2 +- net/ipv6/tcp_ipv6.c | 4 ++-- 9 files changed, 77 insertions(+), 15 deletions(-) diff --git a/include/net/dropreason-core.h b/include/net/dropreason-core.h index 1de089ab73fd..f93ab108df4e 100644 --- a/include/net/dropreason-core.h +++ b/include/net/dropreason-core.h @@ -166,17 +166,24 @@ enum skb_drop_reason { */ SKB_DROP_REASON_TCP_MD5FAILURE, /** - * @SKB_DROP_REASON_TCP_AONOTFOUND: no TCP-AO hash and one was expected + * @SKB_DROP_REASON_TCP_AONOTFOUND: no TCP-AO hash and one was expected, + * corresponding to LINUX_MIB_TCPAOREQUIRED */ SKB_DROP_REASON_TCP_AONOTFOUND, /** * @SKB_DROP_REASON_TCP_AOUNEXPECTED: TCP-AO hash is present and it - * was not expected. + * was not expected, corresponding to LINUX_MIB_TCPAOKEYNOTFOUND */ SKB_DROP_REASON_TCP_AOUNEXPECTED, - /** @SKB_DROP_REASON_TCP_AOKEYNOTFOUND: TCP-AO key is unknown */ + /** + * @SKB_DROP_REASON_TCP_AOKEYNOTFOUND: TCP-AO key is unknown, + * corresponding to LINUX_MIB_TCPAOKEYNOTFOUND + */ SKB_DROP_REASON_TCP_AOKEYNOTFOUND, - /** @SKB_DROP_REASON_TCP_AOFAILURE: TCP-AO hash is wrong */ + /** + * @SKB_DROP_REASON_TCP_AOFAILURE: TCP-AO hash is wrong, + * corresponding to LINUX_MIB_TCPAOBAD + */ SKB_DROP_REASON_TCP_AOFAILURE, /** * @SKB_DROP_REASON_SOCKET_BACKLOG: failed to add skb to socket backlog ( diff --git a/include/net/tcp.h b/include/net/tcp.h index 684d6757e26f..a71e6a6f5192 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -2606,7 +2606,7 @@ static inline int tcp_parse_auth_options(const struct= tcphdr *th, } =20 static inline bool tcp_ao_required(struct sock *sk, const void *saddr, - int family) + int family, bool stat_inc) { #ifdef CONFIG_TCP_AO struct tcp_ao_info *ao_info; @@ -2618,8 +2618,13 @@ static inline bool tcp_ao_required(struct sock *sk, = const void *saddr, return false; =20 ao_key =3D tcp_ao_do_lookup(sk, saddr, family, -1, -1); - if (ao_info->ao_required || ao_key) + if (ao_info->ao_required || ao_key) { + if (stat_inc) { + NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOREQUIRED); + atomic64_inc(&ao_info->counters.ao_required); + } return true; + } #endif return false; } @@ -2641,8 +2646,10 @@ tcp_inbound_hash(struct sock *sk, const struct reque= st_sock *req, return SKB_DROP_REASON_TCP_AUTH_HDR; =20 if (req) { - if (tcp_rsk_used_ao(req) !=3D !!aoh) + if (tcp_rsk_used_ao(req) !=3D !!aoh) { + NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOBAD); return SKB_DROP_REASON_TCP_AOFAILURE; + } } =20 /* sdif set, means packet ingressed via a device @@ -2657,7 +2664,7 @@ tcp_inbound_hash(struct sock *sk, const struct reques= t_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)) + if (tcp_ao_required(sk, saddr, family, true)) 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); diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index e6cf7b1e74db..9fa8ff89f5e4 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -19,6 +19,13 @@ struct tcp_ao_hdr { u8 rnext_keyid; }; =20 +struct tcp_ao_counters { + atomic64_t pkt_good; + atomic64_t pkt_bad; + atomic64_t key_not_found; + atomic64_t ao_required; +}; + struct tcp_ao_key { struct hlist_node node; union tcp_ao_addr addr; @@ -33,6 +40,8 @@ struct tcp_ao_key { u8 rcvid; u8 maclen; struct rcu_head rcu; + atomic64_t pkt_good; + atomic64_t pkt_bad; u8 traffic_keys[]; }; =20 @@ -81,6 +90,7 @@ struct tcp_ao_info { */ struct tcp_ao_key *current_key; struct tcp_ao_key *rnext_key; + struct tcp_ao_counters counters; u32 ao_required :1, __unused :31; __be32 lisn; diff --git a/include/uapi/linux/snmp.h b/include/uapi/linux/snmp.h index 26f33a4c253d..06ddf4cd295c 100644 --- a/include/uapi/linux/snmp.h +++ b/include/uapi/linux/snmp.h @@ -296,6 +296,10 @@ enum LINUX_MIB_TCPMIGRATEREQSUCCESS, /* TCPMigrateReqSuccess */ LINUX_MIB_TCPMIGRATEREQFAILURE, /* TCPMigrateReqFailure */ LINUX_MIB_TCPPLBREHASH, /* TCPPLBRehash */ + LINUX_MIB_TCPAOREQUIRED, /* TCPAORequired */ + LINUX_MIB_TCPAOBAD, /* TCPAOBad */ + LINUX_MIB_TCPAOKEYNOTFOUND, /* TCPAOKeyNotFound */ + LINUX_MIB_TCPAOGOOD, /* TCPAOGood */ __LINUX_MIB_MAX }; =20 diff --git a/include/uapi/linux/tcp.h b/include/uapi/linux/tcp.h index 250e0ce2cc38..3fe0612ec59a 100644 --- a/include/uapi/linux/tcp.h +++ b/include/uapi/linux/tcp.h @@ -391,9 +391,15 @@ struct tcp_ao_info_opt { /* setsockopt(TCP_AO_INFO) */ __u32 set_current :1, /* corresponding ::current_key */ set_rnext :1, /* corresponding ::rnext */ ao_required :1, /* don't accept non-AO connects */ - reserved :29; /* must be 0 */ + set_counters :1, /* set/clear ::pkt_* counters */ + reserved :28; /* must be 0 */ + __u16 reserved2; /* padding, must be 0 */ __u8 current_key; /* KeyID to set as Current_key */ __u8 rnext; /* KeyID to set as Rnext_key */ + __u64 pkt_good; /* verified segments */ + __u64 pkt_bad; /* failed verification */ + __u64 pkt_key_not_found; /* could not find a key to verify */ + __u64 pkt_ao_required; /* segments missing TCP-AO sign */ } __attribute__((aligned(8))); =20 /* setsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, ...) */ diff --git a/net/ipv4/proc.c b/net/ipv4/proc.c index eaf1d3113b62..3f643cd29cfe 100644 --- a/net/ipv4/proc.c +++ b/net/ipv4/proc.c @@ -298,6 +298,10 @@ static const struct snmp_mib snmp4_net_list[] =3D { SNMP_MIB_ITEM("TCPMigrateReqSuccess", LINUX_MIB_TCPMIGRATEREQSUCCESS), SNMP_MIB_ITEM("TCPMigrateReqFailure", LINUX_MIB_TCPMIGRATEREQFAILURE), SNMP_MIB_ITEM("TCPPLBRehash", LINUX_MIB_TCPPLBREHASH), + SNMP_MIB_ITEM("TCPAORequired", LINUX_MIB_TCPAOREQUIRED), + SNMP_MIB_ITEM("TCPAOBad", LINUX_MIB_TCPAOBAD), + SNMP_MIB_ITEM("TCPAOKeyNotFound", LINUX_MIB_TCPAOKEYNOTFOUND), + SNMP_MIB_ITEM("TCPAOGood", LINUX_MIB_TCPAOGOOD), SNMP_MIB_SENTINEL }; =20 diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index d7686586c9c4..88f32b163e0e 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -182,6 +182,8 @@ static struct tcp_ao_key *tcp_ao_copy_key(struct sock *= sk, *new_key =3D *key; INIT_HLIST_NODE(&new_key->node); tcp_sigpool_get(new_key->tcp_sigpool_id); + atomic64_set(&new_key->pkt_good, 0); + atomic64_set(&new_key->pkt_bad, 0); =20 return new_key; } @@ -738,8 +740,12 @@ tcp_ao_verify_hash(const struct sock *sk, const struct= sk_buff *skb, const struct tcphdr *th =3D tcp_hdr(skb); void *hash_buf =3D NULL; =20 - if (maclen !=3D tcp_ao_maclen(key)) + if (maclen !=3D tcp_ao_maclen(key)) { + NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOBAD); + atomic64_inc(&info->counters.pkt_bad); + atomic64_inc(&key->pkt_bad); return SKB_DROP_REASON_TCP_AOFAILURE; + } =20 hash_buf =3D kmalloc(tcp_ao_digest_size(key), GFP_ATOMIC); if (!hash_buf) @@ -749,9 +755,15 @@ tcp_ao_verify_hash(const struct sock *sk, const struct= sk_buff *skb, tcp_ao_hash_skb(family, hash_buf, key, sk, skb, traffic_key, (phash - (u8 *)th), sne); if (memcmp(phash, hash_buf, maclen)) { + NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOBAD); + atomic64_inc(&info->counters.pkt_bad); + atomic64_inc(&key->pkt_bad); kfree(hash_buf); return SKB_DROP_REASON_TCP_AOFAILURE; } + NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOGOOD); + atomic64_inc(&info->counters.pkt_good); + atomic64_inc(&key->pkt_good); kfree(hash_buf); return SKB_NOT_DROPPED_YET; } @@ -771,8 +783,10 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_b= uff *skb, u32 sne =3D 0; =20 info =3D rcu_dereference(tcp_sk(sk)->ao_info); - if (!info) + if (!info) { + NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOKEYNOTFOUND); return SKB_DROP_REASON_TCP_AOUNEXPECTED; + } =20 if (unlikely(th->syn)) { sisn =3D th->seq; @@ -868,6 +882,8 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_bu= ff *skb, return ret; =20 key_not_found: + NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOKEYNOTFOUND); + atomic64_inc(&info->counters.key_not_found); return SKB_DROP_REASON_TCP_AOKEYNOTFOUND; } =20 @@ -1435,6 +1451,8 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned s= hort int family, key->keyflags =3D cmd.keyflags; key->sndid =3D cmd.sndid; key->rcvid =3D cmd.rcvid; + atomic64_set(&key->pkt_good, 0); + atomic64_set(&key->pkt_bad, 0); =20 ret =3D tcp_ao_parse_crypto(&cmd, key); if (ret < 0) @@ -1651,7 +1669,7 @@ static int tcp_ao_info_cmd(struct sock *sk, unsigned = short int family, return -EINVAL; } =20 - if (cmd.reserved !=3D 0) + if (cmd.reserved !=3D 0 || cmd.reserved2 !=3D 0) return -EINVAL; =20 ao_info =3D setsockopt_ao_info(sk); @@ -1686,6 +1704,12 @@ static int tcp_ao_info_cmd(struct sock *sk, unsigned= short int family, goto out; } } + if (cmd.set_counters) { + atomic64_set(&ao_info->counters.pkt_good, cmd.pkt_good); + atomic64_set(&ao_info->counters.pkt_bad, cmd.pkt_bad); + atomic64_set(&ao_info->counters.key_not_found, cmd.pkt_key_not_found); + atomic64_set(&ao_info->counters.ao_required, cmd.pkt_ao_required); + } =20 ao_info->ao_required =3D cmd.ao_required; if (new_current) diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index d15bdaf2dff1..883fa4403de7 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -1526,7 +1526,7 @@ static int tcp_v4_parse_md5_keys(struct sock *sk, int= optname, /* Don't allow keys for peers that have a matching TCP-AO key. * See the comment in tcp_ao_add_cmd() */ - if (tcp_ao_required(sk, addr, AF_INET)) + if (tcp_ao_required(sk, addr, AF_INET, false)) return -EKEYREJECTED; =20 return tcp_md5_do_add(sk, addr, AF_INET, prefixlen, l3index, flags, diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index bd4422064923..fd3402fe77af 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -660,7 +660,7 @@ static int tcp_v6_parse_md5_keys(struct sock *sk, int o= ptname, /* Don't allow keys for peers that have a matching TCP-AO key. * See the comment in tcp_ao_add_cmd() */ - if (tcp_ao_required(sk, addr, AF_INET)) + if (tcp_ao_required(sk, addr, AF_INET, false)) return -EKEYREJECTED; return tcp_md5_do_add(sk, addr, AF_INET, prefixlen, l3index, flags, @@ -672,7 +672,7 @@ static int tcp_v6_parse_md5_keys(struct sock *sk, int o= ptname, /* Don't allow keys for peers that have a matching TCP-AO key. * See the comment in tcp_ao_add_cmd() */ - if (tcp_ao_required(sk, addr, AF_INET6)) + if (tcp_ao_required(sk, addr, AF_INET6, false)) return -EKEYREJECTED; =20 return tcp_md5_do_add(sk, addr, AF_INET6, prefixlen, l3index, flags, --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08EB7C04FE0 for ; Tue, 15 Aug 2023 19:16:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239977AbjHOTQL (ORCPT ); Tue, 15 Aug 2023 15:16:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239914AbjHOTPd (ORCPT ); Tue, 15 Aug 2023 15:15:33 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A6003138 for ; Tue, 15 Aug 2023 12:15:29 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-3fe4cdb724cso53987135e9.1 for ; Tue, 15 Aug 2023 12:15:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126928; x=1692731728; 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=Gl4v14fnWKN5hHK7D99HSHeZgwX9WSWLARpW2GsofK8=; b=d1f3WeRc1i6/ZtkQLtIdfP4NkCbOZEhS3U1Qv02DBKgWhpJIgJ7rpSTb7TehiqPbU0 0YoHYCrnNL0bkaWZBR56bqM6JBJLPfEG87a+Pwhkm0DiKRwSy95jF5REzVtAUwOBAi+Q WQfBPgmrn5cfMwClJRzA0gEvajFzFCoAbDX2u3jvoH6KJzVJq1zzaxrIghywadgVb5J1 /heNze9cvG1tq8+mPALJstbAH8bu/uPERKXwhbvok1HyoIkn9+1XN/7O02k5zgS8tvkb DliP+au4YbL3dhf4QLn5zrCEHs8xAnV6/5Figq4ZFX5siZjh6poXA15pMc3E/YTILABl bBVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126928; x=1692731728; 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=Gl4v14fnWKN5hHK7D99HSHeZgwX9WSWLARpW2GsofK8=; b=NRdipMJgD9W3F9JkxcVRw6ENLJ5oDW5dcOj4Kqs42k0mras5Ya6DMgTJCnDfzfQ193 aug9OxqFTdd/aSBxfhEMefpqjHNl9TV1x63FENJWCsbO5Av9xv1CuNNGwIT6C7rsPcpE KuRF2yUK20zEAaphhjrpJruEhNEZOVCGXQjMpwR3aJrx3B0SAwSLeB+LVnq+ljgRf2eW OY87qwUPwscQ5Jr9AiEtTWUoF4L6MO+n2MTAwrMYQtNoIfm0LedwN8v+Pomx5yGQVlyM mfQvTqcC0plez6oqxv4QN3FlFcyvjMx3gnHOQgpbJGNW2VaQjMHW5s7pdFM1w397K6TL T64Q== X-Gm-Message-State: AOJu0YxJKKEkw6n8vfK5sErh0uxsHbPrt6RXxwrurEtteKUUqiu/bcHb PHacjAP3EhRPIloHPq1fg5DiKw== X-Google-Smtp-Source: AGHT+IH1Q9qzIEmPG8O7sbrVFVMriyt8EUHDQK20ytqIMh0PwILB9A5D4oAF/6ugmXzYZvMRpUbsaA== X-Received: by 2002:a7b:c7c9:0:b0:3fb:bc6d:41f1 with SMTP id z9-20020a7bc7c9000000b003fbbc6d41f1mr10838583wmk.17.1692126928225; Tue, 15 Aug 2023 12:15:28 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:27 -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 v10 net-next 14/23] net/tcp: Add TCP-AO SNE support Date: Tue, 15 Aug 2023 20:14:43 +0100 Message-ID: <20230815191455.1872316-15-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add Sequence Number Extension (SNE) for TCP-AO. This is needed to protect long-living TCP-AO connections from replaying attacks after sequence number roll-over, see RFC5925 (6.2). 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 Acked-by: David Ahern --- include/net/tcp_ao.h | 20 ++++++++++++++++++++ net/ipv4/tcp_ao.c | 40 +++++++++++++++++++++++++++++++++------- net/ipv4/tcp_input.c | 28 ++++++++++++++++++++++++++++ net/ipv4/tcp_ipv4.c | 1 + net/ipv4/tcp_minisocks.c | 15 ++++++++++++++- net/ipv4/tcp_output.c | 6 +++++- net/ipv6/tcp_ipv6.c | 1 + 7 files changed, 102 insertions(+), 9 deletions(-) diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index 9fa8ff89f5e4..d295ea1b6cb7 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -95,6 +95,25 @@ struct tcp_ao_info { __unused :31; __be32 lisn; __be32 risn; + /* Sequence Number Extension (SNE) are upper 4 bytes for SEQ, + * that protect TCP-AO connection from replayed old TCP segments. + * See RFC5925 (6.2). + * In order to get correct SNE, there's a helper tcp_ao_compute_sne(). + * It needs SEQ basis to understand whereabouts are lower SEQ numbers. + * According to that basis vector, it can provide incremented SNE + * when SEQ rolls over or provide decremented SNE when there's + * a retransmitted segment from before-rolling over. + * - for request sockets such basis is rcv_isn/snt_isn, which seems + * good enough as it's unexpected to receive 4 Gbytes on reqsk. + * - for full sockets the basis is rcv_nxt/snd_una. snd_una is + * taken instead of snd_nxt as currently it's easier to track + * in tcp_snd_una_update(), rather than updating SNE in all + * WRITE_ONCE(tp->snd_nxt, ...) + * - for time-wait sockets the basis is tw_rcv_nxt/tw_snd_nxt. + * tw_snd_nxt is not expected to change, while tw_rcv_nxt may. + */ + u32 snd_sne; + u32 rcv_sne; atomic_t refcnt; /* Protects twsk destruction */ struct rcu_head rcu; }; @@ -145,6 +164,7 @@ enum skb_drop_reason tcp_inbound_ao_hash(struct sock *s= k, const struct sk_buff *skb, unsigned short int family, const struct request_sock *req, const struct tcp_ao_hdr *aoh); +u32 tcp_ao_compute_sne(u32 next_sne, u32 next_seq, u32 seq); struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, const union tcp_ao_addr *addr, int family, int sndid, int rcvid); diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index 88f32b163e0e..21a711bf6921 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -401,6 +401,21 @@ static int tcp_ao_hash_pseudoheader(unsigned short int= family, return -EAFNOSUPPORT; } =20 +u32 tcp_ao_compute_sne(u32 next_sne, u32 next_seq, u32 seq) +{ + u32 sne =3D next_sne; + + if (before(seq, next_seq)) { + if (seq > next_seq) + sne--; + } else { + if (seq < next_seq) + sne++; + } + + return sne; +} + /* tcp_ao_hash_sne(struct tcp_sigpool *hp) * @hp - used for hashing * @sne - sne value @@ -639,7 +654,8 @@ int tcp_ao_prepare_reset(const struct sock *sk, struct = sk_buff *skb, =20 sisn =3D htonl(tcp_rsk(req)->rcv_isn); disn =3D htonl(tcp_rsk(req)->snt_isn); - *sne =3D 0; + *sne =3D tcp_ao_compute_sne(0, tcp_rsk(req)->snt_isn, + ntohl(seq)); } else { sisn =3D th->seq; disn =3D 0; @@ -671,11 +687,15 @@ int tcp_ao_prepare_reset(const struct sock *sk, struc= t sk_buff *skb, *keyid =3D (*key)->rcvid; } else { struct tcp_ao_key *rnext_key; + u32 snd_basis; =20 - if (sk->sk_state =3D=3D TCP_TIME_WAIT) + if (sk->sk_state =3D=3D TCP_TIME_WAIT) { ao_info =3D rcu_dereference(tcp_twsk(sk)->ao_info); - else + snd_basis =3D tcp_twsk(sk)->tw_snd_nxt; + } else { ao_info =3D rcu_dereference(tcp_sk(sk)->ao_info); + snd_basis =3D tcp_sk(sk)->snd_una; + } if (!ao_info) return -ENOENT; =20 @@ -685,7 +705,8 @@ int tcp_ao_prepare_reset(const struct sock *sk, struct = sk_buff *skb, *traffic_key =3D snd_other_key(*key); rnext_key =3D READ_ONCE(ao_info->rnext_key); *keyid =3D rnext_key->rcvid; - *sne =3D 0; + *sne =3D tcp_ao_compute_sne(READ_ONCE(ao_info->snd_sne), + snd_basis, ntohl(seq)); } return 0; } @@ -814,7 +835,8 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_bu= ff *skb, if (unlikely(th->syn && !th->ack)) goto verify_hash; =20 - sne =3D 0; + sne =3D tcp_ao_compute_sne(info->rcv_sne, tcp_sk(sk)->rcv_nxt, + ntohl(th->seq)); /* Established socket, traffic key are cached */ traffic_key =3D rcv_other_key(key); err =3D tcp_ao_verify_hash(sk, skb, family, info, aoh, key, @@ -849,14 +871,16 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_= buff *skb, if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV)) { /* Make the initial syn the likely case here */ if (unlikely(req)) { - sne =3D 0; + sne =3D tcp_ao_compute_sne(0, tcp_rsk(req)->rcv_isn, + ntohl(th->seq)); sisn =3D htonl(tcp_rsk(req)->rcv_isn); disn =3D htonl(tcp_rsk(req)->snt_isn); } else if (unlikely(th->ack && !th->syn)) { /* Possible syncookie packet */ sisn =3D htonl(ntohl(th->seq) - 1); disn =3D htonl(ntohl(th->ack_seq) - 1); - sne =3D 0; + sne =3D tcp_ao_compute_sne(0, ntohl(sisn), + ntohl(th->seq)); } else if (unlikely(!th->syn)) { /* no way to figure out initial sisn/disn - drop */ return SKB_DROP_REASON_TCP_FLAGS; @@ -955,6 +979,7 @@ void tcp_ao_connect_init(struct sock *sk) tp->tcp_header_len +=3D tcp_ao_len(key); =20 ao_info->lisn =3D htonl(tp->write_seq); + ao_info->snd_sne =3D 0; } else { /* TODO: probably, it should fail to connect() here */ rcu_assign_pointer(tp->ao_info, NULL); @@ -973,6 +998,7 @@ void tcp_ao_finish_connect(struct sock *sk, struct sk_b= uff *skb) return; =20 ao->risn =3D tcp_hdr(skb)->seq; + ao->rcv_sne =3D 0; =20 hlist_for_each_entry_rcu(key, &ao->head, node) tcp_ao_cache_traffic_keys(sk, ao, key); diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index acf9aa9ce0a0..78e7b04fac4f 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -3532,9 +3532,18 @@ static inline bool tcp_may_update_window(const struc= t tcp_sock *tp, static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack) { u32 delta =3D ack - tp->snd_una; +#ifdef CONFIG_TCP_AO + struct tcp_ao_info *ao; +#endif =20 sock_owned_by_me((struct sock *)tp); tp->bytes_acked +=3D delta; +#ifdef CONFIG_TCP_AO + ao =3D rcu_dereference_protected(tp->ao_info, + lockdep_sock_is_held((struct sock *)tp)); + if (ao && ack < tp->snd_una) + ao->snd_sne++; +#endif tp->snd_una =3D ack; } =20 @@ -3542,9 +3551,18 @@ static void tcp_snd_una_update(struct tcp_sock *tp, = u32 ack) static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq) { u32 delta =3D seq - tp->rcv_nxt; +#ifdef CONFIG_TCP_AO + struct tcp_ao_info *ao; +#endif =20 sock_owned_by_me((struct sock *)tp); tp->bytes_received +=3D delta; +#ifdef CONFIG_TCP_AO + ao =3D rcu_dereference_protected(tp->ao_info, + lockdep_sock_is_held((struct sock *)tp)); + if (ao && seq < tp->rcv_nxt) + ao->rcv_sne++; +#endif WRITE_ONCE(tp->rcv_nxt, seq); } =20 @@ -6392,6 +6410,16 @@ static int tcp_rcv_synsent_state_process(struct sock= *sk, struct sk_buff *skb, * simultaneous connect with crossed SYNs. * Particularly, it can be connect to self. */ +#ifdef CONFIG_TCP_AO + struct tcp_ao_info *ao; + + ao =3D rcu_dereference_protected(tp->ao_info, + lockdep_sock_is_held(sk)); + if (ao) { + ao->risn =3D th->seq; + ao->rcv_sne =3D 0; + } +#endif tcp_set_state(sk, TCP_SYN_RECV); =20 if (tp->rx_opt.saw_tstamp) { diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 883fa4403de7..5d25c974ff61 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -1039,6 +1039,7 @@ static void tcp_v4_timewait_ack(struct sock *sk, stru= ct sk_buff *skb) struct tcp_ao_key *rnext_key; =20 traffic_key =3D snd_other_key(ao_key); + ao_sne =3D READ_ONCE(ao_info->snd_sne); rnext_key =3D READ_ONCE(ao_info->rnext_key); rcv_next =3D rnext_key->rcvid; } diff --git a/net/ipv4/tcp_minisocks.c b/net/ipv4/tcp_minisocks.c index c9360fbcd0b7..7f69bbf8a804 100644 --- a/net/ipv4/tcp_minisocks.c +++ b/net/ipv4/tcp_minisocks.c @@ -51,6 +51,18 @@ tcp_timewait_check_oow_rate_limit(struct inet_timewait_s= ock *tw, return TCP_TW_SUCCESS; } =20 +static void twsk_rcv_nxt_update(struct tcp_timewait_sock *tcptw, u32 seq) +{ +#ifdef CONFIG_TCP_AO + struct tcp_ao_info *ao; + + ao =3D rcu_dereference(tcptw->ao_info); + if (unlikely(ao && seq < tcptw->tw_rcv_nxt)) + WRITE_ONCE(ao->rcv_sne, ao->rcv_sne + 1); +#endif + tcptw->tw_rcv_nxt =3D seq; +} + /* * * Main purpose of TIME-WAIT state is to close connection gracefully, * when one of ends sits in LAST-ACK or CLOSING retransmitting FIN @@ -136,7 +148,8 @@ tcp_timewait_state_process(struct inet_timewait_sock *t= w, struct sk_buff *skb, =20 /* FIN arrived, enter true time-wait state. */ tw->tw_substate =3D TCP_TIME_WAIT; - tcptw->tw_rcv_nxt =3D TCP_SKB_CB(skb)->end_seq; + twsk_rcv_nxt_update(tcptw, TCP_SKB_CB(skb)->end_seq); + if (tmp_opt.saw_tstamp) { tcptw->tw_ts_recent_stamp =3D ktime_get_seconds(); tcptw->tw_ts_recent =3D tmp_opt.rcv_tsval; diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 72c0c0746ce4..675582d08f01 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -1450,6 +1450,7 @@ static int __tcp_transmit_skb(struct sock *sk, struct= sk_buff *skb, u8 *traffic_key; void *tkey_buf =3D NULL; __be32 disn; + u32 sne; =20 sk_gso_disable(sk); if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) { @@ -1467,9 +1468,12 @@ static int __tcp_transmit_skb(struct sock *sk, struc= t sk_buff *skb, } else { traffic_key =3D snd_other_key(ao_key); } + sne =3D tcp_ao_compute_sne(READ_ONCE(ao->snd_sne), + READ_ONCE(tp->snd_una), + ntohl(th->seq)); tp->af_specific->calc_ao_hash(opts.hash_location, ao_key, sk, skb, traffic_key, - opts.hash_location - (u8 *)th, 0); + opts.hash_location - (u8 *)th, sne); kfree(tkey_buf); } #endif diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index fd3402fe77af..1506de3ce5e0 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -1180,6 +1180,7 @@ static void tcp_v6_timewait_ack(struct sock *sk, stru= ct sk_buff *skb) /* rcv_next switches to our rcv_next */ rnext_key =3D READ_ONCE(ao_info->rnext_key); rcv_next =3D rnext_key->rcvid; + ao_sne =3D READ_ONCE(ao_info->snd_sne); } #endif =20 --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1876EC04FE2 for ; Tue, 15 Aug 2023 19:16:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239982AbjHOTQN (ORCPT ); Tue, 15 Aug 2023 15:16:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239913AbjHOTPd (ORCPT ); Tue, 15 Aug 2023 15:15:33 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B615E7A for ; Tue, 15 Aug 2023 12:15:31 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-31781e15a0cso5061398f8f.3 for ; Tue, 15 Aug 2023 12:15:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126930; x=1692731730; 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=lsQiK0TqLbOG2bW6KuQFoGfMxi8zrL+jjjVnjiMjbb8=; b=MSeJFeXLZHFK1M7oVOR4BeUqOV8SrDzoti06AjnZjynIqsGFqgiVw88T6S0Q84GTml cH9TTsS+IeRcvN1DkIwIJobIgMu7J/03xkfnMcnal30MqwMA3gggr69cfJ6f7iKp5iIq MWm7rEaXW33eJDzeNP1hUTcjoT0hw6mnRF58b4MMK2liMU3B+MeVz5K1owUYPo0nONXZ uG95Wd0humqvEc88xz8djE79yUhWpN9vajiC8T5+LQZI2YcdujHte3fWQ2p7/RztnmLe YdfVC9UpJYHEvU730Y6vnX4bVAtzvTGmugrXM+2kZC+4/PHtJjdTXqn/6MserjCsait3 G9Jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126930; x=1692731730; 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=lsQiK0TqLbOG2bW6KuQFoGfMxi8zrL+jjjVnjiMjbb8=; b=A2Qh9sr3UW2czgfzV+vyonFPbwcRm6tMu1V4nQYu/epDv1i7NJMH3ID96s4QOOVTeT 304blOIaGymi3hLqscVRalUjob0XIAy6GFkleOz9ik1HTlHjNwBWawNaeWCW0ryON+eg 1EnP66Y5V6sAWnNtA2Rto3lj4VdRzWg2GXzTi7eZhbcI3WE0hCFlOWCRp6g8rJeZjeGk wmEtBt30SKIzPHmQxLJiRK1xqHGVw8wPyDcZkyxq1W8IJqSTY+/EYM/jPgqVKiJgcC5F 0Qq3QeJAAKSD2MBu0f0iEVE8922TLwP8afFpw7LFd2jLc3zQtxTNUNwEa86Z9ObmvXa+ cGqQ== X-Gm-Message-State: AOJu0YyidbKrAl0qsLX8YqUR6jJ9/Y5YpP8vNhBl0Dcp16K3FGnyBCAQ N1BPMxi1LEXcFFEX0OjRFpzsEQ== X-Google-Smtp-Source: AGHT+IFlZ69hHHCZc8xFF+QoKEMeycB2BpyvGgB10lwx1JREgO1dfAe+bwOgA7jsyqfOP87LNnEp9g== X-Received: by 2002:a5d:6b90:0:b0:317:60a8:f3a7 with SMTP id n16-20020a5d6b90000000b0031760a8f3a7mr8046497wrx.10.1692126930003; Tue, 15 Aug 2023 12:15:30 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:29 -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 v10 net-next 15/23] net/tcp: Add tcp_hash_fail() ratelimited logs Date: Tue, 15 Aug 2023 20:14:44 +0100 Message-ID: <20230815191455.1872316-16-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 Acked-by: David Ahern --- 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 a71e6a6f5192..85e0f0b50261 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -2642,12 +2642,18 @@ tcp_inbound_hash(struct sock *sk, const struct requ= est_sock *req, int l3index; =20 /* 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; + } =20 if (req) { if (tcp_rsk_used_ao(req) !=3D !!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; } } @@ -2664,10 +2670,14 @@ tcp_inbound_hash(struct sock *sk, const struct requ= est_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 d295ea1b6cb7..986e8dcbb150 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -118,6 +118,35 @@ struct tcp_ao_info { struct rcu_head rcu; }; =20 +#define tcp_hash_fail(msg, family, skb, fmt, ...) \ +do { \ + const struct tcphdr *th =3D tcp_hdr(skb); \ + char hdr_flags[5] =3D {}; \ + char *f =3D hdr_flags; \ + \ + if (th->fin) \ + *f++ =3D 'F'; \ + if (th->syn) \ + *f++ =3D 'S'; \ + if (th->rst) \ + *f++ =3D 'R'; \ + if (th->ack) \ + *f++ =3D 'A'; \ + if (f !=3D hdr_flags) \ + *f =3D ' '; \ + if ((family) =3D=3D 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 */ =20 diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 4a83857fa4ea..0b742c693a1f 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -4368,7 +4368,6 @@ tcp_inbound_md5_hash(const struct sock *sk, const str= uct sk_buff *skb, * o MD5 hash and we're not expecting one. * o MD5 hash and its wrong. */ - const struct tcphdr *th =3D tcp_hdr(skb); const struct tcp_sock *tp =3D tcp_sk(sk); struct tcp_md5sig_key *key; u8 newhash[16]; @@ -4378,6 +4377,7 @@ tcp_inbound_md5_hash(const struct sock *sk, const str= uct sk_buff *skb, =20 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; } =20 @@ -4393,16 +4393,19 @@ tcp_inbound_md5_hash(const struct sock *sk, const s= truct sk_buff *skb, if (genhash || memcmp(hash_location, newhash, 16) !=3D 0) { NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5FAILURE); if (family =3D=3D AF_INET) { - net_info_ratelimited("MD5 Hash failed for (%pI4, %d)->(%pI4, %d)%s L3 i= ndex %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 21a711bf6921..226dcefb426a 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -765,6 +765,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 !=3D %d", maclen, tcp_ao_maclen(key)); return SKB_DROP_REASON_TCP_AOFAILURE; } =20 @@ -779,6 +781,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, ""); kfree(hash_buf); return SKB_DROP_REASON_TCP_AOFAILURE; } @@ -806,6 +809,8 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_bu= ff *skb, info =3D 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; } =20 @@ -908,6 +913,8 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_bu= ff *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; } =20 --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5DA99C05052 for ; Tue, 15 Aug 2023 19:16:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240049AbjHOTQT (ORCPT ); Tue, 15 Aug 2023 15:16:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239915AbjHOTPf (ORCPT ); Tue, 15 Aug 2023 15:15:35 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4317EBF for ; Tue, 15 Aug 2023 12:15:33 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-3fe8a158fcbso38883565e9.2 for ; Tue, 15 Aug 2023 12:15:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126932; x=1692731732; 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=5Yo4giMfE1jnHT6Epe0k2WfA/tWTAenv2mRIvgzOSnk=; b=RqjhJPHGz1jxstsukzRFRr4OsbMxMFREkcqVyj6DPl7aKXSmC7xOc2PiAP0Qe/dSCR 4fPdXhSp5y7sN/Dww33XX2iFJ/LECtAcv5FBEhGN8EAXdWQoq2B/ZlWd1YnDLgWmFxI6 Q80FRoKoSG93RMQrRnVXMX22UcPANOYkKr5qniZaq/lnzypBFgRJt9bNFt7zmaU2x4bb ARlRKjn/WuY2oKZV+IMds33+06+YVvSKklD4WU5qeQZQikySmNMic6ycXUv+eJV4eN/w gn/K9YugEnk+FN9Ys0GY7i6Mh+TShLXLXK4aHqfp1U2ZgJO/U/uuP4Z7Py/ldpBa1ul+ Y6QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126932; x=1692731732; 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=5Yo4giMfE1jnHT6Epe0k2WfA/tWTAenv2mRIvgzOSnk=; b=FGE7qaelrKmOyuNes1ny497qZn5zgPRHKabQyRLPH1/U74O3G4Ffnk4zRV4hLd/+xN iVExB9HIoLYgv8p1twzMxru0bfyZI3dGe5NVEE36f05xFLMYNiuPn7kbyGqdv6cik4CL FyV3moCl41Zz2mW2o/FPoHktKBm1sCRj4ZaVsLDkaiPr5L0lbXL1EDoiLAT2pMEIovzn y5SeqU/7kqXnoA9fgYpR/Rs0UXZKcQCOVuxDkweaxFxALcVks9w1g0LnjOqjuqO0wLft ZXcsUTthxhcr0S0I6Z3/dcHzZljSMP/4m7qVypblWAkkym21esc1UdGFWI+zlAKlsQgA lbHg== X-Gm-Message-State: AOJu0YzFI1C7j8eOx+nQZMzSlRgqmoDdDcWGK6Oc6sGjInkAVGHgRsZX SynKOm7BBzc9tf/4o0JLf/Q46g== X-Google-Smtp-Source: AGHT+IErztL4MM+ovuon+u9ESA3xQ//SkYYk0vgebZC8Sxvyxm+28oci3X+vueiC8oiWqbJIUcOPxw== X-Received: by 2002:a7b:c7c7:0:b0:3fe:df1:5b95 with SMTP id z7-20020a7bc7c7000000b003fe0df15b95mr11518456wmk.19.1692126931790; Tue, 15 Aug 2023 12:15:31 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:31 -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 v10 net-next 16/23] net/tcp: Ignore specific ICMPs for TCP-AO connections Date: Tue, 15 Aug 2023 20:14:45 +0100 Message-ID: <20230815191455.1872316-17-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Similarly to IPsec, RFC5925 prescribes: ">> A TCP-AO implementation MUST default to ignore incoming ICMPv4 messages of Type 3 (destination unreachable), Codes 2-4 (protocol unreachable, port unreachable, and fragmentation needed -- =E2=80=99hard errors=E2=80=99), and ICMPv6 Type 1 (destination unreachable), Code 1 (administratively prohibited) and Code 4 (port unreachable) intended for connections in synchronized states (ESTABLISHED, FIN-WAIT-1, FIN- WAIT-2, CLOSE-WAIT, CLOSING, LAST-ACK, TIME-WAIT) that match MKTs." A selftest (later in patch series) verifies that this attack is not possible in this TCP-AO implementation. 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 Acked-by: David Ahern --- include/net/tcp_ao.h | 10 ++++++- include/uapi/linux/snmp.h | 1 + include/uapi/linux/tcp.h | 4 ++- net/ipv4/proc.c | 1 + net/ipv4/tcp_ao.c | 58 +++++++++++++++++++++++++++++++++++++++ net/ipv4/tcp_ipv4.c | 7 +++++ net/ipv6/tcp_ipv6.c | 7 +++++ 7 files changed, 86 insertions(+), 2 deletions(-) diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index 986e8dcbb150..ddd2e412aae3 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -24,6 +24,7 @@ struct tcp_ao_counters { atomic64_t pkt_bad; atomic64_t key_not_found; atomic64_t ao_required; + atomic64_t dropped_icmp; }; =20 struct tcp_ao_key { @@ -92,7 +93,8 @@ struct tcp_ao_info { struct tcp_ao_key *rnext_key; struct tcp_ao_counters counters; u32 ao_required :1, - __unused :31; + accept_icmps :1, + __unused :30; __be32 lisn; __be32 risn; /* Sequence Number Extension (SNE) are upper 4 bytes for SEQ, @@ -189,6 +191,7 @@ int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 = *key, void *ctx, unsigned int len, struct tcp_sigpool *hp); void tcp_ao_destroy_sock(struct sock *sk, bool twsk); 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); 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, @@ -264,6 +267,11 @@ static inline void tcp_ao_syncookie(struct sock *sk, c= onst struct sk_buff *skb, { } =20 +static inline bool tcp_ao_ignore_icmp(const struct sock *sk, int type, int= code) +{ + return false; +} + static inline 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, const struct tcp_ao_hdr *aoh) diff --git a/include/uapi/linux/snmp.h b/include/uapi/linux/snmp.h index 06ddf4cd295c..47a6b47da66f 100644 --- a/include/uapi/linux/snmp.h +++ b/include/uapi/linux/snmp.h @@ -300,6 +300,7 @@ enum LINUX_MIB_TCPAOBAD, /* TCPAOBad */ LINUX_MIB_TCPAOKEYNOTFOUND, /* TCPAOKeyNotFound */ LINUX_MIB_TCPAOGOOD, /* TCPAOGood */ + LINUX_MIB_TCPAODROPPEDICMPS, /* TCPAODroppedIcmps */ __LINUX_MIB_MAX }; =20 diff --git a/include/uapi/linux/tcp.h b/include/uapi/linux/tcp.h index 3fe0612ec59a..ca7ed18ce67b 100644 --- a/include/uapi/linux/tcp.h +++ b/include/uapi/linux/tcp.h @@ -392,7 +392,8 @@ struct tcp_ao_info_opt { /* setsockopt(TCP_AO_INFO) */ set_rnext :1, /* corresponding ::rnext */ ao_required :1, /* don't accept non-AO connects */ set_counters :1, /* set/clear ::pkt_* counters */ - reserved :28; /* must be 0 */ + accept_icmps :1, /* accept incoming ICMPs */ + reserved :27; /* must be 0 */ __u16 reserved2; /* padding, must be 0 */ __u8 current_key; /* KeyID to set as Current_key */ __u8 rnext; /* KeyID to set as Rnext_key */ @@ -400,6 +401,7 @@ struct tcp_ao_info_opt { /* setsockopt(TCP_AO_INFO) */ __u64 pkt_bad; /* failed verification */ __u64 pkt_key_not_found; /* could not find a key to verify */ __u64 pkt_ao_required; /* segments missing TCP-AO sign */ + __u64 pkt_dropped_icmp; /* ICMPs that were ignored */ } __attribute__((aligned(8))); =20 /* setsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, ...) */ diff --git a/net/ipv4/proc.c b/net/ipv4/proc.c index 3f643cd29cfe..5d3c9c96773e 100644 --- a/net/ipv4/proc.c +++ b/net/ipv4/proc.c @@ -302,6 +302,7 @@ static const struct snmp_mib snmp4_net_list[] =3D { SNMP_MIB_ITEM("TCPAOBad", LINUX_MIB_TCPAOBAD), SNMP_MIB_ITEM("TCPAOKeyNotFound", LINUX_MIB_TCPAOKEYNOTFOUND), SNMP_MIB_ITEM("TCPAOGood", LINUX_MIB_TCPAOGOOD), + SNMP_MIB_ITEM("TCPAODroppedIcmps", LINUX_MIB_TCPAODROPPEDICMPS), SNMP_MIB_SENTINEL }; =20 diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index 226dcefb426a..3c8bdd830d96 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -15,6 +15,7 @@ =20 #include #include +#include =20 int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx, unsigned int len, struct tcp_sigpool *hp) @@ -44,6 +45,60 @@ int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *= key, void *ctx, return 1; } =20 +bool tcp_ao_ignore_icmp(const struct sock *sk, int type, int code) +{ + bool ignore_icmp =3D false; + struct tcp_ao_info *ao; + + /* RFC5925, 7.8: + * >> A TCP-AO implementation MUST default to ignore incoming ICMPv4 + * messages of Type 3 (destination unreachable), Codes 2-4 (protocol + * unreachable, port unreachable, and fragmentation needed -- =E2=80=99ha= rd + * errors=E2=80=99), and ICMPv6 Type 1 (destination unreachable), Code 1 + * (administratively prohibited) and Code 4 (port unreachable) intended + * for connections in synchronized states (ESTABLISHED, FIN-WAIT-1, FIN- + * WAIT-2, CLOSE-WAIT, CLOSING, LAST-ACK, TIME-WAIT) that match MKTs. + */ + if (sk->sk_family =3D=3D AF_INET) { + if (type !=3D ICMP_DEST_UNREACH) + return false; + if (code < ICMP_PROT_UNREACH || code > ICMP_FRAG_NEEDED) + return false; + } else { + if (type !=3D ICMPV6_DEST_UNREACH) + return false; + if (code !=3D ICMPV6_ADM_PROHIBITED && code !=3D ICMPV6_PORT_UNREACH) + return false; + } + + rcu_read_lock(); + switch (sk->sk_state) { + case TCP_TIME_WAIT: + ao =3D rcu_dereference(tcp_twsk(sk)->ao_info); + break; + case TCP_SYN_SENT: + case TCP_SYN_RECV: + case TCP_LISTEN: + case TCP_NEW_SYN_RECV: + /* RFC5925 specifies to ignore ICMPs *only* on connections + * in synchronized states. + */ + rcu_read_unlock(); + return false; + default: + ao =3D rcu_dereference(tcp_sk(sk)->ao_info); + } + + if (ao && !ao->accept_icmps) { + ignore_icmp =3D true; + __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAODROPPEDICMPS); + atomic64_inc(&ao->counters.dropped_icmp); + } + rcu_read_unlock(); + + return ignore_icmp; +} + /* Optimized version of tcp_ao_do_lookup(): only for sockets for which * it's known that the keys in ao_info are matching peer's * family/address/VRF/etc. @@ -1036,6 +1091,7 @@ int tcp_ao_copy_all_matching(const struct sock *sk, s= truct sock *newsk, new_ao->lisn =3D htonl(tcp_rsk(req)->snt_isn); new_ao->risn =3D htonl(tcp_rsk(req)->rcv_isn); new_ao->ao_required =3D ao->ao_required; + new_ao->accept_icmps =3D ao->accept_icmps; =20 if (family =3D=3D AF_INET) { addr =3D (union tcp_ao_addr *)&newsk->sk_daddr; @@ -1742,9 +1798,11 @@ static int tcp_ao_info_cmd(struct sock *sk, unsigned= short int family, atomic64_set(&ao_info->counters.pkt_bad, cmd.pkt_bad); atomic64_set(&ao_info->counters.key_not_found, cmd.pkt_key_not_found); atomic64_set(&ao_info->counters.ao_required, cmd.pkt_ao_required); + atomic64_set(&ao_info->counters.dropped_icmp, cmd.pkt_dropped_icmp); } =20 ao_info->ao_required =3D cmd.ao_required; + ao_info->accept_icmps =3D cmd.accept_icmps; if (new_current) WRITE_ONCE(ao_info->current_key, new_current); if (new_rnext) diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 5d25c974ff61..c941c13b924a 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -494,6 +494,8 @@ int tcp_v4_err(struct sk_buff *skb, u32 info) return -ENOENT; } if (sk->sk_state =3D=3D TCP_TIME_WAIT) { + /* To increase the counter of ignored icmps for TCP-AO */ + tcp_ao_ignore_icmp(sk, type, code); inet_twsk_put(inet_twsk(sk)); return 0; } @@ -507,6 +509,11 @@ int tcp_v4_err(struct sk_buff *skb, u32 info) return 0; } =20 + if (tcp_ao_ignore_icmp(sk, type, code)) { + sock_put(sk); + return 0; + } + bh_lock_sock(sk); /* If too many ICMPs get dropped on busy * servers this needs to be solved differently. diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 1506de3ce5e0..402ec6ecb0e6 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -395,6 +395,8 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6= _skb_parm *opt, } =20 if (sk->sk_state =3D=3D TCP_TIME_WAIT) { + /* To increase the counter of ignored icmps for TCP-AO */ + tcp_ao_ignore_icmp(sk, type, code); inet_twsk_put(inet_twsk(sk)); return 0; } @@ -405,6 +407,11 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet= 6_skb_parm *opt, return 0; } =20 + if (tcp_ao_ignore_icmp(sk, type, code)) { + sock_put(sk); + return 0; + } + bh_lock_sock(sk); if (sock_owned_by_user(sk) && type !=3D ICMPV6_PKT_TOOBIG) __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS); --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D83FC07E8C for ; Tue, 15 Aug 2023 19:16:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240061AbjHOTQV (ORCPT ); Tue, 15 Aug 2023 15:16:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239916AbjHOTPg (ORCPT ); Tue, 15 Aug 2023 15:15:36 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F329910E for ; Tue, 15 Aug 2023 12:15:34 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-3fe8a1591c8so42241925e9.3 for ; Tue, 15 Aug 2023 12:15:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126933; x=1692731733; 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=PptJC8HnsVehgFMOHfEd3dK6jYdc+oOKBr6EQXBoEWk=; b=YNU3Z6jg9cpRSUJZ/KEq/qT//ehinXdRWIykMO9tlmkI1QaGzYd6S/k+kzhCjwITYo qRCDXkphqvXGEaYvJZiIbpjnRra9cWlcIjczkb5fZ7voN6+qziVINHF97cFoDi6Y9CCy z00aOkCFYldqjuwiddHPs6ekFeSkWGW1N+DK29OT1Q4mZGGhPlUYWkgbTH6gtajMJTMZ NOdruAF96LOr8NeG+C8Z8ULGDrNxaY4+B97jo2luWjdNE2mt/6LM+ceXCMXtAuudMstE ZjJb4JLsB3xgeWtkQOti+TmdoUXPrK0prh9JcXPskfMk3s9b/TyDLvFIPxi7qaOoe0wA L4yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126933; x=1692731733; 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=PptJC8HnsVehgFMOHfEd3dK6jYdc+oOKBr6EQXBoEWk=; b=HFBlpPZWAxhlQEjKdr1BetP0+ZU9/JbQvzUlsv7Tw/1ZTAqEjMsjPdQxDMJTw+USva FPhcpGwjRXSD7KsvGoV3eGEXD7Wizow7xB55hW+R1AU9PuQIyu8YPmeapXjLhQNI1FkH n0Q4r1d36rmCfqWAb7g7uDwDO+nlthGmOOxBj01OUC7teu4gIJF+bJtPGGbDHxbnlYEG shetOpvc/coaUhkVHsJgg8/7lxvfyhVK0gAnS9FMusSq+8rr5+58FkBHD6XKQKGOpAaT D/TBkayl3d/5QCUOSsmXdivewnYUkeDRd4cjddOFaZlML1AIZFUHdsNruMJ40xndPhU2 c5MA== X-Gm-Message-State: AOJu0Ywo9LBpzBQVIsgAFGGCuSLWKVqGGgcm1++5NXx2iMGqVbs7JISz 7yDal0pGfurhvzchk3ZHZfgsHQ== X-Google-Smtp-Source: AGHT+IEemXbg8bowQ1LZyBMyLl2MIxu41KDDUMqDu9beYVftDqv1WcdGX3VbMNMTWf8vBmAIAepE2w== X-Received: by 2002:a05:600c:290a:b0:3fe:1deb:86 with SMTP id i10-20020a05600c290a00b003fe1deb0086mr11469001wmd.28.1692126933458; Tue, 15 Aug 2023 12:15:33 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:33 -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 v10 net-next 17/23] net/tcp: Add option for TCP-AO to (not) hash header Date: Tue, 15 Aug 2023 20:14:46 +0100 Message-ID: <20230815191455.1872316-18-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Provide setsockopt() key flag that makes TCP-AO exclude hashing TCP header for peers that match the key. This is needed for interraction with middleboxes that may change TCP options, see RFC5925 (9.2). 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 Acked-by: David Ahern --- include/uapi/linux/tcp.h | 5 +++++ net/ipv4/tcp_ao.c | 8 +++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/include/uapi/linux/tcp.h b/include/uapi/linux/tcp.h index ca7ed18ce67b..3275ade3293a 100644 --- a/include/uapi/linux/tcp.h +++ b/include/uapi/linux/tcp.h @@ -354,6 +354,11 @@ struct tcp_diag_md5sig { #define TCP_AO_MAXKEYLEN 80 =20 #define TCP_AO_KEYF_IFINDEX (1 << 0) /* L3 ifindex for VRF */ +#define TCP_AO_KEYF_EXCLUDE_OPT (1 << 1) /* "Indicates whether TCP + * options other than TCP-AO + * are included in the MAC + * calculation" + */ =20 struct tcp_ao_add { /* setsockopt(TCP_AO_ADD_KEY) */ struct __kernel_sockaddr_storage addr; /* peer's address for the key */ diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index 3c8bdd830d96..ecd9b1e0f646 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -562,7 +562,8 @@ int tcp_ao_hash_hdr(unsigned short int family, char *ao= _hash, WARN_ON_ONCE(1); goto clear_hash; } - if (tcp_ao_hash_header(&hp, th, false, + if (tcp_ao_hash_header(&hp, th, + !!(key->keyflags & TCP_AO_KEYF_EXCLUDE_OPT), ao_hash, hash_offset, tcp_ao_maclen(key))) goto clear_hash; ahash_request_set_crypt(hp.req, NULL, hash_buf, 0); @@ -610,7 +611,8 @@ int tcp_ao_hash_skb(unsigned short int family, goto clear_hash; if (tcp_ao_hash_pseudoheader(family, sk, skb, &hp, skb->len)) goto clear_hash; - if (tcp_ao_hash_header(&hp, th, false, + if (tcp_ao_hash_header(&hp, th, + !!(key->keyflags & TCP_AO_KEYF_EXCLUDE_OPT), ao_hash, hash_offset, tcp_ao_maclen(key))) goto clear_hash; if (tcp_sigpool_hash_skb_data(&hp, skb, th->doff << 2)) @@ -1404,7 +1406,7 @@ static struct tcp_ao_info *setsockopt_ao_info(struct = sock *sk) return ERR_PTR(-ESOCKTNOSUPPORT); } =20 -#define TCP_AO_KEYF_ALL (0) +#define TCP_AO_KEYF_ALL (TCP_AO_KEYF_EXCLUDE_OPT) =20 static struct tcp_ao_key *tcp_ao_key_alloc(struct sock *sk, struct tcp_ao_add *cmd) --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 140EBC0015E for ; Tue, 15 Aug 2023 19:16:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240069AbjHOTQW (ORCPT ); Tue, 15 Aug 2023 15:16:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239917AbjHOTPj (ORCPT ); Tue, 15 Aug 2023 15:15:39 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BEE7CBF for ; Tue, 15 Aug 2023 12:15:36 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-3fe2ba3e260so56238115e9.2 for ; Tue, 15 Aug 2023 12:15:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126935; x=1692731735; 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=UL+6Rik4OCALxTdc5UB5OJxh7tiuYWtVzp8gplYUDsY=; b=QPBV2fvvCRtWMuWs+blo53XyfquaM2v5fjRxUiIbadZSR4EmLHVQm852icR3rfAbH6 itDRohAx+MsgNvt9tC1EqL1YkCkEc7iSQwS3SIT18wbwA7f1uzEL2Kp+4m/20hwPH1rl VmkpY0Y8KM8/9FepIXXDYj/KQmgVLAVrVgmb1QdPrdDwzwGbYoC8nFjOFqRhgnFudMa4 MW0Cy4N/XXv0Mo0K8x+QkcLBuvfb/0yT3JarGZCl/Sz01crfiHJti9QyiIvCO3oqZIF+ 60cj1yjIlrvOvZugO+lXbpvQyLNRg0ln4YE7V+9iye/P9nKIz2N2lrR1ZQJFg0KVnZx+ +/tA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126935; x=1692731735; 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=UL+6Rik4OCALxTdc5UB5OJxh7tiuYWtVzp8gplYUDsY=; b=Zo3ADDM7HG3iqodNPz6weboTRaOw1ZmrqMvyGW5ess9xovqj5k64v0yWEKMCgco8KK Vyxs5owGPRBGn7xybjbtF/6QTyN5UvCJhGPfy04v2Vxo3rswQqe+IfP4rCWpjy4rxuU3 Ytch20v0MEI9P8dQDaeG5JGNDkdtcUinPhcKX1vWfCaOdrPy8wZm/E/UH49/21YOwtfg OyySXjXd6jbyBlDWZvT8BWyk9stZtSwQLsbgQ2zLir35NDBujvk/0b36zEAduCyB02NR W9hOOLg2PA3F6j2ZSpl84VF5tXSY5vCJOp+GsfqVOYLj/pbEMcilYYi2SxbxYbWD7xhf ldRQ== X-Gm-Message-State: AOJu0YwAALMz/IQeRk8bOcKMkfG57Vd5aPKDTviMPla0C0UUcOHigvwP 8xwYIkrpLl1e76pHgTL0sBjZOA== X-Google-Smtp-Source: AGHT+IF17dOa+XzciBIcpKMVZQ9csUnRpUOYdQnJFXbQYBxYeWYYbyR1y9nwi5uJxbP/ZJL7LfdhrA== X-Received: by 2002:a05:6000:124c:b0:314:521:ce0a with SMTP id j12-20020a056000124c00b003140521ce0amr10605171wrx.40.1692126935219; Tue, 15 Aug 2023 12:15:35 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:34 -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 v10 net-next 18/23] net/tcp: Add TCP-AO getsockopt()s Date: Tue, 15 Aug 2023 20:14:47 +0100 Message-ID: <20230815191455.1872316-19-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Introduce getsockopt(TCP_AO_GET_KEYS) that lets a user get TCP-AO keys and their properties from a socket. The user can provide a filter to match the specific key to be dumped or ::get_all =3D 1 may be used to dump all keys in one syscall. Add another getsockopt(TCP_AO_INFO) for providing per-socket/per-ao_info stats: packet counters, Current_key/RNext_key and flags like ::ao_required and ::accept_icmps. 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 Acked-by: David Ahern --- include/net/tcp_ao.h | 12 ++ include/uapi/linux/tcp.h | 63 +++++++-- net/ipv4/tcp.c | 13 ++ net/ipv4/tcp_ao.c | 295 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 369 insertions(+), 14 deletions(-) diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index ddd2e412aae3..705e791c0a48 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -192,6 +192,8 @@ int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 = *key, void *ctx, void tcp_ao_destroy_sock(struct sock *sk, bool twsk); 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 optl= en); 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, @@ -301,6 +303,16 @@ static inline void tcp_ao_time_wait(struct tcp_timewai= t_sock *tcptw, static inline void tcp_ao_connect_init(struct sock *sk) { } + +static inline int tcp_ao_get_mkts(struct sock *sk, sockptr_t optval, sockp= tr_t optlen) +{ + return -ENOPROTOOPT; +} + +static inline int tcp_ao_get_sock_info(struct sock *sk, sockptr_t optval, = sockptr_t optlen) +{ + return -ENOPROTOOPT; +} #endif =20 #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) diff --git a/include/uapi/linux/tcp.h b/include/uapi/linux/tcp.h index 3275ade3293a..1109093bbb24 100644 --- a/include/uapi/linux/tcp.h +++ b/include/uapi/linux/tcp.h @@ -131,7 +131,8 @@ enum { =20 #define TCP_AO_ADD_KEY 38 /* Add/Set MKT */ #define TCP_AO_DEL_KEY 39 /* Delete MKT */ -#define TCP_AO_INFO 40 /* Modify TCP-AO per-socket options */ +#define TCP_AO_INFO 40 /* Set/list TCP-AO per-socket options */ +#define TCP_AO_GET_KEYS 41 /* List MKT(s) */ =20 #define TCP_REPAIR_ON 1 #define TCP_REPAIR_OFF 0 @@ -392,21 +393,55 @@ struct tcp_ao_del { /* setsockopt(TCP_AO_DEL_KEY) */ __u8 keyflags; /* see TCP_AO_KEYF_ */ } __attribute__((aligned(8))); =20 -struct tcp_ao_info_opt { /* setsockopt(TCP_AO_INFO) */ - __u32 set_current :1, /* corresponding ::current_key */ - set_rnext :1, /* corresponding ::rnext */ - ao_required :1, /* don't accept non-AO connects */ - set_counters :1, /* set/clear ::pkt_* counters */ - accept_icmps :1, /* accept incoming ICMPs */ +struct tcp_ao_info_opt { /* setsockopt(TCP_AO_INFO), getsockopt(TCP_AO_INF= O) */ + /* Here 'in' is for setsockopt(), 'out' is for getsockopt() */ + __u32 set_current :1, /* in/out: corresponding ::current_key */ + set_rnext :1, /* in/out: corresponding ::rnext */ + ao_required :1, /* in/out: don't accept non-AO connects */ + set_counters :1, /* in: set/clear ::pkt_* counters */ + accept_icmps :1, /* in/out: accept incoming ICMPs */ reserved :27; /* must be 0 */ __u16 reserved2; /* padding, must be 0 */ - __u8 current_key; /* KeyID to set as Current_key */ - __u8 rnext; /* KeyID to set as Rnext_key */ - __u64 pkt_good; /* verified segments */ - __u64 pkt_bad; /* failed verification */ - __u64 pkt_key_not_found; /* could not find a key to verify */ - __u64 pkt_ao_required; /* segments missing TCP-AO sign */ - __u64 pkt_dropped_icmp; /* ICMPs that were ignored */ + __u8 current_key; /* in/out: KeyID of Current_key */ + __u8 rnext; /* in/out: keyid of RNext_key */ + __u64 pkt_good; /* in/out: verified segments */ + __u64 pkt_bad; /* in/out: failed verification */ + __u64 pkt_key_not_found; /* in/out: could not find a key to verify */ + __u64 pkt_ao_required; /* in/out: segments missing TCP-AO sign */ + __u64 pkt_dropped_icmp; /* in/out: ICMPs that were ignored */ +} __attribute__((aligned(8))); + +struct tcp_ao_getsockopt { /* getsockopt(TCP_AO_GET_KEYS) */ + struct __kernel_sockaddr_storage addr; /* in/out: dump keys for peer + * with this address/prefix + */ + char alg_name[64]; /* out: crypto hash algorithm */ + __u8 key[TCP_AO_MAXKEYLEN]; + __u32 nkeys; /* in: size of the userspace buffer + * @optval, measured in @optlen - the + * sizeof(struct tcp_ao_getsockopt) + * out: number of keys that matched + */ + __u16 is_current :1, /* in: match and dump Current_key, + * out: the dumped key is Current_key + */ + + is_rnext :1, /* in: match and dump RNext_key, + * out: the dumped key is RNext_key + */ + get_all :1, /* in: dump all keys */ + reserved :13; /* padding, must be 0 */ + __u8 sndid; /* in/out: dump keys with SendID */ + __u8 rcvid; /* in/out: dump keys with RecvID */ + __u8 prefix; /* in/out: dump keys with address/prefix */ + __u8 maclen; /* out: key's length of authentication + * code (hash) + */ + __u8 keyflags; /* in/out: see TCP_AO_KEYF_ */ + __u8 keylen; /* out: length of ::key */ + __s32 ifindex; /* in/out: L3 dev index for VRF */ + __u64 pkt_good; /* out: verified segments */ + __u64 pkt_bad; /* out: segments that failed verification */ } __attribute__((aligned(8))); =20 /* setsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, ...) */ diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 0b742c693a1f..296e1985ee43 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -4269,6 +4269,19 @@ int do_tcp_getsockopt(struct sock *sk, int level, return err; } #endif + case TCP_AO_GET_KEYS: + case TCP_AO_INFO: { + int err; + + sockopt_lock_sock(sk); + if (optname =3D=3D TCP_AO_GET_KEYS) + err =3D tcp_ao_get_mkts(sk, optval, optlen); + else + err =3D tcp_ao_get_sock_info(sk, optval, optlen); + sockopt_release_sock(sk); + + return err; + } default: return -ENOPROTOOPT; } diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index ecd9b1e0f646..19ca3b36aa5a 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -1844,3 +1844,298 @@ int tcp_v4_parse_ao(struct sock *sk, int cmd, sockp= tr_t optval, int optlen) return tcp_parse_ao(sk, cmd, AF_INET, optval, optlen); } =20 +/* tcp_ao_copy_mkts_to_user(ao_info, optval, optlen) + * + * @ao_info: struct tcp_ao_info on the socket that + * socket getsockopt(TCP_AO_GET_KEYS) is executed on + * @optval: pointer to array of tcp_ao_getsockopt structures in user space. + * Must be !=3D NULL. + * @optlen: pointer to size of tcp_ao_getsockopt structure. + * Must be !=3D NULL. + * + * Return value: 0 on success, a negative error number otherwise. + * + * optval points to an array of tcp_ao_getsockopt structures in user space. + * optval[0] is used as both input and output to getsockopt. It determines + * which keys are returned by the kernel. + * optval[0].nkeys is the size of the array in user space. On return it co= ntains + * the number of keys matching the search criteria. + * If tcp_ao_getsockopt::get_all is set, then all keys in the socket are + * returned, otherwise only keys matching + * in optval[0] are returned. + * optlen is also used as both input and output. The user provides the size + * of struct tcp_ao_getsockopt in user space, and the kernel returns the s= ize + * of the structure in kernel space. + * The size of struct tcp_ao_getsockopt may differ between user and kernel. + * There are three cases to consider: + * * If usize =3D=3D ksize, then keys are copied verbatim. + * * If usize < ksize, then the userspace has passed an old struct to a + * newer kernel. The rest of the trailing bytes in optval[0] + * (ksize - usize) are interpreted as 0 by the kernel. + * * If usize > ksize, then the userspace has passed a new struct to an + * older kernel. The trailing bytes unknown to the kernel (usize - ksiz= e) + * are checked to ensure they are zeroed, otherwise -E2BIG is returned. + * On return the kernel fills in min(usize, ksize) in each entry of the ar= ray. + * The layout of the fields in the user and kernel structures is expected = to + * be the same (including in the 32bit vs 64bit case). + */ +static int tcp_ao_copy_mkts_to_user(struct tcp_ao_info *ao_info, + sockptr_t optval, sockptr_t optlen) +{ + struct tcp_ao_getsockopt opt_in, opt_out; + struct tcp_ao_key *key, *current_key; + bool do_address_matching =3D true; + union tcp_ao_addr *addr =3D NULL; + unsigned int max_keys; /* maximum number of keys to copy to user */ + size_t out_offset =3D 0; + size_t bytes_to_write; /* number of bytes to write to user level */ + int err, user_len; + u32 matched_keys; /* keys from ao_info matched so far */ + int optlen_out; + __be16 port =3D 0; + + if (copy_from_sockptr(&user_len, optlen, sizeof(int))) + return -EFAULT; + + if (user_len <=3D 0) + return -EINVAL; + + memset(&opt_in, 0, sizeof(struct tcp_ao_getsockopt)); + err =3D copy_struct_from_sockptr(&opt_in, sizeof(opt_in), + optval, user_len); + if (err < 0) + return err; + + if (opt_in.pkt_good || opt_in.pkt_bad) + return -EINVAL; + + if (opt_in.reserved !=3D 0) + return -EINVAL; + + max_keys =3D opt_in.nkeys; + + if (opt_in.get_all || opt_in.is_current || opt_in.is_rnext) { + if (opt_in.get_all && (opt_in.is_current || opt_in.is_rnext)) + return -EINVAL; + do_address_matching =3D false; + } + + switch (opt_in.addr.ss_family) { + case AF_INET: { + struct sockaddr_in *sin; + __be32 mask; + + sin =3D (struct sockaddr_in *)&opt_in.addr; + port =3D sin->sin_port; + addr =3D (union tcp_ao_addr *)&sin->sin_addr; + + if (opt_in.prefix > 32) + return -EINVAL; + + if (ntohl(sin->sin_addr.s_addr) =3D=3D INADDR_ANY && + opt_in.prefix !=3D 0) + return -EINVAL; + + mask =3D inet_make_mask(opt_in.prefix); + if (sin->sin_addr.s_addr & ~mask) + return -EINVAL; + + break; + } + case AF_INET6: { + struct sockaddr_in6 *sin6; + struct in6_addr *addr6; + + sin6 =3D (struct sockaddr_in6 *)&opt_in.addr; + addr =3D (union tcp_ao_addr *)&sin6->sin6_addr; + addr6 =3D &sin6->sin6_addr; + port =3D sin6->sin6_port; + + /* We don't have to change family and @addr here if + * ipv6_addr_v4mapped() like in key adding: + * tcp_ao_key_cmp() does it. Do the sanity checks though. + */ + if (opt_in.prefix !=3D 0) { + if (ipv6_addr_v4mapped(addr6)) { + __be32 mask, addr4 =3D addr6->s6_addr32[3]; + + if (opt_in.prefix > 32 || + ntohl(addr4) =3D=3D INADDR_ANY) + return -EINVAL; + mask =3D inet_make_mask(opt_in.prefix); + if (addr4 & ~mask) + return -EINVAL; + } else { + struct in6_addr pfx; + + if (ipv6_addr_any(addr6) || + opt_in.prefix > 128) + return -EINVAL; + + ipv6_addr_prefix(&pfx, addr6, opt_in.prefix); + if (ipv6_addr_cmp(&pfx, addr6)) + return -EINVAL; + } + } else if (!ipv6_addr_any(addr6)) { + return -EINVAL; + } + break; + } + case 0: + if (!do_address_matching) + break; + fallthrough; + default: + return -EAFNOSUPPORT; + } + + if (!do_address_matching) { + /* We could just ignore those, but let's do stricter checks */ + if (addr || port) + return -EINVAL; + if (opt_in.prefix || opt_in.sndid || opt_in.rcvid) + return -EINVAL; + } + + bytes_to_write =3D min_t(int, user_len, sizeof(struct tcp_ao_getsockopt)); + matched_keys =3D 0; + /* May change in RX, while we're dumping, pre-fetch it */ + current_key =3D READ_ONCE(ao_info->current_key); + + hlist_for_each_entry_rcu(key, &ao_info->head, node) { + if (opt_in.get_all) + goto match; + + if (opt_in.is_current || opt_in.is_rnext) { + if (opt_in.is_current && key =3D=3D current_key) + goto match; + if (opt_in.is_rnext && key =3D=3D ao_info->rnext_key) + goto match; + continue; + } + + if (tcp_ao_key_cmp(key, addr, opt_in.prefix, + opt_in.addr.ss_family, + opt_in.sndid, opt_in.rcvid) !=3D 0) + continue; +match: + matched_keys++; + if (matched_keys > max_keys) + continue; + + memset(&opt_out, 0, sizeof(struct tcp_ao_getsockopt)); + + if (key->family =3D=3D AF_INET) { + struct sockaddr_in *sin_out =3D (struct sockaddr_in *)&opt_out.addr; + + sin_out->sin_family =3D key->family; + sin_out->sin_port =3D 0; + memcpy(&sin_out->sin_addr, &key->addr, sizeof(struct in_addr)); + } else { + struct sockaddr_in6 *sin6_out =3D (struct sockaddr_in6 *)&opt_out.addr; + + sin6_out->sin6_family =3D key->family; + sin6_out->sin6_port =3D 0; + memcpy(&sin6_out->sin6_addr, &key->addr, sizeof(struct in6_addr)); + } + opt_out.sndid =3D key->sndid; + opt_out.rcvid =3D key->rcvid; + opt_out.prefix =3D key->prefixlen; + opt_out.keyflags =3D key->keyflags; + opt_out.is_current =3D (key =3D=3D current_key); + opt_out.is_rnext =3D (key =3D=3D ao_info->rnext_key); + opt_out.nkeys =3D 0; + opt_out.maclen =3D key->maclen; + opt_out.keylen =3D key->keylen; + opt_out.pkt_good =3D atomic64_read(&key->pkt_good); + opt_out.pkt_bad =3D atomic64_read(&key->pkt_bad); + memcpy(&opt_out.key, key->key, key->keylen); + tcp_sigpool_algo(key->tcp_sigpool_id, opt_out.alg_name, 64); + + /* Copy key to user */ + if (copy_to_sockptr_offset(optval, out_offset, + &opt_out, bytes_to_write)) + return -EFAULT; + out_offset +=3D user_len; + } + + optlen_out =3D (int)sizeof(struct tcp_ao_getsockopt); + if (copy_to_sockptr(optlen, &optlen_out, sizeof(int))) + return -EFAULT; + + out_offset =3D offsetof(struct tcp_ao_getsockopt, nkeys); + if (copy_to_sockptr_offset(optval, out_offset, + &matched_keys, sizeof(u32))) + return -EFAULT; + + return 0; +} + +int tcp_ao_get_mkts(struct sock *sk, sockptr_t optval, sockptr_t optlen) +{ + struct tcp_ao_info *ao_info; + + ao_info =3D setsockopt_ao_info(sk); + if (IS_ERR(ao_info)) + return PTR_ERR(ao_info); + if (!ao_info) + return -ENOENT; + + return tcp_ao_copy_mkts_to_user(ao_info, optval, optlen); +} + +int tcp_ao_get_sock_info(struct sock *sk, sockptr_t optval, sockptr_t optl= en) +{ + struct tcp_ao_info_opt out, in =3D {}; + struct tcp_ao_key *current_key; + struct tcp_ao_info *ao; + int err, len; + + if (copy_from_sockptr(&len, optlen, sizeof(int))) + return -EFAULT; + + if (len <=3D 0) + return -EINVAL; + + /* Copying this "in" only to check ::reserved, ::reserved2, + * that may be needed to extend (struct tcp_ao_info_opt) and + * what getsockopt() provides in future. + */ + err =3D copy_struct_from_sockptr(&in, sizeof(in), optval, len); + if (err) + return err; + + if (in.reserved !=3D 0 || in.reserved2 !=3D 0) + return -EINVAL; + + ao =3D setsockopt_ao_info(sk); + if (IS_ERR(ao)) + return PTR_ERR(ao); + if (!ao) + return -ENOENT; + + memset(&out, 0, sizeof(out)); + out.ao_required =3D ao->ao_required; + out.accept_icmps =3D ao->accept_icmps; + out.pkt_good =3D atomic64_read(&ao->counters.pkt_good); + out.pkt_bad =3D atomic64_read(&ao->counters.pkt_bad); + out.pkt_key_not_found =3D atomic64_read(&ao->counters.key_not_found); + out.pkt_ao_required =3D atomic64_read(&ao->counters.ao_required); + out.pkt_dropped_icmp =3D atomic64_read(&ao->counters.dropped_icmp); + + current_key =3D READ_ONCE(ao->current_key); + if (current_key) { + out.set_current =3D 1; + out.current_key =3D current_key->sndid; + } + if (ao->rnext_key) { + out.set_rnext =3D 1; + out.rnext =3D ao->rnext_key->rcvid; + } + + if (copy_to_sockptr(optval, &out, min_t(int, len, sizeof(out)))) + return -EFAULT; + + return 0; +} + --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3DD52C04A6A for ; Tue, 15 Aug 2023 19:16:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239905AbjHOTQZ (ORCPT ); Tue, 15 Aug 2023 15:16:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239918AbjHOTPk (ORCPT ); Tue, 15 Aug 2023 15:15:40 -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 6ADB1138 for ; Tue, 15 Aug 2023 12:15:38 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-3fe1e1142caso56465695e9.0 for ; Tue, 15 Aug 2023 12:15:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126937; x=1692731737; 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=TCicV2Aib3M68fPnrmFoMTYBXDUU3WrcaJEwS9KBGbs=; b=KSrZc8SeRtjynMirvrDP537XDedzJqAMIdl1WW0Cepu9/zr4LB/skrKzCktxWF5XaW WPhYvDhP+jk6+6BRd/iVNoJ0KvV5lcxaYonubOY31VzUCCqLRUhdMMkn8oa5Zq0R2lod Q/dwxyNGAbYPG+2niUaq5R2TC71kQAgFBhmhGPD4jUpMGM8AXO4roMO3BLve3u4vXogB l7BYnkN6dWbhHgb2h8On6WjVzbaKr+9L5qRl2whBYMtCjl5I/OqtqgaC/HKdZZGnS9KG jDiQG6Yh4EMlO5Ie0l/2b+Pxey8H5BjbIJ7VB6Db/rxy7+Ydlx0akGK0GwVxv0Oqsc32 WM1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126937; x=1692731737; 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=TCicV2Aib3M68fPnrmFoMTYBXDUU3WrcaJEwS9KBGbs=; b=GKu1JeHNImO4wyYhHDKjsgOyeMrXJBaq5wqW4hT/HQZrjLT8FOFwsNeu1UZyWtpm+e ndLmvos0X0hfnlkbPnaUA7r0ol3Ef/o8/iAN9abYeIOgu1u7m8acUMm18MUflxtBqeo2 mUgcbT8cMfAq1INcfXRTqYW9hiyE7zfy/vqanCwcKEr3TvjazCWRQzJDXoQAp9FXmu6w WCDEOakg4Kw4CuM9ZjH12mS0RTsa8pKeshYVZCuG53BM/0PqTOwytqFlZ8kFjIKHg5sX cOqSvCFSgl0JKuUWag9RF6CAIRZth5ezV6CSW4NlQ0GcRJmkQ4gduzoXpwUWf2FK1nXV 56+w== X-Gm-Message-State: AOJu0YwxzBRVAOORK8LfQbVLxg7Y5kYgwOoqCi+bWD3EuhjistR1OHb1 2ItsWk8BgWV+7mn93KDGh1j9lA== X-Google-Smtp-Source: AGHT+IF7H9W7EnUie786nxeBQ2hdYK8tOO/6cYa85d4L1yr6T/BjFLUhxfUL3oarWO8EVLGMa1/5YQ== X-Received: by 2002:a05:600c:2909:b0:3fe:2b8c:9f00 with SMTP id i9-20020a05600c290900b003fe2b8c9f00mr10327588wmd.24.1692126936972; Tue, 15 Aug 2023 12:15:36 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:36 -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 v10 net-next 19/23] net/tcp: Allow asynchronous delete for TCP-AO keys (MKTs) Date: Tue, 15 Aug 2023 20:14:48 +0100 Message-ID: <20230815191455.1872316-20-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Delete becomes very, very fast - almost free, but after setsockopt() syscall returns, the key is still alive until next RCU grace period. Which is fine for listen sockets as userspace needs to be aware of setsockopt(TCP_AO) and accept() race and resolve it with verification by getsockopt() after TCP connection was accepted. The benchmark results (on non-loaded box, worse with more RCU work pending): > ok 33 Worst case delete 16384 keys: min=3D5ms max=3D10ms mean=3D6.9= 3904ms stddev=3D0.263421 > ok 34 Add a new key 16384 keys: min=3D1ms max=3D4ms mean=3D2.17= 751ms stddev=3D0.147564 > ok 35 Remove random-search 16384 keys: min=3D5ms max=3D10ms mean=3D6.5= 0243ms stddev=3D0.254999 > ok 36 Remove async 16384 keys: min=3D0ms max=3D0ms mean=3D0.02= 96107ms stddev=3D0.0172078 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 Acked-by: David Ahern --- include/uapi/linux/tcp.h | 3 ++- net/ipv4/tcp_ao.c | 21 ++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/include/uapi/linux/tcp.h b/include/uapi/linux/tcp.h index 1109093bbb24..979ff960fddb 100644 --- a/include/uapi/linux/tcp.h +++ b/include/uapi/linux/tcp.h @@ -383,7 +383,8 @@ struct tcp_ao_del { /* setsockopt(TCP_AO_DEL_KEY) */ __s32 ifindex; /* L3 dev index for VRF */ __u32 set_current :1, /* corresponding ::current_key */ set_rnext :1, /* corresponding ::rnext */ - reserved :30; /* must be 0 */ + del_async :1, /* only valid for listen sockets */ + reserved :29; /* must be 0 */ __u16 reserved2; /* padding, must be 0 */ __u8 prefix; /* peer's address prefix */ __u8 sndid; /* SendID for outgoing segments */ diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index 19ca3b36aa5a..124e9489f202 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -1578,7 +1578,7 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned s= hort int family, } =20 static int tcp_ao_delete_key(struct sock *sk, struct tcp_ao_info *ao_info, - struct tcp_ao_key *key, + bool del_async, struct tcp_ao_key *key, struct tcp_ao_key *new_current, struct tcp_ao_key *new_rnext) { @@ -1586,11 +1586,24 @@ static int tcp_ao_delete_key(struct sock *sk, struc= t tcp_ao_info *ao_info, =20 hlist_del_rcu(&key->node); =20 + /* Support for async delete on listening sockets: as they don't + * need current_key/rnext_key maintaining, we don't need to check + * them and we can just free all resources in RCU fashion. + */ + if (del_async) { + atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc); + call_rcu(&key->rcu, tcp_ao_key_free_rcu); + return 0; + } + /* At this moment another CPU could have looked this key up * while it was unlinked from the list. Wait for RCU grace period, * after which the key is off-list and can't be looked up again; * the rx path [just before RCU came] might have used it and set it * as current_key (very unlikely). + * Free the key with next RCU grace period (in case it was + * current_key before tcp_ao_current_rnext() might have + * changed it in forced-delete). */ synchronize_rcu(); if (new_current) @@ -1661,6 +1674,8 @@ static int tcp_ao_del_cmd(struct sock *sk, unsigned s= hort int family, if (!new_rnext) return -ENOENT; } + if (cmd.del_async && sk->sk_state !=3D TCP_LISTEN) + return -EINVAL; =20 if (family =3D=3D AF_INET) { struct sockaddr_in *sin =3D (struct sockaddr_in *)&cmd.addr; @@ -1708,8 +1723,8 @@ static int tcp_ao_del_cmd(struct sock *sk, unsigned s= hort int family, if (key =3D=3D new_current || key =3D=3D new_rnext) continue; =20 - return tcp_ao_delete_key(sk, ao_info, key, - new_current, new_rnext); + return tcp_ao_delete_key(sk, ao_info, cmd.del_async, key, + new_current, new_rnext); } return -ENOENT; } --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5FECAC41513 for ; Tue, 15 Aug 2023 19:16:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239931AbjHOTQ3 (ORCPT ); Tue, 15 Aug 2023 15:16:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239919AbjHOTPl (ORCPT ); Tue, 15 Aug 2023 15:15:41 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2364ABF for ; Tue, 15 Aug 2023 12:15:40 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-3fe4cdb72b9so52266445e9.0 for ; Tue, 15 Aug 2023 12:15:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126938; x=1692731738; 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=7EyHuBFw5EK2jCcTcSbUeTOqQzO5Rjmm6B6zBd+yDm4=; b=VKUH/UbwxOhgithRt2gN9LMv74BX/XIRGKZ/xOACylB46RTasbr1mF8GgJBFnuZhu2 5mIW2PLBnZu/Zhg0Utb7cGVBdOHdvhCvIVHmskFub0c7QguYS5Z6nKsJ/DXM5zkCxPqN 5Cxgjf/HWUsOA3BdP6lYYpjsUBZFFpBZmTO7Y+/MmPu10Xi6ESQV5sKaEueg+gevHuZa eaF62c0TK2MW949l1tTzHZM4J1wwxwaHIoIwSKf+FbLDkREkv4G28p51TPqC+EWQXKh2 j115D22g5ml+OsV3Q5f6Wfb/FAJGopAr/UBRBhBk0oKq3QMz0Ul2KVzKPPY+Jl5u0Tj5 J9lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126938; x=1692731738; 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=7EyHuBFw5EK2jCcTcSbUeTOqQzO5Rjmm6B6zBd+yDm4=; b=Xne+nZy8sEqOS9yKLRaeIjpHIt9ERCYUBDt+18iTk0mjWZyYPTTyt0HmMamkp7BgOA nmJYiDsE3Xa5fM7WCKGfLv6ebcOZKM8PFYMc+60me+qEdDET9l+9BM6A1iVDPCRwe0h0 eB5B1Q9mhoUA/9cuvnx5TaHSAPbvCyud7+aKFERkTtj3oywXcN7Z19UWJpcINfHcvZuU x0GvMlCAX5XpFQ0/kYwYgrKm09eu0F5NZBa3exkt4cCS3z4WU+o2SOK8EFNrf0LRhaY8 p4uyhP9mdsomN/E53IbBQcIdyUeEeN18WsQ2Dg8y63sW7zgzQMSonxkm2mEFtiy/hddy deZQ== X-Gm-Message-State: AOJu0YylZVrIbSUgMyJPz83bBuqIrNXHKdKv1DBcP3+uO1legpVfaULu gxaRpht4YaPJKK4eyjxMHXQJRA== X-Google-Smtp-Source: AGHT+IHGTrm+x1Vu5layC3iYh/XjPQMDHzSnKaBOiU5HOqrYbplpdlDZ4HU2LZK30Hw/fDEj3edw+w== X-Received: by 2002:a7b:cd0f:0:b0:3fa:77ed:9894 with SMTP id f15-20020a7bcd0f000000b003fa77ed9894mr10379835wmj.7.1692126938707; Tue, 15 Aug 2023 12:15:38 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:38 -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 v10 net-next 20/23] net/tcp: Add static_key for TCP-AO Date: Tue, 15 Aug 2023 20:14:49 +0100 Message-ID: <20230815191455.1872316-21-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 Acked-by: David Ahern --- 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 85e0f0b50261..b90ef7090dd6 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -2612,6 +2612,9 @@ static inline bool tcp_ao_required(struct sock *sk, c= onst void *saddr, struct tcp_ao_info *ao_info; struct tcp_ao_key *ao_key; =20 + if (!static_branch_unlikely(&tcp_ao_needed.key)) + return false; + ao_info =3D 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 705e791c0a48..a56b1a68a883 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -151,6 +151,8 @@ do { \ =20 #ifdef CONFIG_TCP_AO /* TCP-AO structures and functions */ +#include +extern struct static_key_false_deferred tcp_ao_needed; =20 struct tcp4_ao_context { __be32 saddr; diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index 124e9489f202..88634fbb54f2 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -17,6 +17,8 @@ #include #include =20 +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(const struct sock *sk, int type, = int code) bool ignore_icmp =3D false; struct tcp_ao_info *ao; =20 + 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 @@ -185,6 +190,9 @@ static struct tcp_ao_key *__tcp_ao_do_lookup(const stru= ct sock *sk, struct tcp_ao_key *key; struct tcp_ao_info *ao; =20 + if (!static_branch_unlikely(&tcp_ao_needed.key)) + return NULL; + ao =3D rcu_dereference_check(tcp_sk(sk)->ao_info, lockdep_sock_is_held(sk)); if (!ao) @@ -276,6 +284,7 @@ void tcp_ao_destroy_sock(struct sock *sk, bool twsk) } =20 kfree_rcu(ao, rcu); + static_branch_slow_dec_deferred(&tcp_ao_needed); } =20 void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp) @@ -1130,6 +1139,11 @@ int tcp_ao_copy_all_matching(const struct sock *sk, = struct sock *newsk, goto free_and_exit; } =20 + if (!static_key_fast_inc_not_disabled(&tcp_ao_needed.key.key)) { + ret =3D -EUSERS; + goto free_and_exit; + } + key_head =3D rcu_dereference(hlist_first_rcu(&new_ao->head)); first_key =3D hlist_entry_safe(key_head, struct tcp_ao_key, node); =20 @@ -1557,6 +1571,10 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned = short int family, =20 tcp_ao_link_mkt(ao_info, key); if (first) { + if (!static_branch_inc(&tcp_ao_needed.key)) { + ret =3D -EUSERS; + goto err_free_sock; + } sk_gso_disable(sk); rcu_assign_pointer(tcp_sk(sk)->ao_info, ao_info); } @@ -1825,6 +1843,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 =3D -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 78e7b04fac4f..c554c25eba52 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 stru= ct tcp_sock *tp, (ack_seq =3D=3D tp->snd_wl1 && (nwin > tp->snd_wnd || !nwin)); } =20 -/* 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 =3D ack - tp->snd_una; #ifdef CONFIG_TCP_AO struct tcp_ao_info *ao; -#endif =20 - sock_owned_by_me((struct sock *)tp); - tp->bytes_acked +=3D delta; -#ifdef CONFIG_TCP_AO + if (!static_branch_unlikely(&tcp_ao_needed.key)) + return; + ao =3D 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 =3D ack - tp->snd_una; + + sock_owned_by_me((struct sock *)tp); + tp->bytes_acked +=3D delta; + tcp_snd_sne_update(tp, ack); tp->snd_una =3D ack; } =20 +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 =3D 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 =3D seq - tp->rcv_nxt; -#ifdef CONFIG_TCP_AO - struct tcp_ao_info *ao; -#endif =20 sock_owned_by_me((struct sock *)tp); tp->bytes_received +=3D delta; -#ifdef CONFIG_TCP_AO - ao =3D 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); } =20 --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6FC43C001E0 for ; Tue, 15 Aug 2023 19:16:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239953AbjHOTQc (ORCPT ); Tue, 15 Aug 2023 15:16:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239921AbjHOTPp (ORCPT ); Tue, 15 Aug 2023 15:15:45 -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 19A2EB8 for ; Tue, 15 Aug 2023 12:15:42 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-3fe12820bffso52241895e9.3 for ; Tue, 15 Aug 2023 12:15:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126940; x=1692731740; 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=m5kRuPeV/5U08ukNL+QLpvtX3t/cSPer1k6HNJ2akzE=; b=P1BbVRuAzli+QudpnL6YANdCOXR/D1x9FF5zOVdpSGl7z52wl1qFk0EHxiYMLf53V9 cNhNQGDEHN/mvpxPp8eGs/hscpv0uCDLGPKYAE5C8/m1AGXOervv9L4Lua4Wa73AtNaY RPM0cHodZYw/k/2oM5FjV5tsjj5Z6v+KN4+yynBmu6GLsAE9wplZVMgPx1geXEJFfIFi UtjJgKWEP77vp3De2ouizZP9KwV+RMDAbwdx09kyUWZccYltldjyb4S4skR7/UT5YE96 GRzXtER4JdQOAmsKwVNbH2wOOPVmts7eTfLn0Y9N/Kl6LKajnsC7F9jyY6fE77/+y8lz 6d3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126940; x=1692731740; 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=m5kRuPeV/5U08ukNL+QLpvtX3t/cSPer1k6HNJ2akzE=; b=Tyz6B6WrgQU/28KIrAxKjLD7W88uLNwBrmi5xZXIWWvvrhOKSJYQJBqgN5OCMRvVA5 BlUFKadXD6kydOqW26jUlTJIDNTO8tNGyOvr1d+/iEUX+spQhiZ1Y+a2y7zxd9a3JX6S r+rbZCAEbCXBddtrk3sClc2DU3Th0hZIVI8dq6nsAQZuGpy2RQH3JhLRF5+q7ePIz3ok x8xkgymthcFToeF1iHWTH/OqmQQMnUIHsq8+zmff5GGTXgSQZRJwyDKcdbcacjmF8pdR cmcgxGi+3y/UJs3uUZywFT9xeEw1VJTMWcNqRh2kdpYeB5V8EHbirQ+z3iCTXT+EfBJ+ 4TfQ== X-Gm-Message-State: AOJu0YzowwV6y3FTwlduMHAe1FR0QksGeQ8FpJWeTwK2iRs2AnsSqoDJ Uy4I9qiGmO1f+jyl78/wFHMWHA== X-Google-Smtp-Source: AGHT+IGhyJsRdEiSmG/SK34zAn1mpSIKHe96CbrOXkjY6TtNgWewGLm0CyoaSRY/iCaZMau4Nds7RQ== X-Received: by 2002:a05:600c:450:b0:3fe:1dad:5403 with SMTP id s16-20020a05600c045000b003fe1dad5403mr10173381wmb.23.1692126940507; Tue, 15 Aug 2023 12:15:40 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:40 -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 v10 net-next 21/23] net/tcp: Wire up l3index to TCP-AO Date: Tue, 15 Aug 2023 20:14:50 +0100 Message-ID: <20230815191455.1872316-22-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Similarly how TCP_MD5SIG_FLAG_IFINDEX works for TCP-MD5, TCP_AO_KEYF_IFINDEX is an AO-key flag that binds that MKT to a specified by L3 ifinndex. Similarly, without this flag the key will work in the default VRF l3index =3D 0 for connections. To prevent AO-keys from overlapping, it's restricted to add key B for a socket that has key A, which have the same sndid/rcvid and one of the following is true: - !(A.keyflags & TCP_AO_KEYF_IFINDEX) or !(B.keyflags & TCP_AO_KEYF_IFINDEX) so that any key is non-bound to a VRF - A.l3index =3D=3D B.l3index both want to work for the same VRF Additionally, it's restricted to match TCP-MD5 keys for the same peer the following way: |--------------|--------------------|----------------|---------------| | | MD5 key without | MD5 key | MD5 key | | | l3index | l3index=3D0 | l3index=3DN | |--------------|--------------------|----------------|---------------| | TCP-AO key | | | | | without | reject | reject | reject | | l3index | | | | |--------------|--------------------|----------------|---------------| | TCP-AO key | | | | | l3index=3D0 | reject | reject | allow | |--------------|--------------------|----------------|---------------| | TCP-AO key | | | | | l3index=3DN | reject | allow | reject | |--------------|--------------------|----------------|---------------| This is done with the help of tcp_md5_do_lookup_any_l3index() to reject adding AO key without TCP_AO_KEYF_IFINDEX if there's TCP-MD5 in any VRF. This is important for case where sysctl_tcp_l3mdev_accept =3D 1 Similarly, for TCP-AO lookups tcp_ao_do_lookup() may be used with l3index < 0, so that __tcp_ao_key_cmp() will match TCP-AO key in any VRF. Signed-off-by: Dmitry Safonov Acked-by: David Ahern --- include/net/tcp.h | 11 +-- include/net/tcp_ao.h | 18 ++--- net/ipv4/syncookies.c | 6 +- net/ipv4/tcp_ao.c | 170 +++++++++++++++++++++++++++++++----------- net/ipv4/tcp_ipv4.c | 14 ++-- net/ipv6/syncookies.c | 5 +- net/ipv6/tcp_ao.c | 21 +++--- net/ipv6/tcp_ipv6.c | 15 +++- 8 files changed, 178 insertions(+), 82 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index b90ef7090dd6..deee8a4bb618 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -2606,7 +2606,7 @@ static inline int tcp_parse_auth_options(const struct= tcphdr *th, } =20 static inline bool tcp_ao_required(struct sock *sk, const void *saddr, - int family, bool stat_inc) + int family, int l3index, bool stat_inc) { #ifdef CONFIG_TCP_AO struct tcp_ao_info *ao_info; @@ -2620,7 +2620,7 @@ static inline bool tcp_ao_required(struct sock *sk, c= onst void *saddr, if (!ao_info) return false; =20 - ao_key =3D tcp_ao_do_lookup(sk, saddr, family, -1, -1); + ao_key =3D tcp_ao_do_lookup(sk, l3index, saddr, family, -1, -1); if (ao_info->ao_required || ao_key) { if (stat_inc) { NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOREQUIRED); @@ -2673,21 +2673,22 @@ tcp_inbound_hash(struct sock *sk, const struct requ= est_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, l3index, 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, ""); + tcp_hash_fail("MD5 Hash not found", + family, skb, "L3 index %d", l3index); return SKB_DROP_REASON_TCP_MD5NOTFOUND; } return SKB_NOT_DROPPED_YET; } =20 if (aoh) - return tcp_inbound_ao_hash(sk, skb, family, req, aoh); + return tcp_inbound_ao_hash(sk, skb, family, req, l3index, aoh); =20 return tcp_inbound_md5_hash(sk, skb, saddr, daddr, family, l3index, md5_location); diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index a56b1a68a883..7bffdc81e2a9 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -33,6 +33,7 @@ struct tcp_ao_key { u8 key[TCP_AO_MAXKEYLEN] __tcp_ao_key_align; unsigned int tcp_sigpool_id; unsigned int digest_size; + int l3index; u8 prefixlen; u8 family; u8 keylen; @@ -198,10 +199,10 @@ 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 optl= en); 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, + const struct request_sock *req, int l3index, const struct tcp_ao_hdr *aoh); u32 tcp_ao_compute_sne(u32 next_sne, u32 next_seq, u32 seq); -struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, +struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, int l3index, const union tcp_ao_addr *addr, int family, int sndid, int rcvid); int tcp_ao_hash_hdr(unsigned short family, char *ao_hash, @@ -243,9 +244,6 @@ int tcp_v6_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *k= ey, __be32 disn, bool send); int tcp_v6_ao_calc_key_rsk(struct tcp_ao_key *mkt, u8 *key, struct request_sock *req); -struct tcp_ao_key *tcp_v6_ao_do_lookup(const struct sock *sk, - const struct in6_addr *addr, - int sndid, int rcvid); struct tcp_ao_key *tcp_v6_ao_lookup(const struct sock *sk, struct sock *addr_sk, int sndid, int rcvid); struct tcp_ao_key *tcp_v6_ao_lookup_rsk(const struct sock *sk, @@ -262,12 +260,12 @@ void tcp_ao_finish_connect(struct sock *sk, struct sk= _buff *skb); void tcp_ao_connect_init(struct sock *sk); void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb, struct tcp_request_sock *treq, - unsigned short int family); + unsigned short int family, int l3index); #else /* CONFIG_TCP_AO */ =20 static inline void tcp_ao_syncookie(struct sock *sk, const struct sk_buff = *skb, struct tcp_request_sock *treq, - unsigned short int family) + unsigned short int family, int l3index) { } =20 @@ -278,13 +276,15 @@ static inline bool tcp_ao_ignore_icmp(const struct so= ck *sk, int type, int code) =20 static inline 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, const struct tcp_ao_hdr *aoh) + const struct request_sock *req, int l3index, + const struct tcp_ao_hdr *aoh) { return SKB_NOT_DROPPED_YET; } =20 static inline struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, - const union tcp_ao_addr *addr, int family, int sndid, int rcvid) + int l3index, const union tcp_ao_addr *addr, + int family, int sndid, int rcvid) { return NULL; } diff --git a/net/ipv4/syncookies.c b/net/ipv4/syncookies.c index 23fca22bc992..40b7f4c659f8 100644 --- a/net/ipv4/syncookies.c +++ b/net/ipv4/syncookies.c @@ -338,6 +338,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk= _buff *skb) __u8 rcv_wscale; struct flowi4 fl4; u32 tsoff =3D 0; + int l3index; =20 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies) || !th->ack || th->rst) @@ -394,13 +395,14 @@ struct sock *cookie_v4_check(struct sock *sk, struct = sk_buff *skb) treq->snt_synack =3D 0; treq->tfo_listener =3D false; =20 - tcp_ao_syncookie(sk, skb, treq, AF_INET); - if (IS_ENABLED(CONFIG_SMC)) ireq->smc_ok =3D 0; =20 ireq->ir_iif =3D inet_request_bound_dev_if(sk, skb); =20 + l3index =3D l3mdev_master_ifindex_by_index(sock_net(sk), ireq->ir_iif); + tcp_ao_syncookie(sk, skb, treq, AF_INET, l3index); + /* We throwed the options of the initial SYN away, so we hope * the ACK carries the same options again (see RFC1122 4.2.3.8) */ diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index 88634fbb54f2..65915313711c 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -136,7 +136,7 @@ static int ipv4_prefix_cmp(const struct in_addr *addr1, return memcmp(&a1, &a2, sizeof(a1)); } =20 -static int __tcp_ao_key_cmp(const struct tcp_ao_key *key, +static int __tcp_ao_key_cmp(const struct tcp_ao_key *key, int l3index, const union tcp_ao_addr *addr, u8 prefixlen, int family, int sndid, int rcvid) { @@ -144,6 +144,10 @@ static int __tcp_ao_key_cmp(const struct tcp_ao_key *k= ey, return (key->sndid > sndid) ? 1 : -1; if (rcvid >=3D 0 && key->rcvid !=3D rcvid) return (key->rcvid > rcvid) ? 1 : -1; + if (l3index >=3D 0 && (key->keyflags & TCP_AO_KEYF_IFINDEX)) { + if (key->l3index !=3D l3index) + return (key->l3index > l3index) ? 1 : -1; + } =20 if (family =3D=3D AF_UNSPEC) return 0; @@ -168,7 +172,7 @@ static int __tcp_ao_key_cmp(const struct tcp_ao_key *ke= y, return -1; } =20 -static int tcp_ao_key_cmp(const struct tcp_ao_key *key, +static int tcp_ao_key_cmp(const struct tcp_ao_key *key, int l3index, const union tcp_ao_addr *addr, u8 prefixlen, int family, int sndid, int rcvid) { @@ -176,14 +180,16 @@ static int tcp_ao_key_cmp(const struct tcp_ao_key *ke= y, if (family =3D=3D AF_INET6 && ipv6_addr_v4mapped(&addr->a6)) { __be32 addr4 =3D addr->a6.s6_addr32[3]; =20 - return __tcp_ao_key_cmp(key, (union tcp_ao_addr *)&addr4, + return __tcp_ao_key_cmp(key, l3index, + (union tcp_ao_addr *)&addr4, prefixlen, AF_INET, sndid, rcvid); } #endif - return __tcp_ao_key_cmp(key, addr, prefixlen, family, sndid, rcvid); + return __tcp_ao_key_cmp(key, l3index, addr, + prefixlen, family, sndid, rcvid); } =20 -static struct tcp_ao_key *__tcp_ao_do_lookup(const struct sock *sk, +static struct tcp_ao_key *__tcp_ao_do_lookup(const struct sock *sk, int l3= index, const union tcp_ao_addr *addr, int family, u8 prefix, int sndid, int rcvid) { @@ -201,17 +207,18 @@ static struct tcp_ao_key *__tcp_ao_do_lookup(const st= ruct sock *sk, hlist_for_each_entry_rcu(key, &ao->head, node) { u8 prefixlen =3D min(prefix, key->prefixlen); =20 - if (!tcp_ao_key_cmp(key, addr, prefixlen, family, sndid, rcvid)) + if (!tcp_ao_key_cmp(key, l3index, addr, prefixlen, + family, sndid, rcvid)) return key; } return NULL; } =20 -struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, +struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, int l3index, const union tcp_ao_addr *addr, int family, int sndid, int rcvid) { - return __tcp_ao_do_lookup(sk, addr, family, U8_MAX, sndid, rcvid); + return __tcp_ao_do_lookup(sk, l3index, addr, family, U8_MAX, sndid, rcvid= ); } =20 static struct tcp_ao_info *tcp_ao_alloc_info(gfp_t flags) @@ -677,18 +684,22 @@ struct tcp_ao_key *tcp_v4_ao_lookup_rsk(const struct = sock *sk, struct request_sock *req, int sndid, int rcvid) { - union tcp_ao_addr *addr =3D - (union tcp_ao_addr *)&inet_rsk(req)->ir_rmt_addr; + struct inet_request_sock *ireq =3D inet_rsk(req); + union tcp_ao_addr *addr =3D (union tcp_ao_addr *)&ireq->ir_rmt_addr; + int l3index; =20 - return tcp_ao_do_lookup(sk, addr, AF_INET, sndid, rcvid); + l3index =3D l3mdev_master_ifindex_by_index(sock_net(sk), ireq->ir_iif); + return tcp_ao_do_lookup(sk, l3index, addr, AF_INET, sndid, rcvid); } =20 struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *ad= dr_sk, int sndid, int rcvid) { + int l3index =3D l3mdev_master_ifindex_by_index(sock_net(sk), + addr_sk->sk_bound_dev_if); union tcp_ao_addr *addr =3D (union tcp_ao_addr *)&addr_sk->sk_daddr; =20 - return tcp_ao_do_lookup(sk, addr, AF_INET, sndid, rcvid); + return tcp_ao_do_lookup(sk, l3index, addr, AF_INET, sndid, rcvid); } =20 int tcp_ao_prepare_reset(const struct sock *sk, struct sk_buff *skb, @@ -740,7 +751,8 @@ int tcp_ao_prepare_reset(const struct sock *sk, struct = sk_buff *skb, ao_info =3D rcu_dereference(tcp_sk(sk)->ao_info); if (!ao_info) return -ENOENT; - *key =3D tcp_ao_do_lookup(sk, addr, family, -1, aoh->rnext_keyid); + *key =3D tcp_ao_do_lookup(sk, l3index, addr, family, + -1, aoh->rnext_keyid); if (!*key) return -ENOENT; *traffic_key =3D kmalloc(tcp_ao_digest_size(*key), GFP_ATOMIC); @@ -779,24 +791,26 @@ int tcp_ao_prepare_reset(const struct sock *sk, struc= t sk_buff *skb, =20 static struct tcp_ao_key *tcp_ao_inbound_lookup(unsigned short int family, const struct sock *sk, const struct sk_buff *skb, - int sndid, int rcvid) + int sndid, int rcvid, int l3index) { if (family =3D=3D AF_INET) { const struct iphdr *iph =3D ip_hdr(skb); =20 - return tcp_ao_do_lookup(sk, (union tcp_ao_addr *)&iph->saddr, - AF_INET, sndid, rcvid); + return tcp_ao_do_lookup(sk, l3index, + (union tcp_ao_addr *)&iph->saddr, + AF_INET, sndid, rcvid); } else { const struct ipv6hdr *iph =3D ipv6_hdr(skb); =20 - return tcp_ao_do_lookup(sk, (union tcp_ao_addr *)&iph->saddr, - AF_INET6, sndid, rcvid); + return tcp_ao_do_lookup(sk, l3index, + (union tcp_ao_addr *)&iph->saddr, + AF_INET6, sndid, rcvid); } } =20 void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb, struct tcp_request_sock *treq, - unsigned short int family) + unsigned short int family, int l3index) { const struct tcphdr *th =3D tcp_hdr(skb); const struct tcp_ao_hdr *aoh; @@ -807,7 +821,7 @@ void tcp_ao_syncookie(struct sock *sk, const struct sk_= buff *skb, if (tcp_parse_auth_options(th, NULL, &aoh) || !aoh) return; =20 - key =3D tcp_ao_inbound_lookup(family, sk, skb, -1, aoh->keyid); + key =3D tcp_ao_inbound_lookup(family, sk, skb, -1, aoh->keyid, l3index); if (!key) /* Key not found, continue without TCP-AO */ return; @@ -821,7 +835,7 @@ static enum skb_drop_reason tcp_ao_verify_hash(const struct sock *sk, const struct sk_buff *skb, unsigned short int family, struct tcp_ao_info *info, const struct tcp_ao_hdr *aoh, struct tcp_ao_key *key, - u8 *traffic_key, u8 *phash, u32 sne) + u8 *traffic_key, u8 *phash, u32 sne, int l3index) { u8 maclen =3D aoh->length - sizeof(struct tcp_ao_hdr); const struct tcphdr *th =3D tcp_hdr(skb); @@ -832,7 +846,8 @@ tcp_ao_verify_hash(const struct sock *sk, const struct = sk_buff *skb, atomic64_inc(&info->counters.pkt_bad); atomic64_inc(&key->pkt_bad); tcp_hash_fail("AO hash wrong length", family, skb, - "%u !=3D %d", maclen, tcp_ao_maclen(key)); + "%u !=3D %d L3index: %d", maclen, + tcp_ao_maclen(key), l3index); return SKB_DROP_REASON_TCP_AOFAILURE; } =20 @@ -847,7 +862,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 mismatch", family, skb, ""); + tcp_hash_fail("AO hash mismatch", family, skb, + "L3index: %d", l3index); kfree(hash_buf); return SKB_DROP_REASON_TCP_AOFAILURE; } @@ -861,7 +877,7 @@ tcp_ao_verify_hash(const struct sock *sk, const struct = sk_buff *skb, 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, - const struct tcp_ao_hdr *aoh) + int l3index, const struct tcp_ao_hdr *aoh) { const struct tcphdr *th =3D tcp_hdr(skb); u8 *phash =3D (u8 *)(aoh + 1); /* hash goes just after the header */ @@ -876,7 +892,7 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_bu= ff *skb, if (!info) { NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOKEYNOTFOUND); tcp_hash_fail("AO key not found", family, skb, - "keyid: %u", aoh->keyid); + "keyid: %u L3index: %d", aoh->keyid, l3index); return SKB_DROP_REASON_TCP_AOUNEXPECTED; } =20 @@ -911,7 +927,7 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_bu= ff *skb, /* Established socket, traffic key are cached */ traffic_key =3D rcv_other_key(key); err =3D tcp_ao_verify_hash(sk, skb, family, info, aoh, key, - traffic_key, phash, sne); + traffic_key, phash, sne, l3index); if (err) return err; current_key =3D READ_ONCE(info->current_key); @@ -932,7 +948,7 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_bu= ff *skb, * - request sockets would race on those key pointers * - tcp_ao_del_cmd() allows async key removal */ - key =3D tcp_ao_inbound_lookup(family, sk, skb, -1, aoh->keyid); + key =3D tcp_ao_inbound_lookup(family, sk, skb, -1, aoh->keyid, l3index); if (!key) goto key_not_found; =20 @@ -972,7 +988,7 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_bu= ff *skb, return SKB_DROP_REASON_NOT_SPECIFIED; tcp_ao_calc_key_skb(key, traffic_key, skb, sisn, disn, family); ret =3D tcp_ao_verify_hash(sk, skb, family, info, aoh, key, - traffic_key, phash, sne); + traffic_key, phash, sne, l3index); kfree(traffic_key); return ret; =20 @@ -980,7 +996,7 @@ tcp_inbound_ao_hash(struct sock *sk, const struct sk_bu= ff *skb, 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, ""); + family, skb, "L3index: %d", l3index); return SKB_DROP_REASON_TCP_AOKEYNOTFOUND; } =20 @@ -1008,7 +1024,7 @@ void tcp_ao_connect_init(struct sock *sk) struct tcp_ao_info *ao_info; union tcp_ao_addr *addr; struct tcp_ao_key *key; - int family; + int family, l3index; =20 ao_info =3D rcu_dereference_protected(tp->ao_info, lockdep_sock_is_held(sk)); @@ -1025,9 +1041,11 @@ void tcp_ao_connect_init(struct sock *sk) #endif else return; + l3index =3D l3mdev_master_ifindex_by_index(sock_net(sk), + sk->sk_bound_dev_if); =20 hlist_for_each_entry_rcu(key, &ao_info->head, node) { - if (!tcp_ao_key_cmp(key, addr, key->prefixlen, family, -1, -1)) + if (!tcp_ao_key_cmp(key, l3index, addr, key->prefixlen, family, -1, -1)) continue; =20 if (key =3D=3D ao_info->current_key) @@ -1084,9 +1102,9 @@ int tcp_ao_copy_all_matching(const struct sock *sk, s= truct sock *newsk, struct tcp_ao_key *key, *new_key, *first_key; struct tcp_ao_info *new_ao, *ao; struct hlist_node *key_head; + int l3index, ret =3D -ENOMEM; union tcp_ao_addr *addr; bool match =3D false; - int ret =3D -ENOMEM; =20 ao =3D rcu_dereference(tcp_sk(sk)->ao_info); if (!ao) @@ -1114,9 +1132,11 @@ int tcp_ao_copy_all_matching(const struct sock *sk, = struct sock *newsk, ret =3D -EAFNOSUPPORT; goto free_ao; } + l3index =3D l3mdev_master_ifindex_by_index(sock_net(newsk), + newsk->sk_bound_dev_if); =20 hlist_for_each_entry_rcu(key, &ao->head, node) { - if (tcp_ao_key_cmp(key, addr, key->prefixlen, family, -1, -1)) + if (tcp_ao_key_cmp(key, l3index, addr, key->prefixlen, family, -1, -1)) continue; =20 new_key =3D tcp_ao_copy_key(newsk, key); @@ -1420,7 +1440,8 @@ static struct tcp_ao_info *setsockopt_ao_info(struct = sock *sk) return ERR_PTR(-ESOCKTNOSUPPORT); } =20 -#define TCP_AO_KEYF_ALL (TCP_AO_KEYF_EXCLUDE_OPT) +#define TCP_AO_KEYF_ALL (TCP_AO_KEYF_IFINDEX | TCP_AO_KEYF_EXCLUDE_OPT) +#define TCP_AO_GET_KEYF_VALID (TCP_AO_KEYF_IFINDEX) =20 static struct tcp_ao_key *tcp_ao_key_alloc(struct sock *sk, struct tcp_ao_add *cmd) @@ -1484,8 +1505,8 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned s= hort int family, union tcp_ao_addr *addr; struct tcp_ao_key *key; struct tcp_ao_add cmd; + int ret, l3index =3D 0; bool first =3D false; - int ret; =20 if (optlen < sizeof(cmd)) return -EINVAL; @@ -1515,9 +1536,46 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned = short int family, return -EINVAL; } =20 + if (cmd.ifindex && !(cmd.keyflags & TCP_AO_KEYF_IFINDEX)) + return -EINVAL; + + /* For cmd.tcp_ifindex =3D 0 the key will apply to the default VRF */ + if (cmd.keyflags & TCP_AO_KEYF_IFINDEX && cmd.ifindex) { + int bound_dev_if =3D READ_ONCE(sk->sk_bound_dev_if); + struct net_device *dev; + + rcu_read_lock(); + dev =3D dev_get_by_index_rcu(sock_net(sk), cmd.ifindex); + if (dev && netif_is_l3_master(dev)) + l3index =3D dev->ifindex; + rcu_read_unlock(); + + if (!dev || !l3index) + return -EINVAL; + + /* It's still possible to bind after adding keys or even + * re-bind to a different dev (with CAP_NET_RAW). + * So, no reason to return error here, rather try to be + * nice and warn the user. + */ + if (bound_dev_if && bound_dev_if !=3D cmd.ifindex) + net_warn_ratelimited("AO key ifindex %d !=3D sk bound ifindex %d\n", + cmd.ifindex, bound_dev_if); + } + /* Don't allow keys for peers that have a matching TCP-MD5 key */ - if (tcp_md5_do_lookup_any_l3index(sk, addr, family)) - return -EKEYREJECTED; + if (cmd.keyflags & TCP_AO_KEYF_IFINDEX) { + /* Non-_exact version of tcp_md5_do_lookup() will + * as well match keys that aren't bound to a specific VRF + * (that will make them match AO key with + * sysctl_tcp_l3dev_accept =3D 1 + */ + if (tcp_md5_do_lookup(sk, l3index, addr, family)) + return -EKEYREJECTED; + } else { + if (tcp_md5_do_lookup_any_l3index(sk, addr, family)) + return -EKEYREJECTED; + } =20 ao_info =3D setsockopt_ao_info(sk); if (IS_ERR(ao_info)) @@ -1534,10 +1592,9 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned = short int family, * > The IDs of MKTs MUST NOT overlap where their * > TCP connection identifiers overlap. */ - if (__tcp_ao_do_lookup(sk, addr, family, - cmd.prefix, -1, cmd.rcvid)) + if (__tcp_ao_do_lookup(sk, l3index, addr, family, cmd.prefix, -1, cmd.rc= vid)) return -EEXIST; - if (__tcp_ao_do_lookup(sk, addr, family, + if (__tcp_ao_do_lookup(sk, l3index, addr, family, cmd.prefix, cmd.sndid, -1)) return -EEXIST; } @@ -1556,6 +1613,7 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned s= hort int family, key->keyflags =3D cmd.keyflags; key->sndid =3D cmd.sndid; key->rcvid =3D cmd.rcvid; + key->l3index =3D l3index; atomic64_set(&key->pkt_good, 0); atomic64_set(&key->pkt_bad, 0); =20 @@ -1644,17 +1702,17 @@ static int tcp_ao_delete_key(struct sock *sk, struc= t tcp_ao_info *ao_info, return err; } =20 +#define TCP_AO_DEL_KEYF_ALL (TCP_AO_KEYF_IFINDEX) static int tcp_ao_del_cmd(struct sock *sk, unsigned short int family, sockptr_t optval, int optlen) { struct tcp_ao_key *key, *new_current =3D NULL, *new_rnext =3D NULL; + int err, addr_len, l3index =3D 0; struct tcp_ao_info *ao_info; union tcp_ao_addr *addr; struct tcp_ao_del cmd; - int addr_len; __u8 prefix; u16 port; - int err; =20 if (optlen < sizeof(cmd)) return -EINVAL; @@ -1671,6 +1729,17 @@ static int tcp_ao_del_cmd(struct sock *sk, unsigned = short int family, return -EINVAL; } =20 + if (cmd.keyflags & ~TCP_AO_DEL_KEYF_ALL) + return -EINVAL; + + /* No sanity check for TCP_AO_KEYF_IFINDEX as if a VRF + * was destroyed, there still should be a way to delete keys, + * that were bound to that l3intf. So, fail late at lookup stage + * if there is no key for that ifindex. + */ + if (cmd.ifindex && !(cmd.keyflags & TCP_AO_KEYF_IFINDEX)) + return -EINVAL; + ao_info =3D setsockopt_ao_info(sk); if (IS_ERR(ao_info)) return PTR_ERR(ao_info); @@ -1738,6 +1807,13 @@ static int tcp_ao_del_cmd(struct sock *sk, unsigned = short int family, memcmp(addr, &key->addr, addr_len)) continue; =20 + if ((cmd.keyflags & TCP_AO_KEYF_IFINDEX) !=3D + (key->keyflags & TCP_AO_KEYF_IFINDEX)) + continue; + + if (key->l3index !=3D l3index) + continue; + if (key =3D=3D new_current || key =3D=3D new_rnext) continue; =20 @@ -1923,10 +1999,10 @@ static int tcp_ao_copy_mkts_to_user(struct tcp_ao_i= nfo *ao_info, struct tcp_ao_key *key, *current_key; bool do_address_matching =3D true; union tcp_ao_addr *addr =3D NULL; + int err, l3index, user_len; unsigned int max_keys; /* maximum number of keys to copy to user */ size_t out_offset =3D 0; size_t bytes_to_write; /* number of bytes to write to user level */ - int err, user_len; u32 matched_keys; /* keys from ao_info matched so far */ int optlen_out; __be16 port =3D 0; @@ -1945,11 +2021,16 @@ static int tcp_ao_copy_mkts_to_user(struct tcp_ao_i= nfo *ao_info, =20 if (opt_in.pkt_good || opt_in.pkt_bad) return -EINVAL; + if (opt_in.keyflags & ~TCP_AO_GET_KEYF_VALID) + return -EINVAL; + if (opt_in.ifindex && !(opt_in.keyflags & TCP_AO_KEYF_IFINDEX)) + return -EINVAL; =20 if (opt_in.reserved !=3D 0) return -EINVAL; =20 max_keys =3D opt_in.nkeys; + l3index =3D (opt_in.keyflags & TCP_AO_KEYF_IFINDEX) ? opt_in.ifindex : -1; =20 if (opt_in.get_all || opt_in.is_current || opt_in.is_rnext) { if (opt_in.get_all && (opt_in.is_current || opt_in.is_rnext)) @@ -2051,7 +2132,7 @@ static int tcp_ao_copy_mkts_to_user(struct tcp_ao_inf= o *ao_info, continue; } =20 - if (tcp_ao_key_cmp(key, addr, opt_in.prefix, + if (tcp_ao_key_cmp(key, l3index, addr, opt_in.prefix, opt_in.addr.ss_family, opt_in.sndid, opt_in.rcvid) !=3D 0) continue; @@ -2084,6 +2165,7 @@ static int tcp_ao_copy_mkts_to_user(struct tcp_ao_inf= o *ao_info, opt_out.nkeys =3D 0; opt_out.maclen =3D key->maclen; opt_out.keylen =3D key->keylen; + opt_out.ifindex =3D key->l3index; opt_out.pkt_good =3D atomic64_read(&key->pkt_good); opt_out.pkt_bad =3D atomic64_read(&key->pkt_bad); memcpy(&opt_out.key, key->key, key->keylen); diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index c941c13b924a..26c84586b60c 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -1079,6 +1079,7 @@ static void tcp_v4_reqsk_send_ack(const struct sock *= sk, struct sk_buff *skb, const union tcp_md5_addr *addr; u8 *traffic_key =3D NULL; u8 keyid =3D 0; + int l3index; #ifdef CONFIG_TCP_AO const struct tcp_ao_hdr *aoh; #endif @@ -1089,6 +1090,7 @@ static void tcp_v4_reqsk_send_ack(const struct sock *= sk, struct sk_buff *skb, u32 seq =3D (sk->sk_state =3D=3D TCP_LISTEN) ? tcp_rsk(req)->snt_isn + 1 : tcp_sk(sk)->snd_nxt; addr =3D (union tcp_md5_addr *)&ip_hdr(skb)->saddr; + l3index =3D tcp_v4_sdif(skb) ? inet_iif(skb) : 0; =20 if (tcp_rsk_used_ao(req)) { #ifdef CONFIG_TCP_AO @@ -1099,10 +1101,11 @@ static void tcp_v4_reqsk_send_ack(const struct sock= *sk, struct sk_buff *skb, if (!aoh) return; =20 - ao_key =3D tcp_ao_do_lookup(sk, addr, AF_INET, aoh->rnext_keyid, -1); + ao_key =3D tcp_ao_do_lookup(sk, l3index, addr, AF_INET, + aoh->rnext_keyid, -1); if (unlikely(!ao_key)) { /* Send ACK with any matching MKT for the peer */ - ao_key =3D tcp_ao_do_lookup(sk, addr, AF_INET, -1, -1); + ao_key =3D tcp_ao_do_lookup(sk, l3index, addr, AF_INET, -1, -1); /* Matching key disappeared (user removed the key?) * let the handshake timeout. */ @@ -1123,9 +1126,6 @@ static void tcp_v4_reqsk_send_ack(const struct sock *= sk, struct sk_buff *skb, tcp_v4_ao_calc_key_rsk(ao_key, traffic_key, req); #endif } else { - int l3index; - - l3index =3D tcp_v4_sdif(skb) ? inet_iif(skb) : 0; md5_key =3D tcp_md5_do_lookup(sk, l3index, addr, AF_INET); } /* RFC 7323 2.3 @@ -1485,6 +1485,7 @@ static int tcp_v4_parse_md5_keys(struct sock *sk, int= optname, const union tcp_md5_addr *addr; u8 prefixlen =3D 32; int l3index =3D 0; + bool l3flag; u8 flags; =20 if (optlen < sizeof(cmd)) @@ -1497,6 +1498,7 @@ static int tcp_v4_parse_md5_keys(struct sock *sk, int= optname, return -EINVAL; =20 flags =3D cmd.tcpm_flags & TCP_MD5SIG_FLAG_IFINDEX; + l3flag =3D cmd.tcpm_flags & TCP_MD5SIG_FLAG_IFINDEX; =20 if (optname =3D=3D TCP_MD5SIG_EXT && cmd.tcpm_flags & TCP_MD5SIG_FLAG_PREFIX) { @@ -1534,7 +1536,7 @@ static int tcp_v4_parse_md5_keys(struct sock *sk, int= optname, /* Don't allow keys for peers that have a matching TCP-AO key. * See the comment in tcp_ao_add_cmd() */ - if (tcp_ao_required(sk, addr, AF_INET, false)) + if (tcp_ao_required(sk, addr, AF_INET, l3flag ? l3index : -1, false)) return -EKEYREJECTED; =20 return tcp_md5_do_add(sk, addr, AF_INET, prefixlen, l3index, flags, diff --git a/net/ipv6/syncookies.c b/net/ipv6/syncookies.c index ad7a8caa7b2a..500f6ed3b8cf 100644 --- a/net/ipv6/syncookies.c +++ b/net/ipv6/syncookies.c @@ -140,6 +140,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk= _buff *skb) struct dst_entry *dst; __u8 rcv_wscale; u32 tsoff =3D 0; + int l3index; =20 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies) || !th->ack || th->rst) @@ -214,7 +215,9 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk= _buff *skb) treq->snt_isn =3D cookie; treq->ts_off =3D 0; treq->txhash =3D net_tx_rndhash(); - tcp_ao_syncookie(sk, skb, treq, AF_INET6); + + l3index =3D l3mdev_master_ifindex_by_index(sock_net(sk), ireq->ir_iif); + tcp_ao_syncookie(sk, skb, treq, AF_INET6, l3index); =20 if (IS_ENABLED(CONFIG_SMC)) ireq->smc_ok =3D 0; diff --git a/net/ipv6/tcp_ao.c b/net/ipv6/tcp_ao.c index 8b04611c9078..3c09ac26206e 100644 --- a/net/ipv6/tcp_ao.c +++ b/net/ipv6/tcp_ao.c @@ -87,30 +87,29 @@ int tcp_v6_ao_calc_key_rsk(struct tcp_ao_key *mkt, u8 *= key, htonl(tcp_rsk(req)->rcv_isn)); } =20 -struct tcp_ao_key *tcp_v6_ao_do_lookup(const struct sock *sk, - const struct in6_addr *addr, - int sndid, int rcvid) -{ - return tcp_ao_do_lookup(sk, (union tcp_ao_addr *)addr, AF_INET6, - sndid, rcvid); -} - struct tcp_ao_key *tcp_v6_ao_lookup(const struct sock *sk, struct sock *addr_sk, int sndid, int rcvid) { + int l3index =3D l3mdev_master_ifindex_by_index(sock_net(sk), + addr_sk->sk_bound_dev_if); struct in6_addr *addr =3D &addr_sk->sk_v6_daddr; =20 - return tcp_v6_ao_do_lookup(sk, addr, sndid, rcvid); + return tcp_ao_do_lookup(sk, l3index, (union tcp_ao_addr *)addr, + AF_INET6, sndid, rcvid); } =20 struct tcp_ao_key *tcp_v6_ao_lookup_rsk(const struct sock *sk, struct request_sock *req, int sndid, int rcvid) { - struct in6_addr *addr =3D &inet_rsk(req)->ir_v6_rmt_addr; + struct inet_request_sock *ireq =3D inet_rsk(req); + struct in6_addr *addr =3D &ireq->ir_v6_rmt_addr; + int l3index; =20 - return tcp_v6_ao_do_lookup(sk, addr, sndid, rcvid); + l3index =3D l3mdev_master_ifindex_by_index(sock_net(sk), ireq->ir_iif); + return tcp_ao_do_lookup(sk, l3index, (union tcp_ao_addr *)addr, + AF_INET6, sndid, rcvid); } =20 int tcp_v6_ao_hash_pseudoheader(struct tcp_sigpool *hp, diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 402ec6ecb0e6..1592cee8a60c 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -609,6 +609,7 @@ static int tcp_v6_parse_md5_keys(struct sock *sk, int o= ptname, union tcp_ao_addr *addr; int l3index =3D 0; u8 prefixlen; + bool l3flag; u8 flags; =20 if (optlen < sizeof(cmd)) @@ -621,6 +622,7 @@ static int tcp_v6_parse_md5_keys(struct sock *sk, int o= ptname, return -EINVAL; =20 flags =3D cmd.tcpm_flags & TCP_MD5SIG_FLAG_IFINDEX; + l3flag =3D cmd.tcpm_flags & TCP_MD5SIG_FLAG_IFINDEX; =20 if (optname =3D=3D TCP_MD5SIG_EXT && cmd.tcpm_flags & TCP_MD5SIG_FLAG_PREFIX) { @@ -667,7 +669,8 @@ static int tcp_v6_parse_md5_keys(struct sock *sk, int o= ptname, /* Don't allow keys for peers that have a matching TCP-AO key. * See the comment in tcp_ao_add_cmd() */ - if (tcp_ao_required(sk, addr, AF_INET, false)) + if (tcp_ao_required(sk, addr, AF_INET, + l3flag ? l3index : -1, false)) return -EKEYREJECTED; return tcp_md5_do_add(sk, addr, AF_INET, prefixlen, l3index, flags, @@ -679,7 +682,7 @@ static int tcp_v6_parse_md5_keys(struct sock *sk, int o= ptname, /* Don't allow keys for peers that have a matching TCP-AO key. * See the comment in tcp_ao_add_cmd() */ - if (tcp_ao_required(sk, addr, AF_INET6, false)) + if (tcp_ao_required(sk, addr, AF_INET6, l3flag ? l3index : -1, false)) return -EKEYREJECTED; =20 return tcp_md5_do_add(sk, addr, AF_INET6, prefixlen, l3index, flags, @@ -1226,10 +1229,14 @@ static void tcp_v6_reqsk_send_ack(const struct sock= *sk, struct sk_buff *skb, return; if (!aoh) return; - ao_key =3D tcp_v6_ao_do_lookup(sk, addr, aoh->rnext_keyid, -1); + ao_key =3D tcp_ao_do_lookup(sk, l3index, + (union tcp_ao_addr *)addr, AF_INET6, + aoh->rnext_keyid, -1); if (unlikely(!ao_key)) { /* Send ACK with any matching MKT for the peer */ - ao_key =3D tcp_v6_ao_do_lookup(sk, addr, -1, -1); + ao_key =3D tcp_ao_do_lookup(sk, l3index, + (union tcp_ao_addr *)addr, + AF_INET6, -1, -1); /* Matching key disappeared (user removed the key?) * let the handshake timeout. */ --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8590AC04E69 for ; Tue, 15 Aug 2023 19:16:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239962AbjHOTQg (ORCPT ); Tue, 15 Aug 2023 15:16:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239920AbjHOTPp (ORCPT ); Tue, 15 Aug 2023 15:15:45 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F108710E for ; Tue, 15 Aug 2023 12:15:43 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-31969580797so3053540f8f.3 for ; Tue, 15 Aug 2023 12:15:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126942; x=1692731742; 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=YUo7LjJkt/gLUmEsfc9WbDcPQ795ordeimMxgj8+/2g=; b=VGjFvMagcUYy9AEqiPKjV8lGxUaLayASGaNGQwSI4NHErE2SCoNh92jVxW+onMhFFQ X52PshJdA2TQY2vSmnkdCUH6CuwFKe4SktT7td1bmbbAH1lnS4dppp0Tpg98MepPzmeF szrC6cP9PTU7Fyq/WBOJudQxhiEgqv6Ak2uaSMdDiYdutq1w61J2Fv+GHk0okTK1S6ix B7ZZV0FWdqntMpQD4Vl3JRaCRUbeK/RiVfsbwPXb1aBHmVbSVQKZKgC6l0Q6JFfxy73Y icA7+UzlNqKlmk5+HbcPQMAoCZkY1Iesh/l59i2ceX5/f9J8YKV5p6Kyg6bPbKLZX5xj A2ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126942; x=1692731742; 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=YUo7LjJkt/gLUmEsfc9WbDcPQ795ordeimMxgj8+/2g=; b=FcPyPaKvW5gTuzZ+4k8CRNu3GEOVCucjOuMqujHsVj3idZERh2whgrstyrlHoDd7CE qzadgKffSjgltUZFuqDoZVvjjCvYOF8BzCGOd9ZjTXq0OMspgIPpW8STFMpMPRDIJx3r zy1irkvSKDSHFGVdyDH2FllF/l522RzV2wyF9pfj7ZLwOLGQ12WjFNBKGoAHhCrRZgER XFJ934nAustODjekzDMDtByufxTjK2poNzCbw6HpNhNGBy5+IY1JUZ5n6Jwxomf2tPNa Im8jxtVrgJLF18eidC16KTHW7Me1WZqqoxpjjAZ2RtWMZ6+/vXlKFSOR/G7eftuw8QsD Wjzg== X-Gm-Message-State: AOJu0Yw9qE0IDeSflKfcVa3CObzuxeg0qd5kvbWmTX1OGHLPfDy6riTq wMR8aNQxzocFjBFDM60xlcazjw== X-Google-Smtp-Source: AGHT+IE68NpvgjIK0xkzouqUi25v+AvtuLrDYbvi9FVAQyX0K+UqUS48W2i4/AxRrWPOnfMXutPLxQ== X-Received: by 2002:a5d:538e:0:b0:314:2732:e81e with SMTP id d14-20020a5d538e000000b003142732e81emr9062594wrv.8.1692126942470; Tue, 15 Aug 2023 12:15:42 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:41 -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 v10 net-next 22/23] net/tcp: Add TCP_AO_REPAIR Date: Tue, 15 Aug 2023 20:14:51 +0100 Message-ID: <20230815191455.1872316-23-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 7bffdc81e2a9..4336e295ac6f 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 optl= en); +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 optl= en); 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 =20 #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 */ =20 #define TCP_REPAIR_ON 1 #define TCP_REPAIR_OFF 0 @@ -445,6 +446,13 @@ struct tcp_ao_getsockopt { /* getsockopt(TCP_AO_GET_KE= YS) */ __u64 pkt_bad; /* out: segments that failed verification */ } __attribute__((aligned(8))); =20 +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, ...) */ =20 #define TCP_RECEIVE_ZEROCOPY_FLAG_TLB_CLEAN_HINT 0x1 diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 296e1985ee43..cdb48f21e782 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -3598,20 +3598,28 @@ int do_tcp_setsockopt(struct sock *sk, int level, i= nt optname, __tcp_sock_set_quickack(sk, val); break; =20 + case TCP_AO_REPAIR: + err =3D 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 =3D tp->af_specific->ao_parse(sk, optname, optval, - optlen); - else - err =3D -EISCONN; + goto ao_parse; + if (tp->repair) + goto ao_parse; + err =3D -EISCONN; + break; +ao_parse: + err =3D tp->af_specific->ao_parse(sk, optname, optval, optlen); break; } #endif @@ -4269,6 +4277,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 65915313711c..2e9f6be90b5a 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -1440,6 +1440,16 @@ static struct tcp_ao_info *setsockopt_ao_info(struct= sock *sk) return ERR_PTR(-ESOCKTNOSUPPORT); } =20 +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 =3D=3D 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) =20 @@ -1621,11 +1631,13 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned= short int family, if (ret < 0) goto err_free_sock; =20 - /* Change this condition if we allow adding keys in states - * like close_wait, syn_sent or fin_wait... - */ - if (sk->sk_state =3D=3D 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 =3D key; + ao_info->rnext_key =3D key; + } + } =20 tcp_ao_link_mkt(ao_info, key); if (first) { @@ -1876,6 +1888,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 =3D tcp_ao_alloc_info(GFP_KERNEL); if (!ao_info) return -ENOMEM; @@ -2258,3 +2272,71 @@ int tcp_ao_get_sock_info(struct sock *sk, sockptr_t = optval, sockptr_t optlen) return 0; } =20 +int tcp_ao_set_repair(struct sock *sk, sockptr_t optval, unsigned int optl= en) +{ + struct tcp_sock *tp =3D 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 =3D copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen); + if (err) + return err; + + if (!tp->repair) + return -EPERM; + + ao =3D 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 =3D 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 <=3D 0) + return -EINVAL; + + if (!tp->repair) + return -EPERM; + + rcu_read_lock(); + ao =3D getsockopt_ao_info(sk); + if (IS_ERR_OR_NULL(ao)) { + rcu_read_unlock(); + return ao ? PTR_ERR(ao) : -ENOENT; + } + + opt.snt_isn =3D ao->lisn; + opt.rcv_isn =3D ao->risn; + opt.snd_sne =3D READ_ONCE(ao->snd_sne); + opt.rcv_sne =3D READ_ONCE(ao->rcv_sne); + rcu_read_unlock(); + + if (copy_to_sockptr(optval, &opt, min_t(int, len, sizeof(opt)))) + return -EFAULT; + return 0; +} --=20 2.41.0 From nobody Wed Dec 17 23:27:12 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 95B37C04A94 for ; Tue, 15 Aug 2023 19:16:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239999AbjHOTQk (ORCPT ); Tue, 15 Aug 2023 15:16:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239922AbjHOTPs (ORCPT ); Tue, 15 Aug 2023 15:15:48 -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 2D48CB8 for ; Tue, 15 Aug 2023 12:15:46 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-3fe8a1591c7so42650585e9.3 for ; Tue, 15 Aug 2023 12:15:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1692126944; x=1692731744; 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=dR1trzy4z5v1EXSbhB/8mnNcfUpUpYzGOOWwrhywfv8=; b=BKBHqZbjDniTZo+IGFLboNZxbOxudEX+Z7r++SNHXxy7wm83hcSgE5yE7KopS0Z/d6 nJ21q0Btq/D2j0tRryh4os7H4kMxnZfz1OEkZL0Lo5H6S/+exWNtReIeUEpcPgbxSBTv 3yM+a/A4csnKRiW0eVIYuphYcLERPjmAbHmeAl1k8n5yEvo0ZoSzieM72IkOm3KLfxrp FFsT6NRV50vzlKYa9Z2bHSIAwIa/WtZTgm1T6mb9jJ9OYQSvZi3eARD/fSAy9m6slGrm mIGLUt+JXrqI/h6WRbKKs0vb02fof9CrGAB3OnIYMMX7kzXEIChN2pvCmBtdZnV3z449 5xOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692126944; x=1692731744; 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=dR1trzy4z5v1EXSbhB/8mnNcfUpUpYzGOOWwrhywfv8=; b=KuRjAxhvV5JppibQPURrWkdO1u1eQE2e9xhhq35fc/06PUrjF9T9/Hij/jaW7nMY2M mvhwwTZO1F0b+c2etjTL9sL88v+nmWu2LTlreq05mP2sLdH3nXQecd4odhF37tMAV31h /dTgyFCkKd7WGQGhdxy1hccdXgR4eGIlBMWH9M4Hj+I7oSsDSVDwBV3gPVuBtl9pfz9x neHG1t89FzmyXsnbMYSHQ4AcRNyu2Jmp/7n/ZS1SmG+i1e3sn7gG5BCT4snYStA0pVgt NnZzAuYrrNRGN3UFCFk+IJaBB2qwF+Ij2MD3mVvogNbyDTVFaEvKo8zYfN6Y9FPPMu6e E8bQ== X-Gm-Message-State: AOJu0Yzdq8UUCMiJF/Y8qGvw/W8LwPeVXMEaq5mECLy1AmcFj9Sr79fK U9YyQzGZ6GeBNYoNqwaSJnqarw== X-Google-Smtp-Source: AGHT+IER3bX9Pf108s+ThXA/xa0/PLAgJmwBAjLu6M1ZqUDWkwwzcYvBXCwbcBLAZQuDG/yX49AJag== X-Received: by 2002:a05:600c:220c:b0:3fb:fef3:53f8 with SMTP id z12-20020a05600c220c00b003fbfef353f8mr9830854wml.25.1692126944535; Tue, 15 Aug 2023 12:15:44 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id q9-20020a1ce909000000b003fbbe41fd78sm18779737wmc.10.2023.08.15.12.15.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Aug 2023 12:15:43 -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, Jonathan Corbet , linux-doc@vger.kernel.org Subject: [PATCH v10 net-next 23/23] Documentation/tcp: Add TCP-AO documentation Date: Tue, 15 Aug 2023 20:14:52 +0100 Message-ID: <20230815191455.1872316-24-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230815191455.1872316-1-dima@arista.com> References: <20230815191455.1872316-1-dima@arista.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It has Frequently Asked Questions (FAQ) on RFC 5925 - I found it very useful answering those before writing the actual code. It provides answers to common questions that arise on a quick read of the RFC, as well as how they were answered. There's also comparison to TCP-MD5 option, evaluation of per-socket vs in-kernel-DB approaches and description of uAPI provided. Hopefully, it will be as useful for reviewing the code as it was for writin= g. Cc: Jonathan Corbet Cc: linux-doc@vger.kernel.org Signed-off-by: Dmitry Safonov Acked-by: David Ahern --- Documentation/networking/index.rst | 1 + Documentation/networking/tcp_ao.rst | 434 ++++++++++++++++++++++++++++ 2 files changed, 435 insertions(+) create mode 100644 Documentation/networking/tcp_ao.rst diff --git a/Documentation/networking/index.rst b/Documentation/networking/= index.rst index 5b75c3f7a137..69c1e53ef88b 100644 --- a/Documentation/networking/index.rst +++ b/Documentation/networking/index.rst @@ -107,6 +107,7 @@ Contents: sysfs-tagging tc-actions-env-rules tc-queue-filters + tcp_ao tcp-thin team timestamping diff --git a/Documentation/networking/tcp_ao.rst b/Documentation/networking= /tcp_ao.rst new file mode 100644 index 000000000000..cfa13a0748a2 --- /dev/null +++ b/Documentation/networking/tcp_ao.rst @@ -0,0 +1,434 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D +TCP Authentication Option Linux implementation (RFC5925) +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D + +TCP Authentication Option (TCP-AO) provides a TCP extension aimed at verif= ying +segments between trusted peers. It adds a new TCP header option with +a Message Authentication Code (MAC). MACs are produced from the content +of a TCP segment using a hashing function with a password known to both pe= ers. +The intent of TCP-AO is to deprecate TCP-MD5 providing better security, +key rotation and support for variety of hashing algorithms. + +1. Introduction +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +.. list-table:: Short and Limited Comparison of TCP-AO and TCP-MD5 + + * - + - TCP-MD5 + - TCP-AO + * - Supported hashing algorithms + - MD5 (cryptographically weak). + - Must support HMAC-SHA1 (chosen-prefix attacks) and CMAC-AES-128 + (only side-channel attacks). May support any hashing algorithm. + * - Length of MACs (bytes) + - 16 + - Typically 12-16. Possible other variants that fit TCP header. + * - Number of keys per TCP connection + - 1 + - Many + * - Possibility to change an active key + - Non-practical (both peers have to change them during MSL) + - Supported by protocol + * - Protection against ICMP 'hard errors' + - No + - Yes: ignoring them by default on established connections + * - Protection against traffic-crossing attack + - No + - Yes: pseudo-header includes TCP ports. + * - Protection against replayed TCP segments + - No + - Sequence Number Extension (SNE) and Initial Sequence Numbers (ISN) + * - Supports Connectionless Resets + - Yes + - No. ISNs+SNEs are needed to correctly sign RST. + * - Standards + - RFC 2385 + - RFC 5925, RFC 5926 + +1.1 Frequently Asked Questions (FAQ) with references to RFC 5925 +---------------------------------------------------------------- + +Q: Can either SendID or RecvID be non-unique for the same 4-tuple +(srcaddr, srcport, dstaddr, dstport)? + +A: No [3.1]:: + + >> The IDs of MKTs MUST NOT overlap where their TCP connection + identifiers overlap. + +Q: Can Master Key Tuple (MKT) for an active connection be removed? + +A: No, unless it's copied to Transport Control Block (TCB) [3.1]:: + + It is presumed that an MKT affecting a particular connection cannot + be destroyed during an active connection -- or, equivalently, that + its parameters are copied to an area local to the connection (i.e., + instantiated) and so changes would affect only new connections. + +Q: If an old MKT needs to be deleted, how should it be done in order +to not remove it for an active connection? (As it can be still in use +at any moment later) + +A: Not specified by RFC 5925, seems to be a problem for key management +to ensure that no one uses such MKT before trying to remove it. + +Q: Can an old MKT exist forever and be used by another peer? + +A: It can, it's a key management task to decide when to remove an old key = [6.1]:: + + Deciding when to start using a key is a performance issue. Deciding + when to remove an MKT is a security issue. Invalid MKTs are expected + to be removed. TCP-AO provides no mechanism to coordinate their removal, + as we consider this a key management operation. + +also [6.1]:: + + The only way to avoid reuse of previously used MKTs is to remove the MKT + when it is no longer considered permitted. + +Linux TCP-AO will try its best to prevent you from removing a key that's +being used, considering it a key management failure. But sine keeping +an outdated key may become a security issue and as a peer may +unintentionally prevent the removal of an old key by always setting +it as RNextKeyID - a forced key removal mechanism is provided, where +userspace has to supply KeyID to use instead of the one that's being remov= ed +and the kernel will atomically delete the old key, even if the peer is +still requesting it. There are no guarantees for force-delete as the peer +may yet not have the new key - the TCP connection may just break. +Alternatively, one may choose to shut down the socket. + +Q: What happens when a packet is received on a new connection with no known +MKT's RecvID? + +A: RFC 5925 specifies that by default it is accepted with a warning logged= , but +the behaviour can be configured by the user [7.5.1.a]:: + + If the segment is a SYN, then this is the first segment of a new + connection. Find the matching MKT for this segment, using the segment's + socket pair and its TCP-AO KeyID, matched against the MKT's TCP connect= ion + identifier and the MKT's RecvID. + + i. If there is no matching MKT, remove TCP-AO from the segment. + Proceed with further TCP handling of the segment. + NOTE: this presumes that connections that do not match any MKT + should be silently accepted, as noted in Section 7.3. + +[7.3]:: + + >> A TCP-AO implementation MUST allow for configuration of the behavior + of segments with TCP-AO but that do not match an MKT. The initial defau= lt + of this configuration SHOULD be to silently accept such connections. + If this is not the desired case, an MKT can be included to match such + connections, or the connection can indicate that TCP-AO is required. + Alternately, the configuration can be changed to discard segments with + the AO option not matching an MKT. + +[10.2.b]:: + + Connections not matching any MKT do not require TCP-AO. Further, incomi= ng + segments with TCP-AO are not discarded solely because they include + the option, provided they do not match any MKT. + +Note that Linux TCP-AO implementation differs in this aspect. Currently, T= CP-AO +segments with unknown key signatures are discarded with warnings logged. + +Q: Does the RFC imply centralized kernel key management in any way? +(i.e. that a key on all connections MUST be rotated at the same time?) + +A: Not specified. MKTs can be managed in userspace, the only relevant part= to +key changes is [7.3]:: + + >> All TCP segments MUST be checked against the set of MKTs for matching + TCP connection identifiers. + +Q: What happens when RNextKeyID requested by a peer is unknown? Should +the connection be reset? + +A: It should not, no action needs to be performed [7.5.2.e]:: + + ii. If they differ, determine whether the RNextKeyID MKT is ready. + + 1. If the MKT corresponding to the segment=E2=80=99s socket pair an= d RNextKeyID + is not available, no action is required (RNextKeyID of a received + segment needs to match the MKT=E2=80=99s SendID). + +Q: How current_key is set and when does it change? It is a user-triggered +change, or is it by a request from the remote peer? Is it set by the user +explicitly, or by a matching rule? + +A: current_key is set by RNextKeyID [6.1]:: + + Rnext_key is changed only by manual user intervention or MKT management + protocol operation. It is not manipulated by TCP-AO. Current_key is upd= ated + by TCP-AO when processing received TCP segments as discussed in the seg= ment + processing description in Section 7.5. Note that the algorithm allows + the current_key to change to a new MKT, then change back to a previously + used MKT (known as "backing up"). This can occur during an MKT change w= hen + segments are received out of order, and is considered a feature of TCP-= AO, + because reordering does not result in drops. + +[7.5.2.e.ii]:: + + 2. If the matching MKT corresponding to the segment=E2=80=99s socket pa= ir and + RNextKeyID is available: + + a. Set current_key to the RNextKeyID MKT. + +Q: If both peers have multiple MKTs matching the connection's socket pair +(with different KeyIDs), how should the sender/receiver pick KeyID to use? + +A: Some mechanism should pick the "desired" MKT [3.3]:: + + Multiple MKTs may match a single outgoing segment, e.g., when MKTs + are being changed. Those MKTs cannot have conflicting IDs (as noted + elsewhere), and some mechanism must determine which MKT to use for each + given outgoing segment. + + >> An outgoing TCP segment MUST match at most one desired MKT, indicated + by the segment=E2=80=99s socket pair. The segment MAY match multiple MK= Ts, provided + that exactly one MKT is indicated as desired. Other information in + the segment MAY be used to determine the desired MKT when multiple MKTs + match; such information MUST NOT include values in any TCP option field= s. + +Q: Can TCP-MD5 connection migrate to TCP-AO (and vice-versa): + +A: No [1]:: + + TCP MD5-protected connections cannot be migrated to TCP-AO because TCP = MD5 + does not support any changes to a connection=E2=80=99s security algorit= hm + once established. + +Q: If all MKTs are removed on a connection, can it become a non-TCP-AO sig= ned +connection? + +A: [7.5.2] doesn't have the same choice as SYN packet handling in [7.5.1.i] +that would allow accepting segments without a sign (which would be insecur= e). +While switching to non-TCP-AO connection is not prohibited directly, it se= ems +what the RFC means. Also, there's a requirement for TCP-AO connections to +always have one current_key [3.3]:: + + TCP-AO requires that every protected TCP segment match exactly one MKT. + +[3.3]:: + + >> An incoming TCP segment including TCP-AO MUST match exactly one MKT, + indicated solely by the segment=E2=80=99s socket pair and its TCP-AO Ke= yID. + +[4.4]:: + + One or more MKTs. These are the MKTs that match this connection=E2=80= =99s + socket pair. + +Q: Can a non-TCP-AO connection become a TCP-AO-enabled one? + +A: No: for already established non-TCP-AO connection it would be impossible +to switch using TCP-AO as the traffic key generation requires the initial +sequence numbers. Paraphrasing, starting using TCP-AO would require +re-establishing the TCP connection. + +2. In-kernel MKTs database vs database in userspace +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D + +Linux TCP-AO support is implemented using ``setsockopt()s``, in a similar = way +to TCP-MD5. It means that a userspace application that wants to use TCP-AO +should perform ``setsockopt()`` on a TCP socket when it wants to add, +remove or rotate MKTs. This approach moves the key management responsibili= ty +to userspace as well as decisions on corner cases, i.e. what to do if +the peer doesn't respect RNextKeyID; moving more code to userspace, especi= ally +responsible for the policy decisions. Besides, it's flexible and scales we= ll +(with less locking needed than in the case of an in-kernel database). One = also +should keep in mind that mainly intended users are BGP processes, not any +random applications, which means that compared to IPsec tunnels, +no transparency is really needed and modern BGP daemons already have +``setsockopt()s`` for TCP-MD5 support. + +.. list-table:: Considered pros and cons of the approaches + + * - + - ``setsockopt()`` + - in-kernel DB + * - Extendability + - ``setsockopt()`` commands should be extendable syscalls + - netlink messages are simple and extendable + * - Required userspace changes + - BGP or any application that wants TCP-AO needs to perform + ``setsockopt()s`` and do key management + - could be transparent as tunnels, providing something like + ``ip tcpao [add|delete|show|rotate] key`` + * - MKTs removal or adding + - harder for userspace + - harder for kernel + * - Dump-ability + - ``getsockopt()`` + - Netlink .dump() callback + * - Limits on kernel resources/memory + - equal + - equal + * - Scalability + - contention on ``TCP_LISTEN`` sockets + - contention on the whole database + * - Monitoring & warnings + - ``TCP_DIAG`` + - Same Netlink socket + * - Matching of MKTs + - Half-problem: only listen sockets + - Hard + +3. uAPI +=3D=3D=3D=3D=3D=3D=3D + +Linux provides a set of ``setsockopt()s`` and ``getsockopt()s`` that let +userspace manage TCP-AO on a per-socket basis. In order to add/delete MKTs +``TCP_AO_ADD_KEY`` and ``TCP_AO_DEL_KEY`` TCP socket options must be used +It is not allowed to add a key on an established non-TCP-AO connection +as well as to remove the last key from TCP-AO connection. + +``setsockopt(TCP_AO_DEL_KEY)`` command may specify ``tcp_ao_del::current_k= ey`` ++ ``tcp_ao_del::set_current`` and/or ``tcp_ao_del::rnext`` ++ ``tcp_ao_del::set_rnext`` which makes such delete "forced": it +provides userspace a way to delete a key that's being used and atomically = set +another one instead. This is not intended for normal use and should be used +only when the peer ignores RNextKeyID and keeps requesting/using an old ke= y. +It provides a way to force-delete a key that's not trusted but may break +the TCP-AO connection. + +The usual/normal key-rotation can be performed with ``setsockopt(TCP_AO_IN= FO)``. +It also provides a uAPI to change per-socket TCP-AO settings, such as +ignoring ICMPs, as well as clear per-socket TCP-AO packet counters. +The corresponding ``getsockopt(TCP_AO_INFO)`` can be used to get those +per-socket TCP-AO settings. + +Another useful command is ``getsockopt(TCP_AO_GET_KEYS)``. One can use it +to list all MKTs on a TCP socket or use a filter to get keys for a specific +peer and/or sndid/rcvid, VRF L3 interface or get current_key/rnext_key. + +To repair TCP-AO connections ``setsockopt(TCP_AO_REPAIR)`` is available, +provided that the user previously has checkpointed/dumped the socket with +``getsockopt(TCP_AO_REPAIR)``. + +A tip here for scaled TCP_LISTEN sockets, that may have some thousands TCP= -AO +keys, is: use filters in ``getsockopt(TCP_AO_GET_KEYS)`` and asynchronous +delete with ``setsockopt(TCP_AO_DEL_KEY)``. + +Linux TCP-AO also provides a bunch of segment counters that can be helpful +with troubleshooting/debugging issues. Every MKT has good/bad counters +that reflect how many packets passed/failed verification. +Each TCP-AO socket has the following counters: +- for good segments (properly signed) +- for bad segments (failed TCP-AO verification) +- for segments with unknown keys +- for segments where an AO signature was expected, but wasn't found +- for the number of ignored ICMPs + +TCP-AO per-socket counters are also duplicated with per-netns counters, +exposed with SNMP. Those are ``TCPAOGood``, ``TCPAOBad``, ``TCPAOKeyNotFou= nd``, +``TCPAORequired`` and ``TCPAODroppedIcmps``. + +RFC 5925 very permissively specifies how TCP port matching can be done for +MKTs:: + + TCP connection identifier. A TCP socket pair, i.e., a local IP + address, a remote IP address, a TCP local port, and a TCP remote port. + Values can be partially specified using ranges (e.g., 2-30), masks + (e.g., 0xF0), wildcards (e.g., "*"), or any other suitable indication. + +Currently Linux TCP-AO implementation doesn't provide any TCP port matchin= g. +Probably, port ranges are the most flexible for uAPI, but so far +not implemented. + +4. ``setsockopt()`` vs ``accept()`` race +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +In contrast with TCP-MD5 established connection which has just one key, +TCP-AO connections may have many keys, which means that accepted connectio= ns +on a listen socket may have any amount of keys as well. As copying all tho= se +keys on a first properly signed SYN would make the request socket bigger, = that +would be undesirable. Currently, the implementation doesn't copy keys +to request sockets, but rather look them up on the "parent" listener socke= t. + +The result is that when userspace removes TCP-AO keys, that may break +not-yet-established connections on request sockets as well as not removing +keys from sockets that were already established, but not yet ``accept()``'= ed, +hanging in the accept queue. + +The reverse is valid as well: if userspace adds a new key for a peer on +a listener socket, the established sockets in accept queue won't +have the new keys. + +At this moment, the resolution for the two races: +``setsockopt(TCP_AO_ADD_KEY)`` vs ``accept()`` +and ``setsockopt(TCP_AO_DEL_KEY)`` vs ``accept()`` is delegated to userspa= ce. +This means that it's expected that userspace would check the MKTs on the s= ocket +that was returned by ``accept()`` to verify that any key rotation that +happened on listen socket is reflected on the newly established connection. + +This is a similar "do-nothing" approach to TCP-MD5 from the kernel side and +may be changed later by introducing new flags to ``tcp_ao_add`` +and ``tcp_ao_del``. + +Note that this race is rare for it needs TCP-AO key rotation to happen +during the 3-way handshake for the new TCP connection. + +5. Interaction with TCP-MD5 +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D + +A TCP connection can not migrate between TCP-AO and TCP-MD5 options. The +established sockets that have either AO or MD5 keys are restricted for +adding keys of the other option. + +For listening sockets the picture is different: BGP server may want to rec= eive +both TCP-AO and (deprecated) TCP-MD5 clients. As a result, both types of k= eys +may be added to TCP_CLOSED or TCP_LISTEN sockets. It's not allowed to add +different types of keys for the same peer. + +6. SNE Linux implementation +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D + +RFC 5925 [6.2] describes the algorithm of how to extend TCP sequence numbe= rs +with SNE. In short: TCP has to track the previous sequence numbers and set +sne_flag when the current SEQ number rolls over. The flag is cleared when +both current and previous SEQ numbers cross 0x7fff, which is 32Kb. + +In times when sne_flag is set, the algorithm compares SEQ for each packet = with +0x7fff and if it's higher than 32Kb, it assumes that the packet should be +verified with SNE before the increment. As a result, there's +this [0; 32Kb] window, when packets with (SNE - 1) can be accepted. + +Linux implementation simplifies this a bit: as the network stack already t= racks +the first SEQ byte that ACK is wanted for (snd_una) and the next SEQ byte = that +is wanted (rcv_nxt) - that's enough information for a rough estimation +on where in the 4GB SEQ number space both sender and receiver are. +When they roll over to zero, the corresponding SNE gets incremented. + +tcp_ao_compute_sne() is called for each TCP-AO segment. It compares SEQ nu= mbers +from the segment with snd_una or rcv_nxt and fits the result into a 2GB wi= ndow around them, +detecting SEQ numbers rolling over. That simplifies the code a lot and only +requires SNE numbers to be stored on every TCP-AO socket. + +The 2GB window at first glance seems much more permissive compared to +RFC 5926. But that is only used to pick the correct SNE before/after +a rollover. It allows more TCP segment replays, but yet all regular +TCP checks in tcp_sequence() are applied on the verified segment. +So, it trades a bit more permissive acceptance of replayed/retransmitted +segments for the simplicity of the algorithm and what seems better behavio= ur +for large TCP windows. + +7. Links +=3D=3D=3D=3D=3D=3D=3D=3D + +RFC 5925 The TCP Authentication Option + https://www.rfc-editor.org/rfc/pdfrfc/rfc5925.txt.pdf + +RFC 5926 Cryptographic Algorithms for the TCP Authentication Option (TCP-A= O) + https://www.rfc-editor.org/rfc/pdfrfc/rfc5926.txt.pdf + +Draft "SHA-2 Algorithm for the TCP Authentication Option (TCP-AO)" + https://datatracker.ietf.org/doc/html/draft-nayak-tcp-sha2-03 + +RFC 2385 Protection of BGP Sessions via the TCP MD5 Signature Option + https://www.rfc-editor.org/rfc/pdfrfc/rfc2385.txt.pdf + +:Author: Dmitry Safonov --=20 2.41.0