]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/bridge/netfilter/ebt_ip.c
netfilter: xtables: deconstify struct xt_action_param for matches
[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
2d06d4a5 27static bool
62fc8051 28ebt_ip_mt(const struct sk_buff *skb, struct xt_action_param *par)
1da177e4 29{
f7108a20 30 const struct ebt_ip_info *info = par->matchinfo;
abfdf1c4
JE
31 const struct iphdr *ih;
32 struct iphdr _iph;
33 const struct tcpudphdr *pptr;
34 struct tcpudphdr _ports;
1da177e4
LT
35
36 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
37 if (ih == NULL)
8cc784ee 38 return false;
1da177e4
LT
39 if (info->bitmask & EBT_IP_TOS &&
40 FWINV(info->tos != ih->tos, EBT_IP_TOS))
8cc784ee 41 return false;
1da177e4
LT
42 if (info->bitmask & EBT_IP_SOURCE &&
43 FWINV((ih->saddr & info->smsk) !=
44 info->saddr, EBT_IP_SOURCE))
8cc784ee 45 return false;
1da177e4
LT
46 if ((info->bitmask & EBT_IP_DEST) &&
47 FWINV((ih->daddr & info->dmsk) !=
48 info->daddr, EBT_IP_DEST))
8cc784ee 49 return false;
1da177e4
LT
50 if (info->bitmask & EBT_IP_PROTO) {
51 if (FWINV(info->protocol != ih->protocol, EBT_IP_PROTO))
8cc784ee 52 return false;
1da177e4
LT
53 if (!(info->bitmask & EBT_IP_DPORT) &&
54 !(info->bitmask & EBT_IP_SPORT))
8cc784ee 55 return true;
8a4c8a96 56 if (ntohs(ih->frag_off) & IP_OFFSET)
8cc784ee 57 return false;
1da177e4
LT
58 pptr = skb_header_pointer(skb, ih->ihl*4,
59 sizeof(_ports), &_ports);
60 if (pptr == NULL)
8cc784ee 61 return false;
1da177e4
LT
62 if (info->bitmask & EBT_IP_DPORT) {
63 u32 dst = ntohs(pptr->dst);
64 if (FWINV(dst < info->dport[0] ||
9d6f229f
YH
65 dst > info->dport[1],
66 EBT_IP_DPORT))
8cc784ee 67 return false;
1da177e4
LT
68 }
69 if (info->bitmask & EBT_IP_SPORT) {
70 u32 src = ntohs(pptr->src);
71 if (FWINV(src < info->sport[0] ||
9d6f229f
YH
72 src > info->sport[1],
73 EBT_IP_SPORT))
8cc784ee 74 return false;
1da177e4
LT
75 }
76 }
8cc784ee 77 return true;
1da177e4
LT
78}
79
b0f38452 80static int ebt_ip_mt_check(const struct xt_mtchk_param *par)
1da177e4 81{
9b4fce7a
JE
82 const struct ebt_ip_info *info = par->matchinfo;
83 const struct ebt_entry *e = par->entryinfo;
1da177e4 84
1da177e4
LT
85 if (e->ethproto != htons(ETH_P_IP) ||
86 e->invflags & EBT_IPROTO)
bd414ee6 87 return -EINVAL;
1da177e4 88 if (info->bitmask & ~EBT_IP_MASK || info->invflags & ~EBT_IP_MASK)
bd414ee6 89 return -EINVAL;
1da177e4
LT
90 if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) {
91 if (info->invflags & EBT_IP_PROTO)
bd414ee6 92 return -EINVAL;
1da177e4 93 if (info->protocol != IPPROTO_TCP &&
ab67a4d5 94 info->protocol != IPPROTO_UDP &&
a8d0f952 95 info->protocol != IPPROTO_UDPLITE &&
ab67a4d5
PM
96 info->protocol != IPPROTO_SCTP &&
97 info->protocol != IPPROTO_DCCP)
bd414ee6 98 return -EINVAL;
1da177e4
LT
99 }
100 if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1])
bd414ee6 101 return -EINVAL;
1da177e4 102 if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1])
bd414ee6
JE
103 return -EINVAL;
104 return 0;
1da177e4
LT
105}
106
043ef46c
JE
107static struct xt_match ebt_ip_mt_reg __read_mostly = {
108 .name = "ip",
001a18d3
JE
109 .revision = 0,
110 .family = NFPROTO_BRIDGE,
2d06d4a5
JE
111 .match = ebt_ip_mt,
112 .checkentry = ebt_ip_mt_check,
fc0e3df4 113 .matchsize = sizeof(struct ebt_ip_info),
1da177e4
LT
114 .me = THIS_MODULE,
115};
116
65b4b4e8 117static int __init ebt_ip_init(void)
1da177e4 118{
043ef46c 119 return xt_register_match(&ebt_ip_mt_reg);
1da177e4
LT
120}
121
65b4b4e8 122static void __exit ebt_ip_fini(void)
1da177e4 123{
043ef46c 124 xt_unregister_match(&ebt_ip_mt_reg);
1da177e4
LT
125}
126
65b4b4e8
AM
127module_init(ebt_ip_init);
128module_exit(ebt_ip_fini);
f776c4cd 129MODULE_DESCRIPTION("Ebtables: IPv4 protocol packet match");
1da177e4 130MODULE_LICENSE("GPL");