From aa55e1345bb84993273fabe4680a08b48fa99944 Mon Sep 17 00:00:00 2001 From: gborowiak Date: Tue, 16 Sep 2003 19:26:38 +0000 Subject: changed the structure of the hash table reedited to 80col/8tab and K-R indent 0.0.0.0 IP address is now illegal --- .../include/linux/netfilter_bridge/ebt_among.h | 34 +--- kernel/linux/net/bridge/netfilter/ebt_among.c | 143 +++++++------- kernel/linux2.5/net/bridge/netfilter/ebt_among.c | 212 ++++++++++++++++----- 3 files changed, 238 insertions(+), 151 deletions(-) (limited to 'kernel') 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 #include +/* #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 #include +#include +#include #include -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"); -- cgit v1.2.3