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