diff mbox

[nf,2/2] bridge: set the pktinfo for IPv4/IPv6 traffic

Message ID 1415960990-19489-2-git-send-email-alvaroneay@gmail.com
State Changes Requested
Delegated to: Pablo Neira
Headers show

Commit Message

Alvaro Neira Nov. 14, 2014, 10:29 a.m. UTC
This patch sets the pktinfo for IPv4/IPv6 traffic. Therefore, we can check the
meta l4proto for IPv4/IPv6 traffic in bridge, before we don't have enough
information to do it.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
---
 net/bridge/netfilter/nf_tables_bridge.c |   17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

Comments

Pablo Neira Ayuso Nov. 20, 2014, 1:06 p.m. UTC | #1
On Fri, Nov 14, 2014 at 11:29:50AM +0100, Alvaro Neira Ayuso wrote:
> This patch sets the pktinfo for IPv4/IPv6 traffic. Therefore, we can check the
> meta l4proto for IPv4/IPv6 traffic in bridge, before we don't have enough
> information to do it.
>
> Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
> ---
>  net/bridge/netfilter/nf_tables_bridge.c |   17 ++++++++++++++++-
>  1 file changed, 16 insertions(+), 1 deletion(-)
> 
> diff --git a/net/bridge/netfilter/nf_tables_bridge.c b/net/bridge/netfilter/nf_tables_bridge.c
> index d468c19..0a0f0ca 100644
> --- a/net/bridge/netfilter/nf_tables_bridge.c
> +++ b/net/bridge/netfilter/nf_tables_bridge.c
> @@ -16,6 +16,8 @@
>  #include <net/netfilter/nf_tables_bridge.h>
>  #include <linux/ip.h>
>  #include <linux/ipv6.h>
> +#include <net/netfilter/nf_tables_ipv4.h>
> +#include <net/netfilter/nf_tables_ipv6.h>
>  
>  int nft_bridge_iphdr_validate(struct sk_buff *skb)
>  {
> @@ -71,8 +73,21 @@ nft_do_chain_bridge(const struct nf_hook_ops *ops,
>  {
>  	struct nft_pktinfo pkt;
>  
> -	nft_set_pktinfo(&pkt, ops, skb, in, out);
> +	switch (eth_hdr(skb)->h_proto) {
> +	case htons(ETH_P_IP):
> +		if (!nft_bridge_iphdr_validate(skb))
> +			break;
> +		nft_set_pktinfo_ipv4(&pkt, ops, skb, in, out);
> +		return nft_do_chain(&pkt, ops);

Please, clean up this, add this function:

static inline void nft_bridge_set_pktinfo_ipv4(...)
{
        if (nft_bridge_iphdr_validate(skb))
                nft_set_pktinfo_ipv4(&pkt, ops, skb, in, out);
        else
                nft_set_pktinfo(&pkt, ops)
}

And use it.

> +	case htons(ETH_P_IPV6):
> +		if (!nft_bridge_ip6hdr_validate(skb))
> +			break;
> +		if (nft_set_pktinfo_ipv6(&pkt, ops, skb, in, out) < 0)
> +			break;
> +		return nft_do_chain(&pkt, ops);

static inline void nft_bridge_set_pktinfo_ipv4(...)
{
#if IS_ENABLED(CONFIG_IPV6)
        if (!nft_bridge_ip6hdr_validate(skb) ||
            nft_set_pktinfo_ipv6(&pkt, ops, skb, in, out) < 0)
                nft_set_pktinfo(&pkt, ops);
#endif
}

And use it. The #if is needed because nft_set_pktinfo_ipv6 calls
ipv6_find_hdr() which may not be available if CONFIG_IPV6 is set.

Then, don't forget to:

        return nft_do_chain(&pkt, ops);

so we only have one single out output path in this function.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/net/bridge/netfilter/nf_tables_bridge.c b/net/bridge/netfilter/nf_tables_bridge.c
index d468c19..0a0f0ca 100644
--- a/net/bridge/netfilter/nf_tables_bridge.c
+++ b/net/bridge/netfilter/nf_tables_bridge.c
@@ -16,6 +16,8 @@ 
 #include <net/netfilter/nf_tables_bridge.h>
 #include <linux/ip.h>
 #include <linux/ipv6.h>
+#include <net/netfilter/nf_tables_ipv4.h>
+#include <net/netfilter/nf_tables_ipv6.h>
 
 int nft_bridge_iphdr_validate(struct sk_buff *skb)
 {
@@ -71,8 +73,21 @@  nft_do_chain_bridge(const struct nf_hook_ops *ops,
 {
 	struct nft_pktinfo pkt;
 
-	nft_set_pktinfo(&pkt, ops, skb, in, out);
+	switch (eth_hdr(skb)->h_proto) {
+	case htons(ETH_P_IP):
+		if (!nft_bridge_iphdr_validate(skb))
+			break;
+		nft_set_pktinfo_ipv4(&pkt, ops, skb, in, out);
+		return nft_do_chain(&pkt, ops);
+	case htons(ETH_P_IPV6):
+		if (!nft_bridge_ip6hdr_validate(skb))
+			break;
+		if (nft_set_pktinfo_ipv6(&pkt, ops, skb, in, out) < 0)
+			break;
+		return nft_do_chain(&pkt, ops);
+	}
 
+	nft_set_pktinfo(&pkt, ops, skb, in, out);
 	return nft_do_chain(&pkt, ops);
 }