From: Antony Antony <antony.antony@secunet.com>
To: Antony Antony <antony.antony@secunet.com>,
Steffen Klassert <steffen.klassert@secunet.com>,
Herbert Xu <herbert@gondor.apana.org.au>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>, David Ahern <dsahern@kernel.org>,
Masahide NAKAMURA <nakam@linux-ipv6.org>,
Paul Moore <paul@paul-moore.com>,
Stephen Smalley <stephen.smalley.work@gmail.com>,
Ondrej Mosnacek <omosnace@redhat.com>,
Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>
Cc: Sabrina Dubroca <sd@queasysnail.net>, <netdev@vger.kernel.org>,
<linux-kernel@vger.kernel.org>, <selinux@vger.kernel.org>,
<linux-doc@vger.kernel.org>,
Chiachang Wang <chiachangwang@google.com>,
Yan Yan <evitayan@google.com>, <devel@linux-ipsec.org>
Subject: [PATCH ipsec-next v9 12/16] xfrm: extract address family and selector validation helpers
Date: Tue, 26 May 2026 21:09:10 +0200 [thread overview]
Message-ID: <migrate-state-v9-12-ad9947e4ae74@secunet.com> (raw)
In-Reply-To: <migrate-state-v9-0-ad9947e4ae74@secunet.com>
Extract verify_xfrm_family() and verify_selector_prefixlen() from
verify_newsa_info() to allow reuse by other netlink handlers.
verify_xfrm_family() validates that a given address family is AF_INET
or AF_INET6 (with CONFIG_IPV6 guard).
verify_selector_prefixlen() validates that the selector prefix lengths
are within the bounds for the given address family.
No functional change.
Signed-off-by: Antony Antony <antony.antony@secunet.com>
---
v8->v9: added this patch
---
net/xfrm/xfrm_user.c | 80 +++++++++++++++++++++++++++++-----------------------
1 file changed, 44 insertions(+), 36 deletions(-)
diff --git a/net/xfrm/xfrm_user.c b/net/xfrm/xfrm_user.c
index 03fa4cabf601..c7ae670212a9 100644
--- a/net/xfrm/xfrm_user.c
+++ b/net/xfrm/xfrm_user.c
@@ -264,66 +264,74 @@ static int verify_mtimer_thresh(bool has_encap, u8 dir,
return 0;
}
-static int verify_newsa_info(struct xfrm_usersa_info *p,
- struct nlattr **attrs,
- struct netlink_ext_ack *extack)
+static int verify_xfrm_family(u16 family, struct netlink_ext_ack *extack)
{
- int err;
- u8 sa_dir = nla_get_u8_default(attrs[XFRMA_SA_DIR], 0);
- u16 family = p->sel.family;
-
- err = -EINVAL;
- switch (p->family) {
+ switch (family) {
case AF_INET:
- break;
-
+ return 0;
case AF_INET6:
#if IS_ENABLED(CONFIG_IPV6)
- break;
+ return 0;
#else
- err = -EAFNOSUPPORT;
NL_SET_ERR_MSG(extack, "IPv6 support disabled");
- goto out;
+ return -EAFNOSUPPORT;
#endif
-
default:
NL_SET_ERR_MSG(extack, "Invalid address family");
- goto out;
+ return -EINVAL;
}
+}
- if (!family && !(p->flags & XFRM_STATE_AF_UNSPEC))
- family = p->family;
-
+static int verify_selector_prefixlen(u16 family,
+ const struct xfrm_selector *sel,
+ struct netlink_ext_ack *extack)
+{
switch (family) {
case AF_UNSPEC:
- break;
-
+ return 0;
case AF_INET:
- if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) {
- NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)");
- goto out;
+ if (sel->prefixlen_d > 32 || sel->prefixlen_s > 32) {
+ NL_SET_ERR_MSG(extack,
+ "Invalid prefix length in selector (must be <= 32 for IPv4)");
+ return -EINVAL;
}
-
- break;
-
+ return 0;
case AF_INET6:
#if IS_ENABLED(CONFIG_IPV6)
- if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) {
- NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)");
- goto out;
+ if (sel->prefixlen_d > 128 || sel->prefixlen_s > 128) {
+ NL_SET_ERR_MSG(extack,
+ "Invalid prefix length in selector (must be <= 128 for IPv6)");
+ return -EINVAL;
}
-
- break;
+ return 0;
#else
NL_SET_ERR_MSG(extack, "IPv6 support disabled");
- err = -EAFNOSUPPORT;
- goto out;
+ return -EAFNOSUPPORT;
#endif
-
default:
NL_SET_ERR_MSG(extack, "Invalid address family in selector");
- goto out;
+ return -EINVAL;
}
+}
+
+static int verify_newsa_info(struct xfrm_usersa_info *p,
+ struct nlattr **attrs,
+ struct netlink_ext_ack *extack)
+{
+ int err;
+ u8 sa_dir = nla_get_u8_default(attrs[XFRMA_SA_DIR], 0);
+ u16 family = p->sel.family;
+
+ err = verify_xfrm_family(p->family, extack);
+ if (err)
+ goto out;
+
+ if (!family && !(p->flags & XFRM_STATE_AF_UNSPEC))
+ family = p->family;
+
+ err = verify_selector_prefixlen(family, &p->sel, extack);
+ if (err)
+ goto out;
err = -EINVAL;
switch (p->id.proto) {
--
2.47.3
next prev parent reply other threads:[~2026-05-26 19:09 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-26 19:05 [PATCH ipsec-next v9 00/16] xfrm: XFRM_MSG_MIGRATE_STATE new netlink message Antony Antony
2026-05-26 19:05 ` [PATCH ipsec-next v9 01/16] xfrm: remove redundant assignments Antony Antony
2026-05-26 19:06 ` [PATCH ipsec-next v9 02/16] xfrm: add extack to xfrm_init_state Antony Antony
2026-05-26 19:06 ` [PATCH ipsec-next v9 03/16] xfrm: allow migration from UDP encapsulated to non-encapsulated ESP Antony Antony
2026-05-26 19:07 ` [PATCH ipsec-next v9 04/16] xfrm: fix NAT-related field inheritance in SA migration Antony Antony
2026-05-26 19:07 ` [PATCH ipsec-next v9 05/16] xfrm: rename reqid in xfrm_migrate Antony Antony
2026-05-26 19:07 ` [PATCH ipsec-next v9 06/16] xfrm: split xfrm_state_migrate into create and install functions Antony Antony
2026-05-26 19:07 ` [PATCH ipsec-next v9 07/16] xfrm: check family before comparing addresses in migrate Antony Antony
2026-05-26 19:08 ` [PATCH ipsec-next v9 08/16] xfrm: add state synchronization after migration Antony Antony
2026-05-26 19:08 ` [PATCH ipsec-next v9 09/16] xfrm: add error messages to state migration Antony Antony
2026-05-26 19:08 ` [PATCH ipsec-next v9 10/16] xfrm: move encap and xuo into struct xfrm_migrate Antony Antony
2026-05-26 19:08 ` [PATCH ipsec-next v9 11/16] xfrm: refactor XFRMA_MTIMER_THRESH validation into a helper Antony Antony
2026-05-26 19:09 ` Antony Antony [this message]
2026-05-26 19:09 ` [PATCH ipsec-next v9 13/16] xfrm: make xfrm_dev_state_add xuo parameter const Antony Antony
2026-05-26 19:09 ` [PATCH ipsec-next v9 14/16] xfrm: add XFRM_MSG_MIGRATE_STATE for single SA migration Antony Antony
2026-05-26 19:10 ` [PATCH ipsec-next v9 15/16] xfrm: restrict netlink attributes for XFRM_MSG_MIGRATE_STATE Antony Antony
2026-05-26 19:10 ` [PATCH ipsec-next v9 16/16] xfrm: add documentation " Antony Antony
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=migrate-state-v9-12-ad9947e4ae74@secunet.com \
--to=antony.antony@secunet.com \
--cc=chiachangwang@google.com \
--cc=corbet@lwn.net \
--cc=davem@davemloft.net \
--cc=devel@linux-ipsec.org \
--cc=dsahern@kernel.org \
--cc=edumazet@google.com \
--cc=evitayan@google.com \
--cc=herbert@gondor.apana.org.au \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nakam@linux-ipv6.org \
--cc=netdev@vger.kernel.org \
--cc=omosnace@redhat.com \
--cc=pabeni@redhat.com \
--cc=paul@paul-moore.com \
--cc=sd@queasysnail.net \
--cc=selinux@vger.kernel.org \
--cc=skhan@linuxfoundation.org \
--cc=steffen.klassert@secunet.com \
--cc=stephen.smalley.work@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox