blob: 8d6ab644e7bf6c7d91d03e944dc1a843cb68afad [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ebt_arp
3 *
4 * Authors:
5 * Bart De Schuymer <bdschuym@pandora.be>
6 * Tim Gardner <timg@tpi.com>
7 *
8 * April, 2002
9 *
10 */
11
12#include <linux/netfilter_bridge/ebtables.h>
13#include <linux/netfilter_bridge/ebt_arp.h>
14#include <linux/if_arp.h>
15#include <linux/if_ether.h>
16#include <linux/module.h>
17
18static int ebt_filter_arp(const struct sk_buff *skb, const struct net_device *in,
19 const struct net_device *out, const void *data, unsigned int datalen)
20{
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080021 const struct ebt_arp_info *info = data;
22 const struct arphdr *ah;
23 struct arphdr _arph;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
26 if (ah == NULL)
27 return EBT_NOMATCH;
28 if (info->bitmask & EBT_ARP_OPCODE && FWINV(info->opcode !=
29 ah->ar_op, EBT_ARP_OPCODE))
30 return EBT_NOMATCH;
31 if (info->bitmask & EBT_ARP_HTYPE && FWINV(info->htype !=
32 ah->ar_hrd, EBT_ARP_HTYPE))
33 return EBT_NOMATCH;
34 if (info->bitmask & EBT_ARP_PTYPE && FWINV(info->ptype !=
35 ah->ar_pro, EBT_ARP_PTYPE))
36 return EBT_NOMATCH;
37
Bart De Schuymere011ff42007-11-05 20:59:47 -080038 if (info->bitmask & (EBT_ARP_SRC_IP | EBT_ARP_DST_IP | EBT_ARP_GRAT)) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080039 const __be32 *sap, *dap;
40 __be32 saddr, daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Bart De Schuymerc15bf6e2007-04-12 22:15:06 -070042 if (ah->ar_pln != sizeof(__be32) || ah->ar_pro != htons(ETH_P_IP))
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 return EBT_NOMATCH;
Bart De Schuymerc15bf6e2007-04-12 22:15:06 -070044 sap = skb_header_pointer(skb, sizeof(struct arphdr) +
45 ah->ar_hln, sizeof(saddr),
46 &saddr);
47 if (sap == NULL)
48 return EBT_NOMATCH;
49 dap = skb_header_pointer(skb, sizeof(struct arphdr) +
50 2*ah->ar_hln+sizeof(saddr),
51 sizeof(daddr), &daddr);
52 if (dap == NULL)
53 return EBT_NOMATCH;
54 if (info->bitmask & EBT_ARP_SRC_IP &&
55 FWINV(info->saddr != (*sap & info->smsk), EBT_ARP_SRC_IP))
56 return EBT_NOMATCH;
57 if (info->bitmask & EBT_ARP_DST_IP &&
58 FWINV(info->daddr != (*dap & info->dmsk), EBT_ARP_DST_IP))
59 return EBT_NOMATCH;
60 if (info->bitmask & EBT_ARP_GRAT &&
61 FWINV(*dap != *sap, EBT_ARP_GRAT))
62 return EBT_NOMATCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
64
65 if (info->bitmask & (EBT_ARP_SRC_MAC | EBT_ARP_DST_MAC)) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080066 const unsigned char *mp;
67 unsigned char _mac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 uint8_t verdict, i;
69
Bart De Schuymerc15bf6e2007-04-12 22:15:06 -070070 if (ah->ar_hln != ETH_ALEN || ah->ar_hrd != htons(ARPHRD_ETHER))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return EBT_NOMATCH;
72 if (info->bitmask & EBT_ARP_SRC_MAC) {
73 mp = skb_header_pointer(skb, sizeof(struct arphdr),
74 sizeof(_mac), &_mac);
75 if (mp == NULL)
76 return EBT_NOMATCH;
77 verdict = 0;
78 for (i = 0; i < 6; i++)
79 verdict |= (mp[i] ^ info->smaddr[i]) &
80 info->smmsk[i];
81 if (FWINV(verdict != 0, EBT_ARP_SRC_MAC))
82 return EBT_NOMATCH;
83 }
84
85 if (info->bitmask & EBT_ARP_DST_MAC) {
86 mp = skb_header_pointer(skb, sizeof(struct arphdr) +
87 ah->ar_hln + ah->ar_pln,
88 sizeof(_mac), &_mac);
89 if (mp == NULL)
90 return EBT_NOMATCH;
91 verdict = 0;
92 for (i = 0; i < 6; i++)
93 verdict |= (mp[i] ^ info->dmaddr[i]) &
94 info->dmmsk[i];
95 if (FWINV(verdict != 0, EBT_ARP_DST_MAC))
96 return EBT_NOMATCH;
97 }
98 }
99
100 return EBT_MATCH;
101}
102
103static int ebt_arp_check(const char *tablename, unsigned int hookmask,
104 const struct ebt_entry *e, void *data, unsigned int datalen)
105{
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800106 const struct ebt_arp_info *info = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 if (datalen != EBT_ALIGN(sizeof(struct ebt_arp_info)))
109 return -EINVAL;
110 if ((e->ethproto != htons(ETH_P_ARP) &&
111 e->ethproto != htons(ETH_P_RARP)) ||
112 e->invflags & EBT_IPROTO)
113 return -EINVAL;
114 if (info->bitmask & ~EBT_ARP_MASK || info->invflags & ~EBT_ARP_MASK)
115 return -EINVAL;
116 return 0;
117}
118
119static struct ebt_match filter_arp =
120{
121 .name = EBT_ARP_MATCH,
122 .match = ebt_filter_arp,
123 .check = ebt_arp_check,
124 .me = THIS_MODULE,
125};
126
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800127static int __init ebt_arp_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 return ebt_register_match(&filter_arp);
130}
131
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800132static void __exit ebt_arp_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 ebt_unregister_match(&filter_arp);
135}
136
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800137module_init(ebt_arp_init);
138module_exit(ebt_arp_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -0800139MODULE_DESCRIPTION("Ebtables: ARP protocol packet match");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140MODULE_LICENSE("GPL");