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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/ip.h> |
| 11 | #include <linux/if_arp.h> |
| 12 | #include <linux/module.h> |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 13 | #include <linux/netfilter/x_tables.h> |
| 14 | #include <linux/netfilter_bridge/ebtables.h> |
| 15 | #include <linux/netfilter_bridge/ebt_among.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 47c183f | 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 | 47c183f | 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 | 47c183f | 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 | 47c183f | 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 | 47c183f | 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 | 47c183f | 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 |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 131 | ebt_among_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 133 | const struct ebt_among_info *info = par->matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | const char *dmac, *smac; |
| 135 | const struct ebt_mac_wormhash *wh_dst, *wh_src; |
Al Viro | 47c183f | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 136 | __be32 dip = 0, sip = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | wh_dst = ebt_among_wh_dst(info); |
| 139 | wh_src = ebt_among_wh_src(info); |
| 140 | |
| 141 | if (wh_src) { |
| 142 | smac = eth_hdr(skb)->h_source; |
| 143 | if (get_ip_src(skb, &sip)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 144 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | if (!(info->bitmask & EBT_AMONG_SRC_NEG)) { |
| 146 | /* we match only if it contains */ |
| 147 | if (!ebt_mac_wormhash_contains(wh_src, smac, sip)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 148 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } else { |
| 150 | /* we match only if it DOES NOT contain */ |
| 151 | if (ebt_mac_wormhash_contains(wh_src, smac, sip)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 152 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | if (wh_dst) { |
| 157 | dmac = eth_hdr(skb)->h_dest; |
| 158 | if (get_ip_dst(skb, &dip)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 159 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | if (!(info->bitmask & EBT_AMONG_DST_NEG)) { |
| 161 | /* we match only if it contains */ |
| 162 | if (!ebt_mac_wormhash_contains(wh_dst, dmac, dip)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 163 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } else { |
| 165 | /* we match only if it DOES NOT contain */ |
| 166 | if (ebt_mac_wormhash_contains(wh_dst, dmac, dip)) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 167 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 171 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 174 | static bool ebt_among_mt_check(const struct xt_mtchk_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 176 | const struct ebt_among_info *info = par->matchinfo; |
Jan Engelhardt | 815377f | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 177 | const struct ebt_entry_match *em = |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 178 | container_of(par->matchinfo, const struct ebt_entry_match, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | int expected_length = sizeof(struct ebt_among_info); |
| 180 | const struct ebt_mac_wormhash *wh_dst, *wh_src; |
| 181 | int err; |
| 182 | |
| 183 | wh_dst = ebt_among_wh_dst(info); |
| 184 | wh_src = ebt_among_wh_src(info); |
| 185 | expected_length += ebt_mac_wormhash_size(wh_dst); |
| 186 | expected_length += ebt_mac_wormhash_size(wh_src); |
| 187 | |
Jan Engelhardt | 815377f | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 188 | if (em->match_size != EBT_ALIGN(expected_length)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | printk(KERN_WARNING |
Joe Perches | 3a47a68 | 2007-11-19 23:46:55 -0800 | [diff] [blame] | 190 | "ebtables: among: wrong size: %d " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | "against expected %d, rounded to %Zd\n", |
Jan Engelhardt | 815377f | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 192 | em->match_size, expected_length, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | EBT_ALIGN(expected_length)); |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 194 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | if (wh_dst && (err = ebt_mac_wormhash_check_integrity(wh_dst))) { |
| 197 | printk(KERN_WARNING |
| 198 | "ebtables: among: dst integrity fail: %x\n", -err); |
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_src && (err = ebt_mac_wormhash_check_integrity(wh_src))) { |
| 202 | printk(KERN_WARNING |
| 203 | "ebtables: among: src 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 | } |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 206 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 209 | static struct xt_match ebt_among_mt_reg __read_mostly = { |
| 210 | .name = "among", |
Jan Engelhardt | 001a18d | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 211 | .revision = 0, |
| 212 | .family = NFPROTO_BRIDGE, |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 213 | .match = ebt_among_mt, |
| 214 | .checkentry = ebt_among_mt_check, |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 215 | .matchsize = -1, /* special case */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | .me = THIS_MODULE, |
| 217 | }; |
| 218 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 219 | static int __init ebt_among_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 221 | return xt_register_match(&ebt_among_mt_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 224 | static void __exit ebt_among_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 226 | xt_unregister_match(&ebt_among_mt_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 229 | module_init(ebt_among_init); |
| 230 | module_exit(ebt_among_fini); |
Jan Engelhardt | f776c4c | 2008-01-31 04:00:30 -0800 | [diff] [blame] | 231 | MODULE_DESCRIPTION("Ebtables: Combined MAC/IP address list matching"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | MODULE_LICENSE("GPL"); |