]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/bridge/netfilter/ebt_ip.c
netfilter: ebt_among: obtain match size through different means
[net-next-2.6.git] / net / bridge / netfilter / ebt_ip.c
CommitLineData
1da177e4
LT
1/*
2 * ebt_ip
3 *
4 * Authors:
5 * Bart De Schuymer <bdschuym@pandora.be>
6 *
7 * April, 2002
8 *
9 * Changes:
10 * added ip-sport and ip-dport
11 * Innominate Security Technologies AG <mhopf@innominate.com>
12 * September, 2002
13 */
1da177e4 14#include <linux/ip.h>
8a4c8a96 15#include <net/ip.h>
1da177e4
LT
16#include <linux/in.h>
17#include <linux/module.h>
18219d3f
JE
18#include <linux/netfilter/x_tables.h>
19#include <linux/netfilter_bridge/ebtables.h>
20#include <linux/netfilter_bridge/ebt_ip.h>
1da177e4
LT
21
22struct tcpudphdr {
47c183fa
AV
23 __be16 src;
24 __be16 dst;
1da177e4
LT
25};
26
8cc784ee
JE
27static bool ebt_filter_ip(const struct sk_buff *skb,
28 const struct net_device *in,
1da177e4
LT
29 const struct net_device *out, const void *data,
30 unsigned int datalen)
31{
abfdf1c4
JE
32 const struct ebt_ip_info *info = data;
33 const struct iphdr *ih;
34 struct iphdr _iph;
35 const struct tcpudphdr *pptr;
36 struct tcpudphdr _ports;
1da177e4
LT
37
38 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
39 if (ih == NULL)
8cc784ee 40 return false;
1da177e4
LT
41 if (info->bitmask & EBT_IP_TOS &&
42 FWINV(info->tos != ih->tos, EBT_IP_TOS))
8cc784ee 43 return false;
1da177e4
LT
44 if (info->bitmask & EBT_IP_SOURCE &&
45 FWINV((ih->saddr & info->smsk) !=
46 info->saddr, EBT_IP_SOURCE))
8cc784ee 47 return false;
1da177e4
LT
48 if ((info->bitmask & EBT_IP_DEST) &&
49 FWINV((ih->daddr & info->dmsk) !=
50 info->daddr, EBT_IP_DEST))
8cc784ee 51 return false;
1da177e4
LT
52 if (info->bitmask & EBT_IP_PROTO) {
53 if (FWINV(info->protocol != ih->protocol, EBT_IP_PROTO))
8cc784ee 54 return false;
1da177e4
LT
55 if (!(info->bitmask & EBT_IP_DPORT) &&
56 !(info->bitmask & EBT_IP_SPORT))
8cc784ee 57 return true;
8a4c8a96 58 if (ntohs(ih->frag_off) & IP_OFFSET)
8cc784ee 59 return false;
1da177e4
LT
60 pptr = skb_header_pointer(skb, ih->ihl*4,
61 sizeof(_ports), &_ports);
62 if (pptr == NULL)
8cc784ee 63 return false;
1da177e4
LT
64 if (info->bitmask & EBT_IP_DPORT) {
65 u32 dst = ntohs(pptr->dst);
66 if (FWINV(dst < info->dport[0] ||
9d6f229f
YH
67 dst > info->dport[1],
68 EBT_IP_DPORT))
8cc784ee 69 return false;
1da177e4
LT
70 }
71 if (info->bitmask & EBT_IP_SPORT) {
72 u32 src = ntohs(pptr->src);
73 if (FWINV(src < info->sport[0] ||
9d6f229f
YH
74 src > info->sport[1],
75 EBT_IP_SPORT))
8cc784ee 76 return false;
1da177e4
LT
77 }
78 }
8cc784ee 79 return true;
1da177e4
LT
80}
81
19eda879 82static bool ebt_ip_check(const char *tablename, unsigned int hookmask,
1da177e4
LT
83 const struct ebt_entry *e, void *data, unsigned int datalen)
84{
abfdf1c4 85 const struct ebt_ip_info *info = data;
1da177e4 86
1da177e4
LT
87 if (e->ethproto != htons(ETH_P_IP) ||
88 e->invflags & EBT_IPROTO)
19eda879 89 return false;
1da177e4 90 if (info->bitmask & ~EBT_IP_MASK || info->invflags & ~EBT_IP_MASK)
19eda879 91 return false;
1da177e4
LT
92 if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) {
93 if (info->invflags & EBT_IP_PROTO)
19eda879 94 return false;
1da177e4 95 if (info->protocol != IPPROTO_TCP &&
ab67a4d5 96 info->protocol != IPPROTO_UDP &&
a8d0f952 97 info->protocol != IPPROTO_UDPLITE &&
ab67a4d5
PM
98 info->protocol != IPPROTO_SCTP &&
99 info->protocol != IPPROTO_DCCP)
19eda879 100 return false;
1da177e4
LT
101 }
102 if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1])
19eda879 103 return false;
1da177e4 104 if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1])
19eda879
JE
105 return false;
106 return true;
1da177e4
LT
107}
108
30083c95 109static struct ebt_match filter_ip __read_mostly = {
1da177e4 110 .name = EBT_IP_MATCH,
001a18d3
JE
111 .revision = 0,
112 .family = NFPROTO_BRIDGE,
1da177e4
LT
113 .match = ebt_filter_ip,
114 .check = ebt_ip_check,
18219d3f 115 .matchsize = XT_ALIGN(sizeof(struct ebt_ip_info)),
1da177e4
LT
116 .me = THIS_MODULE,
117};
118
65b4b4e8 119static int __init ebt_ip_init(void)
1da177e4
LT
120{
121 return ebt_register_match(&filter_ip);
122}
123
65b4b4e8 124static void __exit ebt_ip_fini(void)
1da177e4
LT
125{
126 ebt_unregister_match(&filter_ip);
127}
128
65b4b4e8
AM
129module_init(ebt_ip_init);
130module_exit(ebt_ip_fini);
f776c4cd 131MODULE_DESCRIPTION("Ebtables: IPv4 protocol packet match");
1da177e4 132MODULE_LICENSE("GPL");