Blame SOURCES/xtables-Introduce-and-use-common-function-to-parse-v.patch

43df5c
From cc564f1b24a61d8abcd1163323ba68d373ef3d7c Mon Sep 17 00:00:00 2001
43df5c
From: Phil Sutter <psutter@redhat.com>
43df5c
Date: Wed, 3 Apr 2019 20:30:11 +0200
43df5c
Subject: [PATCH] xtables: Introduce and use common function to parse
43df5c
 val[/mask] arguments
43df5c
43df5c
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1657075
43df5c
Upstream Status: iptables commit 29b1d97764d18
43df5c
43df5c
commit 29b1d97764d1849651388d870565b3fa815a0bd8
43df5c
Author: Serhey Popovych <serhe.popovych@gmail.com>
43df5c
Date:   Thu Mar 1 13:03:11 2018 +0200
43df5c
43df5c
    xtables: Introduce and use common function to parse val[/mask] arguments
43df5c
43df5c
    There are a couple of places in both core and extensions where arguments
43df5c
    in the form of val[/mask] is parsed (see XTTYPE_MARKMASK32).
43df5c
43df5c
    In some cases symbolic name might be used which is mapped in code to
43df5c
    numeric value.
43df5c
43df5c
    Introduce common function to handle both cases where value given is
43df5c
    either val[/mask] or symbolic name.
43df5c
43df5c
    Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
43df5c
    Signed-off-by: Florian Westphal <fw@strlen.de>
43df5c
43df5c
Signed-off-by: Phil Sutter <psutter@redhat.com>
43df5c
---
43df5c
 extensions/libipt_realm.c   | 29 ++++++---------------
43df5c
 extensions/libxt_devgroup.c | 35 ++++---------------------
43df5c
 include/xtables.h           | 11 ++++++++
43df5c
 libxtables/xtables.c        | 52 +++++++++++++++++++++++++++++++++++++
43df5c
 libxtables/xtoptions.c      | 22 +---------------
43df5c
 5 files changed, 77 insertions(+), 72 deletions(-)
43df5c
43df5c
diff --git a/extensions/libipt_realm.c b/extensions/libipt_realm.c
43df5c
index fffb1218db7a6..0bfbaea0add23 100644
43df5c
--- a/extensions/libipt_realm.c
43df5c
+++ b/extensions/libipt_realm.c
43df5c
@@ -34,30 +34,17 @@ static struct xtables_lmap *realms;
43df5c
 
43df5c
 static void realm_parse(struct xt_option_call *cb)
43df5c
 {
43df5c
-	struct xt_realm_info *realminfo = cb->data;
43df5c
-	int id;
43df5c
-	char *end;
43df5c
+	struct xt_realm_info *ri = cb->data;
43df5c
+	unsigned int id, mask;
43df5c
 
43df5c
 	xtables_option_parse(cb);
43df5c
-	realminfo->id = strtoul(cb->arg, &end, 0);
43df5c
-	if (end != cb->arg && (*end == '/' || *end == '\0')) {
43df5c
-		if (*end == '/')
43df5c
-			realminfo->mask = strtoul(end+1, &end, 0);
43df5c
-		else
43df5c
-			realminfo->mask = 0xffffffff;
43df5c
-		if (*end != '\0' || end == cb->arg)
43df5c
-			xtables_error(PARAMETER_PROBLEM,
43df5c
-				   "Bad realm value \"%s\"", cb->arg);
43df5c
-	} else {
43df5c
-		id = xtables_lmap_name2id(realms, cb->arg);
43df5c
-		if (id == -1)
43df5c
-			xtables_error(PARAMETER_PROBLEM,
43df5c
-				   "Realm \"%s\" not found", cb->arg);
43df5c
-		realminfo->id = id;
43df5c
-		realminfo->mask = 0xffffffff;
43df5c
-	}
43df5c
+	xtables_parse_val_mask(cb, &id, &mask, realms);
43df5c
+
43df5c
+	ri->id = id;
43df5c
+	ri->mask = mask;
43df5c
+
43df5c
 	if (cb->invert)
43df5c
-		realminfo->invert = 1;
43df5c
+		ri->invert = 1;
43df5c
 }
43df5c
 
43df5c
 static void
