[SK_BUFF]: Introduce skb_copy_to_linear_data{_offset}
[linux-2.6/btrfs-unstable.git] / net / bridge / br_netfilter.c
blobfd70d041e51f7856aea6b285e2a75b26772d8a42
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>
37 #include <linux/inetdevice.h>
39 #include <net/ip.h>
40 #include <net/ipv6.h>
41 #include <net/route.h>
43 #include <asm/uaccess.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) = ip_hdr(skb)->daddr)
52 #define dnat_took_place(skb) (skb_origaddr(skb) != ip_hdr(skb)->daddr)
54 #ifdef CONFIG_SYSCTL
55 static struct ctl_table_header *brnf_sysctl_header;
56 static int brnf_call_iptables __read_mostly = 1;
57 static int brnf_call_ip6tables __read_mostly = 1;
58 static int brnf_call_arptables __read_mostly = 1;
59 static int brnf_filter_vlan_tagged __read_mostly = 1;
60 #else
61 #define brnf_filter_vlan_tagged 1
62 #endif
64 static inline __be16 vlan_proto(const struct sk_buff *skb)
66 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
69 #define IS_VLAN_IP(skb) \
70 (skb->protocol == htons(ETH_P_8021Q) && \
71 vlan_proto(skb) == htons(ETH_P_IP) && \
72 brnf_filter_vlan_tagged)
74 #define IS_VLAN_IPV6(skb) \
75 (skb->protocol == htons(ETH_P_8021Q) && \
76 vlan_proto(skb) == htons(ETH_P_IPV6) &&\
77 brnf_filter_vlan_tagged)
79 #define IS_VLAN_ARP(skb) \
80 (skb->protocol == htons(ETH_P_8021Q) && \
81 vlan_proto(skb) == htons(ETH_P_ARP) && \
82 brnf_filter_vlan_tagged)
84 /* We need these fake structures to make netfilter happy --
85 * lots of places assume that skb->dst != NULL, which isn't
86 * all that unreasonable.
88 * Currently, we fill in the PMTU entry because netfilter
89 * refragmentation needs it, and the rt_flags entry because
90 * ipt_REJECT needs it. Future netfilter modules might
91 * require us to fill additional fields. */
92 static struct net_device __fake_net_device = {
93 .hard_header_len = ETH_HLEN
96 static struct rtable __fake_rtable = {
97 .u = {
98 .dst = {
99 .__refcnt = ATOMIC_INIT(1),
100 .dev = &__fake_net_device,
101 .path = &__fake_rtable.u.dst,
102 .metrics = {[RTAX_MTU - 1] = 1500},
103 .flags = DST_NOXFRM,
106 .rt_flags = 0,
109 static inline struct net_device *bridge_parent(const struct net_device *dev)
111 struct net_bridge_port *port = rcu_dereference(dev->br_port);
113 return port ? port->br->dev : NULL;
116 static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
118 skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
119 if (likely(skb->nf_bridge))
120 atomic_set(&(skb->nf_bridge->use), 1);
122 return skb->nf_bridge;
125 static inline void nf_bridge_save_header(struct sk_buff *skb)
127 int header_size = ETH_HLEN;
129 if (skb->protocol == htons(ETH_P_8021Q))
130 header_size += VLAN_HLEN;
132 skb_copy_from_linear_data_offset(skb, -header_size,
133 skb->nf_bridge->data, header_size);
137 * When forwarding bridge frames, we save a copy of the original
138 * header before processing.
140 int nf_bridge_copy_header(struct sk_buff *skb)
142 int err;
143 int header_size = ETH_HLEN;
145 if (skb->protocol == htons(ETH_P_8021Q))
146 header_size += VLAN_HLEN;
148 err = skb_cow(skb, header_size);
149 if (err)
150 return err;
152 skb_copy_to_linear_data_offset(skb, -header_size,
153 skb->nf_bridge->data, header_size);
155 if (skb->protocol == htons(ETH_P_8021Q))
156 __skb_push(skb, VLAN_HLEN);
157 return 0;
160 /* PF_BRIDGE/PRE_ROUTING *********************************************/
161 /* Undo the changes made for ip6tables PREROUTING and continue the
162 * bridge PRE_ROUTING hook. */
163 static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
165 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
167 if (nf_bridge->mask & BRNF_PKT_TYPE) {
168 skb->pkt_type = PACKET_OTHERHOST;
169 nf_bridge->mask ^= BRNF_PKT_TYPE;
171 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
173 skb->dst = (struct dst_entry *)&__fake_rtable;
174 dst_hold(skb->dst);
176 skb->dev = nf_bridge->physindev;
177 if (skb->protocol == htons(ETH_P_8021Q)) {
178 skb_push(skb, VLAN_HLEN);
179 skb->network_header -= VLAN_HLEN;
181 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
182 br_handle_frame_finish, 1);
184 return 0;
187 static void __br_dnat_complain(void)
189 static unsigned long last_complaint;
191 if (jiffies - last_complaint >= 5 * HZ) {
192 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
193 "forwarding to be enabled\n");
194 last_complaint = jiffies;
198 /* This requires some explaining. If DNAT has taken place,
199 * we will need to fix up the destination Ethernet address,
200 * and this is a tricky process.
202 * There are two cases to consider:
203 * 1. The packet was DNAT'ed to a device in the same bridge
204 * port group as it was received on. We can still bridge
205 * the packet.
206 * 2. The packet was DNAT'ed to a different device, either
207 * a non-bridged device or another bridge port group.
208 * The packet will need to be routed.
210 * The correct way of distinguishing between these two cases is to
211 * call ip_route_input() and to look at skb->dst->dev, which is
212 * changed to the destination device if ip_route_input() succeeds.
214 * Let us first consider the case that ip_route_input() succeeds:
216 * If skb->dst->dev equals the logical bridge device the packet
217 * came in on, we can consider this bridging. We then call
218 * skb->dst->output() which will make the packet enter br_nf_local_out()
219 * not much later. In that function it is assured that the iptables
220 * FORWARD chain is traversed for the packet.
222 * Otherwise, the packet is considered to be routed and we just
223 * change the destination MAC address so that the packet will
224 * later be passed up to the IP stack to be routed. For a redirected
225 * packet, ip_route_input() will give back the localhost as output device,
226 * which differs from the bridge device.
228 * Let us now consider the case that ip_route_input() fails:
230 * This can be because the destination address is martian, in which case
231 * the packet will be dropped.
232 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
233 * will fail, while __ip_route_output_key() will return success. The source
234 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
235 * thinks we're handling a locally generated packet and won't care
236 * if IP forwarding is allowed. We send a warning message to the users's
237 * log telling her to put IP forwarding on.
239 * ip_route_input() will also fail if there is no route available.
240 * In that case we just drop the packet.
242 * --Lennert, 20020411
243 * --Bart, 20020416 (updated)
244 * --Bart, 20021007 (updated)
245 * --Bart, 20062711 (updated) */
246 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
248 if (skb->pkt_type == PACKET_OTHERHOST) {
249 skb->pkt_type = PACKET_HOST;
250 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
252 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
254 skb->dev = bridge_parent(skb->dev);
255 if (!skb->dev)
256 kfree_skb(skb);
257 else {
258 if (skb->protocol == htons(ETH_P_8021Q)) {
259 skb_pull(skb, VLAN_HLEN);
260 skb->network_header += VLAN_HLEN;
262 skb->dst->output(skb);
264 return 0;
267 static int br_nf_pre_routing_finish(struct sk_buff *skb)
269 struct net_device *dev = skb->dev;
270 struct iphdr *iph = ip_hdr(skb);
271 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
272 int err;
274 if (nf_bridge->mask & BRNF_PKT_TYPE) {
275 skb->pkt_type = PACKET_OTHERHOST;
276 nf_bridge->mask ^= BRNF_PKT_TYPE;
278 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
279 if (dnat_took_place(skb)) {
280 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
281 struct rtable *rt;
282 struct flowi fl = {
283 .nl_u = {
284 .ip4_u = {
285 .daddr = iph->daddr,
286 .saddr = 0,
287 .tos = RT_TOS(iph->tos) },
289 .proto = 0,
291 struct in_device *in_dev = in_dev_get(dev);
293 /* If err equals -EHOSTUNREACH the error is due to a
294 * martian destination or due to the fact that
295 * forwarding is disabled. For most martian packets,
296 * ip_route_output_key() will fail. It won't fail for 2 types of
297 * martian destinations: loopback destinations and destination
298 * 0.0.0.0. In both cases the packet will be dropped because the
299 * destination is the loopback device and not the bridge. */
300 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
301 goto free_skb;
303 if (!ip_route_output_key(&rt, &fl)) {
304 /* - Bridged-and-DNAT'ed traffic doesn't
305 * require ip_forwarding. */
306 if (((struct dst_entry *)rt)->dev == dev) {
307 skb->dst = (struct dst_entry *)rt;
308 goto bridged_dnat;
310 /* we are sure that forwarding is disabled, so printing
311 * this message is no problem. Note that the packet could
312 * still have a martian destination address, in which case
313 * the packet could be dropped even if forwarding were enabled */
314 __br_dnat_complain();
315 dst_release((struct dst_entry *)rt);
317 free_skb:
318 kfree_skb(skb);
319 return 0;
320 } else {
321 if (skb->dst->dev == dev) {
322 bridged_dnat:
323 /* Tell br_nf_local_out this is a
324 * bridged frame */
325 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
326 skb->dev = nf_bridge->physindev;
327 if (skb->protocol ==
328 htons(ETH_P_8021Q)) {
329 skb_push(skb, VLAN_HLEN);
330 skb->network_header -= VLAN_HLEN;
332 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
333 skb, skb->dev, NULL,
334 br_nf_pre_routing_finish_bridge,
336 return 0;
338 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
339 skb->pkt_type = PACKET_HOST;
341 } else {
342 skb->dst = (struct dst_entry *)&__fake_rtable;
343 dst_hold(skb->dst);
346 skb->dev = nf_bridge->physindev;
347 if (skb->protocol == htons(ETH_P_8021Q)) {
348 skb_push(skb, VLAN_HLEN);
349 skb->network_header -= VLAN_HLEN;
351 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
352 br_handle_frame_finish, 1);
354 return 0;
357 /* Some common code for IPv4/IPv6 */
358 static struct net_device *setup_pre_routing(struct sk_buff *skb)
360 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
362 if (skb->pkt_type == PACKET_OTHERHOST) {
363 skb->pkt_type = PACKET_HOST;
364 nf_bridge->mask |= BRNF_PKT_TYPE;
367 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
368 nf_bridge->physindev = skb->dev;
369 skb->dev = bridge_parent(skb->dev);
371 return skb->dev;
374 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
375 static int check_hbh_len(struct sk_buff *skb)
377 unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
378 u32 pkt_len;
379 const unsigned char *nh = skb_network_header(skb);
380 int off = raw - nh;
381 int len = (raw[1] + 1) << 3;
383 if ((raw + len) - skb->data > skb_headlen(skb))
384 goto bad;
386 off += 2;
387 len -= 2;
389 while (len > 0) {
390 int optlen = nh[off + 1] + 2;
392 switch (nh[off]) {
393 case IPV6_TLV_PAD0:
394 optlen = 1;
395 break;
397 case IPV6_TLV_PADN:
398 break;
400 case IPV6_TLV_JUMBO:
401 if (nh[off + 1] != 4 || (off & 3) != 2)
402 goto bad;
403 pkt_len = ntohl(*(__be32 *) (nh + off + 2));
404 if (pkt_len <= IPV6_MAXPLEN ||
405 ipv6_hdr(skb)->payload_len)
406 goto bad;
407 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
408 goto bad;
409 if (pskb_trim_rcsum(skb,
410 pkt_len + sizeof(struct ipv6hdr)))
411 goto bad;
412 nh = skb_network_header(skb);
413 break;
414 default:
415 if (optlen > len)
416 goto bad;
417 break;
419 off += optlen;
420 len -= optlen;
422 if (len == 0)
423 return 0;
424 bad:
425 return -1;
429 /* Replicate the checks that IPv6 does on packet reception and pass the packet
430 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
431 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
432 struct sk_buff *skb,
433 const struct net_device *in,
434 const struct net_device *out,
435 int (*okfn)(struct sk_buff *))
437 struct ipv6hdr *hdr;
438 u32 pkt_len;
440 if (skb->len < sizeof(struct ipv6hdr))
441 goto inhdr_error;
443 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
444 goto inhdr_error;
446 hdr = ipv6_hdr(skb);
448 if (hdr->version != 6)
449 goto inhdr_error;
451 pkt_len = ntohs(hdr->payload_len);
453 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
454 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
455 goto inhdr_error;
456 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
457 goto inhdr_error;
459 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
460 goto inhdr_error;
462 nf_bridge_put(skb->nf_bridge);
463 if (!nf_bridge_alloc(skb))
464 return NF_DROP;
465 if (!setup_pre_routing(skb))
466 return NF_DROP;
468 NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
469 br_nf_pre_routing_finish_ipv6);
471 return NF_STOLEN;
473 inhdr_error:
474 return NF_DROP;
477 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
478 * Replicate the checks that IPv4 does on packet reception.
479 * Set skb->dev to the bridge device (i.e. parent of the
480 * receiving device) to make netfilter happy, the REDIRECT
481 * target in particular. Save the original destination IP
482 * address to be able to detect DNAT afterwards. */
483 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
484 const struct net_device *in,
485 const struct net_device *out,
486 int (*okfn)(struct sk_buff *))
488 struct iphdr *iph;
489 __u32 len;
490 struct sk_buff *skb = *pskb;
492 if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb)) {
493 #ifdef CONFIG_SYSCTL
494 if (!brnf_call_ip6tables)
495 return NF_ACCEPT;
496 #endif
497 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
498 goto out;
500 if (skb->protocol == htons(ETH_P_8021Q)) {
501 skb_pull_rcsum(skb, VLAN_HLEN);
502 skb->network_header += VLAN_HLEN;
504 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
506 #ifdef CONFIG_SYSCTL
507 if (!brnf_call_iptables)
508 return NF_ACCEPT;
509 #endif
511 if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb))
512 return NF_ACCEPT;
514 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
515 goto out;
517 if (skb->protocol == htons(ETH_P_8021Q)) {
518 skb_pull_rcsum(skb, VLAN_HLEN);
519 skb->network_header += VLAN_HLEN;
522 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
523 goto inhdr_error;
525 iph = ip_hdr(skb);
526 if (iph->ihl < 5 || iph->version != 4)
527 goto inhdr_error;
529 if (!pskb_may_pull(skb, 4 * iph->ihl))
530 goto inhdr_error;
532 iph = ip_hdr(skb);
533 if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
534 goto inhdr_error;
536 len = ntohs(iph->tot_len);
537 if (skb->len < len || len < 4 * iph->ihl)
538 goto inhdr_error;
540 pskb_trim_rcsum(skb, len);
542 nf_bridge_put(skb->nf_bridge);
543 if (!nf_bridge_alloc(skb))
544 return NF_DROP;
545 if (!setup_pre_routing(skb))
546 return NF_DROP;
547 store_orig_dstaddr(skb);
549 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
550 br_nf_pre_routing_finish);
552 return NF_STOLEN;
554 inhdr_error:
555 // IP_INC_STATS_BH(IpInHdrErrors);
556 out:
557 return NF_DROP;
561 /* PF_BRIDGE/LOCAL_IN ************************************************/
562 /* The packet is locally destined, which requires a real
563 * dst_entry, so detach the fake one. On the way up, the
564 * packet would pass through PRE_ROUTING again (which already
565 * took place when the packet entered the bridge), but we
566 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
567 * prevent this from happening. */
568 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
569 const struct net_device *in,
570 const struct net_device *out,
571 int (*okfn)(struct sk_buff *))
573 struct sk_buff *skb = *pskb;
575 if (skb->dst == (struct dst_entry *)&__fake_rtable) {
576 dst_release(skb->dst);
577 skb->dst = NULL;
580 return NF_ACCEPT;
583 /* PF_BRIDGE/FORWARD *************************************************/
584 static int br_nf_forward_finish(struct sk_buff *skb)
586 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
587 struct net_device *in;
589 if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
590 in = nf_bridge->physindev;
591 if (nf_bridge->mask & BRNF_PKT_TYPE) {
592 skb->pkt_type = PACKET_OTHERHOST;
593 nf_bridge->mask ^= BRNF_PKT_TYPE;
595 } else {
596 in = *((struct net_device **)(skb->cb));
598 if (skb->protocol == htons(ETH_P_8021Q)) {
599 skb_push(skb, VLAN_HLEN);
600 skb->network_header -= VLAN_HLEN;
602 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
603 skb->dev, br_forward_finish, 1);
604 return 0;
607 /* This is the 'purely bridged' case. For IP, we pass the packet to
608 * netfilter with indev and outdev set to the bridge device,
609 * but we are still able to filter on the 'real' indev/outdev
610 * because of the physdev module. For ARP, indev and outdev are the
611 * bridge ports. */
612 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
613 const struct net_device *in,
614 const struct net_device *out,
615 int (*okfn)(struct sk_buff *))
617 struct sk_buff *skb = *pskb;
618 struct nf_bridge_info *nf_bridge;
619 struct net_device *parent;
620 int pf;
622 if (!skb->nf_bridge)
623 return NF_ACCEPT;
625 parent = bridge_parent(out);
626 if (!parent)
627 return NF_DROP;
629 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
630 pf = PF_INET;
631 else
632 pf = PF_INET6;
634 if (skb->protocol == htons(ETH_P_8021Q)) {
635 skb_pull(*pskb, VLAN_HLEN);
636 (*pskb)->network_header += VLAN_HLEN;
639 nf_bridge = skb->nf_bridge;
640 if (skb->pkt_type == PACKET_OTHERHOST) {
641 skb->pkt_type = PACKET_HOST;
642 nf_bridge->mask |= BRNF_PKT_TYPE;
645 /* The physdev module checks on this */
646 nf_bridge->mask |= BRNF_BRIDGED;
647 nf_bridge->physoutdev = skb->dev;
649 NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in), parent,
650 br_nf_forward_finish);
652 return NF_STOLEN;
655 static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
656 const struct net_device *in,
657 const struct net_device *out,
658 int (*okfn)(struct sk_buff *))
660 struct sk_buff *skb = *pskb;
661 struct net_device **d = (struct net_device **)(skb->cb);
663 #ifdef CONFIG_SYSCTL
664 if (!brnf_call_arptables)
665 return NF_ACCEPT;
666 #endif
668 if (skb->protocol != htons(ETH_P_ARP)) {
669 if (!IS_VLAN_ARP(skb))
670 return NF_ACCEPT;
671 skb_pull(*pskb, VLAN_HLEN);
672 (*pskb)->network_header += VLAN_HLEN;
675 if (arp_hdr(skb)->ar_pln != 4) {
676 if (IS_VLAN_ARP(skb)) {
677 skb_push(*pskb, VLAN_HLEN);
678 (*pskb)->network_header -= VLAN_HLEN;
680 return NF_ACCEPT;
682 *d = (struct net_device *)in;
683 NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
684 (struct net_device *)out, br_nf_forward_finish);
686 return NF_STOLEN;
689 /* PF_BRIDGE/LOCAL_OUT ***********************************************
691 * This function sees both locally originated IP packets and forwarded
692 * IP packets (in both cases the destination device is a bridge
693 * device). It also sees bridged-and-DNAT'ed packets.
695 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
696 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
697 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
698 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
699 * will be executed.
701 static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
702 const struct net_device *in,
703 const struct net_device *out,
704 int (*okfn)(struct sk_buff *))
706 struct net_device *realindev;
707 struct sk_buff *skb = *pskb;
708 struct nf_bridge_info *nf_bridge;
710 if (!skb->nf_bridge)
711 return NF_ACCEPT;
713 nf_bridge = skb->nf_bridge;
714 if (!(nf_bridge->mask & BRNF_BRIDGED_DNAT))
715 return NF_ACCEPT;
717 /* Bridged, take PF_BRIDGE/FORWARD.
718 * (see big note in front of br_nf_pre_routing_finish) */
719 nf_bridge->physoutdev = skb->dev;
720 realindev = nf_bridge->physindev;
722 if (nf_bridge->mask & BRNF_PKT_TYPE) {
723 skb->pkt_type = PACKET_OTHERHOST;
724 nf_bridge->mask ^= BRNF_PKT_TYPE;
726 if (skb->protocol == htons(ETH_P_8021Q)) {
727 skb_push(skb, VLAN_HLEN);
728 skb->network_header -= VLAN_HLEN;
731 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev, skb->dev,
732 br_forward_finish);
733 return NF_STOLEN;
736 static int br_nf_dev_queue_xmit(struct sk_buff *skb)
738 if (skb->protocol == htons(ETH_P_IP) &&
739 skb->len > skb->dev->mtu &&
740 !skb_is_gso(skb))
741 return ip_fragment(skb, br_dev_queue_push_xmit);
742 else
743 return br_dev_queue_push_xmit(skb);
746 /* PF_BRIDGE/POST_ROUTING ********************************************/
747 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
748 const struct net_device *in,
749 const struct net_device *out,
750 int (*okfn)(struct sk_buff *))
752 struct sk_buff *skb = *pskb;
753 struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
754 struct net_device *realoutdev = bridge_parent(skb->dev);
755 int pf;
757 #ifdef CONFIG_NETFILTER_DEBUG
758 /* Be very paranoid. This probably won't happen anymore, but let's
759 * keep the check just to be sure... */
760 if (skb_mac_header(skb) < skb->head ||
761 skb_mac_header(skb) + ETH_HLEN > skb->data) {
762 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
763 "bad mac.raw pointer.\n");
764 goto print_error;
766 #endif
768 if (!nf_bridge)
769 return NF_ACCEPT;
771 if (!realoutdev)
772 return NF_DROP;
774 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
775 pf = PF_INET;
776 else
777 pf = PF_INET6;
779 #ifdef CONFIG_NETFILTER_DEBUG
780 if (skb->dst == NULL) {
781 printk(KERN_INFO "br_netfilter post_routing: skb->dst == NULL\n");
782 goto print_error;
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 == htons(ETH_P_8021Q)) {
794 skb_pull(skb, VLAN_HLEN);
795 skb->network_header += 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_nf_dev_queue_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 (realoutdev)
814 printk("[%s]", realoutdev->name);
816 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb_mac_header(skb),
817 skb->data);
818 dump_stack();
819 return NF_ACCEPT;
820 #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,
828 const struct net_device *out,
829 int (*okfn)(struct sk_buff *))
831 if ((*pskb)->nf_bridge &&
832 !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
833 return NF_STOP;
836 return NF_ACCEPT;
839 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
840 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
841 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
842 * ip_refrag() can return NF_STOLEN. */
843 static struct nf_hook_ops br_nf_ops[] = {
844 { .hook = br_nf_pre_routing,
845 .owner = THIS_MODULE,
846 .pf = PF_BRIDGE,
847 .hooknum = NF_BR_PRE_ROUTING,
848 .priority = NF_BR_PRI_BRNF, },
849 { .hook = br_nf_local_in,
850 .owner = THIS_MODULE,
851 .pf = PF_BRIDGE,
852 .hooknum = NF_BR_LOCAL_IN,
853 .priority = NF_BR_PRI_BRNF, },
854 { .hook = br_nf_forward_ip,
855 .owner = THIS_MODULE,
856 .pf = PF_BRIDGE,
857 .hooknum = NF_BR_FORWARD,
858 .priority = NF_BR_PRI_BRNF - 1, },
859 { .hook = br_nf_forward_arp,
860 .owner = THIS_MODULE,
861 .pf = PF_BRIDGE,
862 .hooknum = NF_BR_FORWARD,
863 .priority = NF_BR_PRI_BRNF, },
864 { .hook = br_nf_local_out,
865 .owner = THIS_MODULE,
866 .pf = PF_BRIDGE,
867 .hooknum = NF_BR_LOCAL_OUT,
868 .priority = NF_BR_PRI_FIRST, },
869 { .hook = br_nf_post_routing,
870 .owner = THIS_MODULE,
871 .pf = PF_BRIDGE,
872 .hooknum = NF_BR_POST_ROUTING,
873 .priority = NF_BR_PRI_LAST, },
874 { .hook = ip_sabotage_in,
875 .owner = THIS_MODULE,
876 .pf = PF_INET,
877 .hooknum = NF_IP_PRE_ROUTING,
878 .priority = NF_IP_PRI_FIRST, },
879 { .hook = ip_sabotage_in,
880 .owner = THIS_MODULE,
881 .pf = PF_INET6,
882 .hooknum = NF_IP6_PRE_ROUTING,
883 .priority = NF_IP6_PRI_FIRST, },
886 #ifdef CONFIG_SYSCTL
887 static
888 int brnf_sysctl_call_tables(ctl_table * ctl, int write, struct file *filp,
889 void __user * buffer, size_t * lenp, loff_t * ppos)
891 int ret;
893 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
895 if (write && *(int *)(ctl->data))
896 *(int *)(ctl->data) = 1;
897 return ret;
900 static ctl_table brnf_table[] = {
902 .ctl_name = NET_BRIDGE_NF_CALL_ARPTABLES,
903 .procname = "bridge-nf-call-arptables",
904 .data = &brnf_call_arptables,
905 .maxlen = sizeof(int),
906 .mode = 0644,
907 .proc_handler = &brnf_sysctl_call_tables,
910 .ctl_name = NET_BRIDGE_NF_CALL_IPTABLES,
911 .procname = "bridge-nf-call-iptables",
912 .data = &brnf_call_iptables,
913 .maxlen = sizeof(int),
914 .mode = 0644,
915 .proc_handler = &brnf_sysctl_call_tables,
918 .ctl_name = NET_BRIDGE_NF_CALL_IP6TABLES,
919 .procname = "bridge-nf-call-ip6tables",
920 .data = &brnf_call_ip6tables,
921 .maxlen = sizeof(int),
922 .mode = 0644,
923 .proc_handler = &brnf_sysctl_call_tables,
926 .ctl_name = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
927 .procname = "bridge-nf-filter-vlan-tagged",
928 .data = &brnf_filter_vlan_tagged,
929 .maxlen = sizeof(int),
930 .mode = 0644,
931 .proc_handler = &brnf_sysctl_call_tables,
933 { .ctl_name = 0 }
936 static ctl_table brnf_bridge_table[] = {
938 .ctl_name = NET_BRIDGE,
939 .procname = "bridge",
940 .mode = 0555,
941 .child = brnf_table,
943 { .ctl_name = 0 }
946 static ctl_table brnf_net_table[] = {
948 .ctl_name = CTL_NET,
949 .procname = "net",
950 .mode = 0555,
951 .child = brnf_bridge_table,
953 { .ctl_name = 0 }
955 #endif
957 int __init br_netfilter_init(void)
959 int ret;
961 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
962 if (ret < 0)
963 return ret;
964 #ifdef CONFIG_SYSCTL
965 brnf_sysctl_header = register_sysctl_table(brnf_net_table);
966 if (brnf_sysctl_header == NULL) {
967 printk(KERN_WARNING
968 "br_netfilter: can't register to sysctl.\n");
969 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
970 return -ENOMEM;
972 #endif
973 printk(KERN_NOTICE "Bridge firewalling registered\n");
974 return 0;
977 void br_netfilter_fini(void)
979 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
980 #ifdef CONFIG_SYSCTL
981 unregister_sysctl_table(brnf_sysctl_header);
982 #endif