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