10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/if_arp.h>
13 #include <linux/module.h>
14 #include <linux/netfilter/x_tables.h>
15 #include <linux/netfilter_bridge/ebtables.h>
16 #include <linux/netfilter_bridge/ebt_among.h>
18 static bool ebt_mac_wormhash_contains(const struct ebt_mac_wormhash *wh,
19 const char *mac, __be32 ip)
21 /* You may be puzzled as to how this code works.
22 * Some tricks were used, refer to
23 * include/linux/netfilter_bridge/ebt_among.h
24 * as there you can find a solution of this mystery.
26 const struct ebt_mac_wormhash_tuple *p;
28 uint32_t cmp[2] = { 0, 0 };
29 int key = ((const unsigned char *)mac)[5];
31 ether_addr_copy(((char *) cmp) + 2, mac);
32 start = wh->table[key];
33 limit = wh->table[key + 1];
35 for (i = start; i < limit; i++) {
37 if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0])
38 if (p->ip == 0 || p->ip == ip)
42 for (i = start; i < limit; i++) {
44 if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0])
52 static int ebt_mac_wormhash_check_integrity(const struct ebt_mac_wormhash
57 for (i = 0; i < 256; i++) {
58 if (wh->table[i] > wh->table[i + 1])
62 if (wh->table[i] > wh->poolsize)
65 if (wh->table[256] > wh->poolsize)
70 static int get_ip_dst(const struct sk_buff *skb, __be32 *addr)
72 if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)) {
73 const struct iphdr *ih;
76 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
80 } else if (eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) {
81 const struct arphdr *ah;
86 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
88 ah->ar_pln != sizeof(__be32) ||
89 ah->ar_hln != ETH_ALEN)
91 bp = skb_header_pointer(skb, sizeof(struct arphdr) +
92 2 * ETH_ALEN + sizeof(__be32),
93 sizeof(__be32), &buf);
101 static int get_ip_src(const struct sk_buff *skb, __be32 *addr)
103 if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)) {
104 const struct iphdr *ih;
107 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
111 } else if (eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) {
112 const struct arphdr *ah;
117 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
119 ah->ar_pln != sizeof(__be32) ||
120 ah->ar_hln != ETH_ALEN)
122 bp = skb_header_pointer(skb, sizeof(struct arphdr) +
123 ETH_ALEN, sizeof(__be32), &buf);
132 ebt_among_mt(const struct sk_buff *skb, struct xt_action_param *par)
134 const struct ebt_among_info *info = par->matchinfo;
135 const char *dmac, *smac;
136 const struct ebt_mac_wormhash *wh_dst, *wh_src;
137 __be32 dip = 0, sip = 0;
139 wh_dst = ebt_among_wh_dst(info);
140 wh_src = ebt_among_wh_src(info);
143 smac = eth_hdr(skb)->h_source;
144 if (get_ip_src(skb, &sip))
146 if (!(info->bitmask & EBT_AMONG_SRC_NEG)) {
147 /* we match only if it contains */
148 if (!ebt_mac_wormhash_contains(wh_src, smac, sip))
151 /* we match only if it DOES NOT contain */
152 if (ebt_mac_wormhash_contains(wh_src, smac, sip))
158 dmac = eth_hdr(skb)->h_dest;
159 if (get_ip_dst(skb, &dip))
161 if (!(info->bitmask & EBT_AMONG_DST_NEG)) {
162 /* we match only if it contains */
163 if (!ebt_mac_wormhash_contains(wh_dst, dmac, dip))
166 /* we match only if it DOES NOT contain */
167 if (ebt_mac_wormhash_contains(wh_dst, dmac, dip))
175 static int ebt_among_mt_check(const struct xt_mtchk_param *par)
177 const struct ebt_among_info *info = par->matchinfo;
178 const struct ebt_entry_match *em =
179 container_of(par->matchinfo, const struct ebt_entry_match, data);
180 int expected_length = sizeof(struct ebt_among_info);
181 const struct ebt_mac_wormhash *wh_dst, *wh_src;
184 wh_dst = ebt_among_wh_dst(info);
185 wh_src = ebt_among_wh_src(info);
186 expected_length += ebt_mac_wormhash_size(wh_dst);
187 expected_length += ebt_mac_wormhash_size(wh_src);
189 if (em->match_size != EBT_ALIGN(expected_length)) {
190 pr_info("wrong size: %d against expected %d, rounded to %Zd\n",
191 em->match_size, expected_length,
192 EBT_ALIGN(expected_length));
195 if (wh_dst && (err = ebt_mac_wormhash_check_integrity(wh_dst))) {
196 pr_info("dst integrity fail: %x\n", -err);
199 if (wh_src && (err = ebt_mac_wormhash_check_integrity(wh_src))) {
200 pr_info("src integrity fail: %x\n", -err);
206 static struct xt_match ebt_among_mt_reg __read_mostly = {
209 .family = NFPROTO_BRIDGE,
210 .match = ebt_among_mt,
211 .checkentry = ebt_among_mt_check,
212 .matchsize = -1, /* special case */
216 static int __init ebt_among_init(void)
218 return xt_register_match(&ebt_among_mt_reg);
221 static void __exit ebt_among_fini(void)
223 xt_unregister_match(&ebt_among_mt_reg);
226 module_init(ebt_among_init);
227 module_exit(ebt_among_fini);
228 MODULE_DESCRIPTION("Ebtables: Combined MAC/IP address list matching");
229 MODULE_LICENSE("GPL");