]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
extensions: libebt_arpreply: Use guided option parser
authorPhil Sutter <phil@nwl.cc>
Sun, 9 Oct 2022 09:51:36 +0000 (11:51 +0200)
committerPhil Sutter <phil@nwl.cc>
Wed, 10 Jan 2024 15:07:31 +0000 (16:07 +0100)
extensions/libebt_arpreply.c
extensions/libebt_arpreply.t

index 80ba2159ff946bfeb7efb4e21f95ede136babc74..1d6ba36a27b036e31605f3350d569f181e865259 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <getopt.h>
 #include <xtables.h>
 #include <netinet/ether.h>
 #include <linux/netfilter_bridge/ebt_arpreply.h>
 #include "iptables/nft.h"
 #include "iptables/nft-bridge.h"
 
-#define OPT_REPLY_MAC     0x01
-#define OPT_REPLY_TARGET  0x02
+enum {
+       O_MAC,
+       O_TARGET,
+};
 
-#define REPLY_MAC '1'
-#define REPLY_TARGET '2'
-static const struct option brarpreply_opts[] = {
-       { "arpreply-mac" ,    required_argument, 0, REPLY_MAC    },
-       { "arpreply-target" , required_argument, 0, REPLY_TARGET },
-       XT_GETOPT_TABLEEND,
+static const struct xt_option_entry brarpreply_opts[] = {
+       { .name = "arpreply-mac" ,    .id = O_MAC, .type = XTTYPE_ETHERMAC,
+         .flags = XTOPT_PUT, XTOPT_POINTER(struct ebt_arpreply_info, mac) },
+       { .name = "arpreply-target" , .id = O_TARGET, .type = XTTYPE_STRING },
+       XTOPT_TABLEEND,
 };
 
 static void brarpreply_print_help(void)
@@ -44,31 +44,15 @@ static void brarpreply_init(struct xt_entry_target *target)
        replyinfo->target = EBT_DROP;
 }
 
-static int
-brarpreply_parse(int c, char **argv, int invert, unsigned int *flags,
-           const void *entry, struct xt_entry_target **tg)
-
+static void brarpreply_parse(struct xt_option_call *cb)
 {
-       struct ebt_arpreply_info *replyinfo = (void *)(*tg)->data;
-       struct ether_addr *addr;
-
-       switch (c) {
-       case REPLY_MAC:
-               EBT_CHECK_OPTION(flags, OPT_REPLY_MAC);
-               if (!(addr = ether_aton(optarg)))
-                       xtables_error(PARAMETER_PROBLEM, "Problem with specified --arpreply-mac mac");
-               memcpy(replyinfo->mac, addr, ETH_ALEN);
-               break;
-       case REPLY_TARGET:
-               EBT_CHECK_OPTION(flags, OPT_REPLY_TARGET);
-               if (ebt_fill_target(optarg, (unsigned int *)&replyinfo->target))
-                       xtables_error(PARAMETER_PROBLEM, "Illegal --arpreply-target target");
-               break;
+       struct ebt_arpreply_info *replyinfo = cb->data;
 
-       default:
-               return 0;
-       }
-       return 1;
+       xtables_option_parse(cb);
+       if (cb->entry->id == O_TARGET &&
+           ebt_fill_target(cb->arg, (unsigned int *)&replyinfo->target))
+               xtables_error(PARAMETER_PROBLEM,
+                             "Illegal --arpreply-target target");
 }
 
 static void brarpreply_print(const void *ip, const struct xt_entry_target *t, int numeric)
@@ -90,9 +74,9 @@ static struct xtables_target arpreply_target = {
        .size           = XT_ALIGN(sizeof(struct ebt_arpreply_info)),
        .userspacesize  = XT_ALIGN(sizeof(struct ebt_arpreply_info)),
        .help           = brarpreply_print_help,
-       .parse          = brarpreply_parse,
+       .x6_parse       = brarpreply_parse,
        .print          = brarpreply_print,
-       .extra_opts     = brarpreply_opts,
+       .x6_options     = brarpreply_opts,
 };
 
 void _init(void)
index 6734501a106b58ce16f04013eeec244a375ac4bd..66103e16dcd4201f1078a15758bdfd0dbf1ff0d9 100644 (file)
@@ -1,4 +1,8 @@
 :PREROUTING
 *nat
+-j arpreply;=;FAIL
+-p ARP -i foo -j arpreply;-p ARP -i foo -j arpreply --arpreply-mac 00:00:00:00:00:00;OK
 -p ARP -i foo -j arpreply --arpreply-mac de:ad:00:be:ee:ff --arpreply-target ACCEPT;=;OK
 -p ARP -i foo -j arpreply --arpreply-mac de:ad:00:be:ee:ff;=;OK
+-p ARP -j arpreply ! --arpreply-mac de:ad:00:be:ee:ff;;FAIL
+-p ARP -j arpreply --arpreply-mac de:ad:00:be:ee:ff ! --arpreply-target ACCEPT;;FAIL