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

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