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