blob: 23bca62d58d290f5085094b3b5d8c74b21a7c8fc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/ip.h>
Bart De Schuymer8a4c8a92006-01-10 13:12:22 -080015#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/in.h>
17#include <linux/module.h>
Jan Engelhardt18219d32008-10-08 11:35:13 +020018#include <linux/netfilter/x_tables.h>
19#include <linux/netfilter_bridge/ebtables.h>
20#include <linux/netfilter_bridge/ebt_ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22struct tcpudphdr {
Al Viro47c183fa2006-11-14 21:11:51 -080023 __be16 src;
24 __be16 dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025};
26
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +020027static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020028ebt_ip_mt(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020030 const struct ebt_ip_info *info = par->matchinfo;
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080031 const struct iphdr *ih;
32 struct iphdr _iph;
33 const struct tcpudphdr *pptr;
34 struct tcpudphdr _ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
37 if (ih == NULL)
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020038 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 if (info->bitmask & EBT_IP_TOS &&
40 FWINV(info->tos != ih->tos, EBT_IP_TOS))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020041 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 if (info->bitmask & EBT_IP_SOURCE &&
43 FWINV((ih->saddr & info->smsk) !=
44 info->saddr, EBT_IP_SOURCE))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020045 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 if ((info->bitmask & EBT_IP_DEST) &&
47 FWINV((ih->daddr & info->dmsk) !=
48 info->daddr, EBT_IP_DEST))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020049 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 if (info->bitmask & EBT_IP_PROTO) {
51 if (FWINV(info->protocol != ih->protocol, EBT_IP_PROTO))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020052 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (!(info->bitmask & EBT_IP_DPORT) &&
54 !(info->bitmask & EBT_IP_SPORT))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020055 return true;
Bart De Schuymer8a4c8a92006-01-10 13:12:22 -080056 if (ntohs(ih->frag_off) & IP_OFFSET)
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020057 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 pptr = skb_header_pointer(skb, ih->ihl*4,
59 sizeof(_ports), &_ports);
60 if (pptr == NULL)
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020061 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (info->bitmask & EBT_IP_DPORT) {
63 u32 dst = ntohs(pptr->dst);
64 if (FWINV(dst < info->dport[0] ||
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090065 dst > info->dport[1],
66 EBT_IP_DPORT))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020067 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
69 if (info->bitmask & EBT_IP_SPORT) {
70 u32 src = ntohs(pptr->src);
71 if (FWINV(src < info->sport[0] ||
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090072 src > info->sport[1],
73 EBT_IP_SPORT))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020074 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76 }
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020077 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Jan Engelhardtb0f38452010-03-19 17:16:42 +010080static int ebt_ip_mt_check(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020082 const struct ebt_ip_info *info = par->matchinfo;
83 const struct ebt_entry *e = par->entryinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (e->ethproto != htons(ETH_P_IP) ||
86 e->invflags & EBT_IPROTO)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010087 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (info->bitmask & ~EBT_IP_MASK || info->invflags & ~EBT_IP_MASK)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010089 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) {
91 if (info->invflags & EBT_IP_PROTO)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010092 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (info->protocol != IPPROTO_TCP &&
Patrick McHardyab67a4d2006-01-17 13:01:31 -080094 info->protocol != IPPROTO_UDP &&
Patrick McHardya8d0f952007-02-07 15:07:43 -080095 info->protocol != IPPROTO_UDPLITE &&
Patrick McHardyab67a4d2006-01-17 13:01:31 -080096 info->protocol != IPPROTO_SCTP &&
97 info->protocol != IPPROTO_DCCP)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010098 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100 if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1])
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100101 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1])
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100103 return -EINVAL;
104 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Jan Engelhardt043ef462008-10-08 11:35:15 +0200107static struct xt_match ebt_ip_mt_reg __read_mostly = {
108 .name = "ip",
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200109 .revision = 0,
110 .family = NFPROTO_BRIDGE,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200111 .match = ebt_ip_mt,
112 .checkentry = ebt_ip_mt_check,
Florian Westphalfc0e3df2010-02-15 18:16:26 +0100113 .matchsize = sizeof(struct ebt_ip_info),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .me = THIS_MODULE,
115};
116
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800117static int __init ebt_ip_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200119 return xt_register_match(&ebt_ip_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800122static void __exit ebt_ip_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200124 xt_unregister_match(&ebt_ip_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800127module_init(ebt_ip_init);
128module_exit(ebt_ip_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -0800129MODULE_DESCRIPTION("Ebtables: IPv4 protocol packet match");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130MODULE_LICENSE("GPL");