ebt_ip.c 3.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  ebt_ip
 *
 *	Authors:
 *	Bart De Schuymer <bdschuym@pandora.be>
 *
 *  April, 2002
 *
 *  Changes:
 *    added ip-sport and ip-dport
 *    Innominate Security Technologies AG <mhopf@innominate.com>
 *    September, 2002
 */
#include <linux/ip.h>
15
#include <net/ip.h>
L
Linus Torvalds 已提交
16 17
#include <linux/in.h>
#include <linux/module.h>
18 19 20
#include <linux/netfilter/x_tables.h>
#include <linux/netfilter_bridge/ebtables.h>
#include <linux/netfilter_bridge/ebt_ip.h>
L
Linus Torvalds 已提交
21 22

struct tcpudphdr {
A
Al Viro 已提交
23 24
	__be16 src;
	__be16 dst;
L
Linus Torvalds 已提交
25 26
};

27 28
static bool ebt_filter_ip(const struct sk_buff *skb,
   const struct net_device *in,
L
Linus Torvalds 已提交
29 30 31
   const struct net_device *out, const void *data,
   unsigned int datalen)
{
32 33 34 35 36
	const struct ebt_ip_info *info = data;
	const struct iphdr *ih;
	struct iphdr _iph;
	const struct tcpudphdr *pptr;
	struct tcpudphdr _ports;
L
Linus Torvalds 已提交
37 38 39

	ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
	if (ih == NULL)
40
		return false;
L
Linus Torvalds 已提交
41 42
	if (info->bitmask & EBT_IP_TOS &&
	   FWINV(info->tos != ih->tos, EBT_IP_TOS))
43
		return false;
L
Linus Torvalds 已提交
44 45 46
	if (info->bitmask & EBT_IP_SOURCE &&
	   FWINV((ih->saddr & info->smsk) !=
	   info->saddr, EBT_IP_SOURCE))
47
		return false;
L
Linus Torvalds 已提交
48 49 50
	if ((info->bitmask & EBT_IP_DEST) &&
	   FWINV((ih->daddr & info->dmsk) !=
	   info->daddr, EBT_IP_DEST))
51
		return false;
L
Linus Torvalds 已提交
52 53
	if (info->bitmask & EBT_IP_PROTO) {
		if (FWINV(info->protocol != ih->protocol, EBT_IP_PROTO))
54
			return false;
L
Linus Torvalds 已提交
55 56
		if (!(info->bitmask & EBT_IP_DPORT) &&
		    !(info->bitmask & EBT_IP_SPORT))
57
			return true;
58
		if (ntohs(ih->frag_off) & IP_OFFSET)
59
			return false;
L
Linus Torvalds 已提交
60 61 62
		pptr = skb_header_pointer(skb, ih->ihl*4,
					  sizeof(_ports), &_ports);
		if (pptr == NULL)
63
			return false;
L
Linus Torvalds 已提交
64 65 66
		if (info->bitmask & EBT_IP_DPORT) {
			u32 dst = ntohs(pptr->dst);
			if (FWINV(dst < info->dport[0] ||
67 68
				  dst > info->dport[1],
				  EBT_IP_DPORT))
69
			return false;
L
Linus Torvalds 已提交
70 71 72 73
		}
		if (info->bitmask & EBT_IP_SPORT) {
			u32 src = ntohs(pptr->src);
			if (FWINV(src < info->sport[0] ||
74 75
				  src > info->sport[1],
				  EBT_IP_SPORT))
76
			return false;
L
Linus Torvalds 已提交
77 78
		}
	}
79
	return true;
L
Linus Torvalds 已提交
80 81
}

82
static bool ebt_ip_check(const char *tablename, unsigned int hookmask,
L
Linus Torvalds 已提交
83 84
   const struct ebt_entry *e, void *data, unsigned int datalen)
{
85
	const struct ebt_ip_info *info = data;
L
Linus Torvalds 已提交
86 87 88

	if (e->ethproto != htons(ETH_P_IP) ||
	   e->invflags & EBT_IPROTO)
89
		return false;
L
Linus Torvalds 已提交
90
	if (info->bitmask & ~EBT_IP_MASK || info->invflags & ~EBT_IP_MASK)
91
		return false;
L
Linus Torvalds 已提交
92 93
	if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) {
		if (info->invflags & EBT_IP_PROTO)
94
			return false;
L
Linus Torvalds 已提交
95
		if (info->protocol != IPPROTO_TCP &&
96
		    info->protocol != IPPROTO_UDP &&
97
		    info->protocol != IPPROTO_UDPLITE &&
98 99
		    info->protocol != IPPROTO_SCTP &&
		    info->protocol != IPPROTO_DCCP)
100
			 return false;
L
Linus Torvalds 已提交
101 102
	}
	if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1])
103
		return false;
L
Linus Torvalds 已提交
104
	if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1])
105 106
		return false;
	return true;
L
Linus Torvalds 已提交
107 108
}

109
static struct ebt_match filter_ip __read_mostly = {
L
Linus Torvalds 已提交
110 111 112
	.name		= EBT_IP_MATCH,
	.match		= ebt_filter_ip,
	.check		= ebt_ip_check,
113
	.matchsize	= XT_ALIGN(sizeof(struct ebt_ip_info)),
L
Linus Torvalds 已提交
114 115 116
	.me		= THIS_MODULE,
};

117
static int __init ebt_ip_init(void)
L
Linus Torvalds 已提交
118 119 120 121
{
	return ebt_register_match(&filter_ip);
}

122
static void __exit ebt_ip_fini(void)
L
Linus Torvalds 已提交
123 124 125 126
{
	ebt_unregister_match(&filter_ip);
}

127 128
module_init(ebt_ip_init);
module_exit(ebt_ip_fini);
129
MODULE_DESCRIPTION("Ebtables: IPv4 protocol packet match");
L
Linus Torvalds 已提交
130
MODULE_LICENSE("GPL");