2005-04-16 16:20:36 -06:00
|
|
|
/*
|
|
|
|
* ebt_among
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Grzegorz Borowiak <grzes@gnu.univ.gda.pl>
|
|
|
|
*
|
|
|
|
* August, 2003
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/netfilter_bridge/ebtables.h>
|
|
|
|
#include <linux/netfilter_bridge/ebt_among.h>
|
|
|
|
#include <linux/ip.h>
|
|
|
|
#include <linux/if_arp.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
|
|
|
static int ebt_mac_wormhash_contains(const struct ebt_mac_wormhash *wh,
|
2006-11-14 22:11:51 -07:00
|
|
|
const char *mac, __be32 ip)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
/* You may be puzzled as to how this code works.
|
2007-02-09 07:24:35 -07:00
|
|
|
* Some tricks were used, refer to
|
2005-04-16 16:20:36 -06:00
|
|
|
* include/linux/netfilter_bridge/ebt_among.h
|
|
|
|
* as there you can find a solution of this mystery.
|
|
|
|
*/
|
|
|
|
const struct ebt_mac_wormhash_tuple *p;
|
|
|
|
int start, limit, i;
|
|
|
|
uint32_t cmp[2] = { 0, 0 };
|
2008-01-31 04:59:24 -07:00
|
|
|
int key = ((const unsigned char *)mac)[5];
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
memcpy(((char *) cmp) + 2, mac, 6);
|
|
|
|
start = wh->table[key];
|
|
|
|
limit = wh->table[key + 1];
|
|
|
|
if (ip) {
|
|
|
|
for (i = start; i < limit; i++) {
|
|
|
|
p = &wh->pool[i];
|
|
|
|
if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0]) {
|
|
|
|
if (p->ip == 0 || p->ip == ip) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = start; i < limit; i++) {
|
|
|
|
p = &wh->pool[i];
|
|
|
|
if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0]) {
|
|
|
|
if (p->ip == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ebt_mac_wormhash_check_integrity(const struct ebt_mac_wormhash
|
|
|
|
*wh)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
if (wh->table[i] > wh->table[i + 1])
|
|
|
|
return -0x100 - i;
|
|
|
|
if (wh->table[i] < 0)
|
|
|
|
return -0x200 - i;
|
|
|
|
if (wh->table[i] > wh->poolsize)
|
|
|
|
return -0x300 - i;
|
|
|
|
}
|
|
|
|
if (wh->table[256] > wh->poolsize)
|
|
|
|
return -0xc00;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-11-14 22:11:51 -07:00
|
|
|
static int get_ip_dst(const struct sk_buff *skb, __be32 *addr)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)) {
|
2008-01-31 04:59:24 -07:00
|
|
|
const struct iphdr *ih;
|
|
|
|
struct iphdr _iph;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
|
|
|
|
if (ih == NULL)
|
|
|
|
return -1;
|
|
|
|
*addr = ih->daddr;
|
|
|
|
} else if (eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) {
|
2008-01-31 04:59:24 -07:00
|
|
|
const struct arphdr *ah;
|
|
|
|
struct arphdr _arph;
|
|
|
|
const __be32 *bp;
|
|
|
|
__be32 buf;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
|
|
|
|
if (ah == NULL ||
|
2006-11-14 22:11:51 -07:00
|
|
|
ah->ar_pln != sizeof(__be32) ||
|
2005-04-16 16:20:36 -06:00
|
|
|
ah->ar_hln != ETH_ALEN)
|
|
|
|
return -1;
|
|
|
|
bp = skb_header_pointer(skb, sizeof(struct arphdr) +
|
2006-11-14 22:11:51 -07:00
|
|
|
2 * ETH_ALEN + sizeof(__be32),
|
|
|
|
sizeof(__be32), &buf);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (bp == NULL)
|
|
|
|
return -1;
|
|
|
|
*addr = *bp;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-11-14 22:11:51 -07:00
|
|
|
static int get_ip_src(const struct sk_buff *skb, __be32 *addr)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)) {
|
2008-01-31 04:59:24 -07:00
|
|
|
const struct iphdr *ih;
|
|
|
|
struct iphdr _iph;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
|
|
|
|
if (ih == NULL)
|
|
|
|
return -1;
|
|
|
|
*addr = ih->saddr;
|
|
|
|
} else if (eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) {
|
2008-01-31 04:59:24 -07:00
|
|
|
const struct arphdr *ah;
|
|
|
|
struct arphdr _arph;
|
|
|
|
const __be32 *bp;
|
|
|
|
__be32 buf;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
|
|
|
|
if (ah == NULL ||
|
2006-11-14 22:11:51 -07:00
|
|
|
ah->ar_pln != sizeof(__be32) ||
|
2005-04-16 16:20:36 -06:00
|
|
|
ah->ar_hln != ETH_ALEN)
|
|
|
|
return -1;
|
|
|
|
bp = skb_header_pointer(skb, sizeof(struct arphdr) +
|
2006-11-14 22:11:51 -07:00
|
|
|
ETH_ALEN, sizeof(__be32), &buf);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (bp == NULL)
|
|
|
|
return -1;
|
|
|
|
*addr = *bp;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ebt_filter_among(const struct sk_buff *skb,
|
|
|
|
const struct net_device *in,
|
|
|
|
const struct net_device *out, const void *data,
|
|
|
|
unsigned int datalen)
|
|
|
|
{
|
2008-01-31 04:59:24 -07:00
|
|
|
const struct ebt_among_info *info = data;
|
2005-04-16 16:20:36 -06:00
|
|
|
const char *dmac, *smac;
|
|
|
|
const struct ebt_mac_wormhash *wh_dst, *wh_src;
|
2006-11-14 22:11:51 -07:00
|
|
|
__be32 dip = 0, sip = 0;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
wh_dst = ebt_among_wh_dst(info);
|
|
|
|
wh_src = ebt_among_wh_src(info);
|
|
|
|
|
|
|
|
if (wh_src) {
|
|
|
|
smac = eth_hdr(skb)->h_source;
|
|
|
|
if (get_ip_src(skb, &sip))
|
|
|
|
return EBT_NOMATCH;
|
|
|
|
if (!(info->bitmask & EBT_AMONG_SRC_NEG)) {
|
|
|
|
/* we match only if it contains */
|
|
|
|
if (!ebt_mac_wormhash_contains(wh_src, smac, sip))
|
|
|
|
return EBT_NOMATCH;
|
|
|
|
} else {
|
|
|
|
/* we match only if it DOES NOT contain */
|
|
|
|
if (ebt_mac_wormhash_contains(wh_src, smac, sip))
|
|
|
|
return EBT_NOMATCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wh_dst) {
|
|
|
|
dmac = eth_hdr(skb)->h_dest;
|
|
|
|
if (get_ip_dst(skb, &dip))
|
|
|
|
return EBT_NOMATCH;
|
|
|
|
if (!(info->bitmask & EBT_AMONG_DST_NEG)) {
|
|
|
|
/* we match only if it contains */
|
|
|
|
if (!ebt_mac_wormhash_contains(wh_dst, dmac, dip))
|
|
|
|
return EBT_NOMATCH;
|
|
|
|
} else {
|
|
|
|
/* we match only if it DOES NOT contain */
|
|
|
|
if (ebt_mac_wormhash_contains(wh_dst, dmac, dip))
|
|
|
|
return EBT_NOMATCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EBT_MATCH;
|
|
|
|
}
|
|
|
|
|
2008-10-08 03:35:13 -06:00
|
|
|
static bool
|
|
|
|
ebt_among_check(const char *tablename, unsigned int hookmask,
|
|
|
|
const struct ebt_entry *e, void *data,
|
|
|
|
unsigned int datalen)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-01-31 04:59:24 -07:00
|
|
|
const struct ebt_among_info *info = data;
|
2005-04-16 16:20:36 -06:00
|
|
|
int expected_length = sizeof(struct ebt_among_info);
|
|
|
|
const struct ebt_mac_wormhash *wh_dst, *wh_src;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
wh_dst = ebt_among_wh_dst(info);
|
|
|
|
wh_src = ebt_among_wh_src(info);
|
|
|
|
expected_length += ebt_mac_wormhash_size(wh_dst);
|
|
|
|
expected_length += ebt_mac_wormhash_size(wh_src);
|
|
|
|
|
|
|
|
if (datalen != EBT_ALIGN(expected_length)) {
|
|
|
|
printk(KERN_WARNING
|
2007-11-20 00:46:55 -07:00
|
|
|
"ebtables: among: wrong size: %d "
|
2005-04-16 16:20:36 -06:00
|
|
|
"against expected %d, rounded to %Zd\n",
|
|
|
|
datalen, expected_length,
|
|
|
|
EBT_ALIGN(expected_length));
|
2008-10-08 03:35:13 -06:00
|
|
|
return false;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
if (wh_dst && (err = ebt_mac_wormhash_check_integrity(wh_dst))) {
|
|
|
|
printk(KERN_WARNING
|
|
|
|
"ebtables: among: dst integrity fail: %x\n", -err);
|
2008-10-08 03:35:13 -06:00
|
|
|
return false;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
if (wh_src && (err = ebt_mac_wormhash_check_integrity(wh_src))) {
|
|
|
|
printk(KERN_WARNING
|
|
|
|
"ebtables: among: src integrity fail: %x\n", -err);
|
2008-10-08 03:35:13 -06:00
|
|
|
return false;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2008-10-08 03:35:13 -06:00
|
|
|
return true;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2008-01-31 05:00:59 -07:00
|
|
|
static struct ebt_match filter_among __read_mostly = {
|
2007-02-09 07:24:35 -07:00
|
|
|
.name = EBT_AMONG_MATCH,
|
|
|
|
.match = ebt_filter_among,
|
2005-04-16 16:20:36 -06:00
|
|
|
.check = ebt_among_check,
|
2008-10-08 03:35:13 -06:00
|
|
|
.matchsize = -1, /* special case */
|
2005-04-16 16:20:36 -06:00
|
|
|
.me = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2006-03-28 17:37:06 -07:00
|
|
|
static int __init ebt_among_init(void)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
return ebt_register_match(&filter_among);
|
|
|
|
}
|
|
|
|
|
2006-03-28 17:37:06 -07:00
|
|
|
static void __exit ebt_among_fini(void)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
ebt_unregister_match(&filter_among);
|
|
|
|
}
|
|
|
|
|
2006-03-28 17:37:06 -07:00
|
|
|
module_init(ebt_among_init);
|
|
|
|
module_exit(ebt_among_fini);
|
2008-01-31 05:00:30 -07:00
|
|
|
MODULE_DESCRIPTION("Ebtables: Combined MAC/IP address list matching");
|
2005-04-16 16:20:36 -06:00
|
|
|
MODULE_LICENSE("GPL");
|