[BRIDGE]: netfilter handle RCU during removal
[linux-2.6/suspend2-2.6.18.git] / net / bridge / br_netfilter.c
blobb5018166b0e5d163716efb5194940a9a0ddf591d
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/netfilter_bridge.h>
33 #include <linux/netfilter_ipv4.h>
34 #include <linux/netfilter_ipv6.h>
35 #include <linux/netfilter_arp.h>
36 #include <linux/in_route.h>
38 #include <net/ip.h>
39 #include <net/ipv6.h>
40 #include <net/route.h>
42 #include <asm/uaccess.h>
43 #include <asm/checksum.h>
44 #include "br_private.h"
45 #ifdef CONFIG_SYSCTL
46 #include <linux/sysctl.h>
47 #endif
49 #define skb_origaddr(skb) (((struct bridge_skb_cb *) \
50 (skb->nf_bridge->data))->daddr.ipv4)
51 #define store_orig_dstaddr(skb) (skb_origaddr(skb) = (skb)->nh.iph->daddr)
52 #define dnat_took_place(skb) (skb_origaddr(skb) != (skb)->nh.iph->daddr)
54 #ifdef CONFIG_SYSCTL
55 static struct ctl_table_header *brnf_sysctl_header;
56 static int brnf_call_iptables = 1;
57 static int brnf_call_ip6tables = 1;
58 static int brnf_call_arptables = 1;
59 static int brnf_filter_vlan_tagged = 1;
60 #else
61 #define brnf_filter_vlan_tagged 1
62 #endif
64 #define IS_VLAN_IP (skb->protocol == __constant_htons(ETH_P_8021Q) && \
65 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IP) && \
66 brnf_filter_vlan_tagged)
67 #define IS_VLAN_IPV6 (skb->protocol == __constant_htons(ETH_P_8021Q) && \
68 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IPV6) && \
69 brnf_filter_vlan_tagged)
70 #define IS_VLAN_ARP (skb->protocol == __constant_htons(ETH_P_8021Q) && \
71 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_ARP) && \
72 brnf_filter_vlan_tagged)
74 /* We need these fake structures to make netfilter happy --
75 * lots of places assume that skb->dst != NULL, which isn't
76 * all that unreasonable.
78 * Currently, we fill in the PMTU entry because netfilter
79 * refragmentation needs it, and the rt_flags entry because
80 * ipt_REJECT needs it. Future netfilter modules might
81 * require us to fill additional fields. */
82 static struct net_device __fake_net_device = {
83 .hard_header_len = ETH_HLEN
86 static struct rtable __fake_rtable = {
87 .u = {
88 .dst = {
89 .__refcnt = ATOMIC_INIT(1),
90 .dev = &__fake_net_device,
91 .path = &__fake_rtable.u.dst,
92 .metrics = {[RTAX_MTU - 1] = 1500},
95 .rt_flags = 0,
98 static inline struct net_device *bridge_parent(const struct net_device *dev)
100 struct net_bridge_port *port = rcu_dereference(dev->br_port);
102 return port ? port->br->dev : NULL;
105 /* PF_BRIDGE/PRE_ROUTING *********************************************/
106 /* Undo the changes made for ip6tables PREROUTING and continue the
107 * bridge PRE_ROUTING hook. */
108 static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
110 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
112 if (nf_bridge->mask & BRNF_PKT_TYPE) {
113 skb->pkt_type = PACKET_OTHERHOST;
114 nf_bridge->mask ^= BRNF_PKT_TYPE;
116 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
118 skb->dst = (struct dst_entry *)&__fake_rtable;
119 dst_hold(skb->dst);
121 skb->dev = nf_bridge->physindev;
122 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
123 skb_push(skb, VLAN_HLEN);
124 skb->nh.raw -= VLAN_HLEN;
126 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
127 br_handle_frame_finish, 1);
129 return 0;
132 static void __br_dnat_complain(void)
134 static unsigned long last_complaint;
136 if (jiffies - last_complaint >= 5 * HZ) {
137 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
138 "forwarding to be enabled\n");
139 last_complaint = jiffies;
143 /* This requires some explaining. If DNAT has taken place,
144 * we will need to fix up the destination Ethernet address,
145 * and this is a tricky process.
147 * There are two cases to consider:
148 * 1. The packet was DNAT'ed to a device in the same bridge
149 * port group as it was received on. We can still bridge
150 * the packet.
151 * 2. The packet was DNAT'ed to a different device, either
152 * a non-bridged device or another bridge port group.
153 * The packet will need to be routed.
155 * The correct way of distinguishing between these two cases is to
156 * call ip_route_input() and to look at skb->dst->dev, which is
157 * changed to the destination device if ip_route_input() succeeds.
159 * Let us first consider the case that ip_route_input() succeeds:
161 * If skb->dst->dev equals the logical bridge device the packet
162 * came in on, we can consider this bridging. We then call
163 * skb->dst->output() which will make the packet enter br_nf_local_out()
164 * not much later. In that function it is assured that the iptables
165 * FORWARD chain is traversed for the packet.
167 * Otherwise, the packet is considered to be routed and we just
168 * change the destination MAC address so that the packet will
169 * later be passed up to the IP stack to be routed.
171 * Let us now consider the case that ip_route_input() fails:
173 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
174 * will fail, while __ip_route_output_key() will return success. The source
175 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
176 * thinks we're handling a locally generated packet and won't care
177 * if IP forwarding is allowed. We send a warning message to the users's
178 * log telling her to put IP forwarding on.
180 * ip_route_input() will also fail if there is no route available.
181 * In that case we just drop the packet.
183 * --Lennert, 20020411
184 * --Bart, 20020416 (updated)
185 * --Bart, 20021007 (updated) */
186 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
188 if (skb->pkt_type == PACKET_OTHERHOST) {
189 skb->pkt_type = PACKET_HOST;
190 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
192 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
194 skb->dev = bridge_parent(skb->dev);
195 if (!skb->dev)
196 kfree_skb(skb);
197 else {
198 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
199 skb_pull(skb, VLAN_HLEN);
200 skb->nh.raw += VLAN_HLEN;
202 skb->dst->output(skb);
204 return 0;
207 static int br_nf_pre_routing_finish(struct sk_buff *skb)
209 struct net_device *dev = skb->dev;
210 struct iphdr *iph = skb->nh.iph;
211 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
213 if (nf_bridge->mask & BRNF_PKT_TYPE) {
214 skb->pkt_type = PACKET_OTHERHOST;
215 nf_bridge->mask ^= BRNF_PKT_TYPE;
217 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
219 if (dnat_took_place(skb)) {
220 if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
221 dev)) {
222 struct rtable *rt;
223 struct flowi fl = { .nl_u =
224 { .ip4_u = { .daddr = iph->daddr, .saddr = 0 ,
225 .tos = RT_TOS(iph->tos)} }, .proto = 0};
227 if (!ip_route_output_key(&rt, &fl)) {
228 /* - Bridged-and-DNAT'ed traffic doesn't
229 * require ip_forwarding.
230 * - Deal with redirected traffic. */
231 if (((struct dst_entry *)rt)->dev == dev ||
232 rt->rt_type == RTN_LOCAL) {
233 skb->dst = (struct dst_entry *)rt;
234 goto bridged_dnat;
236 __br_dnat_complain();
237 dst_release((struct dst_entry *)rt);
239 kfree_skb(skb);
240 return 0;
241 } else {
242 if (skb->dst->dev == dev) {
243 bridged_dnat:
244 /* Tell br_nf_local_out this is a
245 * bridged frame */
246 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
247 skb->dev = nf_bridge->physindev;
248 if (skb->protocol ==
249 __constant_htons(ETH_P_8021Q)) {
250 skb_push(skb, VLAN_HLEN);
251 skb->nh.raw -= VLAN_HLEN;
253 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
254 skb, skb->dev, NULL,
255 br_nf_pre_routing_finish_bridge,
257 return 0;
259 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr,
260 ETH_ALEN);
261 skb->pkt_type = PACKET_HOST;
263 } else {
264 skb->dst = (struct dst_entry *)&__fake_rtable;
265 dst_hold(skb->dst);
268 skb->dev = nf_bridge->physindev;
269 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
270 skb_push(skb, VLAN_HLEN);
271 skb->nh.raw -= VLAN_HLEN;
273 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
274 br_handle_frame_finish, 1);
276 return 0;
279 /* Some common code for IPv4/IPv6 */
280 static struct net_device *setup_pre_routing(struct sk_buff *skb)
282 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
284 if (skb->pkt_type == PACKET_OTHERHOST) {
285 skb->pkt_type = PACKET_HOST;
286 nf_bridge->mask |= BRNF_PKT_TYPE;
289 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
290 nf_bridge->physindev = skb->dev;
291 skb->dev = bridge_parent(skb->dev);
293 return skb->dev;
296 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
297 static int check_hbh_len(struct sk_buff *skb)
299 unsigned char *raw = (u8*)(skb->nh.ipv6h+1);
300 u32 pkt_len;
301 int off = raw - skb->nh.raw;
302 int len = (raw[1]+1)<<3;
304 if ((raw + len) - skb->data > skb_headlen(skb))
305 goto bad;
307 off += 2;
308 len -= 2;
310 while (len > 0) {
311 int optlen = skb->nh.raw[off+1]+2;
313 switch (skb->nh.raw[off]) {
314 case IPV6_TLV_PAD0:
315 optlen = 1;
316 break;
318 case IPV6_TLV_PADN:
319 break;
321 case IPV6_TLV_JUMBO:
322 if (skb->nh.raw[off+1] != 4 || (off&3) != 2)
323 goto bad;
324 pkt_len = ntohl(*(u32*)(skb->nh.raw+off+2));
325 if (pkt_len <= IPV6_MAXPLEN ||
326 skb->nh.ipv6h->payload_len)
327 goto bad;
328 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
329 goto bad;
330 if (pskb_trim_rcsum(skb,
331 pkt_len+sizeof(struct ipv6hdr)))
332 goto bad;
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 nf_bridge_put(skb->nf_bridge);
386 if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
387 return NF_DROP;
388 if (!setup_pre_routing(skb))
389 return NF_DROP;
391 NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
392 br_nf_pre_routing_finish_ipv6);
394 return NF_STOLEN;
396 inhdr_error:
397 return NF_DROP;
400 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
401 * Replicate the checks that IPv4 does on packet reception.
402 * Set skb->dev to the bridge device (i.e. parent of the
403 * receiving device) to make netfilter happy, the REDIRECT
404 * target in particular. Save the original destination IP
405 * address to be able to detect DNAT afterwards. */
406 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
407 const struct net_device *in,
408 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 u8 *vhdr = skb->data;
427 skb_pull(skb, VLAN_HLEN);
428 skb_postpull_rcsum(skb, vhdr, VLAN_HLEN);
429 skb->nh.raw += VLAN_HLEN;
431 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
433 #ifdef CONFIG_SYSCTL
434 if (!brnf_call_iptables)
435 return NF_ACCEPT;
436 #endif
438 if (skb->protocol != __constant_htons(ETH_P_IP) && !IS_VLAN_IP)
439 return NF_ACCEPT;
441 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
442 goto out;
444 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
445 u8 *vhdr = skb->data;
446 skb_pull(skb, VLAN_HLEN);
447 skb_postpull_rcsum(skb, vhdr, VLAN_HLEN);
448 skb->nh.raw += VLAN_HLEN;
451 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
452 goto inhdr_error;
454 iph = skb->nh.iph;
455 if (iph->ihl < 5 || iph->version != 4)
456 goto inhdr_error;
458 if (!pskb_may_pull(skb, 4*iph->ihl))
459 goto inhdr_error;
461 iph = skb->nh.iph;
462 if (ip_fast_csum((__u8 *)iph, iph->ihl) != 0)
463 goto inhdr_error;
465 len = ntohs(iph->tot_len);
466 if (skb->len < len || len < 4*iph->ihl)
467 goto inhdr_error;
469 if (skb->len > len) {
470 __pskb_trim(skb, len);
471 if (skb->ip_summed == CHECKSUM_HW)
472 skb->ip_summed = CHECKSUM_NONE;
475 nf_bridge_put(skb->nf_bridge);
476 if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
477 return NF_DROP;
478 if (!setup_pre_routing(skb))
479 return NF_DROP;
480 store_orig_dstaddr(skb);
482 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
483 br_nf_pre_routing_finish);
485 return NF_STOLEN;
487 inhdr_error:
488 // IP_INC_STATS_BH(IpInHdrErrors);
489 out:
490 return NF_DROP;
494 /* PF_BRIDGE/LOCAL_IN ************************************************/
495 /* The packet is locally destined, which requires a real
496 * dst_entry, so detach the fake one. On the way up, the
497 * packet would pass through PRE_ROUTING again (which already
498 * took place when the packet entered the bridge), but we
499 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
500 * prevent this from happening. */
501 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
502 const struct net_device *in, const struct net_device *out,
503 int (*okfn)(struct sk_buff *))
505 struct sk_buff *skb = *pskb;
507 if (skb->dst == (struct dst_entry *)&__fake_rtable) {
508 dst_release(skb->dst);
509 skb->dst = NULL;
512 return NF_ACCEPT;
516 /* PF_BRIDGE/FORWARD *************************************************/
517 static int br_nf_forward_finish(struct sk_buff *skb)
519 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
520 struct net_device *in;
521 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
523 if (skb->protocol != __constant_htons(ETH_P_ARP) && !IS_VLAN_ARP) {
524 in = nf_bridge->physindev;
525 if (nf_bridge->mask & BRNF_PKT_TYPE) {
526 skb->pkt_type = PACKET_OTHERHOST;
527 nf_bridge->mask ^= BRNF_PKT_TYPE;
529 } else {
530 in = *((struct net_device **)(skb->cb));
532 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
533 skb_push(skb, VLAN_HLEN);
534 skb->nh.raw -= VLAN_HLEN;
536 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
537 skb->dev, br_forward_finish, 1);
538 return 0;
541 /* This is the 'purely bridged' case. For IP, we pass the packet to
542 * netfilter with indev and outdev set to the bridge device,
543 * but we are still able to filter on the 'real' indev/outdev
544 * because of the physdev module. For ARP, indev and outdev are the
545 * bridge ports. */
546 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
547 const struct net_device *in, const struct net_device *out,
548 int (*okfn)(struct sk_buff *))
550 struct sk_buff *skb = *pskb;
551 struct nf_bridge_info *nf_bridge;
552 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
553 struct net_device *parent;
554 int pf;
556 if (!skb->nf_bridge)
557 return NF_ACCEPT;
559 parent = bridge_parent(out);
560 if (!parent)
561 return NF_DROP;
563 if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
564 pf = PF_INET;
565 else
566 pf = PF_INET6;
568 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
569 skb_pull(*pskb, VLAN_HLEN);
570 (*pskb)->nh.raw += VLAN_HLEN;
573 nf_bridge = skb->nf_bridge;
574 if (skb->pkt_type == PACKET_OTHERHOST) {
575 skb->pkt_type = PACKET_HOST;
576 nf_bridge->mask |= BRNF_PKT_TYPE;
579 /* The physdev module checks on this */
580 nf_bridge->mask |= BRNF_BRIDGED;
581 nf_bridge->physoutdev = skb->dev;
583 NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in), parent,
584 br_nf_forward_finish);
586 return NF_STOLEN;
589 static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
590 const struct net_device *in, const struct net_device *out,
591 int (*okfn)(struct sk_buff *))
593 struct sk_buff *skb = *pskb;
594 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
595 struct net_device **d = (struct net_device **)(skb->cb);
597 #ifdef CONFIG_SYSCTL
598 if (!brnf_call_arptables)
599 return NF_ACCEPT;
600 #endif
602 if (skb->protocol != __constant_htons(ETH_P_ARP)) {
603 if (!IS_VLAN_ARP)
604 return NF_ACCEPT;
605 skb_pull(*pskb, VLAN_HLEN);
606 (*pskb)->nh.raw += VLAN_HLEN;
609 if (skb->nh.arph->ar_pln != 4) {
610 if (IS_VLAN_ARP) {
611 skb_push(*pskb, VLAN_HLEN);
612 (*pskb)->nh.raw -= VLAN_HLEN;
614 return NF_ACCEPT;
616 *d = (struct net_device *)in;
617 NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
618 (struct net_device *)out, br_nf_forward_finish);
620 return NF_STOLEN;
624 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
625 static int br_nf_local_out_finish(struct sk_buff *skb)
627 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
628 skb_push(skb, VLAN_HLEN);
629 skb->nh.raw -= VLAN_HLEN;
632 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
633 br_forward_finish, NF_BR_PRI_FIRST + 1);
635 return 0;
638 /* This function sees both locally originated IP packets and forwarded
639 * IP packets (in both cases the destination device is a bridge
640 * device). It also sees bridged-and-DNAT'ed packets.
641 * To be able to filter on the physical bridge devices (with the physdev
642 * module), we steal packets destined to a bridge device away from the
643 * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
644 * when we have determined the real output device. This is done in here.
646 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
647 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
648 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
649 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
650 * will be executed.
651 * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
652 * this packet before, and so the packet was locally originated. We fake
653 * the PF_INET/LOCAL_OUT hook.
654 * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
655 * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
656 * even routed packets that didn't arrive on a bridge interface have their
657 * nf_bridge->physindev set. */
658 static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
659 const struct net_device *in, const struct net_device *out,
660 int (*okfn)(struct sk_buff *))
662 struct net_device *realindev, *realoutdev;
663 struct sk_buff *skb = *pskb;
664 struct nf_bridge_info *nf_bridge;
665 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
666 int pf;
668 if (!skb->nf_bridge)
669 return NF_ACCEPT;
671 if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
672 pf = PF_INET;
673 else
674 pf = PF_INET6;
676 #ifdef CONFIG_NETFILTER_DEBUG
677 /* Sometimes we get packets with NULL ->dst here (for example,
678 * running a dhcp client daemon triggers this). This should now
679 * be fixed, but let's keep the check around. */
680 if (skb->dst == NULL) {
681 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
682 return NF_ACCEPT;
684 #endif
686 nf_bridge = skb->nf_bridge;
687 nf_bridge->physoutdev = skb->dev;
688 realindev = nf_bridge->physindev;
690 /* Bridged, take PF_BRIDGE/FORWARD.
691 * (see big note in front of br_nf_pre_routing_finish) */
692 if (nf_bridge->mask & BRNF_BRIDGED_DNAT) {
693 if (nf_bridge->mask & BRNF_PKT_TYPE) {
694 skb->pkt_type = PACKET_OTHERHOST;
695 nf_bridge->mask ^= BRNF_PKT_TYPE;
697 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
698 skb_push(skb, VLAN_HLEN);
699 skb->nh.raw -= VLAN_HLEN;
702 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev,
703 skb->dev, br_forward_finish);
704 goto out;
706 realoutdev = bridge_parent(skb->dev);
707 if (!realoutdev)
708 return NF_DROP;
710 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
711 /* iptables should match -o br0.x */
712 if (nf_bridge->netoutdev)
713 realoutdev = nf_bridge->netoutdev;
714 #endif
715 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
716 skb_pull(skb, VLAN_HLEN);
717 (*pskb)->nh.raw += VLAN_HLEN;
719 /* IP forwarded traffic has a physindev, locally
720 * generated traffic hasn't. */
721 if (realindev != NULL) {
722 if (!(nf_bridge->mask & BRNF_DONT_TAKE_PARENT) ) {
723 struct net_device *parent = bridge_parent(realindev);
724 if (parent)
725 realindev = parent;
728 NF_HOOK_THRESH(pf, NF_IP_FORWARD, skb, realindev,
729 realoutdev, br_nf_local_out_finish,
730 NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD + 1);
731 } else {
732 NF_HOOK_THRESH(pf, NF_IP_LOCAL_OUT, skb, realindev,
733 realoutdev, br_nf_local_out_finish,
734 NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT + 1);
737 out:
738 return NF_STOLEN;
742 /* PF_BRIDGE/POST_ROUTING ********************************************/
743 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
744 const struct net_device *in, const struct net_device *out,
745 int (*okfn)(struct sk_buff *))
747 struct sk_buff *skb = *pskb;
748 struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
749 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
750 struct net_device *realoutdev = bridge_parent(skb->dev);
751 int pf;
753 #ifdef CONFIG_NETFILTER_DEBUG
754 /* Be very paranoid. This probably won't happen anymore, but let's
755 * keep the check just to be sure... */
756 if (skb->mac.raw < skb->head || skb->mac.raw + ETH_HLEN > skb->data) {
757 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
758 "bad mac.raw pointer.");
759 goto print_error;
761 #endif
763 if (!nf_bridge)
764 return NF_ACCEPT;
766 if (!realoutdev)
767 return NF_DROP;
769 if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
770 pf = PF_INET;
771 else
772 pf = PF_INET6;
774 #ifdef CONFIG_NETFILTER_DEBUG
775 if (skb->dst == NULL) {
776 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
777 goto print_error;
779 #endif
781 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
782 * about the value of skb->pkt_type. */
783 if (skb->pkt_type == PACKET_OTHERHOST) {
784 skb->pkt_type = PACKET_HOST;
785 nf_bridge->mask |= BRNF_PKT_TYPE;
788 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
789 skb_pull(skb, VLAN_HLEN);
790 skb->nh.raw += VLAN_HLEN;
793 nf_bridge_save_header(skb);
795 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
796 if (nf_bridge->netoutdev)
797 realoutdev = nf_bridge->netoutdev;
798 #endif
799 NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev,
800 br_dev_queue_push_xmit);
802 return NF_STOLEN;
804 #ifdef CONFIG_NETFILTER_DEBUG
805 print_error:
806 if (skb->dev != NULL) {
807 printk("[%s]", skb->dev->name);
808 if (has_bridge_parent(skb->dev))
809 printk("[%s]", bridge_parent(skb->dev)->name);
811 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb->mac.raw,
812 skb->data);
813 return NF_ACCEPT;
814 #endif
818 /* IP/SABOTAGE *****************************************************/
819 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
820 * for the second time. */
821 static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff **pskb,
822 const struct net_device *in, const struct net_device *out,
823 int (*okfn)(struct sk_buff *))
825 if ((*pskb)->nf_bridge &&
826 !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
827 return NF_STOP;
830 return NF_ACCEPT;
833 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
834 * and PF_INET(6)/POST_ROUTING until we have done the forwarding
835 * decision in the bridge code and have determined nf_bridge->physoutdev. */
836 static unsigned int ip_sabotage_out(unsigned int hook, struct sk_buff **pskb,
837 const struct net_device *in, const struct net_device *out,
838 int (*okfn)(struct sk_buff *))
840 struct sk_buff *skb = *pskb;
842 if ((out->hard_start_xmit == br_dev_xmit &&
843 okfn != br_nf_forward_finish &&
844 okfn != br_nf_local_out_finish &&
845 okfn != br_dev_queue_push_xmit)
846 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
847 || ((out->priv_flags & IFF_802_1Q_VLAN) &&
848 VLAN_DEV_INFO(out)->real_dev->hard_start_xmit == br_dev_xmit)
849 #endif
851 struct nf_bridge_info *nf_bridge;
853 if (!skb->nf_bridge) {
854 #ifdef CONFIG_SYSCTL
855 /* This code is executed while in the IP(v6) stack,
856 the version should be 4 or 6. We can't use
857 skb->protocol because that isn't set on
858 PF_INET(6)/LOCAL_OUT. */
859 struct iphdr *ip = skb->nh.iph;
861 if (ip->version == 4 && !brnf_call_iptables)
862 return NF_ACCEPT;
863 else if (ip->version == 6 && !brnf_call_ip6tables)
864 return NF_ACCEPT;
865 #endif
866 if (hook == NF_IP_POST_ROUTING)
867 return NF_ACCEPT;
868 if (!nf_bridge_alloc(skb))
869 return NF_DROP;
872 nf_bridge = skb->nf_bridge;
874 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
875 * will need the indev then. For a brouter, the real indev
876 * can be a bridge port, so we make sure br_nf_local_out()
877 * doesn't use the bridge parent of the indev by using
878 * the BRNF_DONT_TAKE_PARENT mask. */
879 if (hook == NF_IP_FORWARD && nf_bridge->physindev == NULL) {
880 nf_bridge->mask |= BRNF_DONT_TAKE_PARENT;
881 nf_bridge->physindev = (struct net_device *)in;
883 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
884 /* the iptables outdev is br0.x, not br0 */
885 if (out->priv_flags & IFF_802_1Q_VLAN)
886 nf_bridge->netoutdev = (struct net_device *)out;
887 #endif
888 return NF_STOP;
891 return NF_ACCEPT;
894 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
895 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
896 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
897 * ip_refrag() can return NF_STOLEN. */
898 static struct nf_hook_ops br_nf_ops[] = {
899 { .hook = br_nf_pre_routing,
900 .owner = THIS_MODULE,
901 .pf = PF_BRIDGE,
902 .hooknum = NF_BR_PRE_ROUTING,
903 .priority = NF_BR_PRI_BRNF, },
904 { .hook = br_nf_local_in,
905 .owner = THIS_MODULE,
906 .pf = PF_BRIDGE,
907 .hooknum = NF_BR_LOCAL_IN,
908 .priority = NF_BR_PRI_BRNF, },
909 { .hook = br_nf_forward_ip,
910 .owner = THIS_MODULE,
911 .pf = PF_BRIDGE,
912 .hooknum = NF_BR_FORWARD,
913 .priority = NF_BR_PRI_BRNF - 1, },
914 { .hook = br_nf_forward_arp,
915 .owner = THIS_MODULE,
916 .pf = PF_BRIDGE,
917 .hooknum = NF_BR_FORWARD,
918 .priority = NF_BR_PRI_BRNF, },
919 { .hook = br_nf_local_out,
920 .owner = THIS_MODULE,
921 .pf = PF_BRIDGE,
922 .hooknum = NF_BR_LOCAL_OUT,
923 .priority = NF_BR_PRI_FIRST, },
924 { .hook = br_nf_post_routing,
925 .owner = THIS_MODULE,
926 .pf = PF_BRIDGE,
927 .hooknum = NF_BR_POST_ROUTING,
928 .priority = NF_BR_PRI_LAST, },
929 { .hook = ip_sabotage_in,
930 .owner = THIS_MODULE,
931 .pf = PF_INET,
932 .hooknum = NF_IP_PRE_ROUTING,
933 .priority = NF_IP_PRI_FIRST, },
934 { .hook = ip_sabotage_in,
935 .owner = THIS_MODULE,
936 .pf = PF_INET6,
937 .hooknum = NF_IP6_PRE_ROUTING,
938 .priority = NF_IP6_PRI_FIRST, },
939 { .hook = ip_sabotage_out,
940 .owner = THIS_MODULE,
941 .pf = PF_INET,
942 .hooknum = NF_IP_FORWARD,
943 .priority = NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD, },
944 { .hook = ip_sabotage_out,
945 .owner = THIS_MODULE,
946 .pf = PF_INET6,
947 .hooknum = NF_IP6_FORWARD,
948 .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD, },
949 { .hook = ip_sabotage_out,
950 .owner = THIS_MODULE,
951 .pf = PF_INET,
952 .hooknum = NF_IP_LOCAL_OUT,
953 .priority = NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
954 { .hook = ip_sabotage_out,
955 .owner = THIS_MODULE,
956 .pf = PF_INET6,
957 .hooknum = NF_IP6_LOCAL_OUT,
958 .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
959 { .hook = ip_sabotage_out,
960 .owner = THIS_MODULE,
961 .pf = PF_INET,
962 .hooknum = NF_IP_POST_ROUTING,
963 .priority = NF_IP_PRI_FIRST, },
964 { .hook = ip_sabotage_out,
965 .owner = THIS_MODULE,
966 .pf = PF_INET6,
967 .hooknum = NF_IP6_POST_ROUTING,
968 .priority = NF_IP6_PRI_FIRST, },
971 #ifdef CONFIG_SYSCTL
972 static
973 int brnf_sysctl_call_tables(ctl_table *ctl, int write, struct file * filp,
974 void __user *buffer, size_t *lenp, loff_t *ppos)
976 int ret;
978 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
980 if (write && *(int *)(ctl->data))
981 *(int *)(ctl->data) = 1;
982 return ret;
985 static ctl_table brnf_table[] = {
987 .ctl_name = NET_BRIDGE_NF_CALL_ARPTABLES,
988 .procname = "bridge-nf-call-arptables",
989 .data = &brnf_call_arptables,
990 .maxlen = sizeof(int),
991 .mode = 0644,
992 .proc_handler = &brnf_sysctl_call_tables,
995 .ctl_name = NET_BRIDGE_NF_CALL_IPTABLES,
996 .procname = "bridge-nf-call-iptables",
997 .data = &brnf_call_iptables,
998 .maxlen = sizeof(int),
999 .mode = 0644,
1000 .proc_handler = &brnf_sysctl_call_tables,
1003 .ctl_name = NET_BRIDGE_NF_CALL_IP6TABLES,
1004 .procname = "bridge-nf-call-ip6tables",
1005 .data = &brnf_call_ip6tables,
1006 .maxlen = sizeof(int),
1007 .mode = 0644,
1008 .proc_handler = &brnf_sysctl_call_tables,
1011 .ctl_name = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
1012 .procname = "bridge-nf-filter-vlan-tagged",
1013 .data = &brnf_filter_vlan_tagged,
1014 .maxlen = sizeof(int),
1015 .mode = 0644,
1016 .proc_handler = &brnf_sysctl_call_tables,
1018 { .ctl_name = 0 }
1021 static ctl_table brnf_bridge_table[] = {
1023 .ctl_name = NET_BRIDGE,
1024 .procname = "bridge",
1025 .mode = 0555,
1026 .child = brnf_table,
1028 { .ctl_name = 0 }
1031 static ctl_table brnf_net_table[] = {
1033 .ctl_name = CTL_NET,
1034 .procname = "net",
1035 .mode = 0555,
1036 .child = brnf_bridge_table,
1038 { .ctl_name = 0 }
1040 #endif
1042 int br_netfilter_init(void)
1044 int i;
1046 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) {
1047 int ret;
1049 if ((ret = nf_register_hook(&br_nf_ops[i])) >= 0)
1050 continue;
1052 while (i--)
1053 nf_unregister_hook(&br_nf_ops[i]);
1055 return ret;
1058 #ifdef CONFIG_SYSCTL
1059 brnf_sysctl_header = register_sysctl_table(brnf_net_table, 0);
1060 if (brnf_sysctl_header == NULL) {
1061 printk(KERN_WARNING "br_netfilter: can't register to sysctl.\n");
1062 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++)
1063 nf_unregister_hook(&br_nf_ops[i]);
1064 return -EFAULT;
1066 #endif
1068 printk(KERN_NOTICE "Bridge firewalling registered\n");
1070 return 0;
1073 void br_netfilter_fini(void)
1075 int i;
1077 for (i = ARRAY_SIZE(br_nf_ops) - 1; i >= 0; i--)
1078 nf_unregister_hook(&br_nf_ops[i]);
1079 #ifdef CONFIG_SYSCTL
1080 unregister_sysctl_table(brnf_sysctl_header);
1081 #endif