5 * Grzegorz Borowiak <grzes@gnu.univ.gda.pl>
11 #include <linux/if_arp.h>
12 #include <linux/module.h>
13 #include <linux/netfilter/x_tables.h>
14 #include <linux/netfilter_bridge/ebtables.h>
15 #include <linux/netfilter_bridge/ebt_among.h>
17 static bool ebt_mac_wormhash_contains(const struct ebt_mac_wormhash *wh,
18 const char *mac, __be32 ip)
20 /* You may be puzzled as to how this code works.
21 * Some tricks were used, refer to
22 * include/linux/netfilter_bridge/ebt_among.h
23 * as there you can find a solution of this mystery.
25 const struct ebt_mac_wormhash_tuple *p;
27 uint32_t cmp[2] = { 0, 0 };
28 int key = ((const unsigned char *)mac)[5];
30 memcpy(((char *) cmp) + 2, mac, 6);
31 start = wh->table[key];
32 limit = wh->table[key + 1];
34 for (i = start; i < limit; i++) {
36 if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0])
37 if (p->ip == 0 || p->ip == ip)
41 for (i = start; i < limit; i++) {
43 if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0])
51 static int ebt_mac_wormhash_check_integrity(const struct ebt_mac_wormhash
56 for (i = 0; i < 256; i++) {
57 if (wh->table[i] > wh->table[i + 1])
61 if (wh->table[i] > wh->poolsize)
64 if (wh->table[256] > wh->poolsize)
69 static int get_ip_dst(const struct sk_buff *skb, __be32 *addr)
71 if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)) {
72 const struct iphdr *ih;
75 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
79 } else if (eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) {
80 const struct arphdr *ah;
85 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
87 ah->ar_pln != sizeof(__be32) ||
88 ah->ar_hln != ETH_ALEN)
90 bp = skb_header_pointer(skb, sizeof(struct arphdr) +
91 2 * ETH_ALEN + sizeof(__be32),
92 sizeof(__be32), &buf);
100 static int get_ip_src(const struct sk_buff *skb, __be32 *addr)
102 if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)) {
103 const struct iphdr *ih;
106 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
110 } else if (eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) {
111 const struct arphdr *ah;
116 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
118 ah->ar_pln != sizeof(__be32) ||
119 ah->ar_hln != ETH_ALEN)
121 bp = skb_header_pointer(skb, sizeof(struct arphdr) +
122 ETH_ALEN, sizeof(__be32), &buf);
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)
135 const struct ebt_among_info *info = data;
136 const char *dmac, *smac;
137 const struct ebt_mac_wormhash *wh_dst, *wh_src;
138 __be32 dip = 0, sip = 0;
140 wh_dst = ebt_among_wh_dst(info);
141 wh_src = ebt_among_wh_src(info);
144 smac = eth_hdr(skb)->h_source;
145 if (get_ip_src(skb, &sip))
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))
152 /* we match only if it DOES NOT contain */
153 if (ebt_mac_wormhash_contains(wh_src, smac, sip))
159 dmac = eth_hdr(skb)->h_dest;
160 if (get_ip_dst(skb, &dip))
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))
167 /* we match only if it DOES NOT contain */
168 if (ebt_mac_wormhash_contains(wh_dst, dmac, dip))
177 ebt_among_mt_check(const char *table, const void *entry,
178 const struct xt_match *match, void *data,
179 unsigned int hook_mask)
181 const struct ebt_entry_match *em =
182 container_of(data, const struct ebt_entry_match, data);
183 const struct ebt_among_info *info = data;
184 int expected_length = sizeof(struct ebt_among_info);
185 const struct ebt_mac_wormhash *wh_dst, *wh_src;
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);
193 if (em->match_size != EBT_ALIGN(expected_length)) {
195 "ebtables: among: wrong size: %d "
196 "against expected %d, rounded to %Zd\n",
197 em->match_size, expected_length,
198 EBT_ALIGN(expected_length));
201 if (wh_dst && (err = ebt_mac_wormhash_check_integrity(wh_dst))) {
203 "ebtables: among: dst integrity fail: %x\n", -err);
206 if (wh_src && (err = ebt_mac_wormhash_check_integrity(wh_src))) {
208 "ebtables: among: src integrity fail: %x\n", -err);
214 static struct xt_match ebt_among_mt_reg __read_mostly = {
217 .family = NFPROTO_BRIDGE,
218 .match = ebt_among_mt,
219 .checkentry = ebt_among_mt_check,
220 .matchsize = -1, /* special case */
224 static int __init ebt_among_init(void)
226 return xt_register_match(&ebt_among_mt_reg);
229 static void __exit ebt_among_fini(void)
231 xt_unregister_match(&ebt_among_mt_reg);
234 module_init(ebt_among_init);
235 module_exit(ebt_among_fini);
236 MODULE_DESCRIPTION("Ebtables: Combined MAC/IP address list matching");
237 MODULE_LICENSE("GPL");