blob: b42c7ce799b38199a43785576266ee2218a25ccc [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 Viro47c183f2006-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
28ebt_ip_mt(const struct sk_buff *skb, const struct net_device *in,
29 const struct net_device *out, const struct xt_match *match,
30 const void *data, int offset, unsigned int protoff, bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080032 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
39 if (ih == NULL)
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020040 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 if (info->bitmask & EBT_IP_TOS &&
42 FWINV(info->tos != ih->tos, EBT_IP_TOS))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020043 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 if (info->bitmask & EBT_IP_SOURCE &&
45 FWINV((ih->saddr & info->smsk) !=
46 info->saddr, EBT_IP_SOURCE))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020047 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 if ((info->bitmask & EBT_IP_DEST) &&
49 FWINV((ih->daddr & info->dmsk) !=
50 info->daddr, EBT_IP_DEST))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020051 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (info->bitmask & EBT_IP_PROTO) {
53 if (FWINV(info->protocol != ih->protocol, EBT_IP_PROTO))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020054 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (!(info->bitmask & EBT_IP_DPORT) &&
56 !(info->bitmask & EBT_IP_SPORT))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020057 return true;
Bart De Schuymer8a4c8a92006-01-10 13:12:22 -080058 if (ntohs(ih->frag_off) & IP_OFFSET)
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020059 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 pptr = skb_header_pointer(skb, ih->ihl*4,
61 sizeof(_ports), &_ports);
62 if (pptr == NULL)
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020063 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (info->bitmask & EBT_IP_DPORT) {
65 u32 dst = ntohs(pptr->dst);
66 if (FWINV(dst < info->dport[0] ||
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090067 dst > info->dport[1],
68 EBT_IP_DPORT))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020069 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71 if (info->bitmask & EBT_IP_SPORT) {
72 u32 src = ntohs(pptr->src);
73 if (FWINV(src < info->sport[0] ||
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090074 src > info->sport[1],
75 EBT_IP_SPORT))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020076 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
78 }
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020079 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +020082static bool
83ebt_ip_mt_check(const char *table, const void *entry,
84 const struct xt_match *match, void *data,
85 unsigned int hook_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080087 const struct ebt_ip_info *info = data;
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +020088 const struct ebt_entry *e = entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (e->ethproto != htons(ETH_P_IP) ||
91 e->invflags & EBT_IPROTO)
Jan Engelhardt19eda872008-10-08 11:35:13 +020092 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (info->bitmask & ~EBT_IP_MASK || info->invflags & ~EBT_IP_MASK)
Jan Engelhardt19eda872008-10-08 11:35:13 +020094 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) {
96 if (info->invflags & EBT_IP_PROTO)
Jan Engelhardt19eda872008-10-08 11:35:13 +020097 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (info->protocol != IPPROTO_TCP &&
Patrick McHardyab67a4d2006-01-17 13:01:31 -080099 info->protocol != IPPROTO_UDP &&
Patrick McHardya8d0f952007-02-07 15:07:43 -0800100 info->protocol != IPPROTO_UDPLITE &&
Patrick McHardyab67a4d2006-01-17 13:01:31 -0800101 info->protocol != IPPROTO_SCTP &&
102 info->protocol != IPPROTO_DCCP)
Jan Engelhardt19eda872008-10-08 11:35:13 +0200103 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105 if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1])
Jan Engelhardt19eda872008-10-08 11:35:13 +0200106 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1])
Jan Engelhardt19eda872008-10-08 11:35:13 +0200108 return false;
109 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Jan Engelhardt043ef462008-10-08 11:35:15 +0200112static struct xt_match ebt_ip_mt_reg __read_mostly = {
113 .name = "ip",
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200114 .revision = 0,
115 .family = NFPROTO_BRIDGE,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200116 .match = ebt_ip_mt,
117 .checkentry = ebt_ip_mt_check,
Jan Engelhardt18219d32008-10-08 11:35:13 +0200118 .matchsize = XT_ALIGN(sizeof(struct ebt_ip_info)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .me = THIS_MODULE,
120};
121
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800122static int __init ebt_ip_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200124 return xt_register_match(&ebt_ip_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800127static void __exit ebt_ip_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200129 xt_unregister_match(&ebt_ip_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800132module_init(ebt_ip_init);
133module_exit(ebt_ip_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -0800134MODULE_DESCRIPTION("Ebtables: IPv4 protocol packet match");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135MODULE_LICENSE("GPL");