Skip to contentSkip to conversation

nft: fix interface wildcard matching

PATCH In review Pablo Neira Ayuso 2 participants 5 messages Oct 16, 2013 – Oct 17, 2013 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 nft: fix interface wildcard matching

In (73ea1cc nft: convert rule into a command state structure), the interface wildcard matching got broken. The previous handling was flawed by the use of ifnametoindex in scenario where the interface may vanished after a rule was added.

This approach relies on the trailing '\0' to identify if this is an exact or wildcard matching, based on discussion with Florian.

Based on initial patch from Anand Raj Manickam.

---
 iptables/nft-shared.c |   38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)
iptables/nft-shared.c +16 -22
index 3987f74..e0eaa17 100644
@@ -124,13 +124,11 @@ void add_iniface(struct nft_rule *r, char *iface, int invflags)
 	else
 		op = NFT_CMP_EQ;
 
-	if (iface[iface_len - 1] == '+') {
-		add_meta(r, NFT_META_IIFNAME);
+	add_meta(r, NFT_META_IIFNAME);
+	if (iface[iface_len - 1] == '+')
 		add_cmp_ptr(r, op, iface, iface_len - 1);
-	} else {
-		add_meta(r, NFT_META_IIF);
-		add_cmp_u32(r, if_nametoindex(iface), op);
-	}
+	else
+		add_cmp_ptr(r, op, iface, iface_len + 1);
 }
 
 void add_outiface(struct nft_rule *r, char *iface, int invflags)
@@ -145,13 +143,11 @@ void add_outiface(struct nft_rule *r, char *iface, int invflags)
 	else
 		op = NFT_CMP_EQ;
 
