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