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