]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
extensions: libebt_arp, libebt_ip: Use xtables_ipparse_any()
authorPhil Sutter <phil@nwl.cc>
Thu, 6 Oct 2022 12:54:48 +0000 (14:54 +0200)
committerPhil Sutter <phil@nwl.cc>
Tue, 15 Nov 2022 15:58:16 +0000 (16:58 +0100)
The libxtables function covers all formerly supported inputs (and more).
The extended libebt_arp.t passes before and after this patch.

Signed-off-by: Phil Sutter <phil@nwl.cc>
extensions/libebt_arp.c
extensions/libebt_arp.t
extensions/libebt_ip.c

index d5035b956cc1529a9a4401a242d34fae3ec0eab1..63a953d4637da9f6b5f7133c1fef239bb4d47b03 100644 (file)
@@ -87,91 +87,17 @@ static void brarp_print_help(void)
 #define OPT_MAC_D  0x40
 #define OPT_GRAT   0x80
 
-static int undot_ip(char *ip, unsigned char *ip2)
-{
-       char *p, *q, *end;
-       long int onebyte;
-       int i;
-       char buf[20];
-
-       strncpy(buf, ip, sizeof(buf) - 1);
-
-       p = buf;
-       for (i = 0; i < 3; i++) {
-               if ((q = strchr(p, '.')) == NULL)
-                       return -1;
-               *q = '\0';
-               onebyte = strtol(p, &end, 10);
-               if (*end != '\0' || onebyte > 255 || onebyte < 0)
-                       return -1;
-               ip2[i] = (unsigned char)onebyte;
-               p = q + 1;
-       }
-
-       onebyte = strtol(p, &end, 10);
-       if (*end != '\0' || onebyte > 255 || onebyte < 0)
-               return -1;
-       ip2[3] = (unsigned char)onebyte;
-
-       return 0;
-}
-
-static int ip_mask(char *mask, unsigned char *mask2)
-{
-       char *end;
-       long int bits;
-       uint32_t mask22;
-
-       if (undot_ip(mask, mask2)) {
-               /* not the /a.b.c.e format, maybe the /x format */
-               bits = strtol(mask, &end, 10);
-               if (*end != '\0' || bits > 32 || bits < 0)
-                       return -1;
-               if (bits != 0) {
-                       mask22 = htonl(0xFFFFFFFF << (32 - bits));
-                       memcpy(mask2, &mask22, 4);
-               } else {
-                       mask22 = 0xFFFFFFFF;
-                       memcpy(mask2, &mask22, 4);
-               }
-       }
-       return 0;
-}
-
-static void ebt_parse_ip_address(char *address, uint32_t *addr, uint32_t *msk)
-{
-       char *p;
-
-       /* first the mask */
-       if ((p = strrchr(address, '/')) != NULL) {
-               *p = '\0';
-               if (ip_mask(p + 1, (unsigned char *)msk)) {
-                       xtables_error(PARAMETER_PROBLEM,
-                                     "Problem with the IP mask '%s'", p + 1);
-                       return;
-               }
-       } else
-               *msk = 0xFFFFFFFF;
-
-       if (undot_ip(address, (unsigned char *)addr)) {
-               xtables_error(PARAMETER_PROBLEM,
-                             "Problem with the IP address '%s'", address);
-               return;
-       }
-       *addr = *addr & *msk;
-}
-
 static int
 brarp_parse(int c, char **argv, int invert, unsigned int *flags,
            const void *entry, struct xt_entry_match **match)
 {
        struct ebt_arp_info *arpinfo = (struct ebt_arp_info *)(*match)->data;
+       struct in_addr *ipaddr, ipmask;
        long int i;
        char *end;
-       uint32_t *addr;
-       uint32_t *mask;
        unsigned char *maddr;
        unsigned char *mmask;
+       unsigned int ipnr;
 
        switch (c) {
        case ARP_OPCODE:
@@ -231,24 +157,25 @@ brarp_parse(int c, char **argv, int invert, unsigned int *flags,
 
        case ARP_IP_S:
        case ARP_IP_D:
+               xtables_ipparse_any(optarg, &ipaddr, &ipmask, &ipnr);
                if (c == ARP_IP_S) {
                        EBT_CHECK_OPTION(flags, OPT_IP_S);
-                       addr = &arpinfo->saddr;
-                       mask = &arpinfo->smsk;
+                       arpinfo->saddr = ipaddr->s_addr;
+                       arpinfo->smsk = ipmask.s_addr;
                        arpinfo->bitmask |= EBT_ARP_SRC_IP;
                } else {
                        EBT_CHECK_OPTION(flags, OPT_IP_D);
-                       addr = &arpinfo->daddr;
-                       mask = &arpinfo->dmsk;
+                       arpinfo->daddr = ipaddr->s_addr;
+                       arpinfo->dmsk = ipmask.s_addr;
                        arpinfo->bitmask |= EBT_ARP_DST_IP;
                }
+               free(ipaddr);
                if (invert) {
                        if (c == ARP_IP_S)
                                arpinfo->invflags |= EBT_ARP_SRC_IP;
                        else
                                arpinfo->invflags |= EBT_ARP_DST_IP;
                }
-               ebt_parse_ip_address(optarg, addr, mask);
                break;
        case ARP_MAC_S:
        case ARP_MAC_D:
index 14ff0f097cfd8cc68171966cb3c4b1996c0701d4..96fbce906107c76527864cda52d4d465072e3601 100644 (file)
@@ -6,6 +6,9 @@
 -p ARP ! --arp-ip-dst 1.2.3.4;-p ARP --arp-ip-dst ! 1.2.3.4 -j CONTINUE;OK
 -p ARP --arp-ip-src ! 0.0.0.0;=;OK
 -p ARP --arp-ip-dst ! 0.0.0.0/8;=;OK
+-p ARP --arp-ip-src ! 1.2.3.4/32;-p ARP --arp-ip-src ! 1.2.3.4;OK
+-p ARP --arp-ip-src ! 1.2.3.4/255.255.255.0;-p ARP --arp-ip-src ! 1.2.3.0/24;OK
+-p ARP --arp-ip-src ! 1.2.3.4/255.0.255.255;-p ARP --arp-ip-src ! 1.0.3.4/255.0.255.255;OK
 -p ARP --arp-mac-src 00:de:ad:be:ef:00;=;OK
 -p ARP --arp-mac-dst de:ad:be:ef:00:00/ff:ff:ff:ff:00:00;=;OK
 -p ARP --arp-gratuitous;=;OK
index 8413a5aa8dd57b173d4f33b03b5e500ed585e4d7..d13e83c06895da56e777c3c4217aeb1123f470de 100644 (file)
@@ -164,80 +164,6 @@ parse_port_range(const char *protocol, const char *portstring, uint16_t *ports)
 }
 
 /* original code from ebtables: useful_functions.c */
-static int undot_ip(char *ip, unsigned char *ip2)
-{
-       char *p, *q, *end;
-       long int onebyte;
-       int i;
-       char buf[20];
-
-       strncpy(buf, ip, sizeof(buf) - 1);
-
-       p = buf;
-       for (i = 0; i < 3; i++) {
-               if ((q = strchr(p, '.')) == NULL)
-                       return -1;
-               *q = '\0';
-               onebyte = strtol(p, &end, 10);
-               if (*end != '\0' || onebyte > 255 || onebyte < 0)
-                       return -1;
-               ip2[i] = (unsigned char)onebyte;
-               p = q + 1;
-       }
-
-       onebyte = strtol(p, &end, 10);
-       if (*end != '\0' || onebyte > 255 || onebyte < 0)
-               return -1;
-       ip2[3] = (unsigned char)onebyte;
-
-       return 0;
-}
-
-static int ip_mask(char *mask, unsigned char *mask2)
-{
-       char *end;
-       long int bits;
-       uint32_t mask22;
-
-       if (undot_ip(mask, mask2)) {
-               /* not the /a.b.c.e format, maybe the /x format */
-               bits = strtol(mask, &end, 10);
-               if (*end != '\0' || bits > 32 || bits < 0)
-                       return -1;
-               if (bits != 0) {
-                       mask22 = htonl(0xFFFFFFFF << (32 - bits));
-                       memcpy(mask2, &mask22, 4);
-               } else {
-                       mask22 = 0xFFFFFFFF;
-                       memcpy(mask2, &mask22, 4);
-               }
-       }
-       return 0;
-}
-
-static void ebt_parse_ip_address(char *address, uint32_t *addr, uint32_t *msk)
-{
-       char *p;
-
-       /* first the mask */
-       if ((p = strrchr(address, '/')) != NULL) {
-               *p = '\0';
-               if (ip_mask(p + 1, (unsigned char *)msk)) {
-                       xtables_error(PARAMETER_PROBLEM,
-                                     "Problem with the IP mask '%s'", p + 1);
-                       return;
-               }
-       } else
-               *msk = 0xFFFFFFFF;
-
-       if (undot_ip(address, (unsigned char *)addr)) {
-               xtables_error(PARAMETER_PROBLEM,
-                             "Problem with the IP address '%s'", address);
-               return;
-       }
-       *addr = *addr & *msk;
-}
-
 static char *parse_range(const char *str, unsigned int res[])
 {
        char *next;
@@ -355,18 +281,26 @@ brip_parse(int c, char **argv, int invert, unsigned int *flags,
           const void *entry, struct xt_entry_match **match)
 {
        struct ebt_ip_info *info = (struct ebt_ip_info *)(*match)->data;
+       struct in_addr *ipaddr, ipmask;
+       unsigned int ipnr;
 
        switch (c) {
        case IP_SOURCE:
                if (invert)
                        info->invflags |= EBT_IP_SOURCE;
-               ebt_parse_ip_address(optarg, &info->saddr, &info->smsk);
+               xtables_ipparse_any(optarg, &ipaddr, &ipmask, &ipnr);
+               info->saddr = ipaddr->s_addr;
+               info->smsk = ipmask.s_addr;
+               free(ipaddr);
                info->bitmask |= EBT_IP_SOURCE;
                break;
        case IP_DEST:
                if (invert)
                        info->invflags |= EBT_IP_DEST;
-               ebt_parse_ip_address(optarg, &info->daddr, &info->dmsk);
+               xtables_ipparse_any(optarg, &ipaddr, &ipmask, &ipnr);
+               info->daddr = ipaddr->s_addr;
+               info->dmsk = ipmask.s_addr;
+               free(ipaddr);
                info->bitmask |= EBT_IP_DEST;
                break;
        case IP_SPORT: