3 # Patch managed by http://www.mn-logistik.de/unsupported/pxa250/patcher
6 --- linux-2.4.20/net/bridge/br_private.h~140-ebtables-brnf-5 2005-01-07 02:52:47.206944000 -0500
7 +++ linux-2.4.20/net/bridge/br_private.h 2005-01-07 02:57:30.880819576 -0500
10 extern void br_deliver(struct net_bridge_port *to,
12 +extern int br_dev_queue_push_xmit(struct sk_buff *skb);
13 extern void br_forward(struct net_bridge_port *to,
15 +extern int br_forward_finish(struct sk_buff *skb);
16 extern void br_flood_deliver(struct net_bridge *br,
23 -extern void br_handle_frame(struct sk_buff *skb);
24 +extern int br_handle_frame_finish(struct sk_buff *skb);
25 +extern int br_handle_frame(struct sk_buff *skb);
28 extern void br_call_ioctl_atomic(void (*fn)(void));
31 extern int br_ioctl_deviceless_stub(unsigned long arg);
34 +extern int br_netfilter_init(void);
35 +extern void br_netfilter_fini(void);
38 extern int br_is_root_bridge(struct net_bridge *br);
39 extern struct net_bridge_port *br_get_port(struct net_bridge *br,
40 --- linux-2.4.20/include/linux/if_bridge.h~140-ebtables-brnf-5 2001-11-22 14:47:12.000000000 -0500
41 +++ linux-2.4.20/include/linux/if_bridge.h 2005-01-07 02:57:30.880819576 -0500
43 struct net_bridge_port;
45 extern int (*br_ioctl_hook)(unsigned long arg);
46 -extern void (*br_handle_frame_hook)(struct sk_buff *skb);
47 +extern int (*br_handle_frame_hook)(struct sk_buff *skb);
48 +extern int (*br_should_route_hook)(struct sk_buff **pskb);
52 --- linux-2.4.20/net/core/dev.c~140-ebtables-brnf-5 2005-01-07 02:52:47.208944000 -0500
53 +++ linux-2.4.20/net/core/dev.c 2005-01-07 02:57:30.882819272 -0500
57 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
58 -void (*br_handle_frame_hook)(struct sk_buff *skb) = NULL;
59 +int (*br_handle_frame_hook)(struct sk_buff *skb) = NULL;
62 static __inline__ int handle_bridge(struct sk_buff *skb,
67 - br_handle_frame_hook(skb);
71 @@ -1466,11 +1465,16 @@
72 if (skb->dev->divert && skb->dev->divert->divert)
73 ret = handle_diverter(skb);
74 #endif /* CONFIG_NET_DIVERT */
77 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
78 if (skb->dev->br_port != NULL &&
79 - br_handle_frame_hook != NULL) {
80 - return handle_bridge(skb, pt_prev);
81 + br_handle_frame_hook != NULL) {
84 + ret = handle_bridge(skb, pt_prev);
85 + if (br_handle_frame_hook(skb) == 0)
92 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
93 * function returns zero.
97 int netdev_set_master(struct net_device *slave, struct net_device *master)
99 struct net_device *old = slave->master;
100 --- linux-2.4.20/net/bridge/br_input.c~140-ebtables-brnf-5 2002-08-02 20:39:46.000000000 -0400
101 +++ linux-2.4.20/net/bridge/br_input.c 2005-01-07 02:57:30.882819272 -0500
104 static int br_pass_frame_up_finish(struct sk_buff *skb)
106 +#ifdef CONFIG_NETFILTER_DEBUG
113 br_pass_frame_up_finish);
116 -static int br_handle_frame_finish(struct sk_buff *skb)
117 +int br_handle_frame_finish(struct sk_buff *skb)
119 struct net_bridge *br;
125 -void br_handle_frame(struct sk_buff *skb)
126 +int br_handle_frame(struct sk_buff *skb)
128 struct net_bridge *br;
130 @@ -146,25 +149,35 @@
131 goto handle_special_frame;
133 if (p->state == BR_STATE_FORWARDING) {
135 + if (br_should_route_hook && br_should_route_hook(&skb)) {
136 + read_unlock(&br->lock);
140 + if (!memcmp(p->br->dev.dev_addr, dest, ETH_ALEN))
141 + skb->pkt_type = PACKET_HOST;
143 NF_HOOK(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
144 br_handle_frame_finish);
145 read_unlock(&br->lock);
151 read_unlock(&br->lock);
157 handle_special_frame:
159 br_stp_handle_bpdu(skb);
160 read_unlock(&br->lock);
165 read_unlock(&br->lock);
169 --- linux-2.4.20/net/bridge/br_forward.c~140-ebtables-brnf-5 2002-08-02 20:39:46.000000000 -0400
170 +++ linux-2.4.20/net/bridge/br_forward.c 2005-01-07 02:57:30.883819120 -0500
175 -static int __dev_queue_push_xmit(struct sk_buff *skb)
176 +int br_dev_queue_push_xmit(struct sk_buff *skb)
178 +#ifdef CONFIG_NETFILTER
179 + if (skb->nf_bridge)
180 + memcpy(skb->data - 16, skb->nf_bridge->hh, 16);
182 skb_push(skb, ETH_HLEN);
188 -static int __br_forward_finish(struct sk_buff *skb)
189 +int br_forward_finish(struct sk_buff *skb)
191 NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
192 - __dev_queue_push_xmit);
193 + br_dev_queue_push_xmit);
198 static void __br_deliver(struct net_bridge_port *to, struct sk_buff *skb)
201 +#ifdef CONFIG_NETFILTER_DEBUG
204 NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
205 - __br_forward_finish);
206 + br_forward_finish);
209 static void __br_forward(struct net_bridge_port *to, struct sk_buff *skb)
213 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
214 - __br_forward_finish);
215 + br_forward_finish);
218 /* called under bridge lock */
219 --- linux-2.4.20/net/bridge/br.c~140-ebtables-brnf-5 2002-11-28 18:53:15.000000000 -0500
220 +++ linux-2.4.20/net/bridge/br.c 2005-01-07 02:57:30.885818816 -0500
222 #include "../atm/lec.h"
225 +int (*br_should_route_hook) (struct sk_buff **pskb) = NULL;
227 void br_dec_use_count()
232 printk(KERN_INFO "NET4: Ethernet Bridge 008 for NET4.0\n");
234 +#ifdef CONFIG_NETFILTER
235 + if (br_netfilter_init())
238 br_handle_frame_hook = br_handle_frame;
239 br_ioctl_hook = br_ioctl_deviceless_stub;
240 #if defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE)
243 static void __exit br_deinit(void)
245 +#ifdef CONFIG_NETFILTER
246 + br_netfilter_fini();
248 unregister_netdevice_notifier(&br_device_notifier);
249 br_call_ioctl_atomic(__br_clear_ioctl_hook);
256 +EXPORT_SYMBOL(br_should_route_hook);
259 module_exit(br_deinit)
260 --- linux-2.4.20/net/bridge/Makefile~140-ebtables-brnf-5 2000-12-29 17:07:24.000000000 -0500
261 +++ linux-2.4.20/net/bridge/Makefile 2005-01-07 02:57:30.885818816 -0500
264 # Note 2! The CFLAGS definition is now in the main makefile...
269 obj-y := br.o br_device.o br_fdb.o br_forward.o br_if.o br_input.o \
270 br_ioctl.o br_notify.o br_stp.o br_stp_bpdu.o \
271 br_stp_if.o br_stp_timer.o
273 +ifeq ($(CONFIG_NETFILTER),y)
274 +obj-y += br_netfilter.o
279 include $(TOPDIR)/Rules.make
280 --- linux-2.4.20/include/linux/netfilter_bridge.h~140-ebtables-brnf-5 2001-06-11 22:15:27.000000000 -0400
281 +++ linux-2.4.20/include/linux/netfilter_bridge.h 2005-01-07 02:57:30.885818816 -0500
283 #ifndef __LINUX_BRIDGE_NETFILTER_H
284 #define __LINUX_BRIDGE_NETFILTER_H
286 -/* bridge-specific defines for netfilter.
287 +/* bridge-specific defines for netfilter.
290 #include <linux/config.h>
291 #include <linux/netfilter.h>
292 +#if defined(__KERNEL__) && defined(CONFIG_NETFILTER)
293 +#include <asm/atomic.h>
297 /* After promisc drops, checksum checks. */
299 #define NF_BR_LOCAL_OUT 3
300 /* Packets about to hit the wire. */
301 #define NF_BR_POST_ROUTING 4
302 -#define NF_BR_NUMHOOKS 5
303 +/* Not really a hook, but used for the ebtables broute table */
304 +#define NF_BR_BROUTING 5
305 +#define NF_BR_NUMHOOKS 6
309 +#define BRNF_PKT_TYPE 0x01
310 +#define BRNF_BRIDGED_DNAT 0x02
311 +#define BRNF_DONT_TAKE_PARENT 0x04
312 +#define BRNF_BRIDGED 0x08
314 +enum nf_br_hook_priorities {
315 + NF_BR_PRI_FIRST = INT_MIN,
316 + NF_BR_PRI_NAT_DST_BRIDGED = -300,
317 + NF_BR_PRI_FILTER_BRIDGED = -200,
318 + NF_BR_PRI_BRNF = 0,
319 + NF_BR_PRI_NAT_DST_OTHER = 100,
320 + NF_BR_PRI_FILTER_OTHER = 200,
321 + NF_BR_PRI_NAT_SRC = 300,
322 + NF_BR_PRI_LAST = INT_MAX,
325 +#ifdef CONFIG_NETFILTER
327 +struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
329 + struct nf_bridge_info **nf_bridge = &(skb->nf_bridge);
331 + if ((*nf_bridge = kmalloc(sizeof(**nf_bridge), GFP_ATOMIC)) != NULL) {
332 + atomic_set(&(*nf_bridge)->use, 1);
333 + (*nf_bridge)->mask = 0;
334 + (*nf_bridge)->physindev = (*nf_bridge)->physoutdev = NULL;
340 +struct bridge_skb_cb {
345 +#endif /* CONFIG_NETFILTER */
347 +#endif /* __KERNEL__ */
349 --- linux-2.4.20/net/Makefile~140-ebtables-brnf-5 2005-01-07 02:55:48.037454000 -0500
350 +++ linux-2.4.20/net/Makefile 2005-01-07 02:57:30.886818664 -0500
353 O_TARGET := network.o
355 -mod-subdirs := ipv4/netfilter ipv6/netfilter ipx irda bluetooth atm netlink sched core
356 +mod-subdirs := ipv4/netfilter ipv6/netfilter bridge/netfilter ipx irda \
357 + bluetooth atm netlink sched core
358 export-objs := netsyms.o
360 subdir-y := core ethernet
365 +ifneq ($(CONFIG_BRIDGE),n)
366 +ifneq ($(CONFIG_BRIDGE),)
367 +subdir-$(CONFIG_BRIDGE) += bridge/netfilter
371 subdir-$(CONFIG_KHTTPD) += khttpd
372 subdir-$(CONFIG_PACKET) += packet
373 subdir-$(CONFIG_NET_SCHED) += sched
374 --- linux-2.4.20/net/Config.in~140-ebtables-brnf-5 2005-01-07 02:55:48.037454000 -0500
375 +++ linux-2.4.20/net/Config.in 2005-01-07 02:57:30.886818664 -0500
377 source net/decnet/Config.in
379 dep_tristate '802.1d Ethernet Bridging' CONFIG_BRIDGE $CONFIG_INET
380 +if [ "$CONFIG_BRIDGE" != "n" -a "$CONFIG_NETFILTER" != "n" ]; then
381 + source net/bridge/netfilter/Config.in
383 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
384 tristate 'CCITT X.25 Packet Layer (EXPERIMENTAL)' CONFIG_X25
385 tristate 'LAPB Data Link Driver (EXPERIMENTAL)' CONFIG_LAPB
386 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
387 +++ linux-2.4.20/net/bridge/netfilter/Makefile 2005-01-07 02:57:30.886818664 -0500
390 +# Makefile for the netfilter modules on top of bridging.
392 +# Note! Dependencies are done automagically by 'make dep', which also
393 +# removes any old dependencies. DON'T put your own dependencies here
394 +# unless it's something special (ie not a .c file).
396 +# Note 2! The CFLAGS definition is now in the main makefile...
398 +O_TARGET := netfilter.o
400 +export-objs := ebtables.o
402 +obj-$(CONFIG_BRIDGE_NF_EBTABLES) += ebtables.o
403 +obj-$(CONFIG_BRIDGE_EBT_T_FILTER) += ebtable_filter.o
404 +obj-$(CONFIG_BRIDGE_EBT_T_NAT) += ebtable_nat.o
405 +obj-$(CONFIG_BRIDGE_EBT_BROUTE) += ebtable_broute.o
406 +obj-$(CONFIG_BRIDGE_EBT_IPF) += ebt_ip.o
407 +obj-$(CONFIG_BRIDGE_EBT_ARPF) += ebt_arp.o
408 +obj-$(CONFIG_BRIDGE_EBT_VLANF) += ebt_vlan.o
409 +obj-$(CONFIG_BRIDGE_EBT_MARKF) += ebt_mark_m.o
410 +obj-$(CONFIG_BRIDGE_EBT_802_3) += ebt_802_3.o
411 +obj-$(CONFIG_BRIDGE_EBT_PKTTYPE) += ebt_pkttype.o
412 +obj-$(CONFIG_BRIDGE_EBT_PKTTYPE) += ebt_stp.o
413 +obj-$(CONFIG_BRIDGE_EBT_LOG) += ebt_log.o
414 +obj-$(CONFIG_BRIDGE_EBT_SNAT) += ebt_snat.o
415 +obj-$(CONFIG_BRIDGE_EBT_DNAT) += ebt_dnat.o
416 +obj-$(CONFIG_BRIDGE_EBT_REDIRECT) += ebt_redirect.o
417 +obj-$(CONFIG_BRIDGE_EBT_MARK_T) += ebt_mark.o
418 +include $(TOPDIR)/Rules.make
419 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
420 +++ linux-2.4.20/net/bridge/netfilter/Config.in 2005-01-07 02:57:30.886818664 -0500
423 +# Bridge netfilter configuration
425 +dep_tristate ' Bridge: ebtables' CONFIG_BRIDGE_NF_EBTABLES $CONFIG_BRIDGE
426 +dep_tristate ' ebt: filter table support' CONFIG_BRIDGE_EBT_T_FILTER $CONFIG_BRIDGE_NF_EBTABLES
427 +dep_tristate ' ebt: nat table support' CONFIG_BRIDGE_EBT_T_NAT $CONFIG_BRIDGE_NF_EBTABLES
428 +dep_tristate ' ebt: broute table support' CONFIG_BRIDGE_EBT_BROUTE $CONFIG_BRIDGE_NF_EBTABLES
429 +dep_tristate ' ebt: log support' CONFIG_BRIDGE_EBT_LOG $CONFIG_BRIDGE_NF_EBTABLES
430 +dep_tristate ' ebt: IP filter support' CONFIG_BRIDGE_EBT_IPF $CONFIG_BRIDGE_NF_EBTABLES
431 +dep_tristate ' ebt: ARP filter support' CONFIG_BRIDGE_EBT_ARPF $CONFIG_BRIDGE_NF_EBTABLES
432 +dep_tristate ' ebt: 802.1Q VLAN filter support' CONFIG_BRIDGE_EBT_VLANF $CONFIG_BRIDGE_NF_EBTABLES
433 +dep_tristate ' ebt: 802.3 filter support' CONFIG_BRIDGE_EBT_802_3 $CONFIG_BRIDGE_NF_EBTABLES
434 +dep_tristate ' ebt: packet type filter support' CONFIG_BRIDGE_EBT_PKTTYPE $CONFIG_BRIDGE_NF_EBTABLES
435 +dep_tristate ' ebt: STP filter support' CONFIG_BRIDGE_EBT_STP $CONFIG_BRIDGE_NF_EBTABLES
436 +dep_tristate ' ebt: mark filter support' CONFIG_BRIDGE_EBT_MARKF $CONFIG_BRIDGE_NF_EBTABLES
437 +dep_tristate ' ebt: snat target support' CONFIG_BRIDGE_EBT_SNAT $CONFIG_BRIDGE_NF_EBTABLES
438 +dep_tristate ' ebt: dnat target support' CONFIG_BRIDGE_EBT_DNAT $CONFIG_BRIDGE_NF_EBTABLES
439 +dep_tristate ' ebt: redirect target support' CONFIG_BRIDGE_EBT_REDIRECT $CONFIG_BRIDGE_NF_EBTABLES
440 +dep_tristate ' ebt: mark target support' CONFIG_BRIDGE_EBT_MARK_T $CONFIG_BRIDGE_NF_EBTABLES
441 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
442 +++ linux-2.4.20/net/bridge/netfilter/ebtable_filter.c 2005-01-07 02:57:30.887818512 -0500
448 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
454 +#include <linux/netfilter_bridge/ebtables.h>
455 +#include <linux/module.h>
457 +#define FILTER_VALID_HOOKS ((1 << NF_BR_LOCAL_IN) | (1 << NF_BR_FORWARD) | \
458 + (1 << NF_BR_LOCAL_OUT))
460 +static struct ebt_entries initial_chains[] =
462 + {0, "INPUT", 0, EBT_ACCEPT, 0},
463 + {0, "FORWARD", 0, EBT_ACCEPT, 0},
464 + {0, "OUTPUT", 0, EBT_ACCEPT, 0}
467 +static struct ebt_replace initial_table =
469 + "filter", FILTER_VALID_HOOKS, 0, 3 * sizeof(struct ebt_entries),
470 + { [NF_BR_LOCAL_IN]&initial_chains[0], [NF_BR_FORWARD]&initial_chains[1],
471 + [NF_BR_LOCAL_OUT]&initial_chains[2] }, 0, NULL, (char *)initial_chains
474 +static int check(const struct ebt_table_info *info, unsigned int valid_hooks)
476 + if (valid_hooks & ~FILTER_VALID_HOOKS)
481 +static struct ebt_table frame_filter =
483 + {NULL, NULL}, "filter", &initial_table, FILTER_VALID_HOOKS,
484 + RW_LOCK_UNLOCKED, check, NULL
488 +ebt_hook (unsigned int hook, struct sk_buff **pskb, const struct net_device *in,
489 + const struct net_device *out, int (*okfn)(struct sk_buff *))
491 + return ebt_do_table(hook, pskb, in, out, &frame_filter);
494 +static struct nf_hook_ops ebt_ops_filter[] = {
495 + { { NULL, NULL }, ebt_hook, PF_BRIDGE, NF_BR_LOCAL_IN,
496 + NF_BR_PRI_FILTER_BRIDGED},
497 + { { NULL, NULL }, ebt_hook, PF_BRIDGE, NF_BR_FORWARD,
498 + NF_BR_PRI_FILTER_BRIDGED},
499 + { { NULL, NULL }, ebt_hook, PF_BRIDGE, NF_BR_LOCAL_OUT,
500 + NF_BR_PRI_FILTER_OTHER}
503 +static int __init init(void)
507 + ret = ebt_register_table(&frame_filter);
510 + for (i = 0; i < sizeof(ebt_ops_filter) / sizeof(ebt_ops_filter[0]); i++)
511 + if ((ret = nf_register_hook(&ebt_ops_filter[i])) < 0)
515 + for (j = 0; j < i; j++)
516 + nf_unregister_hook(&ebt_ops_filter[j]);
517 + ebt_unregister_table(&frame_filter);
521 +static void __exit fini(void)
525 + for (i = 0; i < sizeof(ebt_ops_filter) / sizeof(ebt_ops_filter[0]); i++)
526 + nf_unregister_hook(&ebt_ops_filter[i]);
527 + ebt_unregister_table(&frame_filter);
533 +MODULE_LICENSE("GPL");
534 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
535 +++ linux-2.4.20/net/bridge/netfilter/ebtable_nat.c 2005-01-07 02:57:30.887818512 -0500
541 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
547 +#include <linux/netfilter_bridge/ebtables.h>
548 +#include <linux/module.h>
549 +#define NAT_VALID_HOOKS ((1 << NF_BR_PRE_ROUTING) | (1 << NF_BR_LOCAL_OUT) | \
550 + (1 << NF_BR_POST_ROUTING))
552 +static struct ebt_entries initial_chains[] =
554 + {0, "PREROUTING", 0, EBT_ACCEPT, 0},
555 + {0, "OUTPUT", 0, EBT_ACCEPT, 0},
556 + {0, "POSTROUTING", 0, EBT_ACCEPT, 0}
559 +static struct ebt_replace initial_table =
561 + "nat", NAT_VALID_HOOKS, 0, 3 * sizeof(struct ebt_entries),
562 + { [NF_BR_PRE_ROUTING]&initial_chains[0], [NF_BR_LOCAL_OUT]&initial_chains[1],
563 + [NF_BR_POST_ROUTING]&initial_chains[2] }, 0, NULL, (char *)initial_chains
566 +static int check(const struct ebt_table_info *info, unsigned int valid_hooks)
568 + if (valid_hooks & ~NAT_VALID_HOOKS)
573 +static struct ebt_table frame_nat =
575 + {NULL, NULL}, "nat", &initial_table, NAT_VALID_HOOKS,
576 + RW_LOCK_UNLOCKED, check, NULL
580 +ebt_nat_dst(unsigned int hook, struct sk_buff **pskb, const struct net_device *in
581 + , const struct net_device *out, int (*okfn)(struct sk_buff *))
583 + return ebt_do_table(hook, pskb, in, out, &frame_nat);
587 +ebt_nat_src(unsigned int hook, struct sk_buff **pskb, const struct net_device *in
588 + , const struct net_device *out, int (*okfn)(struct sk_buff *))
590 + return ebt_do_table(hook, pskb, in, out, &frame_nat);
593 +static struct nf_hook_ops ebt_ops_nat[] = {
594 + { { NULL, NULL }, ebt_nat_dst, PF_BRIDGE, NF_BR_LOCAL_OUT,
595 + NF_BR_PRI_NAT_DST_OTHER},
596 + { { NULL, NULL }, ebt_nat_src, PF_BRIDGE, NF_BR_POST_ROUTING,
597 + NF_BR_PRI_NAT_SRC},
598 + { { NULL, NULL }, ebt_nat_dst, PF_BRIDGE, NF_BR_PRE_ROUTING,
599 + NF_BR_PRI_NAT_DST_BRIDGED},
602 +static int __init init(void)
606 + ret = ebt_register_table(&frame_nat);
609 + for (i = 0; i < sizeof(ebt_ops_nat) / sizeof(ebt_ops_nat[0]); i++)
610 + if ((ret = nf_register_hook(&ebt_ops_nat[i])) < 0)
614 + for (j = 0; j < i; j++)
615 + nf_unregister_hook(&ebt_ops_nat[j]);
616 + ebt_unregister_table(&frame_nat);
620 +static void __exit fini(void)
624 + for (i = 0; i < sizeof(ebt_ops_nat) / sizeof(ebt_ops_nat[0]); i++)
625 + nf_unregister_hook(&ebt_ops_nat[i]);
626 + ebt_unregister_table(&frame_nat);
632 +MODULE_LICENSE("GPL");
633 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
634 +++ linux-2.4.20/net/bridge/netfilter/ebtable_broute.c 2005-01-07 02:57:30.887818512 -0500
640 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
644 + * This table lets you choose between routing and bridging for frames
645 + * entering on a bridge enslaved nic. This table is traversed before any
646 + * other ebtables table. See net/bridge/br_input.c.
649 +#include <linux/netfilter_bridge/ebtables.h>
650 +#include <linux/module.h>
651 +#include <linux/if_bridge.h>
652 +#include <linux/brlock.h>
654 +// EBT_ACCEPT means the frame will be bridged
655 +// EBT_DROP means the frame will be routed
656 +static struct ebt_entries initial_chain =
657 + {0, "BROUTING", 0, EBT_ACCEPT, 0};
659 +static struct ebt_replace initial_table =
661 + "broute", 1 << NF_BR_BROUTING, 0, sizeof(struct ebt_entries),
662 + { [NF_BR_BROUTING]&initial_chain}, 0, NULL, (char *)&initial_chain
665 +static int check(const struct ebt_table_info *info, unsigned int valid_hooks)
667 + if (valid_hooks & ~(1 << NF_BR_BROUTING))
672 +static struct ebt_table broute_table =
674 + {NULL, NULL}, "broute", &initial_table, 1 << NF_BR_BROUTING,
675 + RW_LOCK_UNLOCKED, check, NULL
678 +static int ebt_broute(struct sk_buff **pskb)
682 + ret = ebt_do_table(NF_BR_BROUTING, pskb, (*pskb)->dev, NULL,
684 + if (ret == NF_DROP)
685 + return 1; // route it
686 + return 0; // bridge it
689 +static int __init init(void)
693 + ret = ebt_register_table(&broute_table);
696 + br_write_lock_bh(BR_NETPROTO_LOCK);
698 + br_should_route_hook = ebt_broute;
699 + br_write_unlock_bh(BR_NETPROTO_LOCK);
703 +static void __exit fini(void)
705 + br_write_lock_bh(BR_NETPROTO_LOCK);
706 + br_should_route_hook = NULL;
707 + br_write_unlock_bh(BR_NETPROTO_LOCK);
708 + ebt_unregister_table(&broute_table);
714 +MODULE_LICENSE("GPL");
715 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
716 +++ linux-2.4.20/net/bridge/netfilter/ebt_802_3.c 2005-01-07 02:57:30.888818360 -0500
722 + * Chris Vitale csv@bluetail.com
728 +#include <linux/netfilter_bridge/ebtables.h>
729 +#include <linux/netfilter_bridge/ebt_802_3.h>
730 +#include <linux/module.h>
732 +static int ebt_filter_802_3(const struct sk_buff *skb, const struct net_device *in,
733 + const struct net_device *out, const void *data, unsigned int datalen)
735 + struct ebt_802_3_info *info = (struct ebt_802_3_info *)data;
736 + struct ebt_802_3_hdr *hdr = (struct ebt_802_3_hdr *)skb->mac.ethernet;
737 + uint16_t type = hdr->llc.ui.ctrl & IS_UI ? hdr->llc.ui.type : hdr->llc.ni.type;
739 + if (info->bitmask & EBT_802_3_SAP) {
740 + if (FWINV(info->sap != hdr->llc.ui.ssap, EBT_802_3_SAP))
741 + return EBT_NOMATCH;
742 + if (FWINV(info->sap != hdr->llc.ui.dsap, EBT_802_3_SAP))
743 + return EBT_NOMATCH;
746 + if (info->bitmask & EBT_802_3_TYPE) {
747 + if (!(hdr->llc.ui.dsap == CHECK_TYPE && hdr->llc.ui.ssap == CHECK_TYPE))
748 + return EBT_NOMATCH;
749 + if (FWINV(info->type != type, EBT_802_3_TYPE))
750 + return EBT_NOMATCH;
756 +static struct ebt_match filter_802_3;
757 +static int ebt_802_3_check(const char *tablename, unsigned int hookmask,
758 + const struct ebt_entry *e, void *data, unsigned int datalen)
760 + struct ebt_802_3_info *info = (struct ebt_802_3_info *)data;
762 + if (datalen != EBT_ALIGN(sizeof(struct ebt_802_3_info)))
764 + if (info->bitmask & ~EBT_802_3_MASK || info->invflags & ~EBT_802_3_MASK)
770 +static struct ebt_match filter_802_3 =
772 + .name = EBT_802_3_MATCH,
773 + .match = ebt_filter_802_3,
774 + .check = ebt_802_3_check,
778 +static int __init init(void)
780 + return ebt_register_match(&filter_802_3);
783 +static void __exit fini(void)
785 + ebt_unregister_match(&filter_802_3);
791 +MODULE_LICENSE("GPL");
792 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
793 +++ linux-2.4.20/net/bridge/netfilter/ebt_mark.c 2005-01-07 02:57:30.888818360 -0500
799 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
805 +// The mark target can be used in any chain
806 +// I believe adding a mangle table just for marking is total overkill
807 +// Marking a frame doesn't really change anything in the frame anyway
809 +#include <linux/netfilter_bridge/ebtables.h>
810 +#include <linux/netfilter_bridge/ebt_mark_t.h>
811 +#include <linux/module.h>
813 +static int ebt_target_mark(struct sk_buff **pskb, unsigned int hooknr,
814 + const struct net_device *in, const struct net_device *out,
815 + const void *data, unsigned int datalen)
817 + struct ebt_mark_t_info *info = (struct ebt_mark_t_info *)data;
819 + if ((*pskb)->nfmark != info->mark) {
820 + (*pskb)->nfmark = info->mark;
821 + (*pskb)->nfcache |= NFC_ALTERED;
823 + return info->target;
826 +static int ebt_target_mark_check(const char *tablename, unsigned int hookmask,
827 + const struct ebt_entry *e, void *data, unsigned int datalen)
829 + struct ebt_mark_t_info *info = (struct ebt_mark_t_info *)data;
831 + if (datalen != EBT_ALIGN(sizeof(struct ebt_mark_t_info)))
833 + if (BASE_CHAIN && info->target == EBT_RETURN)
835 + CLEAR_BASE_CHAIN_BIT;
836 + if (INVALID_TARGET)
841 +static struct ebt_target mark_target =
843 + {NULL, NULL}, EBT_MARK_TARGET, ebt_target_mark,
844 + ebt_target_mark_check, NULL, THIS_MODULE
847 +static int __init init(void)
849 + return ebt_register_target(&mark_target);
852 +static void __exit fini(void)
854 + ebt_unregister_target(&mark_target);
860 +MODULE_LICENSE("GPL");
861 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
862 +++ linux-2.4.20/net/bridge/netfilter/ebt_mark_m.c 2005-01-07 02:57:30.888818360 -0500
868 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
874 +#include <linux/netfilter_bridge/ebtables.h>
875 +#include <linux/netfilter_bridge/ebt_mark_m.h>
876 +#include <linux/module.h>
878 +static int ebt_filter_mark(const struct sk_buff *skb,
879 + const struct net_device *in, const struct net_device *out, const void *data,
880 + unsigned int datalen)
882 + struct ebt_mark_m_info *info = (struct ebt_mark_m_info *) data;
884 + if (info->bitmask & EBT_MARK_OR)
885 + return !(!!(skb->nfmark & info->mask) ^ info->invert);
886 + return !(((skb->nfmark & info->mask) == info->mark) ^ info->invert);
889 +static int ebt_mark_check(const char *tablename, unsigned int hookmask,
890 + const struct ebt_entry *e, void *data, unsigned int datalen)
892 + struct ebt_mark_m_info *info = (struct ebt_mark_m_info *) data;
894 + if (datalen != EBT_ALIGN(sizeof(struct ebt_mark_m_info)))
896 + if (info->bitmask & ~EBT_MARK_MASK)
898 + if ((info->bitmask & EBT_MARK_OR) && (info->bitmask & EBT_MARK_AND))
900 + if (!info->bitmask)
905 +static struct ebt_match filter_mark =
907 + {NULL, NULL}, EBT_MARK_MATCH, ebt_filter_mark, ebt_mark_check, NULL,
911 +static int __init init(void)
913 + return ebt_register_match(&filter_mark);
916 +static void __exit fini(void)
918 + ebt_unregister_match(&filter_mark);
924 +MODULE_LICENSE("GPL");
925 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
926 +++ linux-2.4.20/net/bridge/netfilter/ebt_pkttype.c 2005-01-07 02:57:30.888818360 -0500
932 + * Bart De Schuymer <bdschuym@pandora.be>
938 +#include <linux/netfilter_bridge/ebtables.h>
939 +#include <linux/netfilter_bridge/ebt_pkttype.h>
940 +#include <linux/module.h>
942 +static int ebt_filter_pkttype(const struct sk_buff *skb,
943 + const struct net_device *in,
944 + const struct net_device *out,
946 + unsigned int datalen)
948 + struct ebt_pkttype_info *info = (struct ebt_pkttype_info *)data;
950 + return (skb->pkt_type != info->pkt_type) ^ info->invert;
953 +static int ebt_pkttype_check(const char *tablename, unsigned int hookmask,
954 + const struct ebt_entry *e, void *data, unsigned int datalen)
956 + struct ebt_pkttype_info *info = (struct ebt_pkttype_info *)data;
958 + if (datalen != EBT_ALIGN(sizeof(struct ebt_pkttype_info)))
960 + if (info->invert != 0 && info->invert != 1)
962 + /* Allow any pkt_type value */
966 +static struct ebt_match filter_pkttype =
968 + .name = EBT_PKTTYPE_MATCH,
969 + .match = ebt_filter_pkttype,
970 + .check = ebt_pkttype_check,
974 +static int __init init(void)
976 + return ebt_register_match(&filter_pkttype);
979 +static void __exit fini(void)
981 + ebt_unregister_match(&filter_pkttype);
987 +MODULE_LICENSE("GPL");
988 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
989 +++ linux-2.4.20/net/bridge/netfilter/ebt_stp.c 2005-01-07 02:57:30.889818208 -0500
995 + * Bart De Schuymer <bdschuym@pandora.be>
996 + * Stephen Hemminger <shemminger@osdl.org>
1001 +#include <linux/netfilter_bridge/ebtables.h>
1002 +#include <linux/netfilter_bridge/ebt_stp.h>
1003 +#include <linux/module.h>
1005 +#define BPDU_TYPE_CONFIG 0
1006 +#define BPDU_TYPE_TCN 0x80
1008 +struct stp_header {
1017 +struct stp_config_pdu {
1020 + uint8_t root_cost[4];
1021 + uint8_t sender[8];
1023 + uint8_t msg_age[2];
1024 + uint8_t max_age[2];
1025 + uint8_t hello_time[2];
1026 + uint8_t forward_delay[2];
1029 +#define NR16(p) (p[0] << 8 | p[1])
1030 +#define NR32(p) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3])
1032 +static int ebt_filter_config(struct ebt_stp_info *info,
1033 + struct stp_config_pdu *stpc)
1035 + struct ebt_stp_config_info *c;
1040 + c = &info->config;
1041 + if ((info->bitmask & EBT_STP_FLAGS) &&
1042 + FWINV(c->flags != stpc->flags, EBT_STP_FLAGS))
1043 + return EBT_NOMATCH;
1044 + if (info->bitmask & EBT_STP_ROOTPRIO) {
1045 + v16 = NR16(stpc->root);
1046 + if (FWINV(v16 < c->root_priol ||
1047 + v16 > c->root_priou, EBT_STP_ROOTPRIO))
1048 + return EBT_NOMATCH;
1050 + if (info->bitmask & EBT_STP_ROOTADDR) {
1052 + for (i = 0; i < 6; i++)
1053 + verdict |= (stpc->root[2+i] ^ c->root_addr[i]) &
1054 + c->root_addrmsk[i];
1055 + if (FWINV(verdict != 0, EBT_STP_ROOTADDR))
1056 + return EBT_NOMATCH;
1058 + if (info->bitmask & EBT_STP_ROOTCOST) {
1059 + v32 = NR32(stpc->root_cost);
1060 + if (FWINV(v32 < c->root_costl ||
1061 + v32 > c->root_costu, EBT_STP_ROOTCOST))
1062 + return EBT_NOMATCH;
1064 + if (info->bitmask & EBT_STP_SENDERPRIO) {
1065 + v16 = NR16(stpc->sender);
1066 + if (FWINV(v16 < c->sender_priol ||
1067 + v16 > c->sender_priou, EBT_STP_SENDERPRIO))
1068 + return EBT_NOMATCH;
1070 + if (info->bitmask & EBT_STP_SENDERADDR) {
1072 + for (i = 0; i < 6; i++)
1073 + verdict |= (stpc->sender[2+i] ^ c->sender_addr[i]) &
1074 + c->sender_addrmsk[i];
1075 + if (FWINV(verdict != 0, EBT_STP_SENDERADDR))
1076 + return EBT_NOMATCH;
1078 + if (info->bitmask & EBT_STP_PORT) {
1079 + v16 = NR16(stpc->port);
1080 + if (FWINV(v16 < c->portl ||
1081 + v16 > c->portu, EBT_STP_PORT))
1082 + return EBT_NOMATCH;
1084 + if (info->bitmask & EBT_STP_MSGAGE) {
1085 + v16 = NR16(stpc->msg_age);
1086 + if (FWINV(v16 < c->msg_agel ||
1087 + v16 > c->msg_ageu, EBT_STP_MSGAGE))
1088 + return EBT_NOMATCH;
1090 + if (info->bitmask & EBT_STP_MAXAGE) {
1091 + v16 = NR16(stpc->max_age);
1092 + if (FWINV(v16 < c->max_agel ||
1093 + v16 > c->max_ageu, EBT_STP_MAXAGE))
1094 + return EBT_NOMATCH;
1096 + if (info->bitmask & EBT_STP_HELLOTIME) {
1097 + v16 = NR16(stpc->hello_time);
1098 + if (FWINV(v16 < c->hello_timel ||
1099 + v16 > c->hello_timeu, EBT_STP_HELLOTIME))
1100 + return EBT_NOMATCH;
1102 + if (info->bitmask & EBT_STP_FWDD) {
1103 + v16 = NR16(stpc->forward_delay);
1104 + if (FWINV(v16 < c->forward_delayl ||
1105 + v16 > c->forward_delayu, EBT_STP_FWDD))
1106 + return EBT_NOMATCH;
1111 +static int ebt_filter_stp(const struct sk_buff *skb, const struct net_device *in,
1112 + const struct net_device *out, const void *data, unsigned int datalen)
1114 + struct ebt_stp_info *info = (struct ebt_stp_info *)data;
1115 + struct stp_header stph;
1116 + uint8_t header[6] = {0x42, 0x42, 0x03, 0x00, 0x00, 0x00};
1117 + if (skb_copy_bits(skb, 0, &stph, sizeof(stph)))
1118 + return EBT_NOMATCH;
1120 + /* The stp code only considers these */
1121 + if (memcmp(&stph, header, sizeof(header)))
1122 + return EBT_NOMATCH;
1124 + if (info->bitmask & EBT_STP_TYPE
1125 + && FWINV(info->type != stph.type, EBT_STP_TYPE))
1126 + return EBT_NOMATCH;
1128 + if (stph.type == BPDU_TYPE_CONFIG &&
1129 + info->bitmask & EBT_STP_CONFIG_MASK) {
1130 + struct stp_config_pdu stpc;
1132 + if (skb_copy_bits(skb, sizeof(stph), &stpc, sizeof(stpc)))
1133 + return EBT_NOMATCH;
1134 + return ebt_filter_config(info, &stpc);
1139 +static int ebt_stp_check(const char *tablename, unsigned int hookmask,
1140 + const struct ebt_entry *e, void *data, unsigned int datalen)
1142 + struct ebt_stp_info *info = (struct ebt_stp_info *)data;
1143 + int len = EBT_ALIGN(sizeof(struct ebt_stp_info));
1144 + uint8_t bridge_ula[6] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
1145 + uint8_t msk[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1147 + if (info->bitmask & ~EBT_STP_MASK || info->invflags & ~EBT_STP_MASK ||
1148 + !(info->bitmask & EBT_STP_MASK))
1150 + if (datalen != len)
1152 + /* Make sure the match only receives stp frames */
1153 + if (memcmp(e->destmac, bridge_ula, ETH_ALEN) ||
1154 + memcmp(e->destmsk, msk, ETH_ALEN) || !(e->bitmask & EBT_DESTMAC))
1160 +static struct ebt_match filter_stp =
1162 + .name = EBT_STP_MATCH,
1163 + .match = ebt_filter_stp,
1164 + .check = ebt_stp_check,
1165 + .me = THIS_MODULE,
1168 +static int __init init(void)
1170 + return ebt_register_match(&filter_stp);
1173 +static void __exit fini(void)
1175 + ebt_unregister_match(&filter_stp);
1181 +MODULE_LICENSE("GPL");
1182 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
1183 +++ linux-2.4.20/net/bridge/netfilter/ebt_redirect.c 2005-01-07 02:57:30.889818208 -0500
1189 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
1195 +#include <linux/netfilter_bridge/ebtables.h>
1196 +#include <linux/netfilter_bridge/ebt_redirect.h>
1197 +#include <linux/module.h>
1198 +#include <net/sock.h>
1199 +#include "../br_private.h"
1201 +static int ebt_target_redirect(struct sk_buff **pskb, unsigned int hooknr,
1202 + const struct net_device *in, const struct net_device *out,
1203 + const void *data, unsigned int datalen)
1205 + struct ebt_redirect_info *info = (struct ebt_redirect_info *)data;
1207 + if (hooknr != NF_BR_BROUTING)
1208 + memcpy((**pskb).mac.ethernet->h_dest,
1209 + in->br_port->br->dev.dev_addr, ETH_ALEN);
1211 + memcpy((**pskb).mac.ethernet->h_dest,
1212 + in->dev_addr, ETH_ALEN);
1213 + (*pskb)->pkt_type = PACKET_HOST;
1215 + return info->target;
1218 +static int ebt_target_redirect_check(const char *tablename, unsigned int hookmask,
1219 + const struct ebt_entry *e, void *data, unsigned int datalen)
1221 + struct ebt_redirect_info *info = (struct ebt_redirect_info *)data;
1223 + if (datalen != EBT_ALIGN(sizeof(struct ebt_redirect_info)))
1225 + if (BASE_CHAIN && info->target == EBT_RETURN)
1227 + CLEAR_BASE_CHAIN_BIT;
1228 + if ( (strcmp(tablename, "nat") || hookmask & ~(1 << NF_BR_PRE_ROUTING)) &&
1229 + (strcmp(tablename, "broute") || hookmask & ~(1 << NF_BR_BROUTING)) )
1231 + if (INVALID_TARGET)
1236 +static struct ebt_target redirect_target =
1238 + {NULL, NULL}, EBT_REDIRECT_TARGET, ebt_target_redirect,
1239 + ebt_target_redirect_check, NULL, THIS_MODULE
1242 +static int __init init(void)
1244 + return ebt_register_target(&redirect_target);
1247 +static void __exit fini(void)
1249 + ebt_unregister_target(&redirect_target);
1255 +MODULE_LICENSE("GPL");
1256 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
1257 +++ linux-2.4.20/net/bridge/netfilter/ebt_arp.c 2005-01-07 02:57:30.889818208 -0500
1263 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
1264 + * Tim Gardner <timg@tpi.com>
1270 +#include <linux/netfilter_bridge/ebtables.h>
1271 +#include <linux/netfilter_bridge/ebt_arp.h>
1272 +#include <linux/if_arp.h>
1273 +#include <linux/if_ether.h>
1274 +#include <linux/module.h>
1276 +static int ebt_filter_arp(const struct sk_buff *skb, const struct net_device *in,
1277 + const struct net_device *out, const void *data, unsigned int datalen)
1279 + struct ebt_arp_info *info = (struct ebt_arp_info *)data;
1281 + if (info->bitmask & EBT_ARP_OPCODE && FWINV(info->opcode !=
1282 + ((*skb).nh.arph)->ar_op, EBT_ARP_OPCODE))
1283 + return EBT_NOMATCH;
1284 + if (info->bitmask & EBT_ARP_HTYPE && FWINV(info->htype !=
1285 + ((*skb).nh.arph)->ar_hrd, EBT_ARP_HTYPE))
1286 + return EBT_NOMATCH;
1287 + if (info->bitmask & EBT_ARP_PTYPE && FWINV(info->ptype !=
1288 + ((*skb).nh.arph)->ar_pro, EBT_ARP_PTYPE))
1289 + return EBT_NOMATCH;
1291 + if (info->bitmask & (EBT_ARP_SRC_IP | EBT_ARP_DST_IP))
1293 + uint32_t arp_len = sizeof(struct arphdr) +
1294 + (2 * (((*skb).nh.arph)->ar_hln)) +
1295 + (2 * (((*skb).nh.arph)->ar_pln));
1299 + // Make sure the packet is long enough.
1300 + if ((((*skb).nh.raw) + arp_len) > (*skb).tail)
1301 + return EBT_NOMATCH;
1302 + // IPv4 addresses are always 4 bytes.
1303 + if (((*skb).nh.arph)->ar_pln != sizeof(uint32_t))
1304 + return EBT_NOMATCH;
1306 + if (info->bitmask & EBT_ARP_SRC_IP) {
1307 + memcpy(&src, ((*skb).nh.raw) + sizeof(struct arphdr) +
1308 + ((*skb).nh.arph)->ar_hln, sizeof(uint32_t));
1309 + if (FWINV(info->saddr != (src & info->smsk),
1311 + return EBT_NOMATCH;
1314 + if (info->bitmask & EBT_ARP_DST_IP) {
1315 + memcpy(&dst, ((*skb).nh.raw)+sizeof(struct arphdr) +
1316 + (2*(((*skb).nh.arph)->ar_hln)) +
1317 + (((*skb).nh.arph)->ar_pln), sizeof(uint32_t));
1318 + if (FWINV(info->daddr != (dst & info->dmsk),
1320 + return EBT_NOMATCH;
1324 + if (info->bitmask & (EBT_ARP_SRC_MAC | EBT_ARP_DST_MAC))
1326 + uint32_t arp_len = sizeof(struct arphdr) +
1327 + (2 * (((*skb).nh.arph)->ar_hln)) +
1328 + (2 * (((*skb).nh.arph)->ar_pln));
1329 + unsigned char dst[ETH_ALEN];
1330 + unsigned char src[ETH_ALEN];
1332 + // Make sure the packet is long enough.
1333 + if ((((*skb).nh.raw) + arp_len) > (*skb).tail)
1334 + return EBT_NOMATCH;
1335 + // MAC addresses are 6 bytes.
1336 + if (((*skb).nh.arph)->ar_hln != ETH_ALEN)
1337 + return EBT_NOMATCH;
1338 + if (info->bitmask & EBT_ARP_SRC_MAC) {
1339 + uint8_t verdict, i;
1341 + memcpy(&src, ((*skb).nh.raw) +
1342 + sizeof(struct arphdr),
1345 + for (i = 0; i < 6; i++)
1346 + verdict |= (src[i] ^ info->smaddr[i]) &
1348 + if (FWINV(verdict != 0, EBT_ARP_SRC_MAC))
1349 + return EBT_NOMATCH;
1352 + if (info->bitmask & EBT_ARP_DST_MAC) {
1353 + uint8_t verdict, i;
1355 + memcpy(&dst, ((*skb).nh.raw) +
1356 + sizeof(struct arphdr) +
1357 + (((*skb).nh.arph)->ar_hln) +
1358 + (((*skb).nh.arph)->ar_pln),
1361 + for (i = 0; i < 6; i++)
1362 + verdict |= (dst[i] ^ info->dmaddr[i]) &
1364 + if (FWINV(verdict != 0, EBT_ARP_DST_MAC))
1365 + return EBT_NOMATCH;
1372 +static int ebt_arp_check(const char *tablename, unsigned int hookmask,
1373 + const struct ebt_entry *e, void *data, unsigned int datalen)
1375 + struct ebt_arp_info *info = (struct ebt_arp_info *)data;
1377 + if (datalen != EBT_ALIGN(sizeof(struct ebt_arp_info)))
1379 + if ((e->ethproto != __constant_htons(ETH_P_ARP) &&
1380 + e->ethproto != __constant_htons(ETH_P_RARP)) ||
1381 + e->invflags & EBT_IPROTO)
1383 + if (info->bitmask & ~EBT_ARP_MASK || info->invflags & ~EBT_ARP_MASK)
1388 +static struct ebt_match filter_arp =
1390 + {NULL, NULL}, EBT_ARP_MATCH, ebt_filter_arp, ebt_arp_check, NULL,
1394 +static int __init init(void)
1396 + return ebt_register_match(&filter_arp);
1399 +static void __exit fini(void)
1401 + ebt_unregister_match(&filter_arp);
1407 +MODULE_LICENSE("GPL");
1408 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
1409 +++ linux-2.4.20/net/bridge/netfilter/ebt_ip.c 2005-01-07 02:57:30.890818056 -0500
1415 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
1420 + * added ip-sport and ip-dport
1421 + * Innominate Security Technologies AG <mhopf@innominate.com>
1425 +#include <linux/netfilter_bridge/ebtables.h>
1426 +#include <linux/netfilter_bridge/ebt_ip.h>
1427 +#include <linux/ip.h>
1428 +#include <linux/in.h>
1429 +#include <linux/module.h>
1437 + unsigned char *raw;
1438 + struct tcpudphdr *tuh;
1441 +static int ebt_filter_ip(const struct sk_buff *skb, const struct net_device *in,
1442 + const struct net_device *out, const void *data,
1443 + unsigned int datalen)
1445 + struct ebt_ip_info *info = (struct ebt_ip_info *)data;
1447 + if (info->bitmask & EBT_IP_TOS &&
1448 + FWINV(info->tos != ((*skb).nh.iph)->tos, EBT_IP_TOS))
1449 + return EBT_NOMATCH;
1450 + if (info->bitmask & EBT_IP_PROTO) {
1451 + if (FWINV(info->protocol != ((*skb).nh.iph)->protocol,
1453 + return EBT_NOMATCH;
1454 + if ( info->protocol == IPPROTO_TCP ||
1455 + info->protocol == IPPROTO_UDP )
1458 + h.raw = skb->data + skb->nh.iph->ihl*4;
1459 + if (info->bitmask & EBT_IP_DPORT) {
1460 + uint16_t port = ntohs(h.tuh->dst);
1461 + if (FWINV(port < info->dport[0] ||
1462 + port > info->dport[1],
1464 + return EBT_NOMATCH;
1466 + if (info->bitmask & EBT_IP_SPORT) {
1467 + uint16_t port = ntohs(h.tuh->src);
1468 + if (FWINV(port < info->sport[0] ||
1469 + port > info->sport[1],
1471 + return EBT_NOMATCH;
1475 + if (info->bitmask & EBT_IP_SOURCE &&
1476 + FWINV((((*skb).nh.iph)->saddr & info->smsk) !=
1477 + info->saddr, EBT_IP_SOURCE))
1478 + return EBT_NOMATCH;
1479 + if ((info->bitmask & EBT_IP_DEST) &&
1480 + FWINV((((*skb).nh.iph)->daddr & info->dmsk) !=
1481 + info->daddr, EBT_IP_DEST))
1482 + return EBT_NOMATCH;
1486 +static int ebt_ip_check(const char *tablename, unsigned int hookmask,
1487 + const struct ebt_entry *e, void *data, unsigned int datalen)
1489 + struct ebt_ip_info *info = (struct ebt_ip_info *)data;
1491 + if (datalen != EBT_ALIGN(sizeof(struct ebt_ip_info)))
1493 + if (e->ethproto != __constant_htons(ETH_P_IP) ||
1494 + e->invflags & EBT_IPROTO)
1496 + if (info->bitmask & ~EBT_IP_MASK || info->invflags & ~EBT_IP_MASK)
1498 + if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) {
1499 + if (!info->bitmask & EBT_IPROTO)
1501 + if (info->protocol != IPPROTO_TCP &&
1502 + info->protocol != IPPROTO_UDP)
1505 + if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1])
1507 + if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1])
1512 +static struct ebt_match filter_ip =
1514 + {NULL, NULL}, EBT_IP_MATCH, ebt_filter_ip, ebt_ip_check, NULL,
1518 +static int __init init(void)
1520 + return ebt_register_match(&filter_ip);
1523 +static void __exit fini(void)
1525 + ebt_unregister_match(&filter_ip);
1531 +MODULE_LICENSE("GPL");
1532 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
1533 +++ linux-2.4.20/net/bridge/netfilter/ebt_vlan.c 2005-01-07 02:57:30.890818056 -0500
1536 + * Description: EBTables 802.1Q match extension kernelspace module.
1537 + * Authors: Nick Fedchik <nick@fedchik.org.ua>
1538 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
1540 + * This program is free software; you can redistribute it and/or modify
1541 + * it under the terms of the GNU General Public License as published by
1542 + * the Free Software Foundation; either version 2 of the License, or
1543 + * (at your option) any later version.
1545 + * This program is distributed in the hope that it will be useful,
1546 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1547 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1548 + * GNU General Public License for more details.
1550 + * You should have received a copy of the GNU General Public License
1551 + * along with this program; if not, write to the Free Software
1552 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1555 +#include <linux/if_ether.h>
1556 +#include <linux/if_vlan.h>
1557 +#include <linux/module.h>
1558 +#include <linux/netfilter_bridge/ebtables.h>
1559 +#include <linux/netfilter_bridge/ebt_vlan.h>
1561 +static unsigned char debug;
1562 +#define MODULE_VERSION "0.6"
1564 +MODULE_PARM(debug, "0-1b");
1565 +MODULE_PARM_DESC(debug, "debug=1 is turn on debug messages");
1566 +MODULE_AUTHOR("Nick Fedchik <nick@fedchik.org.ua>");
1567 +MODULE_DESCRIPTION("802.1Q match module (ebtables extension), v"
1569 +MODULE_LICENSE("GPL");
1572 +#define DEBUG_MSG(args...) if (debug) printk (KERN_DEBUG "ebt_vlan: " args)
1573 +#define INV_FLAG(_inv_flag_) (info->invflags & _inv_flag_) ? "!" : ""
1574 +#define GET_BITMASK(_BIT_MASK_) info->bitmask & _BIT_MASK_
1575 +#define SET_BITMASK(_BIT_MASK_) info->bitmask |= _BIT_MASK_
1576 +#define EXIT_ON_MISMATCH(_MATCH_,_MASK_) if (!((info->_MATCH_ == _MATCH_)^!!(info->invflags & _MASK_))) return 1;
1579 + * Function description: ebt_filter_vlan() is main engine for
1580 + * checking passed 802.1Q frame according to
1581 + * the passed extension parameters (in the *data buffer)
1582 + * ebt_filter_vlan() is called after successfull check the rule params
1583 + * by ebt_check_vlan() function.
1585 + * const struct sk_buff *skb - pointer to passed ethernet frame buffer
1586 + * const void *data - pointer to passed extension parameters
1587 + * unsigned int datalen - length of passed *data buffer
1588 + * const struct net_device *in -
1589 + * const struct net_device *out -
1590 + * const struct ebt_counter *c -
1591 + * Returned values:
1592 + * 0 - ok (all rule params matched)
1593 + * 1 - miss (rule params not acceptable to the parsed frame)
1596 +ebt_filter_vlan(const struct sk_buff *skb,
1597 + const struct net_device *in,
1598 + const struct net_device *out,
1599 + const void *data, unsigned int datalen)
1601 + struct ebt_vlan_info *info = (struct ebt_vlan_info *) data; /* userspace data */
1602 + struct vlan_ethhdr *frame = (struct vlan_ethhdr *) skb->mac.raw; /* Passed tagged frame */
1604 + unsigned short TCI; /* Whole TCI, given from parsed frame */
1605 + unsigned short id; /* VLAN ID, given from frame TCI */
1606 + unsigned char prio; /* user_priority, given from frame TCI */
1607 + unsigned short encap; /* VLAN encapsulated Type/Length field, given from orig frame */
1610 + * Tag Control Information (TCI) consists of the following elements:
1611 + * - User_priority. The user_priority field is three bits in length,
1612 + * interpreted as a binary number.
1613 + * - Canonical Format Indicator (CFI). The Canonical Format Indicator
1614 + * (CFI) is a single bit flag value. Currently ignored.
1615 + * - VLAN Identifier (VID). The VID is encoded as
1616 + * an unsigned binary number.
1618 + TCI = ntohs(frame->h_vlan_TCI);
1619 + id = TCI & VLAN_VID_MASK;
1620 + prio = (TCI >> 13) & 0x7;
1621 + encap = frame->h_vlan_encapsulated_proto;
1624 + * Checking VLAN Identifier (VID)
1626 + if (GET_BITMASK(EBT_VLAN_ID)) { /* Is VLAN ID parsed? */
1627 + EXIT_ON_MISMATCH(id, EBT_VLAN_ID);
1630 + * Checking user_priority
1632 + if (GET_BITMASK(EBT_VLAN_PRIO)) { /* Is VLAN user_priority parsed? */
1633 + EXIT_ON_MISMATCH(prio, EBT_VLAN_PRIO);
1636 + * Checking Encapsulated Proto (Length/Type) field
1638 + if (GET_BITMASK(EBT_VLAN_ENCAP)) { /* Is VLAN Encap parsed? */
1639 + EXIT_ON_MISMATCH(encap, EBT_VLAN_ENCAP);
1642 + * All possible extension parameters was parsed.
1643 + * If rule never returned by missmatch, then all ok.
1649 + * Function description: ebt_vlan_check() is called when userspace
1650 + * delivers the table entry to the kernel,
1651 + * and to check that userspace doesn't give a bad table.
1653 + * const char *tablename - table name string
1654 + * unsigned int hooknr - hook number
1655 + * const struct ebt_entry *e - ebtables entry basic set
1656 + * const void *data - pointer to passed extension parameters
1657 + * unsigned int datalen - length of passed *data buffer
1658 + * Returned values:
1659 + * 0 - ok (all delivered rule params are correct)
1660 + * 1 - miss (rule params is out of range, invalid, incompatible, etc.)
1663 +ebt_check_vlan(const char *tablename,
1664 + unsigned int hooknr,
1665 + const struct ebt_entry *e, void *data, unsigned int datalen)
1667 + struct ebt_vlan_info *info = (struct ebt_vlan_info *) data;
1670 + * Parameters buffer overflow check
1672 + if (datalen != EBT_ALIGN(sizeof(struct ebt_vlan_info))) {
1674 + ("passed size %d is not eq to ebt_vlan_info (%d)\n",
1675 + datalen, sizeof(struct ebt_vlan_info));
1680 + * Is it 802.1Q frame checked?
1682 + if (e->ethproto != __constant_htons(ETH_P_8021Q)) {
1684 + ("passed entry proto %2.4X is not 802.1Q (8100)\n",
1685 + (unsigned short) ntohs(e->ethproto));
1690 + * Check for bitmask range
1691 + * True if even one bit is out of mask
1693 + if (info->bitmask & ~EBT_VLAN_MASK) {
1694 + DEBUG_MSG("bitmask %2X is out of mask (%2X)\n",
1695 + info->bitmask, EBT_VLAN_MASK);
1700 + * Check for inversion flags range
1702 + if (info->invflags & ~EBT_VLAN_MASK) {
1703 + DEBUG_MSG("inversion flags %2X is out of mask (%2X)\n",
1704 + info->invflags, EBT_VLAN_MASK);
1709 + * Reserved VLAN ID (VID) values
1710 + * -----------------------------
1711 + * 0 - The null VLAN ID.
1712 + * 1 - The default Port VID (PVID)
1713 + * 0x0FFF - Reserved for implementation use.
1714 + * if_vlan.h: VLAN_GROUP_ARRAY_LEN 4096.
1716 + if (GET_BITMASK(EBT_VLAN_ID)) { /* when vlan-id param was spec-ed */
1717 + if (!!info->id) { /* if id!=0 => check vid range */
1718 + if (info->id > VLAN_GROUP_ARRAY_LEN) {
1720 + ("id %d is out of range (1-4096)\n",
1725 + * Note: This is valid VLAN-tagged frame point.
1726 + * Any value of user_priority are acceptable,
1727 + * but should be ignored according to 802.1Q Std.
1728 + * So we just drop the prio flag.
1730 + info->bitmask &= ~EBT_VLAN_PRIO;
1733 + * Else, id=0 (null VLAN ID) => user_priority range (any?)
1737 + if (GET_BITMASK(EBT_VLAN_PRIO)) {
1738 + if ((unsigned char) info->prio > 7) {
1740 + ("prio %d is out of range (0-7)\n",
1746 + * Check for encapsulated proto range - it is possible to be
1747 + * any value for u_short range.
1748 + * if_ether.h: ETH_ZLEN 60 - Min. octets in frame sans FCS
1750 + if (GET_BITMASK(EBT_VLAN_ENCAP)) {
1751 + if ((unsigned short) ntohs(info->encap) < ETH_ZLEN) {
1753 + ("encap frame length %d is less than minimal\n",
1754 + ntohs(info->encap));
1762 +static struct ebt_match filter_vlan = {
1772 + * Module initialization function.
1774 +static int __init init(void)
1776 + DEBUG_MSG("ebtables 802.1Q extension module v"
1777 + MODULE_VERSION "\n");
1778 + DEBUG_MSG("module debug=%d\n", !!debug);
1779 + return ebt_register_match(&filter_vlan);
1783 + * Module "finalization" function
1785 +static void __exit fini(void)
1787 + ebt_unregister_match(&filter_vlan);
1794 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
1795 +++ linux-2.4.20/net/bridge/netfilter/ebt_log.c 2005-01-07 02:57:30.890818056 -0500
1801 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
1807 +#include <linux/netfilter_bridge/ebtables.h>
1808 +#include <linux/netfilter_bridge/ebt_log.h>
1809 +#include <linux/module.h>
1810 +#include <linux/ip.h>
1811 +#include <linux/in.h>
1812 +#include <linux/if_arp.h>
1813 +#include <linux/spinlock.h>
1815 +static spinlock_t ebt_log_lock = SPIN_LOCK_UNLOCKED;
1817 +static int ebt_log_check(const char *tablename, unsigned int hookmask,
1818 + const struct ebt_entry *e, void *data, unsigned int datalen)
1820 + struct ebt_log_info *info = (struct ebt_log_info *)data;
1822 + if (datalen != EBT_ALIGN(sizeof(struct ebt_log_info)))
1824 + if (info->bitmask & ~EBT_LOG_MASK)
1826 + if (info->loglevel >= 8)
1828 + info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0';
1840 + unsigned char mac_src[ETH_ALEN];
1841 + unsigned char ip_src[4];
1842 + unsigned char mac_dst[ETH_ALEN];
1843 + unsigned char ip_dst[4];
1846 +static void print_MAC(unsigned char *p)
1850 + for (i = 0; i < ETH_ALEN; i++, p++)
1851 + printk("%02x%c", *p, i == ETH_ALEN - 1 ? ' ':':');
1854 +#define myNIPQUAD(a) a[0], a[1], a[2], a[3]
1855 +static void ebt_log(const struct sk_buff *skb, const struct net_device *in,
1856 + const struct net_device *out, const void *data, unsigned int datalen)
1858 + struct ebt_log_info *info = (struct ebt_log_info *)data;
1859 + char level_string[4] = "< >";
1860 + level_string[1] = '0' + info->loglevel;
1862 + spin_lock_bh(&ebt_log_lock);
1863 + printk(level_string);
1864 + printk("%s IN=%s OUT=%s ", info->prefix, in ? in->name : "",
1865 + out ? out->name : "");
1867 + printk("MAC source = ");
1868 + print_MAC((skb->mac.ethernet)->h_source);
1869 + printk("MAC dest = ");
1870 + print_MAC((skb->mac.ethernet)->h_dest);
1872 + printk("proto = 0x%04x", ntohs(((*skb).mac.ethernet)->h_proto));
1874 + if ((info->bitmask & EBT_LOG_IP) && skb->mac.ethernet->h_proto ==
1876 + struct iphdr *iph = skb->nh.iph;
1877 + printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u,",
1878 + NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
1879 + printk(" IP tos=0x%02X, IP proto=%d", iph->tos, iph->protocol);
1880 + if (iph->protocol == IPPROTO_TCP ||
1881 + iph->protocol == IPPROTO_UDP) {
1882 + struct tcpudphdr *ports = (struct tcpudphdr *)(skb->data + iph->ihl*4);
1884 + if (skb->data + iph->ihl*4 > skb->tail) {
1885 + printk(" INCOMPLETE TCP/UDP header");
1888 + printk(" SPT=%u DPT=%u", ntohs(ports->src),
1889 + ntohs(ports->dst));
1894 + if ((info->bitmask & EBT_LOG_ARP) &&
1895 + ((skb->mac.ethernet->h_proto == __constant_htons(ETH_P_ARP)) ||
1896 + (skb->mac.ethernet->h_proto == __constant_htons(ETH_P_RARP)))) {
1897 + struct arphdr * arph = skb->nh.arph;
1898 + printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
1899 + ntohs(arph->ar_hrd), ntohs(arph->ar_pro),
1900 + ntohs(arph->ar_op));
1901 + /* If it's for Ethernet and the lengths are OK,
1902 + * then log the ARP payload */
1903 + if (arph->ar_hrd == __constant_htons(1) &&
1904 + arph->ar_hln == ETH_ALEN &&
1905 + arph->ar_pln == sizeof(uint32_t)) {
1906 + struct arppayload *arpp = (struct arppayload *)(skb->data + sizeof(*arph));
1908 + if (skb->data + sizeof(*arph) > skb->tail) {
1909 + printk(" INCOMPLETE ARP header");
1913 + printk(" ARP MAC SRC=");
1914 + print_MAC(arpp->mac_src);
1915 + printk(" ARP IP SRC=%u.%u.%u.%u",
1916 + myNIPQUAD(arpp->ip_src));
1917 + printk(" ARP MAC DST=");
1918 + print_MAC(arpp->mac_dst);
1919 + printk(" ARP IP DST=%u.%u.%u.%u",
1920 + myNIPQUAD(arpp->ip_dst));
1926 + spin_unlock_bh(&ebt_log_lock);
1929 +static struct ebt_watcher log =
1931 + {NULL, NULL}, EBT_LOG_WATCHER, ebt_log, ebt_log_check, NULL,
1935 +static int __init init(void)
1937 + return ebt_register_watcher(&log);
1940 +static void __exit fini(void)
1942 + ebt_unregister_watcher(&log);
1948 +MODULE_LICENSE("GPL");
1949 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
1950 +++ linux-2.4.20/net/bridge/netfilter/ebt_snat.c 2005-01-07 02:57:30.891817904 -0500
1956 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
1962 +#include <linux/netfilter_bridge/ebtables.h>
1963 +#include <linux/netfilter_bridge/ebt_nat.h>
1964 +#include <linux/module.h>
1966 +static int ebt_target_snat(struct sk_buff **pskb, unsigned int hooknr,
1967 + const struct net_device *in, const struct net_device *out,
1968 + const void *data, unsigned int datalen)
1970 + struct ebt_nat_info *info = (struct ebt_nat_info *) data;
1972 + memcpy(((**pskb).mac.ethernet)->h_source, info->mac,
1973 + ETH_ALEN * sizeof(unsigned char));
1974 + return info->target;
1977 +static int ebt_target_snat_check(const char *tablename, unsigned int hookmask,
1978 + const struct ebt_entry *e, void *data, unsigned int datalen)
1980 + struct ebt_nat_info *info = (struct ebt_nat_info *) data;
1982 + if (datalen != EBT_ALIGN(sizeof(struct ebt_nat_info)))
1984 + if (BASE_CHAIN && info->target == EBT_RETURN)
1986 + CLEAR_BASE_CHAIN_BIT;
1987 + if (strcmp(tablename, "nat"))
1989 + if (hookmask & ~(1 << NF_BR_POST_ROUTING))
1991 + if (INVALID_TARGET)
1996 +static struct ebt_target snat =
1998 + {NULL, NULL}, EBT_SNAT_TARGET, ebt_target_snat, ebt_target_snat_check,
2002 +static int __init init(void)
2004 + return ebt_register_target(&snat);
2007 +static void __exit fini(void)
2009 + ebt_unregister_target(&snat);
2015 +MODULE_LICENSE("GPL");
2016 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
2017 +++ linux-2.4.20/net/bridge/netfilter/ebt_dnat.c 2005-01-07 02:57:30.891817904 -0500
2023 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
2029 +#include <linux/netfilter_bridge/ebtables.h>
2030 +#include <linux/netfilter_bridge/ebt_nat.h>
2031 +#include <linux/module.h>
2032 +#include <net/sock.h>
2034 +static int ebt_target_dnat(struct sk_buff **pskb, unsigned int hooknr,
2035 + const struct net_device *in, const struct net_device *out,
2036 + const void *data, unsigned int datalen)
2038 + struct ebt_nat_info *info = (struct ebt_nat_info *)data;
2040 + memcpy(((**pskb).mac.ethernet)->h_dest, info->mac,
2041 + ETH_ALEN * sizeof(unsigned char));
2042 + return info->target;
2045 +static int ebt_target_dnat_check(const char *tablename, unsigned int hookmask,
2046 + const struct ebt_entry *e, void *data, unsigned int datalen)
2048 + struct ebt_nat_info *info = (struct ebt_nat_info *)data;
2050 + if (BASE_CHAIN && info->target == EBT_RETURN)
2052 + CLEAR_BASE_CHAIN_BIT;
2053 + if ( (strcmp(tablename, "nat") ||
2054 + (hookmask & ~((1 << NF_BR_PRE_ROUTING) | (1 << NF_BR_LOCAL_OUT)))) &&
2055 + (strcmp(tablename, "broute") || hookmask & ~(1 << NF_BR_BROUTING)) )
2057 + if (datalen != EBT_ALIGN(sizeof(struct ebt_nat_info)))
2059 + if (INVALID_TARGET)
2064 +static struct ebt_target dnat =
2066 + {NULL, NULL}, EBT_DNAT_TARGET, ebt_target_dnat, ebt_target_dnat_check,
2070 +static int __init init(void)
2072 + return ebt_register_target(&dnat);
2075 +static void __exit fini(void)
2077 + ebt_unregister_target(&dnat);
2083 +MODULE_LICENSE("GPL");
2084 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
2085 +++ linux-2.4.20/net/bridge/netfilter/ebtables.c 2005-01-07 02:57:30.893817600 -0500
2091 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
2093 + * ebtables.c,v 2.0, July, 2002
2095 + * This code is stongly inspired on the iptables code which is
2096 + * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
2098 + * This program is free software; you can redistribute it and/or
2099 + * modify it under the terms of the GNU General Public License
2100 + * as published by the Free Software Foundation; either version
2101 + * 2 of the License, or (at your option) any later version.
2104 +// used for print_string
2105 +#include <linux/sched.h>
2106 +#include <linux/tty.h>
2108 +#include <linux/kmod.h>
2109 +#include <linux/module.h>
2110 +#include <linux/vmalloc.h>
2111 +#include <linux/netfilter_bridge/ebtables.h>
2112 +#include <linux/spinlock.h>
2113 +#include <asm/uaccess.h>
2114 +#include <linux/smp.h>
2115 +#include <net/sock.h>
2116 +// needed for logical [in,out]-dev filtering
2117 +#include "../br_private.h"
2120 +#define ASSERT_READ_LOCK(x)
2121 +#define ASSERT_WRITE_LOCK(x)
2122 +#include <linux/netfilter_ipv4/listhelp.h>
2124 +#if 0 // use this for remote debugging
2125 +// Copyright (C) 1998 by Ori Pomerantz
2126 +// Print the string to the appropriate tty, the one
2127 +// the current task uses
2128 +static void print_string(char *str)
2130 + struct tty_struct *my_tty;
2132 + /* The tty for the current task */
2133 + my_tty = current->tty;
2134 + if (my_tty != NULL) {
2135 + (*(my_tty->driver).write)(my_tty, 0, str, strlen(str));
2136 + (*(my_tty->driver).write)(my_tty, 0, "\015\012", 2);
2140 +#define BUGPRINT(args) print_string(args);
2142 +#define BUGPRINT(format, args...) printk("kernel msg: ebtables bug: please "\
2143 + "report to author: "format, ## args)
2144 +// #define BUGPRINT(format, args...)
2146 +#define MEMPRINT(format, args...) printk("kernel msg: ebtables "\
2147 + ": out of memory: "format, ## args)
2148 +// #define MEMPRINT(format, args...)
2152 +// Each cpu has its own set of counters, so there is no need for write_lock in
2154 +// For reading or updating the counters, the user context needs to
2155 +// get a write_lock
2157 +// The size of each set of counters is altered to get cache alignment
2158 +#define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
2159 +#define COUNTER_OFFSET(n) (SMP_ALIGN(n * sizeof(struct ebt_counter)))
2160 +#define COUNTER_BASE(c, n, cpu) ((struct ebt_counter *)(((char *)c) + \
2161 + COUNTER_OFFSET(n) * cpu))
2165 +static DECLARE_MUTEX(ebt_mutex);
2166 +static LIST_HEAD(ebt_tables);
2167 +static LIST_HEAD(ebt_targets);
2168 +static LIST_HEAD(ebt_matches);
2169 +static LIST_HEAD(ebt_watchers);
2171 +static struct ebt_target ebt_standard_target =
2172 +{ {NULL, NULL}, EBT_STANDARD_TARGET, NULL, NULL, NULL, NULL};
2174 +static inline int ebt_do_watcher (struct ebt_entry_watcher *w,
2175 + const struct sk_buff *skb, const struct net_device *in,
2176 + const struct net_device *out)
2178 + w->u.watcher->watcher(skb, in, out, w->data,
2180 + // watchers don't give a verdict
2184 +static inline int ebt_do_match (struct ebt_entry_match *m,
2185 + const struct sk_buff *skb, const struct net_device *in,
2186 + const struct net_device *out)
2188 + return m->u.match->match(skb, in, out, m->data,
2192 +static inline int ebt_dev_check(char *entry, const struct net_device *device)
2194 + if (*entry == '\0')
2198 + return !!strcmp(entry, device->name);
2201 +#define FWINV2(bool,invflg) ((bool) ^ !!(e->invflags & invflg))
2202 +// process standard matches
2203 +static inline int ebt_basic_match(struct ebt_entry *e, struct ethhdr *h,
2204 + const struct net_device *in, const struct net_device *out)
2208 + if (e->bitmask & EBT_802_3) {
2209 + if (FWINV2(ntohs(h->h_proto) >= 1536, EBT_IPROTO))
2211 + } else if (!(e->bitmask & EBT_NOPROTO) &&
2212 + FWINV2(e->ethproto != h->h_proto, EBT_IPROTO))
2215 + if (FWINV2(ebt_dev_check(e->in, in), EBT_IIN))
2217 + if (FWINV2(ebt_dev_check(e->out, out), EBT_IOUT))
2219 + if ((!in || !in->br_port) ? 0 : FWINV2(ebt_dev_check(
2220 + e->logical_in, &in->br_port->br->dev), EBT_ILOGICALIN))
2222 + if ((!out || !out->br_port) ? 0 : FWINV2(ebt_dev_check(
2223 + e->logical_out, &out->br_port->br->dev), EBT_ILOGICALOUT))
2226 + if (e->bitmask & EBT_SOURCEMAC) {
2228 + for (i = 0; i < 6; i++)
2229 + verdict |= (h->h_source[i] ^ e->sourcemac[i]) &
2231 + if (FWINV2(verdict != 0, EBT_ISOURCE) )
2234 + if (e->bitmask & EBT_DESTMAC) {
2236 + for (i = 0; i < 6; i++)
2237 + verdict |= (h->h_dest[i] ^ e->destmac[i]) &
2239 + if (FWINV2(verdict != 0, EBT_IDEST) )
2245 +// Do some firewalling
2246 +unsigned int ebt_do_table (unsigned int hook, struct sk_buff **pskb,
2247 + const struct net_device *in, const struct net_device *out,
2248 + struct ebt_table *table)
2251 + struct ebt_entry *point;
2252 + struct ebt_counter *counter_base, *cb_base;
2253 + struct ebt_entry_target *t;
2254 + int verdict, sp = 0;
2255 + struct ebt_chainstack *cs;
2256 + struct ebt_entries *chaininfo;
2258 + struct ebt_table_info *private = table->private;
2260 + read_lock_bh(&table->lock);
2261 + cb_base = COUNTER_BASE(private->counters, private->nentries,
2262 + cpu_number_map(smp_processor_id()));
2263 + if (private->chainstack)
2264 + cs = private->chainstack[cpu_number_map(smp_processor_id())];
2267 + chaininfo = private->hook_entry[hook];
2268 + nentries = private->hook_entry[hook]->nentries;
2269 + point = (struct ebt_entry *)(private->hook_entry[hook]->data);
2270 + counter_base = cb_base + private->hook_entry[hook]->counter_offset;
2271 + // base for chain jumps
2272 + base = private->entries;
2274 + while (i < nentries) {
2275 + if (ebt_basic_match(point, (**pskb).mac.ethernet, in, out))
2276 + goto letscontinue;
2278 + if (EBT_MATCH_ITERATE(point, ebt_do_match, *pskb, in, out) != 0)
2279 + goto letscontinue;
2281 + // increase counter
2282 + (*(counter_base + i)).pcnt++;
2283 + (*(counter_base + i)).bcnt+=(**pskb).len;
2285 + // these should only watch: not modify, nor tell us
2286 + // what to do with the packet
2287 + EBT_WATCHER_ITERATE(point, ebt_do_watcher, *pskb, in,
2290 + t = (struct ebt_entry_target *)
2291 + (((char *)point) + point->target_offset);
2292 + // standard target
2293 + if (!t->u.target->target)
2294 + verdict = ((struct ebt_standard_target *)t)->verdict;
2296 + verdict = t->u.target->target(pskb, hook,
2297 + in, out, t->data, t->target_size);
2298 + if (verdict == EBT_ACCEPT) {
2299 + read_unlock_bh(&table->lock);
2302 + if (verdict == EBT_DROP) {
2303 + read_unlock_bh(&table->lock);
2306 + if (verdict == EBT_RETURN) {
2308 +#ifdef CONFIG_NETFILTER_DEBUG
2310 + BUGPRINT("RETURN on base chain");
2311 + // act like this is EBT_CONTINUE
2312 + goto letscontinue;
2316 + // put all the local variables right
2318 + chaininfo = cs[sp].chaininfo;
2319 + nentries = chaininfo->nentries;
2321 + counter_base = cb_base +
2322 + chaininfo->counter_offset;
2325 + if (verdict == EBT_CONTINUE)
2326 + goto letscontinue;
2327 +#ifdef CONFIG_NETFILTER_DEBUG
2328 + if (verdict < 0) {
2329 + BUGPRINT("bogus standard verdict\n");
2330 + read_unlock_bh(&table->lock);
2336 + cs[sp].chaininfo = chaininfo;
2337 + cs[sp].e = (struct ebt_entry *)
2338 + (((char *)point) + point->next_offset);
2340 + chaininfo = (struct ebt_entries *) (base + verdict);
2341 +#ifdef CONFIG_NETFILTER_DEBUG
2342 + if (chaininfo->distinguisher) {
2343 + BUGPRINT("jump to non-chain\n");
2344 + read_unlock_bh(&table->lock);
2348 + nentries = chaininfo->nentries;
2349 + point = (struct ebt_entry *)chaininfo->data;
2350 + counter_base = cb_base + chaininfo->counter_offset;
2354 + point = (struct ebt_entry *)
2355 + (((char *)point) + point->next_offset);
2359 + // I actually like this :)
2360 + if (chaininfo->policy == EBT_RETURN)
2362 + if (chaininfo->policy == EBT_ACCEPT) {
2363 + read_unlock_bh(&table->lock);
2366 + read_unlock_bh(&table->lock);
2370 +// If it succeeds, returns element and locks mutex
2371 +static inline void *
2372 +find_inlist_lock_noload(struct list_head *head, const char *name, int *error,
2373 + struct semaphore *mutex)
2377 + *error = down_interruptible(mutex);
2381 + ret = list_named_find(head, name);
2389 +#ifndef CONFIG_KMOD
2390 +#define find_inlist_lock(h,n,p,e,m) find_inlist_lock_noload((h),(n),(e),(m))
2393 +find_inlist_lock(struct list_head *head, const char *name, const char *prefix,
2394 + int *error, struct semaphore *mutex)
2398 + ret = find_inlist_lock_noload(head, name, error, mutex);
2400 + char modulename[EBT_FUNCTION_MAXNAMELEN + strlen(prefix) + 1];
2401 + strcpy(modulename, prefix);
2402 + strcat(modulename, name);
2403 + request_module(modulename);
2404 + ret = find_inlist_lock_noload(head, name, error, mutex);
2410 +static inline struct ebt_table *
2411 +find_table_lock(const char *name, int *error, struct semaphore *mutex)
2413 + return find_inlist_lock(&ebt_tables, name, "ebtable_", error, mutex);
2416 +static inline struct ebt_match *
2417 +find_match_lock(const char *name, int *error, struct semaphore *mutex)
2419 + return find_inlist_lock(&ebt_matches, name, "ebt_", error, mutex);
2422 +static inline struct ebt_watcher *
2423 +find_watcher_lock(const char *name, int *error, struct semaphore *mutex)
2425 + return find_inlist_lock(&ebt_watchers, name, "ebt_", error, mutex);
2428 +static inline struct ebt_target *
2429 +find_target_lock(const char *name, int *error, struct semaphore *mutex)
2431 + return find_inlist_lock(&ebt_targets, name, "ebt_", error, mutex);
2435 +ebt_check_match(struct ebt_entry_match *m, struct ebt_entry *e,
2436 + const char *name, unsigned int hookmask, unsigned int *cnt)
2438 + struct ebt_match *match;
2441 + if (((char *)m) + m->match_size + sizeof(struct ebt_entry_match) >
2442 + ((char *)e) + e->watchers_offset)
2444 + match = find_match_lock(m->u.name, &ret, &ebt_mutex);
2447 + m->u.match = match;
2449 + __MOD_INC_USE_COUNT(match->me);
2451 + if (match->check &&
2452 + match->check(name, hookmask, e, m->data, m->match_size) != 0) {
2453 + BUGPRINT("match->check failed\n");
2455 + __MOD_DEC_USE_COUNT(match->me);
2463 +ebt_check_watcher(struct ebt_entry_watcher *w, struct ebt_entry *e,
2464 + const char *name, unsigned int hookmask, unsigned int *cnt)
2466 + struct ebt_watcher *watcher;
2469 + if (((char *)w) + w->watcher_size + sizeof(struct ebt_entry_watcher) >
2470 + ((char *)e) + e->target_offset)
2472 + watcher = find_watcher_lock(w->u.name, &ret, &ebt_mutex);
2475 + w->u.watcher = watcher;
2477 + __MOD_INC_USE_COUNT(watcher->me);
2479 + if (watcher->check &&
2480 + watcher->check(name, hookmask, e, w->data, w->watcher_size) != 0) {
2481 + BUGPRINT("watcher->check failed\n");
2483 + __MOD_DEC_USE_COUNT(watcher->me);
2490 +// this one is very careful, as it is the first function
2491 +// to parse the userspace data
2493 +ebt_check_entry_size_and_hooks(struct ebt_entry *e,
2494 + struct ebt_table_info *newinfo, char *base, char *limit,
2495 + struct ebt_entries **hook_entries, unsigned int *n, unsigned int *cnt,
2496 + unsigned int *totalcnt, unsigned int *udc_cnt, unsigned int valid_hooks)
2500 + for (i = 0; i < NF_BR_NUMHOOKS; i++) {
2501 + if ((valid_hooks & (1 << i)) == 0)
2503 + if ( (char *)hook_entries[i] - base ==
2504 + (char *)e - newinfo->entries)
2507 + // beginning of a new chain
2508 + // if i == NF_BR_NUMHOOKS it must be a user defined chain
2509 + if (i != NF_BR_NUMHOOKS || !(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
2510 + if ((e->bitmask & EBT_ENTRY_OR_ENTRIES) != 0) {
2511 + // we make userspace set this right,
2512 + // so there is no misunderstanding
2513 + BUGPRINT("EBT_ENTRY_OR_ENTRIES shouldn't be set "
2514 + "in distinguisher\n");
2517 + // this checks if the previous chain has as many entries
2518 + // as it said it has
2520 + BUGPRINT("nentries does not equal the nr of entries "
2521 + "in the chain\n");
2524 + // before we look at the struct, be sure it is not too big
2525 + if ((char *)hook_entries[i] + sizeof(struct ebt_entries)
2527 + BUGPRINT("entries_size too small\n");
2530 + if (((struct ebt_entries *)e)->policy != EBT_DROP &&
2531 + ((struct ebt_entries *)e)->policy != EBT_ACCEPT) {
2532 + // only RETURN from udc
2533 + if (i != NF_BR_NUMHOOKS ||
2534 + ((struct ebt_entries *)e)->policy != EBT_RETURN) {
2535 + BUGPRINT("bad policy\n");
2539 + if (i == NF_BR_NUMHOOKS) // it's a user defined chain
2542 + newinfo->hook_entry[i] = (struct ebt_entries *)e;
2543 + if (((struct ebt_entries *)e)->counter_offset != *totalcnt) {
2544 + BUGPRINT("counter_offset != totalcnt");
2547 + *n = ((struct ebt_entries *)e)->nentries;
2551 + // a plain old entry, heh
2552 + if (sizeof(struct ebt_entry) > e->watchers_offset ||
2553 + e->watchers_offset > e->target_offset ||
2554 + e->target_offset >= e->next_offset) {
2555 + BUGPRINT("entry offsets not in right order\n");
2558 + // this is not checked anywhere else
2559 + if (e->next_offset - e->target_offset < sizeof(struct ebt_entry_target)) {
2560 + BUGPRINT("target size too small\n");
2569 +struct ebt_cl_stack
2571 + struct ebt_chainstack cs;
2573 + unsigned int hookmask;
2576 +// we need these positions to check that the jumps to a different part of the
2577 +// entries is a jump to the beginning of a new chain.
2579 +ebt_get_udc_positions(struct ebt_entry *e, struct ebt_table_info *newinfo,
2580 + struct ebt_entries **hook_entries, unsigned int *n, unsigned int valid_hooks,
2581 + struct ebt_cl_stack *udc)
2585 + // we're only interested in chain starts
2586 + if (e->bitmask & EBT_ENTRY_OR_ENTRIES)
2588 + for (i = 0; i < NF_BR_NUMHOOKS; i++) {
2589 + if ((valid_hooks & (1 << i)) == 0)
2591 + if (newinfo->hook_entry[i] == (struct ebt_entries *)e)
2594 + // only care about udc
2595 + if (i != NF_BR_NUMHOOKS)
2598 + udc[*n].cs.chaininfo = (struct ebt_entries *)e;
2599 + // these initialisations are depended on later in check_chainloops()
2601 + udc[*n].hookmask = 0;
2608 +ebt_cleanup_match(struct ebt_entry_match *m, unsigned int *i)
2610 + if (i && (*i)-- == 0)
2612 + if (m->u.match->destroy)
2613 + m->u.match->destroy(m->data, m->match_size);
2614 + if (m->u.match->me)
2615 + __MOD_DEC_USE_COUNT(m->u.match->me);
2621 +ebt_cleanup_watcher(struct ebt_entry_watcher *w, unsigned int *i)
2623 + if (i && (*i)-- == 0)
2625 + if (w->u.watcher->destroy)
2626 + w->u.watcher->destroy(w->data, w->watcher_size);
2627 + if (w->u.watcher->me)
2628 + __MOD_DEC_USE_COUNT(w->u.watcher->me);
2634 +ebt_cleanup_entry(struct ebt_entry *e, unsigned int *cnt)
2636 + struct ebt_entry_target *t;
2638 + if ((e->bitmask & EBT_ENTRY_OR_ENTRIES) == 0)
2641 + if (cnt && (*cnt)-- == 0)
2643 + EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, NULL);
2644 + EBT_MATCH_ITERATE(e, ebt_cleanup_match, NULL);
2645 + t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
2646 + if (t->u.target->destroy)
2647 + t->u.target->destroy(t->data, t->target_size);
2648 + if (t->u.target->me)
2649 + __MOD_DEC_USE_COUNT(t->u.target->me);
2655 +ebt_check_entry(struct ebt_entry *e, struct ebt_table_info *newinfo,
2656 + const char *name, unsigned int *cnt, unsigned int valid_hooks,
2657 + struct ebt_cl_stack *cl_s, unsigned int udc_cnt)
2659 + struct ebt_entry_target *t;
2660 + struct ebt_target *target;
2661 + unsigned int i, j, hook = 0, hookmask = 0;
2664 + // Don't mess with the struct ebt_entries
2665 + if ((e->bitmask & EBT_ENTRY_OR_ENTRIES) == 0)
2668 + if (e->bitmask & ~EBT_F_MASK) {
2669 + BUGPRINT("Unknown flag for bitmask\n");
2672 + if (e->invflags & ~EBT_INV_MASK) {
2673 + BUGPRINT("Unknown flag for inv bitmask\n");
2676 + if ( (e->bitmask & EBT_NOPROTO) && (e->bitmask & EBT_802_3) ) {
2677 + BUGPRINT("NOPROTO & 802_3 not allowed\n");
2680 + // what hook do we belong to?
2681 + for (i = 0; i < NF_BR_NUMHOOKS; i++) {
2682 + if ((valid_hooks & (1 << i)) == 0)
2684 + if ((char *)newinfo->hook_entry[i] < (char *)e)
2689 + // (1 << NF_BR_NUMHOOKS) tells the check functions the rule is on
2691 + if (i < NF_BR_NUMHOOKS)
2692 + hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
2694 + for (i = 0; i < udc_cnt; i++)
2695 + if ((char *)(cl_s[i].cs.chaininfo) > (char *)e)
2698 + hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
2700 + hookmask = cl_s[i - 1].hookmask;
2703 + ret = EBT_MATCH_ITERATE(e, ebt_check_match, e, name, hookmask, &i);
2705 + goto cleanup_matches;
2707 + ret = EBT_WATCHER_ITERATE(e, ebt_check_watcher, e, name, hookmask, &j);
2709 + goto cleanup_watchers;
2710 + t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
2711 + target = find_target_lock(t->u.name, &ret, &ebt_mutex);
2713 + goto cleanup_watchers;
2715 + __MOD_INC_USE_COUNT(target->me);
2718 + t->u.target = target;
2719 + if (t->u.target == &ebt_standard_target) {
2720 + if (e->target_offset + sizeof(struct ebt_standard_target) >
2722 + BUGPRINT("Standard target size too big\n");
2724 + goto cleanup_watchers;
2726 + if (((struct ebt_standard_target *)t)->verdict <
2727 + -NUM_STANDARD_TARGETS) {
2728 + BUGPRINT("Invalid standard target\n");
2730 + goto cleanup_watchers;
2732 + } else if ((e->target_offset + t->target_size +
2733 + sizeof(struct ebt_entry_target) > e->next_offset) ||
2734 + (t->u.target->check &&
2735 + t->u.target->check(name, hookmask, e, t->data, t->target_size) != 0)){
2736 + if (t->u.target->me)
2737 + __MOD_DEC_USE_COUNT(t->u.target->me);
2739 + goto cleanup_watchers;
2744 + EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, &j);
2746 + EBT_MATCH_ITERATE(e, ebt_cleanup_match, &i);
2750 +// checks for loops and sets the hook mask for udc
2751 +// the hook mask for udc tells us from which base chains the udc can be
2752 +// accessed. This mask is a parameter to the check() functions of the extensions
2753 +static int check_chainloops(struct ebt_entries *chain,
2754 + struct ebt_cl_stack *cl_s, unsigned int udc_cnt,
2755 + unsigned int hooknr, char *base)
2757 + int i, chain_nr = -1, pos = 0, nentries = chain->nentries, verdict;
2758 + struct ebt_entry *e = (struct ebt_entry *)chain->data;
2759 + struct ebt_entry_target *t;
2761 + while (pos < nentries || chain_nr != -1) {
2762 + // end of udc, go back one 'recursion' step
2763 + if (pos == nentries) {
2764 + // put back values of the time when this chain was called
2765 + e = cl_s[chain_nr].cs.e;
2766 + if (cl_s[chain_nr].from != -1)
2768 + cl_s[cl_s[chain_nr].from].cs.chaininfo->nentries;
2770 + nentries = chain->nentries;
2771 + pos = cl_s[chain_nr].cs.n;
2772 + // make sure we won't see a loop that isn't one
2773 + cl_s[chain_nr].cs.n = 0;
2774 + chain_nr = cl_s[chain_nr].from;
2775 + if (pos == nentries)
2778 + t = (struct ebt_entry_target *)
2779 + (((char *)e) + e->target_offset);
2780 + if (strcmp(t->u.name, EBT_STANDARD_TARGET))
2781 + goto letscontinue;
2782 + if (e->target_offset + sizeof(struct ebt_standard_target) >
2784 + BUGPRINT("Standard target size too big\n");
2787 + verdict = ((struct ebt_standard_target *)t)->verdict;
2788 + if (verdict >= 0) { // jump to another chain
2789 + struct ebt_entries *hlp2 =
2790 + (struct ebt_entries *)(base + verdict);
2791 + for (i = 0; i < udc_cnt; i++)
2792 + if (hlp2 == cl_s[i].cs.chaininfo)
2794 + // bad destination or loop
2795 + if (i == udc_cnt) {
2796 + BUGPRINT("bad destination\n");
2799 + if (cl_s[i].cs.n) {
2800 + BUGPRINT("loop\n");
2803 + // this can't be 0, so the above test is correct
2804 + cl_s[i].cs.n = pos + 1;
2806 + cl_s[i].cs.e = ((void *)e + e->next_offset);
2807 + e = (struct ebt_entry *)(hlp2->data);
2808 + nentries = hlp2->nentries;
2809 + cl_s[i].from = chain_nr;
2811 + // this udc is accessible from the base chain for hooknr
2812 + cl_s[i].hookmask |= (1 << hooknr);
2816 + e = (void *)e + e->next_offset;
2822 +// do the parsing of the table/chains/entries/matches/watchers/targets, heh
2823 +static int translate_table(struct ebt_replace *repl,
2824 + struct ebt_table_info *newinfo)
2826 + unsigned int i, j, k, udc_cnt;
2828 + struct ebt_cl_stack *cl_s = NULL; // used in the checking for chain loops
2831 + while (i < NF_BR_NUMHOOKS && !(repl->valid_hooks & (1 << i)))
2833 + if (i == NF_BR_NUMHOOKS) {
2834 + BUGPRINT("No valid hooks specified\n");
2837 + if (repl->hook_entry[i] != (struct ebt_entries *)repl->entries) {
2838 + BUGPRINT("Chains don't start at beginning\n");
2841 + // make sure chains are ordered after each other in same order
2842 + // as their corresponding hooks
2843 + for (j = i + 1; j < NF_BR_NUMHOOKS; j++) {
2844 + if (!(repl->valid_hooks & (1 << j)))
2846 + if ( repl->hook_entry[j] <= repl->hook_entry[i] ) {
2847 + BUGPRINT("Hook order must be followed\n");
2853 + for (i = 0; i < NF_BR_NUMHOOKS; i++)
2854 + newinfo->hook_entry[i] = NULL;
2856 + newinfo->entries_size = repl->entries_size;
2857 + newinfo->nentries = repl->nentries;
2859 + // do some early checkings and initialize some things
2860 + i = 0; // holds the expected nr. of entries for the chain
2861 + j = 0; // holds the up to now counted entries for the chain
2862 + k = 0; // holds the total nr. of entries, should equal
2863 + // newinfo->nentries afterwards
2864 + udc_cnt = 0; // will hold the nr. of user defined chains (udc)
2865 + ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
2866 + ebt_check_entry_size_and_hooks, newinfo, repl->entries,
2867 + repl->entries + repl->entries_size, repl->hook_entry, &i, &j, &k,
2868 + &udc_cnt, repl->valid_hooks);
2874 + BUGPRINT("nentries does not equal the nr of entries in the "
2875 + "(last) chain\n");
2878 + if (k != newinfo->nentries) {
2879 + BUGPRINT("Total nentries is wrong\n");
2883 + // check if all valid hooks have a chain
2884 + for (i = 0; i < NF_BR_NUMHOOKS; i++) {
2885 + if (newinfo->hook_entry[i] == NULL &&
2886 + (repl->valid_hooks & (1 << i))) {
2887 + BUGPRINT("Valid hook without chain\n");
2892 + // Get the location of the udc, put them in an array
2893 + // While we're at it, allocate the chainstack
2895 + // this will get free'd in do_replace()/ebt_register_table()
2896 + // if an error occurs
2897 + newinfo->chainstack = (struct ebt_chainstack **)
2898 + vmalloc(smp_num_cpus * sizeof(struct ebt_chainstack));
2899 + if (!newinfo->chainstack)
2901 + for (i = 0; i < smp_num_cpus; i++) {
2902 + newinfo->chainstack[i] =
2903 + vmalloc(udc_cnt * sizeof(struct ebt_chainstack));
2904 + if (!newinfo->chainstack[i]) {
2906 + vfree(newinfo->chainstack[--i]);
2907 + vfree(newinfo->chainstack);
2908 + newinfo->chainstack = NULL;
2913 + cl_s = (struct ebt_cl_stack *)
2914 + vmalloc(udc_cnt * sizeof(struct ebt_cl_stack));
2917 + i = 0; // the i'th udc
2918 + EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
2919 + ebt_get_udc_positions, newinfo, repl->hook_entry, &i,
2920 + repl->valid_hooks, cl_s);
2922 + if (i != udc_cnt) {
2923 + BUGPRINT("i != udc_cnt\n");
2929 + // Check for loops
2930 + for (i = 0; i < NF_BR_NUMHOOKS; i++)
2931 + if (repl->valid_hooks & (1 << i))
2932 + if (check_chainloops(newinfo->hook_entry[i],
2933 + cl_s, udc_cnt, i, newinfo->entries)) {
2939 + // we now know the following (along with E=mc²):
2940 + // - the nr of entries in each chain is right
2941 + // - the size of the allocated space is right
2942 + // - all valid hooks have a corresponding chain
2943 + // - there are no loops
2944 + // - wrong data can still be on the level of a single entry
2945 + // - could be there are jumps to places that are not the
2946 + // beginning of a chain. This can only occur in chains that
2947 + // are not accessible from any base chains, so we don't care.
2949 + // used to know what we need to clean up if something goes wrong
2951 + ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
2952 + ebt_check_entry, newinfo, repl->name, &i, repl->valid_hooks,
2955 + EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
2956 + ebt_cleanup_entry, &i);
2963 +// called under write_lock
2964 +static void get_counters(struct ebt_counter *oldcounters,
2965 + struct ebt_counter *counters, unsigned int nentries)
2968 + struct ebt_counter *counter_base;
2970 + // counters of cpu 0
2971 + memcpy(counters, oldcounters,
2972 + sizeof(struct ebt_counter) * nentries);
2973 + // add other counters to those of cpu 0
2974 + for (cpu = 1; cpu < smp_num_cpus; cpu++) {
2975 + counter_base = COUNTER_BASE(oldcounters, nentries, cpu);
2976 + for (i = 0; i < nentries; i++) {
2977 + counters[i].pcnt += counter_base[i].pcnt;
2978 + counters[i].bcnt += counter_base[i].bcnt;
2983 +// replace the table
2984 +static int do_replace(void *user, unsigned int len)
2986 + int ret, i, countersize;
2987 + struct ebt_table_info *newinfo;
2988 + struct ebt_replace tmp;
2989 + struct ebt_table *t;
2990 + struct ebt_counter *counterstmp = NULL;
2991 + // used to be able to unlock earlier
2992 + struct ebt_table_info *table;
2994 + if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
2997 + if (len != sizeof(tmp) + tmp.entries_size) {
2998 + BUGPRINT("Wrong len argument\n");
3002 + if (tmp.entries_size == 0) {
3003 + BUGPRINT("Entries_size never zero\n");
3006 + countersize = COUNTER_OFFSET(tmp.nentries) * smp_num_cpus;
3007 + newinfo = (struct ebt_table_info *)
3008 + vmalloc(sizeof(struct ebt_table_info) + countersize);
3013 + memset(newinfo->counters, 0, countersize);
3015 + newinfo->entries = (char *)vmalloc(tmp.entries_size);
3016 + if (!newinfo->entries) {
3018 + goto free_newinfo;
3020 + if (copy_from_user(
3021 + newinfo->entries, tmp.entries, tmp.entries_size) != 0) {
3022 + BUGPRINT("Couldn't copy entries from userspace\n");
3024 + goto free_entries;
3027 + // the user wants counters back
3028 + // the check on the size is done later, when we have the lock
3029 + if (tmp.num_counters) {
3030 + counterstmp = (struct ebt_counter *)
3031 + vmalloc(tmp.num_counters * sizeof(struct ebt_counter));
3032 + if (!counterstmp) {
3034 + goto free_entries;
3038 + counterstmp = NULL;
3040 + // this can get initialized by translate_table()
3041 + newinfo->chainstack = NULL;
3042 + ret = translate_table(&tmp, newinfo);
3045 + goto free_counterstmp;
3047 + t = find_table_lock(tmp.name, &ret, &ebt_mutex);
3049 + goto free_iterate;
3051 + // the table doesn't like it
3052 + if (t->check && (ret = t->check(newinfo, tmp.valid_hooks)))
3055 + if (tmp.num_counters && tmp.num_counters != t->private->nentries) {
3056 + BUGPRINT("Wrong nr. of counters requested\n");
3061 + // we have the mutex lock, so no danger in reading this pointer
3062 + table = t->private;
3063 + // we need an atomic snapshot of the counters
3064 + write_lock_bh(&t->lock);
3065 + if (tmp.num_counters)
3066 + get_counters(t->private->counters, counterstmp,
3067 + t->private->nentries);
3069 + t->private = newinfo;
3070 + write_unlock_bh(&t->lock);
3072 + // So, a user can change the chains while having messed up her counter
3073 + // allocation. Only reason why this is done is because this way the lock
3074 + // is held only once, while this doesn't bring the kernel into a
3075 + // dangerous state.
3076 + if (tmp.num_counters &&
3077 + copy_to_user(tmp.counters, counterstmp,
3078 + tmp.num_counters * sizeof(struct ebt_counter))) {
3079 + BUGPRINT("Couldn't copy counters to userspace\n");
3085 + // decrease module count and free resources
3086 + EBT_ENTRY_ITERATE(table->entries, table->entries_size,
3087 + ebt_cleanup_entry, NULL);
3089 + vfree(table->entries);
3090 + if (table->chainstack) {
3091 + for (i = 0; i < smp_num_cpus; i++)
3092 + vfree(table->chainstack[i]);
3093 + vfree(table->chainstack);
3098 + vfree(counterstmp);
3104 + EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
3105 + ebt_cleanup_entry, NULL);
3108 + vfree(counterstmp);
3109 + // can be initialized in translate_table()
3110 + if (newinfo->chainstack) {
3111 + for (i = 0; i < smp_num_cpus; i++)
3112 + vfree(newinfo->chainstack[i]);
3113 + vfree(newinfo->chainstack);
3116 + if (newinfo->entries)
3117 + vfree(newinfo->entries);
3124 +int ebt_register_target(struct ebt_target *target)
3128 + ret = down_interruptible(&ebt_mutex);
3131 + if (!list_named_insert(&ebt_targets, target)) {
3136 + MOD_INC_USE_COUNT;
3141 +void ebt_unregister_target(struct ebt_target *target)
3144 + LIST_DELETE(&ebt_targets, target);
3146 + MOD_DEC_USE_COUNT;
3149 +int ebt_register_match(struct ebt_match *match)
3153 + ret = down_interruptible(&ebt_mutex);
3156 + if (!list_named_insert(&ebt_matches, match)) {
3161 + MOD_INC_USE_COUNT;
3166 +void ebt_unregister_match(struct ebt_match *match)
3169 + LIST_DELETE(&ebt_matches, match);
3171 + MOD_DEC_USE_COUNT;
3174 +int ebt_register_watcher(struct ebt_watcher *watcher)
3178 + ret = down_interruptible(&ebt_mutex);
3181 + if (!list_named_insert(&ebt_watchers, watcher)) {
3186 + MOD_INC_USE_COUNT;
3191 +void ebt_unregister_watcher(struct ebt_watcher *watcher)
3194 + LIST_DELETE(&ebt_watchers, watcher);
3196 + MOD_DEC_USE_COUNT;
3199 +int ebt_register_table(struct ebt_table *table)
3201 + struct ebt_table_info *newinfo;
3202 + int ret, i, countersize;
3204 + if (!table || !table->table ||!table->table->entries ||
3205 + table->table->entries_size == 0 ||
3206 + table->table->counters || table->private) {
3207 + BUGPRINT("Bad table data for ebt_register_table!!!\n");
3211 + countersize = COUNTER_OFFSET(table->table->nentries) * smp_num_cpus;
3212 + newinfo = (struct ebt_table_info *)
3213 + vmalloc(sizeof(struct ebt_table_info) + countersize);
3218 + newinfo->entries = (char *)vmalloc(table->table->entries_size);
3219 + if (!(newinfo->entries))
3220 + goto free_newinfo;
3222 + memcpy(newinfo->entries, table->table->entries,
3223 + table->table->entries_size);
3226 + memset(newinfo->counters, 0, countersize);
3228 + // fill in newinfo and parse the entries
3229 + newinfo->chainstack = NULL;
3230 + ret = translate_table(table->table, newinfo);
3232 + BUGPRINT("Translate_table failed\n");
3233 + goto free_chainstack;
3236 + if (table->check && table->check(newinfo, table->valid_hooks)) {
3237 + BUGPRINT("The table doesn't like its own initial data, lol\n");
3241 + table->private = newinfo;
3242 + table->lock = RW_LOCK_UNLOCKED;
3243 + ret = down_interruptible(&ebt_mutex);
3245 + goto free_chainstack;
3247 + if (list_named_find(&ebt_tables, table->name)) {
3249 + BUGPRINT("Table name already exists\n");
3253 + list_prepend(&ebt_tables, table);
3255 + MOD_INC_USE_COUNT;
3260 + if (newinfo->chainstack) {
3261 + for (i = 0; i < smp_num_cpus; i++)
3262 + vfree(newinfo->chainstack[i]);
3263 + vfree(newinfo->chainstack);
3265 + vfree(newinfo->entries);
3271 +void ebt_unregister_table(struct ebt_table *table)
3276 + BUGPRINT("Request to unregister NULL table!!!\n");
3280 + LIST_DELETE(&ebt_tables, table);
3282 + EBT_ENTRY_ITERATE(table->private->entries,
3283 + table->private->entries_size, ebt_cleanup_entry, NULL);
3284 + if (table->private->entries)
3285 + vfree(table->private->entries);
3286 + if (table->private->chainstack) {
3287 + for (i = 0; i < smp_num_cpus; i++)
3288 + vfree(table->private->chainstack[i]);
3289 + vfree(table->private->chainstack);
3291 + vfree(table->private);
3292 + MOD_DEC_USE_COUNT;
3295 +// userspace just supplied us with counters
3296 +static int update_counters(void *user, unsigned int len)
3299 + struct ebt_counter *tmp;
3300 + struct ebt_replace hlp;
3301 + struct ebt_table *t;
3303 + if (copy_from_user(&hlp, user, sizeof(hlp)))
3306 + if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter))
3308 + if (hlp.num_counters == 0)
3311 + if ( !(tmp = (struct ebt_counter *)
3312 + vmalloc(hlp.num_counters * sizeof(struct ebt_counter))) ){
3313 + MEMPRINT("Update_counters && nomemory\n");
3317 + t = find_table_lock(hlp.name, &ret, &ebt_mutex);
3321 + if (hlp.num_counters != t->private->nentries) {
3322 + BUGPRINT("Wrong nr of counters\n");
3324 + goto unlock_mutex;
3327 + if ( copy_from_user(tmp, hlp.counters,
3328 + hlp.num_counters * sizeof(struct ebt_counter)) ) {
3329 + BUGPRINT("Updata_counters && !cfu\n");
3331 + goto unlock_mutex;
3334 + // we want an atomic add of the counters
3335 + write_lock_bh(&t->lock);
3337 + // we add to the counters of the first cpu
3338 + for (i = 0; i < hlp.num_counters; i++) {
3339 + t->private->counters[i].pcnt += tmp[i].pcnt;
3340 + t->private->counters[i].bcnt += tmp[i].bcnt;
3343 + write_unlock_bh(&t->lock);
3352 +static inline int ebt_make_matchname(struct ebt_entry_match *m,
3353 + char *base, char *ubase)
3355 + char *hlp = ubase - base + (char *)m;
3356 + if (copy_to_user(hlp, m->u.match->name, EBT_FUNCTION_MAXNAMELEN))
3361 +static inline int ebt_make_watchername(struct ebt_entry_watcher *w,
3362 + char *base, char *ubase)
3364 + char *hlp = ubase - base + (char *)w;
3365 + if (copy_to_user(hlp , w->u.watcher->name, EBT_FUNCTION_MAXNAMELEN))
3370 +static inline int ebt_make_names(struct ebt_entry *e, char *base, char *ubase)
3374 + struct ebt_entry_target *t;
3376 + if ((e->bitmask & EBT_ENTRY_OR_ENTRIES) == 0)
3379 + hlp = ubase - base + (char *)e + e->target_offset;
3380 + t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
3382 + ret = EBT_MATCH_ITERATE(e, ebt_make_matchname, base, ubase);
3385 + ret = EBT_WATCHER_ITERATE(e, ebt_make_watchername, base, ubase);
3388 + if (copy_to_user(hlp, t->u.target->name, EBT_FUNCTION_MAXNAMELEN))
3393 +// called with ebt_mutex down
3394 +static int copy_everything_to_user(struct ebt_table *t, void *user,
3395 + int *len, int cmd)
3397 + struct ebt_replace tmp;
3398 + struct ebt_counter *counterstmp, *oldcounters;
3399 + unsigned int entries_size, nentries;
3402 + if (cmd == EBT_SO_GET_ENTRIES) {
3403 + entries_size = t->private->entries_size;
3404 + nentries = t->private->nentries;
3405 + entries = t->private->entries;
3406 + oldcounters = t->private->counters;
3408 + entries_size = t->table->entries_size;
3409 + nentries = t->table->nentries;
3410 + entries = t->table->entries;
3411 + oldcounters = t->table->counters;
3414 + if (copy_from_user(&tmp, user, sizeof(tmp))) {
3415 + BUGPRINT("Cfu didn't work\n");
3419 + if (*len != sizeof(struct ebt_replace) + entries_size +
3420 + (tmp.num_counters? nentries * sizeof(struct ebt_counter): 0)) {
3421 + BUGPRINT("Wrong size\n");
3425 + if (tmp.nentries != nentries) {
3426 + BUGPRINT("Nentries wrong\n");
3430 + if (tmp.entries_size != entries_size) {
3431 + BUGPRINT("Wrong size\n");
3435 + // userspace might not need the counters
3436 + if (tmp.num_counters) {
3437 + if (tmp.num_counters != nentries) {
3438 + BUGPRINT("Num_counters wrong\n");
3441 + counterstmp = (struct ebt_counter *)
3442 + vmalloc(nentries * sizeof(struct ebt_counter));
3443 + if (!counterstmp) {
3444 + MEMPRINT("Couldn't copy counters, out of memory\n");
3447 + write_lock_bh(&t->lock);
3448 + get_counters(oldcounters, counterstmp, nentries);
3449 + write_unlock_bh(&t->lock);
3451 + if (copy_to_user(tmp.counters, counterstmp,
3452 + nentries * sizeof(struct ebt_counter))) {
3453 + BUGPRINT("Couldn't copy counters to userspace\n");
3454 + vfree(counterstmp);
3457 + vfree(counterstmp);
3460 + if (copy_to_user(tmp.entries, entries, entries_size)) {
3461 + BUGPRINT("Couldn't copy entries to userspace\n");
3464 + // set the match/watcher/target names right
3465 + return EBT_ENTRY_ITERATE(entries, entries_size,
3466 + ebt_make_names, entries, tmp.entries);
3469 +static int do_ebt_set_ctl(struct sock *sk,
3470 + int cmd, void *user, unsigned int len)
3475 + case EBT_SO_SET_ENTRIES:
3476 + ret = do_replace(user, len);
3478 + case EBT_SO_SET_COUNTERS:
3479 + ret = update_counters(user, len);
3487 +static int do_ebt_get_ctl(struct sock *sk, int cmd, void *user, int *len)
3490 + struct ebt_replace tmp;
3491 + struct ebt_table *t;
3493 + if (copy_from_user(&tmp, user, sizeof(tmp)))
3496 + t = find_table_lock(tmp.name, &ret, &ebt_mutex);
3501 + case EBT_SO_GET_INFO:
3502 + case EBT_SO_GET_INIT_INFO:
3503 + if (*len != sizeof(struct ebt_replace)){
3508 + if (cmd == EBT_SO_GET_INFO) {
3509 + tmp.nentries = t->private->nentries;
3510 + tmp.entries_size = t->private->entries_size;
3511 + tmp.valid_hooks = t->valid_hooks;
3513 + tmp.nentries = t->table->nentries;
3514 + tmp.entries_size = t->table->entries_size;
3515 + tmp.valid_hooks = t->table->valid_hooks;
3518 + if (copy_to_user(user, &tmp, *len) != 0){
3519 + BUGPRINT("c2u Didn't work\n");
3526 + case EBT_SO_GET_ENTRIES:
3527 + case EBT_SO_GET_INIT_ENTRIES:
3528 + ret = copy_everything_to_user(t, user, len, cmd);
3540 +static struct nf_sockopt_ops ebt_sockopts =
3541 +{ { NULL, NULL }, PF_INET, EBT_BASE_CTL, EBT_SO_SET_MAX + 1, do_ebt_set_ctl,
3542 + EBT_BASE_CTL, EBT_SO_GET_MAX + 1, do_ebt_get_ctl, 0, NULL
3545 +static int __init init(void)
3550 + list_named_insert(&ebt_targets, &ebt_standard_target);
3552 + if ((ret = nf_register_sockopt(&ebt_sockopts)) < 0)
3555 + printk(KERN_NOTICE "Ebtables v2.0 registered\n");
3559 +static void __exit fini(void)
3561 + nf_unregister_sockopt(&ebt_sockopts);
3562 + printk(KERN_NOTICE "Ebtables v2.0 unregistered\n");
3565 +EXPORT_SYMBOL(ebt_register_table);
3566 +EXPORT_SYMBOL(ebt_unregister_table);
3567 +EXPORT_SYMBOL(ebt_register_match);
3568 +EXPORT_SYMBOL(ebt_unregister_match);
3569 +EXPORT_SYMBOL(ebt_register_watcher);
3570 +EXPORT_SYMBOL(ebt_unregister_watcher);
3571 +EXPORT_SYMBOL(ebt_register_target);
3572 +EXPORT_SYMBOL(ebt_unregister_target);
3573 +EXPORT_SYMBOL(ebt_do_table);
3576 +MODULE_LICENSE("GPL");
3577 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
3578 +++ linux-2.4.20/include/linux/netfilter_bridge/ebtables.h 2005-01-07 02:57:30.893817600 -0500
3584 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
3586 + * ebtables.c,v 2.0, September, 2002
3588 + * This code is stongly inspired on the iptables code which is
3589 + * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
3592 +#ifndef __LINUX_BRIDGE_EFF_H
3593 +#define __LINUX_BRIDGE_EFF_H
3594 +#include <linux/if.h>
3595 +#include <linux/netfilter_bridge.h>
3596 +#include <linux/if_ether.h>
3598 +#define EBT_TABLE_MAXNAMELEN 32
3599 +#define EBT_CHAIN_MAXNAMELEN EBT_TABLE_MAXNAMELEN
3600 +#define EBT_FUNCTION_MAXNAMELEN EBT_TABLE_MAXNAMELEN
3602 +// verdicts >0 are "branches"
3603 +#define EBT_ACCEPT -1
3604 +#define EBT_DROP -2
3605 +#define EBT_CONTINUE -3
3606 +#define EBT_RETURN -4
3607 +#define NUM_STANDARD_TARGETS 4
3611 + char name[EBT_TABLE_MAXNAMELEN];
3612 + unsigned int valid_hooks;
3613 + // nr of rules in the table
3614 + unsigned int nentries;
3615 + // total size of the entries
3616 + unsigned int entries_size;
3617 + // start of the chains
3618 + struct ebt_entries *hook_entry[NF_BR_NUMHOOKS];
3619 + // nr of counters userspace expects back
3620 + unsigned int num_counters;
3621 + // where the kernel will put the old counters
3622 + struct ebt_counter *counters;
3632 +struct ebt_entries {
3633 + // this field is always set to zero
3634 + // See EBT_ENTRY_OR_ENTRIES.
3635 + // Must be same size as ebt_entry.bitmask
3636 + unsigned int distinguisher;
3638 + char name[EBT_CHAIN_MAXNAMELEN];
3639 + // counter offset for this chain
3640 + unsigned int counter_offset;
3641 + // one standard (accept, drop, return) per hook
3644 + unsigned int nentries;
3646 + char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
3649 +// used for the bitmask of struct ebt_entry
3651 +// This is a hack to make a difference between an ebt_entry struct and an
3652 +// ebt_entries struct when traversing the entries from start to end.
3653 +// Using this simplifies the code alot, while still being able to use
3655 +// Contrary, iptables doesn't use something like ebt_entries and therefore uses
3656 +// different techniques for naming the policy and such. So, iptables doesn't
3657 +// need a hack like this.
3658 +#define EBT_ENTRY_OR_ENTRIES 0x01
3659 +// these are the normal masks
3660 +#define EBT_NOPROTO 0x02
3661 +#define EBT_802_3 0x04
3662 +#define EBT_SOURCEMAC 0x08
3663 +#define EBT_DESTMAC 0x10
3664 +#define EBT_F_MASK (EBT_NOPROTO | EBT_802_3 | EBT_SOURCEMAC | EBT_DESTMAC \
3665 + | EBT_ENTRY_OR_ENTRIES)
3667 +#define EBT_IPROTO 0x01
3668 +#define EBT_IIN 0x02
3669 +#define EBT_IOUT 0x04
3670 +#define EBT_ISOURCE 0x8
3671 +#define EBT_IDEST 0x10
3672 +#define EBT_ILOGICALIN 0x20
3673 +#define EBT_ILOGICALOUT 0x40
3674 +#define EBT_INV_MASK (EBT_IPROTO | EBT_IIN | EBT_IOUT | EBT_ILOGICALIN \
3675 + | EBT_ILOGICALOUT | EBT_ISOURCE | EBT_IDEST)
3677 +struct ebt_entry_match
3680 + char name[EBT_FUNCTION_MAXNAMELEN];
3681 + struct ebt_match *match;
3684 + unsigned int match_size;
3685 + unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
3688 +struct ebt_entry_watcher
3691 + char name[EBT_FUNCTION_MAXNAMELEN];
3692 + struct ebt_watcher *watcher;
3695 + unsigned int watcher_size;
3696 + unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
3699 +struct ebt_entry_target
3702 + char name[EBT_FUNCTION_MAXNAMELEN];
3703 + struct ebt_target *target;
3706 + unsigned int target_size;
3707 + unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
3710 +#define EBT_STANDARD_TARGET "standard"
3711 +struct ebt_standard_target
3713 + struct ebt_entry_target target;
3719 + // this needs to be the first field
3720 + unsigned int bitmask;
3721 + unsigned int invflags;
3722 + uint16_t ethproto;
3723 + // the physical in-dev
3724 + char in[IFNAMSIZ];
3725 + // the logical in-dev
3726 + char logical_in[IFNAMSIZ];
3727 + // the physical out-dev
3728 + char out[IFNAMSIZ];
3729 + // the logical out-dev
3730 + char logical_out[IFNAMSIZ];
3731 + unsigned char sourcemac[ETH_ALEN];
3732 + unsigned char sourcemsk[ETH_ALEN];
3733 + unsigned char destmac[ETH_ALEN];
3734 + unsigned char destmsk[ETH_ALEN];
3735 + // sizeof ebt_entry + matches
3736 + unsigned int watchers_offset;
3737 + // sizeof ebt_entry + matches + watchers
3738 + unsigned int target_offset;
3739 + // sizeof ebt_entry + matches + watchers + target
3740 + unsigned int next_offset;
3741 + unsigned char elems[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
3744 +// [gs]etsockopt numbers
3745 +#define EBT_BASE_CTL 128
3747 +#define EBT_SO_SET_ENTRIES (EBT_BASE_CTL)
3748 +#define EBT_SO_SET_COUNTERS (EBT_SO_SET_ENTRIES+1)
3749 +#define EBT_SO_SET_MAX (EBT_SO_SET_COUNTERS+1)
3751 +#define EBT_SO_GET_INFO (EBT_BASE_CTL)
3752 +#define EBT_SO_GET_ENTRIES (EBT_SO_GET_INFO+1)
3753 +#define EBT_SO_GET_INIT_INFO (EBT_SO_GET_ENTRIES+1)
3754 +#define EBT_SO_GET_INIT_ENTRIES (EBT_SO_GET_INIT_INFO+1)
3755 +#define EBT_SO_GET_MAX (EBT_SO_GET_INIT_ENTRIES+1)
3759 +// return values for match() functions
3760 +#define EBT_MATCH 0
3761 +#define EBT_NOMATCH 1
3765 + struct list_head list;
3766 + const char name[EBT_FUNCTION_MAXNAMELEN];
3767 + // 0 == it matches
3768 + int (*match)(const struct sk_buff *skb, const struct net_device *in,
3769 + const struct net_device *out, const void *matchdata,
3770 + unsigned int datalen);
3772 + int (*check)(const char *tablename, unsigned int hookmask,
3773 + const struct ebt_entry *e, void *matchdata, unsigned int datalen);
3774 + void (*destroy)(void *matchdata, unsigned int datalen);
3775 + struct module *me;
3780 + struct list_head list;
3781 + const char name[EBT_FUNCTION_MAXNAMELEN];
3782 + void (*watcher)(const struct sk_buff *skb, const struct net_device *in,
3783 + const struct net_device *out, const void *watcherdata,
3784 + unsigned int datalen);
3786 + int (*check)(const char *tablename, unsigned int hookmask,
3787 + const struct ebt_entry *e, void *watcherdata, unsigned int datalen);
3788 + void (*destroy)(void *watcherdata, unsigned int datalen);
3789 + struct module *me;
3794 + struct list_head list;
3795 + const char name[EBT_FUNCTION_MAXNAMELEN];
3796 + // returns one of the standard verdicts
3797 + int (*target)(struct sk_buff **pskb, unsigned int hooknr,
3798 + const struct net_device *in, const struct net_device *out,
3799 + const void *targetdata, unsigned int datalen);
3801 + int (*check)(const char *tablename, unsigned int hookmask,
3802 + const struct ebt_entry *e, void *targetdata, unsigned int datalen);
3803 + void (*destroy)(void *targetdata, unsigned int datalen);
3804 + struct module *me;
3807 +// used for jumping from and into user defined chains (udc)
3808 +struct ebt_chainstack
3810 + struct ebt_entries *chaininfo; // pointer to chain data
3811 + struct ebt_entry *e; // pointer to entry data
3812 + unsigned int n; // n'th entry
3815 +struct ebt_table_info
3817 + // total size of the entries
3818 + unsigned int entries_size;
3819 + unsigned int nentries;
3820 + // pointers to the start of the chains
3821 + struct ebt_entries *hook_entry[NF_BR_NUMHOOKS];
3822 + // room to maintain the stack used for jumping from and into udc
3823 + struct ebt_chainstack **chainstack;
3825 + struct ebt_counter counters[0] ____cacheline_aligned;
3830 + struct list_head list;
3831 + char name[EBT_TABLE_MAXNAMELEN];
3832 + struct ebt_replace *table;
3833 + unsigned int valid_hooks;
3835 + // e.g. could be the table explicitly only allows certain
3836 + // matches, targets, ... 0 == let it in
3837 + int (*check)(const struct ebt_table_info *info,
3838 + unsigned int valid_hooks);
3839 + // the data used by the kernel
3840 + struct ebt_table_info *private;
3843 +#define EBT_ALIGN(s) (((s) + (__alignof__(struct ebt_replace)-1)) & \
3844 + ~(__alignof__(struct ebt_replace)-1))
3845 +extern int ebt_register_table(struct ebt_table *table);
3846 +extern void ebt_unregister_table(struct ebt_table *table);
3847 +extern int ebt_register_match(struct ebt_match *match);
3848 +extern void ebt_unregister_match(struct ebt_match *match);
3849 +extern int ebt_register_watcher(struct ebt_watcher *watcher);
3850 +extern void ebt_unregister_watcher(struct ebt_watcher *watcher);
3851 +extern int ebt_register_target(struct ebt_target *target);
3852 +extern void ebt_unregister_target(struct ebt_target *target);
3853 +extern unsigned int ebt_do_table(unsigned int hook, struct sk_buff **pskb,
3854 + const struct net_device *in, const struct net_device *out,
3855 + struct ebt_table *table);
3857 + // Used in the kernel match() functions
3858 +#define FWINV(bool,invflg) ((bool) ^ !!(info->invflags & invflg))
3859 +// True if the hook mask denotes that the rule is in a base chain,
3860 +// used in the check() functions
3861 +#define BASE_CHAIN (hookmask & (1 << NF_BR_NUMHOOKS))
3862 +// Clear the bit in the hook mask that tells if the rule is on a base chain
3863 +#define CLEAR_BASE_CHAIN_BIT (hookmask &= ~(1 << NF_BR_NUMHOOKS))
3864 +// True if the target is not a standard target
3865 +#define INVALID_TARGET (info->target < -NUM_STANDARD_TARGETS || info->target >= 0)
3867 +#endif /* __KERNEL__ */
3869 +// blatently stolen from ip_tables.h
3870 +// fn returns 0 to continue iteration
3871 +#define EBT_MATCH_ITERATE(e, fn, args...) \
3873 + unsigned int __i; \
3875 + struct ebt_entry_match *__match; \
3877 + for (__i = sizeof(struct ebt_entry); \
3878 + __i < (e)->watchers_offset; \
3879 + __i += __match->match_size + \
3880 + sizeof(struct ebt_entry_match)) { \
3881 + __match = (void *)(e) + __i; \
3883 + __ret = fn(__match , ## args); \
3887 + if (__ret == 0) { \
3888 + if (__i != (e)->watchers_offset) \
3889 + __ret = -EINVAL; \
3894 +#define EBT_WATCHER_ITERATE(e, fn, args...) \
3896 + unsigned int __i; \
3898 + struct ebt_entry_watcher *__watcher; \
3900 + for (__i = e->watchers_offset; \
3901 + __i < (e)->target_offset; \
3902 + __i += __watcher->watcher_size + \
3903 + sizeof(struct ebt_entry_watcher)) { \
3904 + __watcher = (void *)(e) + __i; \
3906 + __ret = fn(__watcher , ## args); \
3910 + if (__ret == 0) { \
3911 + if (__i != (e)->target_offset) \
3912 + __ret = -EINVAL; \
3917 +#define EBT_ENTRY_ITERATE(entries, size, fn, args...) \
3919 + unsigned int __i; \
3921 + struct ebt_entry *__entry; \
3923 + for (__i = 0; __i < (size);) { \
3924 + __entry = (void *)(entries) + __i; \
3925 + __ret = fn(__entry , ## args); \
3928 + if (__entry->bitmask != 0) \
3929 + __i += __entry->next_offset; \
3931 + __i += sizeof(struct ebt_entries); \
3933 + if (__ret == 0) { \
3934 + if (__i != (size)) \
3935 + __ret = -EINVAL; \
3941 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
3942 +++ linux-2.4.20/include/linux/netfilter_bridge/ebt_802_3.h 2005-01-07 02:57:30.894817448 -0500
3944 +#ifndef __LINUX_BRIDGE_EBT_802_3_H
3945 +#define __LINUX_BRIDGE_EBT_802_3_H
3947 +#define EBT_802_3_SAP 0x01
3948 +#define EBT_802_3_TYPE 0x02
3950 +#define EBT_802_3_MATCH "802_3"
3953 + * If frame has DSAP/SSAP value 0xaa you must check the SNAP type
3954 + * to discover what kind of packet we're carrying.
3956 +#define CHECK_TYPE 0xaa
3959 + * Control field may be one or two bytes. If the first byte has
3960 + * the value 0x03 then the entire length is one byte, otherwise it is two.
3961 + * One byte controls are used in Unnumbered Information frames.
3962 + * Two byte controls are used in Numbered Information frames.
3966 +#define EBT_802_3_MASK (EBT_802_3_SAP | EBT_802_3_TYPE | EBT_802_3)
3968 +/* ui has one byte ctrl, ni has two */
3985 +struct ebt_802_3_hdr {
3995 +struct ebt_802_3_info
4004 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
4005 +++ linux-2.4.20/include/linux/netfilter_bridge/ebt_arp.h 2005-01-07 02:57:30.894817448 -0500
4007 +#ifndef __LINUX_BRIDGE_EBT_ARP_H
4008 +#define __LINUX_BRIDGE_EBT_ARP_H
4010 +#define EBT_ARP_OPCODE 0x01
4011 +#define EBT_ARP_HTYPE 0x02
4012 +#define EBT_ARP_PTYPE 0x04
4013 +#define EBT_ARP_SRC_IP 0x08
4014 +#define EBT_ARP_DST_IP 0x10
4015 +#define EBT_ARP_SRC_MAC 0x20
4016 +#define EBT_ARP_DST_MAC 0x40
4017 +#define EBT_ARP_MASK (EBT_ARP_OPCODE | EBT_ARP_HTYPE | EBT_ARP_PTYPE | \
4018 + EBT_ARP_SRC_IP | EBT_ARP_DST_IP | EBT_ARP_SRC_MAC | EBT_ARP_DST_MAC)
4019 +#define EBT_ARP_MATCH "arp"
4021 +struct ebt_arp_info
4030 + unsigned char smaddr[ETH_ALEN];
4031 + unsigned char smmsk[ETH_ALEN];
4032 + unsigned char dmaddr[ETH_ALEN];
4033 + unsigned char dmmsk[ETH_ALEN];
4039 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
4040 +++ linux-2.4.20/include/linux/netfilter_bridge/ebt_ip.h 2005-01-07 02:57:30.894817448 -0500
4046 + * Bart De Schuymer <bart.de.schuymer@pandora.be>
4051 + * added ip-sport and ip-dport
4052 + * Innominate Security Technologies AG <mhopf@innominate.com>
4056 +#ifndef __LINUX_BRIDGE_EBT_IP_H
4057 +#define __LINUX_BRIDGE_EBT_IP_H
4059 +#define EBT_IP_SOURCE 0x01
4060 +#define EBT_IP_DEST 0x02
4061 +#define EBT_IP_TOS 0x04
4062 +#define EBT_IP_PROTO 0x08
4063 +#define EBT_IP_SPORT 0x10
4064 +#define EBT_IP_DPORT 0x20
4065 +#define EBT_IP_MASK (EBT_IP_SOURCE | EBT_IP_DEST | EBT_IP_TOS | EBT_IP_PROTO |\
4066 + EBT_IP_SPORT | EBT_IP_DPORT )
4067 +#define EBT_IP_MATCH "ip"
4069 +// the same values are used for the invflags
4080 + uint16_t sport[2];
4081 + uint16_t dport[2];
4085 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
4086 +++ linux-2.4.20/include/linux/netfilter_bridge/ebt_pkttype.h 2005-01-07 02:57:30.894817448 -0500
4088 +#ifndef __LINUX_BRIDGE_EBT_PKTTYPE_H
4089 +#define __LINUX_BRIDGE_EBT_PKTTYPE_H
4091 +struct ebt_pkttype_info
4096 +#define EBT_PKTTYPE_MATCH "pkttype"
4099 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
4100 +++ linux-2.4.20/include/linux/netfilter_bridge/ebt_stp.h 2005-01-07 02:57:30.894817448 -0500
4102 +#ifndef __LINUX_BRIDGE_EBT_STP_H
4103 +#define __LINUX_BRIDGE_EBT_STP_H
4105 +#define EBT_STP_TYPE 0x0001
4107 +#define EBT_STP_FLAGS 0x0002
4108 +#define EBT_STP_ROOTPRIO 0x0004
4109 +#define EBT_STP_ROOTADDR 0x0008
4110 +#define EBT_STP_ROOTCOST 0x0010
4111 +#define EBT_STP_SENDERPRIO 0x0020
4112 +#define EBT_STP_SENDERADDR 0x0040
4113 +#define EBT_STP_PORT 0x0080
4114 +#define EBT_STP_MSGAGE 0x0100
4115 +#define EBT_STP_MAXAGE 0x0200
4116 +#define EBT_STP_HELLOTIME 0x0400
4117 +#define EBT_STP_FWDD 0x0800
4119 +#define EBT_STP_MASK 0x0fff
4120 +#define EBT_STP_CONFIG_MASK 0x0ffe
4122 +#define EBT_STP_MATCH "stp"
4124 +struct ebt_stp_config_info
4127 + uint16_t root_priol, root_priou;
4128 + char root_addr[6], root_addrmsk[6];
4129 + uint32_t root_costl, root_costu;
4130 + uint16_t sender_priol, sender_priou;
4131 + char sender_addr[6], sender_addrmsk[6];
4132 + uint16_t portl, portu;
4133 + uint16_t msg_agel, msg_ageu;
4134 + uint16_t max_agel, max_ageu;
4135 + uint16_t hello_timel, hello_timeu;
4136 + uint16_t forward_delayl, forward_delayu;
4139 +struct ebt_stp_info
4142 + struct ebt_stp_config_info config;
4144 + uint16_t invflags;
4148 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
4149 +++ linux-2.4.20/include/linux/netfilter_bridge/ebt_vlan.h 2005-01-07 02:57:30.895817296 -0500
4151 +#ifndef __LINUX_BRIDGE_EBT_VLAN_H
4152 +#define __LINUX_BRIDGE_EBT_VLAN_H
4154 +#define EBT_VLAN_ID 0x01
4155 +#define EBT_VLAN_PRIO 0x02
4156 +#define EBT_VLAN_ENCAP 0x04
4157 +#define EBT_VLAN_MASK (EBT_VLAN_ID | EBT_VLAN_PRIO | EBT_VLAN_ENCAP)
4158 +#define EBT_VLAN_MATCH "vlan"
4160 +struct ebt_vlan_info {
4161 + uint16_t id; /* VLAN ID {1-4095} */
4162 + uint8_t prio; /* VLAN User Priority {0-7} */
4163 + uint16_t encap; /* VLAN Encapsulated frame code {0-65535} */
4164 + uint8_t bitmask; /* Args bitmask bit 1=1 - ID arg,
4165 + bit 2=1 User-Priority arg, bit 3=1 encap*/
4166 + uint8_t invflags; /* Inverse bitmask bit 1=1 - inversed ID arg,
4167 + bit 2=1 - inversed Pirority arg */
4171 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
4172 +++ linux-2.4.20/include/linux/netfilter_bridge/ebt_log.h 2005-01-07 02:57:30.895817296 -0500
4174 +#ifndef __LINUX_BRIDGE_EBT_LOG_H
4175 +#define __LINUX_BRIDGE_EBT_LOG_H
4177 +#define EBT_LOG_IP 0x01 // if the frame is made by ip, log the ip information
4178 +#define EBT_LOG_ARP 0x02
4179 +#define EBT_LOG_MASK (EBT_LOG_IP | EBT_LOG_ARP)
4180 +#define EBT_LOG_PREFIX_SIZE 30
4181 +#define EBT_LOG_WATCHER "log"
4183 +struct ebt_log_info
4186 + uint8_t prefix[EBT_LOG_PREFIX_SIZE];
4191 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
4192 +++ linux-2.4.20/include/linux/netfilter_bridge/ebt_nat.h 2005-01-07 02:57:30.895817296 -0500
4194 +#ifndef __LINUX_BRIDGE_EBT_NAT_H
4195 +#define __LINUX_BRIDGE_EBT_NAT_H
4197 +struct ebt_nat_info
4199 + unsigned char mac[ETH_ALEN];
4200 + // EBT_ACCEPT, EBT_DROP, EBT_CONTINUE or EBT_RETURN
4203 +#define EBT_SNAT_TARGET "snat"
4204 +#define EBT_DNAT_TARGET "dnat"
4207 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
4208 +++ linux-2.4.20/include/linux/netfilter_bridge/ebt_redirect.h 2005-01-07 02:57:30.895817296 -0500
4210 +#ifndef __LINUX_BRIDGE_EBT_REDIRECT_H
4211 +#define __LINUX_BRIDGE_EBT_REDIRECT_H
4213 +struct ebt_redirect_info
4215 + // EBT_ACCEPT, EBT_DROP or EBT_CONTINUE or EBT_RETURN
4218 +#define EBT_REDIRECT_TARGET "redirect"
4221 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
4222 +++ linux-2.4.20/include/linux/netfilter_bridge/ebt_mark_m.h 2005-01-07 02:57:30.896817144 -0500
4224 +#ifndef __LINUX_BRIDGE_EBT_MARK_M_H
4225 +#define __LINUX_BRIDGE_EBT_MARK_M_H
4227 +#define EBT_MARK_AND 0x01
4228 +#define EBT_MARK_OR 0x02
4229 +#define EBT_MARK_MASK (EBT_MARK_AND | EBT_MARK_OR)
4230 +struct ebt_mark_m_info
4232 + unsigned long mark, mask;
4236 +#define EBT_MARK_MATCH "mark_m"
4239 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
4240 +++ linux-2.4.20/include/linux/netfilter_bridge/ebt_mark_t.h 2005-01-07 02:57:30.896817144 -0500
4242 +#ifndef __LINUX_BRIDGE_EBT_MARK_T_H
4243 +#define __LINUX_BRIDGE_EBT_MARK_T_H
4245 +struct ebt_mark_t_info
4247 + unsigned long mark;
4248 + // EBT_ACCEPT, EBT_DROP or EBT_CONTINUE or EBT_RETURN
4251 +#define EBT_MARK_TARGET "mark"
4254 --- linux-2.4.20/include/linux/netfilter.h~140-ebtables-brnf-5 2001-11-22 14:47:48.000000000 -0500
4255 +++ linux-2.4.20/include/linux/netfilter.h 2005-01-07 02:57:30.896817144 -0500
4256 @@ -117,28 +117,34 @@
4257 /* This is gross, but inline doesn't cut it for avoiding the function
4258 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
4259 #ifdef CONFIG_NETFILTER_DEBUG
4260 -#define NF_HOOK nf_hook_slow
4261 +#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
4262 +nf_hook_slow((pf), (hook), (skb), (indev), (outdev), (okfn), INT_MIN)
4263 +#define NF_HOOK_THRESH nf_hook_slow
4265 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
4266 (list_empty(&nf_hooks[(pf)][(hook)]) \
4268 - : nf_hook_slow((pf), (hook), (skb), (indev), (outdev), (okfn)))
4269 + : nf_hook_slow((pf), (hook), (skb), (indev), (outdev), (okfn), INT_MIN))
4270 +#define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
4271 +(list_empty(&nf_hooks[(pf)][(hook)]) \
4273 + : nf_hook_slow((pf), (hook), (skb), (indev), (outdev), (okfn), (thresh)))
4276 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
4277 struct net_device *indev, struct net_device *outdev,
4278 - int (*okfn)(struct sk_buff *));
4279 + int (*okfn)(struct sk_buff *), int thresh);
4281 /* Call setsockopt() */
4282 -int nf_setsockopt(struct sock *sk, int pf, int optval, char *opt,
4283 +int nf_setsockopt(struct sock *sk, int pf, int optval, char *opt,
4285 int nf_getsockopt(struct sock *sk, int pf, int optval, char *opt,
4288 /* Packet queuing */
4289 -typedef int (*nf_queue_outfn_t)(struct sk_buff *skb,
4290 +typedef int (*nf_queue_outfn_t)(struct sk_buff *skb,
4291 struct nf_info *info, void *data);
4292 -extern int nf_register_queue_handler(int pf,
4293 +extern int nf_register_queue_handler(int pf,
4294 nf_queue_outfn_t outfn, void *data);
4295 extern int nf_unregister_queue_handler(int pf);
4296 extern void nf_reinject(struct sk_buff *skb,
4297 --- linux-2.4.20/include/linux/netfilter_ipv4.h~140-ebtables-brnf-5 2002-02-25 14:38:13.000000000 -0500
4298 +++ linux-2.4.20/include/linux/netfilter_ipv4.h 2005-01-07 02:57:30.896817144 -0500
4300 enum nf_ip_hook_priorities {
4301 NF_IP_PRI_FIRST = INT_MIN,
4302 NF_IP_PRI_CONNTRACK = -200,
4303 + NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD = -175,
4304 NF_IP_PRI_MANGLE = -150,
4305 NF_IP_PRI_NAT_DST = -100,
4306 + NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT = -50,
4307 NF_IP_PRI_FILTER = 0,
4308 NF_IP_PRI_NAT_SRC = 100,
4309 NF_IP_PRI_LAST = INT_MAX,
4310 --- linux-2.4.20/include/linux/skbuff.h~140-ebtables-brnf-5 2002-08-02 20:39:46.000000000 -0400
4311 +++ linux-2.4.20/include/linux/skbuff.h 2005-01-07 02:57:30.897816992 -0500
4314 struct nf_conntrack *master;
4317 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4318 +struct nf_bridge_info {
4320 + struct net_device *physindev;
4321 + struct net_device *physoutdev;
4322 + unsigned int mask;
4323 + unsigned long hh[16 / sizeof(unsigned long)];
4329 struct sk_buff_head {
4331 #ifdef CONFIG_NETFILTER_DEBUG
4332 unsigned int nf_debug;
4334 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4335 + struct nf_bridge_info *nf_bridge; /* Saved data about a bridged frame - see br_netfilter.c */
4337 #endif /*CONFIG_NETFILTER*/
4339 #if defined(CONFIG_HIPPI)
4341 * If users==1, we are the only owner and are can avoid redundant
4347 * kfree_skb - free an sk_buff
4348 * @skb: buffer to free
4350 * Drop a reference to the buffer and free it if the usage count has
4355 static inline void kfree_skb(struct sk_buff *skb)
4357 if (atomic_read(&skb->users) == 1 || atomic_dec_and_test(&skb->users))
4358 @@ -1144,6 +1158,20 @@
4360 atomic_inc(&nfct->master->use);
4363 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4364 +static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
4366 + if (nf_bridge && atomic_dec_and_test(&nf_bridge->use))
4369 +static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
4372 + atomic_inc(&nf_bridge->use);
4378 #endif /* __KERNEL__ */
4379 --- linux-2.4.20/net/core/netfilter.c~140-ebtables-brnf-5 2002-08-02 20:39:46.000000000 -0400
4380 +++ linux-2.4.20/net/core/netfilter.c 2005-01-07 02:57:30.898816840 -0500
4382 -/* netfilter.c: look after the filters for various protocols.
4383 +/* netfilter.c: look after the filters for various protocols.
4384 * Heavily influenced by the old firewall.c by David Bonn and Alan Cox.
4386 * Thanks to Rob `CmdrTaco' Malda for not influencing this code in any
4387 @@ -342,10 +342,15 @@
4388 const struct net_device *indev,
4389 const struct net_device *outdev,
4390 struct list_head **i,
4391 - int (*okfn)(struct sk_buff *))
4392 + int (*okfn)(struct sk_buff *),
4395 for (*i = (*i)->next; *i != head; *i = (*i)->next) {
4396 struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;
4398 + if (hook_thresh > elem->priority)
4401 switch (elem->hook(hook, skb, indev, outdev, okfn)) {
4408 - NFDEBUG("Evil return from %p(%u).\n",
4409 + NFDEBUG("Evil return from %p(%u).\n",
4416 int nf_register_queue_handler(int pf, nf_queue_outfn_t outfn, void *data)
4421 br_write_lock_bh(BR_NETPROTO_LOCK);
4422 @@ -400,12 +405,12 @@
4427 - * Any packet that leaves via this function must come back
4429 + * Any packet that leaves via this function must come back
4430 * through nf_reinject().
4432 -static void nf_queue(struct sk_buff *skb,
4433 - struct list_head *elem,
4434 +static void nf_queue(struct sk_buff *skb,
4435 + struct list_head *elem,
4436 int pf, unsigned int hook,
4437 struct net_device *indev,
4438 struct net_device *outdev,
4439 @@ -413,6 +418,10 @@
4442 struct nf_info *info;
4443 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4444 + struct net_device *physindev = NULL;
4445 + struct net_device *physoutdev = NULL;
4448 if (!queue_handler[pf].outfn) {
4450 @@ -428,18 +437,31 @@
4454 - *info = (struct nf_info) {
4455 + *info = (struct nf_info) {
4456 (struct nf_hook_ops *)elem, pf, hook, indev, outdev, okfn };
4458 /* Bump dev refs so they don't vanish while packet is out */
4459 if (indev) dev_hold(indev);
4460 if (outdev) dev_hold(outdev);
4462 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4463 + if (skb->nf_bridge) {
4464 + physindev = skb->nf_bridge->physindev;
4465 + if (physindev) dev_hold(physindev);
4466 + physoutdev = skb->nf_bridge->physoutdev;
4467 + if (physoutdev) dev_hold(physoutdev);
4471 status = queue_handler[pf].outfn(skb, info, queue_handler[pf].data);
4473 /* James M doesn't say fuck enough. */
4474 if (indev) dev_put(indev);
4475 if (outdev) dev_put(outdev);
4476 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4477 + if (physindev) dev_put(physindev);
4478 + if (physoutdev) dev_put(physoutdev);
4484 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
4485 struct net_device *indev,
4486 struct net_device *outdev,
4487 - int (*okfn)(struct sk_buff *))
4488 + int (*okfn)(struct sk_buff *),
4491 struct list_head *elem;
4492 unsigned int verdict;
4495 elem = &nf_hooks[pf][hook];
4496 verdict = nf_iterate(&nf_hooks[pf][hook], &skb, hook, indev,
4497 - outdev, &elem, okfn);
4498 + outdev, &elem, okfn, hook_thresh);
4499 if (verdict == NF_QUEUE) {
4500 NFDEBUG("nf_hook: Verdict = QUEUE.\n");
4501 nf_queue(skb, elem, pf, hook, indev, outdev, okfn);
4502 @@ -510,6 +533,14 @@
4504 /* We don't have BR_NETPROTO_LOCK here */
4505 br_read_lock_bh(BR_NETPROTO_LOCK);
4506 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4507 + if (skb->nf_bridge) {
4508 + if (skb->nf_bridge->physindev)
4509 + dev_put(skb->nf_bridge->physindev);
4510 + if (skb->nf_bridge->physoutdev)
4511 + dev_put(skb->nf_bridge->physoutdev);
4514 for (i = nf_hooks[info->pf][info->hook].next; i != elem; i = i->next) {
4515 if (i == &nf_hooks[info->pf][info->hook]) {
4516 /* The module which sent it to userspace is gone. */
4519 if (verdict == NF_ACCEPT) {
4520 verdict = nf_iterate(&nf_hooks[info->pf][info->hook],
4523 info->indev, info->outdev, &elem,
4525 + info->okfn, INT_MIN);
4529 @@ -539,20 +570,19 @@
4533 - nf_queue(skb, elem, info->pf, info->hook,
4534 + nf_queue(skb, elem, info->pf, info->hook,
4535 info->indev, info->outdev, info->okfn);
4542 br_read_unlock_bh(BR_NETPROTO_LOCK);
4544 /* Release those devices we held, or Alexey will kill me. */
4545 if (info->indev) dev_put(info->indev);
4546 if (info->outdev) dev_put(info->outdev);
4549 + if (verdict == NF_DROP)
4555 --- linux-2.4.20/net/core/skbuff.c~140-ebtables-brnf-5 2002-08-02 20:39:46.000000000 -0400
4556 +++ linux-2.4.20/net/core/skbuff.c 2005-01-07 02:57:30.899816688 -0500
4558 #ifdef CONFIG_NETFILTER_DEBUG
4561 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4562 + skb->nf_bridge = NULL;
4565 #ifdef CONFIG_NET_SCHED
4569 #ifdef CONFIG_NETFILTER
4570 nf_conntrack_put(skb->nfct);
4571 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4572 + nf_bridge_put(skb->nf_bridge);
4575 skb_headerinit(skb, NULL, 0); /* clean state */
4578 #ifdef CONFIG_NETFILTER_DEBUG
4581 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4584 #endif /*CONFIG_NETFILTER*/
4585 #if defined(CONFIG_HIPPI)
4589 #ifdef CONFIG_NETFILTER
4590 nf_conntrack_get(skb->nfct);
4591 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4592 + nf_bridge_get(skb->nf_bridge);
4597 @@ -437,6 +449,10 @@
4598 #ifdef CONFIG_NETFILTER_DEBUG
4599 new->nf_debug=old->nf_debug;
4601 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4602 + new->nf_bridge=old->nf_bridge;
4603 + nf_bridge_get(new->nf_bridge);
4606 #ifdef CONFIG_NET_SCHED
4607 new->tc_index = old->tc_index;
4608 --- linux-2.4.20/net/ipv4/netfilter/ip_tables.c~140-ebtables-brnf-5 2002-11-28 18:53:15.000000000 -0500
4609 +++ linux-2.4.20/net/ipv4/netfilter/ip_tables.c 2005-01-07 02:57:30.901816384 -0500
4610 @@ -122,12 +122,19 @@
4612 ip_packet_match(const struct iphdr *ip,
4614 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4615 + const char *physindev,
4618 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4619 + const char *physoutdev,
4621 const struct ipt_ip *ipinfo,
4626 + unsigned long ret2 = 1;
4628 #define FWINV(bool,invflg) ((bool) ^ !!(ipinfo->invflags & invflg))
4630 @@ -157,7 +164,15 @@
4631 & ((const unsigned long *)ipinfo->iniface_mask)[i];
4634 - if (FWINV(ret != 0, IPT_INV_VIA_IN)) {
4635 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4636 + for (i = 0, ret2 = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
4637 + ret2 |= (((const unsigned long *)physindev)[i]
4638 + ^ ((const unsigned long *)ipinfo->iniface)[i])
4639 + & ((const unsigned long *)ipinfo->iniface_mask)[i];
4643 + if (FWINV(ret != 0 && ret2 != 0, IPT_INV_VIA_IN)) {
4644 dprintf("VIA in mismatch (%s vs %s).%s\n",
4645 indev, ipinfo->iniface,
4646 ipinfo->invflags&IPT_INV_VIA_IN ?" (INV)":"");
4647 @@ -170,7 +185,15 @@
4648 & ((const unsigned long *)ipinfo->outiface_mask)[i];
4651 - if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {
4652 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4653 + for (i = 0, ret2 = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
4654 + ret2 |= (((const unsigned long *)physoutdev)[i]
4655 + ^ ((const unsigned long *)ipinfo->outiface)[i])
4656 + & ((const unsigned long *)ipinfo->outiface_mask)[i];
4660 + if (FWINV(ret != 0 && ret2 != 0, IPT_INV_VIA_OUT)) {
4661 dprintf("VIA out mismatch (%s vs %s).%s\n",
4662 outdev, ipinfo->outiface,
4663 ipinfo->invflags&IPT_INV_VIA_OUT ?" (INV)":"");
4665 /* Initializing verdict to NF_DROP keeps gcc happy. */
4666 unsigned int verdict = NF_DROP;
4667 const char *indev, *outdev;
4668 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4669 + const char *physindev, *physoutdev;
4672 struct ipt_entry *e, *back;
4674 @@ -278,6 +304,13 @@
4675 datalen = (*pskb)->len - ip->ihl * 4;
4676 indev = in ? in->name : nulldevname;
4677 outdev = out ? out->name : nulldevname;
4678 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4679 + physindev = ((*pskb)->nf_bridge && (*pskb)->nf_bridge->physindev) ?
4680 + (*pskb)->nf_bridge->physindev->name : nulldevname;
4681 + physoutdev = ((*pskb)->nf_bridge && (*pskb)->nf_bridge->physoutdev) ?
4682 + (*pskb)->nf_bridge->physoutdev->name : nulldevname;
4685 /* We handle fragments by dealing with the first fragment as
4686 * if it was a normal packet. All other fragments are treated
4687 * normally, except that they will NEVER match rules that ask
4688 @@ -313,7 +346,15 @@
4691 (*pskb)->nfcache |= e->nfcache;
4692 - if (ip_packet_match(ip, indev, outdev, &e->ip, offset)) {
4693 + if (ip_packet_match(ip, indev,
4694 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4698 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4701 + &e->ip, offset)) {
4702 struct ipt_entry_target *t;
4704 if (IPT_MATCH_ITERATE(e, do_match,
4705 --- linux-2.4.20/net/ipv4/ip_output.c~140-ebtables-brnf-5 2002-11-28 18:53:15.000000000 -0500
4706 +++ linux-2.4.20/net/ipv4/ip_output.c 2005-01-07 02:57:30.902816232 -0500
4707 @@ -879,6 +879,10 @@
4708 /* Connection association is same as pre-frag packet */
4709 skb2->nfct = skb->nfct;
4710 nf_conntrack_get(skb2->nfct);
4711 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4712 + skb2->nf_bridge = skb->nf_bridge;
4713 + nf_bridge_get(skb2->nf_bridge);
4715 #ifdef CONFIG_NETFILTER_DEBUG
4716 skb2->nf_debug = skb->nf_debug;
4718 --- linux-2.4.20/net/ipv4/netfilter/ipt_LOG.c~140-ebtables-brnf-5 2002-02-25 14:38:14.000000000 -0500
4719 +++ linux-2.4.20/net/ipv4/netfilter/ipt_LOG.c 2005-01-07 02:57:30.902816232 -0500
4720 @@ -289,6 +289,18 @@
4723 out ? out->name : "");
4724 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4725 + if ((*pskb)->nf_bridge) {
4726 + struct net_device *physindev = (*pskb)->nf_bridge->physindev;
4727 + struct net_device *physoutdev = (*pskb)->nf_bridge->physoutdev;
4729 + if (physindev && in != physindev)
4730 + printk("PHYSIN=%s ", physindev->name);
4731 + if (physoutdev && out != physoutdev)
4732 + printk("PHYSOUT=%s ", physoutdev->name);
4737 /* MAC logging for input chain only. */
4739 --- linux-2.4.20/net/ipv4/netfilter/Makefile~140-ebtables-brnf-5 2002-11-28 18:53:15.000000000 -0500
4740 +++ linux-2.4.20/net/ipv4/netfilter/Makefile 2005-01-07 02:57:30.903816080 -0500
4742 obj-$(CONFIG_IP_NF_MATCH_UNCLEAN) += ipt_unclean.o
4743 obj-$(CONFIG_IP_NF_MATCH_TCPMSS) += ipt_tcpmss.o
4745 +obj-$(CONFIG_IP_NF_MATCH_PHYSDEV) += ipt_physdev.o
4748 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
4749 obj-$(CONFIG_IP_NF_TARGET_MIRROR) += ipt_MIRROR.o
4750 --- linux-2.4.20/net/ipv4/netfilter/Config.in~140-ebtables-brnf-5 2002-11-28 18:53:15.000000000 -0500
4751 +++ linux-2.4.20/net/ipv4/netfilter/Config.in 2005-01-07 02:57:30.903816080 -0500
4753 dep_tristate ' Unclean match support (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_UNCLEAN $CONFIG_IP_NF_IPTABLES
4754 dep_tristate ' Owner match support (EXPERIMENTAL)' CONFIG_IP_NF_MATCH_OWNER $CONFIG_IP_NF_IPTABLES
4756 + if [ "$CONFIG_BRIDGE" != "n" ]; then
4757 + dep_tristate ' Physdev match support' CONFIG_IP_NF_MATCH_PHYSDEV
4760 dep_tristate ' Packet filtering' CONFIG_IP_NF_FILTER $CONFIG_IP_NF_IPTABLES
4761 if [ "$CONFIG_IP_NF_FILTER" != "n" ]; then
4762 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
4763 +++ linux-2.4.20/net/bridge/br_netfilter.c 2005-01-07 02:57:30.904815928 -0500
4766 + * Handle firewalling
4767 + * Linux ethernet bridge
4770 + * Lennert Buytenhek <buytenh@gnu.org>
4771 + * Bart De Schuymer <bdschuym@pandora.be>
4773 + * This program is free software; you can redistribute it and/or
4774 + * modify it under the terms of the GNU General Public License
4775 + * as published by the Free Software Foundation; either version
4776 + * 2 of the License, or (at your option) any later version.
4778 + * Lennert dedicates this file to Kerstin Wurdinger.
4781 +#include <linux/module.h>
4782 +#include <linux/kernel.h>
4783 +#include <linux/ip.h>
4784 +#include <linux/netdevice.h>
4785 +#include <linux/skbuff.h>
4786 +#include <linux/if_ether.h>
4787 +#include <linux/netfilter_bridge.h>
4788 +#include <linux/netfilter_ipv4.h>
4789 +#include <linux/in_route.h>
4790 +#include <net/ip.h>
4791 +#include <asm/uaccess.h>
4792 +#include <asm/checksum.h>
4793 +#include "br_private.h"
4796 +#define skb_origaddr(skb) (((struct bridge_skb_cb *) \
4797 + (skb->cb))->daddr.ipv4)
4798 +#define store_orig_dstaddr(skb) (skb_origaddr(skb) = (skb)->nh.iph->daddr)
4799 +#define dnat_took_place(skb) (skb_origaddr(skb) != (skb)->nh.iph->daddr)
4800 +#define clear_cb(skb) (memset(&skb_origaddr(skb), 0, \
4801 + sizeof(struct bridge_skb_cb)))
4803 +#define has_bridge_parent(device) ((device)->br_port != NULL)
4804 +#define bridge_parent(device) (&((device)->br_port->br->dev))
4806 +/* We need these fake structures to make netfilter happy --
4807 + * lots of places assume that skb->dst != NULL, which isn't
4808 + * all that unreasonable.
4810 + * Currently, we fill in the PMTU entry because netfilter
4811 + * refragmentation needs it, and the rt_flags entry because
4812 + * ipt_REJECT needs it. Future netfilter modules might
4813 + * require us to fill additional fields.
4815 +static struct net_device __fake_net_device = {
4816 + .hard_header_len = ETH_HLEN
4819 +static struct rtable __fake_rtable = {
4822 + __refcnt: ATOMIC_INIT(1),
4823 + dev: &__fake_net_device,
4832 +/* PF_BRIDGE/PRE_ROUTING *********************************************/
4833 +static void __br_dnat_complain(void)
4835 + static unsigned long last_complaint = 0;
4837 + if (jiffies - last_complaint >= 5 * HZ) {
4838 + printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
4839 + "forwarding to be enabled\n");
4840 + last_complaint = jiffies;
4845 +/* This requires some explaining. If DNAT has taken place,
4846 + * we will need to fix up the destination Ethernet address,
4847 + * and this is a tricky process.
4849 + * There are two cases to consider:
4850 + * 1. The packet was DNAT'ed to a device in the same bridge
4851 + * port group as it was received on. We can still bridge
4853 + * 2. The packet was DNAT'ed to a different device, either
4854 + * a non-bridged device or another bridge port group.
4855 + * The packet will need to be routed.
4857 + * The correct way of distinguishing between these two cases is to
4858 + * call ip_route_input() and to look at skb->dst->dev, which is
4859 + * changed to the destination device if ip_route_input() succeeds.
4861 + * Let us first consider the case that ip_route_input() succeeds:
4863 + * If skb->dst->dev equals the logical bridge device the packet
4864 + * came in on, we can consider this bridging. We then call
4865 + * skb->dst->output() which will make the packet enter br_nf_local_out()
4866 + * not much later. In that function it is assured that the iptables
4867 + * FORWARD chain is traversed for the packet.
4869 + * Otherwise, the packet is considered to be routed and we just
4870 + * change the destination MAC address so that the packet will
4871 + * later be passed up to the IP stack to be routed.
4873 + * Let us now consider the case that ip_route_input() fails:
4875 + * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
4876 + * will fail, while __ip_route_output_key() will return success. The source
4877 + * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
4878 + * thinks we're handling a locally generated packet and won't care
4879 + * if IP forwarding is allowed. We send a warning message to the users's
4880 + * log telling her to put IP forwarding on.
4882 + * ip_route_input() will also fail if there is no route available.
4883 + * In that case we just drop the packet.
4885 + * --Lennert, 20020411
4886 + * --Bart, 20020416 (updated)
4887 + * --Bart, 20021007 (updated)
4890 +static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
4892 +#ifdef CONFIG_NETFILTER_DEBUG
4893 + skb->nf_debug |= (1 << NF_BR_PRE_ROUTING) | (1 << NF_BR_FORWARD);
4896 + if (skb->pkt_type == PACKET_OTHERHOST) {
4897 + skb->pkt_type = PACKET_HOST;
4898 + skb->nf_bridge->mask |= BRNF_PKT_TYPE;
4901 + skb->dev = bridge_parent(skb->dev);
4902 + skb->dst->output(skb);
4906 +static int br_nf_pre_routing_finish(struct sk_buff *skb)
4908 + struct net_device *dev = skb->dev;
4909 + struct iphdr *iph = skb->nh.iph;
4910 + struct nf_bridge_info *nf_bridge = skb->nf_bridge;
4912 +#ifdef CONFIG_NETFILTER_DEBUG
4913 + skb->nf_debug ^= (1 << NF_BR_PRE_ROUTING);
4916 + if (nf_bridge->mask & BRNF_PKT_TYPE) {
4917 + skb->pkt_type = PACKET_OTHERHOST;
4918 + nf_bridge->mask ^= BRNF_PKT_TYPE;
4921 + if (dnat_took_place(skb)) {
4922 + if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
4924 + struct rtable *rt;
4926 + if (!ip_route_output(&rt, iph->daddr, 0, iph->tos, 0)) {
4927 + /* Bridged-and-DNAT'ed traffic doesn't
4928 + * require ip_forwarding.
4930 + if (((struct dst_entry *)rt)->dev == dev) {
4931 + skb->dst = (struct dst_entry *)rt;
4932 + goto bridged_dnat;
4934 + __br_dnat_complain();
4935 + dst_release((struct dst_entry *)rt);
4940 + if (skb->dst->dev == dev) {
4942 + /* Tell br_nf_local_out this is a
4945 + nf_bridge->mask |= BRNF_BRIDGED_DNAT;
4946 + skb->dev = nf_bridge->physindev;
4948 + NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
4949 + skb, skb->dev, NULL,
4950 + br_nf_pre_routing_finish_bridge,
4954 + memcpy(skb->mac.ethernet->h_dest, dev->dev_addr,
4958 + skb->dst = (struct dst_entry *)&__fake_rtable;
4959 + dst_hold(skb->dst);
4963 + skb->dev = nf_bridge->physindev;
4964 + NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
4965 + br_handle_frame_finish, 1);
4970 +/* Replicate the checks that IPv4 does on packet reception.
4971 + * Set skb->dev to the bridge device (i.e. parent of the
4972 + * receiving device) to make netfilter happy, the REDIRECT
4973 + * target in particular. Save the original destination IP
4974 + * address to be able to detect DNAT afterwards.
4976 +static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
4977 + const struct net_device *in, const struct net_device *out,
4978 + int (*okfn)(struct sk_buff *))
4980 + struct iphdr *iph;
4982 + struct sk_buff *skb;
4983 + struct nf_bridge_info *nf_bridge;
4985 + if ((*pskb)->protocol != __constant_htons(ETH_P_IP))
4988 + if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
4991 + if (!pskb_may_pull(skb, sizeof(struct iphdr)))
4994 + iph = skb->nh.iph;
4995 + if (iph->ihl < 5 || iph->version != 4)
4998 + if (!pskb_may_pull(skb, 4*iph->ihl))
5001 + iph = skb->nh.iph;
5002 + if (ip_fast_csum((__u8 *)iph, iph->ihl) != 0)
5005 + len = ntohs(iph->tot_len);
5006 + if (skb->len < len || len < 4*iph->ihl)
5009 + if (skb->len > len) {
5010 + __pskb_trim(skb, len);
5011 + if (skb->ip_summed == CHECKSUM_HW)
5012 + skb->ip_summed = CHECKSUM_NONE;
5015 +#ifdef CONFIG_NETFILTER_DEBUG
5016 + skb->nf_debug ^= (1 << NF_IP_PRE_ROUTING);
5018 + if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
5021 + if (skb->pkt_type == PACKET_OTHERHOST) {
5022 + skb->pkt_type = PACKET_HOST;
5023 + nf_bridge->mask |= BRNF_PKT_TYPE;
5026 + nf_bridge->physindev = skb->dev;
5027 + skb->dev = bridge_parent(skb->dev);
5028 + store_orig_dstaddr(skb);
5030 + NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
5031 + br_nf_pre_routing_finish);
5036 +// IP_INC_STATS_BH(IpInHdrErrors);
5042 +/* PF_BRIDGE/LOCAL_IN ************************************************/
5043 +/* The packet is locally destined, which requires a real
5044 + * dst_entry, so detach the fake one. On the way up, the
5045 + * packet would pass through PRE_ROUTING again (which already
5046 + * took place when the packet entered the bridge), but we
5047 + * register an IPv4 PRE_ROUTING 'sabotage' hook that will
5048 + * prevent this from happening.
5050 +static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
5051 + const struct net_device *in, const struct net_device *out,
5052 + int (*okfn)(struct sk_buff *))
5054 + struct sk_buff *skb = *pskb;
5056 + if (skb->protocol != __constant_htons(ETH_P_IP))
5059 + if (skb->dst == (struct dst_entry *)&__fake_rtable) {
5060 + dst_release(skb->dst);
5068 +/* PF_BRIDGE/FORWARD *************************************************/
5069 +static int br_nf_forward_finish(struct sk_buff *skb)
5071 + struct nf_bridge_info *nf_bridge = skb->nf_bridge;
5073 +#ifdef CONFIG_NETFILTER_DEBUG
5074 + skb->nf_debug ^= (1 << NF_BR_FORWARD);
5077 + if (nf_bridge->mask & BRNF_PKT_TYPE) {
5078 + skb->pkt_type = PACKET_OTHERHOST;
5079 + nf_bridge->mask ^= BRNF_PKT_TYPE;
5082 + NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, nf_bridge->physindev,
5083 + skb->dev, br_forward_finish, 1);
5088 +/* This is the 'purely bridged' case. We pass the packet to
5089 + * netfilter with indev and outdev set to the bridge device,
5090 + * but we are still able to filter on the 'real' indev/outdev
5091 + * because of the ipt_physdev.c module.
5093 +static unsigned int br_nf_forward(unsigned int hook, struct sk_buff **pskb,
5094 + const struct net_device *in, const struct net_device *out,
5095 + int (*okfn)(struct sk_buff *))
5097 + struct sk_buff *skb = *pskb;
5098 + struct nf_bridge_info *nf_bridge;
5100 + if (skb->protocol != __constant_htons(ETH_P_IP))
5103 +#ifdef CONFIG_NETFILTER_DEBUG
5104 + skb->nf_debug ^= (1 << NF_BR_FORWARD);
5107 + nf_bridge = skb->nf_bridge;
5108 + if (skb->pkt_type == PACKET_OTHERHOST) {
5109 + skb->pkt_type = PACKET_HOST;
5110 + nf_bridge->mask |= BRNF_PKT_TYPE;
5113 + nf_bridge->mask |= BRNF_BRIDGED; /* The physdev module checks on this */
5114 + nf_bridge->physoutdev = skb->dev;
5116 + NF_HOOK(PF_INET, NF_IP_FORWARD, skb, bridge_parent(nf_bridge->physindev),
5117 + bridge_parent(skb->dev), br_nf_forward_finish);
5123 +/* PF_BRIDGE/LOCAL_OUT ***********************************************/
5124 +static int br_nf_local_out_finish(struct sk_buff *skb)
5126 +#ifdef CONFIG_NETFILTER_DEBUG
5127 + skb->nf_debug &= ~(1 << NF_BR_LOCAL_OUT);
5130 + NF_HOOK_THRESH(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
5131 + br_forward_finish, NF_BR_PRI_FIRST + 1);
5137 +/* This function sees both locally originated IP packets and forwarded
5138 + * IP packets (in both cases the destination device is a bridge
5139 + * device). It also sees bridged-and-DNAT'ed packets.
5140 + * To be able to filter on the physical bridge devices (with the ipt_physdev.c
5141 + * module), we steal packets destined to a bridge device away from the
5142 + * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
5143 + * when we have determined the real output device. This is done in here.
5145 + * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
5146 + * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
5147 + * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
5148 + * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
5149 + * will be executed.
5150 + * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
5151 + * this packet before, and so the packet was locally originated. We fake
5152 + * the PF_INET/LOCAL_OUT hook.
5153 + * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
5154 + * so we fake the PF_INET/FORWARD hook. ipv4_sabotage_out() makes sure
5155 + * even routed packets that didn't arrive on a bridge interface have their
5156 + * nf_bridge->physindev set.
5159 +static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
5160 + const struct net_device *in, const struct net_device *out,
5161 + int (*_okfn)(struct sk_buff *))
5163 + int (*okfn)(struct sk_buff *skb);
5164 + struct net_device *realindev;
5165 + struct sk_buff *skb = *pskb;
5166 + struct nf_bridge_info *nf_bridge;
5168 + if (skb->protocol != __constant_htons(ETH_P_IP))
5171 + /* Sometimes we get packets with NULL ->dst here (for example,
5172 + * running a dhcp client daemon triggers this).
5174 + if (skb->dst == NULL)
5177 + nf_bridge = skb->nf_bridge;
5178 + nf_bridge->physoutdev = skb->dev;
5180 + realindev = nf_bridge->physindev;
5182 + /* Bridged, take PF_BRIDGE/FORWARD.
5183 + * (see big note in front of br_nf_pre_routing_finish)
5185 + if (nf_bridge->mask & BRNF_BRIDGED_DNAT) {
5186 + okfn = br_forward_finish;
5188 + if (nf_bridge->mask & BRNF_PKT_TYPE) {
5189 + skb->pkt_type = PACKET_OTHERHOST;
5190 + nf_bridge->mask ^= BRNF_PKT_TYPE;
5193 + NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev,
5196 + okfn = br_nf_local_out_finish;
5197 + /* IP forwarded traffic has a physindev, locally
5198 + * generated traffic hasn't.
5200 + if (realindev != NULL) {
5201 + if (((nf_bridge->mask & BRNF_DONT_TAKE_PARENT) == 0) &&
5202 + has_bridge_parent(realindev))
5203 + realindev = bridge_parent(realindev);
5205 + NF_HOOK_THRESH(PF_INET, NF_IP_FORWARD, skb, realindev,
5206 + bridge_parent(skb->dev), okfn,
5207 + NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD + 1);
5209 +#ifdef CONFIG_NETFILTER_DEBUG
5210 + skb->nf_debug ^= (1 << NF_IP_LOCAL_OUT);
5213 + NF_HOOK_THRESH(PF_INET, NF_IP_LOCAL_OUT, skb, realindev,
5214 + bridge_parent(skb->dev), okfn,
5215 + NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT + 1);
5223 +/* PF_BRIDGE/POST_ROUTING ********************************************/
5224 +static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
5225 + const struct net_device *in, const struct net_device *out,
5226 + int (*okfn)(struct sk_buff *))
5228 + struct sk_buff *skb = *pskb;
5229 + struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
5231 + /* Be very paranoid. Must be a device driver bug. */
5232 + if (skb->mac.raw < skb->head || skb->mac.raw + ETH_HLEN > skb->data) {
5233 + printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
5234 + "bad mac.raw pointer.");
5235 + if (skb->dev != NULL) {
5236 + printk("[%s]", skb->dev->name);
5237 + if (has_bridge_parent(skb->dev))
5238 + printk("[%s]", bridge_parent(skb->dev)->name);
5244 + if (skb->protocol != __constant_htons(ETH_P_IP))
5247 + /* Sometimes we get packets with NULL ->dst here (for example,
5248 + * running a dhcp client daemon triggers this).
5250 + if (skb->dst == NULL)
5253 +#ifdef CONFIG_NETFILTER_DEBUG
5254 + skb->nf_debug ^= (1 << NF_IP_POST_ROUTING);
5257 + /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
5258 + * about the value of skb->pkt_type.
5260 + if (skb->pkt_type == PACKET_OTHERHOST) {
5261 + skb->pkt_type = PACKET_HOST;
5262 + nf_bridge->mask |= BRNF_PKT_TYPE;
5265 + memcpy(nf_bridge->hh, skb->data - 16, 16);
5267 + NF_HOOK(PF_INET, NF_IP_POST_ROUTING, skb, NULL,
5268 + bridge_parent(skb->dev), br_dev_queue_push_xmit);
5274 +/* IPv4/SABOTAGE *****************************************************/
5276 +/* Don't hand locally destined packets to PF_INET/PRE_ROUTING
5277 + * for the second time.
5279 +static unsigned int ipv4_sabotage_in(unsigned int hook, struct sk_buff **pskb,
5280 + const struct net_device *in, const struct net_device *out,
5281 + int (*okfn)(struct sk_buff *))
5283 + if (in->hard_start_xmit == br_dev_xmit &&
5284 + okfn != br_nf_pre_routing_finish) {
5292 +/* Postpone execution of PF_INET/FORWARD, PF_INET/LOCAL_OUT
5293 + * and PF_INET/POST_ROUTING until we have done the forwarding
5294 + * decision in the bridge code and have determined skb->physoutdev.
5296 +static unsigned int ipv4_sabotage_out(unsigned int hook, struct sk_buff **pskb,
5297 + const struct net_device *in, const struct net_device *out,
5298 + int (*okfn)(struct sk_buff *))
5300 + if (out->hard_start_xmit == br_dev_xmit &&
5301 + okfn != br_nf_forward_finish &&
5302 + okfn != br_nf_local_out_finish &&
5303 + okfn != br_dev_queue_push_xmit) {
5304 + struct sk_buff *skb = *pskb;
5305 + struct nf_bridge_info *nf_bridge;
5307 + if (!skb->nf_bridge && !nf_bridge_alloc(skb))
5310 + nf_bridge = skb->nf_bridge;
5312 + /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
5313 + * will need the indev then. For a brouter, the real indev
5314 + * can be a bridge port, so we make sure br_nf_local_out()
5315 + * doesn't use the bridge parent of the indev by using
5316 + * the BRNF_DONT_TAKE_PARENT mask.
5318 + if (hook == NF_IP_FORWARD && nf_bridge->physindev == NULL) {
5319 + nf_bridge->mask &= BRNF_DONT_TAKE_PARENT;
5320 + nf_bridge->physindev = (struct net_device *)in;
5329 +/* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
5330 + * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
5331 + * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
5332 + * ip_refrag() can return NF_STOLEN.
5334 +static struct nf_hook_ops br_nf_ops[] = {
5335 + { .hook = br_nf_pre_routing,
5337 + .hooknum = NF_BR_PRE_ROUTING,
5338 + .priority = NF_BR_PRI_BRNF, },
5339 + { .hook = br_nf_local_in,
5341 + .hooknum = NF_BR_LOCAL_IN,
5342 + .priority = NF_BR_PRI_BRNF, },
5343 + { .hook = br_nf_forward,
5345 + .hooknum = NF_BR_FORWARD,
5346 + .priority = NF_BR_PRI_BRNF, },
5347 + { .hook = br_nf_local_out,
5349 + .hooknum = NF_BR_LOCAL_OUT,
5350 + .priority = NF_BR_PRI_FIRST, },
5351 + { .hook = br_nf_post_routing,
5353 + .hooknum = NF_BR_POST_ROUTING,
5354 + .priority = NF_BR_PRI_LAST, },
5355 + { .hook = ipv4_sabotage_in,
5357 + .hooknum = NF_IP_PRE_ROUTING,
5358 + .priority = NF_IP_PRI_FIRST, },
5359 + { .hook = ipv4_sabotage_out,
5361 + .hooknum = NF_IP_FORWARD,
5362 + .priority = NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD, },
5363 + { .hook = ipv4_sabotage_out,
5365 + .hooknum = NF_IP_LOCAL_OUT,
5366 + .priority = NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
5367 + { .hook = ipv4_sabotage_out,
5369 + .hooknum = NF_IP_POST_ROUTING,
5370 + .priority = NF_IP_PRI_FIRST, },
5373 +int br_netfilter_init(void)
5377 + for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) {
5380 + if ((ret = nf_register_hook(&br_nf_ops[i])) >= 0)
5384 + nf_unregister_hook(&br_nf_ops[i]);
5389 + printk(KERN_NOTICE "Bridge firewalling registered\n");
5394 +void br_netfilter_fini(void)
5398 + for (i = ARRAY_SIZE(br_nf_ops) - 1; i >= 0; i--)
5399 + nf_unregister_hook(&br_nf_ops[i]);
5401 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
5402 +++ linux-2.4.20/net/ipv4/netfilter/ipt_physdev.c 2005-01-07 02:57:30.904815928 -0500
5404 +/* Kernel module to match the bridge port in and
5405 + * out device for IP packets coming into contact with a bridge. */
5406 +#include <linux/module.h>
5407 +#include <linux/skbuff.h>
5408 +#include <linux/netfilter_ipv4/ipt_physdev.h>
5409 +#include <linux/netfilter_ipv4/ip_tables.h>
5410 +#include <linux/netfilter_bridge.h>
5411 +#include <linux/netdevice.h>
5416 +match(const struct sk_buff *skb,
5417 + const struct net_device *in,
5418 + const struct net_device *out,
5419 + const void *matchinfo,
5422 + u_int16_t datalen,
5426 + static const char nulldevname[IFNAMSIZ] = { 0 };
5427 + const struct ipt_physdev_info *info = matchinfo;
5428 + unsigned long ret;
5429 + const char *indev, *outdev;
5430 + struct nf_bridge_info *nf_bridge;
5432 + /* Not a bridged IP packet or no info available yet:
5433 + * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if
5434 + * the destination device will be a bridge. */
5435 + if (!(nf_bridge = skb->nf_bridge)) {
5436 + /* Return MATCH if the invert flags of the used options are on */
5437 + if ((info->bitmask & IPT_PHYSDEV_OP_BRIDGED) &&
5438 + !(info->invert & IPT_PHYSDEV_OP_BRIDGED))
5440 + if ((info->bitmask & IPT_PHYSDEV_OP_ISIN) &&
5441 + !(info->invert & IPT_PHYSDEV_OP_ISIN))
5443 + if ((info->bitmask & IPT_PHYSDEV_OP_ISOUT) &&
5444 + !(info->invert & IPT_PHYSDEV_OP_ISOUT))
5446 + if ((info->bitmask & IPT_PHYSDEV_OP_IN) &&
5447 + !(info->invert & IPT_PHYSDEV_OP_IN))
5449 + if ((info->bitmask & IPT_PHYSDEV_OP_OUT) &&
5450 + !(info->invert & IPT_PHYSDEV_OP_OUT))
5455 + /* This only makes sense in the FORWARD and POSTROUTING chains */
5456 + if ((info->bitmask & IPT_PHYSDEV_OP_BRIDGED) &&
5457 + (!!(nf_bridge->mask & BRNF_BRIDGED) ^
5458 + !(info->invert & IPT_PHYSDEV_OP_BRIDGED)))
5461 + if ((info->bitmask & IPT_PHYSDEV_OP_ISIN &&
5462 + (!nf_bridge->physindev ^ !!(info->invert & IPT_PHYSDEV_OP_ISIN))) ||
5463 + (info->bitmask & IPT_PHYSDEV_OP_ISOUT &&
5464 + (!nf_bridge->physoutdev ^ !!(info->invert & IPT_PHYSDEV_OP_ISOUT))))
5467 + if (!(info->bitmask & IPT_PHYSDEV_OP_IN))
5468 + goto match_outdev;
5469 + indev = nf_bridge->physindev ? nf_bridge->physindev->name : nulldevname;
5470 + for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
5471 + ret |= (((const unsigned long *)indev)[i]
5472 + ^ ((const unsigned long *)info->physindev)[i])
5473 + & ((const unsigned long *)info->in_mask)[i];
5476 + if ((ret == 0) ^ !(info->invert & IPT_PHYSDEV_OP_IN))
5480 + if (!(info->bitmask & IPT_PHYSDEV_OP_OUT))
5482 + outdev = nf_bridge->physoutdev ?
5483 + nf_bridge->physoutdev->name : nulldevname;
5484 + for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
5485 + ret |= (((const unsigned long *)outdev)[i]
5486 + ^ ((const unsigned long *)info->physoutdev)[i])
5487 + & ((const unsigned long *)info->out_mask)[i];
5490 + return (ret != 0) ^ !(info->invert & IPT_PHYSDEV_OP_OUT);
5494 +checkentry(const char *tablename,
5495 + const struct ipt_ip *ip,
5497 + unsigned int matchsize,
5498 + unsigned int hook_mask)
5500 + const struct ipt_physdev_info *info = matchinfo;
5502 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_physdev_info)))
5504 + if (!(info->bitmask & IPT_PHYSDEV_OP_MASK) ||
5505 + info->bitmask & ~IPT_PHYSDEV_OP_MASK)
5510 +static struct ipt_match physdev_match = {
5511 + .name = "physdev",
5513 + .checkentry = &checkentry,
5514 + .me = THIS_MODULE,
5517 +static int __init init(void)
5519 + return ipt_register_match(&physdev_match);
5522 +static void __exit fini(void)
5524 + ipt_unregister_match(&physdev_match);
5529 +MODULE_LICENSE("GPL");
5531 --- /dev/null 2004-04-06 13:56:48.000000000 -0400
5532 +++ linux-2.4.20/include/linux/netfilter_ipv4/ipt_physdev.h 2005-01-07 02:57:30.905815776 -0500
5534 +#ifndef _IPT_PHYSDEV_H
5535 +#define _IPT_PHYSDEV_H
5538 +#include <linux/if.h>
5541 +#define IPT_PHYSDEV_OP_IN 0x01
5542 +#define IPT_PHYSDEV_OP_OUT 0x02
5543 +#define IPT_PHYSDEV_OP_BRIDGED 0x04
5544 +#define IPT_PHYSDEV_OP_ISIN 0x08
5545 +#define IPT_PHYSDEV_OP_ISOUT 0x10
5546 +#define IPT_PHYSDEV_OP_MASK (0x20 - 1)
5548 +struct ipt_physdev_info {
5551 + char physindev[IFNAMSIZ];
5552 + char in_mask[IFNAMSIZ];
5553 + char physoutdev[IFNAMSIZ];
5554 + char out_mask[IFNAMSIZ];
5557 +#endif /*_IPT_PHYSDEV_H*/