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