]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/bridge/br_netfilter.c
[SOCK]: Introduce sk_receive_skb
[net-next-2.6.git] / net / bridge / br_netfilter.c
CommitLineData
1da177e4
LT
1/*
2 * Handle firewalling
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 * Bart De Schuymer (maintainer) <bdschuym@pandora.be>
8 *
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)
15 *
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.
20 *
21 * Lennert dedicates this file to Kerstin Wurdinger.
22 */
23
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
44
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)
49
50#define has_bridge_parent(device) ((device)->br_port != NULL)
51#define bridge_parent(device) ((device)->br_port->br->dev)
52
53#ifdef CONFIG_SYSCTL
54static struct ctl_table_header *brnf_sysctl_header;
55static int brnf_call_iptables = 1;
56static int brnf_call_ip6tables = 1;
57static int brnf_call_arptables = 1;
58static int brnf_filter_vlan_tagged = 1;
59#else
60#define brnf_filter_vlan_tagged 1
61#endif
62
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)
72
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.
76 *
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. */
81static struct net_device __fake_net_device = {
82 .hard_header_len = ETH_HLEN
83};
84
85static 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},
92 }
93 },
94 .rt_flags = 0,
95};
96
97
98/* PF_BRIDGE/PRE_ROUTING *********************************************/
99/* Undo the changes made for ip6tables PREROUTING and continue the
100 * bridge PRE_ROUTING hook. */
101static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
102{
103 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
104
1da177e4
LT
105 if (nf_bridge->mask & BRNF_PKT_TYPE) {
106 skb->pkt_type = PACKET_OTHERHOST;
107 nf_bridge->mask ^= BRNF_PKT_TYPE;
108 }
109 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
110
111 skb->dst = (struct dst_entry *)&__fake_rtable;
112 dst_hold(skb->dst);
113
114 skb->dev = nf_bridge->physindev;
115 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
116 skb_push(skb, VLAN_HLEN);
117 skb->nh.raw -= VLAN_HLEN;
118 }
119 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
120 br_handle_frame_finish, 1);
121
122 return 0;
123}
124
125static void __br_dnat_complain(void)
126{
127 static unsigned long last_complaint;
128
129 if (jiffies - last_complaint >= 5 * HZ) {
130 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
131 "forwarding to be enabled\n");
132 last_complaint = jiffies;
133 }
134}
135
136/* This requires some explaining. If DNAT has taken place,
137 * we will need to fix up the destination Ethernet address,
138 * and this is a tricky process.
139 *
140 * There are two cases to consider:
141 * 1. The packet was DNAT'ed to a device in the same bridge
142 * port group as it was received on. We can still bridge
143 * the packet.
144 * 2. The packet was DNAT'ed to a different device, either
145 * a non-bridged device or another bridge port group.
146 * The packet will need to be routed.
147 *
148 * The correct way of distinguishing between these two cases is to
149 * call ip_route_input() and to look at skb->dst->dev, which is
150 * changed to the destination device if ip_route_input() succeeds.
151 *
152 * Let us first consider the case that ip_route_input() succeeds:
153 *
154 * If skb->dst->dev equals the logical bridge device the packet
155 * came in on, we can consider this bridging. We then call
156 * skb->dst->output() which will make the packet enter br_nf_local_out()
157 * not much later. In that function it is assured that the iptables
158 * FORWARD chain is traversed for the packet.
159 *
160 * Otherwise, the packet is considered to be routed and we just
161 * change the destination MAC address so that the packet will
162 * later be passed up to the IP stack to be routed.
163 *
164 * Let us now consider the case that ip_route_input() fails:
165 *
166 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
167 * will fail, while __ip_route_output_key() will return success. The source
168 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
169 * thinks we're handling a locally generated packet and won't care
170 * if IP forwarding is allowed. We send a warning message to the users's
171 * log telling her to put IP forwarding on.
172 *
173 * ip_route_input() will also fail if there is no route available.
174 * In that case we just drop the packet.
175 *
176 * --Lennert, 20020411
177 * --Bart, 20020416 (updated)
178 * --Bart, 20021007 (updated) */
179static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
180{
1da177e4
LT
181 if (skb->pkt_type == PACKET_OTHERHOST) {
182 skb->pkt_type = PACKET_HOST;
183 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
184 }
185 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
186
187 skb->dev = bridge_parent(skb->dev);
188 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
189 skb_pull(skb, VLAN_HLEN);
190 skb->nh.raw += VLAN_HLEN;
191 }
192 skb->dst->output(skb);
193 return 0;
194}
195
196static int br_nf_pre_routing_finish(struct sk_buff *skb)
197{
198 struct net_device *dev = skb->dev;
199 struct iphdr *iph = skb->nh.iph;
200 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
201
1da177e4
LT
202 if (nf_bridge->mask & BRNF_PKT_TYPE) {
203 skb->pkt_type = PACKET_OTHERHOST;
204 nf_bridge->mask ^= BRNF_PKT_TYPE;
205 }
206 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
207
208 if (dnat_took_place(skb)) {
209 if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
210 dev)) {
211 struct rtable *rt;
212 struct flowi fl = { .nl_u =
213 { .ip4_u = { .daddr = iph->daddr, .saddr = 0 ,
214 .tos = RT_TOS(iph->tos)} }, .proto = 0};
215
216 if (!ip_route_output_key(&rt, &fl)) {
1c011bed
BDS
217 /* - Bridged-and-DNAT'ed traffic doesn't
218 * require ip_forwarding.
219 * - Deal with redirected traffic. */
220 if (((struct dst_entry *)rt)->dev == dev ||
221 rt->rt_type == RTN_LOCAL) {
1da177e4
LT
222 skb->dst = (struct dst_entry *)rt;
223 goto bridged_dnat;
224 }
225 __br_dnat_complain();
226 dst_release((struct dst_entry *)rt);
227 }
228 kfree_skb(skb);
229 return 0;
230 } else {
231 if (skb->dst->dev == dev) {
232bridged_dnat:
233 /* Tell br_nf_local_out this is a
234 * bridged frame */
235 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
236 skb->dev = nf_bridge->physindev;
237 if (skb->protocol ==
238 __constant_htons(ETH_P_8021Q)) {
239 skb_push(skb, VLAN_HLEN);
240 skb->nh.raw -= VLAN_HLEN;
241 }
242 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
243 skb, skb->dev, NULL,
244 br_nf_pre_routing_finish_bridge,
245 1);
246 return 0;
247 }
248 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr,
249 ETH_ALEN);
250 skb->pkt_type = PACKET_HOST;
251 }
252 } else {
253 skb->dst = (struct dst_entry *)&__fake_rtable;
254 dst_hold(skb->dst);
255 }
256
257 skb->dev = nf_bridge->physindev;
258 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
259 skb_push(skb, VLAN_HLEN);
260 skb->nh.raw -= VLAN_HLEN;
261 }
262 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
263 br_handle_frame_finish, 1);
264
265 return 0;
266}
267
268/* Some common code for IPv4/IPv6 */
269static void setup_pre_routing(struct sk_buff *skb)
270{
271 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
272
273 if (skb->pkt_type == PACKET_OTHERHOST) {
274 skb->pkt_type = PACKET_HOST;
275 nf_bridge->mask |= BRNF_PKT_TYPE;
276 }
277
278 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
279 nf_bridge->physindev = skb->dev;
280 skb->dev = bridge_parent(skb->dev);
281}
282
283/* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
284static int check_hbh_len(struct sk_buff *skb)
285{
286 unsigned char *raw = (u8*)(skb->nh.ipv6h+1);
287 u32 pkt_len;
288 int off = raw - skb->nh.raw;
289 int len = (raw[1]+1)<<3;
290
291 if ((raw + len) - skb->data > skb_headlen(skb))
292 goto bad;
293
294 off += 2;
295 len -= 2;
296
297 while (len > 0) {
b0366486 298 int optlen = skb->nh.raw[off+1]+2;
1da177e4
LT
299
300 switch (skb->nh.raw[off]) {
301 case IPV6_TLV_PAD0:
302 optlen = 1;
303 break;
304
305 case IPV6_TLV_PADN:
306 break;
307
308 case IPV6_TLV_JUMBO:
309 if (skb->nh.raw[off+1] != 4 || (off&3) != 2)
310 goto bad;
1da177e4 311 pkt_len = ntohl(*(u32*)(skb->nh.raw+off+2));
b0366486
BDS
312 if (pkt_len <= IPV6_MAXPLEN ||
313 skb->nh.ipv6h->payload_len)
314 goto bad;
1da177e4
LT
315 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
316 goto bad;
b0366486
BDS
317 if (pskb_trim_rcsum(skb,
318 pkt_len+sizeof(struct ipv6hdr)))
319 goto bad;
1da177e4
LT
320 break;
321 default:
322 if (optlen > len)
323 goto bad;
324 break;
325 }
326 off += optlen;
327 len -= optlen;
328 }
329 if (len == 0)
330 return 0;
331bad:
332 return -1;
333
334}
335
336/* Replicate the checks that IPv6 does on packet reception and pass the packet
337 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
338static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
339 struct sk_buff *skb, const struct net_device *in,
340 const struct net_device *out, int (*okfn)(struct sk_buff *))
341{
342 struct ipv6hdr *hdr;
343 u32 pkt_len;
344 struct nf_bridge_info *nf_bridge;
345
346 if (skb->len < sizeof(struct ipv6hdr))
347 goto inhdr_error;
348
349 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
350 goto inhdr_error;
351
352 hdr = skb->nh.ipv6h;
353
354 if (hdr->version != 6)
355 goto inhdr_error;
356
357 pkt_len = ntohs(hdr->payload_len);
358
359 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
360 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
361 goto inhdr_error;
362 if (pkt_len + sizeof(struct ipv6hdr) < skb->len) {
363 if (__pskb_trim(skb, pkt_len + sizeof(struct ipv6hdr)))
364 goto inhdr_error;
365 if (skb->ip_summed == CHECKSUM_HW)
366 skb->ip_summed = CHECKSUM_NONE;
367 }
368 }
369 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
370 goto inhdr_error;
371
79cac2a2 372 nf_bridge_put(skb->nf_bridge);
1da177e4
LT
373 if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
374 return NF_DROP;
375 setup_pre_routing(skb);
376
377 NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
378 br_nf_pre_routing_finish_ipv6);
379
380 return NF_STOLEN;
381
382inhdr_error:
383 return NF_DROP;
384}
385
386/* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
387 * Replicate the checks that IPv4 does on packet reception.
388 * Set skb->dev to the bridge device (i.e. parent of the
389 * receiving device) to make netfilter happy, the REDIRECT
390 * target in particular. Save the original destination IP
391 * address to be able to detect DNAT afterwards. */
392static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
393 const struct net_device *in, const struct net_device *out,
394 int (*okfn)(struct sk_buff *))
395{
396 struct iphdr *iph;
397 __u32 len;
398 struct sk_buff *skb = *pskb;
399 struct nf_bridge_info *nf_bridge;
400 struct vlan_ethhdr *hdr = vlan_eth_hdr(*pskb);
401
402 if (skb->protocol == __constant_htons(ETH_P_IPV6) || IS_VLAN_IPV6) {
403#ifdef CONFIG_SYSCTL
404 if (!brnf_call_ip6tables)
405 return NF_ACCEPT;
406#endif
407 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
408 goto out;
409
410 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
411 skb_pull(skb, VLAN_HLEN);
412 (skb)->nh.raw += VLAN_HLEN;
413 }
414 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
415 }
416#ifdef CONFIG_SYSCTL
417 if (!brnf_call_iptables)
418 return NF_ACCEPT;
419#endif
420
421 if (skb->protocol != __constant_htons(ETH_P_IP) && !IS_VLAN_IP)
422 return NF_ACCEPT;
423
424 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
425 goto out;
426
427 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
428 skb_pull(skb, VLAN_HLEN);
429 (skb)->nh.raw += VLAN_HLEN;
430 }
431
432 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
433 goto inhdr_error;
434
435 iph = skb->nh.iph;
436 if (iph->ihl < 5 || iph->version != 4)
437 goto inhdr_error;
438
439 if (!pskb_may_pull(skb, 4*iph->ihl))
440 goto inhdr_error;
441
442 iph = skb->nh.iph;
443 if (ip_fast_csum((__u8 *)iph, iph->ihl) != 0)
444 goto inhdr_error;
445
446 len = ntohs(iph->tot_len);
447 if (skb->len < len || len < 4*iph->ihl)
448 goto inhdr_error;
449
450 if (skb->len > len) {
451 __pskb_trim(skb, len);
452 if (skb->ip_summed == CHECKSUM_HW)
453 skb->ip_summed = CHECKSUM_NONE;
454 }
455
79cac2a2 456 nf_bridge_put(skb->nf_bridge);
1da177e4
LT
457 if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
458 return NF_DROP;
459 setup_pre_routing(skb);
460 store_orig_dstaddr(skb);
461
462 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
463 br_nf_pre_routing_finish);
464
465 return NF_STOLEN;
466
467inhdr_error:
468// IP_INC_STATS_BH(IpInHdrErrors);
469out:
470 return NF_DROP;
471}
472
473
474/* PF_BRIDGE/LOCAL_IN ************************************************/
475/* The packet is locally destined, which requires a real
476 * dst_entry, so detach the fake one. On the way up, the
477 * packet would pass through PRE_ROUTING again (which already
478 * took place when the packet entered the bridge), but we
479 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
480 * prevent this from happening. */
481static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
482 const struct net_device *in, const struct net_device *out,
483 int (*okfn)(struct sk_buff *))
484{
485 struct sk_buff *skb = *pskb;
486
487 if (skb->dst == (struct dst_entry *)&__fake_rtable) {
488 dst_release(skb->dst);
489 skb->dst = NULL;
490 }
491
492 return NF_ACCEPT;
493}
494
495
496/* PF_BRIDGE/FORWARD *************************************************/
497static int br_nf_forward_finish(struct sk_buff *skb)
498{
499 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
500 struct net_device *in;
501 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
502
1da177e4
LT
503 if (skb->protocol != __constant_htons(ETH_P_ARP) && !IS_VLAN_ARP) {
504 in = nf_bridge->physindev;
505 if (nf_bridge->mask & BRNF_PKT_TYPE) {
506 skb->pkt_type = PACKET_OTHERHOST;
507 nf_bridge->mask ^= BRNF_PKT_TYPE;
508 }
509 } else {
510 in = *((struct net_device **)(skb->cb));
511 }
512 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
513 skb_push(skb, VLAN_HLEN);
514 skb->nh.raw -= VLAN_HLEN;
515 }
516 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
517 skb->dev, br_forward_finish, 1);
518 return 0;
519}
520
521/* This is the 'purely bridged' case. For IP, we pass the packet to
522 * netfilter with indev and outdev set to the bridge device,
523 * but we are still able to filter on the 'real' indev/outdev
524 * because of the physdev module. For ARP, indev and outdev are the
525 * bridge ports. */
526static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
527 const struct net_device *in, const struct net_device *out,
528 int (*okfn)(struct sk_buff *))
529{
530 struct sk_buff *skb = *pskb;
531 struct nf_bridge_info *nf_bridge;
532 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
533 int pf;
534
535 if (!skb->nf_bridge)
536 return NF_ACCEPT;
537
538 if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
539 pf = PF_INET;
540 else
541 pf = PF_INET6;
542
543 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
544 skb_pull(*pskb, VLAN_HLEN);
545 (*pskb)->nh.raw += VLAN_HLEN;
546 }
547
1da177e4
LT
548 nf_bridge = skb->nf_bridge;
549 if (skb->pkt_type == PACKET_OTHERHOST) {
550 skb->pkt_type = PACKET_HOST;
551 nf_bridge->mask |= BRNF_PKT_TYPE;
552 }
553
554 /* The physdev module checks on this */
555 nf_bridge->mask |= BRNF_BRIDGED;
556 nf_bridge->physoutdev = skb->dev;
557
558 NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in),
559 bridge_parent(out), br_nf_forward_finish);
560
561 return NF_STOLEN;
562}
563
564static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
565 const struct net_device *in, const struct net_device *out,
566 int (*okfn)(struct sk_buff *))
567{
568 struct sk_buff *skb = *pskb;
569 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
570 struct net_device **d = (struct net_device **)(skb->cb);
571
572#ifdef CONFIG_SYSCTL
573 if (!brnf_call_arptables)
574 return NF_ACCEPT;
575#endif
576
577 if (skb->protocol != __constant_htons(ETH_P_ARP)) {
578 if (!IS_VLAN_ARP)
579 return NF_ACCEPT;
580 skb_pull(*pskb, VLAN_HLEN);
581 (*pskb)->nh.raw += VLAN_HLEN;
582 }
583
1da177e4
LT
584 if (skb->nh.arph->ar_pln != 4) {
585 if (IS_VLAN_ARP) {
586 skb_push(*pskb, VLAN_HLEN);
587 (*pskb)->nh.raw -= VLAN_HLEN;
588 }
589 return NF_ACCEPT;
590 }
591 *d = (struct net_device *)in;
592 NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
593 (struct net_device *)out, br_nf_forward_finish);
594
595 return NF_STOLEN;
596}
597
598
599/* PF_BRIDGE/LOCAL_OUT ***********************************************/
600static int br_nf_local_out_finish(struct sk_buff *skb)
601{
1da177e4
LT
602 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
603 skb_push(skb, VLAN_HLEN);
604 skb->nh.raw -= VLAN_HLEN;
605 }
606
607 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
608 br_forward_finish, NF_BR_PRI_FIRST + 1);
609
610 return 0;
611}
612
613/* This function sees both locally originated IP packets and forwarded
614 * IP packets (in both cases the destination device is a bridge
615 * device). It also sees bridged-and-DNAT'ed packets.
616 * To be able to filter on the physical bridge devices (with the physdev
617 * module), we steal packets destined to a bridge device away from the
618 * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
619 * when we have determined the real output device. This is done in here.
620 *
621 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
622 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
623 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
624 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
625 * will be executed.
626 * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
627 * this packet before, and so the packet was locally originated. We fake
628 * the PF_INET/LOCAL_OUT hook.
629 * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
630 * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
631 * even routed packets that didn't arrive on a bridge interface have their
632 * nf_bridge->physindev set. */
633static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
634 const struct net_device *in, const struct net_device *out,
635 int (*okfn)(struct sk_buff *))
636{
637 struct net_device *realindev, *realoutdev;
638 struct sk_buff *skb = *pskb;
639 struct nf_bridge_info *nf_bridge;
640 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
641 int pf;
642
643 if (!skb->nf_bridge)
644 return NF_ACCEPT;
645
646 if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
647 pf = PF_INET;
648 else
649 pf = PF_INET6;
650
651#ifdef CONFIG_NETFILTER_DEBUG
652 /* Sometimes we get packets with NULL ->dst here (for example,
653 * running a dhcp client daemon triggers this). This should now
654 * be fixed, but let's keep the check around. */
655 if (skb->dst == NULL) {
656 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
657 return NF_ACCEPT;
658 }
659#endif
660
661 nf_bridge = skb->nf_bridge;
662 nf_bridge->physoutdev = skb->dev;
663 realindev = nf_bridge->physindev;
664
665 /* Bridged, take PF_BRIDGE/FORWARD.
666 * (see big note in front of br_nf_pre_routing_finish) */
667 if (nf_bridge->mask & BRNF_BRIDGED_DNAT) {
668 if (nf_bridge->mask & BRNF_PKT_TYPE) {
669 skb->pkt_type = PACKET_OTHERHOST;
670 nf_bridge->mask ^= BRNF_PKT_TYPE;
671 }
672 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
673 skb_push(skb, VLAN_HLEN);
674 skb->nh.raw -= VLAN_HLEN;
675 }
676
677 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev,
678 skb->dev, br_forward_finish);
679 goto out;
680 }
681 realoutdev = bridge_parent(skb->dev);
682
683#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
684 /* iptables should match -o br0.x */
685 if (nf_bridge->netoutdev)
686 realoutdev = nf_bridge->netoutdev;
687#endif
688 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
689 skb_pull(skb, VLAN_HLEN);
690 (*pskb)->nh.raw += VLAN_HLEN;
691 }
692 /* IP forwarded traffic has a physindev, locally
693 * generated traffic hasn't. */
694 if (realindev != NULL) {
695 if (!(nf_bridge->mask & BRNF_DONT_TAKE_PARENT) &&
696 has_bridge_parent(realindev))
697 realindev = bridge_parent(realindev);
698
699 NF_HOOK_THRESH(pf, NF_IP_FORWARD, skb, realindev,
700 realoutdev, br_nf_local_out_finish,
701 NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD + 1);
702 } else {
1da177e4
LT
703 NF_HOOK_THRESH(pf, NF_IP_LOCAL_OUT, skb, realindev,
704 realoutdev, br_nf_local_out_finish,
705 NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT + 1);
706 }
707
708out:
709 return NF_STOLEN;
710}
711
712
713/* PF_BRIDGE/POST_ROUTING ********************************************/
714static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
715 const struct net_device *in, const struct net_device *out,
716 int (*okfn)(struct sk_buff *))
717{
718 struct sk_buff *skb = *pskb;
719 struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
720 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
721 struct net_device *realoutdev = bridge_parent(skb->dev);
722 int pf;
723
724#ifdef CONFIG_NETFILTER_DEBUG
725 /* Be very paranoid. This probably won't happen anymore, but let's
726 * keep the check just to be sure... */
727 if (skb->mac.raw < skb->head || skb->mac.raw + ETH_HLEN > skb->data) {
728 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
729 "bad mac.raw pointer.");
730 goto print_error;
731 }
732#endif
733
734 if (!nf_bridge)
735 return NF_ACCEPT;
736
737 if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
738 pf = PF_INET;
739 else
740 pf = PF_INET6;
741
742#ifdef CONFIG_NETFILTER_DEBUG
743 if (skb->dst == NULL) {
744 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
745 goto print_error;
746 }
1da177e4
LT
747#endif
748
749 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
750 * about the value of skb->pkt_type. */
751 if (skb->pkt_type == PACKET_OTHERHOST) {
752 skb->pkt_type = PACKET_HOST;
753 nf_bridge->mask |= BRNF_PKT_TYPE;
754 }
755
756 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
757 skb_pull(skb, VLAN_HLEN);
758 skb->nh.raw += VLAN_HLEN;
759 }
760
761 nf_bridge_save_header(skb);
762
763#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
764 if (nf_bridge->netoutdev)
765 realoutdev = nf_bridge->netoutdev;
766#endif
767 NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev,
768 br_dev_queue_push_xmit);
769
770 return NF_STOLEN;
771
772#ifdef CONFIG_NETFILTER_DEBUG
773print_error:
774 if (skb->dev != NULL) {
775 printk("[%s]", skb->dev->name);
776 if (has_bridge_parent(skb->dev))
777 printk("[%s]", bridge_parent(skb->dev)->name);
778 }
779 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb->mac.raw,
780 skb->data);
781 return NF_ACCEPT;
782#endif
783}
784
785
786/* IP/SABOTAGE *****************************************************/
787/* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
788 * for the second time. */
789static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff **pskb,
790 const struct net_device *in, const struct net_device *out,
791 int (*okfn)(struct sk_buff *))
792{
793 if ((*pskb)->nf_bridge &&
794 !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
795 return NF_STOP;
796 }
797
798 return NF_ACCEPT;
799}
800
801/* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
802 * and PF_INET(6)/POST_ROUTING until we have done the forwarding
803 * decision in the bridge code and have determined nf_bridge->physoutdev. */
804static unsigned int ip_sabotage_out(unsigned int hook, struct sk_buff **pskb,
805 const struct net_device *in, const struct net_device *out,
806 int (*okfn)(struct sk_buff *))
807{
808 struct sk_buff *skb = *pskb;
809
810 if ((out->hard_start_xmit == br_dev_xmit &&
811 okfn != br_nf_forward_finish &&
812 okfn != br_nf_local_out_finish &&
813 okfn != br_dev_queue_push_xmit)
814#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
815 || ((out->priv_flags & IFF_802_1Q_VLAN) &&
816 VLAN_DEV_INFO(out)->real_dev->hard_start_xmit == br_dev_xmit)
817#endif
818 ) {
819 struct nf_bridge_info *nf_bridge;
820
821 if (!skb->nf_bridge) {
822#ifdef CONFIG_SYSCTL
823 /* This code is executed while in the IP(v6) stack,
824 the version should be 4 or 6. We can't use
825 skb->protocol because that isn't set on
826 PF_INET(6)/LOCAL_OUT. */
827 struct iphdr *ip = skb->nh.iph;
828
829 if (ip->version == 4 && !brnf_call_iptables)
830 return NF_ACCEPT;
831 else if (ip->version == 6 && !brnf_call_ip6tables)
832 return NF_ACCEPT;
833#endif
834 if (hook == NF_IP_POST_ROUTING)
835 return NF_ACCEPT;
836 if (!nf_bridge_alloc(skb))
837 return NF_DROP;
838 }
839
840 nf_bridge = skb->nf_bridge;
841
842 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
843 * will need the indev then. For a brouter, the real indev
844 * can be a bridge port, so we make sure br_nf_local_out()
845 * doesn't use the bridge parent of the indev by using
846 * the BRNF_DONT_TAKE_PARENT mask. */
847 if (hook == NF_IP_FORWARD && nf_bridge->physindev == NULL) {
9666dae5 848 nf_bridge->mask |= BRNF_DONT_TAKE_PARENT;
1da177e4
LT
849 nf_bridge->physindev = (struct net_device *)in;
850 }
851#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
852 /* the iptables outdev is br0.x, not br0 */
853 if (out->priv_flags & IFF_802_1Q_VLAN)
854 nf_bridge->netoutdev = (struct net_device *)out;
855#endif
856 return NF_STOP;
857 }
858
859 return NF_ACCEPT;
860}
861
862/* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
863 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
864 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
865 * ip_refrag() can return NF_STOLEN. */
866static struct nf_hook_ops br_nf_ops[] = {
867 { .hook = br_nf_pre_routing,
868 .owner = THIS_MODULE,
869 .pf = PF_BRIDGE,
870 .hooknum = NF_BR_PRE_ROUTING,
871 .priority = NF_BR_PRI_BRNF, },
872 { .hook = br_nf_local_in,
873 .owner = THIS_MODULE,
874 .pf = PF_BRIDGE,
875 .hooknum = NF_BR_LOCAL_IN,
876 .priority = NF_BR_PRI_BRNF, },
877 { .hook = br_nf_forward_ip,
878 .owner = THIS_MODULE,
879 .pf = PF_BRIDGE,
880 .hooknum = NF_BR_FORWARD,
881 .priority = NF_BR_PRI_BRNF - 1, },
882 { .hook = br_nf_forward_arp,
883 .owner = THIS_MODULE,
884 .pf = PF_BRIDGE,
885 .hooknum = NF_BR_FORWARD,
886 .priority = NF_BR_PRI_BRNF, },
887 { .hook = br_nf_local_out,
888 .owner = THIS_MODULE,
889 .pf = PF_BRIDGE,
890 .hooknum = NF_BR_LOCAL_OUT,
891 .priority = NF_BR_PRI_FIRST, },
892 { .hook = br_nf_post_routing,
893 .owner = THIS_MODULE,
894 .pf = PF_BRIDGE,
895 .hooknum = NF_BR_POST_ROUTING,
896 .priority = NF_BR_PRI_LAST, },
897 { .hook = ip_sabotage_in,
898 .owner = THIS_MODULE,
899 .pf = PF_INET,
900 .hooknum = NF_IP_PRE_ROUTING,
901 .priority = NF_IP_PRI_FIRST, },
902 { .hook = ip_sabotage_in,
903 .owner = THIS_MODULE,
904 .pf = PF_INET6,
905 .hooknum = NF_IP6_PRE_ROUTING,
906 .priority = NF_IP6_PRI_FIRST, },
907 { .hook = ip_sabotage_out,
908 .owner = THIS_MODULE,
909 .pf = PF_INET,
910 .hooknum = NF_IP_FORWARD,
911 .priority = NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD, },
912 { .hook = ip_sabotage_out,
913 .owner = THIS_MODULE,
914 .pf = PF_INET6,
915 .hooknum = NF_IP6_FORWARD,
916 .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD, },
917 { .hook = ip_sabotage_out,
918 .owner = THIS_MODULE,
919 .pf = PF_INET,
920 .hooknum = NF_IP_LOCAL_OUT,
921 .priority = NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
922 { .hook = ip_sabotage_out,
923 .owner = THIS_MODULE,
924 .pf = PF_INET6,
925 .hooknum = NF_IP6_LOCAL_OUT,
926 .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
927 { .hook = ip_sabotage_out,
928 .owner = THIS_MODULE,
929 .pf = PF_INET,
930 .hooknum = NF_IP_POST_ROUTING,
931 .priority = NF_IP_PRI_FIRST, },
932 { .hook = ip_sabotage_out,
933 .owner = THIS_MODULE,
934 .pf = PF_INET6,
935 .hooknum = NF_IP6_POST_ROUTING,
936 .priority = NF_IP6_PRI_FIRST, },
937};
938
939#ifdef CONFIG_SYSCTL
940static
941int brnf_sysctl_call_tables(ctl_table *ctl, int write, struct file * filp,
942 void __user *buffer, size_t *lenp, loff_t *ppos)
943{
944 int ret;
945
946 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
947
948 if (write && *(int *)(ctl->data))
949 *(int *)(ctl->data) = 1;
950 return ret;
951}
952
953static ctl_table brnf_table[] = {
954 {
955 .ctl_name = NET_BRIDGE_NF_CALL_ARPTABLES,
956 .procname = "bridge-nf-call-arptables",
957 .data = &brnf_call_arptables,
958 .maxlen = sizeof(int),
959 .mode = 0644,
960 .proc_handler = &brnf_sysctl_call_tables,
961 },
962 {
963 .ctl_name = NET_BRIDGE_NF_CALL_IPTABLES,
964 .procname = "bridge-nf-call-iptables",
965 .data = &brnf_call_iptables,
966 .maxlen = sizeof(int),
967 .mode = 0644,
968 .proc_handler = &brnf_sysctl_call_tables,
969 },
970 {
971 .ctl_name = NET_BRIDGE_NF_CALL_IP6TABLES,
972 .procname = "bridge-nf-call-ip6tables",
973 .data = &brnf_call_ip6tables,
974 .maxlen = sizeof(int),
975 .mode = 0644,
976 .proc_handler = &brnf_sysctl_call_tables,
977 },
978 {
979 .ctl_name = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
980 .procname = "bridge-nf-filter-vlan-tagged",
981 .data = &brnf_filter_vlan_tagged,
982 .maxlen = sizeof(int),
983 .mode = 0644,
984 .proc_handler = &brnf_sysctl_call_tables,
985 },
986 { .ctl_name = 0 }
987};
988
989static ctl_table brnf_bridge_table[] = {
990 {
991 .ctl_name = NET_BRIDGE,
992 .procname = "bridge",
993 .mode = 0555,
994 .child = brnf_table,
995 },
996 { .ctl_name = 0 }
997};
998
999static ctl_table brnf_net_table[] = {
1000 {
1001 .ctl_name = CTL_NET,
1002 .procname = "net",
1003 .mode = 0555,
1004 .child = brnf_bridge_table,
1005 },
1006 { .ctl_name = 0 }
1007};
1008#endif
1009
1010int br_netfilter_init(void)
1011{
1012 int i;
1013
1014 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) {
1015 int ret;
1016
1017 if ((ret = nf_register_hook(&br_nf_ops[i])) >= 0)
1018 continue;
1019
1020 while (i--)
1021 nf_unregister_hook(&br_nf_ops[i]);
1022
1023 return ret;
1024 }
1025
1026#ifdef CONFIG_SYSCTL
1027 brnf_sysctl_header = register_sysctl_table(brnf_net_table, 0);
1028 if (brnf_sysctl_header == NULL) {
1029 printk(KERN_WARNING "br_netfilter: can't register to sysctl.\n");
1030 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++)
1031 nf_unregister_hook(&br_nf_ops[i]);
1032 return -EFAULT;
1033 }
1034#endif
1035
1036 printk(KERN_NOTICE "Bridge firewalling registered\n");
1037
1038 return 0;
1039}
1040
1041void br_netfilter_fini(void)
1042{
1043 int i;
1044
1045 for (i = ARRAY_SIZE(br_nf_ops) - 1; i >= 0; i--)
1046 nf_unregister_hook(&br_nf_ops[i]);
1047#ifdef CONFIG_SYSCTL
1048 unregister_sysctl_table(brnf_sysctl_header);
1049#endif
1050}