solos: Don't clear config registers at startup
[linux-2.6/linux-2.6-openrd.git] / net / bridge / br_netfilter.c
blobcf754ace0b75ba8c57e35e9420cfd97137ff88f9
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 = __constant_htons(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;
232 if (nf_bridge->mask & BRNF_PKT_TYPE) {
233 skb->pkt_type = PACKET_OTHERHOST;
234 nf_bridge->mask ^= BRNF_PKT_TYPE;
236 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
238 skb->rtable = bridge_parent_rtable(nf_bridge->physindev);
239 if (!skb->rtable) {
240 kfree_skb(skb);
241 return 0;
243 dst_hold(&skb->rtable->u.dst);
245 skb->dev = nf_bridge->physindev;
246 nf_bridge_push_encap_header(skb);
247 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
248 br_handle_frame_finish, 1);
250 return 0;
253 static void __br_dnat_complain(void)
255 static unsigned long last_complaint;
257 if (jiffies - last_complaint >= 5 * HZ) {
258 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
259 "forwarding to be enabled\n");
260 last_complaint = jiffies;
264 /* This requires some explaining. If DNAT has taken place,
265 * we will need to fix up the destination Ethernet address,
266 * and this is a tricky process.
268 * There are two cases to consider:
269 * 1. The packet was DNAT'ed to a device in the same bridge
270 * port group as it was received on. We can still bridge
271 * the packet.
272 * 2. The packet was DNAT'ed to a different device, either
273 * a non-bridged device or another bridge port group.
274 * The packet will need to be routed.
276 * The correct way of distinguishing between these two cases is to
277 * call ip_route_input() and to look at skb->dst->dev, which is
278 * changed to the destination device if ip_route_input() succeeds.
280 * Let us first consider the case that ip_route_input() succeeds:
282 * If skb->dst->dev equals the logical bridge device the packet
283 * came in on, we can consider this bridging. The packet is passed
284 * through the neighbour output function to build a new destination
285 * MAC address, which will make the packet enter br_nf_local_out()
286 * not much later. In that function it is assured that the iptables
287 * FORWARD chain is traversed for the packet.
289 * Otherwise, the packet is considered to be routed and we just
290 * change the destination MAC address so that the packet will
291 * later be passed up to the IP stack to be routed. For a redirected
292 * packet, ip_route_input() will give back the localhost as output device,
293 * which differs from the bridge device.
295 * Let us now consider the case that ip_route_input() fails:
297 * This can be because the destination address is martian, in which case
298 * the packet will be dropped.
299 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
300 * will fail, while __ip_route_output_key() will return success. The source
301 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
302 * thinks we're handling a locally generated packet and won't care
303 * if IP forwarding is allowed. We send a warning message to the users's
304 * log telling her to put IP forwarding on.
306 * ip_route_input() will also fail if there is no route available.
307 * In that case we just drop the packet.
309 * --Lennert, 20020411
310 * --Bart, 20020416 (updated)
311 * --Bart, 20021007 (updated)
312 * --Bart, 20062711 (updated) */
313 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
315 if (skb->pkt_type == PACKET_OTHERHOST) {
316 skb->pkt_type = PACKET_HOST;
317 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
319 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
321 skb->dev = bridge_parent(skb->dev);
322 if (skb->dev) {
323 struct dst_entry *dst = skb->dst;
325 nf_bridge_pull_encap_header(skb);
327 if (dst->hh)
328 return neigh_hh_output(dst->hh, skb);
329 else if (dst->neighbour)
330 return dst->neighbour->output(skb);
332 kfree_skb(skb);
333 return 0;
336 static int br_nf_pre_routing_finish(struct sk_buff *skb)
338 struct net_device *dev = skb->dev;
339 struct iphdr *iph = ip_hdr(skb);
340 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
341 int err;
343 if (nf_bridge->mask & BRNF_PKT_TYPE) {
344 skb->pkt_type = PACKET_OTHERHOST;
345 nf_bridge->mask ^= BRNF_PKT_TYPE;
347 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
348 if (dnat_took_place(skb)) {
349 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
350 struct rtable *rt;
351 struct flowi fl = {
352 .nl_u = {
353 .ip4_u = {
354 .daddr = iph->daddr,
355 .saddr = 0,
356 .tos = RT_TOS(iph->tos) },
358 .proto = 0,
360 struct in_device *in_dev = in_dev_get(dev);
362 /* If err equals -EHOSTUNREACH the error is due to a
363 * martian destination or due to the fact that
364 * forwarding is disabled. For most martian packets,
365 * ip_route_output_key() will fail. It won't fail for 2 types of
366 * martian destinations: loopback destinations and destination
367 * 0.0.0.0. In both cases the packet will be dropped because the
368 * destination is the loopback device and not the bridge. */
369 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
370 goto free_skb;
372 if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
373 /* - Bridged-and-DNAT'ed traffic doesn't
374 * require ip_forwarding. */
375 if (((struct dst_entry *)rt)->dev == dev) {
376 skb->dst = (struct dst_entry *)rt;
377 goto bridged_dnat;
379 /* we are sure that forwarding is disabled, so printing
380 * this message is no problem. Note that the packet could
381 * still have a martian destination address, in which case
382 * the packet could be dropped even if forwarding were enabled */
383 __br_dnat_complain();
384 dst_release((struct dst_entry *)rt);
386 free_skb:
387 kfree_skb(skb);
388 return 0;
389 } else {
390 if (skb->dst->dev == dev) {
391 bridged_dnat:
392 /* Tell br_nf_local_out this is a
393 * bridged frame */
394 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
395 skb->dev = nf_bridge->physindev;
396 nf_bridge_push_encap_header(skb);
397 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
398 skb, skb->dev, NULL,
399 br_nf_pre_routing_finish_bridge,
401 return 0;
403 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
404 skb->pkt_type = PACKET_HOST;
406 } else {
407 skb->rtable = bridge_parent_rtable(nf_bridge->physindev);
408 if (!skb->rtable) {
409 kfree_skb(skb);
410 return 0;
412 dst_hold(&skb->rtable->u.dst);
415 skb->dev = nf_bridge->physindev;
416 nf_bridge_push_encap_header(skb);
417 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
418 br_handle_frame_finish, 1);
420 return 0;
423 /* Some common code for IPv4/IPv6 */
424 static struct net_device *setup_pre_routing(struct sk_buff *skb)
426 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
428 if (skb->pkt_type == PACKET_OTHERHOST) {
429 skb->pkt_type = PACKET_HOST;
430 nf_bridge->mask |= BRNF_PKT_TYPE;
433 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
434 nf_bridge->physindev = skb->dev;
435 skb->dev = bridge_parent(skb->dev);
437 return skb->dev;
440 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
441 static int check_hbh_len(struct sk_buff *skb)
443 unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
444 u32 pkt_len;
445 const unsigned char *nh = skb_network_header(skb);
446 int off = raw - nh;
447 int len = (raw[1] + 1) << 3;
449 if ((raw + len) - skb->data > skb_headlen(skb))
450 goto bad;
452 off += 2;
453 len -= 2;
455 while (len > 0) {
456 int optlen = nh[off + 1] + 2;
458 switch (nh[off]) {
459 case IPV6_TLV_PAD0:
460 optlen = 1;
461 break;
463 case IPV6_TLV_PADN:
464 break;
466 case IPV6_TLV_JUMBO:
467 if (nh[off + 1] != 4 || (off & 3) != 2)
468 goto bad;
469 pkt_len = ntohl(*(__be32 *) (nh + off + 2));
470 if (pkt_len <= IPV6_MAXPLEN ||
471 ipv6_hdr(skb)->payload_len)
472 goto bad;
473 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
474 goto bad;
475 if (pskb_trim_rcsum(skb,
476 pkt_len + sizeof(struct ipv6hdr)))
477 goto bad;
478 nh = skb_network_header(skb);
479 break;
480 default:
481 if (optlen > len)
482 goto bad;
483 break;
485 off += optlen;
486 len -= optlen;
488 if (len == 0)
489 return 0;
490 bad:
491 return -1;
495 /* Replicate the checks that IPv6 does on packet reception and pass the packet
496 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
497 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
498 struct sk_buff *skb,
499 const struct net_device *in,
500 const struct net_device *out,
501 int (*okfn)(struct sk_buff *))
503 struct ipv6hdr *hdr;
504 u32 pkt_len;
506 if (skb->len < sizeof(struct ipv6hdr))
507 goto inhdr_error;
509 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
510 goto inhdr_error;
512 hdr = ipv6_hdr(skb);
514 if (hdr->version != 6)
515 goto inhdr_error;
517 pkt_len = ntohs(hdr->payload_len);
519 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
520 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
521 goto inhdr_error;
522 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
523 goto inhdr_error;
525 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
526 goto inhdr_error;
528 nf_bridge_put(skb->nf_bridge);
529 if (!nf_bridge_alloc(skb))
530 return NF_DROP;
531 if (!setup_pre_routing(skb))
532 return NF_DROP;
534 NF_HOOK(PF_INET6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
535 br_nf_pre_routing_finish_ipv6);
537 return NF_STOLEN;
539 inhdr_error:
540 return NF_DROP;
543 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
544 * Replicate the checks that IPv4 does on packet reception.
545 * Set skb->dev to the bridge device (i.e. parent of the
546 * receiving device) to make netfilter happy, the REDIRECT
547 * target in particular. Save the original destination IP
548 * address to be able to detect DNAT afterwards. */
549 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff *skb,
550 const struct net_device *in,
551 const struct net_device *out,
552 int (*okfn)(struct sk_buff *))
554 struct iphdr *iph;
555 __u32 len = nf_bridge_encap_header_len(skb);
557 if (unlikely(!pskb_may_pull(skb, len)))
558 goto out;
560 if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
561 IS_PPPOE_IPV6(skb)) {
562 #ifdef CONFIG_SYSCTL
563 if (!brnf_call_ip6tables)
564 return NF_ACCEPT;
565 #endif
566 nf_bridge_pull_encap_header_rcsum(skb);
567 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
569 #ifdef CONFIG_SYSCTL
570 if (!brnf_call_iptables)
571 return NF_ACCEPT;
572 #endif
574 if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb) &&
575 !IS_PPPOE_IP(skb))
576 return NF_ACCEPT;
578 nf_bridge_pull_encap_header_rcsum(skb);
580 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
581 goto inhdr_error;
583 iph = ip_hdr(skb);
584 if (iph->ihl < 5 || iph->version != 4)
585 goto inhdr_error;
587 if (!pskb_may_pull(skb, 4 * iph->ihl))
588 goto inhdr_error;
590 iph = ip_hdr(skb);
591 if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
592 goto inhdr_error;
594 len = ntohs(iph->tot_len);
595 if (skb->len < len || len < 4 * iph->ihl)
596 goto inhdr_error;
598 pskb_trim_rcsum(skb, len);
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);
607 NF_HOOK(PF_INET, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
608 br_nf_pre_routing_finish);
610 return NF_STOLEN;
612 inhdr_error:
613 // IP_INC_STATS_BH(IpInHdrErrors);
614 out:
615 return NF_DROP;
619 /* PF_BRIDGE/LOCAL_IN ************************************************/
620 /* The packet is locally destined, which requires a real
621 * dst_entry, so detach the fake one. On the way up, the
622 * packet would pass through PRE_ROUTING again (which already
623 * took place when the packet entered the bridge), but we
624 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
625 * prevent this from happening. */
626 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff *skb,
627 const struct net_device *in,
628 const struct net_device *out,
629 int (*okfn)(struct sk_buff *))
631 if (skb->rtable && skb->rtable == bridge_parent_rtable(in)) {
632 dst_release(&skb->rtable->u.dst);
633 skb->rtable = NULL;
636 return NF_ACCEPT;
639 /* PF_BRIDGE/FORWARD *************************************************/
640 static int br_nf_forward_finish(struct sk_buff *skb)
642 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
643 struct net_device *in;
645 if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
646 in = nf_bridge->physindev;
647 if (nf_bridge->mask & BRNF_PKT_TYPE) {
648 skb->pkt_type = PACKET_OTHERHOST;
649 nf_bridge->mask ^= BRNF_PKT_TYPE;
651 } else {
652 in = *((struct net_device **)(skb->cb));
654 nf_bridge_push_encap_header(skb);
655 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
656 skb->dev, br_forward_finish, 1);
657 return 0;
660 /* This is the 'purely bridged' case. For IP, we pass the packet to
661 * netfilter with indev and outdev set to the bridge device,
662 * but we are still able to filter on the 'real' indev/outdev
663 * because of the physdev module. For ARP, indev and outdev are the
664 * bridge ports. */
665 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
666 const struct net_device *in,
667 const struct net_device *out,
668 int (*okfn)(struct sk_buff *))
670 struct nf_bridge_info *nf_bridge;
671 struct net_device *parent;
672 u_int8_t pf;
674 if (!skb->nf_bridge)
675 return NF_ACCEPT;
677 /* Need exclusive nf_bridge_info since we might have multiple
678 * different physoutdevs. */
679 if (!nf_bridge_unshare(skb))
680 return NF_DROP;
682 parent = bridge_parent(out);
683 if (!parent)
684 return NF_DROP;
686 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
687 IS_PPPOE_IP(skb))
688 pf = PF_INET;
689 else if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
690 IS_PPPOE_IPV6(skb))
691 pf = PF_INET6;
692 else
693 return NF_ACCEPT;
695 nf_bridge_pull_encap_header(skb);
697 nf_bridge = skb->nf_bridge;
698 if (skb->pkt_type == PACKET_OTHERHOST) {
699 skb->pkt_type = PACKET_HOST;
700 nf_bridge->mask |= BRNF_PKT_TYPE;
703 /* The physdev module checks on this */
704 nf_bridge->mask |= BRNF_BRIDGED;
705 nf_bridge->physoutdev = skb->dev;
707 NF_HOOK(pf, NF_INET_FORWARD, skb, bridge_parent(in), parent,
708 br_nf_forward_finish);
710 return NF_STOLEN;
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 *))
718 struct net_device **d = (struct net_device **)(skb->cb);
720 #ifdef CONFIG_SYSCTL
721 if (!brnf_call_arptables)
722 return NF_ACCEPT;
723 #endif
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);
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;
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);
740 return NF_STOLEN;
743 /* PF_BRIDGE/LOCAL_OUT ***********************************************
745 * This function sees both locally originated IP packets and forwarded
746 * IP packets (in both cases the destination device is a bridge
747 * device). It also sees bridged-and-DNAT'ed packets.
749 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
750 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
751 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
752 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
753 * will be executed.
755 static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff *skb,
756 const struct net_device *in,
757 const struct net_device *out,
758 int (*okfn)(struct sk_buff *))
760 struct net_device *realindev;
761 struct nf_bridge_info *nf_bridge;
763 if (!skb->nf_bridge)
764 return NF_ACCEPT;
766 /* Need exclusive nf_bridge_info since we might have multiple
767 * different physoutdevs. */
768 if (!nf_bridge_unshare(skb))
769 return NF_DROP;
771 nf_bridge = skb->nf_bridge;
772 if (!(nf_bridge->mask & BRNF_BRIDGED_DNAT))
773 return NF_ACCEPT;
775 /* Bridged, take PF_BRIDGE/FORWARD.
776 * (see big note in front of br_nf_pre_routing_finish) */
777 nf_bridge->physoutdev = skb->dev;
778 realindev = nf_bridge->physindev;
780 if (nf_bridge->mask & BRNF_PKT_TYPE) {
781 skb->pkt_type = PACKET_OTHERHOST;
782 nf_bridge->mask ^= BRNF_PKT_TYPE;
784 nf_bridge_push_encap_header(skb);
786 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev, skb->dev,
787 br_forward_finish);
788 return NF_STOLEN;
791 static int br_nf_dev_queue_xmit(struct sk_buff *skb)
793 if (skb->protocol == htons(ETH_P_IP) &&
794 skb->len > skb->dev->mtu &&
795 !skb_is_gso(skb))
796 return ip_fragment(skb, br_dev_queue_push_xmit);
797 else
798 return br_dev_queue_push_xmit(skb);
801 /* PF_BRIDGE/POST_ROUTING ********************************************/
802 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
803 const struct net_device *in,
804 const struct net_device *out,
805 int (*okfn)(struct sk_buff *))
807 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
808 struct net_device *realoutdev = bridge_parent(skb->dev);
809 u_int8_t pf;
811 #ifdef CONFIG_NETFILTER_DEBUG
812 /* Be very paranoid. This probably won't happen anymore, but let's
813 * keep the check just to be sure... */
814 if (skb_mac_header(skb) < skb->head ||
815 skb_mac_header(skb) + ETH_HLEN > skb->data) {
816 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
817 "bad mac.raw pointer.\n");
818 goto print_error;
820 #endif
822 if (!nf_bridge)
823 return NF_ACCEPT;
825 if (!(nf_bridge->mask & (BRNF_BRIDGED | BRNF_BRIDGED_DNAT)))
826 return NF_ACCEPT;
828 if (!realoutdev)
829 return NF_DROP;
831 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
832 IS_PPPOE_IP(skb))
833 pf = PF_INET;
834 else if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
835 IS_PPPOE_IPV6(skb))
836 pf = PF_INET6;
837 else
838 return NF_ACCEPT;
840 #ifdef CONFIG_NETFILTER_DEBUG
841 if (skb->dst == NULL) {
842 printk(KERN_INFO "br_netfilter post_routing: skb->dst == NULL\n");
843 goto print_error;
845 #endif
847 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
848 * about the value of skb->pkt_type. */
849 if (skb->pkt_type == PACKET_OTHERHOST) {
850 skb->pkt_type = PACKET_HOST;
851 nf_bridge->mask |= BRNF_PKT_TYPE;
854 nf_bridge_pull_encap_header(skb);
855 nf_bridge_save_header(skb);
857 NF_HOOK(pf, NF_INET_POST_ROUTING, skb, NULL, realoutdev,
858 br_nf_dev_queue_xmit);
860 return NF_STOLEN;
862 #ifdef CONFIG_NETFILTER_DEBUG
863 print_error:
864 if (skb->dev != NULL) {
865 printk("[%s]", skb->dev->name);
866 if (realoutdev)
867 printk("[%s]", realoutdev->name);
869 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb_mac_header(skb),
870 skb->data);
871 dump_stack();
872 return NF_ACCEPT;
873 #endif
876 /* IP/SABOTAGE *****************************************************/
877 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
878 * for the second time. */
879 static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff *skb,
880 const struct net_device *in,
881 const struct net_device *out,
882 int (*okfn)(struct sk_buff *))
884 if (skb->nf_bridge &&
885 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
886 return NF_STOP;
889 return NF_ACCEPT;
892 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
893 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
894 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
895 * ip_refrag() can return NF_STOLEN. */
896 static struct nf_hook_ops br_nf_ops[] __read_mostly = {
897 { .hook = br_nf_pre_routing,
898 .owner = THIS_MODULE,
899 .pf = PF_BRIDGE,
900 .hooknum = NF_BR_PRE_ROUTING,
901 .priority = NF_BR_PRI_BRNF, },
902 { .hook = br_nf_local_in,
903 .owner = THIS_MODULE,
904 .pf = PF_BRIDGE,
905 .hooknum = NF_BR_LOCAL_IN,
906 .priority = NF_BR_PRI_BRNF, },
907 { .hook = br_nf_forward_ip,
908 .owner = THIS_MODULE,
909 .pf = PF_BRIDGE,
910 .hooknum = NF_BR_FORWARD,
911 .priority = NF_BR_PRI_BRNF - 1, },
912 { .hook = br_nf_forward_arp,
913 .owner = THIS_MODULE,
914 .pf = PF_BRIDGE,
915 .hooknum = NF_BR_FORWARD,
916 .priority = NF_BR_PRI_BRNF, },
917 { .hook = br_nf_local_out,
918 .owner = THIS_MODULE,
919 .pf = PF_BRIDGE,
920 .hooknum = NF_BR_LOCAL_OUT,
921 .priority = NF_BR_PRI_FIRST, },
922 { .hook = br_nf_post_routing,
923 .owner = THIS_MODULE,
924 .pf = PF_BRIDGE,
925 .hooknum = NF_BR_POST_ROUTING,
926 .priority = NF_BR_PRI_LAST, },
927 { .hook = ip_sabotage_in,
928 .owner = THIS_MODULE,
929 .pf = PF_INET,
930 .hooknum = NF_INET_PRE_ROUTING,
931 .priority = NF_IP_PRI_FIRST, },
932 { .hook = ip_sabotage_in,
933 .owner = THIS_MODULE,
934 .pf = PF_INET6,
935 .hooknum = NF_INET_PRE_ROUTING,
936 .priority = NF_IP6_PRI_FIRST, },
939 #ifdef CONFIG_SYSCTL
940 static
941 int brnf_sysctl_call_tables(ctl_table * ctl, int write, struct file *filp,
942 void __user * buffer, size_t * lenp, loff_t * ppos)
944 int ret;
946 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
948 if (write && *(int *)(ctl->data))
949 *(int *)(ctl->data) = 1;
950 return ret;
953 static ctl_table brnf_table[] = {
955 .procname = "bridge-nf-call-arptables",
956 .data = &brnf_call_arptables,
957 .maxlen = sizeof(int),
958 .mode = 0644,
959 .proc_handler = brnf_sysctl_call_tables,
962 .procname = "bridge-nf-call-iptables",
963 .data = &brnf_call_iptables,
964 .maxlen = sizeof(int),
965 .mode = 0644,
966 .proc_handler = brnf_sysctl_call_tables,
969 .procname = "bridge-nf-call-ip6tables",
970 .data = &brnf_call_ip6tables,
971 .maxlen = sizeof(int),
972 .mode = 0644,
973 .proc_handler = brnf_sysctl_call_tables,
976 .procname = "bridge-nf-filter-vlan-tagged",
977 .data = &brnf_filter_vlan_tagged,
978 .maxlen = sizeof(int),
979 .mode = 0644,
980 .proc_handler = brnf_sysctl_call_tables,
983 .procname = "bridge-nf-filter-pppoe-tagged",
984 .data = &brnf_filter_pppoe_tagged,
985 .maxlen = sizeof(int),
986 .mode = 0644,
987 .proc_handler = brnf_sysctl_call_tables,
989 { .ctl_name = 0 }
992 static struct ctl_path brnf_path[] = {
993 { .procname = "net", .ctl_name = CTL_NET, },
994 { .procname = "bridge", .ctl_name = NET_BRIDGE, },
997 #endif
999 int __init br_netfilter_init(void)
1001 int ret;
1003 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1004 if (ret < 0)
1005 return ret;
1006 #ifdef CONFIG_SYSCTL
1007 brnf_sysctl_header = register_sysctl_paths(brnf_path, brnf_table);
1008 if (brnf_sysctl_header == NULL) {
1009 printk(KERN_WARNING
1010 "br_netfilter: can't register to sysctl.\n");
1011 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1012 return -ENOMEM;
1014 #endif
1015 printk(KERN_NOTICE "Bridge firewalling registered\n");
1016 return 0;
1019 void br_netfilter_fini(void)
1021 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1022 #ifdef CONFIG_SYSCTL
1023 unregister_sysctl_table(brnf_sysctl_header);
1024 #endif