[NET]: Merge TSO/UFO fields in sk_buff
[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 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 (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
411                         goto inhdr_error;
412         }
413         if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
414                 goto inhdr_error;
415
416         nf_bridge_put(skb->nf_bridge);
417         if (!nf_bridge_alloc(skb))
418                 return NF_DROP;
419         if (!setup_pre_routing(skb))
420                 return NF_DROP;
421
422         NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
423                 br_nf_pre_routing_finish_ipv6);
424
425         return NF_STOLEN;
426
427 inhdr_error:
428         return NF_DROP;
429 }
430
431 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
432  * Replicate the checks that IPv4 does on packet reception.
433  * Set skb->dev to the bridge device (i.e. parent of the
434  * receiving device) to make netfilter happy, the REDIRECT
435  * target in particular.  Save the original destination IP
436  * address to be able to detect DNAT afterwards. */
437 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
438                                       const struct net_device *in,
439                                       const struct net_device *out,
440                                       int (*okfn)(struct sk_buff *))
441 {
442         struct iphdr *iph;
443         __u32 len;
444         struct sk_buff *skb = *pskb;
445
446         if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb)) {
447 #ifdef CONFIG_SYSCTL
448                 if (!brnf_call_ip6tables)
449                         return NF_ACCEPT;
450 #endif
451                 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
452                         goto out;
453
454                 if (skb->protocol == htons(ETH_P_8021Q)) {
455                         skb_pull_rcsum(skb, VLAN_HLEN);
456                         skb->nh.raw += VLAN_HLEN;
457                 }
458                 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
459         }
460 #ifdef CONFIG_SYSCTL
461         if (!brnf_call_iptables)
462                 return NF_ACCEPT;
463 #endif
464
465         if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb))
466                 return NF_ACCEPT;
467
468         if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
469                 goto out;
470
471         if (skb->protocol == htons(ETH_P_8021Q)) {
472                 skb_pull_rcsum(skb, VLAN_HLEN);
473                 skb->nh.raw += VLAN_HLEN;
474         }
475
476         if (!pskb_may_pull(skb, sizeof(struct iphdr)))
477                 goto inhdr_error;
478
479         iph = skb->nh.iph;
480         if (iph->ihl < 5 || iph->version != 4)
481                 goto inhdr_error;
482
483         if (!pskb_may_pull(skb, 4 * iph->ihl))
484                 goto inhdr_error;
485
486         iph = skb->nh.iph;
487         if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
488                 goto inhdr_error;
489
490         len = ntohs(iph->tot_len);
491         if (skb->len < len || len < 4 * iph->ihl)
492                 goto inhdr_error;
493
494         pskb_trim_rcsum(skb, len);
495
496         nf_bridge_put(skb->nf_bridge);
497         if (!nf_bridge_alloc(skb))
498                 return NF_DROP;
499         if (!setup_pre_routing(skb))
500                 return NF_DROP;
501         store_orig_dstaddr(skb);
502
503         NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
504                 br_nf_pre_routing_finish);
505
506         return NF_STOLEN;
507
508 inhdr_error:
509 //      IP_INC_STATS_BH(IpInHdrErrors);
510 out:
511         return NF_DROP;
512 }
513
514
515 /* PF_BRIDGE/LOCAL_IN ************************************************/
516 /* The packet is locally destined, which requires a real
517  * dst_entry, so detach the fake one.  On the way up, the
518  * packet would pass through PRE_ROUTING again (which already
519  * took place when the packet entered the bridge), but we
520  * register an IPv4 PRE_ROUTING 'sabotage' hook that will
521  * prevent this from happening. */
522 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
523                                    const struct net_device *in,
524                                    const struct net_device *out,
525                                    int (*okfn)(struct sk_buff *))
526 {
527         struct sk_buff *skb = *pskb;
528
529         if (skb->dst == (struct dst_entry *)&__fake_rtable) {
530                 dst_release(skb->dst);
531                 skb->dst = NULL;
532         }
533
534         return NF_ACCEPT;
535 }
536
537 /* PF_BRIDGE/FORWARD *************************************************/
538 static int br_nf_forward_finish(struct sk_buff *skb)
539 {
540         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
541         struct net_device *in;
542
543         if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
544                 in = nf_bridge->physindev;
545                 if (nf_bridge->mask & BRNF_PKT_TYPE) {
546                         skb->pkt_type = PACKET_OTHERHOST;
547                         nf_bridge->mask ^= BRNF_PKT_TYPE;
548                 }
549         } else {
550                 in = *((struct net_device **)(skb->cb));
551         }
552         if (skb->protocol == htons(ETH_P_8021Q)) {
553                 skb_push(skb, VLAN_HLEN);
554                 skb->nh.raw -= VLAN_HLEN;
555         }
556         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
557                        skb->dev, br_forward_finish, 1);
558         return 0;
559 }
560
561 /* This is the 'purely bridged' case.  For IP, we pass the packet to
562  * netfilter with indev and outdev set to the bridge device,
563  * but we are still able to filter on the 'real' indev/outdev
564  * because of the physdev module. For ARP, indev and outdev are the
565  * bridge ports. */
566 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
567                                      const struct net_device *in,
568                                      const struct net_device *out,
569                                      int (*okfn)(struct sk_buff *))
570 {
571         struct sk_buff *skb = *pskb;
572         struct nf_bridge_info *nf_bridge;
573         struct net_device *parent;
574         int pf;
575
576         if (!skb->nf_bridge)
577                 return NF_ACCEPT;
578
579         parent = bridge_parent(out);
580         if (!parent)
581                 return NF_DROP;
582
583         if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
584                 pf = PF_INET;
585         else
586                 pf = PF_INET6;
587
588         if (skb->protocol == htons(ETH_P_8021Q)) {
589                 skb_pull(*pskb, VLAN_HLEN);
590                 (*pskb)->nh.raw += VLAN_HLEN;
591         }
592
593         nf_bridge = skb->nf_bridge;
594         if (skb->pkt_type == PACKET_OTHERHOST) {
595                 skb->pkt_type = PACKET_HOST;
596                 nf_bridge->mask |= BRNF_PKT_TYPE;
597         }
598
599         /* The physdev module checks on this */
600         nf_bridge->mask |= BRNF_BRIDGED;
601         nf_bridge->physoutdev = skb->dev;
602
603         NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in), parent,
604                 br_nf_forward_finish);
605
606         return NF_STOLEN;
607 }
608
609 static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
610                                       const struct net_device *in,
611                                       const struct net_device *out,
612                                       int (*okfn)(struct sk_buff *))
613 {
614         struct sk_buff *skb = *pskb;
615         struct net_device **d = (struct net_device **)(skb->cb);
616
617 #ifdef CONFIG_SYSCTL
618         if (!brnf_call_arptables)
619                 return NF_ACCEPT;
620 #endif
621
622         if (skb->protocol != htons(ETH_P_ARP)) {
623                 if (!IS_VLAN_ARP(skb))
624                         return NF_ACCEPT;
625                 skb_pull(*pskb, VLAN_HLEN);
626                 (*pskb)->nh.raw += VLAN_HLEN;
627         }
628
629         if (skb->nh.arph->ar_pln != 4) {
630                 if (IS_VLAN_ARP(skb)) {
631                         skb_push(*pskb, VLAN_HLEN);
632                         (*pskb)->nh.raw -= VLAN_HLEN;
633                 }
634                 return NF_ACCEPT;
635         }
636         *d = (struct net_device *)in;
637         NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
638                 (struct net_device *)out, br_nf_forward_finish);
639
640         return NF_STOLEN;
641 }
642
643 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
644 static int br_nf_local_out_finish(struct sk_buff *skb)
645 {
646         if (skb->protocol == htons(ETH_P_8021Q)) {
647                 skb_push(skb, VLAN_HLEN);
648                 skb->nh.raw -= VLAN_HLEN;
649         }
650
651         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
652                        br_forward_finish, NF_BR_PRI_FIRST + 1);
653
654         return 0;
655 }
656
657 /* This function sees both locally originated IP packets and forwarded
658  * IP packets (in both cases the destination device is a bridge
659  * device). It also sees bridged-and-DNAT'ed packets.
660  * To be able to filter on the physical bridge devices (with the physdev
661  * module), we steal packets destined to a bridge device away from the
662  * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
663  * when we have determined the real output device. This is done in here.
664  *
665  * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
666  * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
667  * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
668  * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
669  * will be executed.
670  * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
671  * this packet before, and so the packet was locally originated. We fake
672  * the PF_INET/LOCAL_OUT hook.
673  * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
674  * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
675  * even routed packets that didn't arrive on a bridge interface have their
676  * nf_bridge->physindev set. */
677 static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
678                                     const struct net_device *in,
679                                     const struct net_device *out,
680                                     int (*okfn)(struct sk_buff *))
681 {
682         struct net_device *realindev, *realoutdev;
683         struct sk_buff *skb = *pskb;
684         struct nf_bridge_info *nf_bridge;
685         int pf;
686
687         if (!skb->nf_bridge)
688                 return NF_ACCEPT;
689
690         if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
691                 pf = PF_INET;
692         else
693                 pf = PF_INET6;
694
695 #ifdef CONFIG_NETFILTER_DEBUG
696         /* Sometimes we get packets with NULL ->dst here (for example,
697          * running a dhcp client daemon triggers this). This should now
698          * be fixed, but let's keep the check around. */
699         if (skb->dst == NULL) {
700                 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
701                 return NF_ACCEPT;
702         }
703 #endif
704
705         nf_bridge = skb->nf_bridge;
706         nf_bridge->physoutdev = skb->dev;
707         realindev = nf_bridge->physindev;
708
709         /* Bridged, take PF_BRIDGE/FORWARD.
710          * (see big note in front of br_nf_pre_routing_finish) */
711         if (nf_bridge->mask & BRNF_BRIDGED_DNAT) {
712                 if (nf_bridge->mask & BRNF_PKT_TYPE) {
713                         skb->pkt_type = PACKET_OTHERHOST;
714                         nf_bridge->mask ^= BRNF_PKT_TYPE;
715                 }
716                 if (skb->protocol == htons(ETH_P_8021Q)) {
717                         skb_push(skb, VLAN_HLEN);
718                         skb->nh.raw -= VLAN_HLEN;
719                 }
720
721                 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev,
722                         skb->dev, br_forward_finish);
723                 goto out;
724         }
725         realoutdev = bridge_parent(skb->dev);
726         if (!realoutdev)
727                 return NF_DROP;
728
729 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
730         /* iptables should match -o br0.x */
731         if (nf_bridge->netoutdev)
732                 realoutdev = nf_bridge->netoutdev;
733 #endif
734         if (skb->protocol == htons(ETH_P_8021Q)) {
735                 skb_pull(skb, VLAN_HLEN);
736                 (*pskb)->nh.raw += VLAN_HLEN;
737         }
738         /* IP forwarded traffic has a physindev, locally
739          * generated traffic hasn't. */
740         if (realindev != NULL) {
741                 if (!(nf_bridge->mask & BRNF_DONT_TAKE_PARENT)) {
742                         struct net_device *parent = bridge_parent(realindev);
743                         if (parent)
744                                 realindev = parent;
745                 }
746
747                 NF_HOOK_THRESH(pf, NF_IP_FORWARD, skb, realindev,
748                                realoutdev, br_nf_local_out_finish,
749                                NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD + 1);
750         } else {
751                 NF_HOOK_THRESH(pf, NF_IP_LOCAL_OUT, skb, realindev,
752                                realoutdev, br_nf_local_out_finish,
753                                NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT + 1);
754         }
755
756 out:
757         return NF_STOLEN;
758 }
759
760 static int br_nf_dev_queue_xmit(struct sk_buff *skb)
761 {
762         if (skb->protocol == htons(ETH_P_IP) &&
763             skb->len > skb->dev->mtu &&
764             !skb_shinfo(skb)->gso_size)
765                 return ip_fragment(skb, br_dev_queue_push_xmit);
766         else
767                 return br_dev_queue_push_xmit(skb);
768 }
769
770 /* PF_BRIDGE/POST_ROUTING ********************************************/
771 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
772                                        const struct net_device *in,
773                                        const struct net_device *out,
774                                        int (*okfn)(struct sk_buff *))
775 {
776         struct sk_buff *skb = *pskb;
777         struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
778         struct net_device *realoutdev = bridge_parent(skb->dev);
779         int pf;
780
781 #ifdef CONFIG_NETFILTER_DEBUG
782         /* Be very paranoid. This probably won't happen anymore, but let's
783          * keep the check just to be sure... */
784         if (skb->mac.raw < skb->head || skb->mac.raw + ETH_HLEN > skb->data) {
785                 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
786                        "bad mac.raw pointer.");
787                 goto print_error;
788         }
789 #endif
790
791         if (!nf_bridge)
792                 return NF_ACCEPT;
793
794         if (!realoutdev)
795                 return NF_DROP;
796
797         if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
798                 pf = PF_INET;
799         else
800                 pf = PF_INET6;
801
802 #ifdef CONFIG_NETFILTER_DEBUG
803         if (skb->dst == NULL) {
804                 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
805                 goto print_error;
806         }
807 #endif
808
809         /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
810          * about the value of skb->pkt_type. */
811         if (skb->pkt_type == PACKET_OTHERHOST) {
812                 skb->pkt_type = PACKET_HOST;
813                 nf_bridge->mask |= BRNF_PKT_TYPE;
814         }
815
816         if (skb->protocol == htons(ETH_P_8021Q)) {
817                 skb_pull(skb, VLAN_HLEN);
818                 skb->nh.raw += VLAN_HLEN;
819         }
820
821         nf_bridge_save_header(skb);
822
823 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
824         if (nf_bridge->netoutdev)
825                 realoutdev = nf_bridge->netoutdev;
826 #endif
827         NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev,
828                 br_nf_dev_queue_xmit);
829
830         return NF_STOLEN;
831
832 #ifdef CONFIG_NETFILTER_DEBUG
833 print_error:
834         if (skb->dev != NULL) {
835                 printk("[%s]", skb->dev->name);
836                 if (realoutdev)
837                         printk("[%s]", realoutdev->name);
838         }
839         printk(" head:%p, raw:%p, data:%p\n", skb->head, skb->mac.raw,
840                skb->data);
841         return NF_ACCEPT;
842 #endif
843 }
844
845 /* IP/SABOTAGE *****************************************************/
846 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
847  * for the second time. */
848 static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff **pskb,
849                                    const struct net_device *in,
850                                    const struct net_device *out,
851                                    int (*okfn)(struct sk_buff *))
852 {
853         if ((*pskb)->nf_bridge &&
854             !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
855                 return NF_STOP;
856         }
857
858         return NF_ACCEPT;
859 }
860
861 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
862  * and PF_INET(6)/POST_ROUTING until we have done the forwarding
863  * decision in the bridge code and have determined nf_bridge->physoutdev. */
864 static unsigned int ip_sabotage_out(unsigned int hook, struct sk_buff **pskb,
865                                     const struct net_device *in,
866                                     const struct net_device *out,
867                                     int (*okfn)(struct sk_buff *))
868 {
869         struct sk_buff *skb = *pskb;
870
871         if ((out->hard_start_xmit == br_dev_xmit &&
872              okfn != br_nf_forward_finish &&
873              okfn != br_nf_local_out_finish && okfn != br_nf_dev_queue_xmit)
874 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
875             || ((out->priv_flags & IFF_802_1Q_VLAN) &&
876                 VLAN_DEV_INFO(out)->real_dev->hard_start_xmit == br_dev_xmit)
877 #endif
878             ) {
879                 struct nf_bridge_info *nf_bridge;
880
881                 if (!skb->nf_bridge) {
882 #ifdef CONFIG_SYSCTL
883                         /* This code is executed while in the IP(v6) stack,
884                            the version should be 4 or 6. We can't use
885                            skb->protocol because that isn't set on
886                            PF_INET(6)/LOCAL_OUT. */
887                         struct iphdr *ip = skb->nh.iph;
888
889                         if (ip->version == 4 && !brnf_call_iptables)
890                                 return NF_ACCEPT;
891                         else if (ip->version == 6 && !brnf_call_ip6tables)
892                                 return NF_ACCEPT;
893 #endif
894                         if (hook == NF_IP_POST_ROUTING)
895                                 return NF_ACCEPT;
896                         if (!nf_bridge_alloc(skb))
897                                 return NF_DROP;
898                 }
899
900                 nf_bridge = skb->nf_bridge;
901
902                 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
903                  * will need the indev then. For a brouter, the real indev
904                  * can be a bridge port, so we make sure br_nf_local_out()
905                  * doesn't use the bridge parent of the indev by using
906                  * the BRNF_DONT_TAKE_PARENT mask. */
907                 if (hook == NF_IP_FORWARD && nf_bridge->physindev == NULL) {
908                         nf_bridge->mask |= BRNF_DONT_TAKE_PARENT;
909                         nf_bridge->physindev = (struct net_device *)in;
910                 }
911 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
912                 /* the iptables outdev is br0.x, not br0 */
913                 if (out->priv_flags & IFF_802_1Q_VLAN)
914                         nf_bridge->netoutdev = (struct net_device *)out;
915 #endif
916                 return NF_STOP;
917         }
918
919         return NF_ACCEPT;
920 }
921
922 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
923  * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
924  * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
925  * ip_refrag() can return NF_STOLEN. */
926 static struct nf_hook_ops br_nf_ops[] = {
927         { .hook = br_nf_pre_routing, 
928           .owner = THIS_MODULE, 
929           .pf = PF_BRIDGE, 
930           .hooknum = NF_BR_PRE_ROUTING, 
931           .priority = NF_BR_PRI_BRNF, },
932         { .hook = br_nf_local_in,
933           .owner = THIS_MODULE,
934           .pf = PF_BRIDGE,
935           .hooknum = NF_BR_LOCAL_IN,
936           .priority = NF_BR_PRI_BRNF, },
937         { .hook = br_nf_forward_ip,
938           .owner = THIS_MODULE,
939           .pf = PF_BRIDGE,
940           .hooknum = NF_BR_FORWARD,
941           .priority = NF_BR_PRI_BRNF - 1, },
942         { .hook = br_nf_forward_arp,
943           .owner = THIS_MODULE,
944           .pf = PF_BRIDGE,
945           .hooknum = NF_BR_FORWARD,
946           .priority = NF_BR_PRI_BRNF, },
947         { .hook = br_nf_local_out,
948           .owner = THIS_MODULE,
949           .pf = PF_BRIDGE,
950           .hooknum = NF_BR_LOCAL_OUT,
951           .priority = NF_BR_PRI_FIRST, },
952         { .hook = br_nf_post_routing,
953           .owner = THIS_MODULE,
954           .pf = PF_BRIDGE,
955           .hooknum = NF_BR_POST_ROUTING,
956           .priority = NF_BR_PRI_LAST, },
957         { .hook = ip_sabotage_in,
958           .owner = THIS_MODULE,
959           .pf = PF_INET,
960           .hooknum = NF_IP_PRE_ROUTING,
961           .priority = NF_IP_PRI_FIRST, },
962         { .hook = ip_sabotage_in,
963           .owner = THIS_MODULE,
964           .pf = PF_INET6,
965           .hooknum = NF_IP6_PRE_ROUTING,
966           .priority = NF_IP6_PRI_FIRST, },
967         { .hook = ip_sabotage_out,
968           .owner = THIS_MODULE,
969           .pf = PF_INET,
970           .hooknum = NF_IP_FORWARD,
971           .priority = NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD, },
972         { .hook = ip_sabotage_out,
973           .owner = THIS_MODULE,
974           .pf = PF_INET6,
975           .hooknum = NF_IP6_FORWARD,
976           .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD, },
977         { .hook = ip_sabotage_out,
978           .owner = THIS_MODULE,
979           .pf = PF_INET,
980           .hooknum = NF_IP_LOCAL_OUT,
981           .priority = NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
982         { .hook = ip_sabotage_out,
983           .owner = THIS_MODULE,
984           .pf = PF_INET6,
985           .hooknum = NF_IP6_LOCAL_OUT,
986           .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
987         { .hook = ip_sabotage_out,
988           .owner = THIS_MODULE,
989           .pf = PF_INET,
990           .hooknum = NF_IP_POST_ROUTING,
991           .priority = NF_IP_PRI_FIRST, },
992         { .hook = ip_sabotage_out,
993           .owner = THIS_MODULE,
994           .pf = PF_INET6,
995           .hooknum = NF_IP6_POST_ROUTING,
996           .priority = NF_IP6_PRI_FIRST, },
997 };
998
999 #ifdef CONFIG_SYSCTL
1000 static
1001 int brnf_sysctl_call_tables(ctl_table * ctl, int write, struct file *filp,
1002                             void __user * buffer, size_t * lenp, loff_t * ppos)
1003 {
1004         int ret;
1005
1006         ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1007
1008         if (write && *(int *)(ctl->data))
1009                 *(int *)(ctl->data) = 1;
1010         return ret;
1011 }
1012
1013 static ctl_table brnf_table[] = {
1014         {
1015                 .ctl_name       = NET_BRIDGE_NF_CALL_ARPTABLES,
1016                 .procname       = "bridge-nf-call-arptables",
1017                 .data           = &brnf_call_arptables,
1018                 .maxlen         = sizeof(int),
1019                 .mode           = 0644,
1020                 .proc_handler   = &brnf_sysctl_call_tables,
1021         },
1022         {
1023                 .ctl_name       = NET_BRIDGE_NF_CALL_IPTABLES,
1024                 .procname       = "bridge-nf-call-iptables",
1025                 .data           = &brnf_call_iptables,
1026                 .maxlen         = sizeof(int),
1027                 .mode           = 0644,
1028                 .proc_handler   = &brnf_sysctl_call_tables,
1029         },
1030         {
1031                 .ctl_name       = NET_BRIDGE_NF_CALL_IP6TABLES,
1032                 .procname       = "bridge-nf-call-ip6tables",
1033                 .data           = &brnf_call_ip6tables,
1034                 .maxlen         = sizeof(int),
1035                 .mode           = 0644,
1036                 .proc_handler   = &brnf_sysctl_call_tables,
1037         },
1038         {
1039                 .ctl_name       = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
1040                 .procname       = "bridge-nf-filter-vlan-tagged",
1041                 .data           = &brnf_filter_vlan_tagged,
1042                 .maxlen         = sizeof(int),
1043                 .mode           = 0644,
1044                 .proc_handler   = &brnf_sysctl_call_tables,
1045         },
1046         { .ctl_name = 0 }
1047 };
1048
1049 static ctl_table brnf_bridge_table[] = {
1050         {
1051                 .ctl_name       = NET_BRIDGE,
1052                 .procname       = "bridge",
1053                 .mode           = 0555,
1054                 .child          = brnf_table,
1055         },
1056         { .ctl_name = 0 }
1057 };
1058
1059 static ctl_table brnf_net_table[] = {
1060         {
1061                 .ctl_name       = CTL_NET,
1062                 .procname       = "net",
1063                 .mode           = 0555,
1064                 .child          = brnf_bridge_table,
1065         },
1066         { .ctl_name = 0 }
1067 };
1068 #endif
1069
1070 int br_netfilter_init(void)
1071 {
1072         int i;
1073
1074         for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) {
1075                 int ret;
1076
1077                 if ((ret = nf_register_hook(&br_nf_ops[i])) >= 0)
1078                         continue;
1079
1080                 while (i--)
1081                         nf_unregister_hook(&br_nf_ops[i]);
1082
1083                 return ret;
1084         }
1085
1086 #ifdef CONFIG_SYSCTL
1087         brnf_sysctl_header = register_sysctl_table(brnf_net_table, 0);
1088         if (brnf_sysctl_header == NULL) {
1089                 printk(KERN_WARNING
1090                        "br_netfilter: can't register to sysctl.\n");
1091                 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++)
1092                         nf_unregister_hook(&br_nf_ops[i]);
1093                 return -EFAULT;
1094         }
1095 #endif
1096
1097         printk(KERN_NOTICE "Bridge firewalling registered\n");
1098
1099         return 0;
1100 }
1101
1102 void br_netfilter_fini(void)
1103 {
1104         int i;
1105
1106         for (i = ARRAY_SIZE(br_nf_ops) - 1; i >= 0; i--)
1107                 nf_unregister_hook(&br_nf_ops[i]);
1108 #ifdef CONFIG_SYSCTL
1109         unregister_sysctl_table(brnf_sysctl_header);
1110 #endif
1111 }