diff options
author | gborowiak <gborowiak> | 2003-09-16 19:26:38 +0000 |
---|---|---|
committer | gborowiak <gborowiak> | 2003-09-16 19:26:38 +0000 |
commit | aa55e1345bb84993273fabe4680a08b48fa99944 (patch) | |
tree | e0d05969dc0531294d14fb4bc70eeac019539aa4 | |
parent | 3f6509ec5f69b1483f96dbfd2a36153329334350 (diff) |
changed the structure of the hash table
reedited to 80col/8tab and K-R indent
0.0.0.0 IP address is now illegal
-rw-r--r-- | kernel/linux/include/linux/netfilter_bridge/ebt_among.h | 34 | ||||
-rw-r--r-- | kernel/linux/net/bridge/netfilter/ebt_among.c | 143 | ||||
-rw-r--r-- | kernel/linux2.5/net/bridge/netfilter/ebt_among.c | 212 | ||||
-rw-r--r-- | userspace/ebtables2/extensions/ebt_among.c | 254 |
4 files changed, 402 insertions, 241 deletions
diff --git a/kernel/linux/include/linux/netfilter_bridge/ebt_among.h b/kernel/linux/include/linux/netfilter_bridge/ebt_among.h index e02581a..307c1fe 100644 --- a/kernel/linux/include/linux/netfilter_bridge/ebt_among.h +++ b/kernel/linux/include/linux/netfilter_bridge/ebt_among.h @@ -27,45 +27,23 @@ * if they are the same we compare 2 first. * * Yes, it is a memory overhead, but in 2003 AD, who cares? - * - * `next_ofs' contains a "serialized" pointer to the next tuple in - * the synonym list. It is a difference between address of the next - * tuple and address of the entire wormhash structure, in bytes - * or 0 if there is no next tuple. - * - * `table' contains begins of the synonym lists for - * - * This was introduced to make wormhash structure movable. As you may - * guess, once structure is passed to the kernel, the real pointers - * would become invalid. Also comparison would not work if they were - * built of absolute pointers. - * - * From the other side, using indices of the `pool' array would be - * slower. CPU would have to multiply index * size of tuple at each - * access to a tuple and add this to the address of the beginning - * of the `pool' array. - * - * Summary: - * - * The code is damn unreadable and unclear, but - and that's the - * point - effective. */ struct ebt_mac_wormhash_tuple { - int next_ofs; uint32_t cmp[2]; uint32_t ip; }; struct ebt_mac_wormhash { - int table[256]; + int table[257]; 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) +#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 { @@ -77,8 +55,10 @@ struct ebt_among_info #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_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" diff --git a/kernel/linux/net/bridge/netfilter/ebt_among.c b/kernel/linux/net/bridge/netfilter/ebt_among.c index b4f2aed..e4b2268 100644 --- a/kernel/linux/net/bridge/netfilter/ebt_among.c +++ b/kernel/linux/net/bridge/netfilter/ebt_among.c @@ -14,76 +14,63 @@ #include <linux/if_arp.h> #include <linux/module.h> +/* #define DEBUG +*/ -static int ebt_mac_wormhash_contains(const struct ebt_mac_wormhash *wh, const char *mac, uint32_t ip) +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 + * Some tricks were used, refer to + * 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 offset; - const char *base = (const char*)wh; + int start, limit, i; uint32_t cmp[2] = { 0, 0 }; - int key = (const unsigned char)mac[5]; - memcpy(((char*)cmp)+2, mac, 6); - offset = wh->table[key]; + int key = (const unsigned char) mac[5]; + memcpy(((char *) cmp) + 2, mac, 6); + start = wh->table[key]; + limit = wh->table[key + 1]; if (ip) { - while (offset) { - p = (const struct ebt_mac_wormhash_tuple*)(base + offset); + 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; } } - offset = p->next_ofs; } - } - else { - while (offset) { - p = (const struct ebt_mac_wormhash_tuple*)(base + offset); + } else { + for (i = start; i < limit; i++) { + p = &wh->pool[i]; if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0]) { return 1; } - offset = p->next_ofs; } } return 0; } -static int ebt_mac_wormhash_check_integrity(const struct ebt_mac_wormhash *wh) +static int ebt_mac_wormhash_check_integrity(const struct ebt_mac_wormhash + *wh) { - int i, count; - const struct ebt_mac_wormhash_tuple *p; - int offset; - const char *base = (const char*)wh; - - count = 0; - for (i=256; i--;) { - offset = wh->table[i]; - while (offset) { - p = (const struct ebt_mac_wormhash_tuple*)(base + offset); - if (p < wh->pool) { - printk(KERN_WARNING "ebtables: among: integrity: offset too low; 0x%08x\n", offset); - return -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) { - printk(KERN_WARNING "ebtables: among: integrity: loop at %d\n", i); - return -3; - } - offset = p->next_ofs; - } + 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; } -static int get_ip_dst(const struct sk_buff *skb, uint32_t *addr) +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; @@ -92,26 +79,26 @@ static int get_ip_dst(const struct sk_buff *skb, uint32_t *addr) 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)); + (2 * (((*skb).nh.arph)->ar_hln)) + + (2 * (((*skb).nh.arph)->ar_pln)); - // Make sure the packet is long enough. + /* Make sure the packet is long enough. */ if ((((*skb).nh.raw) + arp_len) > (*skb).tail) return 0; - // IPv4 addresses are always 4 bytes. + /* 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)); - + (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) +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; @@ -120,36 +107,37 @@ static int get_ip_src(const struct sk_buff *skb, uint32_t *addr) 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)); + (2 * (((*skb).nh.arph)->ar_hln)) + + (2 * (((*skb).nh.arph)->ar_pln)); - // Make sure the packet is long enough. + /* Make sure the packet is long enough. */ if ((((*skb).nh.raw) + arp_len) > (*skb).tail) return 0; - // IPv4 addresses are always 4 bytes. + /* 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)); - + ((((*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) + 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; const struct ebt_mac_wormhash *wh_dst, *wh_src; - uint32_t dip=0, sip=0; + 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; get_ip_src(skb, &sip); @@ -158,8 +146,7 @@ static int ebt_filter_among(const struct sk_buff *skb, if (!ebt_mac_wormhash_contains(wh_src, smac, sip)) { return EBT_NOMATCH; } - } - else { + } else { /* we match only if it DOES NOT contain */ if (ebt_mac_wormhash_contains(wh_src, smac, sip)) { return EBT_NOMATCH; @@ -175,8 +162,7 @@ static int ebt_filter_among(const struct sk_buff *skb, if (!ebt_mac_wormhash_contains(wh_dst, dmac, dip)) { return EBT_NOMATCH; } - } - else { + } else { /* we match only if it DOES NOT contain */ if (ebt_mac_wormhash_contains(wh_dst, dmac, dip)) { return EBT_NOMATCH; @@ -188,34 +174,45 @@ static int ebt_filter_among(const struct sk_buff *skb, } static int ebt_among_check(const char *tablename, unsigned int hookmask, - const struct ebt_entry *e, void *data, unsigned int datalen) + 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; + 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 "ebtables: among: wrong size: %d against expected %d, rounded to %d\n", datalen, expected_length, 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 (wh_dst && ebt_mac_wormhash_check_integrity(wh_dst)) { - printk(KERN_WARNING "ebtables: among: dst integrity fail\n"); + if (wh_dst && (err = ebt_mac_wormhash_check_integrity(wh_dst))) { + printk(KERN_WARNING + "ebtables: among: dst integrity fail: %x\n", -err); return -EINVAL; } - if (wh_src && ebt_mac_wormhash_check_integrity(wh_src)) { - printk(KERN_WARNING "ebtables: among: src integrity fail\n"); + if (wh_src && (err = ebt_mac_wormhash_check_integrity(wh_src))) { + printk(KERN_WARNING + "ebtables: among: src integrity fail: %x\n", -err); return -EINVAL; } return 0; } -static struct ebt_match filter_among = -{ - {NULL, NULL}, EBT_AMONG_MATCH, ebt_filter_among, ebt_among_check, NULL, +static struct ebt_match filter_among = { + {NULL, NULL}, + EBT_AMONG_MATCH, + ebt_filter_among, + ebt_among_check, + NULL, THIS_MODULE }; diff --git a/kernel/linux2.5/net/bridge/netfilter/ebt_among.c b/kernel/linux2.5/net/bridge/netfilter/ebt_among.c index 1ae9ad4..e4b2268 100644 --- a/kernel/linux2.5/net/bridge/netfilter/ebt_among.c +++ b/kernel/linux2.5/net/bridge/netfilter/ebt_among.c @@ -10,100 +10,209 @@ #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 + * Some tricks were used, refer to + * 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 offset; - const char *base = (const char*)wh; + int start, limit, i; uint32_t cmp[2] = { 0, 0 }; - 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; + int key = (const unsigned char) mac[5]; + 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]) { + return 1; + } + } } return 0; } -static int ebt_mac_wormhash_check_integrity(const struct ebt_mac_wormhash *wh) +static int ebt_mac_wormhash_check_integrity(const struct ebt_mac_wormhash + *wh) { - int i, count; - const struct ebt_mac_wormhash_tuple *p; - int offset; - const char *base = (const char*)wh; - - count = 0; - for (i=256; i--;) { - offset = wh->table[i]; - while (offset) { - p = (const struct ebt_mac_wormhash_tuple*)(base + offset); - if (p < wh->pool) - return -1; - if (p > wh->pool + 256 - 1) - return -2; - count++; - if (count > 1000) - return -3; - offset = p->next_ofs; - } + 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; +} + +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) + 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; } static int ebt_among_check(const char *tablename, unsigned int hookmask, - const struct ebt_entry *e, void *data, unsigned int datalen) + 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; + 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(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)) { - printk(KERN_WARNING "ebtables: among: dst integrity fail\n"); + if (wh_dst && (err = ebt_mac_wormhash_check_integrity(wh_dst))) { + printk(KERN_WARNING + "ebtables: among: dst integrity fail: %x\n", -err); return -EINVAL; } - if ((info->bitmask & EBT_AMONG_SRC) && ebt_mac_wormhash_check_integrity(&info->wh_src)) { - printk(KERN_WARNING "ebtables: among: src integrity fail\n"); + if (wh_src && (err = ebt_mac_wormhash_check_integrity(wh_src))) { + printk(KERN_WARNING + "ebtables: among: src integrity fail: %x\n", -err); return -EINVAL; } return 0; } -static struct ebt_match filter_among = -{ - {NULL, NULL}, EBT_AMONG_MATCH, ebt_filter_among, ebt_among_check, NULL, +static struct ebt_match filter_among = { + {NULL, NULL}, + EBT_AMONG_MATCH, + ebt_filter_among, + ebt_among_check, + NULL, THIS_MODULE }; @@ -119,4 +228,5 @@ static void __exit fini(void) module_init(init); module_exit(fini); +EXPORT_NO_SYMBOLS; MODULE_LICENSE("GPL"); diff --git a/userspace/ebtables2/extensions/ebt_among.c b/userspace/ebtables2/extensions/ebt_among.c index 16502b2..f451735 100644 --- a/userspace/ebtables2/extensions/ebt_among.c +++ b/userspace/ebtables2/extensions/ebt_among.c @@ -9,16 +9,15 @@ #include <linux/if_ether.h> #include <linux/netfilter_bridge/ebt_among.h> -//#define DEBUG +#define NODEBUG #define AMONG_DST '1' #define AMONG_SRC '2' -static struct option opts[] = -{ - { "among-dst" , required_argument, 0, AMONG_DST }, - { "among-src" , required_argument, 0, AMONG_SRC }, - { 0 } +static struct option opts[] = { + {"among-dst", required_argument, 0, AMONG_DST}, + {"among-src", required_argument, 0, AMONG_SRC}, + {0} }; #ifdef DEBUG @@ -31,11 +30,11 @@ static void hexdump(const void *mem, int howmany) if (i % 32 == 0) { printf("\n%04x: ", i); } - printf("%2.2x%c", p[i], ". "[i%4==3]); + printf("%2.2x%c", p[i], ". "[i % 4 == 3]); } printf("\n"); } -#endif /* DEBUG */ +#endif /* DEBUG */ static void print_help() { @@ -44,31 +43,37 @@ static void print_help() "--among-dst list : matches if ether dst is in list\n" "--among-src list : matches if ether src is in list\n" "list has form:\n" -" xx:xx:xx:xx:xx:xx[=ip.ip.ip.ip],yy:yy:yy:yy:yy:yy[=ip.ip.ip.ip],...,zz:zz:zz:zz:zz:zz[=ip.ip.ip.ip][,]\n" +" xx:xx:xx:xx:xx:xx[=ip.ip.ip.ip],yy:yy:yy:yy:yy:yy[=ip.ip.ip.ip]" +",...,zz:zz:zz:zz:zz:zz[=ip.ip.ip.ip][,]\n" "Things in brackets are optional.\n" -"If you want to allow two (or more) IP addresses to one MAC address, you can\n" -"specify two (or more) pairs witch the same MAC, e.g.\n" +"If you want to allow two (or more) IP addresses to one MAC address, you \n" +"can specify two (or more) pairs witch the same MAC, e.g.\n" " 00:00:00:fa:eb:fe=153.19.120.250,00:00:00:fa:eb:fe=192.168.0.1\n" ); } static void init(struct ebt_entry_match *match) { - struct ebt_among_info *amonginfo = (struct ebt_among_info *)match->data; + struct ebt_among_info *amonginfo = + (struct ebt_among_info *) match->data; memset(amonginfo, 0, sizeof(struct ebt_among_info)); } static struct ebt_mac_wormhash *new_wormhash(int n) { - int size = sizeof(struct ebt_mac_wormhash) + n * sizeof(struct ebt_mac_wormhash_tuple); - struct ebt_mac_wormhash *result = (struct ebt_mac_wormhash *)malloc(size); + int size = + sizeof(struct ebt_mac_wormhash) + + n * sizeof(struct ebt_mac_wormhash_tuple); + struct ebt_mac_wormhash *result = + (struct ebt_mac_wormhash *) malloc(size); memset(result, 0, size); result->poolsize = n; return result; } -static void copy_wormhash(struct ebt_mac_wormhash *d, const struct ebt_mac_wormhash *s) +static void copy_wormhash(struct ebt_mac_wormhash *d, + const struct ebt_mac_wormhash *s) { int dpoolsize = d->poolsize; int dsize, ssize, amount; @@ -86,7 +91,8 @@ static void copy_wormhash(struct ebt_mac_wormhash *d, const struct ebt_mac_wormh * if `destbuf' is not NULL, it is filled by read bytes and ended with '\0' * *pp is set on the first byte not copied to `destbuf' */ -static int read_until(const char **pp, const char *delimiters, char *destbuf, int n) +static int read_until(const char **pp, const char *delimiters, + char *destbuf, int n) { int count = 0; int ret = 0; @@ -105,13 +111,45 @@ static int read_until(const char **pp, const char *delimiters, char *destbuf, in ret = -2; break; } - if (destbuf) destbuf[count++] = c; + if (destbuf) + destbuf[count++] = c; (*pp)++; } - if (destbuf) destbuf[count] = 0; + if (destbuf) + destbuf[count] = 0; return ret; } +static int fcmp(const void *va, const void *vb) { + const struct ebt_mac_wormhash_tuple *a = va; + const struct ebt_mac_wormhash_tuple *b = vb; + int ca = ((const unsigned char*)a->cmp)[7]; + int cb = ((const unsigned char*)b->cmp)[7]; + return ca - cb; +} + +static void index_table(struct ebt_mac_wormhash *wh) +{ + int ipool, itable; + int c; + for (itable = 0; itable <= 256; itable++) { + wh->table[itable] = wh->poolsize; + } + ipool = 0; + itable = 0; + while (1) { + wh->table[itable] = ipool; + c = ((const unsigned char*)wh->pool[ipool].cmp)[7]; + if (itable <= c) { + itable++; + } else { + ipool++; + } + if (ipool > wh->poolsize) + break; + } +} + static struct ebt_mac_wormhash *create_wormhash(const char *arg) { const char *pc = arg; @@ -120,26 +158,29 @@ static struct ebt_mac_wormhash *create_wormhash(const char *arg) struct ebt_mac_wormhash *workcopy, *result, *h; unsigned char mac[6]; unsigned char ip[4]; - int index; int nmacs = 0; int i; char token[4]; if (!(workcopy = new_wormhash(1024))) { - print_error("memory problem"); + print_memory(); } while (1) { /* remember current position, we'll need it on error */ anchor = pc; - /* collect MAC; all its bytes are followed by ':' (colon), except for - * the last one which can be followed by ',' (comma), '=' or '\0' */ + /* collect MAC; all its bytes are followed by ':' (colon), + * except for the last one which can be followed by + * ',' (comma), '=' or '\0' */ for (i = 0; i < 5; i++) { - if (read_until(&pc, ":", token, 2) < 0 || token[0] == 0) { - print_error("MAC parse error: %.20s", anchor); + if (read_until(&pc, ":", token, 2) < 0 + || token[0] == 0) { + print_error("MAC parse error: %.20s", + anchor); } mac[i] = strtol(token, &endptr, 16); if (*endptr) { - print_error("MAC parse error: %.20s", anchor); + print_error("MAC parse error: %.20s", + anchor); } pc++; } @@ -155,77 +196,91 @@ static struct ebt_mac_wormhash *create_wormhash(const char *arg) pc++; anchor = pc; for (i = 0; i < 3; i++) { - if (read_until(&pc, ".", token, 3) < 0 || token[0] == 0) { - print_error("IP parse error: %.20s", anchor); + if (read_until(&pc, ".", token, 3) < 0 + || token[0] == 0) { + print_error + ("IP parse error: %.20s", + anchor); } ip[i] = strtol(token, &endptr, 10); if (*endptr) { - print_error("IP parse error: %.20s", anchor); + print_error + ("IP parse error: %.20s", + anchor); } pc++; } - if (read_until(&pc, ",", token, 3) == -2 || token[0] == 0) { - print_error("IP parse error: %.20s", anchor); + if (read_until(&pc, ",", token, 3) == -2 + || token[0] == 0) { + print_error("IP parse error: %.20s", + anchor); } ip[3] = strtol(token, &endptr, 10); if (*endptr) { - print_error("IP parse error: %.20s", anchor); + print_error("IP parse error: %.20s", + anchor); } - } - else { + if (*(uint32_t*)ip == 0) { + print_error("Illegal IP 0.0.0.0"); + } + } else { /* no IP, we set it to 0.0.0.0 */ memset(ip, 0, 4); } - + /* we have collected MAC and IP, so we add an entry */ - index = (unsigned char)mac[5]; - memcpy(((char*)workcopy->pool[nmacs].cmp)+2, mac, 6); - workcopy->pool[nmacs].ip = *(const uint32_t*)ip; - workcopy->pool[nmacs].next_ofs = workcopy->table[index]; - workcopy->table[index] = ((const char*)&workcopy->pool[nmacs]) - (const char*)workcopy; + memcpy(((char *) workcopy->pool[nmacs].cmp) + 2, mac, 6); + workcopy->pool[nmacs].ip = *(const uint32_t *) ip; nmacs++; - + /* re-allocate memory if needed */ if (*pc && nmacs >= workcopy->poolsize) { if (!(h = new_wormhash(nmacs * 2))) { - print_error("memory problem"); + print_memory(); } copy_wormhash(h, workcopy); free(workcopy); workcopy = h; } - + /* check if end of string was reached */ if (!*pc) { break; } - - /* now `pc' points to comma if we are here; increment this to the next char */ + + /* now `pc' points to comma if we are here; */ + /* increment this to the next char */ /* but first assert :-> */ if (*pc != ',') { print_error("Something went wrong; no comma...\n"); } pc++; - - /* again check if end of string was reached; we allow an ending comma */ + + /* again check if end of string was reached; */ + /* we allow an ending comma */ if (!*pc) { break; } } if (!(result = new_wormhash(nmacs))) { - print_error("memory problem"); + print_memory(); } copy_wormhash(result, workcopy); free(workcopy); + qsort(&result->pool, result->poolsize, + sizeof(struct ebt_mac_wormhash_tuple), fcmp); + index_table(result); return result; } #define OPT_DST 0x01 #define OPT_SRC 0x02 -static int parse(int c, char **argv, int argc, const struct ebt_u_entry *entry, - unsigned int *flags, struct ebt_entry_match **match) +static int parse(int c, char **argv, int argc, + const struct ebt_u_entry *entry, unsigned int *flags, + struct ebt_entry_match **match) { - struct ebt_among_info *info = (struct ebt_among_info *)(*match)->data; + struct ebt_among_info *info = + (struct ebt_among_info *) (*match)->data; struct ebt_mac_wormhash *wh; struct ebt_entry_match *h; int new_size, old_size; @@ -242,18 +297,23 @@ static int parse(int c, char **argv, int argc, const struct ebt_u_entry *entry, if (optind > argc) print_error("No MAC list specified\n"); wh = create_wormhash(argv[optind - 1]); - old_size = sizeof(struct ebt_entry_match) + (**match).match_size; - h = malloc((new_size = old_size + ebt_mac_wormhash_size(wh))); + old_size = sizeof(struct ebt_entry_match) + + (**match).match_size; + h = malloc((new_size = + old_size + ebt_mac_wormhash_size(wh))); memcpy(h, *match, old_size); - memcpy((char*)h + old_size, wh, ebt_mac_wormhash_size(wh)); + memcpy((char *) h + old_size, wh, + ebt_mac_wormhash_size(wh)); h->match_size = new_size - sizeof(struct ebt_entry_match); - info = (struct ebt_among_info *)h->data; + info = (struct ebt_among_info *) h->data; if (c == AMONG_DST) { check_option(flags, OPT_DST); - info->wh_dst_ofs = old_size - sizeof(struct ebt_entry_match); + info->wh_dst_ofs = + old_size - sizeof(struct ebt_entry_match); } else { check_option(flags, OPT_SRC); - info->wh_src_ofs = old_size - sizeof(struct ebt_entry_match); + info->wh_src_ofs = + old_size - sizeof(struct ebt_entry_match); } free(*match); *match = h; @@ -266,35 +326,46 @@ static int parse(int c, char **argv, int argc, const struct ebt_u_entry *entry, } static void final_check(const struct ebt_u_entry *entry, - const struct ebt_entry_match *match, const char *name, - unsigned int hookmask, unsigned int time) + const struct ebt_entry_match *match, + const char *name, unsigned int hookmask, + unsigned int time) { } +#ifdef DEBUG +static void wormhash_debug(const struct ebt_mac_wormhash *wh) +{ + int i; + printf("poolsize: %d\n", wh->poolsize); + for (i = 0; i <= 256; i++) { + printf("%02x ", wh->table[i]); + if (i % 16 == 15) { + printf("\n"); + } + } + printf("\n"); +} +#endif /* DEBUG */ + static void wormhash_printout(const struct ebt_mac_wormhash *wh) { int i; - int offset; unsigned char *ip; - for (i = 0; i < 256; i++) { + for (i = 0; i < wh->poolsize; i++) { const struct ebt_mac_wormhash_tuple *p; - offset = wh->table[i]; - while (offset) { - p = (const struct ebt_mac_wormhash_tuple*)((const char*)wh + offset); - print_mac(((const char*)&p->cmp[0]) + 2); - if (p->ip) { - ip = (unsigned char*)&p->ip; - printf("=%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]); - } - printf(","); - offset = p->next_ofs; + p = (const struct ebt_mac_wormhash_tuple *)(&wh->pool[i]); + print_mac(((const char *) &p->cmp[0]) + 2); + if (p->ip) { + ip = (unsigned char *) &p->ip; + printf("=%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]); } - } + printf(","); + } printf(" "); } static void print(const struct ebt_u_entry *entry, - const struct ebt_entry_match *match) + const struct ebt_entry_match *match) { struct ebt_among_info *info = (struct ebt_among_info *)match->data; @@ -314,7 +385,8 @@ static void print(const struct ebt_u_entry *entry, } } -static int compare_wh(const struct ebt_mac_wormhash *aw, const struct ebt_mac_wormhash *bw) +static int compare_wh(const struct ebt_mac_wormhash *aw, + const struct ebt_mac_wormhash *bw) { int as, bs; as = ebt_mac_wormhash_size(aw); @@ -327,28 +399,30 @@ static int compare_wh(const struct ebt_mac_wormhash *aw, const struct ebt_mac_wo } static int compare(const struct ebt_entry_match *m1, - const struct ebt_entry_match *m2) + const struct ebt_entry_match *m2) { - struct ebt_among_info *a = (struct ebt_among_info *)m1->data; - struct ebt_among_info *b = (struct ebt_among_info *)m2->data; + struct ebt_among_info *a = (struct ebt_among_info *) m1->data; + struct ebt_among_info *b = (struct ebt_among_info *) m2->data; - if (!compare_wh(ebt_among_wh_dst(a), ebt_among_wh_dst(b))) return 0; - if (!compare_wh(ebt_among_wh_src(a), ebt_among_wh_src(b))) return 0; - if (a->bitmask != b->bitmask) return 0; + if (!compare_wh(ebt_among_wh_dst(a), ebt_among_wh_dst(b))) + return 0; + if (!compare_wh(ebt_among_wh_src(a), ebt_among_wh_src(b))) + return 0; + if (a->bitmask != b->bitmask) + return 0; return 1; } -static struct ebt_u_match among_match = -{ - .name = EBT_AMONG_MATCH, - .size = sizeof(struct ebt_among_info), - .help = print_help, - .init = init, - .parse = parse, - .final_check = final_check, - .print = print, - .compare = compare, - .extra_ops = opts, +static struct ebt_u_match among_match = { + .name = EBT_AMONG_MATCH, + .size = sizeof(struct ebt_among_info), + .help = print_help, + .init = init, + .parse = parse, + .final_check = final_check, + .print = print, + .compare = compare, + .extra_ops = opts, }; static void _init(void) __attribute__ ((constructor)); |