init from v2.6.32.60
[mach-moxart.git] / net / bridge / br_netfilter.c
blob3072272eca0e922a99996d0250121e19e9798a96
1 /*
2 * Handle firewalling
3 * Linux ethernet bridge
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 * Bart De Schuymer (maintainer) <bdschuym@pandora.be>
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)
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.
21 * Lennert dedicates this file to Kerstin Wurdinger.
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>
41 #include <net/ip.h>
42 #include <net/ipv6.h>
43 #include <net/route.h>
45 #include <asm/uaccess.h>
46 #include "br_private.h"
47 #ifdef CONFIG_SYSCTL
48 #include <linux/sysctl.h>
49 #endif
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)
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 = 0;
62 static int brnf_filter_pppoe_tagged __read_mostly = 0;
63 #else
64 #define brnf_filter_vlan_tagged 0
65 #define brnf_filter_pppoe_tagged 0
66 #endif
68 static inline __be16 vlan_proto(const struct sk_buff *skb)
70 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
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)
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)
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)
88 static inline __be16 pppoe_proto(const struct sk_buff *skb)
90 return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
91 sizeof(struct pppoe_hdr)));
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)
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)
104 static void fake_update_pmtu(struct dst_entry *dst, u32 mtu)
108 static struct dst_ops fake_dst_ops = {
109 .family = AF_INET,
110 .protocol = cpu_to_be16(ETH_P_IP),
111 .update_pmtu = fake_update_pmtu,
112 .entries = ATOMIC_INIT(0),
116 * Initialize bogus route table used to keep netfilter happy.
117 * Currently, we fill in the PMTU entry because netfilter
118 * refragmentation needs it, and the rt_flags entry because
119 * ipt_REJECT needs it. Future netfilter modules might
120 * require us to fill additional fields.
122 void br_netfilter_rtable_init(struct net_bridge *br)
124 struct rtable *rt = &br->fake_rtable;
126 atomic_set(&rt->u.dst.__refcnt, 1);
127 rt->u.dst.dev = br->dev;
128 rt->u.dst.path = &rt->u.dst;
129 rt->u.dst.metrics[RTAX_MTU - 1] = 1500;
130 rt->u.dst.flags = DST_NOXFRM;
131 rt->u.dst.ops = &fake_dst_ops;
134 static inline struct rtable *bridge_parent_rtable(const struct net_device *dev)
136 struct net_bridge_port *port = rcu_dereference(dev->br_port);
138 return port ? &port->br->fake_rtable : NULL;
141 static inline struct net_device *bridge_parent(const struct net_device *dev)
143 struct net_bridge_port *port = rcu_dereference(dev->br_port);
145 return port ? port->br->dev : NULL;
148 static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
150 skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
151 if (likely(skb->nf_bridge))
152 atomic_set(&(skb->nf_bridge->use), 1);
154 return skb->nf_bridge;
157 static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
159 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
161 if (atomic_read(&nf_bridge->use) > 1) {
162 struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
164 if (tmp) {
165 memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
166 atomic_set(&tmp->use, 1);
167 nf_bridge_put(nf_bridge);
169 nf_bridge = tmp;
171 return nf_bridge;
174 static inline void nf_bridge_push_encap_header(struct sk_buff *skb)
176 unsigned int len = nf_bridge_encap_header_len(skb);
178 skb_push(skb, len);
179 skb->network_header -= len;
182 static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
184 unsigned int len = nf_bridge_encap_header_len(skb);
186 skb_pull(skb, len);
187 skb->network_header += len;
190 static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
192 unsigned int len = nf_bridge_encap_header_len(skb);
194 skb_pull_rcsum(skb, len);
195 skb->network_header += len;
198 static inline void nf_bridge_save_header(struct sk_buff *skb)
200 int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
202 skb_copy_from_linear_data_offset(skb, -header_size,
203 skb->nf_bridge->data, header_size);
207 * When forwarding bridge frames, we save a copy of the original
208 * header before processing.
210 int nf_bridge_copy_header(struct sk_buff *skb)
212 int err;
213 int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
215 err = skb_cow_head(skb, header_size);
216 if (err)
217 return err;
219 skb_copy_to_linear_data_offset(skb, -header_size,
220 skb->nf_bridge->data, header_size);
221 __skb_push(skb, nf_bridge_encap_header_len(skb));
222 return 0;
225 /* PF_BRIDGE/PRE_ROUTING *********************************************/
226 /* Undo the changes made for ip6tables PREROUTING and continue the
227 * bridge PRE_ROUTING hook. */
228 static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
230 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
231 struct rtable *rt;
233 if (nf_bridge->mask & BRNF_PKT_TYPE) {
234 skb->pkt_type = PACKET_OTHERHOST;
235 nf_bridge->mask ^= BRNF_PKT_TYPE;
237 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
239 rt = bridge_parent_rtable(nf_bridge->physindev);
240 if (!rt) {
241 kfree_skb(skb);
242 return 0;
244 dst_hold(&rt->u.dst);
245 skb_dst_set(skb, &rt->u.dst);
247 skb->dev = nf_bridge->physindev;
248 nf_bridge_push_encap_header(skb);
249 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
250 br_handle_frame_finish, 1);
252 return 0;
255 static void __br_dnat_complain(void)
257 static unsigned long last_complaint;
259 if (jiffies - last_complaint >= 5 * HZ) {
260 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
261 "forwarding to be enabled\n");
262 last_complaint = jiffies;
266 /* This requires some explaining. If DNAT has taken place,
267 * we will need to fix up the destination Ethernet address,
268 * and this is a tricky process.
270 * There are two cases to consider:
271 * 1. The packet was DNAT'ed to a device in the same bridge
272 * port group as it was received on. We can still bridge
273 * the packet.
274 * 2. The packet was DNAT'ed to a different device, either
275 * a non-bridged device or another bridge port group.
276 * The packet will need to be routed.
278 * The correct way of distinguishing between these two cases is to
279 * call ip_route_input() and to look at skb->dst->dev, which is
280 * changed to the destination device if ip_route_input() succeeds.
282 * Let us first consider the case that ip_route_input() succeeds:
284 * If skb->dst->dev equals the logical bridge device the packet
285 * came in on, we can consider this bridging. The packet is passed
286 * through the neighbour output function to build a new destination
287 * MAC address, which will make the packet enter br_nf_local_out()
288 * not much later. In that function it is assured that the iptables
289 * FORWARD chain is traversed for the packet.
291 * Otherwise, the packet is considered to be routed and we just
292 * change the destination MAC address so that the packet will
293 * later be passed up to the IP stack to be routed. For a redirected
294 * packet, ip_route_input() will give back the localhost as output device,
295 * which differs from the bridge device.
297 * Let us now consider the case that ip_route_input() fails:
299 * This can be because the destination address is martian, in which case
300 * the packet will be dropped.
301 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
302 * will fail, while __ip_route_output_key() will return success. The source
303 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
304 * thinks we're handling a locally generated packet and won't care
305 * if IP forwarding is allowed. We send a warning message to the users's
306 * log telling her to put IP forwarding on.
308 * ip_route_input() will also fail if there is no route available.
309 * In that case we just drop the packet.
311 * --Lennert, 20020411
312 * --Bart, 20020416 (updated)
313 * --Bart, 20021007 (updated)
314 * --Bart, 20062711 (updated) */
315 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
317 if (skb->pkt_type == PACKET_OTHERHOST) {
318 skb->pkt_type = PACKET_HOST;
319 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
321 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
323 skb->dev = bridge_parent(skb->dev);
324 if (skb->dev) {
325 struct dst_entry *dst = skb_dst(skb);
327 nf_bridge_pull_encap_header(skb);
329 if (dst->hh)
330 return neigh_hh_output(dst->hh, skb);
331 else if (dst->neighbour)
332 return dst->neighbour->output(skb);
334 kfree_skb(skb);
335 return 0;
338 static int br_nf_pre_routing_finish(struct sk_buff *skb)
340 struct net_device *dev = skb->dev;
341 struct iphdr *iph = ip_hdr(skb);
342 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
343 struct rtable *rt;
344 int err;
346 if (nf_bridge->mask & BRNF_PKT_TYPE) {
347 skb->pkt_type = PACKET_OTHERHOST;
348 nf_bridge->mask ^= BRNF_PKT_TYPE;
350 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
351 if (dnat_took_place(skb)) {
352 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
353 struct flowi fl = {
354 .nl_u = {
355 .ip4_u = {
356 .daddr = iph->daddr,
357 .saddr = 0,
358 .tos = RT_TOS(iph->tos) },
360 .proto = 0,
362 struct in_device *in_dev = __in_dev_get_rcu(dev);
364 /* If err equals -EHOSTUNREACH the error is due to a
365 * martian destination or due to the fact that
366 * forwarding is disabled. For most martian packets,
367 * ip_route_output_key() will fail. It won't fail for 2 types of
368 * martian destinations: loopback destinations and destination
369 * 0.0.0.0. In both cases the packet will be dropped because the
370 * destination is the loopback device and not the bridge. */
371 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
372 goto free_skb;
374 if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
375 /* - Bridged-and-DNAT'ed traffic doesn't
376 * require ip_forwarding. */
377 if (((struct dst_entry *)rt)->dev == dev) {
378 skb_dst_set(skb, (struct dst_entry *)rt);
379 goto bridged_dnat;
381 /* we are sure that forwarding is disabled, so printing
382 * this message is no problem. Note that the packet could
383 * still have a martian destination address, in which case
384 * the packet could be dropped even if forwarding were enabled */
385 __br_dnat_complain();
386 dst_release((struct dst_entry *)rt);
388 free_skb:
389 kfree_skb(skb);
390 return 0;
391 } else {
392 if (skb_dst(skb)->dev == dev) {
393 bridged_dnat:
394 /* Tell br_nf_local_out this is a
395 * bridged frame */
396 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
397 skb->dev = nf_bridge->physindev;
398 nf_bridge_push_encap_header(skb);
399 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
400 skb, skb->dev, NULL,
401 br_nf_pre_routing_finish_bridge,
403 return 0;
405 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
406 skb->pkt_type = PACKET_HOST;
408 } else {
409 rt = bridge_parent_rtable(nf_bridge->physindev);
410 if (!rt) {
411 kfree_skb(skb);
412 return 0;
414 dst_hold(&rt->u.dst);
415 skb_dst_set(skb, &rt->u.dst);
418 skb->dev = nf_bridge->physindev;
419 nf_bridge_push_encap_header(skb);
420 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
421 br_handle_frame_finish, 1);
423 return 0;
426 /* Some common code for IPv4/IPv6 */
427 static struct net_device *setup_pre_routing(struct sk_buff *skb)
429 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
431 if (skb->pkt_type == PACKET_OTHERHOST) {
432 skb->pkt_type = PACKET_HOST;
433 nf_bridge->mask |= BRNF_PKT_TYPE;
436 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
437 nf_bridge->physindev = skb->dev;
438 skb->dev = bridge_parent(skb->dev);
440 return skb->dev;
443 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
444 static int check_hbh_len(struct sk_buff *skb)
446 unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
447 u32 pkt_len;
448 const unsigned char *nh = skb_network_header(skb);
449 int off = raw - nh;
450 int len = (raw[1] + 1) << 3;
452 if ((raw + len) - skb->data > skb_headlen(skb))
453 goto bad;
455 off += 2;
456 len -= 2;
458 while (len > 0) {
459 int optlen = nh[off + 1] + 2;
461 switch (nh[off]) {
462 case IPV6_TLV_PAD0:
463 optlen = 1;
464 break;
466 case IPV6_TLV_PADN:
467 break;
469 case IPV6_TLV_JUMBO:
470 if (nh[off + 1] != 4 || (off & 3) != 2)
471 goto bad;
472 pkt_len = ntohl(*(__be32 *) (nh + off + 2));
473 if (pkt_len <= IPV6_MAXPLEN ||
474 ipv6_hdr(skb)->payload_len)
475 goto bad;
476 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
477 goto bad;
478 if (pskb_trim_rcsum(skb,
479 pkt_len + sizeof(struct ipv6hdr)))
480 goto bad;
481 nh = skb_network_header(skb);
482 break;
483 default:
484 if (optlen > len)
485 goto bad;
486 break;
488 off += optlen;
489 len -= optlen;
491 if (len == 0)
492 return 0;
493 bad:
494 return -1;
498 /* Replicate the checks that IPv6 does on packet reception and pass the packet
499 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
500 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
501 struct sk_buff *skb,
502 const struct net_device *in,
503 const struct net_device *out,
504 int (*okfn)(struct sk_buff *))
506 struct ipv6hdr *hdr;
507 u32 pkt_len;
509 if (skb->len < sizeof(struct ipv6hdr))
510 goto inhdr_error;
512 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
513 goto inhdr_error;
515 hdr = ipv6_hdr(skb);
517 if (hdr->version != 6)
518 goto inhdr_error;
520 pkt_len = ntohs(hdr->payload_len);
522 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
523 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
524 goto inhdr_error;
525 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
526 goto inhdr_error;
528 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
529 goto inhdr_error;
531 nf_bridge_put(skb->nf_bridge);
532 if (!nf_bridge_alloc(skb))
533 return NF_DROP;
534 if (!setup_pre_routing(skb))
535 return NF_DROP;
537 NF_HOOK(PF_INET6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
538 br_nf_pre_routing_finish_ipv6);
540 return NF_STOLEN;
542 inhdr_error:
543 return NF_DROP;
546 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
547 * Replicate the checks that IPv4 does on packet reception.
548 * Set skb->dev to the bridge device (i.e. parent of the
549 * receiving device) to make netfilter happy, the REDIRECT
550 * target in particular. Save the original destination IP
551 * address to be able to detect DNAT afterwards. */
552 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff *skb,
553 const struct net_device *in,
554 const struct net_device *out,
555 int (*okfn)(struct sk_buff *))
557 struct iphdr *iph;
558 __u32 len = nf_bridge_encap_header_len(skb);
560 if (unlikely(!pskb_may_pull(skb, len)))
561 goto out;
563 if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
564 IS_PPPOE_IPV6(skb)) {
565 #ifdef CONFIG_SYSCTL
566 if (!brnf_call_ip6tables)
567 return NF_ACCEPT;
568 #endif
569 nf_bridge_pull_encap_header_rcsum(skb);
570 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
572 #ifdef CONFIG_SYSCTL
573 if (!brnf_call_iptables)
574 return NF_ACCEPT;
575 #endif
577 if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb) &&
578 !IS_PPPOE_IP(skb))
579 return NF_ACCEPT;
581 nf_bridge_pull_encap_header_rcsum(skb);
583 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
584 goto inhdr_error;
586 iph = ip_hdr(skb);
587 if (iph->ihl < 5 || iph->version != 4)
588 goto inhdr_error;
590 if (!pskb_may_pull(skb, 4 * iph->ihl))
591 goto inhdr_error;
593 iph = ip_hdr(skb);
594 if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
595 goto inhdr_error;
597 len = ntohs(iph->tot_len);
598 if (skb->len < len || len < 4 * iph->ihl)
599 goto inhdr_error;
601 pskb_trim_rcsum(skb, len);
603 /* BUG: Should really parse the IP options here. */
604 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
606 nf_bridge_put(skb->nf_bridge);
607 if (!nf_bridge_alloc(skb))
608 return NF_DROP;
609 if (!setup_pre_routing(skb))
610 return NF_DROP;
611 store_orig_dstaddr(skb);
613 NF_HOOK(PF_INET, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
614 br_nf_pre_routing_finish);
616 return NF_STOLEN;
618 inhdr_error:
619 // IP_INC_STATS_BH(IpInHdrErrors);
620 out:
621 return NF_DROP;
625 /* PF_BRIDGE/LOCAL_IN ************************************************/
626 /* The packet is locally destined, which requires a real
627 * dst_entry, so detach the fake one. On the way up, the
628 * packet would pass through PRE_ROUTING again (which already
629 * took place when the packet entered the bridge), but we
630 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
631 * prevent this from happening. */
632 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff *skb,
633 const struct net_device *in,
634 const struct net_device *out,
635 int (*okfn)(struct sk_buff *))
637 struct rtable *rt = skb_rtable(skb);
639 if (rt && rt == bridge_parent_rtable(in))
640 skb_dst_drop(skb);
642 return NF_ACCEPT;
645 /* PF_BRIDGE/FORWARD *************************************************/
646 static int br_nf_forward_finish(struct sk_buff *skb)
648 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
649 struct net_device *in;
651 if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
652 in = nf_bridge->physindev;
653 if (nf_bridge->mask & BRNF_PKT_TYPE) {
654 skb->pkt_type = PACKET_OTHERHOST;
655 nf_bridge->mask ^= BRNF_PKT_TYPE;
657 } else {
658 in = *((struct net_device **)(skb->cb));
660 nf_bridge_push_encap_header(skb);
661 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
662 skb->dev, br_forward_finish, 1);
663 return 0;
666 /* This is the 'purely bridged' case. For IP, we pass the packet to
667 * netfilter with indev and outdev set to the bridge device,
668 * but we are still able to filter on the 'real' indev/outdev
669 * because of the physdev module. For ARP, indev and outdev are the
670 * bridge ports. */
671 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
672 const struct net_device *in,
673 const struct net_device *out,
674 int (*okfn)(struct sk_buff *))
676 struct nf_bridge_info *nf_bridge;
677 struct net_device *parent;
678 u_int8_t pf;
680 if (!skb->nf_bridge)
681 return NF_ACCEPT;
683 /* Need exclusive nf_bridge_info since we might have multiple
684 * different physoutdevs. */
685 if (!nf_bridge_unshare(skb))
686 return NF_DROP;
688 parent = bridge_parent(out);
689 if (!parent)
690 return NF_DROP;
692 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
693 IS_PPPOE_IP(skb))
694 pf = PF_INET;
695 else if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
696 IS_PPPOE_IPV6(skb))
697 pf = PF_INET6;
698 else
699 return NF_ACCEPT;
701 nf_bridge_pull_encap_header(skb);
703 nf_bridge = skb->nf_bridge;
704 if (skb->pkt_type == PACKET_OTHERHOST) {
705 skb->pkt_type = PACKET_HOST;
706 nf_bridge->mask |= BRNF_PKT_TYPE;
709 /* The physdev module checks on this */
710 nf_bridge->mask |= BRNF_BRIDGED;
711 nf_bridge->physoutdev = skb->dev;
713 NF_HOOK(pf, NF_INET_FORWARD, skb, bridge_parent(in), parent,
714 br_nf_forward_finish);
716 return NF_STOLEN;
719 static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff *skb,
720 const struct net_device *in,
721 const struct net_device *out,
722 int (*okfn)(struct sk_buff *))
724 struct net_device **d = (struct net_device **)(skb->cb);
726 #ifdef CONFIG_SYSCTL
727 if (!brnf_call_arptables)
728 return NF_ACCEPT;
729 #endif
731 if (skb->protocol != htons(ETH_P_ARP)) {
732 if (!IS_VLAN_ARP(skb))
733 return NF_ACCEPT;
734 nf_bridge_pull_encap_header(skb);
737 if (arp_hdr(skb)->ar_pln != 4) {
738 if (IS_VLAN_ARP(skb))
739 nf_bridge_push_encap_header(skb);
740 return NF_ACCEPT;
742 *d = (struct net_device *)in;
743 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
744 (struct net_device *)out, br_nf_forward_finish);
746 return NF_STOLEN;
749 /* PF_BRIDGE/LOCAL_OUT ***********************************************
751 * This function sees both locally originated IP packets and forwarded
752 * IP packets (in both cases the destination device is a bridge
753 * device). It also sees bridged-and-DNAT'ed packets.
755 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
756 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
757 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
758 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
759 * will be executed.
761 static unsigned int br_nf_local_out(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 *))
766 struct net_device *realindev;
767 struct nf_bridge_info *nf_bridge;
769 if (!skb->nf_bridge)
770 return NF_ACCEPT;
772 /* Need exclusive nf_bridge_info since we might have multiple
773 * different physoutdevs. */
774 if (!nf_bridge_unshare(skb))
775 return NF_DROP;
777 nf_bridge = skb->nf_bridge;
778 if (!(nf_bridge->mask & BRNF_BRIDGED_DNAT))
779 return NF_ACCEPT;
781 /* Bridged, take PF_BRIDGE/FORWARD.
782 * (see big note in front of br_nf_pre_routing_finish) */
783 nf_bridge->physoutdev = skb->dev;
784 realindev = nf_bridge->physindev;
786 if (nf_bridge->mask & BRNF_PKT_TYPE) {
787 skb->pkt_type = PACKET_OTHERHOST;
788 nf_bridge->mask ^= BRNF_PKT_TYPE;
790 nf_bridge_push_encap_header(skb);
792 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev, skb->dev,
793 br_forward_finish);
794 return NF_STOLEN;
797 #if defined(CONFIG_NF_CONNTRACK_IPV4) || defined(CONFIG_NF_CONNTRACK_IPV4_MODULE)
798 static int br_nf_dev_queue_xmit(struct sk_buff *skb)
800 if (skb->nfct != NULL &&
801 (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb)) &&
802 skb->len > skb->dev->mtu &&
803 !skb_is_gso(skb)) {
804 /* BUG: Should really parse the IP options here. */
805 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
806 return ip_fragment(skb, br_dev_queue_push_xmit);
807 } else
808 return br_dev_queue_push_xmit(skb);
810 #else
811 static int br_nf_dev_queue_xmit(struct sk_buff *skb)
813 return br_dev_queue_push_xmit(skb);
815 #endif
817 /* PF_BRIDGE/POST_ROUTING ********************************************/
818 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
819 const struct net_device *in,
820 const struct net_device *out,
821 int (*okfn)(struct sk_buff *))
823 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
824 struct net_device *realoutdev = bridge_parent(skb->dev);
825 u_int8_t pf;
827 #ifdef CONFIG_NETFILTER_DEBUG
828 /* Be very paranoid. This probably won't happen anymore, but let's
829 * keep the check just to be sure... */
830 if (skb_mac_header(skb) < skb->head ||
831 skb_mac_header(skb) + ETH_HLEN > skb->data) {
832 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
833 "bad mac.raw pointer.\n");
834 goto print_error;
836 #endif
838 if (!nf_bridge)
839 return NF_ACCEPT;
841 if (!(nf_bridge->mask & (BRNF_BRIDGED | BRNF_BRIDGED_DNAT)))
842 return NF_ACCEPT;
844 if (!realoutdev)
845 return NF_DROP;
847 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
848 IS_PPPOE_IP(skb))
849 pf = PF_INET;
850 else if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
851 IS_PPPOE_IPV6(skb))
852 pf = PF_INET6;
853 else
854 return NF_ACCEPT;
856 #ifdef CONFIG_NETFILTER_DEBUG
857 if (skb_dst(skb) == NULL) {
858 printk(KERN_INFO "br_netfilter post_routing: skb->dst == NULL\n");
859 goto print_error;
861 #endif
863 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
864 * about the value of skb->pkt_type. */
865 if (skb->pkt_type == PACKET_OTHERHOST) {
866 skb->pkt_type = PACKET_HOST;
867 nf_bridge->mask |= BRNF_PKT_TYPE;
870 nf_bridge_pull_encap_header(skb);
871 nf_bridge_save_header(skb);
873 NF_HOOK(pf, NF_INET_POST_ROUTING, skb, NULL, realoutdev,
874 br_nf_dev_queue_xmit);
876 return NF_STOLEN;
878 #ifdef CONFIG_NETFILTER_DEBUG
879 print_error:
880 if (skb->dev != NULL) {
881 printk("[%s]", skb->dev->name);
882 if (realoutdev)
883 printk("[%s]", realoutdev->name);
885 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb_mac_header(skb),
886 skb->data);
887 dump_stack();
888 return NF_ACCEPT;
889 #endif
892 /* IP/SABOTAGE *****************************************************/
893 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
894 * for the second time. */
895 static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff *skb,
896 const struct net_device *in,
897 const struct net_device *out,
898 int (*okfn)(struct sk_buff *))
900 if (skb->nf_bridge &&
901 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
902 return NF_STOP;
905 return NF_ACCEPT;
908 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
909 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
910 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
911 * ip_refrag() can return NF_STOLEN. */
912 static struct nf_hook_ops br_nf_ops[] __read_mostly = {
914 .hook = br_nf_pre_routing,
915 .owner = THIS_MODULE,
916 .pf = PF_BRIDGE,
917 .hooknum = NF_BR_PRE_ROUTING,
918 .priority = NF_BR_PRI_BRNF,
921 .hook = br_nf_local_in,
922 .owner = THIS_MODULE,
923 .pf = PF_BRIDGE,
924 .hooknum = NF_BR_LOCAL_IN,
925 .priority = NF_BR_PRI_BRNF,
928 .hook = br_nf_forward_ip,
929 .owner = THIS_MODULE,
930 .pf = PF_BRIDGE,
931 .hooknum = NF_BR_FORWARD,
932 .priority = NF_BR_PRI_BRNF - 1,
935 .hook = br_nf_forward_arp,
936 .owner = THIS_MODULE,
937 .pf = PF_BRIDGE,
938 .hooknum = NF_BR_FORWARD,
939 .priority = NF_BR_PRI_BRNF,
942 .hook = br_nf_local_out,
943 .owner = THIS_MODULE,
944 .pf = PF_BRIDGE,
945 .hooknum = NF_BR_LOCAL_OUT,
946 .priority = NF_BR_PRI_FIRST,
949 .hook = br_nf_post_routing,
950 .owner = THIS_MODULE,
951 .pf = PF_BRIDGE,
952 .hooknum = NF_BR_POST_ROUTING,
953 .priority = NF_BR_PRI_LAST,
956 .hook = ip_sabotage_in,
957 .owner = THIS_MODULE,
958 .pf = PF_INET,
959 .hooknum = NF_INET_PRE_ROUTING,
960 .priority = NF_IP_PRI_FIRST,
963 .hook = ip_sabotage_in,
964 .owner = THIS_MODULE,
965 .pf = PF_INET6,
966 .hooknum = NF_INET_PRE_ROUTING,
967 .priority = NF_IP6_PRI_FIRST,
971 #ifdef CONFIG_SYSCTL
972 static
973 int brnf_sysctl_call_tables(ctl_table * ctl, int write,
974 void __user * buffer, size_t * lenp, loff_t * ppos)
976 int ret;
978 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
980 if (write && *(int *)(ctl->data))
981 *(int *)(ctl->data) = 1;
982 return ret;
985 static ctl_table brnf_table[] = {
987 .procname = "bridge-nf-call-arptables",
988 .data = &brnf_call_arptables,
989 .maxlen = sizeof(int),
990 .mode = 0644,
991 .proc_handler = brnf_sysctl_call_tables,
994 .procname = "bridge-nf-call-iptables",
995 .data = &brnf_call_iptables,
996 .maxlen = sizeof(int),
997 .mode = 0644,
998 .proc_handler = brnf_sysctl_call_tables,
1001 .procname = "bridge-nf-call-ip6tables",
1002 .data = &brnf_call_ip6tables,
1003 .maxlen = sizeof(int),
1004 .mode = 0644,
1005 .proc_handler = brnf_sysctl_call_tables,
1008 .procname = "bridge-nf-filter-vlan-tagged",
1009 .data = &brnf_filter_vlan_tagged,
1010 .maxlen = sizeof(int),
1011 .mode = 0644,
1012 .proc_handler = brnf_sysctl_call_tables,
1015 .procname = "bridge-nf-filter-pppoe-tagged",
1016 .data = &brnf_filter_pppoe_tagged,
1017 .maxlen = sizeof(int),
1018 .mode = 0644,
1019 .proc_handler = brnf_sysctl_call_tables,
1021 { .ctl_name = 0 }
1024 static struct ctl_path brnf_path[] = {
1025 { .procname = "net", .ctl_name = CTL_NET, },
1026 { .procname = "bridge", .ctl_name = NET_BRIDGE, },
1029 #endif
1031 int __init br_netfilter_init(void)
1033 int ret;
1035 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1036 if (ret < 0)
1037 return ret;
1038 #ifdef CONFIG_SYSCTL
1039 brnf_sysctl_header = register_sysctl_paths(brnf_path, brnf_table);
1040 if (brnf_sysctl_header == NULL) {
1041 printk(KERN_WARNING
1042 "br_netfilter: can't register to sysctl.\n");
1043 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1044 return -ENOMEM;
1046 #endif
1047 printk(KERN_NOTICE "Bridge firewalling registered\n");
1048 return 0;
1051 void br_netfilter_fini(void)
1053 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1054 #ifdef CONFIG_SYSCTL
1055 unregister_sysctl_table(brnf_sysctl_header);
1056 #endif