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