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