blob: b595f091f35b9cf1c2fc9bd6378edbc5fc5a90a2 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/ip.h>
11#include <linux/if_arp.h>
12#include <linux/module.h>
Jan Engelhardt043ef462008-10-08 11:35:15 +020013#include <linux/netfilter/x_tables.h>
14#include <linux/netfilter_bridge/ebtables.h>
15#include <linux/netfilter_bridge/ebt_among.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
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
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200131ebt_among_mt(const struct sk_buff *skb, const struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200133 const struct ebt_among_info *info = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 const char *dmac, *smac;
135 const struct ebt_mac_wormhash *wh_dst, *wh_src;
Al Viro47c183fa2006-11-14 21:11:51 -0800136 __be32 dip = 0, sip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 wh_dst = ebt_among_wh_dst(info);
139 wh_src = ebt_among_wh_src(info);
140
141 if (wh_src) {
142 smac = eth_hdr(skb)->h_source;
143 if (get_ip_src(skb, &sip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200144 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (!(info->bitmask & EBT_AMONG_SRC_NEG)) {
146 /* we match only if it contains */
147 if (!ebt_mac_wormhash_contains(wh_src, smac, sip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200148 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 } else {
150 /* we match only if it DOES NOT contain */
151 if (ebt_mac_wormhash_contains(wh_src, smac, sip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200152 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154 }
155
156 if (wh_dst) {
157 dmac = eth_hdr(skb)->h_dest;
158 if (get_ip_dst(skb, &dip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200159 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (!(info->bitmask & EBT_AMONG_DST_NEG)) {
161 /* we match only if it contains */
162 if (!ebt_mac_wormhash_contains(wh_dst, dmac, dip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200163 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 } else {
165 /* we match only if it DOES NOT contain */
166 if (ebt_mac_wormhash_contains(wh_dst, dmac, dip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200167 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169 }
170
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200171 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200174static bool ebt_among_mt_check(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200176 const struct ebt_among_info *info = par->matchinfo;
Jan Engelhardt815377f2008-10-08 11:35:14 +0200177 const struct ebt_entry_match *em =
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200178 container_of(par->matchinfo, const struct ebt_entry_match, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 int expected_length = sizeof(struct ebt_among_info);
180 const struct ebt_mac_wormhash *wh_dst, *wh_src;
181 int err;
182
183 wh_dst = ebt_among_wh_dst(info);
184 wh_src = ebt_among_wh_src(info);
185 expected_length += ebt_mac_wormhash_size(wh_dst);
186 expected_length += ebt_mac_wormhash_size(wh_src);
187
Jan Engelhardt815377f2008-10-08 11:35:14 +0200188 if (em->match_size != EBT_ALIGN(expected_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 printk(KERN_WARNING
Joe Perches3a47a682007-11-19 23:46:55 -0800190 "ebtables: among: wrong size: %d "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 "against expected %d, rounded to %Zd\n",
Jan Engelhardt815377f2008-10-08 11:35:14 +0200192 em->match_size, expected_length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 EBT_ALIGN(expected_length));
Jan Engelhardt19eda872008-10-08 11:35:13 +0200194 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196 if (wh_dst && (err = ebt_mac_wormhash_check_integrity(wh_dst))) {
197 printk(KERN_WARNING
198 "ebtables: among: dst integrity fail: %x\n", -err);
Jan Engelhardt19eda872008-10-08 11:35:13 +0200199 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201 if (wh_src && (err = ebt_mac_wormhash_check_integrity(wh_src))) {
202 printk(KERN_WARNING
203 "ebtables: among: src integrity fail: %x\n", -err);
Jan Engelhardt19eda872008-10-08 11:35:13 +0200204 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Jan Engelhardt19eda872008-10-08 11:35:13 +0200206 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Jan Engelhardt043ef462008-10-08 11:35:15 +0200209static struct xt_match ebt_among_mt_reg __read_mostly = {
210 .name = "among",
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200211 .revision = 0,
212 .family = NFPROTO_BRIDGE,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200213 .match = ebt_among_mt,
214 .checkentry = ebt_among_mt_check,
Jan Engelhardt18219d32008-10-08 11:35:13 +0200215 .matchsize = -1, /* special case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 .me = THIS_MODULE,
217};
218
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800219static int __init ebt_among_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200221 return xt_register_match(&ebt_among_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800224static void __exit ebt_among_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200226 xt_unregister_match(&ebt_among_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800229module_init(ebt_among_init);
230module_exit(ebt_among_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -0800231MODULE_DESCRIPTION("Ebtables: Combined MAC/IP address list matching");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232MODULE_LICENSE("GPL");