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