5 * Grzegorz Borowiak <grzes@gnu.univ.gda.pl>
11 #include <linux/netfilter_bridge/ebtables.h>
12 #include <linux/netfilter_bridge/ebt_among.h>
14 #include <linux/if_arp.h>
15 #include <linux/module.h>
17 static int ebt_mac_wormhash_contains(const struct ebt_mac_wormhash
*wh
,
18 const char *mac
, __be32 ip
)
20 /* You may be puzzled as to how this code works.
21 * Some tricks were used, refer to
22 * include/linux/netfilter_bridge/ebt_among.h
23 * as there you can find a solution of this mystery.
25 const struct ebt_mac_wormhash_tuple
*p
;
27 uint32_t cmp
[2] = { 0, 0 };
28 int key
= ((const unsigned char *)mac
)[5];
30 memcpy(((char *) cmp
) + 2, mac
, 6);
31 start
= wh
->table
[key
];
32 limit
= wh
->table
[key
+ 1];
34 for (i
= start
; i
< limit
; i
++) {
36 if (cmp
[1] == p
->cmp
[1] && cmp
[0] == p
->cmp
[0]) {
37 if (p
->ip
== 0 || p
->ip
== ip
) {
43 for (i
= start
; i
< limit
; i
++) {
45 if (cmp
[1] == p
->cmp
[1] && cmp
[0] == p
->cmp
[0]) {
55 static int ebt_mac_wormhash_check_integrity(const struct ebt_mac_wormhash
60 for (i
= 0; i
< 256; i
++) {
61 if (wh
->table
[i
] > wh
->table
[i
+ 1])
65 if (wh
->table
[i
] > wh
->poolsize
)
68 if (wh
->table
[256] > wh
->poolsize
)
73 static int get_ip_dst(const struct sk_buff
*skb
, __be32
*addr
)
75 if (eth_hdr(skb
)->h_proto
== htons(ETH_P_IP
)) {
76 const struct iphdr
*ih
;
79 ih
= skb_header_pointer(skb
, 0, sizeof(_iph
), &_iph
);
83 } else if (eth_hdr(skb
)->h_proto
== htons(ETH_P_ARP
)) {
84 const struct arphdr
*ah
;
89 ah
= skb_header_pointer(skb
, 0, sizeof(_arph
), &_arph
);
91 ah
->ar_pln
!= sizeof(__be32
) ||
92 ah
->ar_hln
!= ETH_ALEN
)
94 bp
= skb_header_pointer(skb
, sizeof(struct arphdr
) +
95 2 * ETH_ALEN
+ sizeof(__be32
),
96 sizeof(__be32
), &buf
);
104 static int get_ip_src(const struct sk_buff
*skb
, __be32
*addr
)
106 if (eth_hdr(skb
)->h_proto
== htons(ETH_P_IP
)) {
107 const struct iphdr
*ih
;
110 ih
= skb_header_pointer(skb
, 0, sizeof(_iph
), &_iph
);
114 } else if (eth_hdr(skb
)->h_proto
== htons(ETH_P_ARP
)) {
115 const struct arphdr
*ah
;
120 ah
= skb_header_pointer(skb
, 0, sizeof(_arph
), &_arph
);
122 ah
->ar_pln
!= sizeof(__be32
) ||
123 ah
->ar_hln
!= ETH_ALEN
)
125 bp
= skb_header_pointer(skb
, sizeof(struct arphdr
) +
126 ETH_ALEN
, sizeof(__be32
), &buf
);
134 static int ebt_filter_among(const struct sk_buff
*skb
,
135 const struct net_device
*in
,
136 const struct net_device
*out
, const void *data
,
137 unsigned int datalen
)
139 const struct ebt_among_info
*info
= data
;
140 const char *dmac
, *smac
;
141 const struct ebt_mac_wormhash
*wh_dst
, *wh_src
;
142 __be32 dip
= 0, sip
= 0;
144 wh_dst
= ebt_among_wh_dst(info
);
145 wh_src
= ebt_among_wh_src(info
);
148 smac
= eth_hdr(skb
)->h_source
;
149 if (get_ip_src(skb
, &sip
))
151 if (!(info
->bitmask
& EBT_AMONG_SRC_NEG
)) {
152 /* we match only if it contains */
153 if (!ebt_mac_wormhash_contains(wh_src
, smac
, sip
))
156 /* we match only if it DOES NOT contain */
157 if (ebt_mac_wormhash_contains(wh_src
, smac
, sip
))
163 dmac
= eth_hdr(skb
)->h_dest
;
164 if (get_ip_dst(skb
, &dip
))
166 if (!(info
->bitmask
& EBT_AMONG_DST_NEG
)) {
167 /* we match only if it contains */
168 if (!ebt_mac_wormhash_contains(wh_dst
, dmac
, dip
))
171 /* we match only if it DOES NOT contain */
172 if (ebt_mac_wormhash_contains(wh_dst
, dmac
, dip
))
180 static int ebt_among_check(const char *tablename
, unsigned int hookmask
,
181 const struct ebt_entry
*e
, void *data
,
182 unsigned int datalen
)
184 const struct ebt_among_info
*info
= data
;
185 int expected_length
= sizeof(struct ebt_among_info
);
186 const struct ebt_mac_wormhash
*wh_dst
, *wh_src
;
189 wh_dst
= ebt_among_wh_dst(info
);
190 wh_src
= ebt_among_wh_src(info
);
191 expected_length
+= ebt_mac_wormhash_size(wh_dst
);
192 expected_length
+= ebt_mac_wormhash_size(wh_src
);
194 if (datalen
!= EBT_ALIGN(expected_length
)) {
196 "ebtables: among: wrong size: %d "
197 "against expected %d, rounded to %Zd\n",
198 datalen
, expected_length
,
199 EBT_ALIGN(expected_length
));
202 if (wh_dst
&& (err
= ebt_mac_wormhash_check_integrity(wh_dst
))) {
204 "ebtables: among: dst integrity fail: %x\n", -err
);
207 if (wh_src
&& (err
= ebt_mac_wormhash_check_integrity(wh_src
))) {
209 "ebtables: among: src integrity fail: %x\n", -err
);
215 static struct ebt_match filter_among __read_mostly
= {
216 .name
= EBT_AMONG_MATCH
,
217 .match
= ebt_filter_among
,
218 .check
= ebt_among_check
,
222 static int __init
ebt_among_init(void)
224 return ebt_register_match(&filter_among
);
227 static void __exit
ebt_among_fini(void)
229 ebt_unregister_match(&filter_among
);
232 module_init(ebt_among_init
);
233 module_exit(ebt_among_fini
);
234 MODULE_DESCRIPTION("Ebtables: Combined MAC/IP address list matching");
235 MODULE_LICENSE("GPL");