Skip to contentSkip to conversation

netfilter: nf_tables: add pkttype support to meta expression

PATCH Superseded Ana Rey 2 participants 6 messages Jun 13, 2014 – Aug 24, 2014

Subsystems (2)
lore
Share & tools
lore

Ana Rey

author
#0 lore raw

PATCH v2 1/1 netfilter: nf_tables: add pkttype support to meta expression

Joint work with Álvaro Neira Ayuso <[email protected]>

---
[Changes in v2:]
Put "case NFT_META_PKTTYPE:" outside of the #ifdef CONFIG_NETWORK_SECMARK. It was a mistake.
Thanks to Arturo Borrero Gonzalez <[email protected]> for reporting this mistake.

 include/uapi/linux/netfilter/nf_tables.h |  2 ++
 net/netfilter/nft_meta.c                 | 17 +++++++++++++++++
 2 files changed, 19 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +2 -0
index 2a88f64..9eb5153 100644
@@ -571,6 +571,7 @@ enum nft_exthdr_attributes {
  * @NFT_META_L4PROTO: layer 4 protocol number
  * @NFT_META_BRI_IIFNAME: packet input bridge interface name
  * @NFT_META_BRI_OIFNAME: packet output bridge interface name
+ * @NFT_META_PKTTYPE: Packet class
  */
 enum nft_meta_keys {
 	NFT_META_LEN,
@@ -592,6 +593,7 @@ enum nft_meta_keys {
 	NFT_META_L4PROTO,
 	NFT_META_BRI_IIFNAME,
 	NFT_META_BRI_OIFNAME,
+	NFT_META_PKTTYPE,
 };
 
 /**
net/netfilter/nft_meta.c +17 -0
index 852b178..eb9882e 100644
@@ -14,6 +14,9 @@
 #include <linux/netlink.h>
 #include <linux/netfilter.h>
 #include <linux/netfilter/nf_tables.h>
+#include <linux/in.h>
+#include <linux/ip.h>
+#include <linux/ipv6.h>
 #include <net/dst.h>
 #include <net/sock.h>
 #include <net/tcp_states.h> /* for TCP_TIME_WAIT */
@@ -124,6 +127,19 @@ void nft_meta_get_eval(const struct nft_expr *expr,
 		dest->data[0] = skb->secmark;
 		break;
 #endif
+	case NFT_META_PKTTYPE:
+		dest->data[0] = skb->pkt_type;
+		if (skb->pkt_type != PACKET_LOOPBACK)
+			dest->data[0] = skb->pkt_type;
+		else if (expr->ops->type->family == NFPROTO_IPV4 &&
+			 ipv4_is_multicast(ip_hdr(skb)->daddr))
+			dest->data[0] = PACKET_MULTICAST;
+		else if (expr->ops->type->family == NFPROTO_IPV6 &&
+			 ipv6_hdr(skb)->daddr.s6_addr[0] == 0xFF)
+			dest->data[0] = PACKET_MULTICAST;
+		else
+			dest->data[0] = PACKET_BROADCAST;
+		break;
 	default:
 		WARN_ON(1);
 		goto err;
@@ -195,6 +211,7 @@ int nft_meta_get_init(const struct nft_ctx *ctx,
 #ifdef CONFIG_NETWORK_SECMARK
 	case NFT_META_SECMARK:
 #endif
+	case NFT_META_PKTTYPE:
 		break;
 	default:
 		return -EOPNOTSUPP;
Signature · 6 lines
2.0.0

--
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
#1 lore raw
Quoted 21 lines · Ana Rey

Joint work with Álvaro Neira Ayuso <[email protected]>

---
[Changes in v2:]
Put "case NFT_META_PKTTYPE:" outside of the #ifdef CONFIG_NETWORK_SECMARK. It was a mistake.
Thanks to Arturo Borrero Gonzalez <[email protected]> for reporting this mistake.

 include/uapi/linux/netfilter/nf_tables.h |  2 ++
 net/netfilter/nft_meta.c                 | 17 +++++++++++++++++
 2 files changed, 19 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +0 -0
index 2a88f64..9eb5153 100644

include/uapi/linux/netfilter/nf_tables.h enum nft_exthdr_attributes { · line 574

Quoted 4 lines
include/uapi/linux/netfilter/nf_tables.h +1 -0
@@ -571,6 +571,7 @@ enum nft_exthdr_attributes {
  * @NFT_META_L4PROTO: layer 4 protocol number
  * @NFT_META_BRI_IIFNAME: packet input bridge interface name
  * @NFT_META_BRI_OIFNAME: packet output bridge interface name
+ * @NFT_META_PKTTYPE: Packet class
                                ^^^^^

type?

Quoted 26 lines
include/uapi/linux/netfilter/nf_tables.h +1 -0
  */
 enum nft_meta_keys {
 	NFT_META_LEN,
@@ -592,6 +593,7 @@ enum nft_meta_keys {
 	NFT_META_L4PROTO,
 	NFT_META_BRI_IIFNAME,
 	NFT_META_BRI_OIFNAME,
+	NFT_META_PKTTYPE,
 };
 
 /**
net/netfilter/nft_meta.c +3 -0
index 852b178..eb9882e 100644
@@ -14,6 +14,9 @@
 #include <linux/netlink.h>
 #include <linux/netfilter.h>
 #include <linux/netfilter/nf_tables.h>
+#include <linux/in.h>
+#include <linux/ip.h>
+#include <linux/ipv6.h>
 #include <net/dst.h>
 #include <net/sock.h>
 #include <net/tcp_states.h> /* for TCP_TIME_WAIT */

net/netfilter/nft_meta.c void nft_meta_get_eval(const struct nft_expr *expr, · line 134

Quoted 8 lines
net/netfilter/nft_meta.c +5 -0
@@ -124,6 +127,19 @@ void nft_meta_get_eval(const struct nft_expr *expr,
 		dest->data[0] = skb->secmark;
 		break;
 #endif
+	case NFT_META_PKTTYPE:
+		dest->data[0] = skb->pkt_type;
+		if (skb->pkt_type != PACKET_LOOPBACK)
+			dest->data[0] = skb->pkt_type;
+		else if (expr->ops->type->family == NFPROTO_IPV4 &&
                         ^----------------------^

This has problems with NFPROTO_INET, NFPROTO_BRIDGE and NFPROTO_ARP when handling the loopback case.

You have to add a nft_meta per family, something similar to:

05513e9 netfilter: nf_tables: add reject module for NFPROTO_INET

In NFPROTO_INET, you have to use:

        if (pkt->ops->pf == NF_PROTO_IPV4)
                ...

In NFPROTO_BRIDGE, you have to use:

        if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)
                ...

In NFPROTO_ARP, you have to check the destination address:

        if (is_broadcast_ether_addr(ethhdr->h_dest)
                ...
Quoted 26 lines
net/netfilter/nft_meta.c +9 -0
+			 ipv4_is_multicast(ip_hdr(skb)->daddr))
+			dest->data[0] = PACKET_MULTICAST;
+		else if (expr->ops->type->family == NFPROTO_IPV6 &&
+			 ipv6_hdr(skb)->daddr.s6_addr[0] == 0xFF)
+			dest->data[0] = PACKET_MULTICAST;
+		else
+			dest->data[0] = PACKET_BROADCAST;
+		break;
 	default:
 		WARN_ON(1);
 		goto err;
@@ -195,6 +211,7 @@ int nft_meta_get_init(const struct nft_ctx *ctx,
 #ifdef CONFIG_NETWORK_SECMARK
 	case NFT_META_SECMARK:
 #endif
+	case NFT_META_PKTTYPE:
 		break;
 	default:
 		return -EOPNOTSUPP;
Signature · 6 lines
2.0.0

--
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

--
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
#2 lore raw

PATCH v3 1/1 netfilter: nf_tables: add pkttype support to meta expression

In reply to Ana Rey

From: Álvaro Neira Ayuso <[email protected]>

Add pkttype support for all families of tables.

Joint work with Álvaro Neira Ayuso <[email protected]>

---

[Changes in v3:]
* Add support for NFPROTO_INET when handing the loopback case. (NFPROTO_BRIDGE and NFPROTO_ARP families do not make sense when handing the loopback case)

* Delete the broadcast case when handing the lookback case. This does not make sense.

* Fix the description of NFT_META_PKTTYPE key.

[Changes in v2]
Put "case NFT_META_PKTTYPE:" outside of the #ifdef CONFIG_NETWORK_SECMARK. It was a mistake.
Thanks to Arturo Borrero Gonzalez <[email protected]> for reporting this mistake.

 include/uapi/linux/netfilter/nf_tables.h |    2 ++
 net/netfilter/nft_meta.c                 |   34 ++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +2 -0
index 801bdd1..72ad208 100644
@@ -571,6 +571,7 @@ enum nft_exthdr_attributes {
  * @NFT_META_L4PROTO: layer 4 protocol number
  * @NFT_META_BRI_IIFNAME: packet input bridge interface name
  * @NFT_META_BRI_OIFNAME: packet output bridge interface name
+ * @NFT_META_PKTTYPE: Packet type
  */
 enum nft_meta_keys {
 	NFT_META_LEN,
@@ -592,6 +593,7 @@ enum nft_meta_keys {
 	NFT_META_L4PROTO,
 	NFT_META_BRI_IIFNAME,
 	NFT_META_BRI_OIFNAME,
+	NFT_META_PKTTYPE,
 };
 
 /**
net/netfilter/nft_meta.c +34 -0
index 852b178..c46d9ac 100644
@@ -14,6 +14,9 @@
 #include <linux/netlink.h>
 #include <linux/netfilter.h>
 #include <linux/netfilter/nf_tables.h>
+#include <linux/in.h>
+#include <linux/ip.h>
+#include <linux/ipv6.h>
 #include <net/dst.h>
 #include <net/sock.h>
 #include <net/tcp_states.h> /* for TCP_TIME_WAIT */
@@ -124,6 +127,36 @@ void nft_meta_get_eval(const struct nft_expr *expr,
 		dest->data[0] = skb->secmark;
 		break;
 #endif
+	case NFT_META_PKTTYPE:
+		if (skb->pkt_type != PACKET_LOOPBACK) {
+			dest->data[0] = skb->pkt_type;
+			break;
+		}
+
+		switch (pkt->ops->pf) {
+		case NFPROTO_IPV4:
+			if (ipv4_is_multicast(ip_hdr(skb)->daddr))
+				dest->data[0] = PACKET_MULTICAST;
+			else
+				dest->data[0] = PACKET_HOST;
+			break;
+		case NFPROTO_IPV6:
+			if (ipv6_hdr(skb)->daddr.s6_addr[0] == 0xFF)
+				dest->data[0] = PACKET_MULTICAST;
+			else
+				dest->data[0] = PACKET_HOST;
+			break;
+		case NFPROTO_INET:
+			if (ipv6_hdr(skb)->daddr.s6_addr[0] == 0xFF ||
+			    ipv4_is_multicast(ip_hdr(skb)->daddr))
+				dest->data[0] = PACKET_MULTICAST;
+			else
+				dest->data[0] = PACKET_HOST;
+			break;
+		default:
+			goto err;
+		}
+		break;
 	default:
 		WARN_ON(1);
 		goto err;
@@ -195,6 +228,7 @@ int nft_meta_get_init(const struct nft_ctx *ctx,
 #ifdef CONFIG_NETWORK_SECMARK
 	case NFT_META_SECMARK:
 #endif
+	case NFT_META_PKTTYPE:
 		break;
 	default:
 		return -EOPNOTSUPP;
Signature · 6 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
#3 lore raw
Quoted 3 lines · Ana Rey

From: Álvaro Neira Ayuso <[email protected]>

Add pkttype support for all families of tables.

Please, include in the description something like:

This allows you to fetch the meta packet type based on the link layer information. The loopback traffic is a special case, the packet type is guessed from the network layer header.

No special handling for bridge and arp since we're not going to see such traffic in the loopback interface.

Quoted 20 lines
net/netfilter/nft_meta.c +5 -0
index 852b178..c46d9ac 100644
@@ -14,6 +14,9 @@
 #include <linux/netlink.h>
 #include <linux/netfilter.h>
 #include <linux/netfilter/nf_tables.h>
+#include <linux/in.h>
+#include <linux/ip.h>
+#include <linux/ipv6.h>
 #include <net/dst.h>
 #include <net/sock.h>
 #include <net/tcp_states.h> /* for TCP_TIME_WAIT */
@@ -124,6 +127,36 @@ void nft_meta_get_eval(const struct nft_expr *expr,
 		dest->data[0] = skb->secmark;
 		break;
 #endif
+	case NFT_META_PKTTYPE:
+		if (skb->pkt_type != PACKET_LOOPBACK) {

net/netfilter/nft_meta.c void nft_meta_get_eval(const struct nft_expr *expr, · line 141

Quoted 10 lines
net/netfilter/nft_meta.c +10 -0
+			dest->data[0] = skb->pkt_type;
+			break;
+		}
+
+		switch (pkt->ops->pf) {
+		case NFPROTO_IPV4:
+			if (ipv4_is_multicast(ip_hdr(skb)->daddr))
+				dest->data[0] = PACKET_MULTICAST;
+			else
+				dest->data[0] = PACKET_HOST;

Please, replace the PACKET_HOST by PACKET_BROADCAST so this looks exactly like xt_pkttype.

Quoted 4 lines
net/netfilter/nft_meta.c +4 -0
+			break;
+		case NFPROTO_IPV6:
+			if (ipv6_hdr(skb)->daddr.s6_addr[0] == 0xFF)
+				dest->data[0] = PACKET_MULTICAST;

net/netfilter/nft_meta.c void nft_meta_get_eval(const struct nft_expr *expr, · line 155

Quoted 10 lines
net/netfilter/nft_meta.c +10 -0
+			else
+				dest->data[0] = PACKET_HOST;
+			break;
+		case NFPROTO_INET:
+			if (ipv6_hdr(skb)->daddr.s6_addr[0] == 0xFF ||
+			    ipv4_is_multicast(ip_hdr(skb)->daddr))
+				dest->data[0] = PACKET_MULTICAST;
+			else
+				dest->data[0] = PACKET_HOST;
+			break;

We're almost there. You have to remove NFPROTO_INET, the pkt->ops->pf is already allowing you to indentify the family. So the NFPROTO_INET part seems dead code to me, remove it.
--
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
#4 lore raw

PATCH v4 1/1 netfilter: nf_tables: add pkttype support to meta expression

In reply to Ana Rey

From: Álvaro Neira Ayuso <[email protected]>

Add pkttype support for ip, ipv6 and inet families of tables.

This allows you to fetch the meta packet type based on the link layer information. The loopback traffic is a special case, the packet type is guessed from the network layer header.

No special handling for bridge and arp since we're not going to see such traffic in the loopback interface.

Joint work with Alvaro Neira Ayuso <[email protected]>

---

[Changes in v4:]
* Improvements in the description of this path.
* Replace the PACKET_HOST by PACKET_BROADCAST so this looks exactly like xt_pkttype
* Remove NFPROTO_INET because the pkt->ops->pf is already allowing you to indentify this family.

These improvements were suggested by Pablo Neira.

[Changes in v3:]
* Add support for NFPROTO_INET when handing the loopback case. (NFPROTO_BRIDGE and NFPROTO_ARP families do not make sense when handing the loopback case)

* Delete the broadcast case when handing the lookback case. This does not make sense.

* Fix the description of NFT_META_PKTTYPE key.

[Changes in v2]
Put "case NFT_META_PKTTYPE:" outside of the #ifdef CONFIG_NETWORK_SECMARK. It was a mistake.
Thanks to Arturo Borrero Gonzalez <[email protected]> for reporting this mistake.

 include/uapi/linux/netfilter/nf_tables.h |    2 ++
 net/netfilter/nft_meta.c                 |   27 +++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +2 -0
index 801bdd1..72ad208 100644
@@ -571,6 +571,7 @@ enum nft_exthdr_attributes {
  * @NFT_META_L4PROTO: layer 4 protocol number
  * @NFT_META_BRI_IIFNAME: packet input bridge interface name
  * @NFT_META_BRI_OIFNAME: packet output bridge interface name
+ * @NFT_META_PKTTYPE: Packet type
  */
 enum nft_meta_keys {
 	NFT_META_LEN,
@@ -592,6 +593,7 @@ enum nft_meta_keys {
 	NFT_META_L4PROTO,
 	NFT_META_BRI_IIFNAME,
 	NFT_META_BRI_OIFNAME,
+	NFT_META_PKTTYPE,
 };
 
 /**
net/netfilter/nft_meta.c +27 -0
index 852b178..dbf7fe1 100644
@@ -14,6 +14,9 @@
 #include <linux/netlink.h>
 #include <linux/netfilter.h>
 #include <linux/netfilter/nf_tables.h>
+#include <linux/in.h>
+#include <linux/ip.h>
+#include <linux/ipv6.h>
 #include <net/dst.h>
 #include <net/sock.h>
 #include <net/tcp_states.h> /* for TCP_TIME_WAIT */
@@ -124,6 +127,29 @@ void nft_meta_get_eval(const struct nft_expr *expr,
 		dest->data[0] = skb->secmark;
 		break;
 #endif
+	case NFT_META_PKTTYPE:
+		if (skb->pkt_type != PACKET_LOOPBACK) {
+			dest->data[0] = skb->pkt_type;
+			break;
+		}
+
+		switch (pkt->ops->pf) {
+		case NFPROTO_IPV4:
+			if (ipv4_is_multicast(ip_hdr(skb)->daddr))
+				dest->data[0] = PACKET_MULTICAST;
+			else
+				dest->data[0] = PACKET_BROADCAST;
+			break;
+		case NFPROTO_IPV6:
+			if (ipv6_hdr(skb)->daddr.s6_addr[0] == 0xFF)
+				dest->data[0] = PACKET_MULTICAST;
+			else
+				dest->data[0] = PACKET_BROADCAST;
+			break;
+		default:
+			goto err;
+		}
+		break;
 	default:
 		WARN_ON(1);
 		goto err;
@@ -195,6 +221,7 @@ int nft_meta_get_init(const struct nft_ctx *ctx,
 #ifdef CONFIG_NETWORK_SECMARK
 	case NFT_META_SECMARK:
 #endif
+	case NFT_META_PKTTYPE:
 		break;
 	default:
 		return -EOPNOTSUPP;
Signature · 6 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
#5 lore raw
Quoted 4 lines · Ana Rey

From: Álvaro Neira Ayuso <[email protected]>

Add pkttype support for ip, ipv6 and inet families of tables.

Quoted 8 lines

This allows you to fetch the meta packet type based on the link layer information. The loopback traffic is a special case, the packet type is guessed from the network layer header.

No special handling for bridge and arp since we're not going to see such traffic in the loopback interface.

Joint work with Alvaro Neira Ayuso <[email protected]>

Applied with minor change in the documentation area in nf_tables.h.

Thanks.
--
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