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