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