ebt_ip.c 3.2 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
static bool
28
ebt_ip_mt(const struct sk_buff *skb, const struct xt_match_param *par)
L
Linus Torvalds 已提交
29
{
30
	const struct ebt_ip_info *info = par->matchinfo;
31 32 33 34
	const struct iphdr *ih;
	struct iphdr _iph;
	const struct tcpudphdr *pptr;
	struct tcpudphdr _ports;
L
Linus Torvalds 已提交
35 36 37

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

80
static int ebt_ip_mt_check(const struct xt_mtchk_param *par)
L
Linus Torvalds 已提交
81
{
82 83
	const struct ebt_ip_info *info = par->matchinfo;
	const struct ebt_entry *e = par->entryinfo;
L
Linus Torvalds 已提交
84 85 86

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

107 108
static struct xt_match ebt_ip_mt_reg __read_mostly = {
	.name		= "ip",
109 110
	.revision	= 0,
	.family		= NFPROTO_BRIDGE,
111 112
	.match		= ebt_ip_mt,
	.checkentry	= ebt_ip_mt_check,
113
	.matchsize	= 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
	return xt_register_match(&ebt_ip_mt_reg);
L
Linus Torvalds 已提交
120 121
}

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

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");