blob: 8b84c581be3082ea4c8a6a21a362a3077ab17751 [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 */
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/ip.h>
12#include <linux/if_arp.h>
13#include <linux/module.h>
Jan Engelhardt043ef462008-10-08 11:35:15 +020014#include <linux/netfilter/x_tables.h>
15#include <linux/netfilter_bridge/ebtables.h>
16#include <linux/netfilter_bridge/ebt_among.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020018static bool ebt_mac_wormhash_contains(const struct ebt_mac_wormhash *wh,
19 const char *mac, __be32 ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
21 /* You may be puzzled as to how this code works.
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090022 * Some tricks were used, refer to
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * include/linux/netfilter_bridge/ebt_among.h
24 * as there you can find a solution of this mystery.
25 */
26 const struct ebt_mac_wormhash_tuple *p;
27 int start, limit, i;
28 uint32_t cmp[2] = { 0, 0 };
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080029 int key = ((const unsigned char *)mac)[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31 memcpy(((char *) cmp) + 2, mac, 6);
32 start = wh->table[key];
33 limit = wh->table[key + 1];
34 if (ip) {
35 for (i = start; i < limit; i++) {
36 p = &wh->pool[i];
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020037 if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0])
38 if (p->ip == 0 || p->ip == ip)
39 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 }
41 } else {
42 for (i = start; i < limit; i++) {
43 p = &wh->pool[i];
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020044 if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0])
45 if (p->ip == 0)
46 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
48 }
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020049 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
52static int ebt_mac_wormhash_check_integrity(const struct ebt_mac_wormhash
53 *wh)
54{
55 int i;
56
57 for (i = 0; i < 256; i++) {
58 if (wh->table[i] > wh->table[i + 1])
59 return -0x100 - i;
60 if (wh->table[i] < 0)
61 return -0x200 - i;
62 if (wh->table[i] > wh->poolsize)
63 return -0x300 - i;
64 }
65 if (wh->table[256] > wh->poolsize)
66 return -0xc00;
67 return 0;
68}
69
Al Viro47c183fa2006-11-14 21:11:51 -080070static int get_ip_dst(const struct sk_buff *skb, __be32 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080073 const struct iphdr *ih;
74 struct iphdr _iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
77 if (ih == NULL)
78 return -1;
79 *addr = ih->daddr;
80 } else if (eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080081 const struct arphdr *ah;
82 struct arphdr _arph;
83 const __be32 *bp;
84 __be32 buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
87 if (ah == NULL ||
Al Viro47c183fa2006-11-14 21:11:51 -080088 ah->ar_pln != sizeof(__be32) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 ah->ar_hln != ETH_ALEN)
90 return -1;
91 bp = skb_header_pointer(skb, sizeof(struct arphdr) +
Al Viro47c183fa2006-11-14 21:11:51 -080092 2 * ETH_ALEN + sizeof(__be32),
93 sizeof(__be32), &buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (bp == NULL)
95 return -1;
96 *addr = *bp;
97 }
98 return 0;
99}
100
Al Viro47c183fa2006-11-14 21:11:51 -0800101static int get_ip_src(const struct sk_buff *skb, __be32 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800104 const struct iphdr *ih;
105 struct iphdr _iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
108 if (ih == NULL)
109 return -1;
110 *addr = ih->saddr;
111 } else if (eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800112 const struct arphdr *ah;
113 struct arphdr _arph;
114 const __be32 *bp;
115 __be32 buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
118 if (ah == NULL ||
Al Viro47c183fa2006-11-14 21:11:51 -0800119 ah->ar_pln != sizeof(__be32) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 ah->ar_hln != ETH_ALEN)
121 return -1;
122 bp = skb_header_pointer(skb, sizeof(struct arphdr) +
Al Viro47c183fa2006-11-14 21:11:51 -0800123 ETH_ALEN, sizeof(__be32), &buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (bp == NULL)
125 return -1;
126 *addr = *bp;
127 }
128 return 0;
129}
130
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200131static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +0200132ebt_among_mt(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200134 const struct ebt_among_info *info = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 const char *dmac, *smac;
136 const struct ebt_mac_wormhash *wh_dst, *wh_src;
Al Viro47c183fa2006-11-14 21:11:51 -0800137 __be32 dip = 0, sip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 wh_dst = ebt_among_wh_dst(info);
140 wh_src = ebt_among_wh_src(info);
141
142 if (wh_src) {
143 smac = eth_hdr(skb)->h_source;
144 if (get_ip_src(skb, &sip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200145 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (!(info->bitmask & EBT_AMONG_SRC_NEG)) {
147 /* we match only if it contains */
148 if (!ebt_mac_wormhash_contains(wh_src, smac, sip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200149 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 } else {
151 /* we match only if it DOES NOT contain */
152 if (ebt_mac_wormhash_contains(wh_src, smac, sip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200153 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155 }
156
157 if (wh_dst) {
158 dmac = eth_hdr(skb)->h_dest;
159 if (get_ip_dst(skb, &dip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200160 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (!(info->bitmask & EBT_AMONG_DST_NEG)) {
162 /* we match only if it contains */
163 if (!ebt_mac_wormhash_contains(wh_dst, dmac, dip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200164 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 } else {
166 /* we match only if it DOES NOT contain */
167 if (ebt_mac_wormhash_contains(wh_dst, dmac, dip))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200168 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170 }
171
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200172 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Jan Engelhardtb0f38452010-03-19 17:16:42 +0100175static int ebt_among_mt_check(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200177 const struct ebt_among_info *info = par->matchinfo;
Jan Engelhardt815377f2008-10-08 11:35:14 +0200178 const struct ebt_entry_match *em =
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200179 container_of(par->matchinfo, const struct ebt_entry_match, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 int expected_length = sizeof(struct ebt_among_info);
181 const struct ebt_mac_wormhash *wh_dst, *wh_src;
182 int err;
183
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);
188
Jan Engelhardt815377f2008-10-08 11:35:14 +0200189 if (em->match_size != EBT_ALIGN(expected_length)) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100190 pr_info("wrong size: %d against expected %d, rounded to %Zd\n",
191 em->match_size, expected_length,
192 EBT_ALIGN(expected_length));
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100193 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195 if (wh_dst && (err = ebt_mac_wormhash_check_integrity(wh_dst))) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100196 pr_info("dst integrity fail: %x\n", -err);
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100197 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199 if (wh_src && (err = ebt_mac_wormhash_check_integrity(wh_src))) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100200 pr_info("src integrity fail: %x\n", -err);
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100201 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Jan Engelhardt043ef462008-10-08 11:35:15 +0200206static struct xt_match ebt_among_mt_reg __read_mostly = {
207 .name = "among",
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200208 .revision = 0,
209 .family = NFPROTO_BRIDGE,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200210 .match = ebt_among_mt,
211 .checkentry = ebt_among_mt_check,
Jan Engelhardt18219d32008-10-08 11:35:13 +0200212 .matchsize = -1, /* special case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .me = THIS_MODULE,
214};
215
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800216static int __init ebt_among_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200218 return xt_register_match(&ebt_among_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800221static void __exit ebt_among_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200223 xt_unregister_match(&ebt_among_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800226module_init(ebt_among_init);
227module_exit(ebt_among_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -0800228MODULE_DESCRIPTION("Ebtables: Combined MAC/IP address list matching");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229MODULE_LICENSE("GPL");