blob: 84a306f085b5f4e9e96ed7a41e104a69fe713687 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ebt_among
3 *
4 * Authors:
5 * Grzegorz Borowiak <grzes@gnu.univ.gda.pl>
6 *
7 * August, 2003
8 *
9 */
10
11#include <linux/netfilter_bridge/ebtables.h>
12#include <linux/netfilter_bridge/ebt_among.h>
13#include <linux/ip.h>
14#include <linux/if_arp.h>
15#include <linux/module.h>
16
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020017static bool ebt_mac_wormhash_contains(const struct ebt_mac_wormhash *wh,
18 const char *mac, __be32 ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019{
20 /* You may be puzzled as to how this code works.
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090021 * Some tricks were used, refer to
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * 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 Engelhardtabfdf1c2008-01-31 03:59:24 -080028 int key = ((const unsigned char *)mac)[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
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 Engelhardt8cc784e2008-10-08 11:35:13 +020036 if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0])
37 if (p->ip == 0 || p->ip == ip)
38 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 }
40 } else {
41 for (i = start; i < limit; i++) {
42 p = &wh->pool[i];
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020043 if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0])
44 if (p->ip == 0)
45 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 }
47 }
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020048 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
51static 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 Viro47c183fa2006-11-14 21:11:51 -080069static int get_ip_dst(const struct sk_buff *skb, __be32 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080072 const struct iphdr *ih;
73 struct iphdr _iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
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 Engelhardtabfdf1c2008-01-31 03:59:24 -080080 const struct arphdr *ah;
81 struct arphdr _arph;
82 const __be32 *bp;
83 __be32 buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
86 if (ah == NULL ||
Al Viro47c183fa2006-11-14 21:11:51 -080087 ah->ar_pln != sizeof(__be32) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 ah->ar_hln != ETH_ALEN)
89 return -1;
90 bp = skb_header_pointer(skb, sizeof(struct arphdr) +
Al Viro47c183fa2006-11-14 21:11:51 -080091 2 * ETH_ALEN + sizeof(__be32),
92 sizeof(__be32), &buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (bp == NULL)
94 return -1;
95 *addr = *bp;
96 }
97 return 0;
98}
99
Al Viro47c183fa2006-11-14 21:11:51 -0800100static int get_ip_src(const struct sk_buff *skb, __be32 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800103 const struct iphdr *ih;
104 struct iphdr _iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
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 Engelhardtabfdf1c2008-01-31 03:59:24 -0800111 const struct arphdr *ah;
112 struct arphdr _arph;
113 const __be32 *bp;
114 __be32 buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
117 if (ah == NULL ||
Al Viro47c183fa2006-11-14 21:11:51 -0800118 ah->ar_pln != sizeof(__be32) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 ah->ar_hln != ETH_ALEN)
120 return -1;
121 bp = skb_header_pointer(skb, sizeof(struct arphdr) +
Al Viro47c183fa2006-11-14 21:11:51 -0800122 ETH_ALEN, sizeof(__be32), &buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (bp == NULL)
124 return -1;
125 *addr = *bp;
126 }
127 return 0;
128}
129
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200130static bool
131ebt_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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800135 const struct ebt_among_info *info = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 const char *dmac, *smac;
137 const struct ebt_mac_wormhash *wh_dst, *wh_src;
Al Viro47c183fa2006-11-14 21:11:51 -0800138 __be32 dip = 0, sip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 wh_dst = ebt_among_wh_dst(info);
141 wh_src = ebt_among_wh_src(info);
142
143 if (wh_src) {
144 smac = eth_hdr(skb)->h_source;
145 if (get_ip_src(skb, &sip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200146 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (!(info->bitmask & EBT_AMONG_SRC_NEG)) {
148 /* we match only if it contains */
149 if (!ebt_mac_wormhash_contains(wh_src, smac, sip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200150 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 } else {
152 /* we match only if it DOES NOT contain */
153 if (ebt_mac_wormhash_contains(wh_src, smac, sip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200154 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156 }
157
158 if (wh_dst) {
159 dmac = eth_hdr(skb)->h_dest;
160 if (get_ip_dst(skb, &dip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200161 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (!(info->bitmask & EBT_AMONG_DST_NEG)) {
163 /* we match only if it contains */
164 if (!ebt_mac_wormhash_contains(wh_dst, dmac, dip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200165 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 } else {
167 /* we match only if it DOES NOT contain */
168 if (ebt_mac_wormhash_contains(wh_dst, dmac, dip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200169 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 }
172
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200173 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Jan Engelhardt19eda872008-10-08 11:35:13 +0200176static bool
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200177ebt_among_mt_check(const char *table, const void *entry,
178 const struct xt_match *match, void *data,
179 unsigned int hook_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Jan Engelhardt815377f2008-10-08 11:35:14 +0200181 const struct ebt_entry_match *em =
182 container_of(data, const struct ebt_entry_match, data);
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800183 const struct ebt_among_info *info = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int expected_length = sizeof(struct ebt_among_info);
185 const struct ebt_mac_wormhash *wh_dst, *wh_src;
186 int err;
187
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);
192
Jan Engelhardt815377f2008-10-08 11:35:14 +0200193 if (em->match_size != EBT_ALIGN(expected_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 printk(KERN_WARNING
Joe Perches3a47a682007-11-19 23:46:55 -0800195 "ebtables: among: wrong size: %d "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 "against expected %d, rounded to %Zd\n",
Jan Engelhardt815377f2008-10-08 11:35:14 +0200197 em->match_size, expected_length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 EBT_ALIGN(expected_length));
Jan Engelhardt19eda872008-10-08 11:35:13 +0200199 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201 if (wh_dst && (err = ebt_mac_wormhash_check_integrity(wh_dst))) {
202 printk(KERN_WARNING
203 "ebtables: among: dst integrity fail: %x\n", -err);
Jan Engelhardt19eda872008-10-08 11:35:13 +0200204 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206 if (wh_src && (err = ebt_mac_wormhash_check_integrity(wh_src))) {
207 printk(KERN_WARNING
208 "ebtables: among: src integrity fail: %x\n", -err);
Jan Engelhardt19eda872008-10-08 11:35:13 +0200209 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Jan Engelhardt19eda872008-10-08 11:35:13 +0200211 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Jan Engelhardt30083c92008-01-31 04:00:59 -0800214static struct ebt_match filter_among __read_mostly = {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900215 .name = EBT_AMONG_MATCH,
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200216 .revision = 0,
217 .family = NFPROTO_BRIDGE,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200218 .match = ebt_among_mt,
219 .checkentry = ebt_among_mt_check,
Jan Engelhardt18219d32008-10-08 11:35:13 +0200220 .matchsize = -1, /* special case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 .me = THIS_MODULE,
222};
223
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800224static int __init ebt_among_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 return ebt_register_match(&filter_among);
227}
228
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800229static void __exit ebt_among_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 ebt_unregister_match(&filter_among);
232}
233
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800234module_init(ebt_among_init);
235module_exit(ebt_among_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -0800236MODULE_DESCRIPTION("Ebtables: Combined MAC/IP address list matching");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237MODULE_LICENSE("GPL");