summaryrefslogtreecommitdiffstats
path: root/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'kernel')
-rw-r--r--kernel/linux/include/linux/netfilter_bridge/ebt_among.h27
-rw-r--r--kernel/linux/net/bridge/netfilter/ebt_among.c152
2 files changed, 152 insertions, 27 deletions
diff --git a/kernel/linux/include/linux/netfilter_bridge/ebt_among.h b/kernel/linux/include/linux/netfilter_bridge/ebt_among.h
index f98f5fb..e02581a 100644
--- a/kernel/linux/include/linux/netfilter_bridge/ebt_among.h
+++ b/kernel/linux/include/linux/netfilter_bridge/ebt_among.h
@@ -4,9 +4,11 @@
#define EBT_AMONG_DST 0x01
#define EBT_AMONG_SRC 0x02
-/* Write-once-read-many hash table, used for checking if a given
- * MAC address belongs to a set or not. It remembers up to 256
- * addresses.
+/* Grzegorz Borowiak <grzes@gnu.univ.gda.pl> 2003
+ *
+ * Write-once-read-many hash table, used for checking if a given
+ * MAC address belongs to a set or not and possibly for checking
+ * if it is related with a given IPv4 address.
*
* The hash value of an address is its last byte.
*
@@ -53,20 +55,31 @@ struct ebt_mac_wormhash_tuple
{
int next_ofs;
uint32_t cmp[2];
+ uint32_t ip;
};
struct ebt_mac_wormhash
{
int table[256];
- struct ebt_mac_wormhash_tuple pool[256];
+ int poolsize;
+ struct ebt_mac_wormhash_tuple pool[0];
};
+#define ebt_mac_wormhash_size(x) ((x) ? sizeof(struct ebt_mac_wormhash) + (x)->poolsize * sizeof(struct ebt_mac_wormhash_tuple) : 0)
+
struct ebt_among_info
{
- uint32_t bitmask;
- struct ebt_mac_wormhash wh_dst;
- struct ebt_mac_wormhash wh_src;
+ int wh_dst_ofs;
+ int wh_src_ofs;
+ int bitmask;
};
+
+#define EBT_AMONG_DST_NEG 0x1
+#define EBT_AMONG_SRC_NEG 0x2
+
+#define ebt_among_wh_dst(x) ((x)->wh_dst_ofs ? (struct ebt_mac_wormhash*)((char*)(x) + (x)->wh_dst_ofs) : NULL)
+#define ebt_among_wh_src(x) ((x)->wh_src_ofs ? (struct ebt_mac_wormhash*)((char*)(x) + (x)->wh_src_ofs) : NULL)
+
#define EBT_AMONG_MATCH "among"
#endif
diff --git a/kernel/linux/net/bridge/netfilter/ebt_among.c b/kernel/linux/net/bridge/netfilter/ebt_among.c
index e69246a..b4f2aed 100644
--- a/kernel/linux/net/bridge/netfilter/ebt_among.c
+++ b/kernel/linux/net/bridge/netfilter/ebt_among.c
@@ -10,9 +10,13 @@
#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, const char *mac)
+#define DEBUG
+
+static int ebt_mac_wormhash_contains(const struct ebt_mac_wormhash *wh, const char *mac, uint32_t ip)
{
/* You may be puzzled as to how this code works.
* Some tricks were used, refer to include/linux/netfilter_bridge/ebt_among.h
@@ -25,11 +29,25 @@ static int ebt_mac_wormhash_contains(const struct ebt_mac_wormhash *wh, const ch
int key = (const unsigned char)mac[5];
memcpy(((char*)cmp)+2, mac, 6);
offset = wh->table[key];
- while (offset) {
- p = (const struct ebt_mac_wormhash_tuple*)(base + offset);
- if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0])
- return 1;
- offset = p->next_ofs;
+ if (ip) {
+ while (offset) {
+ p = (const struct ebt_mac_wormhash_tuple*)(base + offset);
+ if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0]) {
+ if (p->ip == 0 || p->ip == ip) {
+ return 1;
+ }
+ }
+ offset = p->next_ofs;
+ }
+ }
+ else {
+ while (offset) {
+ p = (const struct ebt_mac_wormhash_tuple*)(base + offset);
+ if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0]) {
+ return 1;
+ }
+ offset = p->next_ofs;
+ }
}
return 0;
}
@@ -46,36 +64,124 @@ static int ebt_mac_wormhash_check_integrity(const struct ebt_mac_wormhash *wh)
offset = wh->table[i];
while (offset) {
p = (const struct ebt_mac_wormhash_tuple*)(base + offset);
- if (p < wh->pool)
+ if (p < wh->pool) {
+ printk(KERN_WARNING "ebtables: among: integrity: offset too low; 0x%08x\n", offset);
return -1;
- if (p > wh->pool + 256 - 1)
+ }
+ if (p > wh->pool + wh->poolsize - 1) {
+ printk(KERN_WARNING "ebtables: among: integrity: offset too high; 0x%08x\n", offset);
return -2;
+ }
count++;
- if (count > 1000)
+ if (count > 1000) {
+ printk(KERN_WARNING "ebtables: among: integrity: loop at %d\n", i);
return -3;
+ }
offset = p->next_ofs;
}
}
return 0;
}
+static int get_ip_dst(const struct sk_buff *skb, uint32_t *addr)
+{
+ if (skb->mac.ethernet->h_proto == __constant_htons(ETH_P_IP)) {
+ *addr = skb->nh.iph->daddr;
+ return 1;
+ }
+ if (skb->mac.ethernet->h_proto == __constant_htons(ETH_P_ARP)) {
+
+ uint32_t arp_len = sizeof(struct arphdr) +
+ (2 * (((*skb).nh.arph)->ar_hln)) +
+ (2 * (((*skb).nh.arph)->ar_pln));
+
+ // Make sure the packet is long enough.
+ if ((((*skb).nh.raw) + arp_len) > (*skb).tail)
+ return 0;
+ // IPv4 addresses are always 4 bytes.
+ if (((*skb).nh.arph)->ar_pln != sizeof(uint32_t))
+ return 0;
+
+ memcpy(addr, ((*skb).nh.raw) + sizeof(struct arphdr) +
+ (2*(((*skb).nh.arph)->ar_hln)) +
+ (((*skb).nh.arph)->ar_pln), sizeof(uint32_t));
+
+ return 2;
+ }
+ return 0;
+}
+
+static int get_ip_src(const struct sk_buff *skb, uint32_t *addr)
+{
+ if (skb->mac.ethernet->h_proto == __constant_htons(ETH_P_IP)) {
+ *addr = skb->nh.iph->saddr;
+ return 1;
+ }
+ if (skb->mac.ethernet->h_proto == __constant_htons(ETH_P_ARP)) {
+
+ uint32_t arp_len = sizeof(struct arphdr) +
+ (2 * (((*skb).nh.arph)->ar_hln)) +
+ (2 * (((*skb).nh.arph)->ar_pln));
+
+ // Make sure the packet is long enough.
+ if ((((*skb).nh.raw) + arp_len) > (*skb).tail)
+ return 0;
+ // IPv4 addresses are always 4 bytes.
+ if (((*skb).nh.arph)->ar_pln != sizeof(uint32_t))
+ return 0;
+
+ memcpy(addr, ((*skb).nh.raw) + sizeof(struct arphdr) +
+ ((((*skb).nh.arph)->ar_hln)), sizeof(uint32_t));
+
+ return 2;
+ }
+ 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)
{
struct ebt_among_info *info = (struct ebt_among_info *) data;
-
const char *dmac, *smac;
- if (info->bitmask & EBT_AMONG_SRC) {
+ const struct ebt_mac_wormhash *wh_dst, *wh_src;
+ uint32_t dip=0, sip=0;
+
+ wh_dst = ebt_among_wh_dst(info);
+ wh_src = ebt_among_wh_src(info);
+
+ if (wh_src) {
smac = skb->mac.ethernet->h_source;
- if (!ebt_mac_wormhash_contains(&info->wh_src, smac))
- return EBT_NOMATCH;
+ get_ip_src(skb, &sip);
+ 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 (info->bitmask & EBT_AMONG_DST) {
+ if (wh_dst) {
dmac = skb->mac.ethernet->h_dest;
- if (!ebt_mac_wormhash_contains(&info->wh_dst, dmac))
- return EBT_NOMATCH;
+ get_ip_dst(skb, &dip);
+ 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;
@@ -85,16 +191,22 @@ static int ebt_among_check(const char *tablename, unsigned int hookmask,
const struct ebt_entry *e, void *data, unsigned int datalen)
{
struct ebt_among_info *info = (struct ebt_among_info *) data;
+ int expected_length = sizeof(struct ebt_among_info);
+ const struct ebt_mac_wormhash *wh_dst, *wh_src;
+ 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(sizeof(struct ebt_among_info))) {
- printk(KERN_WARNING "ebtables: among: wrong size\n");
+ if (datalen < EBT_ALIGN(expected_length)) {
+ printk(KERN_WARNING "ebtables: among: wrong size: %d against expected %d, rounded to %d\n", datalen, expected_length, EBT_ALIGN(expected_length));
return -EINVAL;
}
- if ((info->bitmask & EBT_AMONG_DST) && ebt_mac_wormhash_check_integrity(&info->wh_dst)) {
+ if (wh_dst && ebt_mac_wormhash_check_integrity(wh_dst)) {
printk(KERN_WARNING "ebtables: among: dst integrity fail\n");
return -EINVAL;
}
- if ((info->bitmask & EBT_AMONG_SRC) && ebt_mac_wormhash_check_integrity(&info->wh_src)) {
+ if (wh_src && ebt_mac_wormhash_check_integrity(wh_src)) {
printk(KERN_WARNING "ebtables: among: src integrity fail\n");
return -EINVAL;
}