1 ebtables-v2.0.002 vs 2.4.20-pre7 - 17 October 2002
3 --- linux-2.4.20-pre7/net/bridge/br_private.h Mon Feb 25 20:38:14 2002
4 +++ linux-2.4.20-pre7-bcnt/net/bridge/br_private.h Thu Oct 17 22:40:38 2002
5 @@ -166,7 +166,7 @@ extern void br_get_port_ifindices(struct
9 -extern void br_handle_frame(struct sk_buff *skb);
10 +extern int br_handle_frame(struct sk_buff *skb);
13 extern void br_call_ioctl_atomic(void (*fn)(void));
14 --- linux-2.4.20-pre7/include/linux/if_bridge.h Thu Nov 22 20:47:12 2001
15 +++ linux-2.4.20-pre7-bcnt/include/linux/if_bridge.h Thu Oct 17 22:28:23 2002
16 @@ -102,7 +102,8 @@ struct net_bridge;
17 struct net_bridge_port;
19 extern int (*br_ioctl_hook)(unsigned long arg);
20 -extern void (*br_handle_frame_hook)(struct sk_buff *skb);
21 +extern int (*br_handle_frame_hook)(struct sk_buff *skb);
22 +extern int (*br_should_route_hook)(struct sk_buff **pskb);
26 --- linux-2.4.20-pre7/net/core/dev.c Thu Oct 17 23:42:53 2002
27 +++ linux-2.4.20-pre7-bcnt/net/core/dev.c Thu Oct 17 20:31:28 2002
28 @@ -1392,7 +1392,7 @@ void net_call_rx_atomic(void (*fn)(void)
31 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
32 -void (*br_handle_frame_hook)(struct sk_buff *skb) = NULL;
33 +int (*br_handle_frame_hook)(struct sk_buff *skb) = NULL;
36 static __inline__ int handle_bridge(struct sk_buff *skb,
37 @@ -1409,7 +1409,6 @@ static __inline__ int handle_bridge(stru
41 - br_handle_frame_hook(skb);
45 @@ -1469,7 +1468,12 @@ int netif_receive_skb(struct sk_buff *sk
46 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
47 if (skb->dev->br_port != NULL &&
48 br_handle_frame_hook != NULL) {
49 - return handle_bridge(skb, pt_prev);
52 + ret = handle_bridge(skb, pt_prev);
53 + if (br_handle_frame_hook(skb) == 0)
59 --- linux-2.4.20-pre7/net/bridge/br_input.c Sat Aug 3 02:39:46 2002
60 +++ linux-2.4.20-pre7-bcnt/net/bridge/br_input.c Thu Oct 17 20:31:28 2002
61 @@ -24,6 +24,9 @@ unsigned char bridge_ula[6] = { 0x01, 0x
63 static int br_pass_frame_up_finish(struct sk_buff *skb)
65 +#ifdef CONFIG_NETFILTER_DEBUG
71 @@ -112,7 +115,7 @@ err_nolock:
75 -void br_handle_frame(struct sk_buff *skb)
76 +int br_handle_frame(struct sk_buff *skb)
78 struct net_bridge *br;
80 @@ -146,25 +149,31 @@ void br_handle_frame(struct sk_buff *skb
81 goto handle_special_frame;
83 if (p->state == BR_STATE_FORWARDING) {
84 + if (br_should_route_hook && br_should_route_hook(&skb)) {
85 + read_unlock(&br->lock);
89 NF_HOOK(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
90 br_handle_frame_finish);
91 read_unlock(&br->lock);
97 read_unlock(&br->lock);
103 handle_special_frame:
105 br_stp_handle_bpdu(skb);
106 read_unlock(&br->lock);
111 read_unlock(&br->lock);
115 --- linux-2.4.20-pre7/net/bridge/br_forward.c Sat Aug 3 02:39:46 2002
116 +++ linux-2.4.20-pre7-bcnt/net/bridge/br_forward.c Thu Oct 17 20:31:28 2002
117 @@ -49,6 +49,9 @@ static int __br_forward_finish(struct sk
118 static void __br_deliver(struct net_bridge_port *to, struct sk_buff *skb)
121 +#ifdef CONFIG_NETFILTER_DEBUG
124 NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
125 __br_forward_finish);
127 --- linux-2.4.20-pre7/net/bridge/br.c Mon Feb 25 20:38:14 2002
128 +++ linux-2.4.20-pre7-bcnt/net/bridge/br.c Thu Oct 17 20:31:28 2002
130 #include "../atm/lec.h"
133 +int (*br_should_route_hook) (struct sk_buff **pskb) = NULL;
135 void br_dec_use_count()
138 @@ -74,7 +76,7 @@ static void __exit br_deinit(void)
143 +EXPORT_SYMBOL(br_should_route_hook);
146 module_exit(br_deinit)
147 --- linux-2.4.20-pre7/net/bridge/Makefile Fri Dec 29 23:07:24 2000
148 +++ linux-2.4.20-pre7-bcnt/net/bridge/Makefile Thu Oct 17 20:31:28 2002
151 # Note 2! The CFLAGS definition is now in the main makefile...
156 obj-y := br.o br_device.o br_fdb.o br_forward.o br_if.o br_input.o \
157 br_ioctl.o br_notify.o br_stp.o br_stp_bpdu.o \
158 --- linux-2.4.20-pre7/include/linux/netfilter_bridge.h Tue Jun 12 04:15:27 2001
159 +++ linux-2.4.20-pre7-bcnt/include/linux/netfilter_bridge.h Thu Oct 17 22:40:51 2002
161 #define NF_BR_LOCAL_OUT 3
162 /* Packets about to hit the wire. */
163 #define NF_BR_POST_ROUTING 4
164 -#define NF_BR_NUMHOOKS 5
165 +/* Not really a hook, but used for the ebtables broute table */
166 +#define NF_BR_BROUTING 5
167 +#define NF_BR_NUMHOOKS 6
169 +enum nf_br_hook_priorities {
170 + NF_BR_PRI_FIRST = INT_MIN,
171 + NF_BR_PRI_FILTER_BRIDGED = -200,
172 + NF_BR_PRI_FILTER_OTHER = 200,
173 + NF_BR_PRI_NAT_DST_BRIDGED = -300,
174 + NF_BR_PRI_NAT_DST_OTHER = 100,
175 + NF_BR_PRI_NAT_SRC = 300,
176 + NF_BR_PRI_LAST = INT_MAX,
180 --- linux-2.4.20-pre7/net/Makefile Sat Aug 3 02:39:46 2002
181 +++ linux-2.4.20-pre7-bcnt/net/Makefile Thu Oct 17 20:31:29 2002
184 O_TARGET := network.o
186 -mod-subdirs := ipv4/netfilter ipv6/netfilter ipx irda bluetooth atm netlink sched core
187 +mod-subdirs := ipv4/netfilter ipv6/netfilter bridge/netfilter ipx irda \
188 + bluetooth atm netlink sched core
189 export-objs := netsyms.o
191 subdir-y := core ethernet
192 @@ -23,6 +24,12 @@ subdir-$(CONFIG_IPV6) += ipv6
193 ifneq ($(CONFIG_IPV6),n)
194 ifneq ($(CONFIG_IPV6),)
195 subdir-$(CONFIG_NETFILTER) += ipv6/netfilter
199 +ifneq ($(CONFIG_BRIDGE),n)
200 +ifneq ($(CONFIG_BRIDGE),)
201 +subdir-$(CONFIG_BRIDGE) += bridge/netfilter
205 --- linux-2.4.20-pre7/net/Config.in Sat Aug 3 02:39:46 2002
206 +++ linux-2.4.20-pre7-bcnt/net/Config.in Thu Oct 17 20:31:29 2002
207 @@ -65,6 +65,9 @@ if [ "$CONFIG_DECNET" != "n" ]; then
208 source net/decnet/Config.in
210 dep_tristate '802.1d Ethernet Bridging' CONFIG_BRIDGE $CONFIG_INET
211 +if [ "$CONFIG_BRIDGE" != "n" -a "$CONFIG_NETFILTER" != "n" ]; then
212 + source net/bridge/netfilter/Config.in
214 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
215 tristate 'CCITT X.25 Packet Layer (EXPERIMENTAL)' CONFIG_X25
216 tristate 'LAPB Data Link Driver (EXPERIMENTAL)' CONFIG_LAPB
217 --- /dev/null Thu Aug 24 11:00:32 2000
218 +++ linux-2.4.20-pre7-bcnt/net/bridge/netfilter/Makefile Thu Oct 17 20:31:29 2002
221 +# Makefile for the netfilter modules on top of bridging.
223 +# Note! Dependencies are done automagically by 'make dep', which also
224 +# removes any old dependencies. DON'T put your own dependencies here
225 +# unless it's something special (ie not a .c file).
227 +# Note 2! The CFLAGS definition is now in the main makefile...
229 +O_TARGET := netfilter.o
231 +export-objs := ebtables.o
233 +obj-$(CONFIG_BRIDGE_NF_EBTABLES) += ebtables.o
234 +obj-$(CONFIG_BRIDGE_EBT_T_FILTER) += ebtable_filter.o
235 +obj-$(CONFIG_BRIDGE_EBT_T_NAT) += ebtable_nat.o
236 +obj-$(CONFIG_BRIDGE_EBT_BROUTE) += ebtable_broute.o
237 +obj-$(CONFIG_BRIDGE_EBT_IPF) += ebt_ip.o
238 +obj-$(CONFIG_BRIDGE_EBT_ARPF) += ebt_arp.o
239 +obj-$(CONFIG_BRIDGE_EBT_VLANF) += ebt_vlan.o
240 +obj-$(CONFIG_BRIDGE_EBT_MARKF) += ebt_mark_m.o
241 +obj-$(CONFIG_BRIDGE_EBT_LOG) += ebt_log.o
242 +obj-$(CONFIG_BRIDGE_EBT_SNAT) += ebt_snat.o
243 +obj-$(CONFIG_BRIDGE_EBT_DNAT) += ebt_dnat.o
244 +obj-$(CONFIG_BRIDGE_EBT_REDIRECT) += ebt_redirect.o
245 +obj-$(CONFIG_BRIDGE_EBT_MARK_T) += ebt_mark.o
246 +include $(TOPDIR)/Rules.make
247 --- /dev/null Thu Aug 24 11:00:32 2000
248 +++ linux-2.4.20-pre7-bcnt/net/bridge/netfilter/Config.in Thu Oct 17 20:31:29 2002
251 +# Bridge netfilter configuration
253 +dep_tristate ' Bridge: ebtables' CONFIG_BRIDGE_NF_EBTABLES $CONFIG_BRIDGE
254 +dep_tristate ' ebt: filter table support' CONFIG_BRIDGE_EBT_T_FILTER $CONFIG_BRIDGE_NF_EBTABLES
255 +dep_tristate ' ebt: nat table support' CONFIG_BRIDGE_EBT_T_NAT $CONFIG_BRIDGE_NF_EBTABLES
256 +dep_tristate ' ebt: broute table support' CONFIG_BRIDGE_EBT_BROUTE $CONFIG_BRIDGE_NF_EBTABLES
257 +dep_tristate ' ebt: log support' CONFIG_BRIDGE_EBT_LOG $CONFIG_BRIDGE_NF_EBTABLES
258 +dep_tristate ' ebt: IP filter support' CONFIG_BRIDGE_EBT_IPF $CONFIG_BRIDGE_NF_EBTABLES
259 +dep_tristate ' ebt: ARP filter support' CONFIG_BRIDGE_EBT_ARPF $CONFIG_BRIDGE_NF_EBTABLES
260 +dep_tristate ' ebt: 802.1Q VLAN filter support (EXPERIMENTAL)' CONFIG_BRIDGE_EBT_VLANF $CONFIG_BRIDGE_NF_EBTABLES
261 +dep_tristate ' ebt: mark filter support' CONFIG_BRIDGE_EBT_MARKF $CONFIG_BRIDGE_NF_EBTABLES
262 +dep_tristate ' ebt: snat target support' CONFIG_BRIDGE_EBT_SNAT $CONFIG_BRIDGE_NF_EBTABLES
263 +dep_tristate ' ebt: dnat target support' CONFIG_BRIDGE_EBT_DNAT $CONFIG_BRIDGE_NF_EBTABLES
264 +dep_tristate ' ebt: redirect target support' CONFIG_BRIDGE_EBT_REDIRECT $CONFIG_BRIDGE_NF_EBTABLES
265 +dep_tristate ' ebt: mark target support' CONFIG_BRIDGE_EBT_MARK_T $CONFIG_BRIDGE_NF_EBTABLES
266 --- /dev/null Thu Aug 24 11:00:32 2000
267 +++ linux-2.4.20-pre7-bcnt/net/bridge/netfilter/ebtable_filter.c Thu Oct 17 20:31:29 2002
273 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
279 +#include <linux/netfilter_bridge/ebtables.h>
280 +#include <linux/module.h>
282 +#define FILTER_VALID_HOOKS ((1 << NF_BR_LOCAL_IN) | (1 << NF_BR_FORWARD) | \
283 + (1 << NF_BR_LOCAL_OUT))
285 +static struct ebt_entries initial_chains[] =
287 + {0, "INPUT", 0, EBT_ACCEPT, 0},
288 + {0, "FORWARD", 0, EBT_ACCEPT, 0},
289 + {0, "OUTPUT", 0, EBT_ACCEPT, 0}
292 +static struct ebt_replace initial_table =
294 + "filter", FILTER_VALID_HOOKS, 0, 3 * sizeof(struct ebt_entries),
295 + { [NF_BR_LOCAL_IN]&initial_chains[0], [NF_BR_FORWARD]&initial_chains[1],
296 + [NF_BR_LOCAL_OUT]&initial_chains[2] }, 0, NULL, (char *)initial_chains
299 +static int check(const struct ebt_table_info *info, unsigned int valid_hooks)
301 + if (valid_hooks & ~FILTER_VALID_HOOKS)
306 +static struct ebt_table frame_filter =
308 + {NULL, NULL}, "filter", &initial_table, FILTER_VALID_HOOKS,
309 + RW_LOCK_UNLOCKED, check, NULL
313 +ebt_hook (unsigned int hook, struct sk_buff **pskb, const struct net_device *in,
314 + const struct net_device *out, int (*okfn)(struct sk_buff *))
316 + return ebt_do_table(hook, pskb, in, out, &frame_filter);
319 +static struct nf_hook_ops ebt_ops_filter[] = {
320 + { { NULL, NULL }, ebt_hook, PF_BRIDGE, NF_BR_LOCAL_IN,
321 + NF_BR_PRI_FILTER_BRIDGED},
322 + { { NULL, NULL }, ebt_hook, PF_BRIDGE, NF_BR_FORWARD,
323 + NF_BR_PRI_FILTER_BRIDGED},
324 + { { NULL, NULL }, ebt_hook, PF_BRIDGE, NF_BR_LOCAL_OUT,
325 + NF_BR_PRI_FILTER_OTHER}
328 +static int __init init(void)
332 + ret = ebt_register_table(&frame_filter);
335 + for (i = 0; i < sizeof(ebt_ops_filter) / sizeof(ebt_ops_filter[0]); i++)
336 + if ((ret = nf_register_hook(&ebt_ops_filter[i])) < 0)
340 + for (j = 0; j < i; j++)
341 + nf_unregister_hook(&ebt_ops_filter[j]);
342 + ebt_unregister_table(&frame_filter);
346 +static void __exit fini(void)
350 + for (i = 0; i < sizeof(ebt_ops_filter) / sizeof(ebt_ops_filter[0]); i++)
351 + nf_unregister_hook(&ebt_ops_filter[i]);
352 + ebt_unregister_table(&frame_filter);
358 +MODULE_LICENSE("GPL");
359 --- /dev/null Thu Aug 24 11:00:32 2000
360 +++ linux-2.4.20-pre7-bcnt/net/bridge/netfilter/ebtable_nat.c Thu Oct 17 20:31:29 2002
366 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
372 +#include <linux/netfilter_bridge/ebtables.h>
373 +#include <linux/module.h>
374 +#define NAT_VALID_HOOKS ((1 << NF_BR_PRE_ROUTING) | (1 << NF_BR_LOCAL_OUT) | \
375 + (1 << NF_BR_POST_ROUTING))
377 +static struct ebt_entries initial_chains[] =
379 + {0, "PREROUTING", 0, EBT_ACCEPT, 0},
380 + {0, "OUTPUT", 0, EBT_ACCEPT, 0},
381 + {0, "POSTROUTING", 0, EBT_ACCEPT, 0}
384 +static struct ebt_replace initial_table =
386 + "nat", NAT_VALID_HOOKS, 0, 3 * sizeof(struct ebt_entries),
387 + { [NF_BR_PRE_ROUTING]&initial_chains[0], [NF_BR_LOCAL_OUT]&initial_chains[1],
388 + [NF_BR_POST_ROUTING]&initial_chains[2] }, 0, NULL, (char *)initial_chains
391 +static int check(const struct ebt_table_info *info, unsigned int valid_hooks)
393 + if (valid_hooks & ~NAT_VALID_HOOKS)
398 +static struct ebt_table frame_nat =
400 + {NULL, NULL}, "nat", &initial_table, NAT_VALID_HOOKS,
401 + RW_LOCK_UNLOCKED, check, NULL
405 +ebt_nat_dst(unsigned int hook, struct sk_buff **pskb, const struct net_device *in
406 + , const struct net_device *out, int (*okfn)(struct sk_buff *))
408 + return ebt_do_table(hook, pskb, in, out, &frame_nat);
412 +ebt_nat_src(unsigned int hook, struct sk_buff **pskb, const struct net_device *in
413 + , const struct net_device *out, int (*okfn)(struct sk_buff *))
415 + return ebt_do_table(hook, pskb, in, out, &frame_nat);
418 +static struct nf_hook_ops ebt_ops_nat[] = {
419 + { { NULL, NULL }, ebt_nat_dst, PF_BRIDGE, NF_BR_LOCAL_OUT,
420 + NF_BR_PRI_NAT_DST_OTHER},
421 + { { NULL, NULL }, ebt_nat_src, PF_BRIDGE, NF_BR_POST_ROUTING,
422 + NF_BR_PRI_NAT_SRC},
423 + { { NULL, NULL }, ebt_nat_dst, PF_BRIDGE, NF_BR_PRE_ROUTING,
424 + NF_BR_PRI_NAT_DST_BRIDGED},
427 +static int __init init(void)
431 + ret = ebt_register_table(&frame_nat);
434 + for (i = 0; i < sizeof(ebt_ops_nat) / sizeof(ebt_ops_nat[0]); i++)
435 + if ((ret = nf_register_hook(&ebt_ops_nat[i])) < 0)
439 + for (j = 0; j < i; j++)
440 + nf_unregister_hook(&ebt_ops_nat[j]);
441 + ebt_unregister_table(&frame_nat);
445 +static void __exit fini(void)
449 + for (i = 0; i < sizeof(ebt_ops_nat) / sizeof(ebt_ops_nat[0]); i++)
450 + nf_unregister_hook(&ebt_ops_nat[i]);
451 + ebt_unregister_table(&frame_nat);
457 +MODULE_LICENSE("GPL");
458 --- /dev/null Thu Aug 24 11:00:32 2000
459 +++ linux-2.4.20-pre7-bcnt/net/bridge/netfilter/ebtable_broute.c Thu Oct 17 20:31:29 2002
465 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
469 + * This table lets you choose between routing and bridging for frames
470 + * entering on a bridge enslaved nic. This table is traversed before any
471 + * other ebtables table. See net/bridge/br_input.c.
474 +#include <linux/netfilter_bridge/ebtables.h>
475 +#include <linux/module.h>
476 +#include <linux/if_bridge.h>
477 +#include <linux/brlock.h>
479 +// EBT_ACCEPT means the frame will be bridged
480 +// EBT_DROP means the frame will be routed
481 +static struct ebt_entries initial_chain =
482 + {0, "BROUTING", 0, EBT_ACCEPT, 0};
484 +static struct ebt_replace initial_table =
486 + "broute", 1 << NF_BR_BROUTING, 0, sizeof(struct ebt_entries),
487 + { [NF_BR_BROUTING]&initial_chain}, 0, NULL, (char *)&initial_chain
490 +static int check(const struct ebt_table_info *info, unsigned int valid_hooks)
492 + if (valid_hooks & ~(1 << NF_BR_BROUTING))
497 +static struct ebt_table broute_table =
499 + {NULL, NULL}, "broute", &initial_table, 1 << NF_BR_BROUTING,
500 + RW_LOCK_UNLOCKED, check, NULL
503 +static int ebt_broute(struct sk_buff **pskb)
507 + ret = ebt_do_table(NF_BR_BROUTING, pskb, (*pskb)->dev, NULL,
509 + if (ret == NF_DROP)
510 + return 1; // route it
511 + return 0; // bridge it
514 +static int __init init(void)
518 + ret = ebt_register_table(&broute_table);
521 + br_write_lock_bh(BR_NETPROTO_LOCK);
523 + br_should_route_hook = ebt_broute;
524 + br_write_unlock_bh(BR_NETPROTO_LOCK);
528 +static void __exit fini(void)
530 + br_write_lock_bh(BR_NETPROTO_LOCK);
531 + br_should_route_hook = NULL;
532 + br_write_unlock_bh(BR_NETPROTO_LOCK);
533 + ebt_unregister_table(&broute_table);
539 +MODULE_LICENSE("GPL");
540 --- /dev/null Thu Aug 24 11:00:32 2000
541 +++ linux-2.4.20-pre7-bcnt/net/bridge/netfilter/ebt_mark.c Thu Oct 17 20:31:29 2002
547 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
553 +// The mark target can be used in any chain
554 +// I believe adding a mangle table just for marking is total overkill
555 +// Marking a frame doesn't really change anything in the frame anyway
557 +#include <linux/netfilter_bridge/ebtables.h>
558 +#include <linux/netfilter_bridge/ebt_mark_t.h>
559 +#include <linux/module.h>
561 +static int ebt_target_mark(struct sk_buff **pskb, unsigned int hooknr,
562 + const struct net_device *in, const struct net_device *out,
563 + const void *data, unsigned int datalen)
565 + struct ebt_mark_t_info *info = (struct ebt_mark_t_info *)data;
567 + if ((*pskb)->nfmark != info->mark) {
568 + (*pskb)->nfmark = info->mark;
569 + (*pskb)->nfcache |= NFC_ALTERED;
571 + return info->target;
574 +static int ebt_target_mark_check(const char *tablename, unsigned int hookmask,
575 + const struct ebt_entry *e, void *data, unsigned int datalen)
577 + struct ebt_mark_t_info *info = (struct ebt_mark_t_info *)data;
579 + if (datalen != sizeof(struct ebt_mark_t_info))
581 + if (BASE_CHAIN && info->target == EBT_RETURN)
583 + CLEAR_BASE_CHAIN_BIT;
584 + if (INVALID_TARGET)
589 +static struct ebt_target mark_target =
591 + {NULL, NULL}, EBT_MARK_TARGET, ebt_target_mark,
592 + ebt_target_mark_check, NULL, THIS_MODULE
595 +static int __init init(void)
597 + return ebt_register_target(&mark_target);
600 +static void __exit fini(void)
602 + ebt_unregister_target(&mark_target);
608 +MODULE_LICENSE("GPL");
609 --- /dev/null Thu Aug 24 11:00:32 2000
610 +++ linux-2.4.20-pre7-bcnt/net/bridge/netfilter/ebt_mark_m.c Thu Oct 17 20:31:29 2002
616 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
622 +#include <linux/netfilter_bridge/ebtables.h>
623 +#include <linux/netfilter_bridge/ebt_mark_m.h>
624 +#include <linux/module.h>
626 +static int ebt_filter_mark(const struct sk_buff *skb,
627 + const struct net_device *in, const struct net_device *out, const void *data,
628 + unsigned int datalen)
630 + struct ebt_mark_m_info *info = (struct ebt_mark_m_info *) data;
632 + if (info->bitmask & EBT_MARK_OR)
633 + return !(!!(skb->nfmark & info->mask) ^ info->invert);
634 + return !(((skb->nfmark & info->mask) == info->mark) ^ info->invert);
637 +static int ebt_mark_check(const char *tablename, unsigned int hookmask,
638 + const struct ebt_entry *e, void *data, unsigned int datalen)
640 + struct ebt_mark_m_info *info = (struct ebt_mark_m_info *) data;
642 + if (datalen != sizeof(struct ebt_mark_m_info))
644 + if (info->bitmask & ~EBT_MARK_MASK)
646 + if ((info->bitmask & EBT_MARK_OR) && (info->bitmask & EBT_MARK_AND))
648 + if (!info->bitmask)
653 +static struct ebt_match filter_mark =
655 + {NULL, NULL}, EBT_MARK_MATCH, ebt_filter_mark, ebt_mark_check, NULL,
659 +static int __init init(void)
661 + return ebt_register_match(&filter_mark);
664 +static void __exit fini(void)
666 + ebt_unregister_match(&filter_mark);
672 +MODULE_LICENSE("GPL");
673 --- /dev/null Thu Aug 24 11:00:32 2000
674 +++ linux-2.4.20-pre7-bcnt/net/bridge/netfilter/ebt_redirect.c Thu Oct 17 20:31:29 2002
680 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
686 +#include <linux/netfilter_bridge/ebtables.h>
687 +#include <linux/netfilter_bridge/ebt_redirect.h>
688 +#include <linux/module.h>
689 +#include <net/sock.h>
690 +#include "../br_private.h"
692 +static int ebt_target_redirect(struct sk_buff **pskb, unsigned int hooknr,
693 + const struct net_device *in, const struct net_device *out,
694 + const void *data, unsigned int datalen)
696 + struct ebt_redirect_info *info = (struct ebt_redirect_info *)data;
698 + if (hooknr != NF_BR_BROUTING)
699 + memcpy((**pskb).mac.ethernet->h_dest,
700 + in->br_port->br->dev.dev_addr, ETH_ALEN);
702 + memcpy((**pskb).mac.ethernet->h_dest,
703 + in->dev_addr, ETH_ALEN);
704 + (*pskb)->pkt_type = PACKET_HOST;
706 + return info->target;
709 +static int ebt_target_redirect_check(const char *tablename, unsigned int hookmask,
710 + const struct ebt_entry *e, void *data, unsigned int datalen)
712 + struct ebt_redirect_info *info = (struct ebt_redirect_info *)data;
714 + if (datalen != sizeof(struct ebt_redirect_info))
716 + if (BASE_CHAIN && info->target == EBT_RETURN)
718 + CLEAR_BASE_CHAIN_BIT;
719 + if ( (strcmp(tablename, "nat") || hookmask & ~(1 << NF_BR_PRE_ROUTING)) &&
720 + (strcmp(tablename, "broute") || hookmask & ~(1 << NF_BR_BROUTING)) )
722 + if (INVALID_TARGET)
727 +static struct ebt_target redirect_target =
729 + {NULL, NULL}, EBT_REDIRECT_TARGET, ebt_target_redirect,
730 + ebt_target_redirect_check, NULL, THIS_MODULE
733 +static int __init init(void)
735 + return ebt_register_target(&redirect_target);
738 +static void __exit fini(void)
740 + ebt_unregister_target(&redirect_target);
746 +MODULE_LICENSE("GPL");
747 --- /dev/null Thu Aug 24 11:00:32 2000
748 +++ linux-2.4.20-pre7-bcnt/net/bridge/netfilter/ebt_arp.c Thu Oct 17 20:31:29 2002
754 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
755 + * Tim Gardner <timg@tpi.com>
761 +#include <linux/netfilter_bridge/ebtables.h>
762 +#include <linux/netfilter_bridge/ebt_arp.h>
763 +#include <linux/if_arp.h>
764 +#include <linux/module.h>
766 +static int ebt_filter_arp(const struct sk_buff *skb, const struct net_device *in,
767 + const struct net_device *out, const void *data, unsigned int datalen)
769 + struct ebt_arp_info *info = (struct ebt_arp_info *)data;
771 + if (info->bitmask & EBT_ARP_OPCODE && FWINV(info->opcode !=
772 + ((*skb).nh.arph)->ar_op, EBT_ARP_OPCODE))
773 + return EBT_NOMATCH;
774 + if (info->bitmask & EBT_ARP_HTYPE && FWINV(info->htype !=
775 + ((*skb).nh.arph)->ar_hrd, EBT_ARP_HTYPE))
776 + return EBT_NOMATCH;
777 + if (info->bitmask & EBT_ARP_PTYPE && FWINV(info->ptype !=
778 + ((*skb).nh.arph)->ar_pro, EBT_ARP_PTYPE))
779 + return EBT_NOMATCH;
781 + if (info->bitmask & (EBT_ARP_SRC_IP | EBT_ARP_DST_IP))
783 + uint32_t arp_len = sizeof(struct arphdr) +
784 + (2 * (((*skb).nh.arph)->ar_hln)) +
785 + (2 * (((*skb).nh.arph)->ar_pln));
789 + // Make sure the packet is long enough.
790 + if ((((*skb).nh.raw) + arp_len) > (*skb).tail)
791 + return EBT_NOMATCH;
792 + // IPv4 addresses are always 4 bytes.
793 + if (((*skb).nh.arph)->ar_pln != sizeof(uint32_t))
794 + return EBT_NOMATCH;
796 + if (info->bitmask & EBT_ARP_SRC_IP) {
797 + memcpy(&src, ((*skb).nh.raw) + sizeof(struct arphdr) +
798 + ((*skb).nh.arph)->ar_hln, sizeof(uint32_t));
799 + if (FWINV(info->saddr != (src & info->smsk),
801 + return EBT_NOMATCH;
804 + if (info->bitmask & EBT_ARP_DST_IP) {
805 + memcpy(&dst, ((*skb).nh.raw)+sizeof(struct arphdr) +
806 + (2*(((*skb).nh.arph)->ar_hln)) +
807 + (((*skb).nh.arph)->ar_pln), sizeof(uint32_t));
808 + if (FWINV(info->daddr != (dst & info->dmsk),
810 + return EBT_NOMATCH;
816 +static int ebt_arp_check(const char *tablename, unsigned int hookmask,
817 + const struct ebt_entry *e, void *data, unsigned int datalen)
819 + struct ebt_arp_info *info = (struct ebt_arp_info *)data;
821 + if (datalen != sizeof(struct ebt_arp_info))
823 + if ((e->ethproto != __constant_htons(ETH_P_ARP) &&
824 + e->ethproto != __constant_htons(ETH_P_RARP)) ||
825 + e->invflags & EBT_IPROTO)
827 + if (info->bitmask & ~EBT_ARP_MASK || info->invflags & ~EBT_ARP_MASK)
832 +static struct ebt_match filter_arp =
834 + {NULL, NULL}, EBT_ARP_MATCH, ebt_filter_arp, ebt_arp_check, NULL,
838 +static int __init init(void)
840 + return ebt_register_match(&filter_arp);
843 +static void __exit fini(void)
845 + ebt_unregister_match(&filter_arp);
851 +MODULE_LICENSE("GPL");
852 --- /dev/null Thu Aug 24 11:00:32 2000
853 +++ linux-2.4.20-pre7-bcnt/net/bridge/netfilter/ebt_ip.c Thu Oct 17 23:22:58 2002
859 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
864 + * added ip-sport and ip-dport
865 + * Innominate Security Technologies AG <mhopf@innominate.com>
869 +#include <linux/netfilter_bridge/ebtables.h>
870 +#include <linux/netfilter_bridge/ebt_ip.h>
871 +#include <linux/ip.h>
872 +#include <linux/in.h>
873 +#include <linux/module.h>
881 + unsigned char *raw;
882 + struct tcpudphdr *tuh;
885 +static int ebt_filter_ip(const struct sk_buff *skb, const struct net_device *in,
886 + const struct net_device *out, const void *data,
887 + unsigned int datalen)
889 + struct ebt_ip_info *info = (struct ebt_ip_info *)data;
891 + if (info->bitmask & EBT_IP_TOS &&
892 + FWINV(info->tos != ((*skb).nh.iph)->tos, EBT_IP_TOS))
893 + return EBT_NOMATCH;
894 + if (info->bitmask & EBT_IP_PROTO) {
895 + if (FWINV(info->protocol != ((*skb).nh.iph)->protocol,
897 + return EBT_NOMATCH;
898 + if ( info->protocol == IPPROTO_TCP ||
899 + info->protocol == IPPROTO_UDP )
902 + h.raw = skb->data + skb->nh.iph->ihl*4;
903 + if (info->bitmask & EBT_IP_DPORT) {
904 + uint16_t port = ntohs(h.tuh->dst);
905 + if (FWINV(port < info->dport[0] ||
906 + port > info->dport[1],
908 + return EBT_NOMATCH;
910 + if (info->bitmask & EBT_IP_SPORT) {
911 + uint16_t port = ntohs(h.tuh->src);
912 + if (FWINV(port < info->sport[0] ||
913 + port > info->sport[1],
915 + return EBT_NOMATCH;
919 + if (info->bitmask & EBT_IP_SOURCE &&
920 + FWINV((((*skb).nh.iph)->saddr & info->smsk) !=
921 + info->saddr, EBT_IP_SOURCE))
922 + return EBT_NOMATCH;
923 + if ((info->bitmask & EBT_IP_DEST) &&
924 + FWINV((((*skb).nh.iph)->daddr & info->dmsk) !=
925 + info->daddr, EBT_IP_DEST))
926 + return EBT_NOMATCH;
930 +static int ebt_ip_check(const char *tablename, unsigned int hookmask,
931 + const struct ebt_entry *e, void *data, unsigned int datalen)
933 + struct ebt_ip_info *info = (struct ebt_ip_info *)data;
935 + if (datalen != sizeof(struct ebt_ip_info))
937 + if (e->ethproto != __constant_htons(ETH_P_IP) ||
938 + e->invflags & EBT_IPROTO)
940 + if (info->bitmask & ~EBT_IP_MASK || info->invflags & ~EBT_IP_MASK)
942 + if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) {
943 + if (!info->bitmask & EBT_IPROTO)
945 + if (info->protocol != IPPROTO_TCP &&
946 + info->protocol != IPPROTO_UDP)
949 + if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1])
951 + if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1])
956 +static struct ebt_match filter_ip =
958 + {NULL, NULL}, EBT_IP_MATCH, ebt_filter_ip, ebt_ip_check, NULL,
962 +static int __init init(void)
964 + return ebt_register_match(&filter_ip);
967 +static void __exit fini(void)
969 + ebt_unregister_match(&filter_ip);
975 +MODULE_LICENSE("GPL");
976 --- /dev/null Thu Aug 24 11:00:32 2000
977 +++ linux-2.4.20-pre7-bcnt/net/bridge/netfilter/ebt_vlan.c Thu Oct 17 20:31:29 2002
980 + * Description: EBTables 802.1Q match extension kernelspace module.
981 + * Authors: Nick Fedchik <nick@fedchik.org.ua>
982 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
984 + * This program is free software; you can redistribute it and/or modify
985 + * it under the terms of the GNU General Public License as published by
986 + * the Free Software Foundation; either version 2 of the License, or
987 + * (at your option) any later version.
989 + * This program is distributed in the hope that it will be useful,
990 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
991 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
992 + * GNU General Public License for more details.
994 + * You should have received a copy of the GNU General Public License
995 + * along with this program; if not, write to the Free Software
996 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
999 +#include <linux/if_ether.h>
1000 +#include <linux/if_vlan.h>
1001 +#include <linux/module.h>
1002 +#include <linux/netfilter_bridge/ebtables.h>
1003 +#include <linux/netfilter_bridge/ebt_vlan.h>
1005 +static unsigned char debug;
1006 +#define MODULE_VERSION "0.4 (" __DATE__ " " __TIME__ ")"
1008 +MODULE_PARM (debug, "0-1b");
1009 +MODULE_PARM_DESC (debug, "debug=1 is turn on debug messages");
1010 +MODULE_AUTHOR ("Nick Fedchik <nick@fedchik.org.ua>");
1011 +MODULE_DESCRIPTION ("802.1Q match module (ebtables extension), v"
1013 +MODULE_LICENSE ("GPL");
1016 +#define DEBUG_MSG(...) if (debug) printk (KERN_DEBUG __FILE__ ":" __VA_ARGS__)
1017 +#define INV_FLAG(_inv_flag_) (info->invflags & _inv_flag_) ? "!" : ""
1018 +#define GET_BITMASK(_BIT_MASK_) info->bitmask & _BIT_MASK_
1019 +#define SET_BITMASK(_BIT_MASK_) info->bitmask |= _BIT_MASK_
1020 +#define EXIT_ON_MISMATCH(_MATCH_,_MASK_) if (!((info->_MATCH_ == _MATCH_)^!!(info->invflags & _MASK_))) return 1;
1023 + * Function description: ebt_filter_vlan() is main engine for
1024 + * checking passed 802.1Q frame according to
1025 + * the passed extension parameters (in the *data buffer)
1026 + * ebt_filter_vlan() is called after successfull check the rule params
1027 + * by ebt_check_vlan() function.
1029 + * const struct sk_buff *skb - pointer to passed ethernet frame buffer
1030 + * const void *data - pointer to passed extension parameters
1031 + * unsigned int datalen - length of passed *data buffer
1032 + * const struct net_device *in -
1033 + * const struct net_device *out -
1034 + * const struct ebt_counter *c -
1035 + * Returned values:
1036 + * 0 - ok (all rule params matched)
1037 + * 1 - miss (rule params not acceptable to the parsed frame)
1040 +ebt_filter_vlan (const struct sk_buff *skb,
1041 + const struct net_device *in,
1042 + const struct net_device *out,
1044 + unsigned int datalen)
1046 + struct ebt_vlan_info *info = (struct ebt_vlan_info *) data; /* userspace data */
1047 + struct vlan_ethhdr *frame = (struct vlan_ethhdr *) skb->mac.raw; /* Passed tagged frame */
1049 + unsigned short TCI; /* Whole TCI, given from parsed frame */
1050 + unsigned short id; /* VLAN ID, given from frame TCI */
1051 + unsigned char prio; /* user_priority, given from frame TCI */
1052 + unsigned short encap; /* VLAN encapsulated Type/Length field, given from orig frame */
1055 + * Tag Control Information (TCI) consists of the following elements:
1056 + * - User_priority. This field allows the tagged frame to carry user_priority
1057 + * information across Bridged LANs in which individual LAN segments may be unable to signal
1058 + * priority information (e.g., 802.3/Ethernet segments).
1059 + * The user_priority field is three bits in length,
1060 + * interpreted as a binary number. The user_priority is therefore
1061 + * capable of representing eight priority levels, 0 through 7.
1062 + * The use and interpretation of this field is defined in ISO/IEC 15802-3.
1063 + * - Canonical Format Indicator (CFI). This field is used,
1064 + * in 802.3/Ethernet, to signal the presence or absence
1065 + * of a RIF field, and, in combination with the Non-canonical Format Indicator (NCFI) carried
1066 + * in the RIF, to signal the bit order of address information carried in the encapsulated
1067 + * frame. The Canonical Format Indicator (CFI) is a single bit flag value.
1068 + * - VLAN Identifier (VID). This field uniquely identifies the VLAN to
1069 + * which the frame belongs. The twelve-bit VLAN Identifier (VID) field
1070 + * uniquely identify the VLAN to which the frame belongs.
1071 + * The VID is encoded as an unsigned binary number.
1073 + TCI = ntohs (frame->h_vlan_TCI);
1076 + encap = frame->h_vlan_encapsulated_proto;
1079 + * First step is to check is null VLAN ID present
1080 + * in the parsed frame
1084 + * Checking VLAN Identifier (VID)
1086 + if (GET_BITMASK (EBT_VLAN_ID)) { /* Is VLAN ID parsed? */
1087 + EXIT_ON_MISMATCH (id, EBT_VLAN_ID);
1089 + ("matched rule id=%s%d for frame id=%d\n",
1090 + INV_FLAG (EBT_VLAN_ID), info->id, id);
1094 + * Checking user_priority
1096 + if (GET_BITMASK (EBT_VLAN_PRIO)) { /* Is VLAN user_priority parsed? */
1097 + EXIT_ON_MISMATCH (prio, EBT_VLAN_PRIO);
1099 + ("matched rule prio=%s%d for frame prio=%d\n",
1100 + INV_FLAG (EBT_VLAN_PRIO), info->prio,
1105 + * Checking Encapsulated Proto (Length/Type) field
1107 + if (GET_BITMASK (EBT_VLAN_ENCAP)) { /* Is VLAN Encap parsed? */
1108 + EXIT_ON_MISMATCH (encap, EBT_VLAN_ENCAP);
1109 + DEBUG_MSG ("matched encap=%s%2.4X for frame encap=%2.4X\n",
1110 + INV_FLAG (EBT_VLAN_ENCAP),
1111 + ntohs (info->encap), ntohs (encap));
1114 + * All possible extension parameters was parsed.
1115 + * If rule never returned by missmatch, then all ok.
1121 + * Function description: ebt_vlan_check() is called when userspace
1122 + * delivers the table to the kernel,
1123 + * and to check that userspace doesn't give a bad table.
1125 + * const char *tablename - table name string
1126 + * unsigned int hooknr - hook number
1127 + * const struct ebt_entry *e - ebtables entry basic set
1128 + * const void *data - pointer to passed extension parameters
1129 + * unsigned int datalen - length of passed *data buffer
1130 + * Returned values:
1131 + * 0 - ok (all delivered rule params are correct)
1132 + * 1 - miss (rule params is out of range, invalid, incompatible, etc.)
1135 +ebt_check_vlan (const char *tablename,
1136 + unsigned int hooknr,
1137 + const struct ebt_entry *e, void *data,
1138 + unsigned int datalen)
1140 + struct ebt_vlan_info *info = (struct ebt_vlan_info *) data;
1143 + * Parameters buffer overflow check
1145 + if (datalen != sizeof (struct ebt_vlan_info)) {
1147 + ("params size %d is not eq to ebt_vlan_info (%d)\n",
1148 + datalen, sizeof (struct ebt_vlan_info));
1153 + * Is it 802.1Q frame checked?
1155 + if (e->ethproto != __constant_htons (ETH_P_8021Q)) {
1156 + DEBUG_MSG ("passed entry proto %2.4X is not 802.1Q (8100)\n",
1157 + (unsigned short) ntohs (e->ethproto));
1162 + * Check for bitmask range
1163 + * True if even one bit is out of mask
1165 + if (info->bitmask & ~EBT_VLAN_MASK) {
1166 + DEBUG_MSG ("bitmask %2X is out of mask (%2X)\n",
1167 + info->bitmask, EBT_VLAN_MASK);
1172 + * Check for inversion flags range
1174 + if (info->invflags & ~EBT_VLAN_MASK) {
1175 + DEBUG_MSG ("inversion flags %2X is out of mask (%2X)\n",
1176 + info->invflags, EBT_VLAN_MASK);
1181 + * Reserved VLAN ID (VID) values
1182 + * -----------------------------
1183 + * 0 - The null VLAN ID. Indicates that the tag header contains only user_priority information;
1184 + * no VLAN identifier is present in the frame. This VID value shall not be
1185 + * configured as a PVID, configured in any Filtering Database entry, or used in any
1186 + * Management operation.
1188 + * 1 - The default Port VID (PVID) value used for classifying frames on ingress through a Bridge
1189 + * Port. The PVID value can be changed by management on a per-Port basis.
1191 + * 0x0FFF - Reserved for implementation use. This VID value shall not be configured as a
1192 + * PVID or transmitted in a tag header.
1194 + * The remaining values of VID are available for general use as VLAN identifiers.
1195 + * A Bridge may implement the ability to support less than the full range of VID values;
1196 + * i.e., for a given implementation,
1197 + * an upper limit, N, is defined for the VID values supported, where N is less than or equal to 4094.
1198 + * All implementations shall support the use of all VID values in the range 0 through their defined maximum
1201 + * For Linux, N = 4094.
1203 + if (GET_BITMASK (EBT_VLAN_ID)) { /* when vlan-id param was spec-ed */
1204 + if (!!info->id) { /* if id!=0 => check vid range */
1205 + if (info->id > 4094) { /* check if id > than (0x0FFE) */
1207 + ("vlan id %d is out of range (1-4094)\n",
1212 + * Note: This is valid VLAN-tagged frame point.
1213 + * Any value of user_priority are acceptable, but could be ignored
1214 + * according to 802.1Q Std.
1218 + * if id=0 (null VLAN ID) => Check for user_priority range
1220 + if (GET_BITMASK (EBT_VLAN_PRIO)) {
1221 + if ((unsigned char) info->prio > 7) {
1223 + ("prio %d is out of range (0-7)\n",
1229 + * Note2: This is valid priority-tagged frame point
1230 + * with null VID field.
1233 + } else { /* VLAN Id not set */
1234 + if (GET_BITMASK (EBT_VLAN_PRIO)) { /* But user_priority is set - abnormal! */
1235 + info->id = 0; /* Set null VID (case for Priority-tagged frames) */
1236 + SET_BITMASK (EBT_VLAN_ID); /* and set id flag */
1240 + * Check for encapsulated proto range - it is possible to be any value for u_short range.
1241 + * When relaying a tagged frame between 802.3/Ethernet MACs,
1242 + * a Bridge may adjust the padding field such that
1243 + * the minimum size of a transmitted tagged frame is 68 octets (7.2).
1244 + * if_ether.h: ETH_ZLEN 60 - Min. octets in frame sans FCS
1246 + if (GET_BITMASK (EBT_VLAN_ENCAP)) {
1247 + if ((unsigned short) ntohs (info->encap) < ETH_ZLEN) {
1249 + ("encap packet length %d is less than minimal %d\n",
1250 + ntohs (info->encap), ETH_ZLEN);
1256 + * Otherwise is all correct
1258 + DEBUG_MSG ("802.1Q tagged frame checked (%s table, %d hook)\n",
1259 + tablename, hooknr);
1263 +static struct ebt_match filter_vlan = {
1273 + * Module initialization function.
1274 + * Called when module is loaded to kernelspace
1276 +static int __init init (void)
1278 + DEBUG_MSG ("ebtables 802.1Q extension module v"
1279 + MODULE_VERSION "\n");
1280 + DEBUG_MSG ("module debug=%d\n", !!debug);
1281 + return ebt_register_match (&filter_vlan);
1285 + * Module "finalization" function
1286 + * Called when download module from kernelspace
1288 +static void __exit fini (void)
1290 + ebt_unregister_match (&filter_vlan);
1293 +module_init (init);
1294 +module_exit (fini);
1297 --- /dev/null Thu Aug 24 11:00:32 2000
1298 +++ linux-2.4.20-pre7-bcnt/net/bridge/netfilter/ebt_log.c Thu Oct 17 20:31:29 2002
1304 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
1310 +#include <linux/netfilter_bridge/ebtables.h>
1311 +#include <linux/netfilter_bridge/ebt_log.h>
1312 +#include <linux/module.h>
1313 +#include <linux/ip.h>
1314 +#include <linux/if_arp.h>
1315 +#include <linux/spinlock.h>
1317 +static spinlock_t ebt_log_lock = SPIN_LOCK_UNLOCKED;
1319 +static int ebt_log_check(const char *tablename, unsigned int hookmask,
1320 + const struct ebt_entry *e, void *data, unsigned int datalen)
1322 + struct ebt_log_info *info = (struct ebt_log_info *)data;
1324 + if (datalen != sizeof(struct ebt_log_info))
1326 + if (info->bitmask & ~EBT_LOG_MASK)
1328 + if (info->loglevel >= 8)
1330 + info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0';
1334 +static void ebt_log(const struct sk_buff *skb, const struct net_device *in,
1335 + const struct net_device *out, const void *data, unsigned int datalen)
1337 + struct ebt_log_info *info = (struct ebt_log_info *)data;
1338 + char level_string[4] = "< >";
1339 + level_string[1] = '0' + info->loglevel;
1341 + spin_lock_bh(&ebt_log_lock);
1342 + printk(level_string);
1343 + printk("%s IN=%s OUT=%s ", info->prefix, in ? in->name : "",
1344 + out ? out->name : "");
1346 + if (skb->dev->hard_header_len) {
1348 + unsigned char *p = (skb->mac.ethernet)->h_source;
1350 + printk("MAC source = ");
1351 + for (i = 0; i < ETH_ALEN; i++,p++)
1352 + printk("%02x%c", *p, i == ETH_ALEN - 1 ? ' ':':');
1353 + printk("MAC dest = ");
1354 + p = (skb->mac.ethernet)->h_dest;
1355 + for (i = 0; i < ETH_ALEN; i++,p++)
1356 + printk("%02x%c", *p, i == ETH_ALEN - 1 ? ' ':':');
1358 + printk("proto = 0x%04x", ntohs(((*skb).mac.ethernet)->h_proto));
1360 + if ((info->bitmask & EBT_LOG_IP) && skb->mac.ethernet->h_proto ==
1362 + struct iphdr *iph = skb->nh.iph;
1363 + printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u,",
1364 + NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
1365 + printk(" IP tos=0x%02X, IP proto=%d", iph->tos, iph->protocol);
1368 + if ((info->bitmask & EBT_LOG_ARP) &&
1369 + ((skb->mac.ethernet->h_proto == __constant_htons(ETH_P_ARP)) ||
1370 + (skb->mac.ethernet->h_proto == __constant_htons(ETH_P_RARP)))) {
1371 + struct arphdr * arph = skb->nh.arph;
1372 + printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
1373 + ntohs(arph->ar_hrd), ntohs(arph->ar_pro),
1374 + ntohs(arph->ar_op));
1377 + spin_unlock_bh(&ebt_log_lock);
1380 +struct ebt_watcher log =
1382 + {NULL, NULL}, EBT_LOG_WATCHER, ebt_log, ebt_log_check, NULL,
1386 +static int __init init(void)
1388 + return ebt_register_watcher(&log);
1391 +static void __exit fini(void)
1393 + ebt_unregister_watcher(&log);
1399 +MODULE_LICENSE("GPL");
1400 --- /dev/null Thu Aug 24 11:00:32 2000
1401 +++ linux-2.4.20-pre7-bcnt/net/bridge/netfilter/ebt_snat.c Thu Oct 17 20:31:29 2002
1407 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
1413 +#include <linux/netfilter_bridge/ebtables.h>
1414 +#include <linux/netfilter_bridge/ebt_nat.h>
1415 +#include <linux/module.h>
1417 +static int ebt_target_snat(struct sk_buff **pskb, unsigned int hooknr,
1418 + const struct net_device *in, const struct net_device *out,
1419 + const void *data, unsigned int datalen)
1421 + struct ebt_nat_info *info = (struct ebt_nat_info *) data;
1423 + memcpy(((**pskb).mac.ethernet)->h_source, info->mac,
1424 + ETH_ALEN * sizeof(unsigned char));
1425 + return info->target;
1428 +static int ebt_target_snat_check(const char *tablename, unsigned int hookmask,
1429 + const struct ebt_entry *e, void *data, unsigned int datalen)
1431 + struct ebt_nat_info *info = (struct ebt_nat_info *) data;
1433 + if (datalen != sizeof(struct ebt_nat_info))
1435 + if (BASE_CHAIN && info->target == EBT_RETURN)
1437 + CLEAR_BASE_CHAIN_BIT;
1438 + if (strcmp(tablename, "nat"))
1440 + if (hookmask & ~(1 << NF_BR_POST_ROUTING))
1442 + if (INVALID_TARGET)
1447 +static struct ebt_target snat =
1449 + {NULL, NULL}, EBT_SNAT_TARGET, ebt_target_snat, ebt_target_snat_check,
1453 +static int __init init(void)
1455 + return ebt_register_target(&snat);
1458 +static void __exit fini(void)
1460 + ebt_unregister_target(&snat);
1466 +MODULE_LICENSE("GPL");
1467 --- /dev/null Thu Aug 24 11:00:32 2000
1468 +++ linux-2.4.20-pre7-bcnt/net/bridge/netfilter/ebt_dnat.c Thu Oct 17 20:31:29 2002
1474 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
1480 +#include <linux/netfilter_bridge/ebtables.h>
1481 +#include <linux/netfilter_bridge/ebt_nat.h>
1482 +#include <linux/module.h>
1483 +#include <net/sock.h>
1485 +static int ebt_target_dnat(struct sk_buff **pskb, unsigned int hooknr,
1486 + const struct net_device *in, const struct net_device *out,
1487 + const void *data, unsigned int datalen)
1489 + struct ebt_nat_info *info = (struct ebt_nat_info *)data;
1491 + memcpy(((**pskb).mac.ethernet)->h_dest, info->mac,
1492 + ETH_ALEN * sizeof(unsigned char));
1493 + return info->target;
1496 +static int ebt_target_dnat_check(const char *tablename, unsigned int hookmask,
1497 + const struct ebt_entry *e, void *data, unsigned int datalen)
1499 + struct ebt_nat_info *info = (struct ebt_nat_info *)data;
1501 + if (BASE_CHAIN && info->target == EBT_RETURN)
1503 + CLEAR_BASE_CHAIN_BIT;
1504 + if ( (strcmp(tablename, "nat") ||
1505 + (hookmask & ~((1 << NF_BR_PRE_ROUTING) | (1 << NF_BR_LOCAL_OUT)))) &&
1506 + (strcmp(tablename, "broute") || hookmask & ~(1 << NF_BR_BROUTING)) )
1508 + if (datalen != sizeof(struct ebt_nat_info))
1510 + if (INVALID_TARGET)
1515 +static struct ebt_target dnat =
1517 + {NULL, NULL}, EBT_DNAT_TARGET, ebt_target_dnat, ebt_target_dnat_check,
1521 +static int __init init(void)
1523 + return ebt_register_target(&dnat);
1526 +static void __exit fini(void)
1528 + ebt_unregister_target(&dnat);
1534 +MODULE_LICENSE("GPL");
1535 --- /dev/null Thu Aug 24 11:00:32 2000
1536 +++ linux-2.4.20-pre7-bcnt/net/bridge/netfilter/ebtables.c Thu Oct 17 21:58:08 2002
1542 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
1544 + * ebtables.c,v 2.0, July, 2002
1546 + * This code is stongly inspired on the iptables code which is
1547 + * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
1549 + * This program is free software; you can redistribute it and/or
1550 + * modify it under the terms of the GNU General Public License
1551 + * as published by the Free Software Foundation; either version
1552 + * 2 of the License, or (at your option) any later version.
1555 +// used for print_string
1556 +#include <linux/sched.h>
1557 +#include <linux/tty.h>
1559 +#include <linux/kmod.h>
1560 +#include <linux/module.h>
1561 +#include <linux/vmalloc.h>
1562 +#include <linux/netfilter_bridge/ebtables.h>
1563 +#include <linux/spinlock.h>
1564 +#include <asm/uaccess.h>
1565 +#include <linux/smp.h>
1566 +#include <net/sock.h>
1567 +// needed for logical [in,out]-dev filtering
1568 +#include "../br_private.h"
1571 +#define ASSERT_READ_LOCK(x)
1572 +#define ASSERT_WRITE_LOCK(x)
1573 +#include <linux/netfilter_ipv4/listhelp.h>
1575 +#if 0 // use this for remote debugging
1576 +// Copyright (C) 1998 by Ori Pomerantz
1577 +// Print the string to the appropriate tty, the one
1578 +// the current task uses
1579 +static void print_string(char *str)
1581 + struct tty_struct *my_tty;
1583 + /* The tty for the current task */
1584 + my_tty = current->tty;
1585 + if (my_tty != NULL) {
1586 + (*(my_tty->driver).write)(my_tty, 0, str, strlen(str));
1587 + (*(my_tty->driver).write)(my_tty, 0, "\015\012", 2);
1591 +#define BUGPRINT(args) print_string(args);
1593 +#define BUGPRINT(format, args...) printk("kernel msg: ebtables bug: please "\
1594 + "report to author: "format, ## args)
1595 +// #define BUGPRINT(format, args...)
1597 +#define MEMPRINT(format, args...) printk("kernel msg: ebtables "\
1598 + ": out of memory: "format, ## args)
1599 +// #define MEMPRINT(format, args...)
1603 +// Each cpu has its own set of counters, so there is no need for write_lock in
1605 +// For reading or updating the counters, the user context needs to
1606 +// get a write_lock
1608 +// The size of each set of counters is altered to get cache alignment
1609 +#define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
1610 +#define COUNTER_OFFSET(n) (SMP_ALIGN(n * sizeof(struct ebt_counter)))
1611 +#define COUNTER_BASE(c, n, cpu) ((struct ebt_counter *)(((char *)c) + \
1612 + COUNTER_OFFSET(n) * cpu))
1616 +static DECLARE_MUTEX(ebt_mutex);
1617 +static LIST_HEAD(ebt_tables);
1618 +static LIST_HEAD(ebt_targets);
1619 +static LIST_HEAD(ebt_matches);
1620 +static LIST_HEAD(ebt_watchers);
1622 +static struct ebt_target ebt_standard_target =
1623 +{ {NULL, NULL}, EBT_STANDARD_TARGET, NULL, NULL, NULL, NULL};
1625 +static inline int ebt_do_watcher (struct ebt_entry_watcher *w,
1626 + const struct sk_buff *skb, const struct net_device *in,
1627 + const struct net_device *out)
1629 + w->u.watcher->watcher(skb, in, out, w->data,
1631 + // watchers don't give a verdict
1635 +static inline int ebt_do_match (struct ebt_entry_match *m,
1636 + const struct sk_buff *skb, const struct net_device *in,
1637 + const struct net_device *out)
1639 + return m->u.match->match(skb, in, out, m->data,
1643 +static inline int ebt_dev_check(char *entry, const struct net_device *device)
1645 + if (*entry == '\0')
1649 + return !!strcmp(entry, device->name);
1652 +#define FWINV2(bool,invflg) ((bool) ^ !!(e->invflags & invflg))
1653 +// process standard matches
1654 +static inline int ebt_basic_match(struct ebt_entry *e, struct ethhdr *h,
1655 + const struct net_device *in, const struct net_device *out)
1659 + if (e->bitmask & EBT_802_3) {
1660 + if (FWINV2(ntohs(h->h_proto) >= 1536, EBT_IPROTO))
1662 + } else if (!(e->bitmask & EBT_NOPROTO) &&
1663 + FWINV2(e->ethproto != h->h_proto, EBT_IPROTO))
1666 + if (FWINV2(ebt_dev_check(e->in, in), EBT_IIN))
1668 + if (FWINV2(ebt_dev_check(e->out, out), EBT_IOUT))
1670 + if ((!in || !in->br_port) ? 0 : FWINV2(ebt_dev_check(
1671 + e->logical_in, &in->br_port->br->dev), EBT_ILOGICALIN))
1673 + if ((!out || !out->br_port) ? 0 : FWINV2(ebt_dev_check(
1674 + e->logical_out, &out->br_port->br->dev), EBT_ILOGICALOUT))
1677 + if (e->bitmask & EBT_SOURCEMAC) {
1679 + for (i = 0; i < 6; i++)
1680 + verdict |= (h->h_source[i] ^ e->sourcemac[i]) &
1682 + if (FWINV2(verdict != 0, EBT_ISOURCE) )
1685 + if (e->bitmask & EBT_DESTMAC) {
1687 + for (i = 0; i < 6; i++)
1688 + verdict |= (h->h_dest[i] ^ e->destmac[i]) &
1690 + if (FWINV2(verdict != 0, EBT_IDEST) )
1696 +// Do some firewalling
1697 +unsigned int ebt_do_table (unsigned int hook, struct sk_buff **pskb,
1698 + const struct net_device *in, const struct net_device *out,
1699 + struct ebt_table *table)
1702 + struct ebt_entry *point;
1703 + struct ebt_counter *counter_base, *cb_base;
1704 + struct ebt_entry_target *t;
1705 + int verdict, sp = 0;
1706 + struct ebt_chainstack *cs;
1707 + struct ebt_entries *chaininfo;
1709 + struct ebt_table_info *private = table->private;
1711 + read_lock_bh(&table->lock);
1712 + cb_base = COUNTER_BASE(private->counters, private->nentries,
1713 + cpu_number_map(smp_processor_id()));
1714 + if (private->chainstack)
1715 + cs = private->chainstack[cpu_number_map(smp_processor_id())];
1718 + chaininfo = private->hook_entry[hook];
1719 + nentries = private->hook_entry[hook]->nentries;
1720 + point = (struct ebt_entry *)(private->hook_entry[hook]->data);
1721 + counter_base = cb_base + private->hook_entry[hook]->counter_offset;
1722 + // base for chain jumps
1723 + base = (char *)chaininfo;
1725 + while (i < nentries) {
1726 + if (ebt_basic_match(point, (**pskb).mac.ethernet, in, out))
1727 + goto letscontinue;
1729 + if (EBT_MATCH_ITERATE(point, ebt_do_match, *pskb, in, out) != 0)
1730 + goto letscontinue;
1732 + // increase counter
1733 + (*(counter_base + i)).pcnt++;
1734 + (*(counter_base + i)).bcnt+=(**pskb).len;
1736 + // these should only watch: not modify, nor tell us
1737 + // what to do with the packet
1738 + EBT_WATCHER_ITERATE(point, ebt_do_watcher, *pskb, in,
1741 + t = (struct ebt_entry_target *)
1742 + (((char *)point) + point->target_offset);
1743 + // standard target
1744 + if (!t->u.target->target)
1745 + verdict = ((struct ebt_standard_target *)t)->verdict;
1747 + verdict = t->u.target->target(pskb, hook,
1748 + in, out, t->data, t->target_size);
1749 + if (verdict == EBT_ACCEPT) {
1750 + read_unlock_bh(&table->lock);
1753 + if (verdict == EBT_DROP) {
1754 + read_unlock_bh(&table->lock);
1757 + if (verdict == EBT_RETURN) {
1759 +#ifdef CONFIG_NETFILTER_DEBUG
1761 + BUGPRINT("RETURN on base chain");
1762 + // act like this is EBT_CONTINUE
1763 + goto letscontinue;
1767 + // put all the local variables right
1769 + chaininfo = cs[sp].chaininfo;
1770 + nentries = chaininfo->nentries;
1772 + counter_base = cb_base +
1773 + chaininfo->counter_offset;
1776 + if (verdict == EBT_CONTINUE)
1777 + goto letscontinue;
1778 +#ifdef CONFIG_NETFILTER_DEBUG
1779 + if (verdict < 0) {
1780 + BUGPRINT("bogus standard verdict\n");
1781 + read_unlock_bh(&table->lock);
1787 + cs[sp].chaininfo = chaininfo;
1788 + cs[sp].e = (struct ebt_entry *)
1789 + (((char *)point) + point->next_offset);
1791 + chaininfo = (struct ebt_entries *) (base + verdict);
1792 +#ifdef CONFIG_NETFILTER_DEBUG
1793 + if (chaininfo->distinguisher) {
1794 + BUGPRINT("jump to non-chain\n");
1795 + read_unlock_bh(&table->lock);
1799 + nentries = chaininfo->nentries;
1800 + point = (struct ebt_entry *)chaininfo->data;
1801 + counter_base = cb_base + chaininfo->counter_offset;
1805 + point = (struct ebt_entry *)
1806 + (((char *)point) + point->next_offset);
1810 + // I actually like this :)
1811 + if (chaininfo->policy == EBT_RETURN)
1813 + if (chaininfo->policy == EBT_ACCEPT) {
1814 + read_unlock_bh(&table->lock);
1817 + read_unlock_bh(&table->lock);
1821 +// If it succeeds, returns element and locks mutex
1822 +static inline void *
1823 +find_inlist_lock_noload(struct list_head *head, const char *name, int *error,
1824 + struct semaphore *mutex)
1828 + *error = down_interruptible(mutex);
1832 + ret = list_named_find(head, name);
1840 +#ifndef CONFIG_KMOD
1841 +#define find_inlist_lock(h,n,p,e,m) find_inlist_lock_noload((h),(n),(e),(m))
1844 +find_inlist_lock(struct list_head *head, const char *name, const char *prefix,
1845 + int *error, struct semaphore *mutex)
1849 + ret = find_inlist_lock_noload(head, name, error, mutex);
1851 + char modulename[EBT_FUNCTION_MAXNAMELEN + strlen(prefix) + 1];
1852 + strcpy(modulename, prefix);
1853 + strcat(modulename, name);
1854 + request_module(modulename);
1855 + ret = find_inlist_lock_noload(head, name, error, mutex);
1861 +static inline struct ebt_table *
1862 +find_table_lock(const char *name, int *error, struct semaphore *mutex)
1864 + return find_inlist_lock(&ebt_tables, name, "ebtable_", error, mutex);
1867 +static inline struct ebt_match *
1868 +find_match_lock(const char *name, int *error, struct semaphore *mutex)
1870 + return find_inlist_lock(&ebt_matches, name, "ebt_", error, mutex);
1873 +static inline struct ebt_watcher *
1874 +find_watcher_lock(const char *name, int *error, struct semaphore *mutex)
1876 + return find_inlist_lock(&ebt_watchers, name, "ebt_", error, mutex);
1879 +static inline struct ebt_target *
1880 +find_target_lock(const char *name, int *error, struct semaphore *mutex)
1882 + return find_inlist_lock(&ebt_targets, name, "ebt_", error, mutex);
1886 +ebt_check_match(struct ebt_entry_match *m, struct ebt_entry *e,
1887 + const char *name, unsigned int hookmask, unsigned int *cnt)
1889 + struct ebt_match *match;
1892 + if (((char *)m) + m->match_size + sizeof(struct ebt_entry_match) >
1893 + ((char *)e) + e->watchers_offset)
1895 + match = find_match_lock(m->u.name, &ret, &ebt_mutex);
1898 + m->u.match = match;
1900 + __MOD_INC_USE_COUNT(match->me);
1902 + if (match->check &&
1903 + match->check(name, hookmask, e, m->data, m->match_size) != 0) {
1904 + BUGPRINT("match->check failed\n");
1906 + __MOD_DEC_USE_COUNT(match->me);
1914 +ebt_check_watcher(struct ebt_entry_watcher *w, struct ebt_entry *e,
1915 + const char *name, unsigned int hookmask, unsigned int *cnt)
1917 + struct ebt_watcher *watcher;
1920 + if (((char *)w) + w->watcher_size + sizeof(struct ebt_entry_watcher) >
1921 + ((char *)e) + e->target_offset)
1923 + watcher = find_watcher_lock(w->u.name, &ret, &ebt_mutex);
1926 + w->u.watcher = watcher;
1928 + __MOD_INC_USE_COUNT(watcher->me);
1930 + if (watcher->check &&
1931 + watcher->check(name, hookmask, e, w->data, w->watcher_size) != 0) {
1932 + BUGPRINT("watcher->check failed\n");
1934 + __MOD_DEC_USE_COUNT(watcher->me);
1941 +// this one is very careful, as it is the first function
1942 +// to parse the userspace data
1944 +ebt_check_entry_size_and_hooks(struct ebt_entry *e,
1945 + struct ebt_table_info *newinfo, char *base, char *limit,
1946 + struct ebt_entries **hook_entries, unsigned int *n, unsigned int *cnt,
1947 + unsigned int *totalcnt, unsigned int *udc_cnt, unsigned int valid_hooks)
1951 + for (i = 0; i < NF_BR_NUMHOOKS; i++) {
1952 + if ((valid_hooks & (1 << i)) == 0)
1954 + if ( (char *)hook_entries[i] - base ==
1955 + (char *)e - newinfo->entries)
1958 + // beginning of a new chain
1959 + // if i == NF_BR_NUMHOOKS it must be a user defined chain
1960 + if (i != NF_BR_NUMHOOKS || !(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
1961 + if ((e->bitmask & EBT_ENTRY_OR_ENTRIES) != 0) {
1962 + // we make userspace set this right,
1963 + // so there is no misunderstanding
1964 + BUGPRINT("EBT_ENTRY_OR_ENTRIES shouldn't be set "
1965 + "in distinguisher\n");
1968 + // this checks if the previous chain has as many entries
1969 + // as it said it has
1971 + BUGPRINT("nentries does not equal the nr of entries "
1972 + "in the chain\n");
1975 + // before we look at the struct, be sure it is not too big
1976 + if ((char *)hook_entries[i] + sizeof(struct ebt_entries)
1978 + BUGPRINT("entries_size too small\n");
1981 + if (((struct ebt_entries *)e)->policy != EBT_DROP &&
1982 + ((struct ebt_entries *)e)->policy != EBT_ACCEPT) {
1983 + // only RETURN from udc
1984 + if (i != NF_BR_NUMHOOKS ||
1985 + ((struct ebt_entries *)e)->policy != EBT_RETURN) {
1986 + BUGPRINT("bad policy\n");
1990 + if (i == NF_BR_NUMHOOKS) // it's a user defined chain
1993 + newinfo->hook_entry[i] = (struct ebt_entries *)e;
1994 + if (((struct ebt_entries *)e)->counter_offset != *totalcnt) {
1995 + BUGPRINT("counter_offset != totalcnt");
1998 + *n = ((struct ebt_entries *)e)->nentries;
2002 + // a plain old entry, heh
2003 + if (sizeof(struct ebt_entry) > e->watchers_offset ||
2004 + e->watchers_offset > e->target_offset ||
2005 + e->target_offset >= e->next_offset) {
2006 + BUGPRINT("entry offsets not in right order\n");
2009 + // this is not checked anywhere else
2010 + if (e->next_offset - e->target_offset < sizeof(struct ebt_entry_target)) {
2011 + BUGPRINT("target size too small\n");
2020 +struct ebt_cl_stack
2022 + struct ebt_chainstack cs;
2024 + unsigned int hookmask;
2027 +// we need these positions to check that the jumps to a different part of the
2028 +// entries is a jump to the beginning of a new chain.
2030 +ebt_get_udc_positions(struct ebt_entry *e, struct ebt_table_info *newinfo,
2031 + struct ebt_entries **hook_entries, unsigned int *n, unsigned int valid_hooks,
2032 + struct ebt_cl_stack *udc)
2036 + // we're only interested in chain starts
2037 + if (e->bitmask & EBT_ENTRY_OR_ENTRIES)
2039 + for (i = 0; i < NF_BR_NUMHOOKS; i++) {
2040 + if ((valid_hooks & (1 << i)) == 0)
2042 + if (newinfo->hook_entry[i] == (struct ebt_entries *)e)
2045 + // only care about udc
2046 + if (i != NF_BR_NUMHOOKS)
2049 + udc[*n].cs.chaininfo = (struct ebt_entries *)e;
2050 + // these initialisations are depended on later in check_chainloops()
2052 + udc[*n].hookmask = 0;
2059 +ebt_cleanup_match(struct ebt_entry_match *m, unsigned int *i)
2061 + if (i && (*i)-- == 0)
2063 + if (m->u.match->destroy)
2064 + m->u.match->destroy(m->data, m->match_size);
2065 + if (m->u.match->me)
2066 + __MOD_DEC_USE_COUNT(m->u.match->me);
2072 +ebt_cleanup_watcher(struct ebt_entry_watcher *w, unsigned int *i)
2074 + if (i && (*i)-- == 0)
2076 + if (w->u.watcher->destroy)
2077 + w->u.watcher->destroy(w->data, w->watcher_size);
2078 + if (w->u.watcher->me)
2079 + __MOD_DEC_USE_COUNT(w->u.watcher->me);
2085 +ebt_cleanup_entry(struct ebt_entry *e, unsigned int *cnt)
2087 + struct ebt_entry_target *t;
2089 + if ((e->bitmask & EBT_ENTRY_OR_ENTRIES) == 0)
2092 + if (cnt && (*cnt)-- == 0)
2094 + EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, NULL);
2095 + EBT_MATCH_ITERATE(e, ebt_cleanup_match, NULL);
2096 + t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
2097 + if (t->u.target->destroy)
2098 + t->u.target->destroy(t->data, t->target_size);
2099 + if (t->u.target->me)
2100 + __MOD_DEC_USE_COUNT(t->u.target->me);
2106 +ebt_check_entry(struct ebt_entry *e, struct ebt_table_info *newinfo,
2107 + const char *name, unsigned int *cnt, unsigned int valid_hooks,
2108 + struct ebt_cl_stack *cl_s, unsigned int udc_cnt)
2110 + struct ebt_entry_target *t;
2111 + struct ebt_target *target;
2112 + unsigned int i, j, hook = 0, hookmask = 0;
2115 + // Don't mess with the struct ebt_entries
2116 + if ((e->bitmask & EBT_ENTRY_OR_ENTRIES) == 0)
2119 + if (e->bitmask & ~EBT_F_MASK) {
2120 + BUGPRINT("Unknown flag for bitmask\n");
2123 + if (e->invflags & ~EBT_INV_MASK) {
2124 + BUGPRINT("Unknown flag for inv bitmask\n");
2127 + if ( (e->bitmask & EBT_NOPROTO) && (e->bitmask & EBT_802_3) ) {
2128 + BUGPRINT("NOPROTO & 802_3 not allowed\n");
2131 + // what hook do we belong to?
2132 + for (i = 0; i < NF_BR_NUMHOOKS; i++) {
2133 + if ((valid_hooks & (1 << i)) == 0)
2135 + if ((char *)newinfo->hook_entry[i] < (char *)e)
2140 + // (1 << NF_BR_NUMHOOKS) tells the check functions the rule is on
2142 + if (i < NF_BR_NUMHOOKS)
2143 + hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
2145 + for (i = 0; i < udc_cnt; i++)
2146 + if ((char *)(cl_s[i].cs.chaininfo) > (char *)e)
2149 + hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
2151 + hookmask = cl_s[i - 1].hookmask;
2154 + ret = EBT_MATCH_ITERATE(e, ebt_check_match, e, name, hookmask, &i);
2156 + goto cleanup_matches;
2158 + ret = EBT_WATCHER_ITERATE(e, ebt_check_watcher, e, name, hookmask, &j);
2160 + goto cleanup_watchers;
2161 + t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
2162 + target = find_target_lock(t->u.name, &ret, &ebt_mutex);
2164 + goto cleanup_watchers;
2166 + __MOD_INC_USE_COUNT(target->me);
2169 + t->u.target = target;
2170 + if (t->u.target == &ebt_standard_target) {
2171 + if (e->target_offset + sizeof(struct ebt_standard_target) >
2173 + BUGPRINT("Standard target size too big\n");
2175 + goto cleanup_watchers;
2177 + if (((struct ebt_standard_target *)t)->verdict <
2178 + -NUM_STANDARD_TARGETS) {
2179 + BUGPRINT("Invalid standard target\n");
2181 + goto cleanup_watchers;
2183 + } else if ((e->target_offset + t->target_size +
2184 + sizeof(struct ebt_entry_target) > e->next_offset) ||
2185 + (t->u.target->check &&
2186 + t->u.target->check(name, hookmask, e, t->data, t->target_size) != 0)){
2187 + if (t->u.target->me)
2188 + __MOD_DEC_USE_COUNT(t->u.target->me);
2190 + goto cleanup_watchers;
2195 + EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, &j);
2197 + EBT_MATCH_ITERATE(e, ebt_cleanup_match, &i);
2201 +// checks for loops and sets the hook mask for udc
2202 +// the hook mask for udc tells us from which base chains the udc can be
2203 +// accessed. This mask is a parameter to the check() functions of the extensions
2204 +int check_chainloops(struct ebt_entries *chain, struct ebt_cl_stack *cl_s,
2205 + unsigned int udc_cnt, unsigned int hooknr, char *base)
2207 + int i, chain_nr = -1, pos = 0, nentries = chain->nentries, verdict;
2208 + struct ebt_entry *e = (struct ebt_entry *)chain->data;
2209 + struct ebt_entry_target *t;
2211 + while (pos < nentries || chain_nr != -1) {
2212 + // end of udc, go back one 'recursion' step
2213 + if (pos == nentries) {
2214 + // put back values of the time when this chain was called
2215 + e = cl_s[chain_nr].cs.e;
2216 + if (cl_s[chain_nr].from != -1)
2218 + cl_s[cl_s[chain_nr].from].cs.chaininfo->nentries;
2220 + nentries = chain->nentries;
2221 + pos = cl_s[chain_nr].cs.n;
2222 + // make sure we won't see a loop that isn't one
2223 + cl_s[chain_nr].cs.n = 0;
2224 + chain_nr = cl_s[chain_nr].from;
2225 + if (pos == nentries)
2228 + t = (struct ebt_entry_target *)
2229 + (((char *)e) + e->target_offset);
2230 + if (strcmp(t->u.name, EBT_STANDARD_TARGET))
2231 + goto letscontinue;
2232 + if (e->target_offset + sizeof(struct ebt_standard_target) >
2234 + BUGPRINT("Standard target size too big\n");
2237 + verdict = ((struct ebt_standard_target *)t)->verdict;
2238 + if (verdict >= 0) { // jump to another chain
2239 + struct ebt_entries *hlp2 =
2240 + (struct ebt_entries *)(base + verdict);
2241 + for (i = 0; i < udc_cnt; i++)
2242 + if (hlp2 == cl_s[i].cs.chaininfo)
2244 + // bad destination or loop
2245 + if (i == udc_cnt) {
2246 + BUGPRINT("bad destination\n");
2249 + if (cl_s[i].cs.n) {
2250 + BUGPRINT("loop\n");
2253 + // this can't be 0, so the above test is correct
2254 + cl_s[i].cs.n = pos + 1;
2256 + cl_s[i].cs.e = ((void *)e + e->next_offset);
2257 + e = (struct ebt_entry *)(hlp2->data);
2258 + nentries = hlp2->nentries;
2259 + cl_s[i].from = chain_nr;
2261 + // this udc is accessible from the base chain for hooknr
2262 + cl_s[i].hookmask |= (1 << hooknr);
2266 + e = (void *)e + e->next_offset;
2272 +// do the parsing of the table/chains/entries/matches/watchers/targets, heh
2273 +static int translate_table(struct ebt_replace *repl,
2274 + struct ebt_table_info *newinfo)
2276 + unsigned int i, j, k, udc_cnt;
2278 + struct ebt_cl_stack *cl_s = NULL; // used in the checking for chain loops
2281 + while (i < NF_BR_NUMHOOKS && !(repl->valid_hooks & (1 << i)))
2283 + if (i == NF_BR_NUMHOOKS) {
2284 + BUGPRINT("No valid hooks specified\n");
2287 + if (repl->hook_entry[i] != (struct ebt_entries *)repl->entries) {
2288 + BUGPRINT("Chains don't start at beginning\n");
2291 + // make sure chains are ordered after each other in same order
2292 + // as their corresponding hooks
2293 + for (j = i + 1; j < NF_BR_NUMHOOKS; j++) {
2294 + if (!(repl->valid_hooks & (1 << j)))
2296 + if ( repl->hook_entry[j] <= repl->hook_entry[i] ) {
2297 + BUGPRINT("Hook order must be followed\n");
2303 + for (i = 0; i < NF_BR_NUMHOOKS; i++)
2304 + newinfo->hook_entry[i] = NULL;
2306 + newinfo->entries_size = repl->entries_size;
2307 + newinfo->nentries = repl->nentries;
2309 + // do some early checkings and initialize some things
2310 + i = 0; // holds the expected nr. of entries for the chain
2311 + j = 0; // holds the up to now counted entries for the chain
2312 + k = 0; // holds the total nr. of entries, should equal
2313 + // newinfo->nentries afterwards
2314 + udc_cnt = 0; // will hold the nr. of user defined chains (udc)
2315 + ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
2316 + ebt_check_entry_size_and_hooks, newinfo, repl->entries,
2317 + repl->entries + repl->entries_size, repl->hook_entry, &i, &j, &k,
2318 + &udc_cnt, repl->valid_hooks);
2324 + BUGPRINT("nentries does not equal the nr of entries in the "
2325 + "(last) chain\n");
2328 + if (k != newinfo->nentries) {
2329 + BUGPRINT("Total nentries is wrong\n");
2333 + // check if all valid hooks have a chain
2334 + for (i = 0; i < NF_BR_NUMHOOKS; i++) {
2335 + if (newinfo->hook_entry[i] == NULL &&
2336 + (repl->valid_hooks & (1 << i))) {
2337 + BUGPRINT("Valid hook without chain\n");
2342 + // Get the location of the udc, put them in an array
2343 + // While we're at it, allocate the chainstack
2345 + // this will get free'd in do_replace()/ebt_register_table()
2346 + // if an error occurs
2347 + newinfo->chainstack = (struct ebt_chainstack **)
2348 + vmalloc(smp_num_cpus * sizeof(struct ebt_chainstack));
2349 + if (!newinfo->chainstack)
2351 + for (i = 0; i < smp_num_cpus; i++) {
2352 + newinfo->chainstack[i] =
2353 + vmalloc(udc_cnt * sizeof(struct ebt_chainstack));
2354 + if (!newinfo->chainstack[i]) {
2356 + vfree(newinfo->chainstack[--i]);
2357 + vfree(newinfo->chainstack);
2358 + newinfo->chainstack = NULL;
2363 + cl_s = (struct ebt_cl_stack *)
2364 + vmalloc(udc_cnt * sizeof(struct ebt_cl_stack));
2367 + i = 0; // the i'th udc
2368 + EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
2369 + ebt_get_udc_positions, newinfo, repl->hook_entry, &i,
2370 + repl->valid_hooks, cl_s);
2372 + if (i != udc_cnt) {
2373 + BUGPRINT("i != udc_cnt\n");
2379 + // Check for loops
2380 + for (i = 0; i < NF_BR_NUMHOOKS; i++)
2381 + if (repl->valid_hooks & (1 << i))
2382 + if (check_chainloops(newinfo->hook_entry[i],
2383 + cl_s, udc_cnt, i, newinfo->entries)) {
2389 + // we now know the following (along with E=mc²):
2390 + // - the nr of entries in each chain is right
2391 + // - the size of the allocated space is right
2392 + // - all valid hooks have a corresponding chain
2393 + // - there are no loops
2394 + // - wrong data can still be on the level of a single entry
2395 + // - could be there are jumps to places that are not the
2396 + // beginning of a chain. This can only occur in chains that
2397 + // are not accessible from any base chains, so we don't care.
2399 + // used to know what we need to clean up if something goes wrong
2401 + ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
2402 + ebt_check_entry, newinfo, repl->name, &i, repl->valid_hooks,
2405 + EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
2406 + ebt_cleanup_entry, &i);
2413 +// called under write_lock
2414 +static void get_counters(struct ebt_counter *oldcounters,
2415 + struct ebt_counter *counters, unsigned int nentries)
2418 + struct ebt_counter *counter_base;
2420 + // counters of cpu 0
2421 + memcpy(counters, oldcounters,
2422 + sizeof(struct ebt_counter) * nentries);
2423 + // add other counters to those of cpu 0
2424 + for (cpu = 1; cpu < smp_num_cpus; cpu++) {
2425 + counter_base = COUNTER_BASE(oldcounters, nentries, cpu);
2426 + for (i = 0; i < nentries; i++) {
2427 + counters[i].pcnt += counter_base[i].pcnt;
2428 + counters[i].bcnt += counter_base[i].bcnt;
2433 +// replace the table
2434 +static int do_replace(void *user, unsigned int len)
2436 + int ret, i, countersize;
2437 + struct ebt_table_info *newinfo;
2438 + struct ebt_replace tmp;
2439 + struct ebt_table *t;
2440 + struct ebt_counter *counterstmp = NULL;
2441 + // used to be able to unlock earlier
2442 + struct ebt_table_info *table;
2444 + if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
2447 + if (len != sizeof(tmp) + tmp.entries_size) {
2448 + BUGPRINT("Wrong len argument\n");
2452 + if (tmp.entries_size == 0) {
2453 + BUGPRINT("Entries_size never zero\n");
2456 + countersize = COUNTER_OFFSET(tmp.nentries) * smp_num_cpus;
2457 + newinfo = (struct ebt_table_info *)
2458 + vmalloc(sizeof(struct ebt_table_info) + countersize);
2463 + memset(newinfo->counters, 0, countersize);
2465 + newinfo->entries = (char *)vmalloc(tmp.entries_size);
2466 + if (!newinfo->entries) {
2468 + goto free_newinfo;
2470 + if (copy_from_user(
2471 + newinfo->entries, tmp.entries, tmp.entries_size) != 0) {
2472 + BUGPRINT("Couldn't copy entries from userspace\n");
2474 + goto free_entries;
2477 + // the user wants counters back
2478 + // the check on the size is done later, when we have the lock
2479 + if (tmp.num_counters) {
2480 + counterstmp = (struct ebt_counter *)
2481 + vmalloc(tmp.num_counters * sizeof(struct ebt_counter));
2482 + if (!counterstmp) {
2484 + goto free_entries;
2488 + counterstmp = NULL;
2490 + // this can get initialized by translate_table()
2491 + newinfo->chainstack = NULL;
2492 + ret = translate_table(&tmp, newinfo);
2495 + goto free_counterstmp;
2497 + t = find_table_lock(tmp.name, &ret, &ebt_mutex);
2499 + goto free_iterate;
2501 + // the table doesn't like it
2502 + if (t->check && (ret = t->check(newinfo, tmp.valid_hooks)))
2505 + if (tmp.num_counters && tmp.num_counters != t->private->nentries) {
2506 + BUGPRINT("Wrong nr. of counters requested\n");
2511 + // we have the mutex lock, so no danger in reading this pointer
2512 + table = t->private;
2513 + // we need an atomic snapshot of the counters
2514 + write_lock_bh(&t->lock);
2515 + if (tmp.num_counters)
2516 + get_counters(t->private->counters, counterstmp,
2517 + t->private->nentries);
2519 + t->private = newinfo;
2520 + write_unlock_bh(&t->lock);
2522 + // So, a user can change the chains while having messed up her counter
2523 + // allocation. Only reason why this is done is because this way the lock
2524 + // is held only once, while this doesn't bring the kernel into a
2525 + // dangerous state.
2526 + if (tmp.num_counters &&
2527 + copy_to_user(tmp.counters, counterstmp,
2528 + tmp.num_counters * sizeof(struct ebt_counter))) {
2529 + BUGPRINT("Couldn't copy counters to userspace\n");
2535 + // decrease module count and free resources
2536 + EBT_ENTRY_ITERATE(table->entries, table->entries_size,
2537 + ebt_cleanup_entry, NULL);
2539 + vfree(table->entries);
2540 + if (table->chainstack) {
2541 + for (i = 0; i < smp_num_cpus; i++)
2542 + vfree(table->chainstack[i]);
2543 + vfree(table->chainstack);
2548 + vfree(counterstmp);
2554 + EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
2555 + ebt_cleanup_entry, NULL);
2558 + vfree(counterstmp);
2559 + // can be initialized in translate_table()
2560 + if (newinfo->chainstack) {
2561 + for (i = 0; i < smp_num_cpus; i++)
2562 + vfree(newinfo->chainstack[i]);
2563 + vfree(newinfo->chainstack);
2566 + if (newinfo->entries)
2567 + vfree(newinfo->entries);
2574 +int ebt_register_target(struct ebt_target *target)
2578 + ret = down_interruptible(&ebt_mutex);
2581 + if (!list_named_insert(&ebt_targets, target)) {
2586 + MOD_INC_USE_COUNT;
2591 +void ebt_unregister_target(struct ebt_target *target)
2594 + LIST_DELETE(&ebt_targets, target);
2596 + MOD_DEC_USE_COUNT;
2599 +int ebt_register_match(struct ebt_match *match)
2603 + ret = down_interruptible(&ebt_mutex);
2606 + if (!list_named_insert(&ebt_matches, match)) {
2611 + MOD_INC_USE_COUNT;
2616 +void ebt_unregister_match(struct ebt_match *match)
2619 + LIST_DELETE(&ebt_matches, match);
2621 + MOD_DEC_USE_COUNT;
2624 +int ebt_register_watcher(struct ebt_watcher *watcher)
2628 + ret = down_interruptible(&ebt_mutex);
2631 + if (!list_named_insert(&ebt_watchers, watcher)) {
2636 + MOD_INC_USE_COUNT;
2641 +void ebt_unregister_watcher(struct ebt_watcher *watcher)
2644 + LIST_DELETE(&ebt_watchers, watcher);
2646 + MOD_DEC_USE_COUNT;
2649 +int ebt_register_table(struct ebt_table *table)
2651 + struct ebt_table_info *newinfo;
2652 + int ret, i, countersize;
2654 + if (!table || !table->table ||!table->table->entries ||
2655 + table->table->entries_size == 0 ||
2656 + table->table->counters || table->private) {
2657 + BUGPRINT("Bad table data for ebt_register_table!!!\n");
2661 + countersize = COUNTER_OFFSET(table->table->nentries) * smp_num_cpus;
2662 + newinfo = (struct ebt_table_info *)
2663 + vmalloc(sizeof(struct ebt_table_info) + countersize);
2668 + newinfo->entries = (char *)vmalloc(table->table->entries_size);
2669 + if (!(newinfo->entries))
2670 + goto free_newinfo;
2672 + memcpy(newinfo->entries, table->table->entries,
2673 + table->table->entries_size);
2676 + memset(newinfo->counters, 0, countersize);
2678 + // fill in newinfo and parse the entries
2679 + newinfo->chainstack = NULL;
2680 + ret = translate_table(table->table, newinfo);
2682 + BUGPRINT("Translate_table failed\n");
2683 + goto free_chainstack;
2686 + if (table->check && table->check(newinfo, table->valid_hooks)) {
2687 + BUGPRINT("The table doesn't like its own initial data, lol\n");
2691 + table->private = newinfo;
2692 + table->lock = RW_LOCK_UNLOCKED;
2693 + ret = down_interruptible(&ebt_mutex);
2695 + goto free_chainstack;
2697 + if (list_named_find(&ebt_tables, table->name)) {
2699 + BUGPRINT("Table name already exists\n");
2703 + list_prepend(&ebt_tables, table);
2705 + MOD_INC_USE_COUNT;
2710 + if (newinfo->chainstack) {
2711 + for (i = 0; i < smp_num_cpus; i++)
2712 + vfree(newinfo->chainstack[i]);
2713 + vfree(newinfo->chainstack);
2715 + vfree(newinfo->entries);
2721 +void ebt_unregister_table(struct ebt_table *table)
2726 + BUGPRINT("Request to unregister NULL table!!!\n");
2730 + LIST_DELETE(&ebt_tables, table);
2732 + EBT_ENTRY_ITERATE(table->private->entries,
2733 + table->private->entries_size, ebt_cleanup_entry, NULL);
2734 + if (table->private->entries)
2735 + vfree(table->private->entries);
2736 + if (table->private->chainstack) {
2737 + for (i = 0; i < smp_num_cpus; i++)
2738 + vfree(table->private->chainstack[i]);
2739 + vfree(table->private->chainstack);
2741 + vfree(table->private);
2742 + MOD_DEC_USE_COUNT;
2745 +// userspace just supplied us with counters
2746 +static int update_counters(void *user, unsigned int len)
2749 + struct ebt_counter *tmp;
2750 + struct ebt_replace hlp;
2751 + struct ebt_table *t;
2753 + if (copy_from_user(&hlp, user, sizeof(hlp)))
2756 + if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter))
2758 + if (hlp.num_counters == 0)
2761 + if ( !(tmp = (struct ebt_counter *)
2762 + vmalloc(hlp.num_counters * sizeof(struct ebt_counter))) ){
2763 + MEMPRINT("Update_counters && nomemory\n");
2767 + t = find_table_lock(hlp.name, &ret, &ebt_mutex);
2771 + if (hlp.num_counters != t->private->nentries) {
2772 + BUGPRINT("Wrong nr of counters\n");
2774 + goto unlock_mutex;
2777 + if ( copy_from_user(tmp, hlp.counters,
2778 + hlp.num_counters * sizeof(struct ebt_counter)) ) {
2779 + BUGPRINT("Updata_counters && !cfu\n");
2781 + goto unlock_mutex;
2784 + // we want an atomic add of the counters
2785 + write_lock_bh(&t->lock);
2787 + // we add to the counters of the first cpu
2788 + for (i = 0; i < hlp.num_counters; i++) {
2789 + t->private->counters[i].pcnt += tmp[i].pcnt;
2790 + t->private->counters[i].bcnt += tmp[i].bcnt;
2793 + write_unlock_bh(&t->lock);
2802 +static inline int ebt_make_matchname(struct ebt_entry_match *m,
2803 + char *base, char *ubase)
2805 + char *hlp = ubase - base + (char *)m;
2806 + if (copy_to_user(hlp, m->u.match->name, EBT_FUNCTION_MAXNAMELEN))
2811 +static inline int ebt_make_watchername(struct ebt_entry_watcher *w,
2812 + char *base, char *ubase)
2814 + char *hlp = ubase - base + (char *)w;
2815 + if (copy_to_user(hlp , w->u.watcher->name, EBT_FUNCTION_MAXNAMELEN))
2820 +static inline int ebt_make_names(struct ebt_entry *e, char *base, char *ubase)
2824 + struct ebt_entry_target *t;
2826 + if ((e->bitmask & EBT_ENTRY_OR_ENTRIES) == 0)
2829 + hlp = ubase - base + (char *)e + e->target_offset;
2830 + t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
2832 + ret = EBT_MATCH_ITERATE(e, ebt_make_matchname, base, ubase);
2835 + ret = EBT_WATCHER_ITERATE(e, ebt_make_watchername, base, ubase);
2838 + if (copy_to_user(hlp, t->u.target->name, EBT_FUNCTION_MAXNAMELEN))
2843 +// called with ebt_mutex down
2844 +static int copy_everything_to_user(struct ebt_table *t, void *user,
2845 + int *len, int cmd)
2847 + struct ebt_replace tmp;
2848 + struct ebt_counter *counterstmp, *oldcounters;
2849 + unsigned int entries_size, nentries;
2852 + if (cmd == EBT_SO_GET_ENTRIES) {
2853 + entries_size = t->private->entries_size;
2854 + nentries = t->private->nentries;
2855 + entries = t->private->entries;
2856 + oldcounters = t->private->counters;
2858 + entries_size = t->table->entries_size;
2859 + nentries = t->table->nentries;
2860 + entries = t->table->entries;
2861 + oldcounters = t->table->counters;
2864 + if (copy_from_user(&tmp, user, sizeof(tmp))) {
2865 + BUGPRINT("Cfu didn't work\n");
2869 + if (*len != sizeof(struct ebt_replace) + entries_size +
2870 + (tmp.num_counters? nentries * sizeof(struct ebt_counter): 0)) {
2871 + BUGPRINT("Wrong size\n");
2875 + if (tmp.nentries != nentries) {
2876 + BUGPRINT("Nentries wrong\n");
2880 + if (tmp.entries_size != entries_size) {
2881 + BUGPRINT("Wrong size\n");
2885 + // userspace might not need the counters
2886 + if (tmp.num_counters) {
2887 + if (tmp.num_counters != nentries) {
2888 + BUGPRINT("Num_counters wrong\n");
2891 + counterstmp = (struct ebt_counter *)
2892 + vmalloc(nentries * sizeof(struct ebt_counter));
2893 + if (!counterstmp) {
2894 + MEMPRINT("Couldn't copy counters, out of memory\n");
2897 + write_lock_bh(&t->lock);
2898 + get_counters(oldcounters, counterstmp, nentries);
2899 + write_unlock_bh(&t->lock);
2901 + if (copy_to_user(tmp.counters, counterstmp,
2902 + nentries * sizeof(struct ebt_counter))) {
2903 + BUGPRINT("Couldn't copy counters to userspace\n");
2904 + vfree(counterstmp);
2907 + vfree(counterstmp);
2910 + if (copy_to_user(tmp.entries, entries, entries_size)) {
2911 + BUGPRINT("Couldn't copy entries to userspace\n");
2914 + // set the match/watcher/target names right
2915 + return EBT_ENTRY_ITERATE(entries, entries_size,
2916 + ebt_make_names, entries, tmp.entries);
2919 +static int do_ebt_set_ctl(struct sock *sk,
2920 + int cmd, void *user, unsigned int len)
2925 + case EBT_SO_SET_ENTRIES:
2926 + ret = do_replace(user, len);
2928 + case EBT_SO_SET_COUNTERS:
2929 + ret = update_counters(user, len);
2937 +static int do_ebt_get_ctl(struct sock *sk, int cmd, void *user, int *len)
2940 + struct ebt_replace tmp;
2941 + struct ebt_table *t;
2943 + if (copy_from_user(&tmp, user, sizeof(tmp)))
2946 + t = find_table_lock(tmp.name, &ret, &ebt_mutex);
2951 + case EBT_SO_GET_INFO:
2952 + case EBT_SO_GET_INIT_INFO:
2953 + if (*len != sizeof(struct ebt_replace)){
2958 + if (cmd == EBT_SO_GET_INFO) {
2959 + tmp.nentries = t->private->nentries;
2960 + tmp.entries_size = t->private->entries_size;
2961 + tmp.valid_hooks = t->valid_hooks;
2963 + tmp.nentries = t->table->nentries;
2964 + tmp.entries_size = t->table->entries_size;
2965 + tmp.valid_hooks = t->table->valid_hooks;
2968 + if (copy_to_user(user, &tmp, *len) != 0){
2969 + BUGPRINT("c2u Didn't work\n");
2976 + case EBT_SO_GET_ENTRIES:
2977 + case EBT_SO_GET_INIT_ENTRIES:
2978 + ret = copy_everything_to_user(t, user, len, cmd);
2990 +static struct nf_sockopt_ops ebt_sockopts =
2991 +{ { NULL, NULL }, PF_INET, EBT_BASE_CTL, EBT_SO_SET_MAX + 1, do_ebt_set_ctl,
2992 + EBT_BASE_CTL, EBT_SO_GET_MAX + 1, do_ebt_get_ctl, 0, NULL
2995 +static int __init init(void)
3000 + list_named_insert(&ebt_targets, &ebt_standard_target);
3002 + if ((ret = nf_register_sockopt(&ebt_sockopts)) < 0)
3005 + printk("Ebtables v2.0 registered");
3009 +static void __exit fini(void)
3011 + nf_unregister_sockopt(&ebt_sockopts);
3012 + printk("Ebtables v2.0 unregistered");
3015 +EXPORT_SYMBOL(ebt_register_table);
3016 +EXPORT_SYMBOL(ebt_unregister_table);
3017 +EXPORT_SYMBOL(ebt_register_match);
3018 +EXPORT_SYMBOL(ebt_unregister_match);
3019 +EXPORT_SYMBOL(ebt_register_watcher);
3020 +EXPORT_SYMBOL(ebt_unregister_watcher);
3021 +EXPORT_SYMBOL(ebt_register_target);
3022 +EXPORT_SYMBOL(ebt_unregister_target);
3023 +EXPORT_SYMBOL(ebt_do_table);
3026 +MODULE_LICENSE("GPL");
3027 --- /dev/null Thu Aug 24 11:00:32 2000
3028 +++ linux-2.4.20-pre7-bcnt/include/linux/netfilter_bridge/ebtables.h Thu Oct 17 22:41:32 2002
3034 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
3036 + * ebtables.c,v 2.0, September, 2002
3038 + * This code is stongly inspired on the iptables code which is
3039 + * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
3042 +#ifndef __LINUX_BRIDGE_EFF_H
3043 +#define __LINUX_BRIDGE_EFF_H
3044 +#include <linux/if.h>
3045 +#include <linux/netfilter_bridge.h>
3046 +#include <linux/if_ether.h>
3048 +#define EBT_TABLE_MAXNAMELEN 32
3049 +#define EBT_CHAIN_MAXNAMELEN EBT_TABLE_MAXNAMELEN
3050 +#define EBT_FUNCTION_MAXNAMELEN EBT_TABLE_MAXNAMELEN
3052 +// verdicts >0 are "branches"
3053 +#define EBT_ACCEPT -1
3054 +#define EBT_DROP -2
3055 +#define EBT_CONTINUE -3
3056 +#define EBT_RETURN -4
3057 +#define NUM_STANDARD_TARGETS 4
3065 +struct ebt_entries {
3066 + // this field is always set to zero
3067 + // See EBT_ENTRY_OR_ENTRIES.
3068 + // Must be same size as ebt_entry.bitmask
3069 + unsigned int distinguisher;
3071 + char name[EBT_CHAIN_MAXNAMELEN];
3072 + // counter offset for this chain
3073 + unsigned int counter_offset;
3074 + // one standard (accept, drop, return) per hook
3077 + unsigned int nentries;
3082 +// used for the bitmask of struct ebt_entry
3084 +// This is a hack to make a difference between an ebt_entry struct and an
3085 +// ebt_entries struct when traversing the entries from start to end.
3086 +// Using this simplifies the code alot, while still being able to use
3088 +// Contrary, iptables doesn't use something like ebt_entries and therefore uses
3089 +// different techniques for naming the policy and such. So, iptables doesn't
3090 +// need a hack like this.
3091 +#define EBT_ENTRY_OR_ENTRIES 0x01
3092 +// these are the normal masks
3093 +#define EBT_NOPROTO 0x02
3094 +#define EBT_802_3 0x04
3095 +#define EBT_SOURCEMAC 0x08
3096 +#define EBT_DESTMAC 0x10
3097 +#define EBT_F_MASK (EBT_NOPROTO | EBT_802_3 | EBT_SOURCEMAC | EBT_DESTMAC \
3098 + | EBT_ENTRY_OR_ENTRIES)
3100 +#define EBT_IPROTO 0x01
3101 +#define EBT_IIN 0x02
3102 +#define EBT_IOUT 0x04
3103 +#define EBT_ISOURCE 0x8
3104 +#define EBT_IDEST 0x10
3105 +#define EBT_ILOGICALIN 0x20
3106 +#define EBT_ILOGICALOUT 0x40
3107 +#define EBT_INV_MASK (EBT_IPROTO | EBT_IIN | EBT_IOUT | EBT_ILOGICALIN \
3108 + | EBT_ILOGICALOUT | EBT_ISOURCE | EBT_IDEST)
3110 +struct ebt_entry_match
3113 + char name[EBT_FUNCTION_MAXNAMELEN];
3114 + struct ebt_match *match;
3117 + unsigned int match_size;
3118 + unsigned char data[0];
3121 +struct ebt_entry_watcher
3124 + char name[EBT_FUNCTION_MAXNAMELEN];
3125 + struct ebt_watcher *watcher;
3128 + unsigned int watcher_size;
3129 + unsigned char data[0];
3132 +struct ebt_entry_target
3135 + char name[EBT_FUNCTION_MAXNAMELEN];
3136 + struct ebt_target *target;
3139 + unsigned int target_size;
3140 + unsigned char data[0];
3143 +#define EBT_STANDARD_TARGET "standard"
3144 +struct ebt_standard_target
3146 + struct ebt_entry_target target;
3152 + // this needs to be the first field
3153 + unsigned int bitmask;
3154 + unsigned int invflags;
3155 + uint16_t ethproto;
3156 + // the physical in-dev
3157 + char in[IFNAMSIZ];
3158 + // the logical in-dev
3159 + char logical_in[IFNAMSIZ];
3160 + // the physical out-dev
3161 + char out[IFNAMSIZ];
3162 + // the logical out-dev
3163 + char logical_out[IFNAMSIZ];
3164 + unsigned char sourcemac[ETH_ALEN];
3165 + unsigned char sourcemsk[ETH_ALEN];
3166 + unsigned char destmac[ETH_ALEN];
3167 + unsigned char destmsk[ETH_ALEN];
3168 + // sizeof ebt_entry + matches
3169 + unsigned int watchers_offset;
3170 + // sizeof ebt_entry + matches + watchers
3171 + unsigned int target_offset;
3172 + // sizeof ebt_entry + matches + watchers + target
3173 + unsigned int next_offset;
3174 + unsigned char elems[0];
3179 + char name[EBT_TABLE_MAXNAMELEN];
3180 + unsigned int valid_hooks;
3181 + // nr of rules in the table
3182 + unsigned int nentries;
3183 + // total size of the entries
3184 + unsigned int entries_size;
3185 + // start of the chains
3186 + struct ebt_entries *hook_entry[NF_BR_NUMHOOKS];
3187 + // nr of counters userspace expects back
3188 + unsigned int num_counters;
3189 + // where the kernel will put the old counters
3190 + struct ebt_counter *counters;
3194 +// [gs]etsockopt numbers
3195 +#define EBT_BASE_CTL 128
3197 +#define EBT_SO_SET_ENTRIES (EBT_BASE_CTL)
3198 +#define EBT_SO_SET_COUNTERS (EBT_SO_SET_ENTRIES+1)
3199 +#define EBT_SO_SET_MAX (EBT_SO_SET_COUNTERS+1)
3201 +#define EBT_SO_GET_INFO (EBT_BASE_CTL)
3202 +#define EBT_SO_GET_ENTRIES (EBT_SO_GET_INFO+1)
3203 +#define EBT_SO_GET_INIT_INFO (EBT_SO_GET_ENTRIES+1)
3204 +#define EBT_SO_GET_INIT_ENTRIES (EBT_SO_GET_INIT_INFO+1)
3205 +#define EBT_SO_GET_MAX (EBT_SO_GET_INIT_ENTRIES+1)
3209 +// return values for match() functions
3210 +#define EBT_MATCH 0
3211 +#define EBT_NOMATCH 1
3215 + struct list_head list;
3216 + const char name[EBT_FUNCTION_MAXNAMELEN];
3217 + // 0 == it matches
3218 + int (*match)(const struct sk_buff *skb, const struct net_device *in,
3219 + const struct net_device *out, const void *matchdata,
3220 + unsigned int datalen);
3222 + int (*check)(const char *tablename, unsigned int hookmask,
3223 + const struct ebt_entry *e, void *matchdata, unsigned int datalen);
3224 + void (*destroy)(void *matchdata, unsigned int datalen);
3225 + struct module *me;
3230 + struct list_head list;
3231 + const char name[EBT_FUNCTION_MAXNAMELEN];
3232 + void (*watcher)(const struct sk_buff *skb, const struct net_device *in,
3233 + const struct net_device *out, const void *watcherdata,
3234 + unsigned int datalen);
3236 + int (*check)(const char *tablename, unsigned int hookmask,
3237 + const struct ebt_entry *e, void *watcherdata, unsigned int datalen);
3238 + void (*destroy)(void *watcherdata, unsigned int datalen);
3239 + struct module *me;
3244 + struct list_head list;
3245 + const char name[EBT_FUNCTION_MAXNAMELEN];
3246 + // returns one of the standard verdicts
3247 + int (*target)(struct sk_buff **pskb, unsigned int hooknr,
3248 + const struct net_device *in, const struct net_device *out,
3249 + const void *targetdata, unsigned int datalen);
3251 + int (*check)(const char *tablename, unsigned int hookmask,
3252 + const struct ebt_entry *e, void *targetdata, unsigned int datalen);
3253 + void (*destroy)(void *targetdata, unsigned int datalen);
3254 + struct module *me;
3257 +// used for jumping from and into user defined chains (udc)
3258 +struct ebt_chainstack
3260 + struct ebt_entries *chaininfo; // pointer to chain data
3261 + struct ebt_entry *e; // pointer to entry data
3262 + unsigned int n; // n'th entry
3265 +struct ebt_table_info
3267 + // total size of the entries
3268 + unsigned int entries_size;
3269 + unsigned int nentries;
3270 + // pointers to the start of the chains
3271 + struct ebt_entries *hook_entry[NF_BR_NUMHOOKS];
3272 + // room to maintain the stack used for jumping from and into udc
3273 + struct ebt_chainstack **chainstack;
3275 + struct ebt_counter counters[0] ____cacheline_aligned;
3280 + struct list_head list;
3281 + char name[EBT_TABLE_MAXNAMELEN];
3282 + struct ebt_replace *table;
3283 + unsigned int valid_hooks;
3285 + // e.g. could be the table explicitly only allows certain
3286 + // matches, targets, ... 0 == let it in
3287 + int (*check)(const struct ebt_table_info *info,
3288 + unsigned int valid_hooks);
3289 + // the data used by the kernel
3290 + struct ebt_table_info *private;
3293 +extern int ebt_register_table(struct ebt_table *table);
3294 +extern void ebt_unregister_table(struct ebt_table *table);
3295 +extern int ebt_register_match(struct ebt_match *match);
3296 +extern void ebt_unregister_match(struct ebt_match *match);
3297 +extern int ebt_register_watcher(struct ebt_watcher *watcher);
3298 +extern void ebt_unregister_watcher(struct ebt_watcher *watcher);
3299 +extern int ebt_register_target(struct ebt_target *target);
3300 +extern void ebt_unregister_target(struct ebt_target *target);
3301 +extern unsigned int ebt_do_table(unsigned int hook, struct sk_buff **pskb,
3302 + const struct net_device *in, const struct net_device *out,
3303 + struct ebt_table *table);
3305 + // Used in the kernel match() functions
3306 +#define FWINV(bool,invflg) ((bool) ^ !!(info->invflags & invflg))
3307 +// True if the hook mask denotes that the rule is in a base chain,
3308 +// used in the check() functions
3309 +#define BASE_CHAIN (hookmask & (1 << NF_BR_NUMHOOKS))
3310 +// Clear the bit in the hook mask that tells if the rule is on a base chain
3311 +#define CLEAR_BASE_CHAIN_BIT (hookmask &= ~(1 << NF_BR_NUMHOOKS))
3312 +// True if the target is not a standard target
3313 +#define INVALID_TARGET (info->target < -NUM_STANDARD_TARGETS || info->target >= 0)
3315 +#endif /* __KERNEL__ */
3317 +// blatently stolen from ip_tables.h
3318 +// fn returns 0 to continue iteration
3319 +#define EBT_MATCH_ITERATE(e, fn, args...) \
3321 + unsigned int __i; \
3323 + struct ebt_entry_match *__match; \
3325 + for (__i = sizeof(struct ebt_entry); \
3326 + __i < (e)->watchers_offset; \
3327 + __i += __match->match_size + \
3328 + sizeof(struct ebt_entry_match)) { \
3329 + __match = (void *)(e) + __i; \
3331 + __ret = fn(__match , ## args); \
3335 + if (__ret == 0) { \
3336 + if (__i != (e)->watchers_offset) \
3337 + __ret = -EINVAL; \
3342 +#define EBT_WATCHER_ITERATE(e, fn, args...) \
3344 + unsigned int __i; \
3346 + struct ebt_entry_watcher *__watcher; \
3348 + for (__i = e->watchers_offset; \
3349 + __i < (e)->target_offset; \
3350 + __i += __watcher->watcher_size + \
3351 + sizeof(struct ebt_entry_watcher)) { \
3352 + __watcher = (void *)(e) + __i; \
3354 + __ret = fn(__watcher , ## args); \
3358 + if (__ret == 0) { \
3359 + if (__i != (e)->target_offset) \
3360 + __ret = -EINVAL; \
3365 +#define EBT_ENTRY_ITERATE(entries, size, fn, args...) \
3367 + unsigned int __i; \
3369 + struct ebt_entry *__entry; \
3371 + for (__i = 0; __i < (size);) { \
3372 + __entry = (void *)(entries) + __i; \
3373 + __ret = fn(__entry , ## args); \
3376 + if (__entry->bitmask != 0) \
3377 + __i += __entry->next_offset; \
3379 + __i += sizeof(struct ebt_entries); \
3381 + if (__ret == 0) { \
3382 + if (__i != (size)) \
3383 + __ret = -EINVAL; \
3389 --- /dev/null Thu Aug 24 11:00:32 2000
3390 +++ linux-2.4.20-pre7-bcnt/include/linux/netfilter_bridge/ebt_arp.h Thu Oct 17 20:31:29 2002
3392 +#ifndef __LINUX_BRIDGE_EBT_ARP_H
3393 +#define __LINUX_BRIDGE_EBT_ARP_H
3395 +#define EBT_ARP_OPCODE 0x01
3396 +#define EBT_ARP_HTYPE 0x02
3397 +#define EBT_ARP_PTYPE 0x04
3398 +#define EBT_ARP_SRC_IP 0x08
3399 +#define EBT_ARP_DST_IP 0x10
3400 +#define EBT_ARP_MASK (EBT_ARP_OPCODE | EBT_ARP_HTYPE | EBT_ARP_PTYPE | \
3401 + EBT_ARP_SRC_IP | EBT_ARP_DST_IP)
3402 +#define EBT_ARP_MATCH "arp"
3404 +struct ebt_arp_info
3418 --- /dev/null Thu Aug 24 11:00:32 2000
3419 +++ linux-2.4.20-pre7-bcnt/include/linux/netfilter_bridge/ebt_ip.h Thu Oct 17 23:22:45 2002
3425 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
3430 + * added ip-sport and ip-dport
3431 + * Innominate Security Technologies AG <mhopf@innominate.com>
3435 +#ifndef __LINUX_BRIDGE_EBT_IP_H
3436 +#define __LINUX_BRIDGE_EBT_IP_H
3438 +#define EBT_IP_SOURCE 0x01
3439 +#define EBT_IP_DEST 0x02
3440 +#define EBT_IP_TOS 0x04
3441 +#define EBT_IP_PROTO 0x08
3442 +#define EBT_IP_SPORT 0x10
3443 +#define EBT_IP_DPORT 0x20
3444 +#define EBT_IP_MASK (EBT_IP_SOURCE | EBT_IP_DEST | EBT_IP_TOS | EBT_IP_PROTO |\
3445 + EBT_IP_SPORT | EBT_IP_DPORT )
3446 +#define EBT_IP_MATCH "ip"
3448 +// the same values are used for the invflags
3459 + uint16_t sport[2];
3460 + uint16_t dport[2];
3464 --- /dev/null Thu Aug 24 11:00:32 2000
3465 +++ linux-2.4.20-pre7-bcnt/include/linux/netfilter_bridge/ebt_vlan.h Thu Oct 17 20:31:29 2002
3467 +#ifndef __LINUX_BRIDGE_EBT_VLAN_H
3468 +#define __LINUX_BRIDGE_EBT_VLAN_H
3470 +#define EBT_VLAN_ID 0x01
3471 +#define EBT_VLAN_PRIO 0x02
3472 +#define EBT_VLAN_ENCAP 0x04
3473 +#define EBT_VLAN_MASK (EBT_VLAN_ID | EBT_VLAN_PRIO | EBT_VLAN_ENCAP)
3474 +#define EBT_VLAN_MATCH "vlan"
3476 +struct ebt_vlan_info {
3477 + uint16_t id; /* VLAN ID {1-4095} */
3478 + uint8_t prio; /* VLAN User Priority {0-7} */
3479 + uint16_t encap; /* VLAN Encapsulated frame code {0-65535} */
3480 + uint8_t bitmask; /* Args bitmask bit 1=1 - ID arg,
3481 + bit 2=1 User-Priority arg, bit 3=1 encap*/
3482 + uint8_t invflags; /* Inverse bitmask bit 1=1 - inversed ID arg,
3483 + bit 2=1 - inversed Pirority arg */
3487 --- /dev/null Thu Aug 24 11:00:32 2000
3488 +++ linux-2.4.20-pre7-bcnt/include/linux/netfilter_bridge/ebt_log.h Thu Oct 17 20:31:29 2002
3490 +#ifndef __LINUX_BRIDGE_EBT_LOG_H
3491 +#define __LINUX_BRIDGE_EBT_LOG_H
3493 +#define EBT_LOG_IP 0x01 // if the frame is made by ip, log the ip information
3494 +#define EBT_LOG_ARP 0x02
3495 +#define EBT_LOG_MASK (EBT_LOG_IP | EBT_LOG_ARP)
3496 +#define EBT_LOG_PREFIX_SIZE 30
3497 +#define EBT_LOG_WATCHER "log"
3499 +struct ebt_log_info
3502 + uint8_t prefix[EBT_LOG_PREFIX_SIZE];
3507 --- /dev/null Thu Aug 24 11:00:32 2000
3508 +++ linux-2.4.20-pre7-bcnt/include/linux/netfilter_bridge/ebt_nat.h Thu Oct 17 20:31:29 2002
3510 +#ifndef __LINUX_BRIDGE_EBT_NAT_H
3511 +#define __LINUX_BRIDGE_EBT_NAT_H
3513 +struct ebt_nat_info
3515 + unsigned char mac[ETH_ALEN];
3516 + // EBT_ACCEPT, EBT_DROP, EBT_CONTINUE or EBT_RETURN
3519 +#define EBT_SNAT_TARGET "snat"
3520 +#define EBT_DNAT_TARGET "dnat"
3523 --- /dev/null Thu Aug 24 11:00:32 2000
3524 +++ linux-2.4.20-pre7-bcnt/include/linux/netfilter_bridge/ebt_redirect.h Thu Oct 17 20:31:29 2002
3526 +#ifndef __LINUX_BRIDGE_EBT_REDIRECT_H
3527 +#define __LINUX_BRIDGE_EBT_REDIRECT_H
3529 +struct ebt_redirect_info
3531 + // EBT_ACCEPT, EBT_DROP or EBT_CONTINUE or EBT_RETURN
3534 +#define EBT_REDIRECT_TARGET "redirect"
3537 --- /dev/null Thu Aug 24 11:00:32 2000
3538 +++ linux-2.4.20-pre7-bcnt/include/linux/netfilter_bridge/ebt_mark_m.h Thu Oct 17 20:31:29 2002
3540 +#ifndef __LINUX_BRIDGE_EBT_MARK_M_H
3541 +#define __LINUX_BRIDGE_EBT_MARK_M_H
3543 +#define EBT_MARK_AND 0x01
3544 +#define EBT_MARK_OR 0x02
3545 +#define EBT_MARK_MASK (EBT_MARK_AND | EBT_MARK_OR)
3546 +struct ebt_mark_m_info
3548 + unsigned long mark, mask;
3552 +#define EBT_MARK_MATCH "mark_m"
3555 --- /dev/null Thu Aug 24 11:00:32 2000
3556 +++ linux-2.4.20-pre7-bcnt/include/linux/netfilter_bridge/ebt_mark_t.h Thu Oct 17 20:31:29 2002
3558 +#ifndef __LINUX_BRIDGE_EBT_MARK_T_H
3559 +#define __LINUX_BRIDGE_EBT_MARK_T_H
3561 +struct ebt_mark_t_info
3563 + unsigned long mark;
3564 + // EBT_ACCEPT, EBT_DROP or EBT_CONTINUE or EBT_RETURN
3567 +#define EBT_MARK_TARGET "mark"