]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/bridge/br_netfilter.c
netfilter: ipv6: add IPSKB_REROUTED exclusion to NF_HOOK/POSTROUTING invocation
[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
LT
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 306 if (skb->dev) {
adf30907 307 struct dst_entry *dst = skb_dst(skb);
2948d2eb 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;
511c3f92 325 struct rtable *rt;
f216f082 326 int err;
1da177e4 327
1da177e4
LT
328 if (nf_bridge->mask & BRNF_PKT_TYPE) {
329 skb->pkt_type = PACKET_OTHERHOST;
330 nf_bridge->mask ^= BRNF_PKT_TYPE;
331 }
332 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
1da177e4 333 if (dnat_took_place(skb)) {
f216f082 334 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
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 };
f3abc9b9 344 struct in_device *in_dev = __in_dev_get_rcu(dev);
f216f082
BDS
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 355
249b6203 356 if (!ip_route_output_key(dev_net(dev), &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) {
adf30907 360 skb_dst_set(skb, (struct dst_entry *)rt);
1da177e4
LT
361 goto bridged_dnat;
362 }
1da177e4
LT
363 dst_release((struct dst_entry *)rt);
364 }
f216f082 365free_skb:
1da177e4
LT
366 kfree_skb(skb);
367 return 0;
368 } else {
adf30907 369 if (skb_dst(skb)->dev == dev) {
1da177e4
LT
370bridged_dnat:
371 /* Tell br_nf_local_out this is a
372 * bridged frame */
373 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
374 skb->dev = nf_bridge->physindev;
fc38582d 375 nf_bridge_push_encap_header(skb);
713aefa3
JE
376 NF_HOOK_THRESH(NFPROTO_BRIDGE,
377 NF_BR_PRE_ROUTING,
1da177e4
LT
378 skb, skb->dev, NULL,
379 br_nf_pre_routing_finish_bridge,
380 1);
381 return 0;
382 }
789bc3e5 383 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
1da177e4
LT
384 skb->pkt_type = PACKET_HOST;
385 }
386 } else {
511c3f92
ED
387 rt = bridge_parent_rtable(nf_bridge->physindev);
388 if (!rt) {
4adf0af6
SW
389 kfree_skb(skb);
390 return 0;
391 }
511c3f92 392 dst_hold(&rt->u.dst);
adf30907 393 skb_dst_set(skb, &rt->u.dst);
1da177e4
LT
394 }
395
396 skb->dev = nf_bridge->physindev;
fc38582d 397 nf_bridge_push_encap_header(skb);
713aefa3 398 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
1da177e4
LT
399 br_handle_frame_finish, 1);
400
401 return 0;
402}
403
404/* Some common code for IPv4/IPv6 */
5dce971a 405static struct net_device *setup_pre_routing(struct sk_buff *skb)
1da177e4
LT
406{
407 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
408
409 if (skb->pkt_type == PACKET_OTHERHOST) {
410 skb->pkt_type = PACKET_HOST;
411 nf_bridge->mask |= BRNF_PKT_TYPE;
412 }
413
414 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
415 nf_bridge->physindev = skb->dev;
416 skb->dev = bridge_parent(skb->dev);
5dce971a
SH
417
418 return skb->dev;
1da177e4
LT
419}
420
421/* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
422static int check_hbh_len(struct sk_buff *skb)
423{
0660e03f 424 unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
1da177e4 425 u32 pkt_len;
d56f90a7
ACM
426 const unsigned char *nh = skb_network_header(skb);
427 int off = raw - nh;
789bc3e5 428 int len = (raw[1] + 1) << 3;
1da177e4
LT
429
430 if ((raw + len) - skb->data > skb_headlen(skb))
431 goto bad;
432
433 off += 2;
434 len -= 2;
435
436 while (len > 0) {
d56f90a7 437 int optlen = nh[off + 1] + 2;
1da177e4 438
d56f90a7 439 switch (nh[off]) {
1da177e4
LT
440 case IPV6_TLV_PAD0:
441 optlen = 1;
442 break;
443
444 case IPV6_TLV_PADN:
445 break;
446
447 case IPV6_TLV_JUMBO:
d56f90a7 448 if (nh[off + 1] != 4 || (off & 3) != 2)
1da177e4 449 goto bad;
d56f90a7 450 pkt_len = ntohl(*(__be32 *) (nh + off + 2));
b0366486 451 if (pkt_len <= IPV6_MAXPLEN ||
0660e03f 452 ipv6_hdr(skb)->payload_len)
b0366486 453 goto bad;
1da177e4
LT
454 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
455 goto bad;
b0366486 456 if (pskb_trim_rcsum(skb,
789bc3e5 457 pkt_len + sizeof(struct ipv6hdr)))
b0366486 458 goto bad;
d56f90a7 459 nh = skb_network_header(skb);
1da177e4
LT
460 break;
461 default:
462 if (optlen > len)
463 goto bad;
464 break;
465 }
466 off += optlen;
467 len -= optlen;
468 }
469 if (len == 0)
470 return 0;
471bad:
472 return -1;
473
474}
475
476/* Replicate the checks that IPv6 does on packet reception and pass the packet
477 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
478static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
789bc3e5
SH
479 struct sk_buff *skb,
480 const struct net_device *in,
481 const struct net_device *out,
482 int (*okfn)(struct sk_buff *))
1da177e4
LT
483{
484 struct ipv6hdr *hdr;
485 u32 pkt_len;
1da177e4
LT
486
487 if (skb->len < sizeof(struct ipv6hdr))
488 goto inhdr_error;
489
490 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
491 goto inhdr_error;
492
0660e03f 493 hdr = ipv6_hdr(skb);
1da177e4
LT
494
495 if (hdr->version != 6)
496 goto inhdr_error;
497
498 pkt_len = ntohs(hdr->payload_len);
499
500 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
501 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
502 goto inhdr_error;
b38dfee3
HX
503 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
504 goto inhdr_error;
1da177e4
LT
505 }
506 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
789bc3e5 507 goto inhdr_error;
1da177e4 508
789bc3e5 509 nf_bridge_put(skb->nf_bridge);
fdeabdef 510 if (!nf_bridge_alloc(skb))
1da177e4 511 return NF_DROP;
5dce971a
SH
512 if (!setup_pre_routing(skb))
513 return NF_DROP;
1da177e4 514
713aefa3 515 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
1da177e4
LT
516 br_nf_pre_routing_finish_ipv6);
517
518 return NF_STOLEN;
519
520inhdr_error:
521 return NF_DROP;
522}
523
524/* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
525 * Replicate the checks that IPv4 does on packet reception.
526 * Set skb->dev to the bridge device (i.e. parent of the
527 * receiving device) to make netfilter happy, the REDIRECT
528 * target in particular. Save the original destination IP
529 * address to be able to detect DNAT afterwards. */
3db05fea 530static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff *skb,
ee02b3a6
SH
531 const struct net_device *in,
532 const struct net_device *out,
533 int (*okfn)(struct sk_buff *))
1da177e4
LT
534{
535 struct iphdr *iph;
e7c243c9
EP
536 __u32 len = nf_bridge_encap_header_len(skb);
537
e7c243c9
EP
538 if (unlikely(!pskb_may_pull(skb, len)))
539 goto out;
1da177e4 540
516299d2
MM
541 if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
542 IS_PPPOE_IPV6(skb)) {
1da177e4
LT
543#ifdef CONFIG_SYSCTL
544 if (!brnf_call_ip6tables)
545 return NF_ACCEPT;
546#endif
fc38582d 547 nf_bridge_pull_encap_header_rcsum(skb);
1da177e4
LT
548 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
549 }
550#ifdef CONFIG_SYSCTL
551 if (!brnf_call_iptables)
552 return NF_ACCEPT;
553#endif
554
516299d2
MM
555 if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb) &&
556 !IS_PPPOE_IP(skb))
1da177e4
LT
557 return NF_ACCEPT;
558
fc38582d 559 nf_bridge_pull_encap_header_rcsum(skb);
1da177e4
LT
560
561 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
562 goto inhdr_error;
563
eddc9ec5 564 iph = ip_hdr(skb);
1da177e4
LT
565 if (iph->ihl < 5 || iph->version != 4)
566 goto inhdr_error;
567
789bc3e5 568 if (!pskb_may_pull(skb, 4 * iph->ihl))
1da177e4
LT
569 goto inhdr_error;
570
eddc9ec5 571 iph = ip_hdr(skb);
789bc3e5 572 if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
1da177e4
LT
573 goto inhdr_error;
574
575 len = ntohs(iph->tot_len);
789bc3e5 576 if (skb->len < len || len < 4 * iph->ihl)
1da177e4
LT
577 goto inhdr_error;
578
b38dfee3 579 pskb_trim_rcsum(skb, len);
1da177e4 580
789bc3e5 581 nf_bridge_put(skb->nf_bridge);
fdeabdef 582 if (!nf_bridge_alloc(skb))
1da177e4 583 return NF_DROP;
5dce971a
SH
584 if (!setup_pre_routing(skb))
585 return NF_DROP;
1da177e4
LT
586 store_orig_dstaddr(skb);
587
713aefa3 588 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
1da177e4
LT
589 br_nf_pre_routing_finish);
590
591 return NF_STOLEN;
592
593inhdr_error:
789bc3e5 594// IP_INC_STATS_BH(IpInHdrErrors);
1da177e4
LT
595out:
596 return NF_DROP;
597}
598
599
600/* PF_BRIDGE/LOCAL_IN ************************************************/
601/* The packet is locally destined, which requires a real
602 * dst_entry, so detach the fake one. On the way up, the
603 * packet would pass through PRE_ROUTING again (which already
604 * took place when the packet entered the bridge), but we
605 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
606 * prevent this from happening. */
3db05fea 607static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff *skb,
789bc3e5
SH
608 const struct net_device *in,
609 const struct net_device *out,
610 int (*okfn)(struct sk_buff *))
1da177e4 611{
511c3f92
ED
612 struct rtable *rt = skb_rtable(skb);
613
adf30907
ED
614 if (rt && rt == bridge_parent_rtable(in))
615 skb_dst_drop(skb);
1da177e4
LT
616
617 return NF_ACCEPT;
618}
619
1da177e4
LT
620/* PF_BRIDGE/FORWARD *************************************************/
621static int br_nf_forward_finish(struct sk_buff *skb)
622{
623 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
624 struct net_device *in;
1da177e4 625
8b42ec39 626 if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
1da177e4
LT
627 in = nf_bridge->physindev;
628 if (nf_bridge->mask & BRNF_PKT_TYPE) {
629 skb->pkt_type = PACKET_OTHERHOST;
630 nf_bridge->mask ^= BRNF_PKT_TYPE;
631 }
632 } else {
633 in = *((struct net_device **)(skb->cb));
634 }
fc38582d 635 nf_bridge_push_encap_header(skb);
713aefa3 636 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, skb, in,
789bc3e5 637 skb->dev, br_forward_finish, 1);
1da177e4
LT
638 return 0;
639}
640
641/* This is the 'purely bridged' case. For IP, we pass the packet to
642 * netfilter with indev and outdev set to the bridge device,
643 * but we are still able to filter on the 'real' indev/outdev
644 * because of the physdev module. For ARP, indev and outdev are the
645 * bridge ports. */
3db05fea 646static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
789bc3e5
SH
647 const struct net_device *in,
648 const struct net_device *out,
649 int (*okfn)(struct sk_buff *))
1da177e4 650{
1da177e4 651 struct nf_bridge_info *nf_bridge;
5dce971a 652 struct net_device *parent;
76108cea 653 u_int8_t pf;
1da177e4
LT
654
655 if (!skb->nf_bridge)
656 return NF_ACCEPT;
657
2dc2f207
PM
658 /* Need exclusive nf_bridge_info since we might have multiple
659 * different physoutdevs. */
660 if (!nf_bridge_unshare(skb))
661 return NF_DROP;
662
5dce971a
SH
663 parent = bridge_parent(out);
664 if (!parent)
665 return NF_DROP;
666
516299d2
MM
667 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
668 IS_PPPOE_IP(skb))
1da177e4 669 pf = PF_INET;
a2bd40ad
HX
670 else if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
671 IS_PPPOE_IPV6(skb))
1da177e4 672 pf = PF_INET6;
a2bd40ad
HX
673 else
674 return NF_ACCEPT;
1da177e4 675
3db05fea 676 nf_bridge_pull_encap_header(skb);
1da177e4 677
1da177e4
LT
678 nf_bridge = skb->nf_bridge;
679 if (skb->pkt_type == PACKET_OTHERHOST) {
680 skb->pkt_type = PACKET_HOST;
681 nf_bridge->mask |= BRNF_PKT_TYPE;
682 }
683
684 /* The physdev module checks on this */
685 nf_bridge->mask |= BRNF_BRIDGED;
686 nf_bridge->physoutdev = skb->dev;
687
6e23ae2a 688 NF_HOOK(pf, NF_INET_FORWARD, skb, bridge_parent(in), parent,
5dce971a 689 br_nf_forward_finish);
1da177e4
LT
690
691 return NF_STOLEN;
692}
693
3db05fea 694static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff *skb,
789bc3e5
SH
695 const struct net_device *in,
696 const struct net_device *out,
697 int (*okfn)(struct sk_buff *))
1da177e4 698{
1da177e4
LT
699 struct net_device **d = (struct net_device **)(skb->cb);
700
701#ifdef CONFIG_SYSCTL
702 if (!brnf_call_arptables)
703 return NF_ACCEPT;
704#endif
705
f8a26028 706 if (skb->protocol != htons(ETH_P_ARP)) {
8b42ec39 707 if (!IS_VLAN_ARP(skb))
1da177e4 708 return NF_ACCEPT;
3db05fea 709 nf_bridge_pull_encap_header(skb);
1da177e4
LT
710 }
711
d0a92be0 712 if (arp_hdr(skb)->ar_pln != 4) {
fc38582d 713 if (IS_VLAN_ARP(skb))
3db05fea 714 nf_bridge_push_encap_header(skb);
1da177e4
LT
715 return NF_ACCEPT;
716 }
717 *d = (struct net_device *)in;
fdc9314c 718 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
1da177e4
LT
719 (struct net_device *)out, br_nf_forward_finish);
720
721 return NF_STOLEN;
722}
723
2bf540b7
PM
724/* PF_BRIDGE/LOCAL_OUT ***********************************************
725 *
726 * This function sees both locally originated IP packets and forwarded
1da177e4
LT
727 * IP packets (in both cases the destination device is a bridge
728 * device). It also sees bridged-and-DNAT'ed packets.
1da177e4
LT
729 *
730 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
731 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
732 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
733 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
734 * will be executed.
2bf540b7 735 */
3db05fea 736static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff *skb,
789bc3e5
SH
737 const struct net_device *in,
738 const struct net_device *out,
739 int (*okfn)(struct sk_buff *))
1da177e4 740{
2bf540b7 741 struct net_device *realindev;
1da177e4 742 struct nf_bridge_info *nf_bridge;
1da177e4
LT
743
744 if (!skb->nf_bridge)
745 return NF_ACCEPT;
746
2dc2f207
PM
747 /* Need exclusive nf_bridge_info since we might have multiple
748 * different physoutdevs. */
749 if (!nf_bridge_unshare(skb))
750 return NF_DROP;
751
1da177e4 752 nf_bridge = skb->nf_bridge;
2bf540b7
PM
753 if (!(nf_bridge->mask & BRNF_BRIDGED_DNAT))
754 return NF_ACCEPT;
1da177e4
LT
755
756 /* Bridged, take PF_BRIDGE/FORWARD.
757 * (see big note in front of br_nf_pre_routing_finish) */
2bf540b7
PM
758 nf_bridge->physoutdev = skb->dev;
759 realindev = nf_bridge->physindev;
1da177e4 760
2bf540b7
PM
761 if (nf_bridge->mask & BRNF_PKT_TYPE) {
762 skb->pkt_type = PACKET_OTHERHOST;
763 nf_bridge->mask ^= BRNF_PKT_TYPE;
1da177e4 764 }
fc38582d 765 nf_bridge_push_encap_header(skb);
1da177e4 766
713aefa3 767 NF_HOOK(NFPROTO_BRIDGE, NF_BR_FORWARD, skb, realindev, skb->dev,
2bf540b7 768 br_forward_finish);
1da177e4
LT
769 return NF_STOLEN;
770}
771
c197facc 772#if defined(CONFIG_NF_CONNTRACK_IPV4) || defined(CONFIG_NF_CONNTRACK_IPV4_MODULE)
2e2f7aef
PM
773static int br_nf_dev_queue_xmit(struct sk_buff *skb)
774{
c197facc 775 if (skb->nfct != NULL &&
776 (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb)) &&
2e2f7aef 777 skb->len > skb->dev->mtu &&
89114afd 778 !skb_is_gso(skb))
2e2f7aef
PM
779 return ip_fragment(skb, br_dev_queue_push_xmit);
780 else
781 return br_dev_queue_push_xmit(skb);
782}
c197facc 783#else
784static int br_nf_dev_queue_xmit(struct sk_buff *skb)
785{
786 return br_dev_queue_push_xmit(skb);
787}
788#endif
1da177e4
LT
789
790/* PF_BRIDGE/POST_ROUTING ********************************************/
3db05fea 791static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
789bc3e5
SH
792 const struct net_device *in,
793 const struct net_device *out,
794 int (*okfn)(struct sk_buff *))
1da177e4 795{
3db05fea 796 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
1da177e4 797 struct net_device *realoutdev = bridge_parent(skb->dev);
76108cea 798 u_int8_t pf;
1da177e4 799
1da177e4
LT
800 if (!nf_bridge)
801 return NF_ACCEPT;
802
81d9ddae
PM
803 if (!(nf_bridge->mask & (BRNF_BRIDGED | BRNF_BRIDGED_DNAT)))
804 return NF_ACCEPT;
805
5dce971a
SH
806 if (!realoutdev)
807 return NF_DROP;
808
516299d2
MM
809 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
810 IS_PPPOE_IP(skb))
1da177e4 811 pf = PF_INET;
a2bd40ad
HX
812 else if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
813 IS_PPPOE_IPV6(skb))
1da177e4 814 pf = PF_INET6;
a2bd40ad
HX
815 else
816 return NF_ACCEPT;
1da177e4 817
1da177e4
LT
818 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
819 * about the value of skb->pkt_type. */
820 if (skb->pkt_type == PACKET_OTHERHOST) {
821 skb->pkt_type = PACKET_HOST;
822 nf_bridge->mask |= BRNF_PKT_TYPE;
823 }
824
fc38582d 825 nf_bridge_pull_encap_header(skb);
1da177e4
LT
826 nf_bridge_save_header(skb);
827
6e23ae2a 828 NF_HOOK(pf, NF_INET_POST_ROUTING, skb, NULL, realoutdev,
2e2f7aef 829 br_nf_dev_queue_xmit);
1da177e4
LT
830
831 return NF_STOLEN;
1da177e4
LT
832}
833
1da177e4
LT
834/* IP/SABOTAGE *****************************************************/
835/* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
836 * for the second time. */
3db05fea 837static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff *skb,
789bc3e5
SH
838 const struct net_device *in,
839 const struct net_device *out,
840 int (*okfn)(struct sk_buff *))
1da177e4 841{
3db05fea
HX
842 if (skb->nf_bridge &&
843 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
1da177e4
LT
844 return NF_STOP;
845 }
846
847 return NF_ACCEPT;
848}
849
1da177e4
LT
850/* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
851 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
852 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
853 * ip_refrag() can return NF_STOLEN. */
1999414a 854static struct nf_hook_ops br_nf_ops[] __read_mostly = {
1490fd89
CG
855 {
856 .hook = br_nf_pre_routing,
857 .owner = THIS_MODULE,
858 .pf = PF_BRIDGE,
859 .hooknum = NF_BR_PRE_ROUTING,
860 .priority = NF_BR_PRI_BRNF,
861 },
862 {
863 .hook = br_nf_local_in,
864 .owner = THIS_MODULE,
865 .pf = PF_BRIDGE,
866 .hooknum = NF_BR_LOCAL_IN,
867 .priority = NF_BR_PRI_BRNF,
868 },
869 {
870 .hook = br_nf_forward_ip,
871 .owner = THIS_MODULE,
872 .pf = PF_BRIDGE,
873 .hooknum = NF_BR_FORWARD,
874 .priority = NF_BR_PRI_BRNF - 1,
875 },
876 {
877 .hook = br_nf_forward_arp,
878 .owner = THIS_MODULE,
879 .pf = PF_BRIDGE,
880 .hooknum = NF_BR_FORWARD,
881 .priority = NF_BR_PRI_BRNF,
882 },
883 {
884 .hook = br_nf_local_out,
885 .owner = THIS_MODULE,
886 .pf = PF_BRIDGE,
887 .hooknum = NF_BR_LOCAL_OUT,
888 .priority = NF_BR_PRI_FIRST,
889 },
890 {
891 .hook = br_nf_post_routing,
892 .owner = THIS_MODULE,
893 .pf = PF_BRIDGE,
894 .hooknum = NF_BR_POST_ROUTING,
895 .priority = NF_BR_PRI_LAST,
896 },
897 {
898 .hook = ip_sabotage_in,
899 .owner = THIS_MODULE,
900 .pf = PF_INET,
901 .hooknum = NF_INET_PRE_ROUTING,
902 .priority = NF_IP_PRI_FIRST,
903 },
904 {
905 .hook = ip_sabotage_in,
906 .owner = THIS_MODULE,
907 .pf = PF_INET6,
908 .hooknum = NF_INET_PRE_ROUTING,
909 .priority = NF_IP6_PRI_FIRST,
910 },
1da177e4
LT
911};
912
913#ifdef CONFIG_SYSCTL
914static
8d65af78 915int brnf_sysctl_call_tables(ctl_table * ctl, int write,
789bc3e5 916 void __user * buffer, size_t * lenp, loff_t * ppos)
1da177e4
LT
917{
918 int ret;
919
8d65af78 920 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1da177e4
LT
921
922 if (write && *(int *)(ctl->data))
923 *(int *)(ctl->data) = 1;
924 return ret;
925}
926
927static ctl_table brnf_table[] = {
928 {
1da177e4
LT
929 .procname = "bridge-nf-call-arptables",
930 .data = &brnf_call_arptables,
931 .maxlen = sizeof(int),
932 .mode = 0644,
6d9f239a 933 .proc_handler = brnf_sysctl_call_tables,
1da177e4
LT
934 },
935 {
1da177e4
LT
936 .procname = "bridge-nf-call-iptables",
937 .data = &brnf_call_iptables,
938 .maxlen = sizeof(int),
939 .mode = 0644,
6d9f239a 940 .proc_handler = brnf_sysctl_call_tables,
1da177e4
LT
941 },
942 {
1da177e4
LT
943 .procname = "bridge-nf-call-ip6tables",
944 .data = &brnf_call_ip6tables,
945 .maxlen = sizeof(int),
946 .mode = 0644,
6d9f239a 947 .proc_handler = brnf_sysctl_call_tables,
1da177e4
LT
948 },
949 {
1da177e4
LT
950 .procname = "bridge-nf-filter-vlan-tagged",
951 .data = &brnf_filter_vlan_tagged,
952 .maxlen = sizeof(int),
953 .mode = 0644,
6d9f239a 954 .proc_handler = brnf_sysctl_call_tables,
516299d2
MM
955 },
956 {
516299d2
MM
957 .procname = "bridge-nf-filter-pppoe-tagged",
958 .data = &brnf_filter_pppoe_tagged,
959 .maxlen = sizeof(int),
960 .mode = 0644,
6d9f239a 961 .proc_handler = brnf_sysctl_call_tables,
1da177e4 962 },
f8572d8f 963 { }
1da177e4
LT
964};
965
b5ccd792 966static struct ctl_path brnf_path[] = {
f8572d8f
EB
967 { .procname = "net", },
968 { .procname = "bridge", },
b5ccd792 969 { }
1da177e4
LT
970};
971#endif
972
5eb87f45 973int __init br_netfilter_init(void)
1da177e4 974{
5eb87f45 975 int ret;
1da177e4 976
5eb87f45
PM
977 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
978 if (ret < 0)
1da177e4 979 return ret;
1da177e4 980#ifdef CONFIG_SYSCTL
b5ccd792 981 brnf_sysctl_header = register_sysctl_paths(brnf_path, brnf_table);
1da177e4 982 if (brnf_sysctl_header == NULL) {
789bc3e5
SH
983 printk(KERN_WARNING
984 "br_netfilter: can't register to sysctl.\n");
5eb87f45
PM
985 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
986 return -ENOMEM;
1da177e4
LT
987 }
988#endif
1da177e4 989 printk(KERN_NOTICE "Bridge firewalling registered\n");
1da177e4
LT
990 return 0;
991}
992
993void br_netfilter_fini(void)
994{
5eb87f45 995 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1da177e4
LT
996#ifdef CONFIG_SYSCTL
997 unregister_sysctl_table(brnf_sysctl_header);
998#endif
999}