netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: netfilter-devel@vger.kernel.org
Subject: [iptables PATCH 2/6] libxtables: Fix guided option parser for use with arptables
Date: Wed, 22 Nov 2023 22:52:57 +0100	[thread overview]
Message-ID: <20231122215301.15725-3-phil@nwl.cc> (raw)
In-Reply-To: <20231122215301.15725-1-phil@nwl.cc>

With an unexpected value in afinfo->family, guided option parser was
rather useless when called from arptables extensions. Introduce
afinfo_family() wrapper to sanitize at least NFPROTO_ARP value.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 libxtables/xtoptions.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/libxtables/xtoptions.c b/libxtables/xtoptions.c
index 0667315ceccf8..25540f8b88c6d 100644
--- a/libxtables/xtoptions.c
+++ b/libxtables/xtoptions.c
@@ -65,6 +65,19 @@ static const size_t xtopt_psize[] = {
 	[XTTYPE_ETHERMAC]    = sizeof(uint8_t[6]),
 };
 
+/**
+ * Return a sanitized afinfo->family value, covering for NFPROTO_ARP
+ */
+static uint8_t afinfo_family(void)
+{
+	switch (afinfo->family) {
+	case NFPROTO_ARP:
+		return NFPROTO_IPV4;
+	default:
+		return afinfo->family;
+	}
+}
+
 /**
  * Creates getopt options from the x6-style option map, and assigns each a
  * getopt id.
@@ -461,7 +474,7 @@ static socklen_t xtables_sa_hostlen(unsigned int afproto)
  */
 static void xtopt_parse_host(struct xt_option_call *cb)
 {
-	struct addrinfo hints = {.ai_family = afinfo->family};
+	struct addrinfo hints = {.ai_family = afinfo_family()};
 	unsigned int adcount = 0;
 	struct addrinfo *res, *p;
 	int ret;
@@ -472,7 +485,7 @@ static void xtopt_parse_host(struct xt_option_call *cb)
 			"getaddrinfo: %s\n", gai_strerror(ret));
 
 	memset(&cb->val.hmask, 0xFF, sizeof(cb->val.hmask));
-	cb->val.hlen = (afinfo->family == NFPROTO_IPV4) ? 32 : 128;
+	cb->val.hlen = (afinfo_family() == NFPROTO_IPV4) ? 32 : 128;
 
 	for (p = res; p != NULL; p = p->ai_next) {
 		if (adcount == 0) {
@@ -615,7 +628,7 @@ static void xtopt_parse_mport(struct xt_option_call *cb)
 
 static int xtopt_parse_mask(struct xt_option_call *cb)
 {
-	struct addrinfo hints = {.ai_family = afinfo->family,
+	struct addrinfo hints = {.ai_family = afinfo_family(),
 				 .ai_flags = AI_NUMERICHOST };
 	struct addrinfo *res;
 	int ret;
@@ -627,7 +640,7 @@ static int xtopt_parse_mask(struct xt_option_call *cb)
 	memcpy(&cb->val.hmask, xtables_sa_host(res->ai_addr, res->ai_family),
 	       xtables_sa_hostlen(res->ai_family));
 
-	switch(afinfo->family) {
+	switch(afinfo_family()) {
 	case AF_INET:
 		cb->val.hlen = xtables_ipmask_to_cidr(&cb->val.hmask.in);
 		break;
@@ -649,7 +662,7 @@ static void xtopt_parse_plen(struct xt_option_call *cb)
 	const struct xt_option_entry *entry = cb->entry;
 	unsigned int prefix_len = 128; /* happiness is a warm gcc */
 
-	cb->val.hlen = (afinfo->family == NFPROTO_IPV4) ? 32 : 128;
+	cb->val.hlen = (afinfo_family() == NFPROTO_IPV4) ? 32 : 128;
 	if (!xtables_strtoui(cb->arg, NULL, &prefix_len, 0, cb->val.hlen)) {
 		/* Is this mask expressed in full format? e.g. 255.255.255.0 */
 		if (xtopt_parse_mask(cb))
-- 
2.41.0


  parent reply	other threads:[~2023-11-22 21:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-22 21:52 [iptables PATCH 0/6] Extend guided option parser for use by arptables Phil Sutter
2023-11-22 21:52 ` [iptables PATCH 1/6] libxtables: Combine the two extension option mergers Phil Sutter
2023-11-22 21:52 ` Phil Sutter [this message]
2023-11-22 21:52 ` [iptables PATCH 3/6] libxtables: Introduce xtables_strtoul_base() Phil Sutter
2023-11-22 21:52 ` [iptables PATCH 4/6] libxtables: Introduce struct xt_option_entry::base Phil Sutter
2023-11-22 21:53 ` [iptables PATCH 5/6] extensions: libarpt_mangle: Use guided option parser Phil Sutter
2023-11-22 21:53 ` [iptables PATCH 6/6] extensions: MARK: arptables: " Phil Sutter
2023-11-23 16:55 ` [iptables PATCH 0/6] Extend guided option parser for use by arptables Phil Sutter

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=20231122215301.15725-3-phil@nwl.cc \
    --to=phil@nwl.cc \
    --cc=netfilter-devel@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).