2019-05-19 12:08:20 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
2006-01-10 21:12:22 +00:00
|
|
|
#include <net/ip.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/in.h>
|
|
|
|
#include <linux/module.h>
|
2008-10-08 09:35:13 +00:00
|
|
|
#include <linux/netfilter/x_tables.h>
|
|
|
|
#include <linux/netfilter_bridge/ebtables.h>
|
|
|
|
#include <linux/netfilter_bridge/ebt_ip.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2018-03-04 08:28:53 +00:00
|
|
|
union pkthdr {
|
|
|
|
struct {
|
|
|
|
__be16 src;
|
|
|
|
__be16 dst;
|
|
|
|
} tcpudphdr;
|
|
|
|
struct {
|
|
|
|
u8 type;
|
|
|
|
u8 code;
|
|
|
|
} icmphdr;
|
2018-03-04 08:28:54 +00:00
|
|
|
struct {
|
|
|
|
u8 type;
|
|
|
|
} igmphdr;
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
2008-10-08 09:35:15 +00:00
|
|
|
static bool
|
2009-07-07 18:42:08 +00:00
|
|
|
ebt_ip_mt(const struct sk_buff *skb, struct xt_action_param *par)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-10-08 09:35:18 +00:00
|
|
|
const struct ebt_ip_info *info = par->matchinfo;
|
2008-01-31 11:59:24 +00:00
|
|
|
const struct iphdr *ih;
|
|
|
|
struct iphdr _iph;
|
2018-03-04 08:28:53 +00:00
|
|
|
const union pkthdr *pptr;
|
|
|
|
union pkthdr _pkthdr;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
|
|
|
|
if (ih == NULL)
|
2008-10-08 09:35:13 +00:00
|
|
|
return false;
|
2016-06-24 20:25:22 +00:00
|
|
|
if ((info->bitmask & EBT_IP_TOS) &&
|
|
|
|
NF_INVF(info, EBT_IP_TOS, info->tos != ih->tos))
|
2008-10-08 09:35:13 +00:00
|
|
|
return false;
|
2016-06-24 20:25:22 +00:00
|
|
|
if ((info->bitmask & EBT_IP_SOURCE) &&
|
|
|
|
NF_INVF(info, EBT_IP_SOURCE,
|
|
|
|
(ih->saddr & info->smsk) != info->saddr))
|
2008-10-08 09:35:13 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
if ((info->bitmask & EBT_IP_DEST) &&
|
2016-06-24 20:25:22 +00:00
|
|
|
NF_INVF(info, EBT_IP_DEST,
|
|
|
|
(ih->daddr & info->dmsk) != info->daddr))
|
2008-10-08 09:35:13 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (info->bitmask & EBT_IP_PROTO) {
|
2016-06-24 20:25:22 +00:00
|
|
|
if (NF_INVF(info, EBT_IP_PROTO, info->protocol != ih->protocol))
|
2008-10-08 09:35:13 +00:00
|
|
|
return false;
|
2018-03-04 08:28:53 +00:00
|
|
|
if (!(info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT |
|
2018-03-04 08:28:54 +00:00
|
|
|
EBT_IP_ICMP | EBT_IP_IGMP)))
|
2008-10-08 09:35:13 +00:00
|
|
|
return true;
|
2006-01-10 21:12:22 +00:00
|
|
|
if (ntohs(ih->frag_off) & IP_OFFSET)
|
2008-10-08 09:35:13 +00:00
|
|
|
return false;
|
2018-03-04 08:28:53 +00:00
|
|
|
|
2018-03-04 08:28:54 +00:00
|
|
|
/* min icmp/igmp headersize is 4, so sizeof(_pkthdr) is ok. */
|
2005-04-16 22:20:36 +00:00
|
|
|
pptr = skb_header_pointer(skb, ih->ihl*4,
|
2018-03-04 08:28:53 +00:00
|
|
|
sizeof(_pkthdr), &_pkthdr);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (pptr == NULL)
|
2008-10-08 09:35:13 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (info->bitmask & EBT_IP_DPORT) {
|
2018-03-04 08:28:53 +00:00
|
|
|
u32 dst = ntohs(pptr->tcpudphdr.dst);
|
2016-06-24 20:25:22 +00:00
|
|
|
if (NF_INVF(info, EBT_IP_DPORT,
|
|
|
|
dst < info->dport[0] ||
|
|
|
|
dst > info->dport[1]))
|
2017-08-15 09:50:34 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
if (info->bitmask & EBT_IP_SPORT) {
|
2018-03-04 08:28:53 +00:00
|
|
|
u32 src = ntohs(pptr->tcpudphdr.src);
|
2016-06-24 20:25:22 +00:00
|
|
|
if (NF_INVF(info, EBT_IP_SPORT,
|
|
|
|
src < info->sport[0] ||
|
|
|
|
src > info->sport[1]))
|
2017-08-15 09:50:34 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2018-03-04 08:28:53 +00:00
|
|
|
if ((info->bitmask & EBT_IP_ICMP) &&
|
|
|
|
NF_INVF(info, EBT_IP_ICMP,
|
|
|
|
pptr->icmphdr.type < info->icmp_type[0] ||
|
|
|
|
pptr->icmphdr.type > info->icmp_type[1] ||
|
|
|
|
pptr->icmphdr.code < info->icmp_code[0] ||
|
|
|
|
pptr->icmphdr.code > info->icmp_code[1]))
|
|
|
|
return false;
|
2018-03-04 08:28:54 +00:00
|
|
|
if ((info->bitmask & EBT_IP_IGMP) &&
|
|
|
|
NF_INVF(info, EBT_IP_IGMP,
|
|
|
|
pptr->igmphdr.type < info->igmp_type[0] ||
|
|
|
|
pptr->igmphdr.type > info->igmp_type[1]))
|
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2008-10-08 09:35:13 +00:00
|
|
|
return true;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 16:16:42 +00:00
|
|
|
static int ebt_ip_mt_check(const struct xt_mtchk_param *par)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-10-08 09:35:18 +00:00
|
|
|
const struct ebt_ip_info *info = par->matchinfo;
|
|
|
|
const struct ebt_entry *e = par->entryinfo;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (e->ethproto != htons(ETH_P_IP) ||
|
|
|
|
e->invflags & EBT_IPROTO)
|
2010-03-23 15:35:56 +00:00
|
|
|
return -EINVAL;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (info->bitmask & ~EBT_IP_MASK || info->invflags & ~EBT_IP_MASK)
|
2010-03-23 15:35:56 +00:00
|
|
|
return -EINVAL;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) {
|
|
|
|
if (info->invflags & EBT_IP_PROTO)
|
2010-03-23 15:35:56 +00:00
|
|
|
return -EINVAL;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (info->protocol != IPPROTO_TCP &&
|
2006-01-17 21:01:31 +00:00
|
|
|
info->protocol != IPPROTO_UDP &&
|
2007-02-07 23:07:43 +00:00
|
|
|
info->protocol != IPPROTO_UDPLITE &&
|
2006-01-17 21:01:31 +00:00
|
|
|
info->protocol != IPPROTO_SCTP &&
|
|
|
|
info->protocol != IPPROTO_DCCP)
|
2010-03-23 15:35:56 +00:00
|
|
|
return -EINVAL;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1])
|
2010-03-23 15:35:56 +00:00
|
|
|
return -EINVAL;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1])
|
2010-03-23 15:35:56 +00:00
|
|
|
return -EINVAL;
|
2018-03-04 08:28:53 +00:00
|
|
|
if (info->bitmask & EBT_IP_ICMP) {
|
|
|
|
if ((info->invflags & EBT_IP_PROTO) ||
|
|
|
|
info->protocol != IPPROTO_ICMP)
|
|
|
|
return -EINVAL;
|
|
|
|
if (info->icmp_type[0] > info->icmp_type[1] ||
|
|
|
|
info->icmp_code[0] > info->icmp_code[1])
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-03-04 08:28:54 +00:00
|
|
|
if (info->bitmask & EBT_IP_IGMP) {
|
|
|
|
if ((info->invflags & EBT_IP_PROTO) ||
|
|
|
|
info->protocol != IPPROTO_IGMP)
|
|
|
|
return -EINVAL;
|
|
|
|
if (info->igmp_type[0] > info->igmp_type[1])
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2010-03-23 15:35:56 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-10-08 09:35:15 +00:00
|
|
|
static struct xt_match ebt_ip_mt_reg __read_mostly = {
|
|
|
|
.name = "ip",
|
2008-10-08 09:35:14 +00:00
|
|
|
.revision = 0,
|
|
|
|
.family = NFPROTO_BRIDGE,
|
2008-10-08 09:35:15 +00:00
|
|
|
.match = ebt_ip_mt,
|
|
|
|
.checkentry = ebt_ip_mt_check,
|
2010-02-15 17:16:26 +00:00
|
|
|
.matchsize = sizeof(struct ebt_ip_info),
|
2005-04-16 22:20:36 +00:00
|
|
|
.me = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2006-03-29 00:37:06 +00:00
|
|
|
static int __init ebt_ip_init(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-10-08 09:35:15 +00:00
|
|
|
return xt_register_match(&ebt_ip_mt_reg);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-03-29 00:37:06 +00:00
|
|
|
static void __exit ebt_ip_fini(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-10-08 09:35:15 +00:00
|
|
|
xt_unregister_match(&ebt_ip_mt_reg);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-03-29 00:37:06 +00:00
|
|
|
module_init(ebt_ip_init);
|
|
|
|
module_exit(ebt_ip_fini);
|
2008-01-31 12:00:30 +00:00
|
|
|
MODULE_DESCRIPTION("Ebtables: IPv4 protocol packet match");
|
2005-04-16 22:20:36 +00:00
|
|
|
MODULE_LICENSE("GPL");
|