Commit 68b0faa8 authored by Alvaro Neira's avatar Alvaro Neira Committed by Pablo Neira Ayuso

netfilter: nf_tables_bridge: export nft_reject_ip*hdr_validate functions

This patch exports the functions nft_reject_iphdr_validate and
nft_reject_ip6hdr_validate to use it in follow up patches.
These functions check if the IPv4/IPv6 header is correct.
Signed-off-by: default avatarAlvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: default avatarPablo Neira Ayuso <pablo@netfilter.org>
parent c41884ce
#ifndef _NET_NF_TABLES_BRIDGE_H
#define _NET_NF_TABLES_BRIDGE_H
int nft_bridge_iphdr_validate(struct sk_buff *skb);
int nft_bridge_ip6hdr_validate(struct sk_buff *skb);
#endif /* _NET_NF_TABLES_BRIDGE_H */
...@@ -13,6 +13,54 @@ ...@@ -13,6 +13,54 @@
#include <linux/module.h> #include <linux/module.h>
#include <linux/netfilter_bridge.h> #include <linux/netfilter_bridge.h>
#include <net/netfilter/nf_tables.h> #include <net/netfilter/nf_tables.h>
#include <net/netfilter/nf_tables_bridge.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
int nft_bridge_iphdr_validate(struct sk_buff *skb)
{
struct iphdr *iph;
u32 len;
if (!pskb_may_pull(skb, sizeof(struct iphdr)))
return 0;
iph = ip_hdr(skb);
if (iph->ihl < 5 || iph->version != 4)
return 0;
len = ntohs(iph->tot_len);
if (skb->len < len)
return 0;
else if (len < (iph->ihl*4))
return 0;
if (!pskb_may_pull(skb, iph->ihl*4))
return 0;
return 1;
}
EXPORT_SYMBOL_GPL(nft_bridge_iphdr_validate);
int nft_bridge_ip6hdr_validate(struct sk_buff *skb)
{
struct ipv6hdr *hdr;
u32 pkt_len;
if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
return 0;
hdr = ipv6_hdr(skb);
if (hdr->version != 6)
return 0;
pkt_len = ntohs(hdr->payload_len);
if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
return 0;
return 1;
}
EXPORT_SYMBOL_GPL(nft_bridge_ip6hdr_validate);
static unsigned int static unsigned int
nft_do_chain_bridge(const struct nf_hook_ops *ops, nft_do_chain_bridge(const struct nf_hook_ops *ops,
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include <linux/netfilter/nf_tables.h> #include <linux/netfilter/nf_tables.h>
#include <net/netfilter/nf_tables.h> #include <net/netfilter/nf_tables.h>
#include <net/netfilter/nft_reject.h> #include <net/netfilter/nft_reject.h>
#include <net/netfilter/nf_tables_bridge.h>
#include <net/netfilter/ipv4/nf_reject.h> #include <net/netfilter/ipv4/nf_reject.h>
#include <net/netfilter/ipv6/nf_reject.h> #include <net/netfilter/ipv6/nf_reject.h>
#include <linux/ip.h> #include <linux/ip.h>
...@@ -35,30 +36,6 @@ static void nft_reject_br_push_etherhdr(struct sk_buff *oldskb, ...@@ -35,30 +36,6 @@ static void nft_reject_br_push_etherhdr(struct sk_buff *oldskb,
skb_pull(nskb, ETH_HLEN); skb_pull(nskb, ETH_HLEN);
} }
static int nft_reject_iphdr_validate(struct sk_buff *oldskb)
{
struct iphdr *iph;
u32 len;
if (!pskb_may_pull(oldskb, sizeof(struct iphdr)))
return 0;
iph = ip_hdr(oldskb);
if (iph->ihl < 5 || iph->version != 4)
return 0;
len = ntohs(iph->tot_len);
if (oldskb->len < len)
return 0;
else if (len < (iph->ihl*4))
return 0;
if (!pskb_may_pull(oldskb, iph->ihl*4))
return 0;
return 1;
}
static void nft_reject_br_send_v4_tcp_reset(struct sk_buff *oldskb, int hook) static void nft_reject_br_send_v4_tcp_reset(struct sk_buff *oldskb, int hook)
{ {
struct sk_buff *nskb; struct sk_buff *nskb;
...@@ -66,7 +43,7 @@ static void nft_reject_br_send_v4_tcp_reset(struct sk_buff *oldskb, int hook) ...@@ -66,7 +43,7 @@ static void nft_reject_br_send_v4_tcp_reset(struct sk_buff *oldskb, int hook)
const struct tcphdr *oth; const struct tcphdr *oth;
struct tcphdr _oth; struct tcphdr _oth;
if (!nft_reject_iphdr_validate(oldskb)) if (!nft_bridge_iphdr_validate(oldskb))
return; return;
oth = nf_reject_ip_tcphdr_get(oldskb, &_oth, hook); oth = nf_reject_ip_tcphdr_get(oldskb, &_oth, hook);
...@@ -101,7 +78,7 @@ static void nft_reject_br_send_v4_unreach(struct sk_buff *oldskb, int hook, ...@@ -101,7 +78,7 @@ static void nft_reject_br_send_v4_unreach(struct sk_buff *oldskb, int hook,
void *payload; void *payload;
__wsum csum; __wsum csum;
if (!nft_reject_iphdr_validate(oldskb)) if (!nft_bridge_iphdr_validate(oldskb))
return; return;
/* IP header checks: fragment. */ /* IP header checks: fragment. */
...@@ -146,25 +123,6 @@ static void nft_reject_br_send_v4_unreach(struct sk_buff *oldskb, int hook, ...@@ -146,25 +123,6 @@ static void nft_reject_br_send_v4_unreach(struct sk_buff *oldskb, int hook,
br_deliver(br_port_get_rcu(oldskb->dev), nskb); br_deliver(br_port_get_rcu(oldskb->dev), nskb);
} }
static int nft_reject_ip6hdr_validate(struct sk_buff *oldskb)
{
struct ipv6hdr *hdr;
u32 pkt_len;
if (!pskb_may_pull(oldskb, sizeof(struct ipv6hdr)))
return 0;
hdr = ipv6_hdr(oldskb);
if (hdr->version != 6)
return 0;
pkt_len = ntohs(hdr->payload_len);
if (pkt_len + sizeof(struct ipv6hdr) > oldskb->len)
return 0;
return 1;
}
static void nft_reject_br_send_v6_tcp_reset(struct net *net, static void nft_reject_br_send_v6_tcp_reset(struct net *net,
struct sk_buff *oldskb, int hook) struct sk_buff *oldskb, int hook)
{ {
...@@ -174,7 +132,7 @@ static void nft_reject_br_send_v6_tcp_reset(struct net *net, ...@@ -174,7 +132,7 @@ static void nft_reject_br_send_v6_tcp_reset(struct net *net,
unsigned int otcplen; unsigned int otcplen;
struct ipv6hdr *nip6h; struct ipv6hdr *nip6h;
if (!nft_reject_ip6hdr_validate(oldskb)) if (!nft_bridge_ip6hdr_validate(oldskb))
return; return;
oth = nf_reject_ip6_tcphdr_get(oldskb, &_oth, &otcplen, hook); oth = nf_reject_ip6_tcphdr_get(oldskb, &_oth, &otcplen, hook);
...@@ -207,7 +165,7 @@ static void nft_reject_br_send_v6_unreach(struct net *net, ...@@ -207,7 +165,7 @@ static void nft_reject_br_send_v6_unreach(struct net *net,
unsigned int len; unsigned int len;
void *payload; void *payload;
if (!nft_reject_ip6hdr_validate(oldskb)) if (!nft_bridge_ip6hdr_validate(oldskb))
return; return;
/* Include "As much of invoking packet as possible without the ICMPv6 /* Include "As much of invoking packet as possible without the ICMPv6
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment