[PATCH] aic7xxx_osm build fix
[cris-mirror.git] / net / bridge / br_netfilter.c
blobbe03d3ad264849f1e726b2e3ab76de1d0c44527d
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_ether.h>
30 #include <linux/if_vlan.h>
31 #include <linux/netfilter_bridge.h>
32 #include <linux/netfilter_ipv4.h>
33 #include <linux/netfilter_ipv6.h>
34 #include <linux/netfilter_arp.h>
35 #include <linux/in_route.h>
36 #include <net/ip.h>
37 #include <net/ipv6.h>
38 #include <asm/uaccess.h>
39 #include <asm/checksum.h>
40 #include "br_private.h"
41 #ifdef CONFIG_SYSCTL
42 #include <linux/sysctl.h>
43 #endif
45 #define skb_origaddr(skb) (((struct bridge_skb_cb *) \
46 (skb->nf_bridge->data))->daddr.ipv4)
47 #define store_orig_dstaddr(skb) (skb_origaddr(skb) = (skb)->nh.iph->daddr)
48 #define dnat_took_place(skb) (skb_origaddr(skb) != (skb)->nh.iph->daddr)
50 #define has_bridge_parent(device) ((device)->br_port != NULL)
51 #define bridge_parent(device) ((device)->br_port->br->dev)
53 #ifdef CONFIG_SYSCTL
54 static struct ctl_table_header *brnf_sysctl_header;
55 static int brnf_call_iptables = 1;
56 static int brnf_call_ip6tables = 1;
57 static int brnf_call_arptables = 1;
58 static int brnf_filter_vlan_tagged = 1;
59 #else
60 #define brnf_filter_vlan_tagged 1
61 #endif
63 #define IS_VLAN_IP (skb->protocol == __constant_htons(ETH_P_8021Q) && \
64 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IP) && \
65 brnf_filter_vlan_tagged)
66 #define IS_VLAN_IPV6 (skb->protocol == __constant_htons(ETH_P_8021Q) && \
67 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IPV6) && \
68 brnf_filter_vlan_tagged)
69 #define IS_VLAN_ARP (skb->protocol == __constant_htons(ETH_P_8021Q) && \
70 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_ARP) && \
71 brnf_filter_vlan_tagged)
73 /* We need these fake structures to make netfilter happy --
74 * lots of places assume that skb->dst != NULL, which isn't
75 * all that unreasonable.
77 * Currently, we fill in the PMTU entry because netfilter
78 * refragmentation needs it, and the rt_flags entry because
79 * ipt_REJECT needs it. Future netfilter modules might
80 * require us to fill additional fields. */
81 static struct net_device __fake_net_device = {
82 .hard_header_len = ETH_HLEN
85 static struct rtable __fake_rtable = {
86 .u = {
87 .dst = {
88 .__refcnt = ATOMIC_INIT(1),
89 .dev = &__fake_net_device,
90 .path = &__fake_rtable.u.dst,
91 .metrics = {[RTAX_MTU - 1] = 1500},
94 .rt_flags = 0,
98 /* PF_BRIDGE/PRE_ROUTING *********************************************/
99 /* Undo the changes made for ip6tables PREROUTING and continue the
100 * bridge PRE_ROUTING hook. */
101 static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
103 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
105 #ifdef CONFIG_NETFILTER_DEBUG
106 skb->nf_debug ^= (1 << NF_BR_PRE_ROUTING);
107 #endif
109 if (nf_bridge->mask & BRNF_PKT_TYPE) {
110 skb->pkt_type = PACKET_OTHERHOST;
111 nf_bridge->mask ^= BRNF_PKT_TYPE;
113 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
115 skb->dst = (struct dst_entry *)&__fake_rtable;
116 dst_hold(skb->dst);
118 skb->dev = nf_bridge->physindev;
119 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
120 skb_push(skb, VLAN_HLEN);
121 skb->nh.raw -= VLAN_HLEN;
123 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
124 br_handle_frame_finish, 1);
126 return 0;
129 static void __br_dnat_complain(void)
131 static unsigned long last_complaint;
133 if (jiffies - last_complaint >= 5 * HZ) {
134 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
135 "forwarding to be enabled\n");
136 last_complaint = jiffies;
140 /* This requires some explaining. If DNAT has taken place,
141 * we will need to fix up the destination Ethernet address,
142 * and this is a tricky process.
144 * There are two cases to consider:
145 * 1. The packet was DNAT'ed to a device in the same bridge
146 * port group as it was received on. We can still bridge
147 * the packet.
148 * 2. The packet was DNAT'ed to a different device, either
149 * a non-bridged device or another bridge port group.
150 * The packet will need to be routed.
152 * The correct way of distinguishing between these two cases is to
153 * call ip_route_input() and to look at skb->dst->dev, which is
154 * changed to the destination device if ip_route_input() succeeds.
156 * Let us first consider the case that ip_route_input() succeeds:
158 * If skb->dst->dev equals the logical bridge device the packet
159 * came in on, we can consider this bridging. We then call
160 * skb->dst->output() which will make the packet enter br_nf_local_out()
161 * not much later. In that function it is assured that the iptables
162 * FORWARD chain is traversed for the packet.
164 * Otherwise, the packet is considered to be routed and we just
165 * change the destination MAC address so that the packet will
166 * later be passed up to the IP stack to be routed.
168 * Let us now consider the case that ip_route_input() fails:
170 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
171 * will fail, while __ip_route_output_key() will return success. The source
172 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
173 * thinks we're handling a locally generated packet and won't care
174 * if IP forwarding is allowed. We send a warning message to the users's
175 * log telling her to put IP forwarding on.
177 * ip_route_input() will also fail if there is no route available.
178 * In that case we just drop the packet.
180 * --Lennert, 20020411
181 * --Bart, 20020416 (updated)
182 * --Bart, 20021007 (updated) */
183 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
185 #ifdef CONFIG_NETFILTER_DEBUG
186 skb->nf_debug |= (1 << NF_BR_PRE_ROUTING) | (1 << NF_BR_FORWARD);
187 #endif
189 if (skb->pkt_type == PACKET_OTHERHOST) {
190 skb->pkt_type = PACKET_HOST;
191 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
193 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
195 skb->dev = bridge_parent(skb->dev);
196 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
197 skb_pull(skb, VLAN_HLEN);
198 skb->nh.raw += VLAN_HLEN;
200 skb->dst->output(skb);
201 return 0;
204 static int br_nf_pre_routing_finish(struct sk_buff *skb)
206 struct net_device *dev = skb->dev;
207 struct iphdr *iph = skb->nh.iph;
208 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
210 #ifdef CONFIG_NETFILTER_DEBUG
211 skb->nf_debug ^= (1 << NF_BR_PRE_ROUTING);
212 #endif
214 if (nf_bridge->mask & BRNF_PKT_TYPE) {
215 skb->pkt_type = PACKET_OTHERHOST;
216 nf_bridge->mask ^= BRNF_PKT_TYPE;
218 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
220 if (dnat_took_place(skb)) {
221 if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
222 dev)) {
223 struct rtable *rt;
224 struct flowi fl = { .nl_u =
225 { .ip4_u = { .daddr = iph->daddr, .saddr = 0 ,
226 .tos = RT_TOS(iph->tos)} }, .proto = 0};
228 if (!ip_route_output_key(&rt, &fl)) {
229 /* Bridged-and-DNAT'ed traffic doesn't
230 * require ip_forwarding. */
231 if (((struct dst_entry *)rt)->dev == dev) {
232 skb->dst = (struct dst_entry *)rt;
233 goto bridged_dnat;
235 __br_dnat_complain();
236 dst_release((struct dst_entry *)rt);
238 kfree_skb(skb);
239 return 0;
240 } else {
241 if (skb->dst->dev == dev) {
242 bridged_dnat:
243 /* Tell br_nf_local_out this is a
244 * bridged frame */
245 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
246 skb->dev = nf_bridge->physindev;
247 if (skb->protocol ==
248 __constant_htons(ETH_P_8021Q)) {
249 skb_push(skb, VLAN_HLEN);
250 skb->nh.raw -= VLAN_HLEN;
252 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
253 skb, skb->dev, NULL,
254 br_nf_pre_routing_finish_bridge,
256 return 0;
258 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr,
259 ETH_ALEN);
260 skb->pkt_type = PACKET_HOST;
262 } else {
263 skb->dst = (struct dst_entry *)&__fake_rtable;
264 dst_hold(skb->dst);
267 skb->dev = nf_bridge->physindev;
268 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
269 skb_push(skb, VLAN_HLEN);
270 skb->nh.raw -= VLAN_HLEN;
272 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
273 br_handle_frame_finish, 1);
275 return 0;
278 /* Some common code for IPv4/IPv6 */
279 static void setup_pre_routing(struct sk_buff *skb)
281 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
283 if (skb->pkt_type == PACKET_OTHERHOST) {
284 skb->pkt_type = PACKET_HOST;
285 nf_bridge->mask |= BRNF_PKT_TYPE;
288 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
289 nf_bridge->physindev = skb->dev;
290 skb->dev = bridge_parent(skb->dev);
293 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
294 static int check_hbh_len(struct sk_buff *skb)
296 unsigned char *raw = (u8*)(skb->nh.ipv6h+1);
297 u32 pkt_len;
298 int off = raw - skb->nh.raw;
299 int len = (raw[1]+1)<<3;
301 if ((raw + len) - skb->data > skb_headlen(skb))
302 goto bad;
304 off += 2;
305 len -= 2;
307 while (len > 0) {
308 int optlen = raw[off+1]+2;
310 switch (skb->nh.raw[off]) {
311 case IPV6_TLV_PAD0:
312 optlen = 1;
313 break;
315 case IPV6_TLV_PADN:
316 break;
318 case IPV6_TLV_JUMBO:
319 if (skb->nh.raw[off+1] != 4 || (off&3) != 2)
320 goto bad;
322 pkt_len = ntohl(*(u32*)(skb->nh.raw+off+2));
324 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
325 goto bad;
326 if (pkt_len + sizeof(struct ipv6hdr) < skb->len) {
327 if (__pskb_trim(skb,
328 pkt_len + sizeof(struct ipv6hdr)))
329 goto bad;
330 if (skb->ip_summed == CHECKSUM_HW)
331 skb->ip_summed = CHECKSUM_NONE;
333 break;
334 default:
335 if (optlen > len)
336 goto bad;
337 break;
339 off += optlen;
340 len -= optlen;
342 if (len == 0)
343 return 0;
344 bad:
345 return -1;
349 /* Replicate the checks that IPv6 does on packet reception and pass the packet
350 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
351 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
352 struct sk_buff *skb, const struct net_device *in,
353 const struct net_device *out, int (*okfn)(struct sk_buff *))
355 struct ipv6hdr *hdr;
356 u32 pkt_len;
357 struct nf_bridge_info *nf_bridge;
359 if (skb->len < sizeof(struct ipv6hdr))
360 goto inhdr_error;
362 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
363 goto inhdr_error;
365 hdr = skb->nh.ipv6h;
367 if (hdr->version != 6)
368 goto inhdr_error;
370 pkt_len = ntohs(hdr->payload_len);
372 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
373 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
374 goto inhdr_error;
375 if (pkt_len + sizeof(struct ipv6hdr) < skb->len) {
376 if (__pskb_trim(skb, pkt_len + sizeof(struct ipv6hdr)))
377 goto inhdr_error;
378 if (skb->ip_summed == CHECKSUM_HW)
379 skb->ip_summed = CHECKSUM_NONE;
382 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
383 goto inhdr_error;
385 #ifdef CONFIG_NETFILTER_DEBUG
386 skb->nf_debug ^= (1 << NF_IP6_PRE_ROUTING);
387 #endif
388 if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
389 return NF_DROP;
390 setup_pre_routing(skb);
392 NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
393 br_nf_pre_routing_finish_ipv6);
395 return NF_STOLEN;
397 inhdr_error:
398 return NF_DROP;
401 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
402 * Replicate the checks that IPv4 does on packet reception.
403 * Set skb->dev to the bridge device (i.e. parent of the
404 * receiving device) to make netfilter happy, the REDIRECT
405 * target in particular. Save the original destination IP
406 * address to be able to detect DNAT afterwards. */
407 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
408 const struct net_device *in, const struct net_device *out,
409 int (*okfn)(struct sk_buff *))
411 struct iphdr *iph;
412 __u32 len;
413 struct sk_buff *skb = *pskb;
414 struct nf_bridge_info *nf_bridge;
415 struct vlan_ethhdr *hdr = vlan_eth_hdr(*pskb);
417 if (skb->protocol == __constant_htons(ETH_P_IPV6) || IS_VLAN_IPV6) {
418 #ifdef CONFIG_SYSCTL
419 if (!brnf_call_ip6tables)
420 return NF_ACCEPT;
421 #endif
422 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
423 goto out;
425 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
426 skb_pull(skb, VLAN_HLEN);
427 (skb)->nh.raw += VLAN_HLEN;
429 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
431 #ifdef CONFIG_SYSCTL
432 if (!brnf_call_iptables)
433 return NF_ACCEPT;
434 #endif
436 if (skb->protocol != __constant_htons(ETH_P_IP) && !IS_VLAN_IP)
437 return NF_ACCEPT;
439 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
440 goto out;
442 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
443 skb_pull(skb, VLAN_HLEN);
444 (skb)->nh.raw += VLAN_HLEN;
447 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
448 goto inhdr_error;
450 iph = skb->nh.iph;
451 if (iph->ihl < 5 || iph->version != 4)
452 goto inhdr_error;
454 if (!pskb_may_pull(skb, 4*iph->ihl))
455 goto inhdr_error;
457 iph = skb->nh.iph;
458 if (ip_fast_csum((__u8 *)iph, iph->ihl) != 0)
459 goto inhdr_error;
461 len = ntohs(iph->tot_len);
462 if (skb->len < len || len < 4*iph->ihl)
463 goto inhdr_error;
465 if (skb->len > len) {
466 __pskb_trim(skb, len);
467 if (skb->ip_summed == CHECKSUM_HW)
468 skb->ip_summed = CHECKSUM_NONE;
471 #ifdef CONFIG_NETFILTER_DEBUG
472 skb->nf_debug ^= (1 << NF_IP_PRE_ROUTING);
473 #endif
474 if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
475 return NF_DROP;
476 setup_pre_routing(skb);
477 store_orig_dstaddr(skb);
479 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
480 br_nf_pre_routing_finish);
482 return NF_STOLEN;
484 inhdr_error:
485 // IP_INC_STATS_BH(IpInHdrErrors);
486 out:
487 return NF_DROP;
491 /* PF_BRIDGE/LOCAL_IN ************************************************/
492 /* The packet is locally destined, which requires a real
493 * dst_entry, so detach the fake one. On the way up, the
494 * packet would pass through PRE_ROUTING again (which already
495 * took place when the packet entered the bridge), but we
496 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
497 * prevent this from happening. */
498 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
499 const struct net_device *in, const struct net_device *out,
500 int (*okfn)(struct sk_buff *))
502 struct sk_buff *skb = *pskb;
504 if (skb->dst == (struct dst_entry *)&__fake_rtable) {
505 dst_release(skb->dst);
506 skb->dst = NULL;
509 return NF_ACCEPT;
513 /* PF_BRIDGE/FORWARD *************************************************/
514 static int br_nf_forward_finish(struct sk_buff *skb)
516 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
517 struct net_device *in;
518 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
520 #ifdef CONFIG_NETFILTER_DEBUG
521 skb->nf_debug ^= (1 << NF_BR_FORWARD);
522 #endif
524 if (skb->protocol != __constant_htons(ETH_P_ARP) && !IS_VLAN_ARP) {
525 in = nf_bridge->physindev;
526 if (nf_bridge->mask & BRNF_PKT_TYPE) {
527 skb->pkt_type = PACKET_OTHERHOST;
528 nf_bridge->mask ^= BRNF_PKT_TYPE;
530 } else {
531 in = *((struct net_device **)(skb->cb));
533 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
534 skb_push(skb, VLAN_HLEN);
535 skb->nh.raw -= VLAN_HLEN;
537 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
538 skb->dev, br_forward_finish, 1);
539 return 0;
542 /* This is the 'purely bridged' case. For IP, we pass the packet to
543 * netfilter with indev and outdev set to the bridge device,
544 * but we are still able to filter on the 'real' indev/outdev
545 * because of the physdev module. For ARP, indev and outdev are the
546 * bridge ports. */
547 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
548 const struct net_device *in, const struct net_device *out,
549 int (*okfn)(struct sk_buff *))
551 struct sk_buff *skb = *pskb;
552 struct nf_bridge_info *nf_bridge;
553 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
554 int pf;
556 if (!skb->nf_bridge)
557 return NF_ACCEPT;
559 if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
560 pf = PF_INET;
561 else
562 pf = PF_INET6;
564 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
565 skb_pull(*pskb, VLAN_HLEN);
566 (*pskb)->nh.raw += VLAN_HLEN;
569 #ifdef CONFIG_NETFILTER_DEBUG
570 skb->nf_debug ^= (1 << NF_BR_FORWARD);
571 #endif
572 nf_bridge = skb->nf_bridge;
573 if (skb->pkt_type == PACKET_OTHERHOST) {
574 skb->pkt_type = PACKET_HOST;
575 nf_bridge->mask |= BRNF_PKT_TYPE;
578 /* The physdev module checks on this */
579 nf_bridge->mask |= BRNF_BRIDGED;
580 nf_bridge->physoutdev = skb->dev;
582 NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in),
583 bridge_parent(out), br_nf_forward_finish);
585 return NF_STOLEN;
588 static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
589 const struct net_device *in, const struct net_device *out,
590 int (*okfn)(struct sk_buff *))
592 struct sk_buff *skb = *pskb;
593 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
594 struct net_device **d = (struct net_device **)(skb->cb);
596 #ifdef CONFIG_SYSCTL
597 if (!brnf_call_arptables)
598 return NF_ACCEPT;
599 #endif
601 if (skb->protocol != __constant_htons(ETH_P_ARP)) {
602 if (!IS_VLAN_ARP)
603 return NF_ACCEPT;
604 skb_pull(*pskb, VLAN_HLEN);
605 (*pskb)->nh.raw += VLAN_HLEN;
608 #ifdef CONFIG_NETFILTER_DEBUG
609 skb->nf_debug ^= (1 << NF_BR_FORWARD);
610 #endif
612 if (skb->nh.arph->ar_pln != 4) {
613 if (IS_VLAN_ARP) {
614 skb_push(*pskb, VLAN_HLEN);
615 (*pskb)->nh.raw -= VLAN_HLEN;
617 return NF_ACCEPT;
619 *d = (struct net_device *)in;
620 NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
621 (struct net_device *)out, br_nf_forward_finish);
623 return NF_STOLEN;
627 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
628 static int br_nf_local_out_finish(struct sk_buff *skb)
630 #ifdef CONFIG_NETFILTER_DEBUG
631 skb->nf_debug &= ~(1 << NF_BR_LOCAL_OUT);
632 #endif
633 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
634 skb_push(skb, VLAN_HLEN);
635 skb->nh.raw -= VLAN_HLEN;
638 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
639 br_forward_finish, NF_BR_PRI_FIRST + 1);
641 return 0;
644 /* This function sees both locally originated IP packets and forwarded
645 * IP packets (in both cases the destination device is a bridge
646 * device). It also sees bridged-and-DNAT'ed packets.
647 * To be able to filter on the physical bridge devices (with the physdev
648 * module), we steal packets destined to a bridge device away from the
649 * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
650 * when we have determined the real output device. This is done in here.
652 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
653 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
654 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
655 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
656 * will be executed.
657 * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
658 * this packet before, and so the packet was locally originated. We fake
659 * the PF_INET/LOCAL_OUT hook.
660 * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
661 * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
662 * even routed packets that didn't arrive on a bridge interface have their
663 * nf_bridge->physindev set. */
664 static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
665 const struct net_device *in, const struct net_device *out,
666 int (*okfn)(struct sk_buff *))
668 struct net_device *realindev, *realoutdev;
669 struct sk_buff *skb = *pskb;
670 struct nf_bridge_info *nf_bridge;
671 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
672 int pf;
674 if (!skb->nf_bridge)
675 return NF_ACCEPT;
677 if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
678 pf = PF_INET;
679 else
680 pf = PF_INET6;
682 #ifdef CONFIG_NETFILTER_DEBUG
683 /* Sometimes we get packets with NULL ->dst here (for example,
684 * running a dhcp client daemon triggers this). This should now
685 * be fixed, but let's keep the check around. */
686 if (skb->dst == NULL) {
687 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
688 return NF_ACCEPT;
690 #endif
692 nf_bridge = skb->nf_bridge;
693 nf_bridge->physoutdev = skb->dev;
694 realindev = nf_bridge->physindev;
696 /* Bridged, take PF_BRIDGE/FORWARD.
697 * (see big note in front of br_nf_pre_routing_finish) */
698 if (nf_bridge->mask & BRNF_BRIDGED_DNAT) {
699 if (nf_bridge->mask & BRNF_PKT_TYPE) {
700 skb->pkt_type = PACKET_OTHERHOST;
701 nf_bridge->mask ^= BRNF_PKT_TYPE;
703 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
704 skb_push(skb, VLAN_HLEN);
705 skb->nh.raw -= VLAN_HLEN;
708 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev,
709 skb->dev, br_forward_finish);
710 goto out;
712 realoutdev = bridge_parent(skb->dev);
714 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
715 /* iptables should match -o br0.x */
716 if (nf_bridge->netoutdev)
717 realoutdev = nf_bridge->netoutdev;
718 #endif
719 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
720 skb_pull(skb, VLAN_HLEN);
721 (*pskb)->nh.raw += VLAN_HLEN;
723 /* IP forwarded traffic has a physindev, locally
724 * generated traffic hasn't. */
725 if (realindev != NULL) {
726 if (!(nf_bridge->mask & BRNF_DONT_TAKE_PARENT) &&
727 has_bridge_parent(realindev))
728 realindev = bridge_parent(realindev);
730 NF_HOOK_THRESH(pf, NF_IP_FORWARD, skb, realindev,
731 realoutdev, br_nf_local_out_finish,
732 NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD + 1);
733 } else {
734 #ifdef CONFIG_NETFILTER_DEBUG
735 skb->nf_debug ^= (1 << NF_IP_LOCAL_OUT);
736 #endif
738 NF_HOOK_THRESH(pf, NF_IP_LOCAL_OUT, skb, realindev,
739 realoutdev, br_nf_local_out_finish,
740 NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT + 1);
743 out:
744 return NF_STOLEN;
748 /* PF_BRIDGE/POST_ROUTING ********************************************/
749 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
750 const struct net_device *in, const struct net_device *out,
751 int (*okfn)(struct sk_buff *))
753 struct sk_buff *skb = *pskb;
754 struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
755 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
756 struct net_device *realoutdev = bridge_parent(skb->dev);
757 int pf;
759 #ifdef CONFIG_NETFILTER_DEBUG
760 /* Be very paranoid. This probably won't happen anymore, but let's
761 * keep the check just to be sure... */
762 if (skb->mac.raw < skb->head || skb->mac.raw + ETH_HLEN > skb->data) {
763 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
764 "bad mac.raw pointer.");
765 goto print_error;
767 #endif
769 if (!nf_bridge)
770 return NF_ACCEPT;
772 if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
773 pf = PF_INET;
774 else
775 pf = PF_INET6;
777 #ifdef CONFIG_NETFILTER_DEBUG
778 if (skb->dst == NULL) {
779 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
780 goto print_error;
783 skb->nf_debug ^= (1 << NF_IP_POST_ROUTING);
784 #endif
786 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
787 * about the value of skb->pkt_type. */
788 if (skb->pkt_type == PACKET_OTHERHOST) {
789 skb->pkt_type = PACKET_HOST;
790 nf_bridge->mask |= BRNF_PKT_TYPE;
793 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
794 skb_pull(skb, VLAN_HLEN);
795 skb->nh.raw += VLAN_HLEN;
798 nf_bridge_save_header(skb);
800 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
801 if (nf_bridge->netoutdev)
802 realoutdev = nf_bridge->netoutdev;
803 #endif
804 NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev,
805 br_dev_queue_push_xmit);
807 return NF_STOLEN;
809 #ifdef CONFIG_NETFILTER_DEBUG
810 print_error:
811 if (skb->dev != NULL) {
812 printk("[%s]", skb->dev->name);
813 if (has_bridge_parent(skb->dev))
814 printk("[%s]", bridge_parent(skb->dev)->name);
816 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb->mac.raw,
817 skb->data);
818 return NF_ACCEPT;
819 #endif
823 /* IP/SABOTAGE *****************************************************/
824 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
825 * for the second time. */
826 static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff **pskb,
827 const struct net_device *in, const struct net_device *out,
828 int (*okfn)(struct sk_buff *))
830 if ((*pskb)->nf_bridge &&
831 !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
832 return NF_STOP;
835 return NF_ACCEPT;
838 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
839 * and PF_INET(6)/POST_ROUTING until we have done the forwarding
840 * decision in the bridge code and have determined nf_bridge->physoutdev. */
841 static unsigned int ip_sabotage_out(unsigned int hook, struct sk_buff **pskb,
842 const struct net_device *in, const struct net_device *out,
843 int (*okfn)(struct sk_buff *))
845 struct sk_buff *skb = *pskb;
847 if ((out->hard_start_xmit == br_dev_xmit &&
848 okfn != br_nf_forward_finish &&
849 okfn != br_nf_local_out_finish &&
850 okfn != br_dev_queue_push_xmit)
851 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
852 || ((out->priv_flags & IFF_802_1Q_VLAN) &&
853 VLAN_DEV_INFO(out)->real_dev->hard_start_xmit == br_dev_xmit)
854 #endif
856 struct nf_bridge_info *nf_bridge;
858 if (!skb->nf_bridge) {
859 #ifdef CONFIG_SYSCTL
860 /* This code is executed while in the IP(v6) stack,
861 the version should be 4 or 6. We can't use
862 skb->protocol because that isn't set on
863 PF_INET(6)/LOCAL_OUT. */
864 struct iphdr *ip = skb->nh.iph;
866 if (ip->version == 4 && !brnf_call_iptables)
867 return NF_ACCEPT;
868 else if (ip->version == 6 && !brnf_call_ip6tables)
869 return NF_ACCEPT;
870 #endif
871 if (hook == NF_IP_POST_ROUTING)
872 return NF_ACCEPT;
873 if (!nf_bridge_alloc(skb))
874 return NF_DROP;
877 nf_bridge = skb->nf_bridge;
879 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
880 * will need the indev then. For a brouter, the real indev
881 * can be a bridge port, so we make sure br_nf_local_out()
882 * doesn't use the bridge parent of the indev by using
883 * the BRNF_DONT_TAKE_PARENT mask. */
884 if (hook == NF_IP_FORWARD && nf_bridge->physindev == NULL) {
885 nf_bridge->mask &= BRNF_DONT_TAKE_PARENT;
886 nf_bridge->physindev = (struct net_device *)in;
888 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
889 /* the iptables outdev is br0.x, not br0 */
890 if (out->priv_flags & IFF_802_1Q_VLAN)
891 nf_bridge->netoutdev = (struct net_device *)out;
892 #endif
893 return NF_STOP;
896 return NF_ACCEPT;
899 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
900 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
901 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
902 * ip_refrag() can return NF_STOLEN. */
903 static struct nf_hook_ops br_nf_ops[] = {
904 { .hook = br_nf_pre_routing,
905 .owner = THIS_MODULE,
906 .pf = PF_BRIDGE,
907 .hooknum = NF_BR_PRE_ROUTING,
908 .priority = NF_BR_PRI_BRNF, },
909 { .hook = br_nf_local_in,
910 .owner = THIS_MODULE,
911 .pf = PF_BRIDGE,
912 .hooknum = NF_BR_LOCAL_IN,
913 .priority = NF_BR_PRI_BRNF, },
914 { .hook = br_nf_forward_ip,
915 .owner = THIS_MODULE,
916 .pf = PF_BRIDGE,
917 .hooknum = NF_BR_FORWARD,
918 .priority = NF_BR_PRI_BRNF - 1, },
919 { .hook = br_nf_forward_arp,
920 .owner = THIS_MODULE,
921 .pf = PF_BRIDGE,
922 .hooknum = NF_BR_FORWARD,
923 .priority = NF_BR_PRI_BRNF, },
924 { .hook = br_nf_local_out,
925 .owner = THIS_MODULE,
926 .pf = PF_BRIDGE,
927 .hooknum = NF_BR_LOCAL_OUT,
928 .priority = NF_BR_PRI_FIRST, },
929 { .hook = br_nf_post_routing,
930 .owner = THIS_MODULE,
931 .pf = PF_BRIDGE,
932 .hooknum = NF_BR_POST_ROUTING,
933 .priority = NF_BR_PRI_LAST, },
934 { .hook = ip_sabotage_in,
935 .owner = THIS_MODULE,
936 .pf = PF_INET,
937 .hooknum = NF_IP_PRE_ROUTING,
938 .priority = NF_IP_PRI_FIRST, },
939 { .hook = ip_sabotage_in,
940 .owner = THIS_MODULE,
941 .pf = PF_INET6,
942 .hooknum = NF_IP6_PRE_ROUTING,
943 .priority = NF_IP6_PRI_FIRST, },
944 { .hook = ip_sabotage_out,
945 .owner = THIS_MODULE,
946 .pf = PF_INET,
947 .hooknum = NF_IP_FORWARD,
948 .priority = NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD, },
949 { .hook = ip_sabotage_out,
950 .owner = THIS_MODULE,
951 .pf = PF_INET6,
952 .hooknum = NF_IP6_FORWARD,
953 .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD, },
954 { .hook = ip_sabotage_out,
955 .owner = THIS_MODULE,
956 .pf = PF_INET,
957 .hooknum = NF_IP_LOCAL_OUT,
958 .priority = NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
959 { .hook = ip_sabotage_out,
960 .owner = THIS_MODULE,
961 .pf = PF_INET6,
962 .hooknum = NF_IP6_LOCAL_OUT,
963 .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
964 { .hook = ip_sabotage_out,
965 .owner = THIS_MODULE,
966 .pf = PF_INET,
967 .hooknum = NF_IP_POST_ROUTING,
968 .priority = NF_IP_PRI_FIRST, },
969 { .hook = ip_sabotage_out,
970 .owner = THIS_MODULE,
971 .pf = PF_INET6,
972 .hooknum = NF_IP6_POST_ROUTING,
973 .priority = NF_IP6_PRI_FIRST, },
976 #ifdef CONFIG_SYSCTL
977 static
978 int brnf_sysctl_call_tables(ctl_table *ctl, int write, struct file * filp,
979 void __user *buffer, size_t *lenp, loff_t *ppos)
981 int ret;
983 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
985 if (write && *(int *)(ctl->data))
986 *(int *)(ctl->data) = 1;
987 return ret;
990 static ctl_table brnf_table[] = {
992 .ctl_name = NET_BRIDGE_NF_CALL_ARPTABLES,
993 .procname = "bridge-nf-call-arptables",
994 .data = &brnf_call_arptables,
995 .maxlen = sizeof(int),
996 .mode = 0644,
997 .proc_handler = &brnf_sysctl_call_tables,
1000 .ctl_name = NET_BRIDGE_NF_CALL_IPTABLES,
1001 .procname = "bridge-nf-call-iptables",
1002 .data = &brnf_call_iptables,
1003 .maxlen = sizeof(int),
1004 .mode = 0644,
1005 .proc_handler = &brnf_sysctl_call_tables,
1008 .ctl_name = NET_BRIDGE_NF_CALL_IP6TABLES,
1009 .procname = "bridge-nf-call-ip6tables",
1010 .data = &brnf_call_ip6tables,
1011 .maxlen = sizeof(int),
1012 .mode = 0644,
1013 .proc_handler = &brnf_sysctl_call_tables,
1016 .ctl_name = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
1017 .procname = "bridge-nf-filter-vlan-tagged",
1018 .data = &brnf_filter_vlan_tagged,
1019 .maxlen = sizeof(int),
1020 .mode = 0644,
1021 .proc_handler = &brnf_sysctl_call_tables,
1023 { .ctl_name = 0 }
1026 static ctl_table brnf_bridge_table[] = {
1028 .ctl_name = NET_BRIDGE,
1029 .procname = "bridge",
1030 .mode = 0555,
1031 .child = brnf_table,
1033 { .ctl_name = 0 }
1036 static ctl_table brnf_net_table[] = {
1038 .ctl_name = CTL_NET,
1039 .procname = "net",
1040 .mode = 0555,
1041 .child = brnf_bridge_table,
1043 { .ctl_name = 0 }
1045 #endif
1047 int br_netfilter_init(void)
1049 int i;
1051 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) {
1052 int ret;
1054 if ((ret = nf_register_hook(&br_nf_ops[i])) >= 0)
1055 continue;
1057 while (i--)
1058 nf_unregister_hook(&br_nf_ops[i]);
1060 return ret;
1063 #ifdef CONFIG_SYSCTL
1064 brnf_sysctl_header = register_sysctl_table(brnf_net_table, 0);
1065 if (brnf_sysctl_header == NULL) {
1066 printk(KERN_WARNING "br_netfilter: can't register to sysctl.\n");
1067 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++)
1068 nf_unregister_hook(&br_nf_ops[i]);
1069 return -EFAULT;
1071 #endif
1073 printk(KERN_NOTICE "Bridge firewalling registered\n");
1075 return 0;
1078 void br_netfilter_fini(void)
1080 int i;
1082 for (i = ARRAY_SIZE(br_nf_ops) - 1; i >= 0; i--)
1083 nf_unregister_hook(&br_nf_ops[i]);
1084 #ifdef CONFIG_SYSCTL
1085 unregister_sysctl_table(brnf_sysctl_header);
1086 #endif