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