]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/packages/linux/linux-wrt-2.4.20/140-ebtables-brnf-5.patch
OE tree imported from monotone branch org.openembedded.oz354fam083 at revision 8b12e3...
[familiar-h63xx-build.git] / org.handhelds.familiar / packages / linux / linux-wrt-2.4.20 / 140-ebtables-brnf-5.patch
1
2 #
3 # Patch managed by http://www.mn-logistik.de/unsupported/pxa250/patcher
4 #
5
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
8 @@ -145,8 +145,10 @@
9  /* br_forward.c */
10  extern void br_deliver(struct net_bridge_port *to,
11                 struct sk_buff *skb);
12 +extern int br_dev_queue_push_xmit(struct sk_buff *skb);
13  extern void br_forward(struct net_bridge_port *to,
14                 struct sk_buff *skb);
15 +extern int br_forward_finish(struct sk_buff *skb);
16  extern void br_flood_deliver(struct net_bridge *br,
17                       struct sk_buff *skb,
18                       int clone);
19 @@ -167,7 +169,8 @@
20                            int *ifindices);
21  
22  /* br_input.c */
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);
26  
27  /* br_ioctl.c */
28  extern void br_call_ioctl_atomic(void (*fn)(void));
29 @@ -178,6 +181,10 @@
30              unsigned long arg2);
31  extern int br_ioctl_deviceless_stub(unsigned long arg);
32  
33 +/* br_netfilter.c */
34 +extern int br_netfilter_init(void);
35 +extern void br_netfilter_fini(void);
36 +
37  /* br_stp.c */
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
42 @@ -102,7 +102,8 @@
43  struct net_bridge_port;
44  
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);
49  
50  #endif
51  
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
54 @@ -1393,7 +1393,7 @@
55  
56  
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;
60  #endif
61  
62  static __inline__ int handle_bridge(struct sk_buff *skb,
63 @@ -1410,7 +1410,6 @@
64                 }
65         }
66  
67 -       br_handle_frame_hook(skb);
68         return ret;
69  }
70  
71 @@ -1466,11 +1465,16 @@
72         if (skb->dev->divert && skb->dev->divert->divert)
73                 ret = handle_diverter(skb);
74  #endif /* CONFIG_NET_DIVERT */
75 -                       
76 +
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) {
82 +               int ret;
83 +
84 +               ret = handle_bridge(skb, pt_prev);
85 +               if (br_handle_frame_hook(skb) == 0)
86 +                       return ret;
87 +               pt_prev = NULL;
88         }
89  #endif
90  
91 @@ -1866,7 +1870,7 @@
92   *     are adjusted, %RTM_NEWLINK is sent to the routing socket and the
93   *     function returns zero.
94   */
95
96 +
97  int netdev_set_master(struct net_device *slave, struct net_device *master)
98  {
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
102 @@ -24,6 +24,9 @@
103  
104  static int br_pass_frame_up_finish(struct sk_buff *skb)
105  {
106 +#ifdef CONFIG_NETFILTER_DEBUG
107 +       skb->nf_debug = 0;
108 +#endif
109         netif_rx(skb);
110  
111         return 0;
112 @@ -46,7 +49,7 @@
113                         br_pass_frame_up_finish);
114  }
115  
116 -static int br_handle_frame_finish(struct sk_buff *skb)
117 +int br_handle_frame_finish(struct sk_buff *skb)
118  {
119         struct net_bridge *br;
120         unsigned char *dest;
121 @@ -112,7 +115,7 @@
122         return 0;
123  }
124  
125 -void br_handle_frame(struct sk_buff *skb)
126 +int br_handle_frame(struct sk_buff *skb)
127  {
128         struct net_bridge *br;
129         unsigned char *dest;
130 @@ -146,25 +149,35 @@
131                 goto handle_special_frame;
132  
133         if (p->state == BR_STATE_FORWARDING) {
134 +
135 +               if (br_should_route_hook && br_should_route_hook(&skb)) {
136 +                       read_unlock(&br->lock);
137 +                       return -1;
138 +               }
139 +
140 +               if (!memcmp(p->br->dev.dev_addr, dest, ETH_ALEN))
141 +                       skb->pkt_type = PACKET_HOST;
142 +
143                 NF_HOOK(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
144                         br_handle_frame_finish);
145                 read_unlock(&br->lock);
146 -               return;
147 +               return 0;
148         }
149  
150  err:
151         read_unlock(&br->lock);
152  err_nolock:
153         kfree_skb(skb);
154 -       return;
155 +       return 0;
156  
157  handle_special_frame:
158         if (!dest[5]) {
159                 br_stp_handle_bpdu(skb);
160                 read_unlock(&br->lock);
161 -               return;
162 +               return 0;
163         }
164  
165         read_unlock(&br->lock);
166         kfree_skb(skb);
167 +       return 0;
168  }
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
171 @@ -30,18 +30,22 @@
172         return 1;
173  }
174  
175 -static int __dev_queue_push_xmit(struct sk_buff *skb)
176 +int br_dev_queue_push_xmit(struct sk_buff *skb)
177  {
178 +#ifdef CONFIG_NETFILTER
179 +       if (skb->nf_bridge)
180 +               memcpy(skb->data - 16, skb->nf_bridge->hh, 16);
181 +#endif
182         skb_push(skb, ETH_HLEN);
183         dev_queue_xmit(skb);
184  
185         return 0;
186  }
187  
188 -static int __br_forward_finish(struct sk_buff *skb)
189 +int br_forward_finish(struct sk_buff *skb)
190  {
191         NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
192 -                       __dev_queue_push_xmit);
193 +                       br_dev_queue_push_xmit);
194  
195         return 0;
196  }
197 @@ -49,8 +53,11 @@
198  static void __br_deliver(struct net_bridge_port *to, struct sk_buff *skb)
199  {
200         skb->dev = to->dev;
201 +#ifdef CONFIG_NETFILTER_DEBUG
202 +       skb->nf_debug = 0;
203 +#endif
204         NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
205 -                       __br_forward_finish);
206 +                       br_forward_finish);
207  }
208  
209  static void __br_forward(struct net_bridge_port *to, struct sk_buff *skb)
210 @@ -61,7 +68,7 @@
211         skb->dev = to->dev;
212  
213         NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
214 -                       __br_forward_finish);
215 +                       br_forward_finish);
216  }
217  
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
221 @@ -29,6 +29,8 @@
222  #include "../atm/lec.h"
223  #endif
224  
225 +int (*br_should_route_hook) (struct sk_buff **pskb) = NULL;
226 +
227  void br_dec_use_count()
228  {
229         MOD_DEC_USE_COUNT;
230 @@ -43,6 +45,10 @@
231  {
232         printk(KERN_INFO "NET4: Ethernet Bridge 008 for NET4.0\n");
233  
234 +#ifdef CONFIG_NETFILTER
235 +       if (br_netfilter_init())
236 +               return 1;
237 +#endif
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)
241 @@ -61,6 +67,9 @@
242  
243  static void __exit br_deinit(void)
244  {
245 +#ifdef CONFIG_NETFILTER
246 +       br_netfilter_fini();
247 +#endif
248         unregister_netdevice_notifier(&br_device_notifier);
249         br_call_ioctl_atomic(__br_clear_ioctl_hook);
250  
251 @@ -74,7 +83,7 @@
252  #endif
253  }
254  
255 -EXPORT_NO_SYMBOLS;
256 +EXPORT_SYMBOL(br_should_route_hook);
257  
258  module_init(br_init)
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
262 @@ -7,10 +7,17 @@
263  #
264  # Note 2! The CFLAGS definition is now in the main makefile...
265  
266 +export-objs := br.o
267 +
268  O_TARGET       := bridge.o
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
272 +
273 +ifeq ($(CONFIG_NETFILTER),y)
274 +obj-y          += br_netfilter.o
275 +endif
276 +
277  obj-m          := $(O_TARGET)
278  
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
282 @@ -1,11 +1,14 @@
283  #ifndef __LINUX_BRIDGE_NETFILTER_H
284  #define __LINUX_BRIDGE_NETFILTER_H
285  
286 -/* bridge-specific defines for netfilter. 
287 +/* bridge-specific defines for netfilter.
288   */
289  
290  #include <linux/config.h>
291  #include <linux/netfilter.h>
292 +#if defined(__KERNEL__) && defined(CONFIG_NETFILTER)
293 +#include <asm/atomic.h>
294 +#endif
295  
296  /* Bridge Hooks */
297  /* After promisc drops, checksum checks. */
298 @@ -18,7 +21,49 @@
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
306  
307 +#ifdef __KERNEL__
308  
309 +#define BRNF_PKT_TYPE                  0x01
310 +#define BRNF_BRIDGED_DNAT              0x02
311 +#define BRNF_DONT_TAKE_PARENT          0x04
312 +#define BRNF_BRIDGED                   0x08
313 +
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,
323 +};
324 +
325 +#ifdef CONFIG_NETFILTER
326 +static inline
327 +struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
328 +{
329 +       struct nf_bridge_info **nf_bridge = &(skb->nf_bridge);
330 +
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;
335 +       }
336 +
337 +       return *nf_bridge;
338 +}
339 +
340 +struct bridge_skb_cb {
341 +       union {
342 +               __u32 ipv4;
343 +       } daddr;
344 +};
345 +#endif /* CONFIG_NETFILTER */
346 +
347 +#endif /* __KERNEL__ */
348  #endif
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
351 @@ -7,7 +7,8 @@
352  
353  O_TARGET :=    network.o
354  
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
359  
360  subdir-y :=    core ethernet
361 @@ -26,6 +27,12 @@
362  endif
363  endif
364  
365 +ifneq ($(CONFIG_BRIDGE),n)
366 +ifneq ($(CONFIG_BRIDGE),)
367 +subdir-$(CONFIG_BRIDGE)                += bridge/netfilter
368 +endif
369 +endif
370 +
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
376 @@ -68,6 +68,9 @@
377     source net/decnet/Config.in
378  fi
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
382 +fi
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
388 @@ -0,0 +1,30 @@
389 +#
390 +# Makefile for the netfilter modules on top of bridging.
391 +#
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).
395 +#
396 +# Note 2! The CFLAGS definition is now in the main makefile...
397 +
398 +O_TARGET       := netfilter.o
399 +
400 +export-objs := ebtables.o
401 +
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
421 @@ -0,0 +1,19 @@
422 +#
423 +# Bridge netfilter configuration
424 +#
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
443 @@ -0,0 +1,90 @@
444 +/*
445 + *  ebtable_filter
446 + *
447 + *     Authors:
448 + *     Bart De Schuymer <bart.de.schuymer@pandora.be>
449 + *
450 + *  April, 2002
451 + *
452 + */
453 +
454 +#include <linux/netfilter_bridge/ebtables.h>
455 +#include <linux/module.h>
456 +
457 +#define FILTER_VALID_HOOKS ((1 << NF_BR_LOCAL_IN) | (1 << NF_BR_FORWARD) | \
458 +   (1 << NF_BR_LOCAL_OUT))
459 +
460 +static struct ebt_entries initial_chains[] =
461 +{
462 +  {0, "INPUT", 0, EBT_ACCEPT, 0},
463 +  {0, "FORWARD", 0, EBT_ACCEPT, 0},
464 +  {0, "OUTPUT", 0, EBT_ACCEPT, 0}
465 +};
466 +
467 +static struct ebt_replace initial_table =
468 +{
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
472 +};
473 +
474 +static int check(const struct ebt_table_info *info, unsigned int valid_hooks)
475 +{
476 +       if (valid_hooks & ~FILTER_VALID_HOOKS)
477 +               return -EINVAL;
478 +       return 0;
479 +}
480 +
481 +static struct ebt_table frame_filter =
482 +{ 
483 +  {NULL, NULL}, "filter", &initial_table, FILTER_VALID_HOOKS, 
484 +  RW_LOCK_UNLOCKED, check, NULL
485 +};
486 +
487 +static unsigned int
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 *))
490 +{
491 +       return ebt_do_table(hook, pskb, in, out, &frame_filter);
492 +}
493 +
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}
501 +};
502 +
503 +static int __init init(void)
504 +{
505 +       int i, j, ret;
506 +
507 +       ret = ebt_register_table(&frame_filter);
508 +       if (ret < 0)
509 +               return ret;
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)
512 +                       goto cleanup;
513 +       return ret;
514 +cleanup:
515 +       for (j = 0; j < i; j++)
516 +               nf_unregister_hook(&ebt_ops_filter[j]);
517 +       ebt_unregister_table(&frame_filter);
518 +       return ret;
519 +}
520 +
521 +static void __exit fini(void)
522 +{
523 +       int i;
524 +
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);
528 +}
529 +
530 +module_init(init);
531 +module_exit(fini);
532 +EXPORT_NO_SYMBOLS;
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
536 @@ -0,0 +1,96 @@
537 +/*
538 + *  ebtable_nat
539 + *
540 + *     Authors:
541 + *     Bart De Schuymer <bart.de.schuymer@pandora.be>
542 + *
543 + *  April, 2002
544 + *
545 + */
546 +
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))
551 +
552 +static struct ebt_entries initial_chains[] =
553 +{
554 +  {0, "PREROUTING", 0, EBT_ACCEPT, 0},
555 +  {0, "OUTPUT", 0, EBT_ACCEPT, 0},
556 +  {0, "POSTROUTING", 0, EBT_ACCEPT, 0}
557 +};
558 +
559 +static struct ebt_replace initial_table =
560 +{
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
564 +};
565 +
566 +static int check(const struct ebt_table_info *info, unsigned int valid_hooks)
567 +{
568 +       if (valid_hooks & ~NAT_VALID_HOOKS)
569 +               return -EINVAL;
570 +       return 0;
571 +}
572 +
573 +static struct ebt_table frame_nat =
574 +{
575 +  {NULL, NULL}, "nat", &initial_table, NAT_VALID_HOOKS,
576 +  RW_LOCK_UNLOCKED, check, NULL
577 +};
578 +
579 +static unsigned int
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 *))
582 +{
583 +       return ebt_do_table(hook, pskb, in, out, &frame_nat);
584 +}
585 +
586 +static unsigned int
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 *))
589 +{
590 +       return ebt_do_table(hook, pskb, in, out, &frame_nat);
591 +}
592 +
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},
600 +};
601 +
602 +static int __init init(void)
603 +{
604 +       int i, ret, j;
605 +
606 +       ret = ebt_register_table(&frame_nat);
607 +       if (ret < 0)
608 +               return ret;
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)
611 +                       goto cleanup;
612 +       return ret;
613 +cleanup:
614 +       for (j = 0; j < i; j++)
615 +               nf_unregister_hook(&ebt_ops_nat[j]);
616 +       ebt_unregister_table(&frame_nat);
617 +       return ret;
618 +}
619 +
620 +static void __exit fini(void)
621 +{
622 +       int i;
623 +
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);
627 +}
628 +
629 +module_init(init);
630 +module_exit(fini);
631 +EXPORT_NO_SYMBOLS;
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
635 @@ -0,0 +1,79 @@
636 +/*
637 + *  ebtable_broute
638 + *
639 + *     Authors:
640 + *     Bart De Schuymer <bart.de.schuymer@pandora.be>
641 + *
642 + *  April, 2002
643 + *
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.
647 + */
648 +
649 +#include <linux/netfilter_bridge/ebtables.h>
650 +#include <linux/module.h>
651 +#include <linux/if_bridge.h>
652 +#include <linux/brlock.h>
653 +
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};
658 +
659 +static struct ebt_replace initial_table =
660 +{
661 +  "broute", 1 << NF_BR_BROUTING, 0, sizeof(struct ebt_entries),
662 +  { [NF_BR_BROUTING]&initial_chain}, 0, NULL, (char *)&initial_chain
663 +};
664 +
665 +static int check(const struct ebt_table_info *info, unsigned int valid_hooks)
666 +{
667 +       if (valid_hooks & ~(1 << NF_BR_BROUTING))
668 +               return -EINVAL;
669 +       return 0;
670 +}
671 +
672 +static struct ebt_table broute_table =
673 +{
674 +  {NULL, NULL}, "broute", &initial_table, 1 << NF_BR_BROUTING,
675 +  RW_LOCK_UNLOCKED, check, NULL
676 +};
677 +
678 +static int ebt_broute(struct sk_buff **pskb)
679 +{
680 +       int ret;
681 +
682 +       ret = ebt_do_table(NF_BR_BROUTING, pskb, (*pskb)->dev, NULL,
683 +          &broute_table);
684 +       if (ret == NF_DROP)
685 +               return 1; // route it
686 +       return 0; // bridge it
687 +}
688 +
689 +static int __init init(void)
690 +{
691 +       int ret;
692 +
693 +       ret = ebt_register_table(&broute_table);
694 +       if (ret < 0)
695 +               return ret;
696 +       br_write_lock_bh(BR_NETPROTO_LOCK);
697 +       // see br_input.c
698 +       br_should_route_hook = ebt_broute;
699 +       br_write_unlock_bh(BR_NETPROTO_LOCK);
700 +       return ret;
701 +}
702 +
703 +static void __exit fini(void)
704 +{
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);
709 +}
710 +
711 +module_init(init);
712 +module_exit(fini);
713 +EXPORT_NO_SYMBOLS;
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
717 @@ -0,0 +1,74 @@
718 +/*
719 + * 802_3
720 + *
721 + * Author:
722 + * Chris Vitale csv@bluetail.com
723 + *
724 + * May 2003
725 + * 
726 + */
727 +
728 +#include <linux/netfilter_bridge/ebtables.h>
729 +#include <linux/netfilter_bridge/ebt_802_3.h>
730 +#include <linux/module.h>
731 +
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)
734 +{
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;
738 +
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;
744 +       }
745 +
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;
751 +       }
752 +
753 +       return EBT_MATCH;
754 +}
755 +
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)
759 +{
760 +       struct ebt_802_3_info *info = (struct ebt_802_3_info *)data;
761 +
762 +       if (datalen != EBT_ALIGN(sizeof(struct ebt_802_3_info)))
763 +               return -EINVAL;
764 +       if (info->bitmask & ~EBT_802_3_MASK || info->invflags & ~EBT_802_3_MASK)
765 +               return -EINVAL;
766 +
767 +       return 0;
768 +}
769 +
770 +static struct ebt_match filter_802_3 =
771 +{
772 +       .name           = EBT_802_3_MATCH,
773 +       .match          = ebt_filter_802_3,
774 +       .check          = ebt_802_3_check,
775 +       .me             = THIS_MODULE,
776 +};
777 +
778 +static int __init init(void)
779 +{
780 +       return ebt_register_match(&filter_802_3);
781 +}
782 +
783 +static void __exit fini(void)
784 +{
785 +       ebt_unregister_match(&filter_802_3);
786 +}
787 +
788 +module_init(init);
789 +module_exit(fini);
790 +EXPORT_NO_SYMBOLS;
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
794 @@ -0,0 +1,66 @@
795 +/*
796 + *  ebt_mark
797 + *
798 + *     Authors:
799 + *     Bart De Schuymer <bart.de.schuymer@pandora.be>
800 + *
801 + *  July, 2002
802 + *
803 + */
804 +
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
808 +
809 +#include <linux/netfilter_bridge/ebtables.h>
810 +#include <linux/netfilter_bridge/ebt_mark_t.h>
811 +#include <linux/module.h>
812 +
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)
816 +{
817 +       struct ebt_mark_t_info *info = (struct ebt_mark_t_info *)data;
818 +
819 +       if ((*pskb)->nfmark != info->mark) {
820 +               (*pskb)->nfmark = info->mark;
821 +               (*pskb)->nfcache |= NFC_ALTERED;
822 +       }
823 +       return info->target;
824 +}
825 +
826 +static int ebt_target_mark_check(const char *tablename, unsigned int hookmask,
827 +   const struct ebt_entry *e, void *data, unsigned int datalen)
828 +{
829 +       struct ebt_mark_t_info *info = (struct ebt_mark_t_info *)data;
830 +
831 +       if (datalen != EBT_ALIGN(sizeof(struct ebt_mark_t_info)))
832 +               return -EINVAL;
833 +       if (BASE_CHAIN && info->target == EBT_RETURN)
834 +               return -EINVAL;
835 +       CLEAR_BASE_CHAIN_BIT;
836 +       if (INVALID_TARGET)
837 +               return -EINVAL;
838 +       return 0;
839 +}
840 +
841 +static struct ebt_target mark_target =
842 +{
843 +       {NULL, NULL}, EBT_MARK_TARGET, ebt_target_mark,
844 +       ebt_target_mark_check, NULL, THIS_MODULE
845 +};
846 +
847 +static int __init init(void)
848 +{
849 +       return ebt_register_target(&mark_target);
850 +}
851 +
852 +static void __exit fini(void)
853 +{
854 +       ebt_unregister_target(&mark_target);
855 +}
856 +
857 +module_init(init);
858 +module_exit(fini);
859 +EXPORT_NO_SYMBOLS;
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
863 @@ -0,0 +1,61 @@
864 +/*
865 + *  ebt_mark_m
866 + *
867 + *     Authors:
868 + *     Bart De Schuymer <bart.de.schuymer@pandora.be>
869 + *
870 + *  July, 2002
871 + *
872 + */
873 +
874 +#include <linux/netfilter_bridge/ebtables.h>
875 +#include <linux/netfilter_bridge/ebt_mark_m.h>
876 +#include <linux/module.h>
877 +
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)
881 +{
882 +       struct ebt_mark_m_info *info = (struct ebt_mark_m_info *) data;
883 +
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);
887 +}
888 +
889 +static int ebt_mark_check(const char *tablename, unsigned int hookmask,
890 +   const struct ebt_entry *e, void *data, unsigned int datalen)
891 +{
892 +        struct ebt_mark_m_info *info = (struct ebt_mark_m_info *) data;
893 +
894 +       if (datalen != EBT_ALIGN(sizeof(struct ebt_mark_m_info)))
895 +               return -EINVAL;
896 +       if (info->bitmask & ~EBT_MARK_MASK)
897 +               return -EINVAL;
898 +       if ((info->bitmask & EBT_MARK_OR) && (info->bitmask & EBT_MARK_AND))
899 +               return -EINVAL;
900 +       if (!info->bitmask)
901 +               return -EINVAL;
902 +       return 0;
903 +}
904 +
905 +static struct ebt_match filter_mark =
906 +{
907 +       {NULL, NULL}, EBT_MARK_MATCH, ebt_filter_mark, ebt_mark_check, NULL,
908 +       THIS_MODULE
909 +};
910 +
911 +static int __init init(void)
912 +{
913 +       return ebt_register_match(&filter_mark);
914 +}
915 +
916 +static void __exit fini(void)
917 +{
918 +       ebt_unregister_match(&filter_mark);
919 +}
920 +
921 +module_init(init);
922 +module_exit(fini);
923 +EXPORT_NO_SYMBOLS;
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
927 @@ -0,0 +1,60 @@
928 +/*
929 + *  ebt_pkttype
930 + *
931 + *     Authors:
932 + *     Bart De Schuymer <bdschuym@pandora.be>
933 + *
934 + *  April, 2003
935 + *
936 + */
937 +
938 +#include <linux/netfilter_bridge/ebtables.h>
939 +#include <linux/netfilter_bridge/ebt_pkttype.h>
940 +#include <linux/module.h>
941 +
942 +static int ebt_filter_pkttype(const struct sk_buff *skb,
943 +   const struct net_device *in,
944 +   const struct net_device *out,
945 +   const void *data,
946 +   unsigned int datalen)
947 +{
948 +       struct ebt_pkttype_info *info = (struct ebt_pkttype_info *)data;
949 +
950 +       return (skb->pkt_type != info->pkt_type) ^ info->invert;
951 +}
952 +
953 +static int ebt_pkttype_check(const char *tablename, unsigned int hookmask,
954 +   const struct ebt_entry *e, void *data, unsigned int datalen)
955 +{
956 +       struct ebt_pkttype_info *info = (struct ebt_pkttype_info *)data;
957 +
958 +       if (datalen != EBT_ALIGN(sizeof(struct ebt_pkttype_info)))
959 +               return -EINVAL;
960 +       if (info->invert != 0 && info->invert != 1)
961 +               return -EINVAL;
962 +       /* Allow any pkt_type value */
963 +       return 0;
964 +}
965 +
966 +static struct ebt_match filter_pkttype =
967 +{
968 +       .name           = EBT_PKTTYPE_MATCH,
969 +       .match          = ebt_filter_pkttype,
970 +       .check          = ebt_pkttype_check,
971 +       .me             = THIS_MODULE,
972 +};
973 +
974 +static int __init init(void)
975 +{
976 +       return ebt_register_match(&filter_pkttype);
977 +}
978 +
979 +static void __exit fini(void)
980 +{
981 +       ebt_unregister_match(&filter_pkttype);
982 +}
983 +
984 +module_init(init);
985 +module_exit(fini);
986 +EXPORT_NO_SYMBOLS;
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
990 @@ -0,0 +1,191 @@
991 +/*
992 + *  ebt_stp
993 + *
994 + *     Authors:
995 + *     Bart De Schuymer <bdschuym@pandora.be>
996 + *     Stephen Hemminger <shemminger@osdl.org>
997 + *
998 + *  June, 2003
999 + */
1000 +
1001 +#include <linux/netfilter_bridge/ebtables.h>
1002 +#include <linux/netfilter_bridge/ebt_stp.h>
1003 +#include <linux/module.h>
1004 +
1005 +#define BPDU_TYPE_CONFIG 0
1006 +#define BPDU_TYPE_TCN 0x80
1007 +
1008 +struct stp_header {
1009 +       uint8_t dsap;
1010 +       uint8_t ssap;
1011 +       uint8_t ctrl;
1012 +       uint8_t pid;
1013 +       uint8_t vers;
1014 +       uint8_t type;
1015 +};
1016 +
1017 +struct stp_config_pdu {
1018 +       uint8_t flags;
1019 +       uint8_t root[8];
1020 +       uint8_t root_cost[4];
1021 +       uint8_t sender[8];
1022 +       uint8_t port[2];
1023 +       uint8_t msg_age[2];
1024 +       uint8_t max_age[2];
1025 +       uint8_t hello_time[2];
1026 +       uint8_t forward_delay[2];
1027 +};
1028 +
1029 +#define NR16(p) (p[0] << 8 | p[1])
1030 +#define NR32(p) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3])
1031 +
1032 +static int ebt_filter_config(struct ebt_stp_info *info,
1033 +   struct stp_config_pdu *stpc)
1034 +{
1035 +       struct ebt_stp_config_info *c;
1036 +       uint16_t v16;
1037 +       uint32_t v32;
1038 +       int verdict, i;
1039 +
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;
1049 +       }
1050 +       if (info->bitmask & EBT_STP_ROOTADDR) {
1051 +               verdict = 0;
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;
1057 +       }
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;
1063 +       }
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;
1069 +       }
1070 +       if (info->bitmask & EBT_STP_SENDERADDR) {
1071 +               verdict = 0;
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;
1077 +       }
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;
1083 +       }
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;
1089 +       }
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;
1095 +       }
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;
1101 +       }
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;
1107 +       }
1108 +       return EBT_MATCH;
1109 +}
1110 +
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)
1113 +{
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;
1119 +
1120 +       /* The stp code only considers these */
1121 +       if (memcmp(&stph, header, sizeof(header)))
1122 +               return EBT_NOMATCH;
1123 +
1124 +       if (info->bitmask & EBT_STP_TYPE
1125 +           && FWINV(info->type != stph.type, EBT_STP_TYPE))
1126 +               return EBT_NOMATCH;
1127 +
1128 +       if (stph.type == BPDU_TYPE_CONFIG &&
1129 +           info->bitmask & EBT_STP_CONFIG_MASK) {
1130 +               struct stp_config_pdu stpc;
1131 +
1132 +               if (skb_copy_bits(skb, sizeof(stph), &stpc, sizeof(stpc)))
1133 +                   return EBT_NOMATCH;
1134 +               return ebt_filter_config(info, &stpc);
1135 +       }
1136 +       return EBT_MATCH;
1137 +}
1138 +
1139 +static int ebt_stp_check(const char *tablename, unsigned int hookmask,
1140 +   const struct ebt_entry *e, void *data, unsigned int datalen)
1141 +{
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 };
1146 +
1147 +       if (info->bitmask & ~EBT_STP_MASK || info->invflags & ~EBT_STP_MASK ||
1148 +           !(info->bitmask & EBT_STP_MASK))
1149 +               return -EINVAL;
1150 +       if (datalen != len)
1151 +               return -EINVAL;
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))
1155 +               return -EINVAL;
1156 +
1157 +       return 0;
1158 +}
1159 +
1160 +static struct ebt_match filter_stp =
1161 +{
1162 +       .name           = EBT_STP_MATCH,
1163 +       .match          = ebt_filter_stp,
1164 +       .check          = ebt_stp_check,
1165 +       .me             = THIS_MODULE,
1166 +};
1167 +
1168 +static int __init init(void)
1169 +{
1170 +       return ebt_register_match(&filter_stp);
1171 +}
1172 +
1173 +static void __exit fini(void)
1174 +{
1175 +       ebt_unregister_match(&filter_stp);
1176 +}
1177 +
1178 +module_init(init);
1179 +module_exit(fini);
1180 +EXPORT_NO_SYMBOLS;
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
1184 @@ -0,0 +1,71 @@
1185 +/*
1186 + *  ebt_redirect
1187 + *
1188 + *     Authors:
1189 + *     Bart De Schuymer <bart.de.schuymer@pandora.be>
1190 + *
1191 + *  April, 2002
1192 + *
1193 + */
1194 +
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"
1200 +
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)
1204 +{
1205 +       struct ebt_redirect_info *info = (struct ebt_redirect_info *)data;
1206 +
1207 +       if (hooknr != NF_BR_BROUTING)
1208 +               memcpy((**pskb).mac.ethernet->h_dest,
1209 +                  in->br_port->br->dev.dev_addr, ETH_ALEN);
1210 +       else {
1211 +               memcpy((**pskb).mac.ethernet->h_dest,
1212 +                  in->dev_addr, ETH_ALEN);
1213 +               (*pskb)->pkt_type = PACKET_HOST;
1214 +       }
1215 +       return info->target;
1216 +}
1217 +
1218 +static int ebt_target_redirect_check(const char *tablename, unsigned int hookmask,
1219 +   const struct ebt_entry *e, void *data, unsigned int datalen)
1220 +{
1221 +       struct ebt_redirect_info *info = (struct ebt_redirect_info *)data;
1222 +
1223 +       if (datalen != EBT_ALIGN(sizeof(struct ebt_redirect_info)))
1224 +               return -EINVAL;
1225 +       if (BASE_CHAIN && info->target == EBT_RETURN)
1226 +               return -EINVAL;
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)) )
1230 +               return -EINVAL;
1231 +       if (INVALID_TARGET)
1232 +               return -EINVAL;
1233 +       return 0;
1234 +}
1235 +
1236 +static struct ebt_target redirect_target =
1237 +{
1238 +       {NULL, NULL}, EBT_REDIRECT_TARGET, ebt_target_redirect,
1239 +       ebt_target_redirect_check, NULL, THIS_MODULE
1240 +};
1241 +
1242 +static int __init init(void)
1243 +{
1244 +       return ebt_register_target(&redirect_target);
1245 +}
1246 +
1247 +static void __exit fini(void)
1248 +{
1249 +       ebt_unregister_target(&redirect_target);
1250 +}
1251 +
1252 +module_init(init);
1253 +module_exit(fini);
1254 +EXPORT_NO_SYMBOLS;
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
1258 @@ -0,0 +1,149 @@
1259 +/*
1260 + *  ebt_arp
1261 + *
1262 + *     Authors:
1263 + *     Bart De Schuymer <bart.de.schuymer@pandora.be>
1264 + *     Tim Gardner <timg@tpi.com>
1265 + *
1266 + *  April, 2002
1267 + *
1268 + */
1269 +
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>
1275 +
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)
1278 +{
1279 +       struct ebt_arp_info *info = (struct ebt_arp_info *)data;
1280 +
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;
1290 +
1291 +       if (info->bitmask & (EBT_ARP_SRC_IP | EBT_ARP_DST_IP))
1292 +       {
1293 +               uint32_t arp_len = sizeof(struct arphdr) +
1294 +                  (2 * (((*skb).nh.arph)->ar_hln)) +
1295 +                  (2 * (((*skb).nh.arph)->ar_pln));
1296 +               uint32_t dst;
1297 +               uint32_t src;
1298 +
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;
1305 +
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),
1310 +                          EBT_ARP_SRC_IP))
1311 +                               return EBT_NOMATCH;
1312 +               }
1313 +
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),
1319 +                          EBT_ARP_DST_IP))
1320 +                               return EBT_NOMATCH;
1321 +               }
1322 +       }
1323 +
1324 +       if (info->bitmask & (EBT_ARP_SRC_MAC | EBT_ARP_DST_MAC))
1325 +       {
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];
1331 +
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;
1340 +
1341 +                       memcpy(&src, ((*skb).nh.raw) +
1342 +                                       sizeof(struct arphdr),
1343 +                                       ETH_ALEN);
1344 +                       verdict = 0;
1345 +                       for (i = 0; i < 6; i++)
1346 +                               verdict |= (src[i] ^ info->smaddr[i]) &
1347 +                                      info->smmsk[i];  
1348 +                       if (FWINV(verdict != 0, EBT_ARP_SRC_MAC))
1349 +                               return EBT_NOMATCH;
1350 +               }
1351 +
1352 +               if (info->bitmask & EBT_ARP_DST_MAC) { 
1353 +                       uint8_t verdict, i;
1354 +
1355 +                       memcpy(&dst, ((*skb).nh.raw) +
1356 +                                       sizeof(struct arphdr) +
1357 +                                       (((*skb).nh.arph)->ar_hln) +
1358 +                                       (((*skb).nh.arph)->ar_pln),
1359 +                                       ETH_ALEN);
1360 +                       verdict = 0;
1361 +                       for (i = 0; i < 6; i++)
1362 +                               verdict |= (dst[i] ^ info->dmaddr[i]) &
1363 +                                       info->dmmsk[i];
1364 +                       if (FWINV(verdict != 0, EBT_ARP_DST_MAC))
1365 +                               return EBT_NOMATCH;
1366 +               }
1367 +       }
1368 +
1369 +       return EBT_MATCH;
1370 +}
1371 +
1372 +static int ebt_arp_check(const char *tablename, unsigned int hookmask,
1373 +   const struct ebt_entry *e, void *data, unsigned int datalen)
1374 +{
1375 +       struct ebt_arp_info *info = (struct ebt_arp_info *)data;
1376 +
1377 +       if (datalen != EBT_ALIGN(sizeof(struct ebt_arp_info)))
1378 +               return -EINVAL;
1379 +       if ((e->ethproto != __constant_htons(ETH_P_ARP) &&
1380 +          e->ethproto != __constant_htons(ETH_P_RARP)) ||
1381 +          e->invflags & EBT_IPROTO)
1382 +               return -EINVAL;
1383 +       if (info->bitmask & ~EBT_ARP_MASK || info->invflags & ~EBT_ARP_MASK)
1384 +               return -EINVAL;
1385 +       return 0;
1386 +}
1387 +
1388 +static struct ebt_match filter_arp =
1389 +{
1390 +       {NULL, NULL}, EBT_ARP_MATCH, ebt_filter_arp, ebt_arp_check, NULL,
1391 +       THIS_MODULE
1392 +};
1393 +
1394 +static int __init init(void)
1395 +{
1396 +       return ebt_register_match(&filter_arp);
1397 +}
1398 +
1399 +static void __exit fini(void)
1400 +{
1401 +       ebt_unregister_match(&filter_arp);
1402 +}
1403 +
1404 +module_init(init);
1405 +module_exit(fini);
1406 +EXPORT_NO_SYMBOLS;
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
1410 @@ -0,0 +1,121 @@
1411 +/*
1412 + *  ebt_ip
1413 + *
1414 + *     Authors:
1415 + *     Bart De Schuymer <bart.de.schuymer@pandora.be>
1416 + *
1417 + *  April, 2002
1418 + *
1419 + *  Changes:
1420 + *    added ip-sport and ip-dport
1421 + *    Innominate Security Technologies AG <mhopf@innominate.com>
1422 + *    September, 2002
1423 + */
1424 +
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>
1430 +
1431 +struct tcpudphdr {
1432 +       uint16_t src;
1433 +       uint16_t dst;
1434 +};
1435 +
1436 +union h_u {
1437 +       unsigned char *raw;
1438 +       struct tcpudphdr *tuh;
1439 +};
1440 +
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)
1444 +{
1445 +       struct ebt_ip_info *info = (struct ebt_ip_info *)data;
1446 +
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,
1452 +                         EBT_IP_PROTO))
1453 +                       return EBT_NOMATCH;
1454 +               if ( info->protocol == IPPROTO_TCP ||
1455 +                    info->protocol == IPPROTO_UDP )
1456 +               {
1457 +                       union h_u h;
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],
1463 +                                         EBT_IP_DPORT))
1464 +                               return EBT_NOMATCH;
1465 +                       }
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],
1470 +                                         EBT_IP_SPORT))
1471 +                               return EBT_NOMATCH;
1472 +                       }
1473 +               }
1474 +       }
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;
1483 +       return EBT_MATCH;
1484 +}
1485 +
1486 +static int ebt_ip_check(const char *tablename, unsigned int hookmask,
1487 +   const struct ebt_entry *e, void *data, unsigned int datalen)
1488 +{
1489 +       struct ebt_ip_info *info = (struct ebt_ip_info *)data;
1490 +
1491 +       if (datalen != EBT_ALIGN(sizeof(struct ebt_ip_info)))
1492 +               return -EINVAL;
1493 +       if (e->ethproto != __constant_htons(ETH_P_IP) ||
1494 +          e->invflags & EBT_IPROTO)
1495 +               return -EINVAL;
1496 +       if (info->bitmask & ~EBT_IP_MASK || info->invflags & ~EBT_IP_MASK)
1497 +               return -EINVAL;
1498 +       if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) {
1499 +               if (!info->bitmask & EBT_IPROTO)
1500 +                       return -EINVAL;
1501 +               if (info->protocol != IPPROTO_TCP &&
1502 +                   info->protocol != IPPROTO_UDP)
1503 +                        return -EINVAL;
1504 +       }
1505 +       if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1])
1506 +               return -EINVAL;
1507 +       if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1])
1508 +               return -EINVAL;
1509 +       return 0;
1510 +}
1511 +
1512 +static struct ebt_match filter_ip =
1513 +{
1514 +       {NULL, NULL}, EBT_IP_MATCH, ebt_filter_ip, ebt_ip_check, NULL,
1515 +       THIS_MODULE
1516 +};
1517 +
1518 +static int __init init(void)
1519 +{
1520 +       return ebt_register_match(&filter_ip);
1521 +}
1522 +
1523 +static void __exit fini(void)
1524 +{
1525 +       ebt_unregister_match(&filter_ip);
1526 +}
1527 +
1528 +module_init(init);
1529 +module_exit(fini);
1530 +EXPORT_NO_SYMBOLS;
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
1534 @@ -0,0 +1,259 @@
1535 +/*
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>
1539 + *    
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.
1544 + * 
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.
1549 + *  
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
1553 + */
1554 +
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>
1560 +
1561 +static unsigned char debug;
1562 +#define MODULE_VERSION "0.6"
1563 +
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"
1568 +                  MODULE_VERSION);
1569 +MODULE_LICENSE("GPL");
1570 +
1571 +
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;
1577 +
1578 +/*
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.
1584 + * Parameters:
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)
1594 + */
1595 +static int
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)
1600 +{
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 */
1603 +
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 */
1608 +
1609 +       /*
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. 
1617 +        */
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;
1622 +
1623 +       /*
1624 +        * Checking VLAN Identifier (VID)
1625 +        */
1626 +       if (GET_BITMASK(EBT_VLAN_ID)) { /* Is VLAN ID parsed? */
1627 +               EXIT_ON_MISMATCH(id, EBT_VLAN_ID);
1628 +       }
1629 +       /*
1630 +        * Checking user_priority
1631 +        */
1632 +       if (GET_BITMASK(EBT_VLAN_PRIO)) {       /* Is VLAN user_priority parsed? */
1633 +               EXIT_ON_MISMATCH(prio, EBT_VLAN_PRIO);
1634 +       }
1635 +       /*
1636 +        * Checking Encapsulated Proto (Length/Type) field
1637 +        */
1638 +       if (GET_BITMASK(EBT_VLAN_ENCAP)) {      /* Is VLAN Encap parsed? */
1639 +               EXIT_ON_MISMATCH(encap, EBT_VLAN_ENCAP);
1640 +       }
1641 +       /*
1642 +        * All possible extension parameters was parsed.
1643 +        * If rule never returned by missmatch, then all ok.
1644 +        */
1645 +       return 0;
1646 +}
1647 +
1648 +/*
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.
1652 + * Parameters:
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.)
1661 + */
1662 +static int
1663 +ebt_check_vlan(const char *tablename,
1664 +              unsigned int hooknr,
1665 +              const struct ebt_entry *e, void *data, unsigned int datalen)
1666 +{
1667 +       struct ebt_vlan_info *info = (struct ebt_vlan_info *) data;
1668 +
1669 +       /*
1670 +        * Parameters buffer overflow check 
1671 +        */
1672 +       if (datalen != EBT_ALIGN(sizeof(struct ebt_vlan_info))) {
1673 +               DEBUG_MSG
1674 +                   ("passed size %d is not eq to ebt_vlan_info (%d)\n",
1675 +                    datalen, sizeof(struct ebt_vlan_info));
1676 +               return -EINVAL;
1677 +       }
1678 +
1679 +       /*
1680 +        * Is it 802.1Q frame checked?
1681 +        */
1682 +       if (e->ethproto != __constant_htons(ETH_P_8021Q)) {
1683 +               DEBUG_MSG
1684 +                   ("passed entry proto %2.4X is not 802.1Q (8100)\n",
1685 +                    (unsigned short) ntohs(e->ethproto));
1686 +               return -EINVAL;
1687 +       }
1688 +
1689 +       /*
1690 +        * Check for bitmask range 
1691 +        * True if even one bit is out of mask
1692 +        */
1693 +       if (info->bitmask & ~EBT_VLAN_MASK) {
1694 +               DEBUG_MSG("bitmask %2X is out of mask (%2X)\n",
1695 +                         info->bitmask, EBT_VLAN_MASK);
1696 +               return -EINVAL;
1697 +       }
1698 +
1699 +       /*
1700 +        * Check for inversion flags range 
1701 +        */
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);
1705 +               return -EINVAL;
1706 +       }
1707 +
1708 +       /*
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.
1715 +        */
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) {
1719 +                               DEBUG_MSG
1720 +                                   ("id %d is out of range (1-4096)\n",
1721 +                                    info->id);
1722 +                               return -EINVAL;
1723 +                       }
1724 +                       /*
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. 
1729 +                        */
1730 +                       info->bitmask &= ~EBT_VLAN_PRIO;
1731 +               }
1732 +               /*
1733 +                * Else, id=0 (null VLAN ID)  => user_priority range (any?)
1734 +                */
1735 +       }
1736 +
1737 +       if (GET_BITMASK(EBT_VLAN_PRIO)) {
1738 +               if ((unsigned char) info->prio > 7) {
1739 +                       DEBUG_MSG
1740 +                           ("prio %d is out of range (0-7)\n",
1741 +                            info->prio);
1742 +                       return -EINVAL;
1743 +               }
1744 +       }
1745 +       /*
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
1749 +        */
1750 +       if (GET_BITMASK(EBT_VLAN_ENCAP)) {
1751 +               if ((unsigned short) ntohs(info->encap) < ETH_ZLEN) {
1752 +                       DEBUG_MSG
1753 +                           ("encap frame length %d is less than minimal\n",
1754 +                            ntohs(info->encap));
1755 +                       return -EINVAL;
1756 +               }
1757 +       }
1758 +
1759 +       return 0;
1760 +}
1761 +
1762 +static struct ebt_match filter_vlan = {
1763 +       {NULL, NULL},
1764 +       EBT_VLAN_MATCH,
1765 +       ebt_filter_vlan,
1766 +       ebt_check_vlan,
1767 +       NULL,
1768 +       THIS_MODULE
1769 +};
1770 +
1771 +/*
1772 + * Module initialization function.
1773 + */
1774 +static int __init init(void)
1775 +{
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);
1780 +}
1781 +
1782 +/*
1783 + * Module "finalization" function
1784 + */
1785 +static void __exit fini(void)
1786 +{
1787 +       ebt_unregister_match(&filter_vlan);
1788 +}
1789 +
1790 +module_init(init);
1791 +module_exit(fini);
1792 +
1793 +EXPORT_NO_SYMBOLS;
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
1796 @@ -0,0 +1,152 @@
1797 +/*
1798 + *  ebt_log
1799 + *
1800 + *     Authors:
1801 + *     Bart De Schuymer <bart.de.schuymer@pandora.be>
1802 + *
1803 + *  April, 2002
1804 + *
1805 + */
1806 +
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>
1814 +
1815 +static spinlock_t ebt_log_lock = SPIN_LOCK_UNLOCKED;
1816 +
1817 +static int ebt_log_check(const char *tablename, unsigned int hookmask,
1818 +   const struct ebt_entry *e, void *data, unsigned int datalen)
1819 +{
1820 +       struct ebt_log_info *info = (struct ebt_log_info *)data;
1821 +
1822 +       if (datalen != EBT_ALIGN(sizeof(struct ebt_log_info)))
1823 +               return -EINVAL;
1824 +       if (info->bitmask & ~EBT_LOG_MASK)
1825 +               return -EINVAL;
1826 +       if (info->loglevel >= 8)
1827 +               return -EINVAL;
1828 +       info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0';
1829 +       return 0;
1830 +}
1831 +
1832 +struct tcpudphdr
1833 +{
1834 +       uint16_t src;
1835 +       uint16_t dst;
1836 +};
1837 +
1838 +struct arppayload
1839 +{
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];
1844 +};
1845 +
1846 +static void print_MAC(unsigned char *p)
1847 +{
1848 +       int i;
1849 +
1850 +       for (i = 0; i < ETH_ALEN; i++, p++)
1851 +               printk("%02x%c", *p, i == ETH_ALEN - 1 ? ' ':':');
1852 +}
1853 +
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)
1857 +{
1858 +       struct ebt_log_info *info = (struct ebt_log_info *)data;
1859 +       char level_string[4] = "< >";
1860 +       level_string[1] = '0' + info->loglevel;
1861 +
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 : "");
1866 +
1867 +       printk("MAC source = ");
1868 +       print_MAC((skb->mac.ethernet)->h_source);
1869 +       printk("MAC dest = ");
1870 +       print_MAC((skb->mac.ethernet)->h_dest);
1871 +
1872 +       printk("proto = 0x%04x", ntohs(((*skb).mac.ethernet)->h_proto));
1873 +
1874 +       if ((info->bitmask & EBT_LOG_IP) && skb->mac.ethernet->h_proto ==
1875 +          htons(ETH_P_IP)){
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);
1883 +
1884 +                       if (skb->data + iph->ihl*4 > skb->tail) {
1885 +                               printk(" INCOMPLETE TCP/UDP header");
1886 +                               goto out;
1887 +                       }
1888 +                       printk(" SPT=%u DPT=%u", ntohs(ports->src),
1889 +                          ntohs(ports->dst));
1890 +               }
1891 +               goto out;
1892 +       }
1893 +
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));
1907 +
1908 +                       if (skb->data + sizeof(*arph) > skb->tail) {
1909 +                               printk(" INCOMPLETE ARP header");
1910 +                               goto out;
1911 +                       }
1912 +
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));
1921 +               }
1922 +
1923 +       }
1924 +out:
1925 +       printk("\n");
1926 +       spin_unlock_bh(&ebt_log_lock);
1927 +}
1928 +
1929 +static struct ebt_watcher log =
1930 +{
1931 +       {NULL, NULL}, EBT_LOG_WATCHER, ebt_log, ebt_log_check, NULL,
1932 +       THIS_MODULE
1933 +};
1934 +
1935 +static int __init init(void)
1936 +{
1937 +       return ebt_register_watcher(&log);
1938 +}
1939 +
1940 +static void __exit fini(void)
1941 +{
1942 +       ebt_unregister_watcher(&log);
1943 +}
1944 +
1945 +module_init(init);
1946 +module_exit(fini);
1947 +EXPORT_NO_SYMBOLS;
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
1951 @@ -0,0 +1,64 @@
1952 +/*
1953 + *  ebt_snat
1954 + *
1955 + *     Authors:
1956 + *     Bart De Schuymer <bart.de.schuymer@pandora.be>
1957 + *
1958 + *  June, 2002
1959 + *
1960 + */
1961 +
1962 +#include <linux/netfilter_bridge/ebtables.h>
1963 +#include <linux/netfilter_bridge/ebt_nat.h>
1964 +#include <linux/module.h>
1965 +
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)
1969 +{
1970 +       struct ebt_nat_info *info = (struct ebt_nat_info *) data;
1971 +
1972 +       memcpy(((**pskb).mac.ethernet)->h_source, info->mac,
1973 +          ETH_ALEN * sizeof(unsigned char));
1974 +       return info->target;
1975 +}
1976 +
1977 +static int ebt_target_snat_check(const char *tablename, unsigned int hookmask,
1978 +   const struct ebt_entry *e, void *data, unsigned int datalen)
1979 +{
1980 +       struct ebt_nat_info *info = (struct ebt_nat_info *) data;
1981 +
1982 +       if (datalen != EBT_ALIGN(sizeof(struct ebt_nat_info)))
1983 +               return -EINVAL;
1984 +       if (BASE_CHAIN && info->target == EBT_RETURN)
1985 +               return -EINVAL;
1986 +       CLEAR_BASE_CHAIN_BIT;
1987 +       if (strcmp(tablename, "nat"))
1988 +               return -EINVAL;
1989 +       if (hookmask & ~(1 << NF_BR_POST_ROUTING))
1990 +               return -EINVAL;
1991 +       if (INVALID_TARGET)
1992 +               return -EINVAL;
1993 +       return 0;
1994 +}
1995 +
1996 +static struct ebt_target snat =
1997 +{
1998 +       {NULL, NULL}, EBT_SNAT_TARGET, ebt_target_snat, ebt_target_snat_check,
1999 +       NULL, THIS_MODULE
2000 +};
2001 +
2002 +static int __init init(void)
2003 +{
2004 +       return ebt_register_target(&snat);
2005 +}
2006 +
2007 +static void __exit fini(void)
2008 +{
2009 +       ebt_unregister_target(&snat);
2010 +}
2011 +
2012 +module_init(init);
2013 +module_exit(fini);
2014 +EXPORT_NO_SYMBOLS;
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
2018 @@ -0,0 +1,65 @@
2019 +/*
2020 + *  ebt_dnat
2021 + *
2022 + *     Authors:
2023 + *     Bart De Schuymer <bart.de.schuymer@pandora.be>
2024 + *
2025 + *  June, 2002
2026 + *
2027 + */
2028 +
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>
2033 +
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)
2037 +{
2038 +       struct ebt_nat_info *info = (struct ebt_nat_info *)data;
2039 +
2040 +       memcpy(((**pskb).mac.ethernet)->h_dest, info->mac,
2041 +          ETH_ALEN * sizeof(unsigned char));
2042 +       return info->target;
2043 +}
2044 +
2045 +static int ebt_target_dnat_check(const char *tablename, unsigned int hookmask,
2046 +   const struct ebt_entry *e, void *data, unsigned int datalen)
2047 +{
2048 +       struct ebt_nat_info *info = (struct ebt_nat_info *)data;
2049 +
2050 +       if (BASE_CHAIN && info->target == EBT_RETURN)
2051 +               return -EINVAL;
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)) )
2056 +               return -EINVAL;
2057 +       if (datalen != EBT_ALIGN(sizeof(struct ebt_nat_info)))
2058 +               return -EINVAL;
2059 +       if (INVALID_TARGET)
2060 +               return -EINVAL;
2061 +       return 0;
2062 +}
2063 +
2064 +static struct ebt_target dnat =
2065 +{
2066 +       {NULL, NULL}, EBT_DNAT_TARGET, ebt_target_dnat, ebt_target_dnat_check,
2067 +       NULL, THIS_MODULE
2068 +};
2069 +
2070 +static int __init init(void)
2071 +{
2072 +       return ebt_register_target(&dnat);
2073 +}
2074 +
2075 +static void __exit fini(void)
2076 +{
2077 +       ebt_unregister_target(&dnat);
2078 +}
2079 +
2080 +module_init(init);
2081 +module_exit(fini);
2082 +EXPORT_NO_SYMBOLS;
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
2086 @@ -0,0 +1,1490 @@
2087 +/*
2088 + *  ebtables
2089 + *
2090 + *  Author:
2091 + *  Bart De Schuymer           <bart.de.schuymer@pandora.be>
2092 + *
2093 + *  ebtables.c,v 2.0, July, 2002
2094 + *
2095 + *  This code is stongly inspired on the iptables code which is
2096 + *  Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
2097 + *
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.
2102 + */
2103 +
2104 +// used for print_string
2105 +#include <linux/sched.h>
2106 +#include <linux/tty.h>
2107 +
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"
2118 +
2119 +// list_named_find
2120 +#define ASSERT_READ_LOCK(x)
2121 +#define ASSERT_WRITE_LOCK(x)
2122 +#include <linux/netfilter_ipv4/listhelp.h>
2123 +
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)
2129 +{
2130 +       struct tty_struct *my_tty;
2131 +
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);
2137 +       }
2138 +}
2139 +
2140 +#define BUGPRINT(args) print_string(args);
2141 +#else
2142 +#define BUGPRINT(format, args...) printk("kernel msg: ebtables bug: please "\
2143 +                                         "report to author: "format, ## args)
2144 +// #define BUGPRINT(format, args...)
2145 +#endif
2146 +#define MEMPRINT(format, args...) printk("kernel msg: ebtables "\
2147 +                                         ": out of memory: "format, ## args)
2148 +// #define MEMPRINT(format, args...)
2149 +
2150 +
2151 +
2152 +// Each cpu has its own set of counters, so there is no need for write_lock in
2153 +// the softirq
2154 +// For reading or updating the counters, the user context needs to
2155 +// get a write_lock
2156 +
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))
2162 +
2163 +
2164 +
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);
2170 +
2171 +static struct ebt_target ebt_standard_target =
2172 +{ {NULL, NULL}, EBT_STANDARD_TARGET, NULL, NULL, NULL, NULL};
2173 +
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)
2177 +{
2178 +       w->u.watcher->watcher(skb, in, out, w->data,
2179 +          w->watcher_size);
2180 +       // watchers don't give a verdict
2181 +       return 0;
2182 +}
2183 +
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)
2187 +{
2188 +       return m->u.match->match(skb, in, out, m->data,
2189 +          m->match_size);
2190 +}
2191 +
2192 +static inline int ebt_dev_check(char *entry, const struct net_device *device)
2193 +{
2194 +       if (*entry == '\0')
2195 +               return 0;
2196 +       if (!device)
2197 +               return 1;
2198 +       return !!strcmp(entry, device->name);
2199 +}
2200 +
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)
2205 +{
2206 +       int verdict, i;
2207 +
2208 +       if (e->bitmask & EBT_802_3) {
2209 +               if (FWINV2(ntohs(h->h_proto) >= 1536, EBT_IPROTO))
2210 +                       return 1;
2211 +       } else if (!(e->bitmask & EBT_NOPROTO) &&
2212 +          FWINV2(e->ethproto != h->h_proto, EBT_IPROTO))
2213 +               return 1;
2214 +
2215 +       if (FWINV2(ebt_dev_check(e->in, in), EBT_IIN))
2216 +               return 1;
2217 +       if (FWINV2(ebt_dev_check(e->out, out), EBT_IOUT))
2218 +               return 1;
2219 +       if ((!in || !in->br_port) ? 0 : FWINV2(ebt_dev_check(
2220 +          e->logical_in, &in->br_port->br->dev), EBT_ILOGICALIN))
2221 +               return 1;
2222 +       if ((!out || !out->br_port) ? 0 : FWINV2(ebt_dev_check(
2223 +          e->logical_out, &out->br_port->br->dev), EBT_ILOGICALOUT))
2224 +               return 1;
2225 +
2226 +       if (e->bitmask & EBT_SOURCEMAC) {
2227 +               verdict = 0;
2228 +               for (i = 0; i < 6; i++)
2229 +                       verdict |= (h->h_source[i] ^ e->sourcemac[i]) &
2230 +                          e->sourcemsk[i];
2231 +               if (FWINV2(verdict != 0, EBT_ISOURCE) )
2232 +                       return 1;
2233 +       }
2234 +       if (e->bitmask & EBT_DESTMAC) {
2235 +               verdict = 0;
2236 +               for (i = 0; i < 6; i++)
2237 +                       verdict |= (h->h_dest[i] ^ e->destmac[i]) &
2238 +                          e->destmsk[i];
2239 +               if (FWINV2(verdict != 0, EBT_IDEST) )
2240 +                       return 1;
2241 +       }
2242 +       return 0;
2243 +}
2244 +
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)
2249 +{
2250 +       int i, nentries;
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;
2257 +       char *base;
2258 +       struct ebt_table_info *private = table->private;
2259 +
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())];
2265 +       else
2266 +               cs = NULL;
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;
2273 +       i = 0;
2274 +       while (i < nentries) {
2275 +               if (ebt_basic_match(point, (**pskb).mac.ethernet, in, out))
2276 +                       goto letscontinue;
2277 +
2278 +               if (EBT_MATCH_ITERATE(point, ebt_do_match, *pskb, in, out) != 0)
2279 +                       goto letscontinue;
2280 +
2281 +               // increase counter
2282 +               (*(counter_base + i)).pcnt++;
2283 +               (*(counter_base + i)).bcnt+=(**pskb).len;
2284 +
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,
2288 +                  out);
2289 +
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;
2295 +               else
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);
2300 +                       return NF_ACCEPT;
2301 +               }
2302 +               if (verdict == EBT_DROP) {
2303 +                       read_unlock_bh(&table->lock);
2304 +                       return NF_DROP;
2305 +               }
2306 +               if (verdict == EBT_RETURN) {
2307 +letsreturn:
2308 +#ifdef CONFIG_NETFILTER_DEBUG
2309 +                       if (sp == 0) {
2310 +                               BUGPRINT("RETURN on base chain");
2311 +                               // act like this is EBT_CONTINUE
2312 +                               goto letscontinue;
2313 +                       }
2314 +#endif
2315 +                       sp--;
2316 +                       // put all the local variables right
2317 +                       i = cs[sp].n;
2318 +                       chaininfo = cs[sp].chaininfo;
2319 +                       nentries = chaininfo->nentries;
2320 +                       point = cs[sp].e;
2321 +                       counter_base = cb_base +
2322 +                          chaininfo->counter_offset;
2323 +                       continue;
2324 +               }
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);
2331 +                       return NF_DROP;
2332 +               }
2333 +#endif
2334 +               // jump to a udc
2335 +               cs[sp].n = i + 1;
2336 +               cs[sp].chaininfo = chaininfo;
2337 +               cs[sp].e = (struct ebt_entry *)
2338 +                  (((char *)point) + point->next_offset);
2339 +               i = 0;
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);
2345 +                       return NF_DROP;
2346 +               }
2347 +#endif
2348 +               nentries = chaininfo->nentries;
2349 +               point = (struct ebt_entry *)chaininfo->data;
2350 +               counter_base = cb_base + chaininfo->counter_offset;
2351 +               sp++;
2352 +               continue;
2353 +letscontinue:
2354 +               point = (struct ebt_entry *)
2355 +                  (((char *)point) + point->next_offset);
2356 +               i++;
2357 +       }
2358 +
2359 +       // I actually like this :)
2360 +       if (chaininfo->policy == EBT_RETURN)
2361 +               goto letsreturn;
2362 +       if (chaininfo->policy == EBT_ACCEPT) {
2363 +               read_unlock_bh(&table->lock);
2364 +               return NF_ACCEPT;
2365 +       }
2366 +       read_unlock_bh(&table->lock);
2367 +       return NF_DROP;
2368 +}
2369 +
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)
2374 +{
2375 +       void *ret;
2376 +
2377 +       *error = down_interruptible(mutex);
2378 +       if (*error != 0)
2379 +               return NULL;
2380 +
2381 +       ret = list_named_find(head, name);
2382 +       if (!ret) {
2383 +               *error = -ENOENT;
2384 +               up(mutex);
2385 +       }
2386 +       return ret;
2387 +}
2388 +
2389 +#ifndef CONFIG_KMOD
2390 +#define find_inlist_lock(h,n,p,e,m) find_inlist_lock_noload((h),(n),(e),(m))
2391 +#else
2392 +static void *
2393 +find_inlist_lock(struct list_head *head, const char *name, const char *prefix,
2394 +   int *error, struct semaphore *mutex)
2395 +{
2396 +       void *ret;
2397 +
2398 +       ret = find_inlist_lock_noload(head, name, error, mutex);
2399 +       if (!ret) {
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);
2405 +       }
2406 +       return ret;
2407 +}
2408 +#endif
2409 +
2410 +static inline struct ebt_table *
2411 +find_table_lock(const char *name, int *error, struct semaphore *mutex)
2412 +{
2413 +       return find_inlist_lock(&ebt_tables, name, "ebtable_", error, mutex);
2414 +}
2415 +
2416 +static inline struct ebt_match *
2417 +find_match_lock(const char *name, int *error, struct semaphore *mutex)
2418 +{
2419 +       return find_inlist_lock(&ebt_matches, name, "ebt_", error, mutex);
2420 +}
2421 +
2422 +static inline struct ebt_watcher *
2423 +find_watcher_lock(const char *name, int *error, struct semaphore *mutex)
2424 +{
2425 +       return find_inlist_lock(&ebt_watchers, name, "ebt_", error, mutex);
2426 +}
2427 +
2428 +static inline struct ebt_target *
2429 +find_target_lock(const char *name, int *error, struct semaphore *mutex)
2430 +{
2431 +       return find_inlist_lock(&ebt_targets, name, "ebt_", error, mutex);
2432 +}
2433 +
2434 +static inline int
2435 +ebt_check_match(struct ebt_entry_match *m, struct ebt_entry *e,
2436 +   const char *name, unsigned int hookmask, unsigned int *cnt)
2437 +{
2438 +       struct ebt_match *match;
2439 +       int ret;
2440 +
2441 +       if (((char *)m) + m->match_size + sizeof(struct ebt_entry_match) >
2442 +          ((char *)e) + e->watchers_offset)
2443 +               return -EINVAL;
2444 +       match = find_match_lock(m->u.name, &ret, &ebt_mutex);
2445 +       if (!match)
2446 +               return ret;
2447 +       m->u.match = match;
2448 +       if (match->me)
2449 +               __MOD_INC_USE_COUNT(match->me);
2450 +       up(&ebt_mutex);
2451 +       if (match->check &&
2452 +          match->check(name, hookmask, e, m->data, m->match_size) != 0) {
2453 +               BUGPRINT("match->check failed\n");
2454 +               if (match->me)
2455 +                       __MOD_DEC_USE_COUNT(match->me);
2456 +               return -EINVAL;
2457 +       }
2458 +       (*cnt)++;
2459 +       return 0;
2460 +}
2461 +
2462 +static inline int
2463 +ebt_check_watcher(struct ebt_entry_watcher *w, struct ebt_entry *e,
2464 +   const char *name, unsigned int hookmask, unsigned int *cnt)
2465 +{
2466 +       struct ebt_watcher *watcher;
2467 +       int ret;
2468 +
2469 +       if (((char *)w) + w->watcher_size + sizeof(struct ebt_entry_watcher) >
2470 +          ((char *)e) + e->target_offset)
2471 +               return -EINVAL;
2472 +       watcher = find_watcher_lock(w->u.name, &ret, &ebt_mutex);
2473 +       if (!watcher)
2474 +               return ret;
2475 +       w->u.watcher = watcher;
2476 +       if (watcher->me)
2477 +               __MOD_INC_USE_COUNT(watcher->me);
2478 +       up(&ebt_mutex);
2479 +       if (watcher->check &&
2480 +          watcher->check(name, hookmask, e, w->data, w->watcher_size) != 0) {
2481 +               BUGPRINT("watcher->check failed\n");
2482 +               if (watcher->me)
2483 +                       __MOD_DEC_USE_COUNT(watcher->me);
2484 +               return -EINVAL;
2485 +       }
2486 +       (*cnt)++;
2487 +       return 0;
2488 +}
2489 +
2490 +// this one is very careful, as it is the first function
2491 +// to parse the userspace data
2492 +static inline int
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)
2497 +{
2498 +       int i;
2499 +
2500 +       for (i = 0; i < NF_BR_NUMHOOKS; i++) {
2501 +               if ((valid_hooks & (1 << i)) == 0)
2502 +                       continue;
2503 +               if ( (char *)hook_entries[i] - base ==
2504 +                  (char *)e - newinfo->entries)
2505 +                       break;
2506 +       }
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");
2515 +                       return -EINVAL;
2516 +               }
2517 +               // this checks if the previous chain has as many entries
2518 +               // as it said it has
2519 +               if (*n != *cnt) {
2520 +                       BUGPRINT("nentries does not equal the nr of entries "
2521 +                                "in the chain\n");
2522 +                       return -EINVAL;
2523 +               }
2524 +               // before we look at the struct, be sure it is not too big
2525 +               if ((char *)hook_entries[i] + sizeof(struct ebt_entries)
2526 +                  > limit) {
2527 +                       BUGPRINT("entries_size too small\n");
2528 +                       return -EINVAL;
2529 +               }
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");
2536 +                               return -EINVAL;
2537 +                       }
2538 +               }
2539 +               if (i == NF_BR_NUMHOOKS) // it's a user defined chain
2540 +                       (*udc_cnt)++;
2541 +               else
2542 +                       newinfo->hook_entry[i] = (struct ebt_entries *)e;
2543 +               if (((struct ebt_entries *)e)->counter_offset != *totalcnt) {
2544 +                       BUGPRINT("counter_offset != totalcnt");
2545 +                       return -EINVAL;
2546 +               }
2547 +               *n = ((struct ebt_entries *)e)->nentries;
2548 +               *cnt = 0;
2549 +               return 0;
2550 +       }
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");
2556 +               return -EINVAL;
2557 +       }
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");
2561 +               return -EINVAL;
2562 +       }
2563 +
2564 +       (*cnt)++;
2565 +       (*totalcnt)++;
2566 +       return 0;
2567 +}
2568 +
2569 +struct ebt_cl_stack
2570 +{
2571 +       struct ebt_chainstack cs;
2572 +       int from;
2573 +       unsigned int hookmask;
2574 +};
2575 +
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.
2578 +static inline int
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)
2582 +{
2583 +       int i;
2584 +
2585 +       // we're only interested in chain starts
2586 +       if (e->bitmask & EBT_ENTRY_OR_ENTRIES)
2587 +               return 0;
2588 +       for (i = 0; i < NF_BR_NUMHOOKS; i++) {
2589 +               if ((valid_hooks & (1 << i)) == 0)
2590 +                       continue;
2591 +               if (newinfo->hook_entry[i] == (struct ebt_entries *)e)
2592 +                       break;
2593 +       }
2594 +       // only care about udc
2595 +       if (i != NF_BR_NUMHOOKS)
2596 +               return 0;
2597 +
2598 +       udc[*n].cs.chaininfo = (struct ebt_entries *)e;
2599 +       // these initialisations are depended on later in check_chainloops()
2600 +       udc[*n].cs.n = 0;
2601 +       udc[*n].hookmask = 0;
2602 +
2603 +       (*n)++;
2604 +       return 0;
2605 +}
2606 +
2607 +static inline int
2608 +ebt_cleanup_match(struct ebt_entry_match *m, unsigned int *i)
2609 +{
2610 +       if (i && (*i)-- == 0)
2611 +               return 1;
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);
2616 +
2617 +       return 0;
2618 +}
2619 +
2620 +static inline int
2621 +ebt_cleanup_watcher(struct ebt_entry_watcher *w, unsigned int *i)
2622 +{
2623 +       if (i && (*i)-- == 0)
2624 +               return 1;
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);
2629 +
2630 +       return 0;
2631 +}
2632 +
2633 +static inline int
2634 +ebt_cleanup_entry(struct ebt_entry *e, unsigned int *cnt)
2635 +{
2636 +       struct ebt_entry_target *t;
2637 +
2638 +       if ((e->bitmask & EBT_ENTRY_OR_ENTRIES) == 0)
2639 +               return 0;
2640 +       // we're done
2641 +       if (cnt && (*cnt)-- == 0)
2642 +               return 1;
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);
2650 +
2651 +       return 0;
2652 +}
2653 +
2654 +static inline int
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)
2658 +{
2659 +       struct ebt_entry_target *t;
2660 +       struct ebt_target *target;
2661 +       unsigned int i, j, hook = 0, hookmask = 0;
2662 +       int ret;
2663 +
2664 +       // Don't mess with the struct ebt_entries
2665 +       if ((e->bitmask & EBT_ENTRY_OR_ENTRIES) == 0)
2666 +               return 0;
2667 +
2668 +       if (e->bitmask & ~EBT_F_MASK) {
2669 +               BUGPRINT("Unknown flag for bitmask\n");
2670 +               return -EINVAL;
2671 +       }
2672 +       if (e->invflags & ~EBT_INV_MASK) {
2673 +               BUGPRINT("Unknown flag for inv bitmask\n");
2674 +               return -EINVAL;
2675 +       }
2676 +       if ( (e->bitmask & EBT_NOPROTO) && (e->bitmask & EBT_802_3) ) {
2677 +               BUGPRINT("NOPROTO & 802_3 not allowed\n");
2678 +               return -EINVAL;
2679 +       }
2680 +       // what hook do we belong to?
2681 +       for (i = 0; i < NF_BR_NUMHOOKS; i++) {
2682 +               if ((valid_hooks & (1 << i)) == 0)
2683 +                       continue;
2684 +               if ((char *)newinfo->hook_entry[i] < (char *)e)
2685 +                       hook = i;
2686 +               else
2687 +                       break;
2688 +       }
2689 +       // (1 << NF_BR_NUMHOOKS) tells the check functions the rule is on
2690 +       // a base chain
2691 +       if (i < NF_BR_NUMHOOKS)
2692 +               hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
2693 +       else {
2694 +               for (i = 0; i < udc_cnt; i++)
2695 +                       if ((char *)(cl_s[i].cs.chaininfo) > (char *)e)
2696 +                               break;
2697 +               if (i == 0)
2698 +                       hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
2699 +               else
2700 +                       hookmask = cl_s[i - 1].hookmask;
2701 +       }
2702 +       i = 0;
2703 +       ret = EBT_MATCH_ITERATE(e, ebt_check_match, e, name, hookmask, &i);
2704 +       if (ret != 0)
2705 +               goto cleanup_matches;
2706 +       j = 0;
2707 +       ret = EBT_WATCHER_ITERATE(e, ebt_check_watcher, e, name, hookmask, &j);
2708 +       if (ret != 0)
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);
2712 +       if (!target)
2713 +               goto cleanup_watchers;
2714 +       if (target->me)
2715 +               __MOD_INC_USE_COUNT(target->me);
2716 +       up(&ebt_mutex);
2717 +
2718 +       t->u.target = target;
2719 +       if (t->u.target == &ebt_standard_target) {
2720 +               if (e->target_offset + sizeof(struct ebt_standard_target) >
2721 +                  e->next_offset) {
2722 +                       BUGPRINT("Standard target size too big\n");
2723 +                       ret = -EFAULT;
2724 +                       goto cleanup_watchers;
2725 +               }
2726 +               if (((struct ebt_standard_target *)t)->verdict <
2727 +                  -NUM_STANDARD_TARGETS) {
2728 +                       BUGPRINT("Invalid standard target\n");
2729 +                       ret = -EFAULT;
2730 +                       goto cleanup_watchers;
2731 +               }
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);
2738 +               ret = -EFAULT;
2739 +               goto cleanup_watchers;
2740 +       }
2741 +       (*cnt)++;
2742 +       return 0;
2743 +cleanup_watchers:
2744 +       EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, &j);
2745 +cleanup_matches:
2746 +       EBT_MATCH_ITERATE(e, ebt_cleanup_match, &i);
2747 +       return ret;
2748 +}
2749 +
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)
2756 +{
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;
2760 +
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)
2767 +                               nentries =
2768 +                               cl_s[cl_s[chain_nr].from].cs.chaininfo->nentries;
2769 +                       else
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)
2776 +                               continue;
2777 +               }
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) >
2783 +                  e->next_offset) {
2784 +                       BUGPRINT("Standard target size too big\n");
2785 +                       return -1;
2786 +               }
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)
2793 +                                       break;
2794 +                       // bad destination or loop
2795 +                       if (i == udc_cnt) {
2796 +                               BUGPRINT("bad destination\n");
2797 +                               return -1;
2798 +                       }
2799 +                       if (cl_s[i].cs.n) {
2800 +                               BUGPRINT("loop\n");
2801 +                               return -1;
2802 +                       }
2803 +                       // this can't be 0, so the above test is correct
2804 +                       cl_s[i].cs.n = pos + 1;
2805 +                       pos = 0;
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;
2810 +                       chain_nr = i;
2811 +                       // this udc is accessible from the base chain for hooknr
2812 +                       cl_s[i].hookmask |= (1 << hooknr);
2813 +                       continue;
2814 +               }
2815 +letscontinue:
2816 +               e = (void *)e + e->next_offset;
2817 +               pos++;
2818 +       }
2819 +       return 0;
2820 +}
2821 +
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)
2825 +{
2826 +       unsigned int i, j, k, udc_cnt;
2827 +       int ret;
2828 +       struct ebt_cl_stack *cl_s = NULL; // used in the checking for chain loops
2829 +
2830 +       i = 0;
2831 +       while (i < NF_BR_NUMHOOKS && !(repl->valid_hooks & (1 << i)))
2832 +               i++;
2833 +       if (i == NF_BR_NUMHOOKS) {
2834 +               BUGPRINT("No valid hooks specified\n");
2835 +               return -EINVAL;
2836 +       }
2837 +       if (repl->hook_entry[i] != (struct ebt_entries *)repl->entries) {
2838 +               BUGPRINT("Chains don't start at beginning\n");
2839 +               return -EINVAL;
2840 +       }
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)))
2845 +                       continue;
2846 +               if ( repl->hook_entry[j] <= repl->hook_entry[i] ) {
2847 +                       BUGPRINT("Hook order must be followed\n");
2848 +                       return -EINVAL;
2849 +               }
2850 +               i = j;
2851 +       }
2852 +
2853 +       for (i = 0; i < NF_BR_NUMHOOKS; i++)
2854 +               newinfo->hook_entry[i] = NULL;
2855 +
2856 +       newinfo->entries_size = repl->entries_size;
2857 +       newinfo->nentries = repl->nentries;
2858 +
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);
2869 +
2870 +       if (ret != 0)
2871 +               return ret;
2872 +
2873 +       if (i != j) {
2874 +               BUGPRINT("nentries does not equal the nr of entries in the "
2875 +                        "(last) chain\n");
2876 +               return -EINVAL;
2877 +       }
2878 +       if (k != newinfo->nentries) {
2879 +               BUGPRINT("Total nentries is wrong\n");
2880 +               return -EINVAL;
2881 +       }
2882 +
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");
2888 +                       return -EINVAL;
2889 +               }
2890 +       }
2891 +
2892 +       // Get the location of the udc, put them in an array
2893 +       // While we're at it, allocate the chainstack
2894 +       if (udc_cnt) {
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)
2900 +                       return -ENOMEM;
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]) {
2905 +                               while (i)
2906 +                                       vfree(newinfo->chainstack[--i]);
2907 +                               vfree(newinfo->chainstack);
2908 +                               newinfo->chainstack = NULL;
2909 +                               return -ENOMEM;
2910 +                       }
2911 +               }
2912 +
2913 +               cl_s = (struct ebt_cl_stack *)
2914 +                  vmalloc(udc_cnt * sizeof(struct ebt_cl_stack));
2915 +               if (!cl_s)
2916 +                       return -ENOMEM;
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);
2921 +               // sanity check
2922 +               if (i != udc_cnt) {
2923 +                       BUGPRINT("i != udc_cnt\n");
2924 +                       vfree(cl_s);
2925 +                       return -EFAULT;
2926 +               }
2927 +       }
2928 +
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)) {
2934 +                               if (cl_s)
2935 +                                       vfree(cl_s);
2936 +                               return -EINVAL;
2937 +                       }
2938 +
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.
2948 +
2949 +       // used to know what we need to clean up if something goes wrong
2950 +       i = 0;
2951 +       ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
2952 +          ebt_check_entry, newinfo, repl->name, &i, repl->valid_hooks,
2953 +          cl_s, udc_cnt);
2954 +       if (ret != 0) {
2955 +               EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
2956 +                  ebt_cleanup_entry, &i);
2957 +       }
2958 +       if (cl_s)
2959 +               vfree(cl_s);
2960 +       return ret;
2961 +}
2962 +
2963 +// called under write_lock
2964 +static void get_counters(struct ebt_counter *oldcounters,
2965 +   struct ebt_counter *counters, unsigned int nentries)
2966 +{
2967 +       int i, cpu;
2968 +       struct ebt_counter *counter_base;
2969 +
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;
2979 +               }
2980 +       }
2981 +}
2982 +
2983 +// replace the table
2984 +static int do_replace(void *user, unsigned int len)
2985 +{
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;
2993 +
2994 +       if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
2995 +               return -EFAULT;
2996 +
2997 +       if (len != sizeof(tmp) + tmp.entries_size) {
2998 +               BUGPRINT("Wrong len argument\n");
2999 +               return -EINVAL;
3000 +       }
3001 +
3002 +       if (tmp.entries_size == 0) {
3003 +               BUGPRINT("Entries_size never zero\n");
3004 +               return -EINVAL;
3005 +       }
3006 +       countersize = COUNTER_OFFSET(tmp.nentries) * smp_num_cpus;
3007 +       newinfo = (struct ebt_table_info *)
3008 +          vmalloc(sizeof(struct ebt_table_info) + countersize);
3009 +       if (!newinfo)
3010 +               return -ENOMEM;
3011 +
3012 +       if (countersize)
3013 +               memset(newinfo->counters, 0, countersize);
3014 +
3015 +       newinfo->entries = (char *)vmalloc(tmp.entries_size);
3016 +       if (!newinfo->entries) {
3017 +               ret = -ENOMEM;
3018 +               goto free_newinfo;
3019 +       }
3020 +       if (copy_from_user(
3021 +          newinfo->entries, tmp.entries, tmp.entries_size) != 0) {
3022 +               BUGPRINT("Couldn't copy entries from userspace\n");
3023 +               ret = -EFAULT;
3024 +               goto free_entries;
3025 +       }
3026 +
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) {
3033 +                       ret = -ENOMEM;
3034 +                       goto free_entries;
3035 +               }
3036 +       }
3037 +       else
3038 +               counterstmp = NULL;
3039 +
3040 +       // this can get initialized by translate_table()
3041 +       newinfo->chainstack = NULL;
3042 +       ret = translate_table(&tmp, newinfo);
3043 +
3044 +       if (ret != 0)
3045 +               goto free_counterstmp;
3046 +
3047 +       t = find_table_lock(tmp.name, &ret, &ebt_mutex);
3048 +       if (!t)
3049 +               goto free_iterate;
3050 +
3051 +       // the table doesn't like it
3052 +       if (t->check && (ret = t->check(newinfo, tmp.valid_hooks)))
3053 +               goto free_unlock;
3054 +
3055 +       if (tmp.num_counters && tmp.num_counters != t->private->nentries) {
3056 +               BUGPRINT("Wrong nr. of counters requested\n");
3057 +               ret = -EINVAL;
3058 +               goto free_unlock;
3059 +       }
3060 +
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);
3068 +
3069 +       t->private = newinfo;
3070 +       write_unlock_bh(&t->lock);
3071 +       up(&ebt_mutex);
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");
3080 +               ret = -EFAULT;
3081 +       }
3082 +       else
3083 +               ret = 0;
3084 +
3085 +       // decrease module count and free resources
3086 +       EBT_ENTRY_ITERATE(table->entries, table->entries_size,
3087 +          ebt_cleanup_entry, NULL);
3088 +
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);
3094 +       }
3095 +       vfree(table);
3096 +
3097 +       if (counterstmp)
3098 +               vfree(counterstmp);
3099 +       return ret;
3100 +
3101 +free_unlock:
3102 +       up(&ebt_mutex);
3103 +free_iterate:
3104 +       EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
3105 +          ebt_cleanup_entry, NULL);
3106 +free_counterstmp:
3107 +       if (counterstmp)
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);
3114 +       }
3115 +free_entries:
3116 +       if (newinfo->entries)
3117 +               vfree(newinfo->entries);
3118 +free_newinfo:
3119 +       if (newinfo)
3120 +               vfree(newinfo);
3121 +       return ret;
3122 +}
3123 +
3124 +int ebt_register_target(struct ebt_target *target)
3125 +{
3126 +       int ret;
3127 +
3128 +       ret = down_interruptible(&ebt_mutex);
3129 +       if (ret != 0)
3130 +               return ret;
3131 +       if (!list_named_insert(&ebt_targets, target)) {
3132 +               up(&ebt_mutex);
3133 +               return -EEXIST;
3134 +       }
3135 +       up(&ebt_mutex);
3136 +       MOD_INC_USE_COUNT;
3137 +
3138 +       return 0;
3139 +}
3140 +
3141 +void ebt_unregister_target(struct ebt_target *target)
3142 +{
3143 +       down(&ebt_mutex);
3144 +       LIST_DELETE(&ebt_targets, target);
3145 +       up(&ebt_mutex);
3146 +       MOD_DEC_USE_COUNT;
3147 +}
3148 +
3149 +int ebt_register_match(struct ebt_match *match)
3150 +{
3151 +       int ret;
3152 +
3153 +       ret = down_interruptible(&ebt_mutex);
3154 +       if (ret != 0)
3155 +               return ret;
3156 +       if (!list_named_insert(&ebt_matches, match)) {
3157 +               up(&ebt_mutex);
3158 +               return -EEXIST;
3159 +       }
3160 +       up(&ebt_mutex);
3161 +       MOD_INC_USE_COUNT;
3162 +
3163 +       return 0;
3164 +}
3165 +
3166 +void ebt_unregister_match(struct ebt_match *match)
3167 +{
3168 +       down(&ebt_mutex);
3169 +       LIST_DELETE(&ebt_matches, match);
3170 +       up(&ebt_mutex);
3171 +       MOD_DEC_USE_COUNT;
3172 +}
3173 +
3174 +int ebt_register_watcher(struct ebt_watcher *watcher)
3175 +{
3176 +       int ret;
3177 +
3178 +       ret = down_interruptible(&ebt_mutex);
3179 +       if (ret != 0)
3180 +               return ret;
3181 +       if (!list_named_insert(&ebt_watchers, watcher)) {
3182 +               up(&ebt_mutex);
3183 +               return -EEXIST;
3184 +       }
3185 +       up(&ebt_mutex);
3186 +       MOD_INC_USE_COUNT;
3187 +
3188 +       return 0;
3189 +}
3190 +
3191 +void ebt_unregister_watcher(struct ebt_watcher *watcher)
3192 +{
3193 +       down(&ebt_mutex);
3194 +       LIST_DELETE(&ebt_watchers, watcher);
3195 +       up(&ebt_mutex);
3196 +       MOD_DEC_USE_COUNT;
3197 +}
3198 +
3199 +int ebt_register_table(struct ebt_table *table)
3200 +{
3201 +       struct ebt_table_info *newinfo;
3202 +       int ret, i, countersize;
3203 +
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");
3208 +               return -EINVAL;
3209 +       }
3210 +
3211 +       countersize = COUNTER_OFFSET(table->table->nentries) * smp_num_cpus;
3212 +       newinfo = (struct ebt_table_info *)
3213 +          vmalloc(sizeof(struct ebt_table_info) + countersize);
3214 +       ret = -ENOMEM;
3215 +       if (!newinfo)
3216 +               return -ENOMEM;
3217 +
3218 +       newinfo->entries = (char *)vmalloc(table->table->entries_size);
3219 +       if (!(newinfo->entries))
3220 +               goto free_newinfo;
3221 +
3222 +       memcpy(newinfo->entries, table->table->entries,
3223 +          table->table->entries_size);
3224 +
3225 +       if (countersize)
3226 +               memset(newinfo->counters, 0, countersize);
3227 +
3228 +       // fill in newinfo and parse the entries
3229 +       newinfo->chainstack = NULL;
3230 +       ret = translate_table(table->table, newinfo);
3231 +       if (ret != 0) {
3232 +               BUGPRINT("Translate_table failed\n");
3233 +               goto free_chainstack;
3234 +       }
3235 +
3236 +       if (table->check && table->check(newinfo, table->valid_hooks)) {
3237 +               BUGPRINT("The table doesn't like its own initial data, lol\n");
3238 +               return -EINVAL;
3239 +       }
3240 +
3241 +       table->private = newinfo;
3242 +       table->lock = RW_LOCK_UNLOCKED;
3243 +       ret = down_interruptible(&ebt_mutex);
3244 +       if (ret != 0)
3245 +               goto free_chainstack;
3246 +
3247 +       if (list_named_find(&ebt_tables, table->name)) {
3248 +               ret = -EEXIST;
3249 +               BUGPRINT("Table name already exists\n");
3250 +               goto free_unlock;
3251 +       }
3252 +
3253 +       list_prepend(&ebt_tables, table);
3254 +       up(&ebt_mutex);
3255 +       MOD_INC_USE_COUNT;
3256 +       return 0;
3257 +free_unlock:
3258 +       up(&ebt_mutex);
3259 +free_chainstack:
3260 +       if (newinfo->chainstack) {
3261 +               for (i = 0; i < smp_num_cpus; i++)
3262 +                       vfree(newinfo->chainstack[i]);
3263 +               vfree(newinfo->chainstack);
3264 +       }
3265 +       vfree(newinfo->entries);
3266 +free_newinfo:
3267 +       vfree(newinfo);
3268 +       return ret;
3269 +}
3270 +
3271 +void ebt_unregister_table(struct ebt_table *table)
3272 +{
3273 +       int i;
3274 +
3275 +       if (!table) {
3276 +               BUGPRINT("Request to unregister NULL table!!!\n");
3277 +               return;
3278 +       }
3279 +       down(&ebt_mutex);
3280 +       LIST_DELETE(&ebt_tables, table);
3281 +       up(&ebt_mutex);
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);
3290 +       }
3291 +       vfree(table->private);
3292 +       MOD_DEC_USE_COUNT;
3293 +}
3294 +
3295 +// userspace just supplied us with counters
3296 +static int update_counters(void *user, unsigned int len)
3297 +{
3298 +       int i, ret;
3299 +       struct ebt_counter *tmp;
3300 +       struct ebt_replace hlp;
3301 +       struct ebt_table *t;
3302 +
3303 +       if (copy_from_user(&hlp, user, sizeof(hlp)))
3304 +               return -EFAULT;
3305 +
3306 +       if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter))
3307 +               return -EINVAL;
3308 +       if (hlp.num_counters == 0)
3309 +               return -EINVAL;
3310 +
3311 +       if ( !(tmp = (struct ebt_counter *)
3312 +          vmalloc(hlp.num_counters * sizeof(struct ebt_counter))) ){
3313 +               MEMPRINT("Update_counters && nomemory\n");
3314 +               return -ENOMEM;
3315 +       }
3316 +
3317 +       t = find_table_lock(hlp.name, &ret, &ebt_mutex);
3318 +       if (!t)
3319 +               goto free_tmp;
3320 +
3321 +       if (hlp.num_counters != t->private->nentries) {
3322 +               BUGPRINT("Wrong nr of counters\n");
3323 +               ret = -EINVAL;
3324 +               goto unlock_mutex;
3325 +       }
3326 +
3327 +       if ( copy_from_user(tmp, hlp.counters,
3328 +          hlp.num_counters * sizeof(struct ebt_counter)) ) {
3329 +               BUGPRINT("Updata_counters && !cfu\n");
3330 +               ret = -EFAULT;
3331 +               goto unlock_mutex;
3332 +       }
3333 +
3334 +       // we want an atomic add of the counters
3335 +       write_lock_bh(&t->lock);
3336 +
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;
3341 +       }
3342 +
3343 +       write_unlock_bh(&t->lock);
3344 +       ret = 0;
3345 +unlock_mutex:
3346 +       up(&ebt_mutex);
3347 +free_tmp:
3348 +       vfree(tmp);
3349 +       return ret;
3350 +}
3351 +
3352 +static inline int ebt_make_matchname(struct ebt_entry_match *m,
3353 +   char *base, char *ubase)
3354 +{
3355 +       char *hlp = ubase - base + (char *)m;
3356 +       if (copy_to_user(hlp, m->u.match->name, EBT_FUNCTION_MAXNAMELEN))
3357 +               return -EFAULT;
3358 +       return 0;
3359 +}
3360 +
3361 +static inline int ebt_make_watchername(struct ebt_entry_watcher *w,
3362 +   char *base, char *ubase)
3363 +{
3364 +       char *hlp = ubase - base + (char *)w;
3365 +       if (copy_to_user(hlp , w->u.watcher->name, EBT_FUNCTION_MAXNAMELEN))
3366 +               return -EFAULT;
3367 +       return 0;
3368 +}
3369 +
3370 +static inline int ebt_make_names(struct ebt_entry *e, char *base, char *ubase)
3371 +{
3372 +       int ret;
3373 +       char *hlp;
3374 +       struct ebt_entry_target *t;
3375 +
3376 +       if ((e->bitmask & EBT_ENTRY_OR_ENTRIES) == 0)
3377 +               return 0;
3378 +
3379 +       hlp = ubase - base + (char *)e + e->target_offset;
3380 +       t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
3381 +       
3382 +       ret = EBT_MATCH_ITERATE(e, ebt_make_matchname, base, ubase);
3383 +       if (ret != 0)
3384 +               return ret;
3385 +       ret = EBT_WATCHER_ITERATE(e, ebt_make_watchername, base, ubase);
3386 +       if (ret != 0)
3387 +               return ret;
3388 +       if (copy_to_user(hlp, t->u.target->name, EBT_FUNCTION_MAXNAMELEN))
3389 +               return -EFAULT;
3390 +       return 0;
3391 +}
3392 +
3393 +// called with ebt_mutex down
3394 +static int copy_everything_to_user(struct ebt_table *t, void *user,
3395 +   int *len, int cmd)
3396 +{
3397 +       struct ebt_replace tmp;
3398 +       struct ebt_counter *counterstmp, *oldcounters;
3399 +       unsigned int entries_size, nentries;
3400 +       char *entries;
3401 +
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;
3407 +       } else {
3408 +               entries_size = t->table->entries_size;
3409 +               nentries = t->table->nentries;
3410 +               entries = t->table->entries;
3411 +               oldcounters = t->table->counters;
3412 +       }
3413 +
3414 +       if (copy_from_user(&tmp, user, sizeof(tmp))) {
3415 +               BUGPRINT("Cfu didn't work\n");
3416 +               return -EFAULT;
3417 +       }
3418 +
3419 +       if (*len != sizeof(struct ebt_replace) + entries_size +
3420 +          (tmp.num_counters? nentries * sizeof(struct ebt_counter): 0)) {
3421 +               BUGPRINT("Wrong size\n");
3422 +               return -EINVAL;
3423 +       }
3424 +
3425 +       if (tmp.nentries != nentries) {
3426 +               BUGPRINT("Nentries wrong\n");
3427 +               return -EINVAL;
3428 +       }
3429 +
3430 +       if (tmp.entries_size != entries_size) {
3431 +               BUGPRINT("Wrong size\n");
3432 +               return -EINVAL;
3433 +       }
3434 +
3435 +       // userspace might not need the counters
3436 +       if (tmp.num_counters) {
3437 +               if (tmp.num_counters != nentries) {
3438 +                       BUGPRINT("Num_counters wrong\n");
3439 +                       return -EINVAL;
3440 +               }
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");
3445 +                       return -ENOMEM;
3446 +               }
3447 +               write_lock_bh(&t->lock);
3448 +               get_counters(oldcounters, counterstmp, nentries);
3449 +               write_unlock_bh(&t->lock);
3450 +
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);
3455 +                       return -EFAULT;
3456 +               }
3457 +               vfree(counterstmp);
3458 +       }
3459 +
3460 +       if (copy_to_user(tmp.entries, entries, entries_size)) {
3461 +               BUGPRINT("Couldn't copy entries to userspace\n");
3462 +               return -EFAULT;
3463 +       }
3464 +       // set the match/watcher/target names right
3465 +       return EBT_ENTRY_ITERATE(entries, entries_size,
3466 +          ebt_make_names, entries, tmp.entries);
3467 +}
3468 +
3469 +static int do_ebt_set_ctl(struct sock *sk,
3470 +       int cmd, void *user, unsigned int len)
3471 +{
3472 +       int ret;
3473 +
3474 +       switch(cmd) {
3475 +       case EBT_SO_SET_ENTRIES:
3476 +               ret = do_replace(user, len);
3477 +               break;
3478 +       case EBT_SO_SET_COUNTERS:
3479 +               ret = update_counters(user, len);
3480 +               break;
3481 +       default:
3482 +               ret = -EINVAL;
3483 +  }
3484 +       return ret;
3485 +}
3486 +
3487 +static int do_ebt_get_ctl(struct sock *sk, int cmd, void *user, int *len)
3488 +{
3489 +       int ret;
3490 +       struct ebt_replace tmp;
3491 +       struct ebt_table *t;
3492 +
3493 +       if (copy_from_user(&tmp, user, sizeof(tmp)))
3494 +               return -EFAULT;
3495 +
3496 +       t = find_table_lock(tmp.name, &ret, &ebt_mutex);
3497 +       if (!t)
3498 +               return ret;
3499 +
3500 +       switch(cmd) {
3501 +       case EBT_SO_GET_INFO:
3502 +       case EBT_SO_GET_INIT_INFO:
3503 +               if (*len != sizeof(struct ebt_replace)){
3504 +                       ret = -EINVAL;
3505 +                       up(&ebt_mutex);
3506 +                       break;
3507 +               }
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;
3512 +               } else {
3513 +                       tmp.nentries = t->table->nentries;
3514 +                       tmp.entries_size = t->table->entries_size;
3515 +                       tmp.valid_hooks = t->table->valid_hooks;
3516 +               }
3517 +               up(&ebt_mutex);
3518 +               if (copy_to_user(user, &tmp, *len) != 0){
3519 +                       BUGPRINT("c2u Didn't work\n");
3520 +                       ret = -EFAULT;
3521 +                       break;
3522 +               }
3523 +               ret = 0;
3524 +               break;
3525 +
3526 +       case EBT_SO_GET_ENTRIES:
3527 +       case EBT_SO_GET_INIT_ENTRIES:
3528 +               ret = copy_everything_to_user(t, user, len, cmd);
3529 +               up(&ebt_mutex);
3530 +               break;
3531 +
3532 +       default:
3533 +               up(&ebt_mutex);
3534 +               ret = -EINVAL;
3535 +       }
3536 +
3537 +       return ret;
3538 +}
3539 +
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
3543 +};
3544 +
3545 +static int __init init(void)
3546 +{
3547 +       int ret;
3548 +
3549 +       down(&ebt_mutex);
3550 +       list_named_insert(&ebt_targets, &ebt_standard_target);
3551 +       up(&ebt_mutex);
3552 +       if ((ret = nf_register_sockopt(&ebt_sockopts)) < 0)
3553 +               return ret;
3554 +
3555 +       printk(KERN_NOTICE "Ebtables v2.0 registered\n");
3556 +       return 0;
3557 +}
3558 +
3559 +static void __exit fini(void)
3560 +{
3561 +       nf_unregister_sockopt(&ebt_sockopts);
3562 +       printk(KERN_NOTICE "Ebtables v2.0 unregistered\n");
3563 +}
3564 +
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);
3574 +module_init(init);
3575 +module_exit(fini);
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
3579 @@ -0,0 +1,361 @@
3580 +/*
3581 + *  ebtables
3582 + *
3583 + *     Authors:
3584 + *     Bart De Schuymer                <bart.de.schuymer@pandora.be>
3585 + *
3586 + *  ebtables.c,v 2.0, September, 2002
3587 + *
3588 + *  This code is stongly inspired on the iptables code which is
3589 + *  Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
3590 + */
3591 +
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>
3597 +
3598 +#define EBT_TABLE_MAXNAMELEN 32
3599 +#define EBT_CHAIN_MAXNAMELEN EBT_TABLE_MAXNAMELEN
3600 +#define EBT_FUNCTION_MAXNAMELEN EBT_TABLE_MAXNAMELEN
3601 +
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
3608 +
3609 +struct ebt_replace
3610 +{
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;
3623 +       char *entries;
3624 +};
3625 +
3626 +struct ebt_counter
3627 +{
3628 +       uint64_t pcnt;
3629 +       uint64_t bcnt;
3630 +};
3631 +
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;
3637 +       // the chain name
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
3642 +       int policy;
3643 +       // nr. of entries
3644 +       unsigned int nentries;
3645 +       // entry list
3646 +       char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
3647 +};
3648 +
3649 +// used for the bitmask of struct ebt_entry
3650 +
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
3654 +// ebt_entries.
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)
3666 +
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)
3676 +
3677 +struct ebt_entry_match
3678 +{
3679 +       union {
3680 +               char name[EBT_FUNCTION_MAXNAMELEN];
3681 +               struct ebt_match *match;
3682 +       } u;
3683 +       // size of data
3684 +       unsigned int match_size;
3685 +       unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
3686 +};
3687 +
3688 +struct ebt_entry_watcher
3689 +{
3690 +       union {
3691 +               char name[EBT_FUNCTION_MAXNAMELEN];
3692 +               struct ebt_watcher *watcher;
3693 +       } u;
3694 +       // size of data
3695 +       unsigned int watcher_size;
3696 +       unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
3697 +};
3698 +
3699 +struct ebt_entry_target
3700 +{
3701 +       union {
3702 +               char name[EBT_FUNCTION_MAXNAMELEN];
3703 +               struct ebt_target *target;
3704 +       } u;
3705 +       // size of data
3706 +       unsigned int target_size;
3707 +       unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
3708 +};
3709 +
3710 +#define EBT_STANDARD_TARGET "standard"
3711 +struct ebt_standard_target
3712 +{
3713 +       struct ebt_entry_target target;
3714 +       int verdict;
3715 +};
3716 +
3717 +// one entry
3718 +struct ebt_entry {
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))));
3742 +};
3743 +
3744 +// [gs]etsockopt numbers
3745 +#define EBT_BASE_CTL            128
3746 +
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)
3750 +
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)
3756 +
3757 +#ifdef __KERNEL__
3758 +
3759 +// return values for match() functions
3760 +#define EBT_MATCH 0
3761 +#define EBT_NOMATCH 1
3762 +
3763 +struct ebt_match
3764 +{
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);
3771 +       // 0 == let it in
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;
3776 +};
3777 +
3778 +struct ebt_watcher
3779 +{
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);
3785 +       // 0 == let it in
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;
3790 +};
3791 +
3792 +struct ebt_target
3793 +{
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);
3800 +       // 0 == let it in
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;
3805 +};
3806 +
3807 +// used for jumping from and into user defined chains (udc)
3808 +struct ebt_chainstack
3809 +{
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
3813 +};
3814 +
3815 +struct ebt_table_info
3816 +{
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;
3824 +       char *entries;
3825 +       struct ebt_counter counters[0] ____cacheline_aligned;
3826 +};
3827 +
3828 +struct ebt_table
3829 +{
3830 +       struct list_head list;
3831 +       char name[EBT_TABLE_MAXNAMELEN];
3832 +       struct ebt_replace *table;
3833 +       unsigned int valid_hooks;
3834 +       rwlock_t lock;
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;
3841 +};
3842 +
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);
3856 +
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)
3866 +
3867 +#endif /* __KERNEL__ */
3868 +
3869 +// blatently stolen from ip_tables.h
3870 +// fn returns 0 to continue iteration
3871 +#define EBT_MATCH_ITERATE(e, fn, args...)                   \
3872 +({                                                          \
3873 +       unsigned int __i;                                   \
3874 +       int __ret = 0;                                      \
3875 +       struct ebt_entry_match *__match;                    \
3876 +                                                           \
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;                \
3882 +                                                           \
3883 +               __ret = fn(__match , ## args);              \
3884 +               if (__ret != 0)                             \
3885 +                       break;                              \
3886 +       }                                                   \
3887 +       if (__ret == 0) {                                   \
3888 +               if (__i != (e)->watchers_offset)            \
3889 +                       __ret = -EINVAL;                    \
3890 +       }                                                   \
3891 +       __ret;                                              \
3892 +})
3893 +
3894 +#define EBT_WATCHER_ITERATE(e, fn, args...)                 \
3895 +({                                                          \
3896 +       unsigned int __i;                                   \
3897 +       int __ret = 0;                                      \
3898 +       struct ebt_entry_watcher *__watcher;                \
3899 +                                                           \
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;              \
3905 +                                                           \
3906 +               __ret = fn(__watcher , ## args);            \
3907 +               if (__ret != 0)                             \
3908 +                       break;                              \
3909 +       }                                                   \
3910 +       if (__ret == 0) {                                   \
3911 +               if (__i != (e)->target_offset)              \
3912 +                       __ret = -EINVAL;                    \
3913 +       }                                                   \
3914 +       __ret;                                              \
3915 +})
3916 +
3917 +#define EBT_ENTRY_ITERATE(entries, size, fn, args...)       \
3918 +({                                                          \
3919 +       unsigned int __i;                                   \
3920 +       int __ret = 0;                                      \
3921 +       struct ebt_entry *__entry;                          \
3922 +                                                           \
3923 +       for (__i = 0; __i < (size);) {                      \
3924 +               __entry = (void *)(entries) + __i;          \
3925 +               __ret = fn(__entry , ## args);              \
3926 +               if (__ret != 0)                             \
3927 +                       break;                              \
3928 +               if (__entry->bitmask != 0)                  \
3929 +                       __i += __entry->next_offset;        \
3930 +               else                                        \
3931 +                       __i += sizeof(struct ebt_entries);  \
3932 +       }                                                   \
3933 +       if (__ret == 0) {                                   \
3934 +               if (__i != (size))                          \
3935 +                       __ret = -EINVAL;                    \
3936 +       }                                                   \
3937 +       __ret;                                              \
3938 +})
3939 +
3940 +#endif
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
3943 @@ -0,0 +1,60 @@
3944 +#ifndef __LINUX_BRIDGE_EBT_802_3_H
3945 +#define __LINUX_BRIDGE_EBT_802_3_H
3946 +
3947 +#define EBT_802_3_SAP 0x01
3948 +#define EBT_802_3_TYPE 0x02
3949 +
3950 +#define EBT_802_3_MATCH "802_3"
3951 +
3952 +/*
3953 + * If frame has DSAP/SSAP value 0xaa you must check the SNAP type
3954 + * to discover what kind of packet we're carrying. 
3955 + */
3956 +#define CHECK_TYPE 0xaa
3957 +
3958 +/*
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.
3963 + */
3964 +#define IS_UI 0x03
3965 +
3966 +#define EBT_802_3_MASK (EBT_802_3_SAP | EBT_802_3_TYPE | EBT_802_3)
3967 +
3968 +/* ui has one byte ctrl, ni has two */
3969 +struct hdr_ui {
3970 +       uint8_t dsap;
3971 +       uint8_t ssap;
3972 +       uint8_t ctrl;
3973 +       uint8_t orig[3];
3974 +       uint16_t type;
3975 +};
3976 +
3977 +struct hdr_ni {
3978 +       uint8_t dsap;
3979 +       uint8_t ssap;
3980 +       uint16_t ctrl;
3981 +       uint8_t  orig[3];
3982 +       uint16_t type;
3983 +};
3984 +
3985 +struct ebt_802_3_hdr {
3986 +       uint8_t  daddr[6];
3987 +       uint8_t  saddr[6];
3988 +       uint16_t len;
3989 +       union {
3990 +               struct hdr_ui ui;
3991 +               struct hdr_ni ni;
3992 +       } llc;
3993 +};
3994 +
3995 +struct ebt_802_3_info 
3996 +{
3997 +       uint8_t  sap;
3998 +       uint16_t type;
3999 +       uint8_t  bitmask;
4000 +       uint8_t  invflags;
4001 +};
4002 +
4003 +#endif
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
4006 @@ -0,0 +1,32 @@
4007 +#ifndef __LINUX_BRIDGE_EBT_ARP_H
4008 +#define __LINUX_BRIDGE_EBT_ARP_H
4009 +
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"
4020 +
4021 +struct ebt_arp_info
4022 +{
4023 +       uint16_t htype;
4024 +       uint16_t ptype;
4025 +       uint16_t opcode;
4026 +       uint32_t saddr;
4027 +       uint32_t smsk;
4028 +       uint32_t daddr;
4029 +       uint32_t dmsk;
4030 +       unsigned char smaddr[ETH_ALEN];
4031 +       unsigned char smmsk[ETH_ALEN];
4032 +       unsigned char dmaddr[ETH_ALEN];
4033 +       unsigned char dmmsk[ETH_ALEN];
4034 +       uint8_t  bitmask;
4035 +       uint8_t  invflags;
4036 +};
4037 +
4038 +#endif
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
4041 @@ -0,0 +1,43 @@
4042 +/*
4043 + *  ebt_ip
4044 + *
4045 + *     Authors:
4046 + *     Bart De Schuymer <bart.de.schuymer@pandora.be>
4047 + *
4048 + *  April, 2002
4049 + *
4050 + *  Changes:
4051 + *    added ip-sport and ip-dport
4052 + *    Innominate Security Technologies AG <mhopf@innominate.com>
4053 + *    September, 2002
4054 + */
4055 +
4056 +#ifndef __LINUX_BRIDGE_EBT_IP_H
4057 +#define __LINUX_BRIDGE_EBT_IP_H
4058 +
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"
4068 +
4069 +// the same values are used for the invflags
4070 +struct ebt_ip_info
4071 +{
4072 +       uint32_t saddr;
4073 +       uint32_t daddr;
4074 +       uint32_t smsk;
4075 +       uint32_t dmsk;
4076 +       uint8_t  tos;
4077 +       uint8_t  protocol;
4078 +       uint8_t  bitmask;
4079 +       uint8_t  invflags;
4080 +       uint16_t sport[2];
4081 +       uint16_t dport[2];
4082 +};
4083 +
4084 +#endif
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
4087 @@ -0,0 +1,11 @@
4088 +#ifndef __LINUX_BRIDGE_EBT_PKTTYPE_H
4089 +#define __LINUX_BRIDGE_EBT_PKTTYPE_H
4090 +
4091 +struct ebt_pkttype_info
4092 +{
4093 +       uint8_t pkt_type;
4094 +       uint8_t invert;
4095 +};
4096 +#define EBT_PKTTYPE_MATCH "pkttype"
4097 +
4098 +#endif
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
4101 @@ -0,0 +1,46 @@
4102 +#ifndef __LINUX_BRIDGE_EBT_STP_H
4103 +#define __LINUX_BRIDGE_EBT_STP_H
4104 +
4105 +#define EBT_STP_TYPE           0x0001
4106 +
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
4118 +
4119 +#define EBT_STP_MASK           0x0fff
4120 +#define EBT_STP_CONFIG_MASK    0x0ffe
4121 +
4122 +#define EBT_STP_MATCH "stp"
4123 +
4124 +struct ebt_stp_config_info
4125 +{
4126 +       uint8_t flags;
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;
4137 +};
4138 +
4139 +struct ebt_stp_info
4140 +{
4141 +       uint8_t type;
4142 +       struct ebt_stp_config_info config;
4143 +       uint16_t bitmask;
4144 +       uint16_t invflags;
4145 +};
4146 +
4147 +#endif
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
4150 @@ -0,0 +1,20 @@
4151 +#ifndef __LINUX_BRIDGE_EBT_VLAN_H
4152 +#define __LINUX_BRIDGE_EBT_VLAN_H
4153 +
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"
4159 +
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 */
4168 +};
4169 +
4170 +#endif
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
4173 @@ -0,0 +1,17 @@
4174 +#ifndef __LINUX_BRIDGE_EBT_LOG_H
4175 +#define __LINUX_BRIDGE_EBT_LOG_H
4176 +
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"
4182 +
4183 +struct ebt_log_info
4184 +{
4185 +       uint8_t loglevel;
4186 +       uint8_t prefix[EBT_LOG_PREFIX_SIZE];
4187 +       uint32_t bitmask;
4188 +};
4189 +
4190 +#endif
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
4193 @@ -0,0 +1,13 @@
4194 +#ifndef __LINUX_BRIDGE_EBT_NAT_H
4195 +#define __LINUX_BRIDGE_EBT_NAT_H
4196 +
4197 +struct ebt_nat_info
4198 +{
4199 +       unsigned char mac[ETH_ALEN];
4200 +       // EBT_ACCEPT, EBT_DROP, EBT_CONTINUE or EBT_RETURN
4201 +       int target;
4202 +};
4203 +#define EBT_SNAT_TARGET "snat"
4204 +#define EBT_DNAT_TARGET "dnat"
4205 +
4206 +#endif
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
4209 @@ -0,0 +1,11 @@
4210 +#ifndef __LINUX_BRIDGE_EBT_REDIRECT_H
4211 +#define __LINUX_BRIDGE_EBT_REDIRECT_H
4212 +
4213 +struct ebt_redirect_info
4214 +{
4215 +       // EBT_ACCEPT, EBT_DROP or EBT_CONTINUE or EBT_RETURN
4216 +       int target;
4217 +};
4218 +#define EBT_REDIRECT_TARGET "redirect"
4219 +
4220 +#endif
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
4223 @@ -0,0 +1,15 @@
4224 +#ifndef __LINUX_BRIDGE_EBT_MARK_M_H
4225 +#define __LINUX_BRIDGE_EBT_MARK_M_H
4226 +
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
4231 +{
4232 +       unsigned long mark, mask;
4233 +       uint8_t invert;
4234 +       uint8_t bitmask;
4235 +};
4236 +#define EBT_MARK_MATCH "mark_m"
4237 +
4238 +#endif
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
4241 @@ -0,0 +1,12 @@
4242 +#ifndef __LINUX_BRIDGE_EBT_MARK_T_H
4243 +#define __LINUX_BRIDGE_EBT_MARK_T_H
4244 +
4245 +struct ebt_mark_t_info
4246 +{
4247 +       unsigned long mark;
4248 +       // EBT_ACCEPT, EBT_DROP or EBT_CONTINUE or EBT_RETURN
4249 +       int target;
4250 +};
4251 +#define EBT_MARK_TARGET "mark"
4252 +
4253 +#endif
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
4264  #else
4265  #define NF_HOOK(pf, hook, skb, indev, outdev, okfn)                    \
4266  (list_empty(&nf_hooks[(pf)][(hook)])                                   \
4267   ? (okfn)(skb)                                                         \
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)])                                   \
4272 + ? (okfn)(skb)                                                         \
4273 + : nf_hook_slow((pf), (hook), (skb), (indev), (outdev), (okfn), (thresh)))
4274  #endif
4275  
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);
4280  
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,
4284                   int len);
4285  int nf_getsockopt(struct sock *sk, int pf, int optval, char *opt,
4286                   int *len);
4287  
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
4299 @@ -52,8 +52,10 @@
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
4312 @@ -92,6 +92,17 @@
4313  struct nf_ct_info {
4314         struct nf_conntrack *master;
4315  };
4316 +
4317 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4318 +struct nf_bridge_info {
4319 +       atomic_t use;
4320 +       struct net_device *physindev;
4321 +       struct net_device *physoutdev;
4322 +       unsigned int mask;
4323 +       unsigned long hh[16 / sizeof(unsigned long)];
4324 +};
4325 +#endif
4326 +
4327  #endif
4328  
4329  struct sk_buff_head {
4330 @@ -204,6 +215,9 @@
4331  #ifdef CONFIG_NETFILTER_DEBUG
4332          unsigned int nf_debug;
4333  #endif
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 */
4336 +#endif
4337  #endif /*CONFIG_NETFILTER*/
4338  
4339  #if defined(CONFIG_HIPPI)
4340 @@ -277,7 +291,7 @@
4341   * If users==1, we are the only owner and are can avoid redundant
4342   * atomic change.
4343   */
4344
4345 +
4346  /**
4347   *     kfree_skb - free an sk_buff
4348   *     @skb: buffer to free
4349 @@ -285,7 +299,7 @@
4350   *     Drop a reference to the buffer and free it if the usage count has
4351   *     hit zero.
4352   */
4353
4354 +
4355  static inline void kfree_skb(struct sk_buff *skb)
4356  {
4357         if (atomic_read(&skb->users) == 1 || atomic_dec_and_test(&skb->users))
4358 @@ -1144,6 +1158,20 @@
4359         if (nfct)
4360                 atomic_inc(&nfct->master->use);
4361  }
4362 +
4363 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4364 +static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
4365 +{
4366 +       if (nf_bridge && atomic_dec_and_test(&nf_bridge->use))
4367 +               kfree(nf_bridge);
4368 +}
4369 +static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
4370 +{
4371 +       if (nf_bridge)
4372 +               atomic_inc(&nf_bridge->use);
4373 +}
4374 +#endif
4375 +
4376  #endif
4377  
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
4381 @@ -1,4 +1,4 @@
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.
4385   *
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 *),
4393 +                              int hook_thresh)
4394  {
4395         for (*i = (*i)->next; *i != head; *i = (*i)->next) {
4396                 struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;
4397 +
4398 +               if (hook_thresh > elem->priority)
4399 +                       continue;
4400 +
4401                 switch (elem->hook(hook, skb, indev, outdev, okfn)) {
4402                 case NF_QUEUE:
4403                         return NF_QUEUE;
4404 @@ -365,7 +370,7 @@
4405                         break;
4406  
4407                 default:
4408 -                       NFDEBUG("Evil return from %p(%u).\n", 
4409 +                       NFDEBUG("Evil return from %p(%u).\n",
4410                                 elem->hook, hook);
4411  #endif
4412                 }
4413 @@ -374,7 +379,7 @@
4414  }
4415  
4416  int nf_register_queue_handler(int pf, nf_queue_outfn_t outfn, void *data)
4417 -{      
4418 +{
4419         int ret;
4420  
4421         br_write_lock_bh(BR_NETPROTO_LOCK);
4422 @@ -400,12 +405,12 @@
4423         return 0;
4424  }
4425  
4426 -/* 
4427 - * Any packet that leaves via this function must come back 
4428 +/*
4429 + * Any packet that leaves via this function must come back
4430   * through nf_reinject().
4431   */
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 @@
4440  {
4441         int status;
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;
4446 +#endif
4447  
4448         if (!queue_handler[pf].outfn) {
4449                 kfree_skb(skb);
4450 @@ -428,18 +437,31 @@
4451                 return;
4452         }
4453  
4454 -       *info = (struct nf_info) { 
4455 +       *info = (struct nf_info) {
4456                 (struct nf_hook_ops *)elem, pf, hook, indev, outdev, okfn };
4457  
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);
4461  
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);
4468 +       }
4469 +#endif
4470 +
4471         status = queue_handler[pf].outfn(skb, info, queue_handler[pf].data);
4472         if (status < 0) {
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);
4479 +#endif
4480                 kfree(info);
4481                 kfree_skb(skb);
4482                 return;
4483 @@ -449,7 +471,8 @@
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 *),
4489 +                int hook_thresh)
4490  {
4491         struct list_head *elem;
4492         unsigned int verdict;
4493 @@ -481,7 +504,7 @@
4494  
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 @@
4503  
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);
4512 +       }
4513 +#endif
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. */
4517 @@ -528,9 +559,9 @@
4518  
4519         if (verdict == NF_ACCEPT) {
4520                 verdict = nf_iterate(&nf_hooks[info->pf][info->hook],
4521 -                                    &skb, info->hook, 
4522 +                                    &skb, info->hook,
4523                                      info->indev, info->outdev, &elem,
4524 -                                    info->okfn);
4525 +                                    info->okfn, INT_MIN);
4526         }
4527  
4528         switch (verdict) {
4529 @@ -539,20 +570,19 @@
4530                 break;
4531  
4532         case NF_QUEUE:
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);
4536                 break;
4537 -
4538 -       case NF_DROP:
4539 -               kfree_skb(skb);
4540 -               break;
4541         }
4542         br_read_unlock_bh(BR_NETPROTO_LOCK);
4543  
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);
4547 -       
4548 +
4549 +       if (verdict == NF_DROP)
4550 +               kfree_skb(skb);
4551 +
4552         kfree(info);
4553         return;
4554  }
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
4557 @@ -245,6 +245,9 @@
4558  #ifdef CONFIG_NETFILTER_DEBUG
4559         skb->nf_debug = 0;
4560  #endif
4561 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4562 +       skb->nf_bridge    = NULL;
4563 +#endif
4564  #endif
4565  #ifdef CONFIG_NET_SCHED
4566         skb->tc_index = 0;
4567 @@ -325,6 +328,9 @@
4568         }
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);
4573 +#endif
4574  #endif
4575         skb_headerinit(skb, NULL, 0);  /* clean state */
4576         kfree_skbmem(skb);
4577 @@ -391,6 +397,9 @@
4578  #ifdef CONFIG_NETFILTER_DEBUG
4579         C(nf_debug);
4580  #endif
4581 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4582 +       C(nf_bridge);
4583 +#endif
4584  #endif /*CONFIG_NETFILTER*/
4585  #if defined(CONFIG_HIPPI)
4586         C(private);
4587 @@ -403,6 +412,9 @@
4588         skb->cloned = 1;
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);
4593 +#endif
4594  #endif
4595         return n;
4596  }
4597 @@ -437,6 +449,10 @@
4598  #ifdef CONFIG_NETFILTER_DEBUG
4599         new->nf_debug=old->nf_debug;
4600  #endif
4601 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4602 +       new->nf_bridge=old->nf_bridge;
4603 +       nf_bridge_get(new->nf_bridge);
4604 +#endif
4605  #endif
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 @@
4611  static inline int
4612  ip_packet_match(const struct iphdr *ip,
4613                 const char *indev,
4614 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4615 +               const char *physindev,
4616 +#endif
4617                 const char *outdev,
4618 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4619 +               const char *physoutdev,
4620 +#endif
4621                 const struct ipt_ip *ipinfo,
4622                 int isfrag)
4623  {
4624         size_t i;
4625         unsigned long ret;
4626 +       unsigned long ret2 = 1;
4627  
4628  #define FWINV(bool,invflg) ((bool) ^ !!(ipinfo->invflags & invflg))
4629  
4630 @@ -157,7 +164,15 @@
4631                         & ((const unsigned long *)ipinfo->iniface_mask)[i];
4632         }
4633  
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];
4640 +       }
4641 +#endif
4642 +
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];
4649         }
4650  
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];
4657 +       }
4658 +#endif
4659 +
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)":"");
4664 @@ -269,6 +292,9 @@
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;
4670 +#endif
4671         void *table_base;
4672         struct ipt_entry *e, *back;
4673  
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;
4683 +#endif
4684 +
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 @@
4689                 IP_NF_ASSERT(e);
4690                 IP_NF_ASSERT(back);
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)
4695 +                   physindev,
4696 +#endif
4697 +                   outdev,
4698 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4699 +                   physoutdev,
4700 +#endif
4701 +                   &e->ip, offset)) {
4702                         struct ipt_entry_target *t;
4703  
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);
4714 +#endif
4715  #ifdef CONFIG_NETFILTER_DEBUG
4716                 skb2->nf_debug = skb->nf_debug;
4717  #endif
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 @@
4721                loginfo->prefix,
4722                in ? in->name : "",
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;
4728 +
4729 +               if (physindev && in != physindev)
4730 +                       printk("PHYSIN=%s ", physindev->name);
4731 +               if (physoutdev && out != physoutdev)
4732 +                       printk("PHYSOUT=%s ", physoutdev->name);
4733 +       }
4734 +#endif
4735 +
4736         if (in && !out) {
4737                 /* MAC logging for input chain only. */
4738                 printk("MAC=");
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
4741 @@ -76,6 +76,8 @@
4742  obj-$(CONFIG_IP_NF_MATCH_UNCLEAN) += ipt_unclean.o
4743  obj-$(CONFIG_IP_NF_MATCH_TCPMSS) += ipt_tcpmss.o
4744  
4745 +obj-$(CONFIG_IP_NF_MATCH_PHYSDEV) += ipt_physdev.o
4746 +
4747  # targets
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
4752 @@ -41,6 +41,9 @@
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
4755    fi
4756 +  if [ "$CONFIG_BRIDGE" != "n" ]; then
4757 +    dep_tristate '  Physdev match support' CONFIG_IP_NF_MATCH_PHYSDEV
4758 +  fi
4759  # The targets
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
4764 @@ -0,0 +1,636 @@
4765 +/*
4766 + *     Handle firewalling
4767 + *     Linux ethernet bridge
4768 + *
4769 + *     Authors:
4770 + *     Lennert Buytenhek               <buytenh@gnu.org>
4771 + *     Bart De Schuymer                <bdschuym@pandora.be>
4772 + *
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.
4777 + *
4778 + *     Lennert dedicates this file to Kerstin Wurdinger.
4779 + */
4780 +
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"
4794 +
4795 +
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)))
4802 +
4803 +#define has_bridge_parent(device)      ((device)->br_port != NULL)
4804 +#define bridge_parent(device)          (&((device)->br_port->br->dev))
4805 +
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.
4809 + *
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.
4814 + */
4815 +static struct net_device __fake_net_device = {
4816 +       .hard_header_len        = ETH_HLEN
4817 +};
4818 +
4819 +static struct rtable __fake_rtable = {
4820 +       u: {
4821 +               dst: {
4822 +                       __refcnt:               ATOMIC_INIT(1),
4823 +                       dev:                    &__fake_net_device,
4824 +                       pmtu:                   1500
4825 +               }
4826 +       },
4827 +
4828 +       rt_flags:       0
4829 +};
4830 +
4831 +
4832 +/* PF_BRIDGE/PRE_ROUTING *********************************************/
4833 +static void __br_dnat_complain(void)
4834 +{
4835 +       static unsigned long last_complaint = 0;
4836 +
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;
4841 +       }
4842 +}
4843 +
4844 +
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.
4848 + *
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
4852 + *    the packet.
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.
4856 + *
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.
4860 + *
4861 + * Let us first consider the case that ip_route_input() succeeds:
4862 + *
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.
4868 + *
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.
4872 + *
4873 + * Let us now consider the case that ip_route_input() fails:
4874 + *
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.
4881 + *
4882 + * ip_route_input() will also fail if there is no route available.
4883 + * In that case we just drop the packet.
4884 + *
4885 + * --Lennert, 20020411
4886 + * --Bart, 20020416 (updated)
4887 + * --Bart, 20021007 (updated)
4888 + */
4889 +
4890 +static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
4891 +{
4892 +#ifdef CONFIG_NETFILTER_DEBUG
4893 +       skb->nf_debug |= (1 << NF_BR_PRE_ROUTING) | (1 << NF_BR_FORWARD);
4894 +#endif
4895 +
4896 +       if (skb->pkt_type == PACKET_OTHERHOST) {
4897 +               skb->pkt_type = PACKET_HOST;
4898 +               skb->nf_bridge->mask |= BRNF_PKT_TYPE;
4899 +       }
4900 +
4901 +       skb->dev = bridge_parent(skb->dev);
4902 +       skb->dst->output(skb);
4903 +       return 0;
4904 +}
4905 +
4906 +static int br_nf_pre_routing_finish(struct sk_buff *skb)
4907 +{
4908 +       struct net_device *dev = skb->dev;
4909 +       struct iphdr *iph = skb->nh.iph;
4910 +       struct nf_bridge_info *nf_bridge = skb->nf_bridge;
4911 +
4912 +#ifdef CONFIG_NETFILTER_DEBUG
4913 +       skb->nf_debug ^= (1 << NF_BR_PRE_ROUTING);
4914 +#endif
4915 +
4916 +       if (nf_bridge->mask & BRNF_PKT_TYPE) {
4917 +               skb->pkt_type = PACKET_OTHERHOST;
4918 +               nf_bridge->mask ^= BRNF_PKT_TYPE;
4919 +       }
4920 +
4921 +       if (dnat_took_place(skb)) {
4922 +               if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
4923 +                   dev)) {
4924 +                       struct rtable *rt;
4925 +
4926 +                       if (!ip_route_output(&rt, iph->daddr, 0, iph->tos, 0)) {
4927 +                               /* Bridged-and-DNAT'ed traffic doesn't
4928 +                                * require ip_forwarding.
4929 +                                */
4930 +                               if (((struct dst_entry *)rt)->dev == dev) {
4931 +                                       skb->dst = (struct dst_entry *)rt;
4932 +                                       goto bridged_dnat;
4933 +                               }
4934 +                               __br_dnat_complain();
4935 +                               dst_release((struct dst_entry *)rt);
4936 +                       }
4937 +                       kfree_skb(skb);
4938 +                       return 0;
4939 +               } else {
4940 +                       if (skb->dst->dev == dev) {
4941 +bridged_dnat:
4942 +                               /* Tell br_nf_local_out this is a
4943 +                                * bridged frame
4944 +                                */
4945 +                               nf_bridge->mask |= BRNF_BRIDGED_DNAT;
4946 +                               skb->dev = nf_bridge->physindev;
4947 +                               clear_cb(skb);
4948 +                               NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
4949 +                                              skb, skb->dev, NULL,
4950 +                                              br_nf_pre_routing_finish_bridge,
4951 +                                              1);
4952 +                               return 0;
4953 +                       }
4954 +                       memcpy(skb->mac.ethernet->h_dest, dev->dev_addr,
4955 +                              ETH_ALEN);
4956 +               }
4957 +       } else {
4958 +               skb->dst = (struct dst_entry *)&__fake_rtable;
4959 +               dst_hold(skb->dst);
4960 +       }
4961 +
4962 +       clear_cb(skb);
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);
4966 +
4967 +       return 0;
4968 +}
4969 +
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.
4975 + */
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 *))
4979 +{
4980 +       struct iphdr *iph;
4981 +       __u32 len;
4982 +       struct sk_buff *skb;
4983 +       struct nf_bridge_info *nf_bridge;
4984 +
4985 +       if ((*pskb)->protocol != __constant_htons(ETH_P_IP))
4986 +               return NF_ACCEPT;
4987 +
4988 +       if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
4989 +               goto out;
4990 +
4991 +       if (!pskb_may_pull(skb, sizeof(struct iphdr)))
4992 +               goto inhdr_error;
4993 +
4994 +       iph = skb->nh.iph;
4995 +       if (iph->ihl < 5 || iph->version != 4)
4996 +               goto inhdr_error;
4997 +
4998 +       if (!pskb_may_pull(skb, 4*iph->ihl))
4999 +               goto inhdr_error;
5000 +
5001 +       iph = skb->nh.iph;
5002 +       if (ip_fast_csum((__u8 *)iph, iph->ihl) != 0)
5003 +               goto inhdr_error;
5004 +
5005 +       len = ntohs(iph->tot_len);
5006 +       if (skb->len < len || len < 4*iph->ihl)
5007 +               goto inhdr_error;
5008 +
5009 +       if (skb->len > len) {
5010 +               __pskb_trim(skb, len);
5011 +               if (skb->ip_summed == CHECKSUM_HW)
5012 +                       skb->ip_summed = CHECKSUM_NONE;
5013 +       }
5014 +
5015 +#ifdef CONFIG_NETFILTER_DEBUG
5016 +       skb->nf_debug ^= (1 << NF_IP_PRE_ROUTING);
5017 +#endif
5018 +       if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
5019 +               return NF_DROP;
5020 +
5021 +       if (skb->pkt_type == PACKET_OTHERHOST) {
5022 +               skb->pkt_type = PACKET_HOST;
5023 +               nf_bridge->mask |= BRNF_PKT_TYPE;
5024 +       }
5025 +
5026 +       nf_bridge->physindev = skb->dev;
5027 +       skb->dev = bridge_parent(skb->dev);
5028 +       store_orig_dstaddr(skb);
5029 +
5030 +       NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
5031 +               br_nf_pre_routing_finish);
5032 +
5033 +       return NF_STOLEN;
5034 +
5035 +inhdr_error:
5036 +//     IP_INC_STATS_BH(IpInHdrErrors);
5037 +out:
5038 +       return NF_DROP;
5039 +}
5040 +
5041 +
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.
5049 + */
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 *))
5053 +{
5054 +       struct sk_buff *skb = *pskb;
5055 +
5056 +       if (skb->protocol != __constant_htons(ETH_P_IP))
5057 +               return NF_ACCEPT;
5058 +
5059 +       if (skb->dst == (struct dst_entry *)&__fake_rtable) {
5060 +               dst_release(skb->dst);
5061 +               skb->dst = NULL;
5062 +       }
5063 +
5064 +       return NF_ACCEPT;
5065 +}
5066 +
5067 +
5068 +/* PF_BRIDGE/FORWARD *************************************************/
5069 +static int br_nf_forward_finish(struct sk_buff *skb)
5070 +{
5071 +       struct nf_bridge_info *nf_bridge = skb->nf_bridge;
5072 +
5073 +#ifdef CONFIG_NETFILTER_DEBUG
5074 +       skb->nf_debug ^= (1 << NF_BR_FORWARD);
5075 +#endif
5076 +
5077 +       if (nf_bridge->mask & BRNF_PKT_TYPE) {
5078 +               skb->pkt_type = PACKET_OTHERHOST;
5079 +               nf_bridge->mask ^= BRNF_PKT_TYPE;
5080 +       }
5081 +
5082 +       NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, nf_bridge->physindev,
5083 +                       skb->dev, br_forward_finish, 1);
5084 +
5085 +       return 0;
5086 +}
5087 +
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.
5092 + */
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 *))
5096 +{
5097 +       struct sk_buff *skb = *pskb;
5098 +       struct nf_bridge_info *nf_bridge;
5099 +
5100 +       if (skb->protocol != __constant_htons(ETH_P_IP))
5101 +               return NF_ACCEPT;
5102 +
5103 +#ifdef CONFIG_NETFILTER_DEBUG
5104 +       skb->nf_debug ^= (1 << NF_BR_FORWARD);
5105 +#endif
5106 +
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;
5111 +       }
5112 +
5113 +       nf_bridge->mask |= BRNF_BRIDGED; /* The physdev module checks on this */
5114 +       nf_bridge->physoutdev = skb->dev;
5115 +
5116 +       NF_HOOK(PF_INET, NF_IP_FORWARD, skb, bridge_parent(nf_bridge->physindev),
5117 +                       bridge_parent(skb->dev), br_nf_forward_finish);
5118 +
5119 +       return NF_STOLEN;
5120 +}
5121 +
5122 +
5123 +/* PF_BRIDGE/LOCAL_OUT ***********************************************/
5124 +static int br_nf_local_out_finish(struct sk_buff *skb)
5125 +{
5126 +#ifdef CONFIG_NETFILTER_DEBUG
5127 +       skb->nf_debug &= ~(1 << NF_BR_LOCAL_OUT);
5128 +#endif
5129 +
5130 +       NF_HOOK_THRESH(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
5131 +                       br_forward_finish, NF_BR_PRI_FIRST + 1);
5132 +
5133 +       return 0;
5134 +}
5135 +
5136 +
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.
5144 + *
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.
5157 + */
5158 +
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 *))
5162 +{
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;
5167 +
5168 +       if (skb->protocol != __constant_htons(ETH_P_IP))
5169 +               return NF_ACCEPT;
5170 +
5171 +       /* Sometimes we get packets with NULL ->dst here (for example,
5172 +        * running a dhcp client daemon triggers this).
5173 +        */
5174 +       if (skb->dst == NULL)
5175 +               return NF_ACCEPT;
5176 +
5177 +       nf_bridge = skb->nf_bridge;
5178 +       nf_bridge->physoutdev = skb->dev;
5179 +
5180 +       realindev = nf_bridge->physindev;
5181 +
5182 +       /* Bridged, take PF_BRIDGE/FORWARD.
5183 +        * (see big note in front of br_nf_pre_routing_finish)
5184 +        */
5185 +       if (nf_bridge->mask & BRNF_BRIDGED_DNAT) {
5186 +               okfn = br_forward_finish;
5187 +
5188 +               if (nf_bridge->mask & BRNF_PKT_TYPE) {
5189 +                       skb->pkt_type = PACKET_OTHERHOST;
5190 +                       nf_bridge->mask ^= BRNF_PKT_TYPE;
5191 +               }
5192 +
5193 +               NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev,
5194 +                       skb->dev, okfn);
5195 +       } else {
5196 +               okfn = br_nf_local_out_finish;
5197 +               /* IP forwarded traffic has a physindev, locally
5198 +                * generated traffic hasn't.
5199 +                */
5200 +               if (realindev != NULL) {
5201 +                       if (((nf_bridge->mask & BRNF_DONT_TAKE_PARENT) == 0) &&
5202 +                           has_bridge_parent(realindev))
5203 +                               realindev = bridge_parent(realindev);
5204 +
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);
5208 +               } else {
5209 +#ifdef CONFIG_NETFILTER_DEBUG
5210 +                       skb->nf_debug ^= (1 << NF_IP_LOCAL_OUT);
5211 +#endif
5212 +
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);
5216 +               }
5217 +       }
5218 +
5219 +       return NF_STOLEN;
5220 +}
5221 +
5222 +
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 *))
5227 +{
5228 +       struct sk_buff *skb = *pskb;
5229 +       struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
5230 +
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);
5239 +               }
5240 +               printk("\n");
5241 +               return NF_ACCEPT;
5242 +       }
5243 +
5244 +       if (skb->protocol != __constant_htons(ETH_P_IP))
5245 +               return NF_ACCEPT;
5246 +
5247 +       /* Sometimes we get packets with NULL ->dst here (for example,
5248 +        * running a dhcp client daemon triggers this).
5249 +        */
5250 +       if (skb->dst == NULL)
5251 +               return NF_ACCEPT;
5252 +
5253 +#ifdef CONFIG_NETFILTER_DEBUG
5254 +       skb->nf_debug ^= (1 << NF_IP_POST_ROUTING);
5255 +#endif
5256 +
5257 +       /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
5258 +        * about the value of skb->pkt_type.
5259 +        */
5260 +       if (skb->pkt_type == PACKET_OTHERHOST) {
5261 +               skb->pkt_type = PACKET_HOST;
5262 +               nf_bridge->mask |= BRNF_PKT_TYPE;
5263 +       }
5264 +
5265 +       memcpy(nf_bridge->hh, skb->data - 16, 16);
5266 +
5267 +       NF_HOOK(PF_INET, NF_IP_POST_ROUTING, skb, NULL,
5268 +               bridge_parent(skb->dev), br_dev_queue_push_xmit);
5269 +
5270 +       return NF_STOLEN;
5271 +}
5272 +
5273 +
5274 +/* IPv4/SABOTAGE *****************************************************/
5275 +
5276 +/* Don't hand locally destined packets to PF_INET/PRE_ROUTING
5277 + * for the second time.
5278 + */
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 *))
5282 +{
5283 +       if (in->hard_start_xmit == br_dev_xmit &&
5284 +           okfn != br_nf_pre_routing_finish) {
5285 +               okfn(*pskb);
5286 +               return NF_STOLEN;
5287 +       }
5288 +
5289 +       return NF_ACCEPT;
5290 +}
5291 +
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.
5295 + */
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 *))
5299 +{
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;
5306 +
5307 +               if (!skb->nf_bridge && !nf_bridge_alloc(skb))
5308 +                       return NF_DROP;
5309 +
5310 +               nf_bridge = skb->nf_bridge;
5311 +
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.
5317 +                */
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;
5321 +               }
5322 +               okfn(skb);
5323 +               return NF_STOLEN;
5324 +       }
5325 +
5326 +       return NF_ACCEPT;
5327 +}
5328 +
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.
5333 + */
5334 +static struct nf_hook_ops br_nf_ops[] = {
5335 +       { .hook = br_nf_pre_routing,
5336 +         .pf = PF_BRIDGE,
5337 +         .hooknum = NF_BR_PRE_ROUTING,
5338 +         .priority = NF_BR_PRI_BRNF, },
5339 +       { .hook = br_nf_local_in,
5340 +         .pf = PF_BRIDGE,
5341 +         .hooknum = NF_BR_LOCAL_IN,
5342 +         .priority = NF_BR_PRI_BRNF, },
5343 +       { .hook = br_nf_forward,
5344 +         .pf = PF_BRIDGE,
5345 +         .hooknum = NF_BR_FORWARD,
5346 +         .priority = NF_BR_PRI_BRNF, },
5347 +       { .hook = br_nf_local_out,
5348 +         .pf = PF_BRIDGE,
5349 +         .hooknum = NF_BR_LOCAL_OUT,
5350 +         .priority = NF_BR_PRI_FIRST, },
5351 +       { .hook = br_nf_post_routing,
5352 +         .pf = PF_BRIDGE,
5353 +         .hooknum = NF_BR_POST_ROUTING,
5354 +         .priority = NF_BR_PRI_LAST, },
5355 +       { .hook = ipv4_sabotage_in,
5356 +         .pf = PF_INET,
5357 +         .hooknum = NF_IP_PRE_ROUTING,
5358 +         .priority = NF_IP_PRI_FIRST, },
5359 +       { .hook = ipv4_sabotage_out,
5360 +         .pf = PF_INET,
5361 +         .hooknum = NF_IP_FORWARD,
5362 +         .priority = NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD, },
5363 +       { .hook = ipv4_sabotage_out,
5364 +         .pf = PF_INET,
5365 +         .hooknum = NF_IP_LOCAL_OUT,
5366 +         .priority = NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
5367 +       { .hook = ipv4_sabotage_out,
5368 +         .pf = PF_INET,
5369 +         .hooknum = NF_IP_POST_ROUTING,
5370 +         .priority = NF_IP_PRI_FIRST, },
5371 +};
5372 +
5373 +int br_netfilter_init(void)
5374 +{
5375 +       int i;
5376 +
5377 +       for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) {
5378 +               int ret;
5379 +
5380 +               if ((ret = nf_register_hook(&br_nf_ops[i])) >= 0)
5381 +                       continue;
5382 +
5383 +               while (i--)
5384 +                       nf_unregister_hook(&br_nf_ops[i]);
5385 +
5386 +               return ret;
5387 +       }
5388 +
5389 +       printk(KERN_NOTICE "Bridge firewalling registered\n");
5390 +
5391 +       return 0;
5392 +}
5393 +
5394 +void br_netfilter_fini(void)
5395 +{
5396 +       int i;
5397 +
5398 +       for (i = ARRAY_SIZE(br_nf_ops) - 1; i >= 0; i--)
5399 +               nf_unregister_hook(&br_nf_ops[i]);
5400 +}
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
5403 @@ -0,0 +1,127 @@
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>
5412 +#define MATCH   1
5413 +#define NOMATCH 0
5414 +
5415 +static int
5416 +match(const struct sk_buff *skb,
5417 +      const struct net_device *in,
5418 +      const struct net_device *out,
5419 +      const void *matchinfo,
5420 +      int offset,
5421 +      const void *hdr,
5422 +      u_int16_t datalen,
5423 +      int *hotdrop)
5424 +{
5425 +       int i;
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;
5431 +
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))
5439 +                       return NOMATCH;
5440 +               if ((info->bitmask & IPT_PHYSDEV_OP_ISIN) &&
5441 +                   !(info->invert & IPT_PHYSDEV_OP_ISIN))
5442 +                       return NOMATCH;
5443 +               if ((info->bitmask & IPT_PHYSDEV_OP_ISOUT) &&
5444 +                   !(info->invert & IPT_PHYSDEV_OP_ISOUT))
5445 +                       return NOMATCH;
5446 +               if ((info->bitmask & IPT_PHYSDEV_OP_IN) &&
5447 +                   !(info->invert & IPT_PHYSDEV_OP_IN))
5448 +                       return NOMATCH;
5449 +               if ((info->bitmask & IPT_PHYSDEV_OP_OUT) &&
5450 +                   !(info->invert & IPT_PHYSDEV_OP_OUT))
5451 +                       return NOMATCH;
5452 +               return MATCH;
5453 +       }
5454 +
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)))
5459 +               return NOMATCH;
5460 +
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))))
5465 +               return NOMATCH;
5466 +
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];
5474 +       }
5475 +
5476 +       if ((ret == 0) ^ !(info->invert & IPT_PHYSDEV_OP_IN))
5477 +               return NOMATCH;
5478 +
5479 +match_outdev:
5480 +       if (!(info->bitmask & IPT_PHYSDEV_OP_OUT))
5481 +               return MATCH;
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];
5488 +       }
5489 +
5490 +       return (ret != 0) ^ !(info->invert & IPT_PHYSDEV_OP_OUT);
5491 +}
5492 +
5493 +static int
5494 +checkentry(const char *tablename,
5495 +                      const struct ipt_ip *ip,
5496 +                      void *matchinfo,
5497 +                      unsigned int matchsize,
5498 +                      unsigned int hook_mask)
5499 +{
5500 +       const struct ipt_physdev_info *info = matchinfo;
5501 +
5502 +       if (matchsize != IPT_ALIGN(sizeof(struct ipt_physdev_info)))
5503 +               return 0;
5504 +       if (!(info->bitmask & IPT_PHYSDEV_OP_MASK) ||
5505 +           info->bitmask & ~IPT_PHYSDEV_OP_MASK)
5506 +               return 0;
5507 +       return 1;
5508 +}
5509 +
5510 +static struct ipt_match physdev_match = {
5511 +       .name           = "physdev",
5512 +       .match          = &match,
5513 +       .checkentry     = &checkentry,
5514 +       .me             = THIS_MODULE,
5515 +};
5516 +
5517 +static int __init init(void)
5518 +{
5519 +       return ipt_register_match(&physdev_match);
5520 +}
5521 +
5522 +static void __exit fini(void)
5523 +{
5524 +       ipt_unregister_match(&physdev_match);
5525 +}
5526 +
5527 +module_init(init);
5528 +module_exit(fini);
5529 +MODULE_LICENSE("GPL");
5530 +EXPORT_NO_SYMBOLS;
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
5533 @@ -0,0 +1,24 @@
5534 +#ifndef _IPT_PHYSDEV_H
5535 +#define _IPT_PHYSDEV_H
5536 +
5537 +#ifdef __KERNEL__
5538 +#include <linux/if.h>
5539 +#endif
5540 +
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)
5547 +
5548 +struct ipt_physdev_info {
5549 +       u_int8_t invert;
5550 +       u_int8_t bitmask;
5551 +       char physindev[IFNAMSIZ];
5552 +       char in_mask[IFNAMSIZ];
5553 +       char physoutdev[IFNAMSIZ];
5554 +       char out_mask[IFNAMSIZ];
5555 +};
5556 +
5557 +#endif /*_IPT_PHYSDEV_H*/