5 * Bart De Schuymer <bdschuym@pandora.be>
6 * Tim Gardner <timg@tpi.com>
11 #include <linux/if_arp.h>
12 #include <linux/if_ether.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_arp.h>
19 ebt_arp_mt(const struct sk_buff *skb, const struct net_device *in,
20 const struct net_device *out, const struct xt_match *match,
21 const void *data, int offset, unsigned int protoff, bool *hotdrop)
23 const struct ebt_arp_info *info = data;
24 const struct arphdr *ah;
27 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
30 if (info->bitmask & EBT_ARP_OPCODE && FWINV(info->opcode !=
31 ah->ar_op, EBT_ARP_OPCODE))
33 if (info->bitmask & EBT_ARP_HTYPE && FWINV(info->htype !=
34 ah->ar_hrd, EBT_ARP_HTYPE))
36 if (info->bitmask & EBT_ARP_PTYPE && FWINV(info->ptype !=
37 ah->ar_pro, EBT_ARP_PTYPE))
40 if (info->bitmask & (EBT_ARP_SRC_IP | EBT_ARP_DST_IP | EBT_ARP_GRAT)) {
41 const __be32 *sap, *dap;
44 if (ah->ar_pln != sizeof(__be32) || ah->ar_pro != htons(ETH_P_IP))
46 sap = skb_header_pointer(skb, sizeof(struct arphdr) +
47 ah->ar_hln, sizeof(saddr),
51 dap = skb_header_pointer(skb, sizeof(struct arphdr) +
52 2*ah->ar_hln+sizeof(saddr),
53 sizeof(daddr), &daddr);
56 if (info->bitmask & EBT_ARP_SRC_IP &&
57 FWINV(info->saddr != (*sap & info->smsk), EBT_ARP_SRC_IP))
59 if (info->bitmask & EBT_ARP_DST_IP &&
60 FWINV(info->daddr != (*dap & info->dmsk), EBT_ARP_DST_IP))
62 if (info->bitmask & EBT_ARP_GRAT &&
63 FWINV(*dap != *sap, EBT_ARP_GRAT))
67 if (info->bitmask & (EBT_ARP_SRC_MAC | EBT_ARP_DST_MAC)) {
68 const unsigned char *mp;
69 unsigned char _mac[ETH_ALEN];
72 if (ah->ar_hln != ETH_ALEN || ah->ar_hrd != htons(ARPHRD_ETHER))
74 if (info->bitmask & EBT_ARP_SRC_MAC) {
75 mp = skb_header_pointer(skb, sizeof(struct arphdr),
80 for (i = 0; i < 6; i++)
81 verdict |= (mp[i] ^ info->smaddr[i]) &
83 if (FWINV(verdict != 0, EBT_ARP_SRC_MAC))
87 if (info->bitmask & EBT_ARP_DST_MAC) {
88 mp = skb_header_pointer(skb, sizeof(struct arphdr) +
89 ah->ar_hln + ah->ar_pln,
94 for (i = 0; i < 6; i++)
95 verdict |= (mp[i] ^ info->dmaddr[i]) &
97 if (FWINV(verdict != 0, EBT_ARP_DST_MAC))
106 ebt_arp_mt_check(const char *table, const void *entry,
107 const struct xt_match *match, void *data,
108 unsigned int hook_mask)
110 const struct ebt_arp_info *info = data;
111 const struct ebt_entry *e = entry;
113 if ((e->ethproto != htons(ETH_P_ARP) &&
114 e->ethproto != htons(ETH_P_RARP)) ||
115 e->invflags & EBT_IPROTO)
117 if (info->bitmask & ~EBT_ARP_MASK || info->invflags & ~EBT_ARP_MASK)
122 static struct ebt_match filter_arp __read_mostly = {
123 .name = EBT_ARP_MATCH,
125 .family = NFPROTO_BRIDGE,
127 .checkentry = ebt_arp_mt_check,
128 .matchsize = XT_ALIGN(sizeof(struct ebt_arp_info)),
132 static int __init ebt_arp_init(void)
134 return ebt_register_match(&filter_arp);
137 static void __exit ebt_arp_fini(void)
139 ebt_unregister_match(&filter_arp);
142 module_init(ebt_arp_init);
143 module_exit(ebt_arp_fini);
144 MODULE_DESCRIPTION("Ebtables: ARP protocol packet match");
145 MODULE_LICENSE("GPL");