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