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