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