43df5c
diff --git a/extensions/libxt_devgroup.c b/extensions/libxt_devgroup.c
43df5c
index ebfa2aee80cf2..604828276177b 100644
43df5c
--- a/extensions/libxt_devgroup.c
43df5c
+++ b/extensions/libxt_devgroup.c
43df5c
@@ -35,49 +35,24 @@ static const char f_devgroups[] = "/etc/iproute2/group";
43df5c
 /* array of devgroups from f_devgroups[] */
43df5c
 static struct xtables_lmap *devgroups;
43df5c
 
43df5c
-static void devgroup_parse_groupspec(const char *arg, unsigned int *group,
43df5c
-				     unsigned int *mask)
43df5c
-{
43df5c
-	char *end;
43df5c
-	bool ok;
43df5c
-
43df5c
-	ok = xtables_strtoui(arg, &end, group, 0, UINT32_MAX);
43df5c
-	if (ok && (*end == '/' || *end == '\0')) {
43df5c
-		if (*end == '/')
43df5c
-			ok = xtables_strtoui(end + 1, NULL, mask,
43df5c
-			                     0, UINT32_MAX);
43df5c
-		else
43df5c
-			*mask = ~0U;
43df5c
-		if (!ok)
43df5c
-			xtables_error(PARAMETER_PROBLEM,
43df5c
-				      "Bad group value \"%s\"", arg);
43df5c
-	} else {
43df5c
-		*group = xtables_lmap_name2id(devgroups, arg);
43df5c
-		if (*group == -1)
43df5c
-			xtables_error(PARAMETER_PROBLEM,
43df5c
-				      "Device group \"%s\" not found", arg);
43df5c
-		*mask = ~0U;
43df5c
-	}
43df5c
-}
43df5c
-
43df5c
 static void devgroup_parse(struct xt_option_call *cb)
