diff mbox series

[iptables,17/23] extensions: libebt_802_3: Use guided option parser

Message ID 20231220160636.11778-18-phil@nwl.cc
State Accepted
Headers show
Series Guided option parser for ebtables | expand

Commit Message

Phil Sutter Dec. 20, 2023, 4:06 p.m. UTC
Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 extensions/libebt_802_3.c | 74 ++++++++++-----------------------------
 extensions/libebt_802_3.t |  2 ++
 2 files changed, 21 insertions(+), 55 deletions(-)
diff mbox series

Patch

diff --git a/extensions/libebt_802_3.c b/extensions/libebt_802_3.c
index 8cbcdcea4912f..26a7725cd0074 100644
--- a/extensions/libebt_802_3.c
+++ b/extensions/libebt_802_3.c
@@ -13,17 +13,20 @@ 
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <getopt.h>
 #include <xtables.h>
 #include <linux/netfilter_bridge/ebt_802_3.h>
 
-#define _802_3_SAP	'1'
-#define _802_3_TYPE	'2'
-
-static const struct option br802_3_opts[] = {
-	{ .name = "802_3-sap",	.has_arg = true, .val = _802_3_SAP },
-	{ .name = "802_3-type",	.has_arg = true, .val = _802_3_TYPE },
-	XT_GETOPT_TABLEEND,
+static const struct xt_option_entry br802_3_opts[] =
+{
+	{ .name = "802_3-sap", .id = EBT_802_3_SAP,
+	  .type = XTTYPE_UINT8, .base = 16,
+	  .flags = XTOPT_INVERT | XTOPT_PUT,
+	  XTOPT_POINTER(struct ebt_802_3_info, sap) },
+	{ .name = "802_3-type", .id = EBT_802_3_TYPE,
+	  .type = XTTYPE_UINT16, .base = 16,
+	  .flags = XTOPT_INVERT | XTOPT_PUT | XTOPT_NBO,
+	  XTOPT_POINTER(struct ebt_802_3_info, type) },
+	XTOPT_TABLEEND,
 };
 
 static void br802_3_print_help(void)
@@ -36,52 +39,14 @@  static void br802_3_print_help(void)
 "  Type implies SAP value 0xaa\n");
 }
 
-static int
-br802_3_parse(int c, char **argv, int invert, unsigned int *flags,
-	      const void *entry, struct xt_entry_match **match)
+static void br802_3_parse(struct xt_option_call *cb)
 {
-	struct ebt_802_3_info *info = (struct ebt_802_3_info *) (*match)->data;
-	unsigned int i;
-	char *end;
-
-	switch (c) {
-	case _802_3_SAP:
-		if (invert)
-			info->invflags |= EBT_802_3_SAP;
-		i = strtoul(optarg, &end, 16);
-		if (i > 255 || *end != '\0')
-			xtables_error(PARAMETER_PROBLEM,
-				      "Problem with specified "
-					"sap hex value, %x",i);
-		info->sap = i; /* one byte, so no byte order worries */
-		info->bitmask |= EBT_802_3_SAP;
-		break;
-	case _802_3_TYPE:
-		if (invert)
-			info->invflags |= EBT_802_3_TYPE;
-		i = strtoul(optarg, &end, 16);
-		if (i > 65535 || *end != '\0') {
-			xtables_error(PARAMETER_PROBLEM,
-				      "Problem with the specified "
-					"type hex value, %x",i);
-		}
-		info->type = htons(i);
-		info->bitmask |= EBT_802_3_TYPE;
-		break;
-	default:
-		return 0;
-	}
+	struct ebt_802_3_info *info = cb->data;
 
-	*flags |= info->bitmask;
-	return 1;
-}
-
-static void
-br802_3_final_check(unsigned int flags)
-{
-	if (!flags)
-		xtables_error(PARAMETER_PROBLEM,
-			      "You must specify proper arguments");
+	xtables_option_parse(cb);
+	info->bitmask |= cb->entry->id;
+	if (cb->invert)
+		info->invflags |= cb->entry->id;
 }
 
 static void br802_3_print(const void *ip, const struct xt_entry_match *match,
@@ -112,10 +77,9 @@  static struct xtables_match br802_3_match =
 	.size		= XT_ALIGN(sizeof(struct ebt_802_3_info)),
 	.userspacesize	= XT_ALIGN(sizeof(struct ebt_802_3_info)),
 	.help		= br802_3_print_help,
-	.parse		= br802_3_parse,
-	.final_check	= br802_3_final_check,
+	.x6_parse	= br802_3_parse,
 	.print		= br802_3_print,
-	.extra_opts	= br802_3_opts,
+	.x6_options	= br802_3_opts,
 };
 
 void _init(void)
diff --git a/extensions/libebt_802_3.t b/extensions/libebt_802_3.t
index a138f35d2c756..2e4945b388830 100644
--- a/extensions/libebt_802_3.t
+++ b/extensions/libebt_802_3.t
@@ -1,5 +1,7 @@ 
 :INPUT,FORWARD,OUTPUT
 --802_3-sap ! 0x0a -j CONTINUE;=;FAIL
 --802_3-type 0x000a -j RETURN;=;FAIL
+-p Length --802_3-sap 0x0a -j CONTINUE;=;OK
 -p Length --802_3-sap ! 0x0a -j CONTINUE;=;OK
 -p Length --802_3-type 0x000a -j RETURN;=;OK
+-p Length --802_3-type ! 0x000a -j RETURN;=;OK