-	if (iface[iface_len - 1] == '+') {
-		add_meta(r, NFT_META_OIFNAME);
+	add_meta(r, NFT_META_OIFNAME);
+	if (iface[iface_len - 1] == '+')
 		add_cmp_ptr(r, op, iface, iface_len - 1);
-	} else {
-		add_meta(r, NFT_META_OIF);
-		add_cmp_u32(r, if_nametoindex(iface), op);
-	}
+	else
+		add_cmp_ptr(r, op, iface, iface_len + 1);
 }
 
 void add_addr(struct nft_rule *r, int offset,
@@ -251,15 +247,14 @@ void parse_meta(struct nft_rule_expr *e, uint8_t key, char *iniface,
 			*invflags |= IPT_INV_VIA_IN;
 
 		memcpy(iniface, ifname, len);
-		iniface[len] = '\0';
 
-		/* If zero, then this is an interface mask */
-		if (if_nametoindex(iniface) == 0) {
+		if (iniface[len] == '\0')
+			memset(iniface_mask, 0xff, len);
+		else {
 			iniface[len] = '+';
 			iniface[len+1] = '\0';
+			memset(iniface_mask, 0xff, len + 1);
 		}
-
-		memset(iniface_mask, 0xff, len);
 		break;
 	case NFT_META_OIFNAME:
 		ifname = nft_rule_expr_get(e, NFT_EXPR_CMP_DATA, &len);
@@ -267,15 +262,14 @@ void parse_meta(struct nft_rule_expr *e, uint8_t key, char *iniface,
 			*invflags |= IPT_INV_VIA_OUT;
 
 		memcpy(outiface, ifname, len);
-		outiface[len] = '\0';
 
-		/* If zero, then this is an interface mask */
-		if (if_nametoindex(outiface) == 0) {
+		if (outiface[len] == '\0')
+			memset(outiface_mask, 0xff, len);
+		else {
 			outiface[len] = '+';
 			outiface[len+1] = '\0';
+			memset(outiface_mask, 0xff, len + 1);
 		}
-
-		memset(outiface_mask, 0xff, len);
 		break;
 	default:
 		DEBUGP("unknown meta key %d\n", key);
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 85 lines · Pablo Neira Ayuso

In (73ea1cc nft: convert rule into a command state structure), the interface wildcard matching got broken. The previous handling was flawed by the use of ifnametoindex in scenario where the interface may vanished after a rule was added.

This approach relies on the trailing '\0' to identify if this is an exact or wildcard matching, based on discussion with Florian.

Based on initial patch from Anand Raj Manickam.

---
 iptables/nft-shared.c |   38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)
iptables/nft-shared.c +14 -20
index 3987f74..e0eaa17 100644
@@ -124,13 +124,11 @@ void add_iniface(struct nft_rule *r, char *iface, int invflags)
        else
                op = NFT_CMP_EQ;
 
-       if (iface[iface_len - 1] == '+') {
-               add_meta(r, NFT_META_IIFNAME);
+       add_meta(r, NFT_META_IIFNAME);
+       if (iface[iface_len - 1] == '+')
                add_cmp_ptr(r, op, iface, iface_len - 1);
-       } else {
-               add_meta(r, NFT_META_IIF);
-               add_cmp_u32(r, if_nametoindex(iface), op);
-       }
+       else
+               add_cmp_ptr(r, op, iface, iface_len + 1);
 }
 
 void add_outiface(struct nft_rule *r, char *iface, int invflags)
@@ -145,13 +143,11 @@ void add_outiface(struct nft_rule *r, char *iface, int invflags)
        else
                op = NFT_CMP_EQ;
 
-       if (iface[iface_len - 1] == '+') {
-               add_meta(r, NFT_META_OIFNAME);
+       add_meta(r, NFT_META_OIFNAME);
+       if (iface[iface_len - 1] == '+')
                add_cmp_ptr(r, op, iface, iface_len - 1);
-       } else {
-               add_meta(r, NFT_META_OIF);
-               add_cmp_u32(r, if_nametoindex(iface), op);
-       }
+       else
+               add_cmp_ptr(r, op, iface, iface_len + 1);
 }
 
 void add_addr(struct nft_rule *r, int offset,
@@ -251,15 +247,14 @@ void parse_meta(struct nft_rule_expr *e, uint8_t key, char *iniface,
                        *invflags |= IPT_INV_VIA_IN;
 
                memcpy(iniface, ifname, len);
-               iniface[len] = '\0';
 
-               /* If zero, then this is an interface mask */
-               if (if_nametoindex(iniface) == 0) {
+               if (iniface[len] == '\0')
+                       memset(iniface_mask, 0xff, len);
+               else {
                        iniface[len] = '+';
                        iniface[len+1] = '\0';
+                       memset(iniface_mask, 0xff, len + 1);
                }
-
-               memset(iniface_mask, 0xff, len);
                break;
        case NFT_META_OIFNAME:
                ifname = nft_rule_expr_get(e, NFT_EXPR_CMP_DATA, &len);
@@ -267,15 +262,14 @@ void parse_meta(struct nft_rule_expr *e, uint8_t key, char *iniface,
                        *invflags |= IPT_INV_VIA_OUT;
 
                memcpy(outiface, ifname, len);
-               outiface[len] = '\0';
 
-               /* If zero, then this is an interface mask */
-               if (if_nametoindex(outiface) == 0) {
+               if (outiface[len] == '\0')
+                       memset(outiface_mask, 0xff, len);

iptables/nft-shared.c void parse_meta(struct nft_rule_expr *e, uint8_t key, char *iniface, · line 275

Quoted 10 lines
iptables/nft-shared.c +2 -2
+               else {
                        outiface[len] = '+';
                        outiface[len+1] = '\0';
+                       memset(outiface_mask, 0xff, len + 1);
                }
-
-               memset(outiface_mask, 0xff, len);
                break;
        default:
                DEBUGP("unknown meta key %d\n", key);

Pablo,
This again breaks the delete functionality .

Quoted 7 lines

--
1.7.10.4

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author maintainer
#2 lore raw
Quoted 91 lines · Anand Raj Manickam
Quoted 90 lines · Pablo Neira Ayuso

In (73ea1cc nft: convert rule into a command state structure), the interface wildcard matching got broken. The previous handling was flawed by the use of ifnametoindex in scenario where the interface may vanished after a rule was added.

This approach relies on the trailing '\0' to identify if this is an exact or wildcard matching, based on discussion with Florian.

Based on initial patch from Anand Raj Manickam.

---
 iptables/nft-shared.c |   38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)
iptables/nft-shared.c +16 -20
index 3987f74..e0eaa17 100644
@@ -124,13 +124,11 @@ void add_iniface(struct nft_rule *r, char *iface, int invflags)
        else
                op = NFT_CMP_EQ;
 
-       if (iface[iface_len - 1] == '+') {
-               add_meta(r, NFT_META_IIFNAME);
+       add_meta(r, NFT_META_IIFNAME);
+       if (iface[iface_len - 1] == '+')
                add_cmp_ptr(r, op, iface, iface_len - 1);
-       } else {
-               add_meta(r, NFT_META_IIF);
-               add_cmp_u32(r, if_nametoindex(iface), op);
-       }
+       else
+               add_cmp_ptr(r, op, iface, iface_len + 1);
 }
 
 void add_outiface(struct nft_rule *r, char *iface, int invflags)
@@ -145,13 +143,11 @@ void add_outiface(struct nft_rule *r, char *iface, int invflags)
        else
                op = NFT_CMP_EQ;
 
-       if (iface[iface_len - 1] == '+') {
-               add_meta(r, NFT_META_OIFNAME);
+       add_meta(r, NFT_META_OIFNAME);
+       if (iface[iface_len - 1] == '+')
                add_cmp_ptr(r, op, iface, iface_len - 1);
-       } else {
-               add_meta(r, NFT_META_OIF);
-               add_cmp_u32(r, if_nametoindex(iface), op);
-       }
+       else
+               add_cmp_ptr(r, op, iface, iface_len + 1);
 }
 
 void add_addr(struct nft_rule *r, int offset,
@@ -251,15 +247,14 @@ void parse_meta(struct nft_rule_expr *e, uint8_t key, char *iniface,
                        *invflags |= IPT_INV_VIA_IN;
 
                memcpy(iniface, ifname, len);
-               iniface[len] = '\0';
 
-               /* If zero, then this is an interface mask */
-               if (if_nametoindex(iniface) == 0) {
+               if (iniface[len] == '\0')
+                       memset(iniface_mask, 0xff, len);
+               else {
                        iniface[len] = '+';
                        iniface[len+1] = '\0';
+                       memset(iniface_mask, 0xff, len + 1);
                }
-
-               memset(iniface_mask, 0xff, len);
                break;
        case NFT_META_OIFNAME:
                ifname = nft_rule_expr_get(e, NFT_EXPR_CMP_DATA, &len);
@@ -267,15 +262,14 @@ void parse_meta(struct nft_rule_expr *e, uint8_t key, char *iniface,
                        *invflags |= IPT_INV_VIA_OUT;
 
                memcpy(outiface, ifname, len);
-               outiface[len] = '\0';
 
-               /* If zero, then this is an interface mask */
-               if (if_nametoindex(outiface) == 0) {
+               if (outiface[len] == '\0')
+                       memset(outiface_mask, 0xff, len);
+               else {
                        outiface[len] = '+';
                        outiface[len+1] = '\0';
+                       memset(outiface_mask, 0xff, len + 1);
                }
Quoted 8 lines

iptables/nft-shared.c void parse_meta(struct nft_rule_expr *e, uint8_t key, char *iniface, · line 275

Quoted 5 lines
iptables/nft-shared.c +0 -2
-
-               memset(outiface_mask, 0xff, len);
                break;
        default:
                DEBUGP("unknown meta key %d\n", key);

Pablo,
This again breaks the delete functionality .

This is working here with a fresh compilation:

# xtables -I INPUT -i eth+
# xtables -D INPUT -i eth+
# xtables -I INPUT -i eth0
# xtables -D INPUT -i eth0
# which xtables
/usr/sbin/xtables
# ls -la /usr/sbin/xtables
lrwxrwxrwx 1 root root 13 oct 17 10:42 /usr/sbin/xtables -> xtables-multi

What problem are you noticing?

#3 lore raw
Quoted 108 lines · Pablo Neira Ayuso
Quoted 91 lines · Anand Raj Manickam
Quoted 90 lines · Pablo Neira Ayuso

In (73ea1cc nft: convert rule into a command state structure), the interface wildcard matching got broken. The previous handling was flawed by the use of ifnametoindex in scenario where the interface may vanished after a rule was added.

This approach relies on the trailing '\0' to identify if this is an exact or wildcard matching, based on discussion with Florian.

Based on initial patch from Anand Raj Manickam.

---
 iptables/nft-shared.c |   38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)
iptables/nft-shared.c +16 -20
index 3987f74..e0eaa17 100644
@@ -124,13 +124,11 @@ void add_iniface(struct nft_rule *r, char *iface, int invflags)
        else
                op = NFT_CMP_EQ;
 
-       if (iface[iface_len - 1] == '+') {
-               add_meta(r, NFT_META_IIFNAME);
+       add_meta(r, NFT_META_IIFNAME);
+       if (iface[iface_len - 1] == '+')
                add_cmp_ptr(r, op, iface, iface_len - 1);
-       } else {
-               add_meta(r, NFT_META_IIF);
-               add_cmp_u32(r, if_nametoindex(iface), op);
-       }
+       else
+               add_cmp_ptr(r, op, iface, iface_len + 1);
 }
 
 void add_outiface(struct nft_rule *r, char *iface, int invflags)
@@ -145,13 +143,11 @@ void add_outiface(struct nft_rule *r, char *iface, int invflags)
        else
                op = NFT_CMP_EQ;
 
-       if (iface[iface_len - 1] == '+') {
-               add_meta(r, NFT_META_OIFNAME);
+       add_meta(r, NFT_META_OIFNAME);
+       if (iface[iface_len - 1] == '+')
                add_cmp_ptr(r, op, iface, iface_len - 1);
-       } else {
-               add_meta(r, NFT_META_OIF);
-               add_cmp_u32(r, if_nametoindex(iface), op);
-       }
+       else
+               add_cmp_ptr(r, op, iface, iface_len + 1);
 }
 
 void add_addr(struct nft_rule *r, int offset,
@@ -251,15 +247,14 @@ void parse_meta(struct nft_rule_expr *e, uint8_t key, char *iniface,
                        *invflags |= IPT_INV_VIA_IN;
 
                memcpy(iniface, ifname, len);
-               iniface[len] = '\0';
 
-               /* If zero, then this is an interface mask */
-               if (if_nametoindex(iniface) == 0) {
+               if (iniface[len] == '\0')
+                       memset(iniface_mask, 0xff, len);
+               else {
                        iniface[len] = '+';
                        iniface[len+1] = '\0';
+                       memset(iniface_mask, 0xff, len + 1);
                }
-
-               memset(iniface_mask, 0xff, len);
                break;
        case NFT_META_OIFNAME:
                ifname = nft_rule_expr_get(e, NFT_EXPR_CMP_DATA, &len);
@@ -267,15 +262,14 @@ void parse_meta(struct nft_rule_expr *e, uint8_t key, char *iniface,
                        *invflags |= IPT_INV_VIA_OUT;
 
                memcpy(outiface, ifname, len);
-               outiface[len] = '\0';
 
-               /* If zero, then this is an interface mask */
-               if (if_nametoindex(outiface) == 0) {
+               if (outiface[len] == '\0')
+                       memset(outiface_mask, 0xff, len);
+               else {
                        outiface[len] = '+';
                        outiface[len+1] = '\0';
+                       memset(outiface_mask, 0xff, len + 1);
                }
Quoted 8 lines

iptables/nft-shared.c void parse_meta(struct nft_rule_expr *e, uint8_t key, char *iniface, · line 275

Quoted 5 lines
iptables/nft-shared.c +0 -2
-
-               memset(outiface_mask, 0xff, len);
                break;
        default:
                DEBUGP("unknown meta key %d\n", key);

Pablo,
This again breaks the delete functionality .

This is working here with a fresh compilation:

# xtables -I INPUT -i eth+
# xtables -D INPUT -i eth+
# xtables -I INPUT -i eth0
# xtables -D INPUT -i eth0
Quoted 6 lines
# which xtables
/usr/sbin/xtables
# ls -la /usr/sbin/xtables
lrwxrwxrwx 1 root root 13 oct 17 10:42 /usr/sbin/xtables -> xtables-multi

What problem are you noticing?

Sorry about it , i guess i had the wrong build .. works great .. Do you still want to maintain the refrence for NFT_META_IIF / NFT_META_OIF ?

author maintainer
#4 lore raw
Quoted 3 lines · Anand Raj Manickam
Quoted 2 lines · Pablo Neira Ayuso
Quoted 1 line · Anand Raj Manickam

On Wed, Oct 16, 2013 at 7:37 PM, Pablo Neira Ayuso <[email protected]> wrote:

[...]

Quoted 9 lines
Quoted 9 lines
Quoted 1 line

This again breaks the delete functionality .

This is working here with a fresh compilation:

# xtables -I INPUT -i eth+
# xtables -D INPUT -i eth+
# xtables -I INPUT -i eth0
# xtables -D INPUT -i eth0
Quoted 8 lines
Quoted 6 lines
# which xtables
/usr/sbin/xtables
# ls -la /usr/sbin/xtables
lrwxrwxrwx 1 root root 13 oct 17 10:42 /usr/sbin/xtables -> xtables-multi

What problem are you noticing?

Sorry about it , i guess i had the wrong build .. works great ..

No problem.

Quoted 1 line

Do you still want to maintain the refrence for NFT_META_IIF / NFT_META_OIF ?

Yes, currently you cannot use nftables and xtables at the same time, but some degree of interaction is desired. This should allow xtables to interpret add rule added using ifindex from nft.