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