Merge master.kernel.org:/home/rmk/linux-2.6-serial
[linux-2.6] / net / bridge / br_netfilter.c
1 /*
2  *      Handle firewalling
3  *      Linux ethernet bridge
4  *
5  *      Authors:
6  *      Lennert Buytenhek               <buytenh@gnu.org>
7  *      Bart De Schuymer (maintainer)   <bdschuym@pandora.be>
8  *
9  *      Changes:
10  *      Apr 29 2003: physdev module support (bdschuym)
11  *      Jun 19 2003: let arptables see bridged ARP traffic (bdschuym)
12  *      Oct 06 2003: filter encapsulated IP/ARP VLAN traffic on untagged bridge
13  *                   (bdschuym)
14  *      Sep 01 2004: add IPv6 filtering (bdschuym)
15  *
16  *      This program is free software; you can redistribute it and/or
17  *      modify it under the terms of the GNU General Public License
18  *      as published by the Free Software Foundation; either version
19  *      2 of the License, or (at your option) any later version.
20  *
21  *      Lennert dedicates this file to Kerstin Wurdinger.
22  */
23
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/ip.h>
27 #include <linux/netdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/if_arp.h>
30 #include <linux/if_ether.h>
31 #include <linux/if_vlan.h>
32 #include <linux/netfilter_bridge.h>
33 #include <linux/netfilter_ipv4.h>
34 #include <linux/netfilter_ipv6.h>
35 #include <linux/netfilter_arp.h>
36 #include <linux/in_route.h>
37
38 #include <net/ip.h>
39 #include <net/ipv6.h>
40 #include <net/route.h>
41
42 #include <asm/uaccess.h>
43 #include <asm/checksum.h>
44 #include "br_private.h"
45 #ifdef CONFIG_SYSCTL
46 #include <linux/sysctl.h>
47 #endif
48
49 #define skb_origaddr(skb)        (((struct bridge_skb_cb *) \
50                                  (skb->nf_bridge->data))->daddr.ipv4)
51 #define store_orig_dstaddr(skb)  (skb_origaddr(skb) = (skb)->nh.iph->daddr)
52 #define dnat_took_place(skb)     (skb_origaddr(skb) != (skb)->nh.iph->daddr)
53
54 #ifdef CONFIG_SYSCTL
55 static struct ctl_table_header *brnf_sysctl_header;
56 static int brnf_call_iptables = 1;
57 static int brnf_call_ip6tables = 1;
58 static int brnf_call_arptables = 1;
59 static int brnf_filter_vlan_tagged = 1;
60 #else
61 #define brnf_filter_vlan_tagged 1
62 #endif
63
64 #define IS_VLAN_IP (skb->protocol == __constant_htons(ETH_P_8021Q) &&    \
65         hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IP) &&  \
66         brnf_filter_vlan_tagged)
67 #define IS_VLAN_IPV6 (skb->protocol == __constant_htons(ETH_P_8021Q) &&    \
68         hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IPV6) &&  \
69         brnf_filter_vlan_tagged)
70 #define IS_VLAN_ARP (skb->protocol == __constant_htons(ETH_P_8021Q) &&   \
71         hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_ARP) && \
72         brnf_filter_vlan_tagged)
73
74 /* We need these fake structures to make netfilter happy --
75  * lots of places assume that skb->dst != NULL, which isn't
76  * all that unreasonable.
77  *
78  * Currently, we fill in the PMTU entry because netfilter
79  * refragmentation needs it, and the rt_flags entry because
80  * ipt_REJECT needs it.  Future netfilter modules might
81  * require us to fill additional fields. */
82 static struct net_device __fake_net_device = {
83         .hard_header_len        = ETH_HLEN
84 };
85
86 static struct rtable __fake_rtable = {
87         .u = {
88                 .dst = {
89                         .__refcnt               = ATOMIC_INIT(1),
90                         .dev                    = &__fake_net_device,
91                         .path                   = &__fake_rtable.u.dst,
92                         .metrics                = {[RTAX_MTU - 1] = 1500},
93                         .flags                  = DST_NOXFRM,
94                 }
95         },
96         .rt_flags       = 0,
97 };
98
99 static inline struct net_device *bridge_parent(const struct net_device *dev)
100 {
101         struct net_bridge_port *port = rcu_dereference(dev->br_port);
102
103         return port ? port->br->dev : NULL;
104 }
105
106 /* PF_BRIDGE/PRE_ROUTING *********************************************/
107 /* Undo the changes made for ip6tables PREROUTING and continue the
108  * bridge PRE_ROUTING hook. */
109 static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
110 {
111         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
112
113         if (nf_bridge->mask & BRNF_PKT_TYPE) {
114                 skb->pkt_type = PACKET_OTHERHOST;
115                 nf_bridge->mask ^= BRNF_PKT_TYPE;
116         }
117         nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
118
119         skb->dst = (struct dst_entry *)&__fake_rtable;
120         dst_hold(skb->dst);
121
122         skb->dev = nf_bridge->physindev;
123         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
124                 skb_push(skb, VLAN_HLEN);
125                 skb->nh.raw -= VLAN_HLEN;
126         }
127         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
128                        br_handle_frame_finish, 1);
129
130         return 0;
131 }
132
133 static void __br_dnat_complain(void)
134 {
135         static unsigned long last_complaint;
136
137         if (jiffies - last_complaint >= 5 * HZ) {
138                 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
139                         "forwarding to be enabled\n");
140                 last_complaint = jiffies;
141         }
142 }
143
144 /* This requires some explaining. If DNAT has taken place,
145  * we will need to fix up the destination Ethernet address,
146  * and this is a tricky process.
147  *
148  * There are two cases to consider:
149  * 1. The packet was DNAT'ed to a device in the same bridge
150  *    port group as it was received on. We can still bridge
151  *    the packet.
152  * 2. The packet was DNAT'ed to a different device, either
153  *    a non-bridged device or another bridge port group.
154  *    The packet will need to be routed.
155  *
156  * The correct way of distinguishing between these two cases is to
157  * call ip_route_input() and to look at skb->dst->dev, which is
158  * changed to the destination device if ip_route_input() succeeds.
159  *
160  * Let us first consider the case that ip_route_input() succeeds:
161  *
162  * If skb->dst->dev equals the logical bridge device the packet
163  * came in on, we can consider this bridging. We then call
164  * skb->dst->output() which will make the packet enter br_nf_local_out()
165  * not much later. In that function it is assured that the iptables
166  * FORWARD chain is traversed for the packet.
167  *
168  * Otherwise, the packet is considered to be routed and we just
169  * change the destination MAC address so that the packet will
170  * later be passed up to the IP stack to be routed.
171  *
172  * Let us now consider the case that ip_route_input() fails:
173  *
174  * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
175  * will fail, while __ip_route_output_key() will return success. The source
176  * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
177  * thinks we're handling a locally generated packet and won't care
178  * if IP forwarding is allowed. We send a warning message to the users's
179  * log telling her to put IP forwarding on.
180  *
181  * ip_route_input() will also fail if there is no route available.
182  * In that case we just drop the packet.
183  *
184  * --Lennert, 20020411
185  * --Bart, 20020416 (updated)
186  * --Bart, 20021007 (updated) */
187 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
188 {
189         if (skb->pkt_type == PACKET_OTHERHOST) {
190                 skb->pkt_type = PACKET_HOST;
191                 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
192         }
193         skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
194
195         skb->dev = bridge_parent(skb->dev);
196         if (!skb->dev)
197                 kfree_skb(skb);
198         else {
199                 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
200                         skb_pull(skb, VLAN_HLEN);
201                         skb->nh.raw += VLAN_HLEN;
202                 }
203                 skb->dst->output(skb);
204         }
205         return 0;
206 }
207
208 static int br_nf_pre_routing_finish(struct sk_buff *skb)
209 {
210         struct net_device *dev = skb->dev;
211         struct iphdr *iph = skb->nh.iph;
212         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
213
214         if (nf_bridge->mask & BRNF_PKT_TYPE) {
215                 skb->pkt_type = PACKET_OTHERHOST;
216                 nf_bridge->mask ^= BRNF_PKT_TYPE;
217         }
218         nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
219
220         if (dnat_took_place(skb)) {
221                 if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
222                     dev)) {
223                         struct rtable *rt;
224                         struct flowi fl = { .nl_u = 
225                         { .ip4_u = { .daddr = iph->daddr, .saddr = 0 ,
226                                      .tos = RT_TOS(iph->tos)} }, .proto = 0};
227
228                         if (!ip_route_output_key(&rt, &fl)) {
229                                 /* - Bridged-and-DNAT'ed traffic doesn't
230                                  *   require ip_forwarding.
231                                  * - Deal with redirected traffic. */
232                                 if (((struct dst_entry *)rt)->dev == dev ||
233                                     rt->rt_type == RTN_LOCAL) {
234                                         skb->dst = (struct dst_entry *)rt;
235                                         goto bridged_dnat;
236                                 }
237                                 __br_dnat_complain();
238                                 dst_release((struct dst_entry *)rt);
239                         }
240                         kfree_skb(skb);
241                         return 0;
242                 } else {
243                         if (skb->dst->dev == dev) {
244 bridged_dnat:
245                                 /* Tell br_nf_local_out this is a
246                                  * bridged frame */
247                                 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
248                                 skb->dev = nf_bridge->physindev;
249                                 if (skb->protocol ==
250                                     __constant_htons(ETH_P_8021Q)) {
251                                         skb_push(skb, VLAN_HLEN);
252                                         skb->nh.raw -= VLAN_HLEN;
253                                 }
254                                 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
255                                                skb, skb->dev, NULL,
256                                                br_nf_pre_routing_finish_bridge,
257                                                1);
258                                 return 0;
259                         }
260                         memcpy(eth_hdr(skb)->h_dest, dev->dev_addr,
261                                ETH_ALEN);
262                         skb->pkt_type = PACKET_HOST;
263                 }
264         } else {
265                 skb->dst = (struct dst_entry *)&__fake_rtable;
266                 dst_hold(skb->dst);
267         }
268
269         skb->dev = nf_bridge->physindev;
270         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
271                 skb_push(skb, VLAN_HLEN);
272                 skb->nh.raw -= VLAN_HLEN;
273         }
274         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
275                        br_handle_frame_finish, 1);
276
277         return 0;
278 }
279
280 /* Some common code for IPv4/IPv6 */
281 static struct net_device *setup_pre_routing(struct sk_buff *skb)
282 {
283         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
284
285         if (skb->pkt_type == PACKET_OTHERHOST) {
286                 skb->pkt_type = PACKET_HOST;
287                 nf_bridge->mask |= BRNF_PKT_TYPE;
288         }
289
290         nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
291         nf_bridge->physindev = skb->dev;
292         skb->dev = bridge_parent(skb->dev);
293
294         return skb->dev;
295 }
296
297 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
298 static int check_hbh_len(struct sk_buff *skb)
299 {
300         unsigned char *raw = (u8*)(skb->nh.ipv6h+1);
301         u32 pkt_len;
302         int off = raw - skb->nh.raw;
303         int len = (raw[1]+1)<<3;
304
305         if ((raw + len) - skb->data > skb_headlen(skb))
306                 goto bad;
307
308         off += 2;
309         len -= 2;
310
311         while (len > 0) {
312                 int optlen = skb->nh.raw[off+1]+2;
313
314                 switch (skb->nh.raw[off]) {
315                 case IPV6_TLV_PAD0:
316                         optlen = 1;
317                         break;
318
319                 case IPV6_TLV_PADN:
320                         break;
321
322                 case IPV6_TLV_JUMBO:
323                         if (skb->nh.raw[off+1] != 4 || (off&3) != 2)
324                                 goto bad;
325                         pkt_len = ntohl(*(u32*)(skb->nh.raw+off+2));
326                         if (pkt_len <= IPV6_MAXPLEN ||
327                             skb->nh.ipv6h->payload_len)
328                                 goto bad;
329                         if (pkt_len > skb->len - sizeof(struct ipv6hdr))
330                                 goto bad;
331                         if (pskb_trim_rcsum(skb,
332                             pkt_len+sizeof(struct ipv6hdr)))
333                                 goto bad;
334                         break;
335                 default:
336                         if (optlen > len)
337                                 goto bad;
338                         break;
339                 }
340                 off += optlen;
341                 len -= optlen;
342         }
343         if (len == 0)
344                 return 0;
345 bad:
346         return -1;
347
348 }
349
350 /* Replicate the checks that IPv6 does on packet reception and pass the packet
351  * to ip6tables, which doesn't support NAT, so things are fairly simple. */
352 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
353    struct sk_buff *skb, const struct net_device *in,
354    const struct net_device *out, int (*okfn)(struct sk_buff *))
355 {
356         struct ipv6hdr *hdr;
357         u32 pkt_len;
358         struct nf_bridge_info *nf_bridge;
359
360         if (skb->len < sizeof(struct ipv6hdr))
361                 goto inhdr_error;
362
363         if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
364                 goto inhdr_error;
365
366         hdr = skb->nh.ipv6h;
367
368         if (hdr->version != 6)
369                 goto inhdr_error;
370
371         pkt_len = ntohs(hdr->payload_len);
372
373         if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
374                 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
375                         goto inhdr_error;
376                 if (pkt_len + sizeof(struct ipv6hdr) < skb->len) {
377                         if (__pskb_trim(skb, pkt_len + sizeof(struct ipv6hdr)))
378                                 goto inhdr_error;
379                         if (skb->ip_summed == CHECKSUM_HW)
380                                 skb->ip_summed = CHECKSUM_NONE;
381                 }
382         }
383         if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
384                         goto inhdr_error;
385
386         nf_bridge_put(skb->nf_bridge);
387         if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
388                 return NF_DROP;
389         if (!setup_pre_routing(skb))
390                 return NF_DROP;
391
392         NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
393                 br_nf_pre_routing_finish_ipv6);
394
395         return NF_STOLEN;
396
397 inhdr_error:
398         return NF_DROP;
399 }
400
401 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
402  * Replicate the checks that IPv4 does on packet reception.
403  * Set skb->dev to the bridge device (i.e. parent of the
404  * receiving device) to make netfilter happy, the REDIRECT
405  * target in particular.  Save the original destination IP
406  * address to be able to detect DNAT afterwards. */
407 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
408                                       const struct net_device *in,
409                                       const struct net_device *out,
410                                       int (*okfn)(struct sk_buff *))
411 {
412         struct iphdr *iph;
413         __u32 len;
414         struct sk_buff *skb = *pskb;
415         struct nf_bridge_info *nf_bridge;
416         struct vlan_ethhdr *hdr = vlan_eth_hdr(*pskb);
417
418         if (skb->protocol == __constant_htons(ETH_P_IPV6) || IS_VLAN_IPV6) {
419 #ifdef CONFIG_SYSCTL
420                 if (!brnf_call_ip6tables)
421                         return NF_ACCEPT;
422 #endif
423                 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
424                         goto out;
425
426                 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
427                         u8 *vhdr = skb->data;
428                         skb_pull(skb, VLAN_HLEN);
429                         skb_postpull_rcsum(skb, vhdr, VLAN_HLEN);
430                         skb->nh.raw += VLAN_HLEN;
431                 }
432                 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
433         }
434 #ifdef CONFIG_SYSCTL
435         if (!brnf_call_iptables)
436                 return NF_ACCEPT;
437 #endif
438
439         if (skb->protocol != __constant_htons(ETH_P_IP) && !IS_VLAN_IP)
440                 return NF_ACCEPT;
441
442         if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
443                 goto out;
444
445         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
446                 u8 *vhdr = skb->data;
447                 skb_pull(skb, VLAN_HLEN);
448                 skb_postpull_rcsum(skb, vhdr, VLAN_HLEN);
449                 skb->nh.raw += VLAN_HLEN;
450         }
451
452         if (!pskb_may_pull(skb, sizeof(struct iphdr)))
453                 goto inhdr_error;
454
455         iph = skb->nh.iph;
456         if (iph->ihl < 5 || iph->version != 4)
457                 goto inhdr_error;
458
459         if (!pskb_may_pull(skb, 4*iph->ihl))
460                 goto inhdr_error;
461
462         iph = skb->nh.iph;
463         if (ip_fast_csum((__u8 *)iph, iph->ihl) != 0)
464                 goto inhdr_error;
465
466         len = ntohs(iph->tot_len);
467         if (skb->len < len || len < 4*iph->ihl)
468                 goto inhdr_error;
469
470         if (skb->len > len) {
471                 __pskb_trim(skb, len);
472                 if (skb->ip_summed == CHECKSUM_HW)
473                         skb->ip_summed = CHECKSUM_NONE;
474         }
475
476         nf_bridge_put(skb->nf_bridge);
477         if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
478                 return NF_DROP;
479         if (!setup_pre_routing(skb))
480                 return NF_DROP;
481         store_orig_dstaddr(skb);
482
483         NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
484                 br_nf_pre_routing_finish);
485
486         return NF_STOLEN;
487
488 inhdr_error:
489 //      IP_INC_STATS_BH(IpInHdrErrors);
490 out:
491         return NF_DROP;
492 }
493
494
495 /* PF_BRIDGE/LOCAL_IN ************************************************/
496 /* The packet is locally destined, which requires a real
497  * dst_entry, so detach the fake one.  On the way up, the
498  * packet would pass through PRE_ROUTING again (which already
499  * took place when the packet entered the bridge), but we
500  * register an IPv4 PRE_ROUTING 'sabotage' hook that will
501  * prevent this from happening. */
502 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
503    const struct net_device *in, const struct net_device *out,
504    int (*okfn)(struct sk_buff *))
505 {
506         struct sk_buff *skb = *pskb;
507
508         if (skb->dst == (struct dst_entry *)&__fake_rtable) {
509                 dst_release(skb->dst);
510                 skb->dst = NULL;
511         }
512
513         return NF_ACCEPT;
514 }
515
516
517 /* PF_BRIDGE/FORWARD *************************************************/
518 static int br_nf_forward_finish(struct sk_buff *skb)
519 {
520         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
521         struct net_device *in;
522         struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
523
524         if (skb->protocol != __constant_htons(ETH_P_ARP) && !IS_VLAN_ARP) {
525                 in = nf_bridge->physindev;
526                 if (nf_bridge->mask & BRNF_PKT_TYPE) {
527                         skb->pkt_type = PACKET_OTHERHOST;
528                         nf_bridge->mask ^= BRNF_PKT_TYPE;
529                 }
530         } else {
531                 in = *((struct net_device **)(skb->cb));
532         }
533         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
534                 skb_push(skb, VLAN_HLEN);
535                 skb->nh.raw -= VLAN_HLEN;
536         }
537         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
538                         skb->dev, br_forward_finish, 1);
539         return 0;
540 }
541
542 /* This is the 'purely bridged' case.  For IP, we pass the packet to
543  * netfilter with indev and outdev set to the bridge device,
544  * but we are still able to filter on the 'real' indev/outdev
545  * because of the physdev module. For ARP, indev and outdev are the
546  * bridge ports. */
547 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
548    const struct net_device *in, const struct net_device *out,
549    int (*okfn)(struct sk_buff *))
550 {
551         struct sk_buff *skb = *pskb;
552         struct nf_bridge_info *nf_bridge;
553         struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
554         struct net_device *parent;
555         int pf;
556
557         if (!skb->nf_bridge)
558                 return NF_ACCEPT;
559
560         parent = bridge_parent(out);
561         if (!parent)
562                 return NF_DROP;
563
564         if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
565                 pf = PF_INET;
566         else
567                 pf = PF_INET6;
568
569         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
570                 skb_pull(*pskb, VLAN_HLEN);
571                 (*pskb)->nh.raw += VLAN_HLEN;
572         }
573
574         nf_bridge = skb->nf_bridge;
575         if (skb->pkt_type == PACKET_OTHERHOST) {
576                 skb->pkt_type = PACKET_HOST;
577                 nf_bridge->mask |= BRNF_PKT_TYPE;
578         }
579
580         /* The physdev module checks on this */
581         nf_bridge->mask |= BRNF_BRIDGED;
582         nf_bridge->physoutdev = skb->dev;
583
584         NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in), parent,
585                 br_nf_forward_finish);
586
587         return NF_STOLEN;
588 }
589
590 static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
591    const struct net_device *in, const struct net_device *out,
592    int (*okfn)(struct sk_buff *))
593 {
594         struct sk_buff *skb = *pskb;
595         struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
596         struct net_device **d = (struct net_device **)(skb->cb);
597
598 #ifdef CONFIG_SYSCTL
599         if (!brnf_call_arptables)
600                 return NF_ACCEPT;
601 #endif
602
603         if (skb->protocol != __constant_htons(ETH_P_ARP)) {
604                 if (!IS_VLAN_ARP)
605                         return NF_ACCEPT;
606                 skb_pull(*pskb, VLAN_HLEN);
607                 (*pskb)->nh.raw += VLAN_HLEN;
608         }
609
610         if (skb->nh.arph->ar_pln != 4) {
611                 if (IS_VLAN_ARP) {
612                         skb_push(*pskb, VLAN_HLEN);
613                         (*pskb)->nh.raw -= VLAN_HLEN;
614                 }
615                 return NF_ACCEPT;
616         }
617         *d = (struct net_device *)in;
618         NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
619                 (struct net_device *)out, br_nf_forward_finish);
620
621         return NF_STOLEN;
622 }
623
624
625 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
626 static int br_nf_local_out_finish(struct sk_buff *skb)
627 {
628         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
629                 skb_push(skb, VLAN_HLEN);
630                 skb->nh.raw -= VLAN_HLEN;
631         }
632
633         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
634                         br_forward_finish, NF_BR_PRI_FIRST + 1);
635
636         return 0;
637 }
638
639 /* This function sees both locally originated IP packets and forwarded
640  * IP packets (in both cases the destination device is a bridge
641  * device). It also sees bridged-and-DNAT'ed packets.
642  * To be able to filter on the physical bridge devices (with the physdev
643  * module), we steal packets destined to a bridge device away from the
644  * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
645  * when we have determined the real output device. This is done in here.
646  *
647  * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
648  * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
649  * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
650  * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
651  * will be executed.
652  * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
653  * this packet before, and so the packet was locally originated. We fake
654  * the PF_INET/LOCAL_OUT hook.
655  * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
656  * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
657  * even routed packets that didn't arrive on a bridge interface have their
658  * nf_bridge->physindev set. */
659 static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
660    const struct net_device *in, const struct net_device *out,
661    int (*okfn)(struct sk_buff *))
662 {
663         struct net_device *realindev, *realoutdev;
664         struct sk_buff *skb = *pskb;
665         struct nf_bridge_info *nf_bridge;
666         struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
667         int pf;
668
669         if (!skb->nf_bridge)
670                 return NF_ACCEPT;
671
672         if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
673                 pf = PF_INET;
674         else
675                 pf = PF_INET6;
676
677 #ifdef CONFIG_NETFILTER_DEBUG
678         /* Sometimes we get packets with NULL ->dst here (for example,
679          * running a dhcp client daemon triggers this). This should now
680          * be fixed, but let's keep the check around. */
681         if (skb->dst == NULL) {
682                 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
683                 return NF_ACCEPT;
684         }
685 #endif
686
687         nf_bridge = skb->nf_bridge;
688         nf_bridge->physoutdev = skb->dev;
689         realindev = nf_bridge->physindev;
690
691         /* Bridged, take PF_BRIDGE/FORWARD.
692          * (see big note in front of br_nf_pre_routing_finish) */
693         if (nf_bridge->mask & BRNF_BRIDGED_DNAT) {
694                 if (nf_bridge->mask & BRNF_PKT_TYPE) {
695                         skb->pkt_type = PACKET_OTHERHOST;
696                         nf_bridge->mask ^= BRNF_PKT_TYPE;
697                 }
698                 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
699                         skb_push(skb, VLAN_HLEN);
700                         skb->nh.raw -= VLAN_HLEN;
701                 }
702
703                 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev,
704                         skb->dev, br_forward_finish);
705                 goto out;
706         }
707         realoutdev = bridge_parent(skb->dev);
708         if (!realoutdev)
709                 return NF_DROP;
710
711 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
712         /* iptables should match -o br0.x */
713         if (nf_bridge->netoutdev)
714                 realoutdev = nf_bridge->netoutdev;
715 #endif
716         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
717                 skb_pull(skb, VLAN_HLEN);
718                 (*pskb)->nh.raw += VLAN_HLEN;
719         }
720         /* IP forwarded traffic has a physindev, locally
721          * generated traffic hasn't. */
722         if (realindev != NULL) {
723                 if (!(nf_bridge->mask & BRNF_DONT_TAKE_PARENT) ) {
724                         struct net_device *parent = bridge_parent(realindev);
725                         if (parent)
726                                 realindev = parent;
727                 }
728
729                 NF_HOOK_THRESH(pf, NF_IP_FORWARD, skb, realindev,
730                                realoutdev, br_nf_local_out_finish,
731                                NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD + 1);
732         } else {
733                 NF_HOOK_THRESH(pf, NF_IP_LOCAL_OUT, skb, realindev,
734                                realoutdev, br_nf_local_out_finish,
735                                NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT + 1);
736         }
737
738 out:
739         return NF_STOLEN;
740 }
741
742
743 /* PF_BRIDGE/POST_ROUTING ********************************************/
744 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
745    const struct net_device *in, const struct net_device *out,
746    int (*okfn)(struct sk_buff *))
747 {
748         struct sk_buff *skb = *pskb;
749         struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
750         struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
751         struct net_device *realoutdev = bridge_parent(skb->dev);
752         int pf;
753
754 #ifdef CONFIG_NETFILTER_DEBUG
755         /* Be very paranoid. This probably won't happen anymore, but let's
756          * keep the check just to be sure... */
757         if (skb->mac.raw < skb->head || skb->mac.raw + ETH_HLEN > skb->data) {
758                 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
759                                  "bad mac.raw pointer.");
760                 goto print_error;
761         }
762 #endif
763
764         if (!nf_bridge)
765                 return NF_ACCEPT;
766
767         if (!realoutdev)
768                 return NF_DROP;
769
770         if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
771                 pf = PF_INET;
772         else
773                 pf = PF_INET6;
774
775 #ifdef CONFIG_NETFILTER_DEBUG
776         if (skb->dst == NULL) {
777                 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
778                 goto print_error;
779         }
780 #endif
781
782         /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
783          * about the value of skb->pkt_type. */
784         if (skb->pkt_type == PACKET_OTHERHOST) {
785                 skb->pkt_type = PACKET_HOST;
786                 nf_bridge->mask |= BRNF_PKT_TYPE;
787         }
788
789         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
790                 skb_pull(skb, VLAN_HLEN);
791                 skb->nh.raw += VLAN_HLEN;
792         }
793
794         nf_bridge_save_header(skb);
795
796 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
797         if (nf_bridge->netoutdev)
798                 realoutdev = nf_bridge->netoutdev;
799 #endif
800         NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev,
801                 br_dev_queue_push_xmit);
802
803         return NF_STOLEN;
804
805 #ifdef CONFIG_NETFILTER_DEBUG
806 print_error:
807         if (skb->dev != NULL) {
808                 printk("[%s]", skb->dev->name);
809                 if (realoutdev)
810                         printk("[%s]", realoutdev->name);
811         }
812         printk(" head:%p, raw:%p, data:%p\n", skb->head, skb->mac.raw,
813                                               skb->data);
814         return NF_ACCEPT;
815 #endif
816 }
817
818
819 /* IP/SABOTAGE *****************************************************/
820 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
821  * for the second time. */
822 static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff **pskb,
823    const struct net_device *in, const struct net_device *out,
824    int (*okfn)(struct sk_buff *))
825 {
826         if ((*pskb)->nf_bridge &&
827             !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
828                 return NF_STOP;
829         }
830
831         return NF_ACCEPT;
832 }
833
834 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
835  * and PF_INET(6)/POST_ROUTING until we have done the forwarding
836  * decision in the bridge code and have determined nf_bridge->physoutdev. */
837 static unsigned int ip_sabotage_out(unsigned int hook, struct sk_buff **pskb,
838    const struct net_device *in, const struct net_device *out,
839    int (*okfn)(struct sk_buff *))
840 {
841         struct sk_buff *skb = *pskb;
842
843         if ((out->hard_start_xmit == br_dev_xmit &&
844             okfn != br_nf_forward_finish &&
845             okfn != br_nf_local_out_finish &&
846             okfn != br_dev_queue_push_xmit)
847 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
848             || ((out->priv_flags & IFF_802_1Q_VLAN) &&
849             VLAN_DEV_INFO(out)->real_dev->hard_start_xmit == br_dev_xmit)
850 #endif
851             ) {
852                 struct nf_bridge_info *nf_bridge;
853
854                 if (!skb->nf_bridge) {
855 #ifdef CONFIG_SYSCTL
856                         /* This code is executed while in the IP(v6) stack,
857                            the version should be 4 or 6. We can't use
858                            skb->protocol because that isn't set on
859                            PF_INET(6)/LOCAL_OUT. */
860                         struct iphdr *ip = skb->nh.iph;
861
862                         if (ip->version == 4 && !brnf_call_iptables)
863                                 return NF_ACCEPT;
864                         else if (ip->version == 6 && !brnf_call_ip6tables)
865                                 return NF_ACCEPT;
866 #endif
867                         if (hook == NF_IP_POST_ROUTING)
868                                 return NF_ACCEPT;
869                         if (!nf_bridge_alloc(skb))
870                                 return NF_DROP;
871                 }
872
873                 nf_bridge = skb->nf_bridge;
874
875                 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
876                  * will need the indev then. For a brouter, the real indev
877                  * can be a bridge port, so we make sure br_nf_local_out()
878                  * doesn't use the bridge parent of the indev by using
879                  * the BRNF_DONT_TAKE_PARENT mask. */
880                 if (hook == NF_IP_FORWARD && nf_bridge->physindev == NULL) {
881                         nf_bridge->mask |= BRNF_DONT_TAKE_PARENT;
882                         nf_bridge->physindev = (struct net_device *)in;
883                 }
884 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
885                 /* the iptables outdev is br0.x, not br0 */
886                 if (out->priv_flags & IFF_802_1Q_VLAN)
887                         nf_bridge->netoutdev = (struct net_device *)out;
888 #endif
889                 return NF_STOP;
890         }
891
892         return NF_ACCEPT;
893 }
894
895 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
896  * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
897  * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
898  * ip_refrag() can return NF_STOLEN. */
899 static struct nf_hook_ops br_nf_ops[] = {
900         { .hook = br_nf_pre_routing, 
901           .owner = THIS_MODULE, 
902           .pf = PF_BRIDGE, 
903           .hooknum = NF_BR_PRE_ROUTING, 
904           .priority = NF_BR_PRI_BRNF, },
905         { .hook = br_nf_local_in,
906           .owner = THIS_MODULE,
907           .pf = PF_BRIDGE,
908           .hooknum = NF_BR_LOCAL_IN,
909           .priority = NF_BR_PRI_BRNF, },
910         { .hook = br_nf_forward_ip,
911           .owner = THIS_MODULE,
912           .pf = PF_BRIDGE,
913           .hooknum = NF_BR_FORWARD,
914           .priority = NF_BR_PRI_BRNF - 1, },
915         { .hook = br_nf_forward_arp,
916           .owner = THIS_MODULE,
917           .pf = PF_BRIDGE,
918           .hooknum = NF_BR_FORWARD,
919           .priority = NF_BR_PRI_BRNF, },
920         { .hook = br_nf_local_out,
921           .owner = THIS_MODULE,
922           .pf = PF_BRIDGE,
923           .hooknum = NF_BR_LOCAL_OUT,
924           .priority = NF_BR_PRI_FIRST, },
925         { .hook = br_nf_post_routing,
926           .owner = THIS_MODULE,
927           .pf = PF_BRIDGE,
928           .hooknum = NF_BR_POST_ROUTING,
929           .priority = NF_BR_PRI_LAST, },
930         { .hook = ip_sabotage_in,
931           .owner = THIS_MODULE,
932           .pf = PF_INET,
933           .hooknum = NF_IP_PRE_ROUTING,
934           .priority = NF_IP_PRI_FIRST, },
935         { .hook = ip_sabotage_in,
936           .owner = THIS_MODULE,
937           .pf = PF_INET6,
938           .hooknum = NF_IP6_PRE_ROUTING,
939           .priority = NF_IP6_PRI_FIRST, },
940         { .hook = ip_sabotage_out,
941           .owner = THIS_MODULE,
942           .pf = PF_INET,
943           .hooknum = NF_IP_FORWARD,
944           .priority = NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD, },
945         { .hook = ip_sabotage_out,
946           .owner = THIS_MODULE,
947           .pf = PF_INET6,
948           .hooknum = NF_IP6_FORWARD,
949           .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD, },
950         { .hook = ip_sabotage_out,
951           .owner = THIS_MODULE,
952           .pf = PF_INET,
953           .hooknum = NF_IP_LOCAL_OUT,
954           .priority = NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
955         { .hook = ip_sabotage_out,
956           .owner = THIS_MODULE,
957           .pf = PF_INET6,
958           .hooknum = NF_IP6_LOCAL_OUT,
959           .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
960         { .hook = ip_sabotage_out,
961           .owner = THIS_MODULE,
962           .pf = PF_INET,
963           .hooknum = NF_IP_POST_ROUTING,
964           .priority = NF_IP_PRI_FIRST, },
965         { .hook = ip_sabotage_out,
966           .owner = THIS_MODULE,
967           .pf = PF_INET6,
968           .hooknum = NF_IP6_POST_ROUTING,
969           .priority = NF_IP6_PRI_FIRST, },
970 };
971
972 #ifdef CONFIG_SYSCTL
973 static
974 int brnf_sysctl_call_tables(ctl_table *ctl, int write, struct file * filp,
975                         void __user *buffer, size_t *lenp, loff_t *ppos)
976 {
977         int ret;
978
979         ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
980
981         if (write && *(int *)(ctl->data))
982                 *(int *)(ctl->data) = 1;
983         return ret;
984 }
985
986 static ctl_table brnf_table[] = {
987         {
988                 .ctl_name       = NET_BRIDGE_NF_CALL_ARPTABLES,
989                 .procname       = "bridge-nf-call-arptables",
990                 .data           = &brnf_call_arptables,
991                 .maxlen         = sizeof(int),
992                 .mode           = 0644,
993                 .proc_handler   = &brnf_sysctl_call_tables,
994         },
995         {
996                 .ctl_name       = NET_BRIDGE_NF_CALL_IPTABLES,
997                 .procname       = "bridge-nf-call-iptables",
998                 .data           = &brnf_call_iptables,
999                 .maxlen         = sizeof(int),
1000                 .mode           = 0644,
1001                 .proc_handler   = &brnf_sysctl_call_tables,
1002         },
1003         {
1004                 .ctl_name       = NET_BRIDGE_NF_CALL_IP6TABLES,
1005                 .procname       = "bridge-nf-call-ip6tables",
1006                 .data           = &brnf_call_ip6tables,
1007                 .maxlen         = sizeof(int),
1008                 .mode           = 0644,
1009                 .proc_handler   = &brnf_sysctl_call_tables,
1010         },
1011         {
1012                 .ctl_name       = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
1013                 .procname       = "bridge-nf-filter-vlan-tagged",
1014                 .data           = &brnf_filter_vlan_tagged,
1015                 .maxlen         = sizeof(int),
1016                 .mode           = 0644,
1017                 .proc_handler   = &brnf_sysctl_call_tables,
1018         },
1019         { .ctl_name = 0 }
1020 };
1021
1022 static ctl_table brnf_bridge_table[] = {
1023         {
1024                 .ctl_name       = NET_BRIDGE,
1025                 .procname       = "bridge",
1026                 .mode           = 0555,
1027                 .child          = brnf_table,
1028         },
1029         { .ctl_name = 0 }
1030 };
1031
1032 static ctl_table brnf_net_table[] = {
1033         {
1034                 .ctl_name       = CTL_NET,
1035                 .procname       = "net",
1036                 .mode           = 0555,
1037                 .child          = brnf_bridge_table,
1038         },
1039         { .ctl_name = 0 }
1040 };
1041 #endif
1042
1043 int br_netfilter_init(void)
1044 {
1045         int i;
1046
1047         for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) {
1048                 int ret;
1049
1050                 if ((ret = nf_register_hook(&br_nf_ops[i])) >= 0)
1051                         continue;
1052
1053                 while (i--)
1054                         nf_unregister_hook(&br_nf_ops[i]);
1055
1056                 return ret;
1057         }
1058
1059 #ifdef CONFIG_SYSCTL
1060         brnf_sysctl_header = register_sysctl_table(brnf_net_table, 0);
1061         if (brnf_sysctl_header == NULL) {
1062                 printk(KERN_WARNING "br_netfilter: can't register to sysctl.\n");
1063                 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++)
1064                         nf_unregister_hook(&br_nf_ops[i]);
1065                 return -EFAULT;
1066         }
1067 #endif
1068
1069         printk(KERN_NOTICE "Bridge firewalling registered\n");
1070
1071         return 0;
1072 }
1073
1074 void br_netfilter_fini(void)
1075 {
1076         int i;
1077
1078         for (i = ARRAY_SIZE(br_nf_ops) - 1; i >= 0; i--)
1079                 nf_unregister_hook(&br_nf_ops[i]);
1080 #ifdef CONFIG_SYSCTL
1081         unregister_sysctl_table(brnf_sysctl_header);
1082 #endif
1083 }