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