From 0cc37ceccd6056a72b9e4a7a65a8b0fc317be751 Mon Sep 17 00:00:00 2001 From: gborowiak Date: Sun, 7 Sep 2003 13:16:26 +0000 Subject: - variable size of the pool of addresses, no limit to 256 - IP/MAC correspondences matching available - negation mark '!' handled NOTE: incompatible with older version! --- .../include/linux/netfilter_bridge/ebt_among.h | 27 +- kernel/linux/net/bridge/netfilter/ebt_among.c | 152 +++++++++-- userspace/ebtables2/extensions/ebt_among.c | 281 +++++++++++++++------ 3 files changed, 355 insertions(+), 105 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 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 #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 @@ -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; } diff --git a/userspace/ebtables2/extensions/ebt_among.c b/userspace/ebtables2/extensions/ebt_among.c index 5ec8a45..df98b9a 100644 --- a/userspace/ebtables2/extensions/ebt_among.c +++ b/userspace/ebtables2/extensions/ebt_among.c @@ -9,9 +9,7 @@ #include #include -/* -#define DEBUG -*/ +//#define DEBUG #define AMONG_DST '1' #define AMONG_SRC '2' @@ -33,7 +31,7 @@ static void hexdump(const void *mem, int howmany) if (i % 32 == 0) { printf("\n%04x: ", i); } - printf("%2.2x ", p[i]); + printf("%2.2x%c", p[i], ". "[i%4==3]); } printf("\n"); } @@ -46,11 +44,11 @@ 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" -"\txx:xx:xx:xx:xx:xx,yy:yy:yy:yy:yy:yy,...,zz:zz:zz:zz:zz:zz\n" -"i.e. MAC addresses separated by commas, without spaces.\n" -"Optional comma can be included after the last MAC address, i.e.:\n" -"\txx:xx:xx:xx:xx:xx,yy:yy:yy:yy:yy:yy,...,zz:zz:zz:zz:zz:zz,\n" -"Each list can contain up to 256 addresses.\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" +" 00:00:00:fa:eb:fe=153.19.120.250,00:00:00:fa:eb:fe=192.168.0.1\n" ); } @@ -61,75 +59,165 @@ static void init(struct ebt_entry_match *match) memset(amonginfo, 0, sizeof(struct ebt_among_info)); } -static int fill_mac(char *mac, const char *string) +static struct ebt_mac_wormhash *new_wormhash(int n) { - char xnum[3]; - const char *p = string; - int i = 0; - int j = 0; + 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) +{ + int dpoolsize = d->poolsize; + int dsize, ssize, amount; + dsize = ebt_mac_wormhash_size(d); + ssize = ebt_mac_wormhash_size(s); + amount = dsize < ssize ? dsize : ssize; + memcpy(d, s, amount); + d->poolsize = dpoolsize; +} + +/* Returns: + * -1 when '\0' reached + * -2 when `n' bytes read and no delimiter found + * 0 when no less than `n' bytes read and delimiter found + * 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) +{ + int count = 0; + int ret = 0; + char c; while (1) { - if (isxdigit(*p)) { - xnum[j] = *p; - j++; - if (j >= 3) { - /* 3 or more hex digits for a single byte */ - return -3; - } + c = **pp; + if (!c) { + ret = -1; + break; } - else { - xnum[j] = 0; - j = 0; - mac[i] = strtol(xnum, 0, 16); - i++; - if (i >= 6) { - if (*p == ':') { - /* MAC address too long */ - return -2; - } - else { - return 0; - } - } - else { - if (*p != ':') { - /* MAC address too short */ - return -1; - } - } + if (strchr(delimiters, c)) { + ret = 0; + break; } - p++; + if (count == n) { + ret = -2; + break; + } + if (destbuf) destbuf[count++] = c; + (*pp)++; } - + if (destbuf) destbuf[count] = 0; + return ret; } -static void fill_wormhash(struct ebt_mac_wormhash *wh, const char *arg) +static struct ebt_mac_wormhash *create_wormhash(const char *arg) { const char *pc = arg; const char *anchor; - char mac[6]; + char *endptr; + struct ebt_mac_wormhash *workcopy, *result, *h; + unsigned char mac[6]; + unsigned char ip[4]; int index; int nmacs = 0; - char *base = (char*)wh; - memset(wh, 0, sizeof(struct ebt_mac_wormhash)); + int i; + char token[4]; + if (!(workcopy = new_wormhash(1024))) { + print_error("memory problem"); + } while (1) { + /* remember current position, we'll need it on error */ anchor = pc; - while (*pc && *pc != ',') pc++; - while (*pc && *pc == ',') pc++; - if (fill_mac(mac, anchor)) { - print_error("problem with MAC %20s...", anchor); + + /* 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); + } + mac[i] = strtol(token, &endptr, 16); + if (*endptr) { + print_error("MAC parse error: %.20s", anchor); + } + pc++; + } + if (read_until(&pc, "=,", token, 2) == -2 || 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); + } + if (*pc == '=') { + /* an IP follows the MAC; collect similarly to MAC */ + 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); + } + ip[i] = strtol(token, &endptr, 10); + if (*endptr) { + 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); + } + ip[3] = strtol(token, &endptr, 10); + if (*endptr) { + print_error("IP parse error: %.20s", anchor); + } } + 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*)wh->pool[nmacs].cmp)+2, mac, 6); - wh->pool[nmacs].next_ofs = wh->table[index]; - wh->table[index] = ((const char*)&wh->pool[nmacs]) - base; + 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; nmacs++; - if (*pc && nmacs >= 256) { - print_error("--among-src/--among-dst list can contain no more than 256 addresses\n"); + + /* re-allocate memory if needed */ + if (*pc && nmacs >= workcopy->poolsize) { + if (!(h = new_wormhash(nmacs * 2))) { + print_error("memory problem"); + } + 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 */ + /* 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 */ if (!*pc) { break; } } + if (!(result = new_wormhash(nmacs))) { + print_error("memory problem"); + } + copy_wormhash(result, workcopy); + free(workcopy); + return result; } #define OPT_DST 0x01 @@ -137,24 +225,39 @@ static void fill_wormhash(struct ebt_mac_wormhash *wh, const char *arg) 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 *amonginfo = (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; switch (c) { case AMONG_DST: case AMONG_SRC: + if (check_inverse(optarg)) { + if (c == AMONG_DST) + info->bitmask |= EBT_AMONG_DST_NEG; + else + info->bitmask |= EBT_AMONG_SRC_NEG; + } + 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))); + memcpy(h, *match, old_size); + 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; if (c == AMONG_DST) { check_option(flags, OPT_DST); - wh = &amonginfo->wh_dst; - amonginfo->bitmask |= EBT_AMONG_DST; + info->wh_dst_ofs = old_size - sizeof(struct ebt_entry_match); } else { check_option(flags, OPT_SRC); - wh = &amonginfo->wh_src; - amonginfo->bitmask |= EBT_AMONG_SRC; + info->wh_src_ofs = old_size - sizeof(struct ebt_entry_match); } - if (optind > argc) - print_error("No MAC list specified\n"); - fill_wormhash(wh, argv[optind - 1]); + free(*match); + *match = h; + free(wh); break; default: return 0; @@ -172,12 +275,18 @@ static void wormhash_printout(const struct ebt_mac_wormhash *wh) { int i; int offset; + unsigned char *ip; for (i = 0; i < 256; 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); - printf("%s,", ether_ntoa((const struct ether_addr *)(((const char*)&p->cmp[0]) + 2))); + printf("%s", ether_ntoa((const struct ether_addr *)(((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; } } @@ -187,30 +296,46 @@ static void wormhash_printout(const struct ebt_mac_wormhash *wh) static void print(const struct ebt_u_entry *entry, const struct ebt_entry_match *match) { - struct ebt_among_info *amonginfo = (struct ebt_among_info *)match->data; + struct ebt_among_info *info = (struct ebt_among_info *)match->data; - if (amonginfo->bitmask & EBT_AMONG_DST) { + if (info->wh_dst_ofs) { printf("--among-dst "); - wormhash_printout(&amonginfo->wh_dst); + if (info->bitmask && EBT_AMONG_DST_NEG) { + printf("! "); + } + wormhash_printout(ebt_among_wh_dst(info)); } - if (amonginfo->bitmask & EBT_AMONG_SRC) { + if (info->wh_src_ofs) { printf("--among-src "); - wormhash_printout(&amonginfo->wh_src); + if (info->bitmask && EBT_AMONG_SRC_NEG) { + printf("! "); + } + wormhash_printout(ebt_among_wh_src(info)); } } +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); + bs = ebt_mac_wormhash_size(bw); + if (as != bs) + return 0; + if (as && memcmp(aw, bw, as)) + return 0; + return 1; +} + static int compare(const struct ebt_entry_match *m1, const struct ebt_entry_match *m2) { - struct ebt_among_info *amonginfo1 = (struct ebt_among_info *)m1->data; - struct ebt_among_info *amonginfo2 = (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; -#ifdef DEBUG -// hexdump(amonginfo1, sizeof(struct ebt_among_info)); -// hexdump(amonginfo2, sizeof(struct ebt_among_info)); -#endif /* DEBUG */ - - return memcmp(amonginfo1, amonginfo2, sizeof(struct ebt_among_info)) == 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 = -- cgit v1.2.3