43df5c
 {
43df5c
 	struct xt_devgroup_info *info = cb->data;
43df5c
-	unsigned int id, mask;
43df5c
+	unsigned int group, mask;
43df5c
 
43df5c
 	xtables_option_parse(cb);
43df5c
+	xtables_parse_val_mask(cb, &group, &mask, devgroups);
43df5c
+
43df5c
 	switch (cb->entry->id) {
43df5c
 	case O_SRC_GROUP:
43df5c
-		devgroup_parse_groupspec(cb->arg, &id, &mask);
43df5c
-		info->src_group = id;
43df5c
+		info->src_group = group;
43df5c
 		info->src_mask  = mask;
43df5c
 		info->flags |= XT_DEVGROUP_MATCH_SRC;
43df5c
 		if (cb->invert)
43df5c
 			info->flags |= XT_DEVGROUP_INVERT_SRC;
43df5c
 		break;
43df5c
 	case O_DST_GROUP:
43df5c
-		devgroup_parse_groupspec(cb->arg, &id, &mask);
43df5c
-		info->dst_group = id;
43df5c
+		info->dst_group = group;
43df5c
 		info->dst_mask  = mask;
43df5c
 		info->flags |= XT_DEVGROUP_MATCH_DST;
43df5c
 		if (cb->invert)
43df5c
diff --git a/include/xtables.h b/include/xtables.h
43df5c
index 021726708b2ee..47481e693ca25 100644
43df5c
--- a/include/xtables.h
43df5c
+++ b/include/xtables.h
43df5c
@@ -501,6 +501,17 @@ extern void xtables_save_string(const char *value);
43df5c
 
43df5c
 extern void xtables_print_num(uint64_t number, unsigned int format);
43df5c
 
43df5c
+extern void xtables_parse_val_mask(struct xt_option_call *cb,
43df5c
+				   unsigned int *val, unsigned int *mask,
43df5c
+				   const struct xtables_lmap *lmap);
43df5c
+
43df5c
+static inline void xtables_parse_mark_mask(struct xt_option_call *cb,
43df5c
+					   unsigned int *mark,
43df5c
+					   unsigned int *mask)
43df5c
+{
43df5c
+	xtables_parse_val_mask(cb, mark, mask, NULL);
43df5c
+}
43df5c
+
43df5c
 #if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS)
43df5c
 #	ifdef _INIT
43df5c
 #		undef _init
43df5c
diff --git a/libxtables/xtables.c b/libxtables/xtables.c
43df5c
index 7210d3706bf26..2981f52bc767f 100644
43df5c
--- a/libxtables/xtables.c
43df5c
+++ b/libxtables/xtables.c
43df5c
@@ -1950,6 +1950,58 @@ void xtables_print_num(uint64_t number, unsigned int format)
43df5c
 	printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
43df5c
 }
43df5c
 
43df5c
+void xtables_parse_val_mask(struct xt_option_call *cb,
43df5c
+			    unsigned int *val, unsigned int *mask,
43df5c
+			    const struct xtables_lmap *lmap)
43df5c
+{
43df5c
+	char *end;
43df5c
+
43df5c
+	*mask = ~0U;
43df5c
+
43df5c
+	if (!xtables_strtoui(cb->arg, &end, val, 0, UINT32_MAX)) {
43df5c
+		if (lmap)
43df5c
+			goto name2val;
43df5c
+		else
43df5c
+			goto bad_val;
43df5c
+	}
43df5c
+
43df5c
+	if (*end == '\0')
43df5c
+		return;
43df5c
+
43df5c
+	if (*end != '/') {
43df5c
+		if (lmap)
43df5c
+			goto name2val;
43df5c
+		else
43df5c
+			goto garbage;
43df5c
+	}
43df5c
+
43df5c
+	if (!xtables_strtoui(end + 1, &end, mask, 0, UINT32_MAX))
43df5c
+		goto bad_val;
43df5c
+
43df5c
+	if (*end == '\0')
43df5c
+		return;
43df5c
+
43df5c
+garbage:
43df5c
+	xt_params->exit_err(PARAMETER_PROBLEM,
43df5c
+			"%s: trailing garbage after value "
43df5c
+			"for option \"--%s\".\n",
43df5c
+			cb->ext_name, cb->entry->name);
43df5c
+
43df5c
+bad_val:
43df5c
+	xt_params->exit_err(PARAMETER_PROBLEM,
43df5c
+			"%s: bad integer value for option \"--%s\", "
43df5c
+			"or out of range.\n",
43df5c
+			cb->ext_name, cb->entry->name);
43df5c
+
43df5c
+name2val:
43df5c
+	*val = xtables_lmap_name2id(lmap, cb->arg);
43df5c
+	if ((int)*val == -1)
43df5c
+		xt_params->exit_err(PARAMETER_PROBLEM,
43df5c
+			"%s: could not map name %s to an integer value "
43df5c
+			"for option \"--%s\".\n",
43df5c
+			cb->ext_name, cb->arg, cb->entry->name);
43df5c
+}
43df5c
+
43df5c
 int kernel_version;
43df5c
 
43df5c
 void get_kernel_version(void)
43df5c
diff --git a/libxtables/xtoptions.c b/libxtables/xtoptions.c
43df5c
index 1d3fda73dedf7..4bbc03ab0f047 100644
43df5c
--- a/libxtables/xtoptions.c
43df5c
+++ b/libxtables/xtoptions.c
43df5c
@@ -432,27 +432,7 @@ static void xtopt_parse_tosmask(struct xt_option_call *cb)
43df5c
  */
43df5c
 static void xtopt_parse_markmask(struct xt_option_call *cb)
43df5c
 {
43df5c
-	unsigned int mark = 0, mask = ~0U;
43df5c
-	char *end;
43df5c
-
43df5c
-	if (!xtables_strtoui(cb->arg, &end, &mark, 0, UINT32_MAX))
43df5c
-		xt_params->exit_err(PARAMETER_PROBLEM,
43df5c
-			"%s: bad mark value for option \"--%s\", "
43df5c
-			"or out of range.\n",
43df5c
-			cb->ext_name, cb->entry->name);
43df5c
-	if (*end == '/' &&
43df5c
-	    !xtables_strtoui(end + 1, &end, &mask, 0, UINT32_MAX))
43df5c
-		xt_params->exit_err(PARAMETER_PROBLEM,
43df5c
-			"%s: bad mask value for option \"--%s\", "
43df5c
-			"or out of range.\n",
43df5c
-			cb->ext_name, cb->entry->name);
43df5c
-	if (*end != '\0')
43df5c
-		xt_params->exit_err(PARAMETER_PROBLEM,
43df5c
-			"%s: trailing garbage after value "
43df5c
-			"for option \"--%s\".\n",
43df5c
-			cb->ext_name, cb->entry->name);
43df5c
-	cb->val.mark = mark;
43df5c
-	cb->val.mask = mask;
43df5c
+	xtables_parse_mark_mask(cb, &cb->val.mark, &cb->val.mask);
43df5c
 }
43df5c
 
43df5c
 static int xtopt_sysloglvl_compare(const void *a, const void *b)
43df5c
-- 
43df5c
2.21.0
43df5c