Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ebt_among |
| 3 | * |
| 4 | * Authors: |
| 5 | * Grzegorz Borowiak <grzes@gnu.univ.gda.pl> |
| 6 | * |
| 7 | * August, 2003 |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <linux/netfilter_bridge/ebtables.h> |
| 12 | #include <linux/netfilter_bridge/ebt_among.h> |
| 13 | #include <linux/ip.h> |
| 14 | #include <linux/if_arp.h> |
| 15 | #include <linux/module.h> |
| 16 | |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 17 | static bool ebt_mac_wormhash_contains(const struct ebt_mac_wormhash *wh, |
| 18 | const char *mac, __be32 ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | { |
| 20 | /* You may be puzzled as to how this code works. |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 21 | * Some tricks were used, refer to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * include/linux/netfilter_bridge/ebt_among.h |
| 23 | * as there you can find a solution of this mystery. |
| 24 | */ |
| 25 | const struct ebt_mac_wormhash_tuple *p; |
| 26 | int start, limit, i; |
| 27 | uint32_t cmp[2] = { 0, 0 }; |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 28 | int key = ((const unsigned char *)mac)[5]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | memcpy(((char *) cmp) + 2, mac, 6); |
| 31 | start = wh->table[key]; |
| 32 | limit = wh->table[key + 1]; |
| 33 | if (ip) { |
| 34 | for (i = start; i < limit; i++) { |
| 35 | p = &wh->pool[i]; |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 36 | if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0]) |
| 37 | if (p->ip == 0 || p->ip == ip) |
| 38 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | } else { |
| 41 | for (i = start; i < limit; i++) { |
| 42 | p = &wh->pool[i]; |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 43 | if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0]) |
| 44 | if (p->ip == 0) |
| 45 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | } |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 48 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static int ebt_mac_wormhash_check_integrity(const struct ebt_mac_wormhash |
| 52 | *wh) |
| 53 | { |
| 54 | int i; |
| 55 | |
| 56 | for (i = 0; i < 256; i++) { |
| 57 | if (wh->table[i] > wh->table[i + 1]) |
| 58 | return -0x100 - i; |
| 59 | if (wh->table[i] < 0) |
| 60 | return -0x200 - i; |
| 61 | if (wh->table[i] > wh->poolsize) |
| 62 | return -0x300 - i; |
| 63 | } |
| 64 | if (wh->table[256] > wh->poolsize) |
| 65 | return -0xc00; |
| 66 | return 0; |
| 67 | } |
| 68 | |
Al Viro | 47c183fa | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 69 | static int get_ip_dst(const struct sk_buff *skb, __be32 *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
| 71 | if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)) { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 72 | const struct iphdr *ih; |
| 73 | struct iphdr _iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); |
| 76 | if (ih == NULL) |
| 77 | return -1; |
| 78 | *addr = ih->daddr; |
| 79 | } else if (eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 80 | const struct arphdr *ah; |
| 81 | struct arphdr _arph; |
| 82 | const __be32 *bp; |
| 83 | __be32 buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph); |
| 86 | if (ah == NULL || |
Al Viro | 47c183fa | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 87 | ah->ar_pln != sizeof(__be32) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | ah->ar_hln != ETH_ALEN) |
| 89 | return -1; |
| 90 | bp = skb_header_pointer(skb, sizeof(struct arphdr) + |
Al Viro | 47c183fa | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 91 | 2 * ETH_ALEN + sizeof(__be32), |
| 92 | sizeof(__be32), &buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | if (bp == NULL) |
| 94 | return -1; |
| 95 | *addr = *bp; |
| 96 | } |
| 97 | return 0; |
| 98 | } |
| 99 | |
Al Viro | 47c183fa | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 100 | static int get_ip_src(const struct sk_buff *skb, __be32 *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
| 102 | if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)) { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 103 | const struct iphdr *ih; |
| 104 | struct iphdr _iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); |
| 107 | if (ih == NULL) |
| 108 | return -1; |
| 109 | *addr = ih->saddr; |
| 110 | } else if (eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 111 | const struct arphdr *ah; |
| 112 | struct arphdr _arph; |
| 113 | const __be32 *bp; |
| 114 | __be32 buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph); |
| 117 | if (ah == NULL || |
Al Viro | 47c183fa | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 118 | ah->ar_pln != sizeof(__be32) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | ah->ar_hln != ETH_ALEN) |
| 120 | return -1; |
| 121 | bp = skb_header_pointer(skb, sizeof(struct arphdr) + |
Al Viro | 47c183fa | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 122 | ETH_ALEN, sizeof(__be32), &buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | if (bp == NULL) |
| 124 | return -1; |
| 125 | *addr = *bp; |
| 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame^] | 130 | static bool |
| 131 | ebt_among_mt(const struct sk_buff *skb, const struct net_device *in, |
| 132 | const struct net_device *out, const struct xt_match *match, |
| 133 | const void *data, int offset, unsigned int protoff, bool *hotdrop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 135 | const struct ebt_among_info *info = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | const char *dmac, *smac; |
| 137 | const struct ebt_mac_wormhash *wh_dst, *wh_src; |
Al Viro | 47c183fa | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 138 | __be32 dip = 0, sip = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | wh_dst = ebt_among_wh_dst(info); |
| 141 | wh_src = ebt_among_wh_src(info); |
| 142 | |
| 143 | if (wh_src) { |
| 144 | smac = eth_hdr(skb)->h_source; |
| 145 | if (get_ip_src(skb, &sip)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 146 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | if (!(info->bitmask & EBT_AMONG_SRC_NEG)) { |
| 148 | /* we match only if it contains */ |
| 149 | if (!ebt_mac_wormhash_contains(wh_src, smac, sip)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 150 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } else { |
| 152 | /* we match only if it DOES NOT contain */ |
| 153 | if (ebt_mac_wormhash_contains(wh_src, smac, sip)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 154 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | if (wh_dst) { |
| 159 | dmac = eth_hdr(skb)->h_dest; |
| 160 | if (get_ip_dst(skb, &dip)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 161 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | if (!(info->bitmask & EBT_AMONG_DST_NEG)) { |
| 163 | /* we match only if it contains */ |
| 164 | if (!ebt_mac_wormhash_contains(wh_dst, dmac, dip)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 165 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } else { |
| 167 | /* we match only if it DOES NOT contain */ |
| 168 | if (ebt_mac_wormhash_contains(wh_dst, dmac, dip)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 169 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 173 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 176 | static bool |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame^] | 177 | ebt_among_mt_check(const char *table, const void *entry, |
| 178 | const struct xt_match *match, void *data, |
| 179 | unsigned int hook_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
Jan Engelhardt | 815377f | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 181 | const struct ebt_entry_match *em = |
| 182 | container_of(data, const struct ebt_entry_match, data); |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 183 | const struct ebt_among_info *info = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | int expected_length = sizeof(struct ebt_among_info); |
| 185 | const struct ebt_mac_wormhash *wh_dst, *wh_src; |
| 186 | int err; |
| 187 | |
| 188 | wh_dst = ebt_among_wh_dst(info); |
| 189 | wh_src = ebt_among_wh_src(info); |
| 190 | expected_length += ebt_mac_wormhash_size(wh_dst); |
| 191 | expected_length += ebt_mac_wormhash_size(wh_src); |
| 192 | |
Jan Engelhardt | 815377f | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 193 | if (em->match_size != EBT_ALIGN(expected_length)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | printk(KERN_WARNING |
Joe Perches | 3a47a68 | 2007-11-19 23:46:55 -0800 | [diff] [blame] | 195 | "ebtables: among: wrong size: %d " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | "against expected %d, rounded to %Zd\n", |
Jan Engelhardt | 815377f | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 197 | em->match_size, expected_length, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | EBT_ALIGN(expected_length)); |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 199 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
| 201 | if (wh_dst && (err = ebt_mac_wormhash_check_integrity(wh_dst))) { |
| 202 | printk(KERN_WARNING |
| 203 | "ebtables: among: dst integrity fail: %x\n", -err); |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 204 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | if (wh_src && (err = ebt_mac_wormhash_check_integrity(wh_src))) { |
| 207 | printk(KERN_WARNING |
| 208 | "ebtables: among: src integrity fail: %x\n", -err); |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 209 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 211 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Jan Engelhardt | 30083c9 | 2008-01-31 04:00:59 -0800 | [diff] [blame] | 214 | static struct ebt_match filter_among __read_mostly = { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 215 | .name = EBT_AMONG_MATCH, |
Jan Engelhardt | 001a18d | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 216 | .revision = 0, |
| 217 | .family = NFPROTO_BRIDGE, |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame^] | 218 | .match = ebt_among_mt, |
| 219 | .checkentry = ebt_among_mt_check, |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 220 | .matchsize = -1, /* special case */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | .me = THIS_MODULE, |
| 222 | }; |
| 223 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 224 | static int __init ebt_among_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
| 226 | return ebt_register_match(&filter_among); |
| 227 | } |
| 228 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 229 | static void __exit ebt_among_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | { |
| 231 | ebt_unregister_match(&filter_among); |
| 232 | } |
| 233 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 234 | module_init(ebt_among_init); |
| 235 | module_exit(ebt_among_fini); |
Jan Engelhardt | f776c4c | 2008-01-31 04:00:30 -0800 | [diff] [blame] | 236 | MODULE_DESCRIPTION("Ebtables: Combined MAC/IP address list matching"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | MODULE_LICENSE("GPL"); |