]> Git Repo - linux.git/blame - net/bridge/br_netfilter.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[linux.git] / net / bridge / br_netfilter.c
CommitLineData
1da177e4
LT
1/*
2 * Handle firewalling
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <[email protected]>
7 * Bart De Schuymer (maintainer) <[email protected]>
8 *
9 * Changes:
10 * Apr 29 2003: physdev module support (bdschuym)
11 * Jun 19 2003: let arptables see bridged ARP traffic (bdschuym)
12 * Oct 06 2003: filter encapsulated IP/ARP VLAN traffic on untagged bridge
13 * (bdschuym)
14 * Sep 01 2004: add IPv6 filtering (bdschuym)
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
20 *
21 * Lennert dedicates this file to Kerstin Wurdinger.
22 */
23
24#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/ip.h>
27#include <linux/netdevice.h>
28#include <linux/skbuff.h>
14c85021 29#include <linux/if_arp.h>
1da177e4
LT
30#include <linux/if_ether.h>
31#include <linux/if_vlan.h>
516299d2
MM
32#include <linux/if_pppox.h>
33#include <linux/ppp_defs.h>
1da177e4
LT
34#include <linux/netfilter_bridge.h>
35#include <linux/netfilter_ipv4.h>
36#include <linux/netfilter_ipv6.h>
37#include <linux/netfilter_arp.h>
38#include <linux/in_route.h>
f216f082 39#include <linux/inetdevice.h>
14c85021 40
1da177e4
LT
41#include <net/ip.h>
42#include <net/ipv6.h>
14c85021
ACM
43#include <net/route.h>
44
1da177e4 45#include <asm/uaccess.h>
1da177e4
LT
46#include "br_private.h"
47#ifdef CONFIG_SYSCTL
48#include <linux/sysctl.h>
49#endif
50
51#define skb_origaddr(skb) (((struct bridge_skb_cb *) \
52 (skb->nf_bridge->data))->daddr.ipv4)
eddc9ec5
ACM
53#define store_orig_dstaddr(skb) (skb_origaddr(skb) = ip_hdr(skb)->daddr)
54#define dnat_took_place(skb) (skb_origaddr(skb) != ip_hdr(skb)->daddr)
1da177e4 55
1da177e4
LT
56#ifdef CONFIG_SYSCTL
57static struct ctl_table_header *brnf_sysctl_header;
9c1ea148
BH
58static int brnf_call_iptables __read_mostly = 1;
59static int brnf_call_ip6tables __read_mostly = 1;
60static int brnf_call_arptables __read_mostly = 1;
61static int brnf_filter_vlan_tagged __read_mostly = 1;
516299d2 62static int brnf_filter_pppoe_tagged __read_mostly = 1;
1da177e4
LT
63#else
64#define brnf_filter_vlan_tagged 1
516299d2 65#define brnf_filter_pppoe_tagged 1
1da177e4
LT
66#endif
67
b6f99a21 68static inline __be16 vlan_proto(const struct sk_buff *skb)
8b42ec39
SH
69{
70 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
71}
72
73#define IS_VLAN_IP(skb) \
74 (skb->protocol == htons(ETH_P_8021Q) && \
9d6f229f 75 vlan_proto(skb) == htons(ETH_P_IP) && \
8b42ec39
SH
76 brnf_filter_vlan_tagged)
77
78#define IS_VLAN_IPV6(skb) \
79 (skb->protocol == htons(ETH_P_8021Q) && \
80 vlan_proto(skb) == htons(ETH_P_IPV6) &&\
81 brnf_filter_vlan_tagged)
82
83#define IS_VLAN_ARP(skb) \
84 (skb->protocol == htons(ETH_P_8021Q) && \
85 vlan_proto(skb) == htons(ETH_P_ARP) && \
86 brnf_filter_vlan_tagged)
1da177e4 87
516299d2
MM
88static inline __be16 pppoe_proto(const struct sk_buff *skb)
89{
90 return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
91 sizeof(struct pppoe_hdr)));
92}
93
94#define IS_PPPOE_IP(skb) \
95 (skb->protocol == htons(ETH_P_PPP_SES) && \
96 pppoe_proto(skb) == htons(PPP_IP) && \
97 brnf_filter_pppoe_tagged)
98
99#define IS_PPPOE_IPV6(skb) \
100 (skb->protocol == htons(ETH_P_PPP_SES) && \
101 pppoe_proto(skb) == htons(PPP_IPV6) && \
102 brnf_filter_pppoe_tagged)
103
1da177e4
LT
104/* We need these fake structures to make netfilter happy --
105 * lots of places assume that skb->dst != NULL, which isn't
106 * all that unreasonable.
107 *
108 * Currently, we fill in the PMTU entry because netfilter
109 * refragmentation needs it, and the rt_flags entry because
110 * ipt_REJECT needs it. Future netfilter modules might
111 * require us to fill additional fields. */
112static struct net_device __fake_net_device = {
159d8336
PM
113 .hard_header_len = ETH_HLEN,
114 .nd_net = &init_net,
1da177e4
LT
115};
116
117static struct rtable __fake_rtable = {
118 .u = {
119 .dst = {
120 .__refcnt = ATOMIC_INIT(1),
121 .dev = &__fake_net_device,
122 .path = &__fake_rtable.u.dst,
123 .metrics = {[RTAX_MTU - 1] = 1500},
42cf93cd 124 .flags = DST_NOXFRM,
1da177e4
LT
125 }
126 },
127 .rt_flags = 0,
128};
129
5dce971a
SH
130static inline struct net_device *bridge_parent(const struct net_device *dev)
131{
132 struct net_bridge_port *port = rcu_dereference(dev->br_port);
133
134 return port ? port->br->dev : NULL;
135}
1da177e4 136
fdeabdef
SH
137static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
138{
139 skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
140 if (likely(skb->nf_bridge))
141 atomic_set(&(skb->nf_bridge->use), 1);
142
143 return skb->nf_bridge;
144}
145
2dc2f207
PM
146static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
147{
148 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
149
150 if (atomic_read(&nf_bridge->use) > 1) {
151 struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
152
153 if (tmp) {
154 memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
155 atomic_set(&tmp->use, 1);
156 nf_bridge_put(nf_bridge);
157 }
158 nf_bridge = tmp;
159 }
160 return nf_bridge;
161}
162
fc38582d
PM
163static inline void nf_bridge_push_encap_header(struct sk_buff *skb)
164{
165 unsigned int len = nf_bridge_encap_header_len(skb);
166
167 skb_push(skb, len);
168 skb->network_header -= len;
169}
170
171static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
fdeabdef 172{
fc38582d
PM
173 unsigned int len = nf_bridge_encap_header_len(skb);
174
175 skb_pull(skb, len);
176 skb->network_header += len;
177}
fdeabdef 178
fc38582d
PM
179static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
180{
181 unsigned int len = nf_bridge_encap_header_len(skb);
182
183 skb_pull_rcsum(skb, len);
184 skb->network_header += len;
185}
186
187static inline void nf_bridge_save_header(struct sk_buff *skb)
188{
189 int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
fdeabdef 190
d626f62b
ACM
191 skb_copy_from_linear_data_offset(skb, -header_size,
192 skb->nf_bridge->data, header_size);
fdeabdef
SH
193}
194
07317621
SH
195/*
196 * When forwarding bridge frames, we save a copy of the original
197 * header before processing.
198 */
199int nf_bridge_copy_header(struct sk_buff *skb)
200{
201 int err;
fc38582d 202 int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
07317621 203
d9cc2048 204 err = skb_cow_head(skb, header_size);
07317621
SH
205 if (err)
206 return err;
207
27d7ff46
ACM
208 skb_copy_to_linear_data_offset(skb, -header_size,
209 skb->nf_bridge->data, header_size);
fc38582d 210 __skb_push(skb, nf_bridge_encap_header_len(skb));
07317621
SH
211 return 0;
212}
213
1da177e4
LT
214/* PF_BRIDGE/PRE_ROUTING *********************************************/
215/* Undo the changes made for ip6tables PREROUTING and continue the
216 * bridge PRE_ROUTING hook. */
217static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
218{
219 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
220
1da177e4
LT
221 if (nf_bridge->mask & BRNF_PKT_TYPE) {
222 skb->pkt_type = PACKET_OTHERHOST;
223 nf_bridge->mask ^= BRNF_PKT_TYPE;
224 }
225 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
226
227 skb->dst = (struct dst_entry *)&__fake_rtable;
228 dst_hold(skb->dst);
229
230 skb->dev = nf_bridge->physindev;
fc38582d 231 nf_bridge_push_encap_header(skb);
1da177e4
LT
232 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
233 br_handle_frame_finish, 1);
234
235 return 0;
236}
237
238static void __br_dnat_complain(void)
239{
240 static unsigned long last_complaint;
241
242 if (jiffies - last_complaint >= 5 * HZ) {
243 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
789bc3e5 244 "forwarding to be enabled\n");
1da177e4
LT
245 last_complaint = jiffies;
246 }
247}
248
249/* This requires some explaining. If DNAT has taken place,
250 * we will need to fix up the destination Ethernet address,
251 * and this is a tricky process.
252 *
253 * There are two cases to consider:
254 * 1. The packet was DNAT'ed to a device in the same bridge
255 * port group as it was received on. We can still bridge
256 * the packet.
257 * 2. The packet was DNAT'ed to a different device, either
258 * a non-bridged device or another bridge port group.
259 * The packet will need to be routed.
260 *
261 * The correct way of distinguishing between these two cases is to
262 * call ip_route_input() and to look at skb->dst->dev, which is
263 * changed to the destination device if ip_route_input() succeeds.
264 *
265 * Let us first consider the case that ip_route_input() succeeds:
266 *
267 * If skb->dst->dev equals the logical bridge device the packet
2948d2eb
PM
268 * came in on, we can consider this bridging. The packet is passed
269 * through the neighbour output function to build a new destination
270 * MAC address, which will make the packet enter br_nf_local_out()
1da177e4
LT
271 * not much later. In that function it is assured that the iptables
272 * FORWARD chain is traversed for the packet.
273 *
274 * Otherwise, the packet is considered to be routed and we just
275 * change the destination MAC address so that the packet will
f216f082
BDS
276 * later be passed up to the IP stack to be routed. For a redirected
277 * packet, ip_route_input() will give back the localhost as output device,
278 * which differs from the bridge device.
1da177e4
LT
279 *
280 * Let us now consider the case that ip_route_input() fails:
281 *
f216f082
BDS
282 * This can be because the destination address is martian, in which case
283 * the packet will be dropped.
1da177e4
LT
284 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
285 * will fail, while __ip_route_output_key() will return success. The source
286 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
287 * thinks we're handling a locally generated packet and won't care
288 * if IP forwarding is allowed. We send a warning message to the users's
289 * log telling her to put IP forwarding on.
290 *
291 * ip_route_input() will also fail if there is no route available.
292 * In that case we just drop the packet.
293 *
294 * --Lennert, 20020411
295 * --Bart, 20020416 (updated)
f216f082
BDS
296 * --Bart, 20021007 (updated)
297 * --Bart, 20062711 (updated) */
1da177e4
LT
298static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
299{
1da177e4
LT
300 if (skb->pkt_type == PACKET_OTHERHOST) {
301 skb->pkt_type = PACKET_HOST;
302 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
303 }
304 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
305
306 skb->dev = bridge_parent(skb->dev);
2948d2eb
PM
307 if (skb->dev) {
308 struct dst_entry *dst = skb->dst;
309
fc38582d 310 nf_bridge_pull_encap_header(skb);
2948d2eb
PM
311
312 if (dst->hh)
313 return neigh_hh_output(dst->hh, skb);
314 else if (dst->neighbour)
315 return dst->neighbour->output(skb);
1da177e4 316 }
2948d2eb 317 kfree_skb(skb);
1da177e4
LT
318 return 0;
319}
320
321static int br_nf_pre_routing_finish(struct sk_buff *skb)
322{
323 struct net_device *dev = skb->dev;
eddc9ec5 324 struct iphdr *iph = ip_hdr(skb);
1da177e4 325 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
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))) {
1da177e4 335 struct rtable *rt;
789bc3e5
SH
336 struct flowi fl = {
337 .nl_u = {
338 .ip4_u = {
339 .daddr = iph->daddr,
340 .saddr = 0,
341 .tos = RT_TOS(iph->tos) },
342 },
343 .proto = 0,
344 };
f216f082
BDS
345 struct in_device *in_dev = in_dev_get(dev);
346
347 /* If err equals -EHOSTUNREACH the error is due to a
348 * martian destination or due to the fact that
349 * forwarding is disabled. For most martian packets,
350 * ip_route_output_key() will fail. It won't fail for 2 types of
351 * martian destinations: loopback destinations and destination
352 * 0.0.0.0. In both cases the packet will be dropped because the
353 * destination is the loopback device and not the bridge. */
354 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
355 goto free_skb;
1da177e4 356
f206351a 357 if (!ip_route_output_key(&init_net, &rt, &fl)) {
1c011bed 358 /* - Bridged-and-DNAT'ed traffic doesn't
f216f082
BDS
359 * require ip_forwarding. */
360 if (((struct dst_entry *)rt)->dev == dev) {
1da177e4
LT
361 skb->dst = (struct dst_entry *)rt;
362 goto bridged_dnat;
363 }
f216f082
BDS
364 /* we are sure that forwarding is disabled, so printing
365 * this message is no problem. Note that the packet could
366 * still have a martian destination address, in which case
367 * the packet could be dropped even if forwarding were enabled */
1da177e4
LT
368 __br_dnat_complain();
369 dst_release((struct dst_entry *)rt);
370 }
f216f082 371free_skb:
1da177e4
LT
372 kfree_skb(skb);
373 return 0;
374 } else {
375 if (skb->dst->dev == dev) {
376bridged_dnat:
377 /* Tell br_nf_local_out this is a
378 * bridged frame */
379 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
380 skb->dev = nf_bridge->physindev;
fc38582d 381 nf_bridge_push_encap_header(skb);
1da177e4
LT
382 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
383 skb, skb->dev, NULL,
384 br_nf_pre_routing_finish_bridge,
385 1);
386 return 0;
387 }
789bc3e5 388 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
1da177e4
LT
389 skb->pkt_type = PACKET_HOST;
390 }
391 } else {
392 skb->dst = (struct dst_entry *)&__fake_rtable;
393 dst_hold(skb->dst);
394 }
395
396 skb->dev = nf_bridge->physindev;
fc38582d 397 nf_bridge_push_encap_header(skb);
1da177e4
LT
398 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
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
6e23ae2a 515 NF_HOOK(PF_INET6, 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
6e23ae2a 588 NF_HOOK(PF_INET, 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{
1da177e4
LT
612 if (skb->dst == (struct dst_entry *)&__fake_rtable) {
613 dst_release(skb->dst);
614 skb->dst = NULL;
615 }
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);
1da177e4 636 NF_HOOK_THRESH(PF_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;
1da177e4
LT
653 int pf;
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
LT
669 pf = PF_INET;
670 else
671 pf = PF_INET6;
672
3db05fea 673 nf_bridge_pull_encap_header(skb);
1da177e4 674
1da177e4
LT
675 nf_bridge = skb->nf_bridge;
676 if (skb->pkt_type == PACKET_OTHERHOST) {
677 skb->pkt_type = PACKET_HOST;
678 nf_bridge->mask |= BRNF_PKT_TYPE;
679 }
680
681 /* The physdev module checks on this */
682 nf_bridge->mask |= BRNF_BRIDGED;
683 nf_bridge->physoutdev = skb->dev;
684
6e23ae2a 685 NF_HOOK(pf, NF_INET_FORWARD, skb, bridge_parent(in), parent,
5dce971a 686 br_nf_forward_finish);
1da177e4
LT
687
688 return NF_STOLEN;
689}
690
3db05fea 691static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff *skb,
789bc3e5
SH
692 const struct net_device *in,
693 const struct net_device *out,
694 int (*okfn)(struct sk_buff *))
1da177e4 695{
1da177e4
LT
696 struct net_device **d = (struct net_device **)(skb->cb);
697
698#ifdef CONFIG_SYSCTL
699 if (!brnf_call_arptables)
700 return NF_ACCEPT;
701#endif
702
f8a26028 703 if (skb->protocol != htons(ETH_P_ARP)) {
8b42ec39 704 if (!IS_VLAN_ARP(skb))
1da177e4 705 return NF_ACCEPT;
3db05fea 706 nf_bridge_pull_encap_header(skb);
1da177e4
LT
707 }
708
d0a92be0 709 if (arp_hdr(skb)->ar_pln != 4) {
fc38582d 710 if (IS_VLAN_ARP(skb))
3db05fea 711 nf_bridge_push_encap_header(skb);
1da177e4
LT
712 return NF_ACCEPT;
713 }
714 *d = (struct net_device *)in;
715 NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
716 (struct net_device *)out, br_nf_forward_finish);
717
718 return NF_STOLEN;
719}
720
2bf540b7
PM
721/* PF_BRIDGE/LOCAL_OUT ***********************************************
722 *
723 * This function sees both locally originated IP packets and forwarded
1da177e4
LT
724 * IP packets (in both cases the destination device is a bridge
725 * device). It also sees bridged-and-DNAT'ed packets.
1da177e4
LT
726 *
727 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
728 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
729 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
730 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
731 * will be executed.
2bf540b7 732 */
3db05fea 733static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff *skb,
789bc3e5
SH
734 const struct net_device *in,
735 const struct net_device *out,
736 int (*okfn)(struct sk_buff *))
1da177e4 737{
2bf540b7 738 struct net_device *realindev;
1da177e4 739 struct nf_bridge_info *nf_bridge;
1da177e4
LT
740
741 if (!skb->nf_bridge)
742 return NF_ACCEPT;
743
2dc2f207
PM
744 /* Need exclusive nf_bridge_info since we might have multiple
745 * different physoutdevs. */
746 if (!nf_bridge_unshare(skb))
747 return NF_DROP;
748
1da177e4 749 nf_bridge = skb->nf_bridge;
2bf540b7
PM
750 if (!(nf_bridge->mask & BRNF_BRIDGED_DNAT))
751 return NF_ACCEPT;
1da177e4
LT
752
753 /* Bridged, take PF_BRIDGE/FORWARD.
754 * (see big note in front of br_nf_pre_routing_finish) */
2bf540b7
PM
755 nf_bridge->physoutdev = skb->dev;
756 realindev = nf_bridge->physindev;
1da177e4 757
2bf540b7
PM
758 if (nf_bridge->mask & BRNF_PKT_TYPE) {
759 skb->pkt_type = PACKET_OTHERHOST;
760 nf_bridge->mask ^= BRNF_PKT_TYPE;
1da177e4 761 }
fc38582d 762 nf_bridge_push_encap_header(skb);
1da177e4 763
2bf540b7
PM
764 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev, skb->dev,
765 br_forward_finish);
1da177e4
LT
766 return NF_STOLEN;
767}
768
2e2f7aef
PM
769static int br_nf_dev_queue_xmit(struct sk_buff *skb)
770{
771 if (skb->protocol == htons(ETH_P_IP) &&
772 skb->len > skb->dev->mtu &&
89114afd 773 !skb_is_gso(skb))
2e2f7aef
PM
774 return ip_fragment(skb, br_dev_queue_push_xmit);
775 else
776 return br_dev_queue_push_xmit(skb);
777}
1da177e4
LT
778
779/* PF_BRIDGE/POST_ROUTING ********************************************/
3db05fea 780static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
789bc3e5
SH
781 const struct net_device *in,
782 const struct net_device *out,
783 int (*okfn)(struct sk_buff *))
1da177e4 784{
3db05fea 785 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
1da177e4
LT
786 struct net_device *realoutdev = bridge_parent(skb->dev);
787 int pf;
788
789#ifdef CONFIG_NETFILTER_DEBUG
790 /* Be very paranoid. This probably won't happen anymore, but let's
791 * keep the check just to be sure... */
98e399f8
ACM
792 if (skb_mac_header(skb) < skb->head ||
793 skb_mac_header(skb) + ETH_HLEN > skb->data) {
1da177e4 794 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
8394e9b2 795 "bad mac.raw pointer.\n");
1da177e4
LT
796 goto print_error;
797 }
798#endif
799
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
LT
811 pf = PF_INET;
812 else
813 pf = PF_INET6;
814
815#ifdef CONFIG_NETFILTER_DEBUG
816 if (skb->dst == NULL) {
8394e9b2 817 printk(KERN_INFO "br_netfilter post_routing: skb->dst == NULL\n");
1da177e4
LT
818 goto print_error;
819 }
1da177e4
LT
820#endif
821
822 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
823 * about the value of skb->pkt_type. */
824 if (skb->pkt_type == PACKET_OTHERHOST) {
825 skb->pkt_type = PACKET_HOST;
826 nf_bridge->mask |= BRNF_PKT_TYPE;
827 }
828
fc38582d 829 nf_bridge_pull_encap_header(skb);
1da177e4
LT
830 nf_bridge_save_header(skb);
831
6e23ae2a 832 NF_HOOK(pf, NF_INET_POST_ROUTING, skb, NULL, realoutdev,
2e2f7aef 833 br_nf_dev_queue_xmit);
1da177e4
LT
834
835 return NF_STOLEN;
836
837#ifdef CONFIG_NETFILTER_DEBUG
838print_error:
839 if (skb->dev != NULL) {
840 printk("[%s]", skb->dev->name);
178a3259
SH
841 if (realoutdev)
842 printk("[%s]", realoutdev->name);
1da177e4 843 }
98e399f8 844 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb_mac_header(skb),
789bc3e5 845 skb->data);
8394e9b2 846 dump_stack();
1da177e4
LT
847 return NF_ACCEPT;
848#endif
849}
850
1da177e4
LT
851/* IP/SABOTAGE *****************************************************/
852/* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
853 * for the second time. */
3db05fea 854static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff *skb,
789bc3e5
SH
855 const struct net_device *in,
856 const struct net_device *out,
857 int (*okfn)(struct sk_buff *))
1da177e4 858{
3db05fea
HX
859 if (skb->nf_bridge &&
860 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
1da177e4
LT
861 return NF_STOP;
862 }
863
864 return NF_ACCEPT;
865}
866
1da177e4
LT
867/* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
868 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
869 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
870 * ip_refrag() can return NF_STOLEN. */
1999414a 871static struct nf_hook_ops br_nf_ops[] __read_mostly = {
9d6f229f
YH
872 { .hook = br_nf_pre_routing,
873 .owner = THIS_MODULE,
874 .pf = PF_BRIDGE,
875 .hooknum = NF_BR_PRE_ROUTING,
1da177e4
LT
876 .priority = NF_BR_PRI_BRNF, },
877 { .hook = br_nf_local_in,
878 .owner = THIS_MODULE,
879 .pf = PF_BRIDGE,
880 .hooknum = NF_BR_LOCAL_IN,
881 .priority = NF_BR_PRI_BRNF, },
882 { .hook = br_nf_forward_ip,
883 .owner = THIS_MODULE,
884 .pf = PF_BRIDGE,
885 .hooknum = NF_BR_FORWARD,
886 .priority = NF_BR_PRI_BRNF - 1, },
887 { .hook = br_nf_forward_arp,
888 .owner = THIS_MODULE,
889 .pf = PF_BRIDGE,
890 .hooknum = NF_BR_FORWARD,
891 .priority = NF_BR_PRI_BRNF, },
892 { .hook = br_nf_local_out,
893 .owner = THIS_MODULE,
894 .pf = PF_BRIDGE,
895 .hooknum = NF_BR_LOCAL_OUT,
896 .priority = NF_BR_PRI_FIRST, },
897 { .hook = br_nf_post_routing,
898 .owner = THIS_MODULE,
899 .pf = PF_BRIDGE,
900 .hooknum = NF_BR_POST_ROUTING,
901 .priority = NF_BR_PRI_LAST, },
902 { .hook = ip_sabotage_in,
903 .owner = THIS_MODULE,
904 .pf = PF_INET,
6e23ae2a 905 .hooknum = NF_INET_PRE_ROUTING,
1da177e4
LT
906 .priority = NF_IP_PRI_FIRST, },
907 { .hook = ip_sabotage_in,
908 .owner = THIS_MODULE,
909 .pf = PF_INET6,
6e23ae2a 910 .hooknum = NF_INET_PRE_ROUTING,
1da177e4 911 .priority = NF_IP6_PRI_FIRST, },
1da177e4
LT
912};
913
914#ifdef CONFIG_SYSCTL
915static
789bc3e5
SH
916int brnf_sysctl_call_tables(ctl_table * ctl, int write, struct file *filp,
917 void __user * buffer, size_t * lenp, loff_t * ppos)
1da177e4
LT
918{
919 int ret;
920
921 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
922
923 if (write && *(int *)(ctl->data))
924 *(int *)(ctl->data) = 1;
925 return ret;
926}
927
928static ctl_table brnf_table[] = {
929 {
1da177e4
LT
930 .procname = "bridge-nf-call-arptables",
931 .data = &brnf_call_arptables,
932 .maxlen = sizeof(int),
933 .mode = 0644,
934 .proc_handler = &brnf_sysctl_call_tables,
935 },
936 {
1da177e4
LT
937 .procname = "bridge-nf-call-iptables",
938 .data = &brnf_call_iptables,
939 .maxlen = sizeof(int),
940 .mode = 0644,
941 .proc_handler = &brnf_sysctl_call_tables,
942 },
943 {
1da177e4
LT
944 .procname = "bridge-nf-call-ip6tables",
945 .data = &brnf_call_ip6tables,
946 .maxlen = sizeof(int),
947 .mode = 0644,
948 .proc_handler = &brnf_sysctl_call_tables,
949 },
950 {
1da177e4
LT
951 .procname = "bridge-nf-filter-vlan-tagged",
952 .data = &brnf_filter_vlan_tagged,
953 .maxlen = sizeof(int),
954 .mode = 0644,
955 .proc_handler = &brnf_sysctl_call_tables,
516299d2
MM
956 },
957 {
516299d2
MM
958 .procname = "bridge-nf-filter-pppoe-tagged",
959 .data = &brnf_filter_pppoe_tagged,
960 .maxlen = sizeof(int),
961 .mode = 0644,
962 .proc_handler = &brnf_sysctl_call_tables,
1da177e4
LT
963 },
964 { .ctl_name = 0 }
965};
966
b5ccd792
PE
967static struct ctl_path brnf_path[] = {
968 { .procname = "net", .ctl_name = CTL_NET, },
969 { .procname = "bridge", .ctl_name = NET_BRIDGE, },
970 { }
1da177e4
LT
971};
972#endif
973
5eb87f45 974int __init br_netfilter_init(void)
1da177e4 975{
5eb87f45 976 int ret;
1da177e4 977
5eb87f45
PM
978 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
979 if (ret < 0)
1da177e4 980 return ret;
1da177e4 981#ifdef CONFIG_SYSCTL
b5ccd792 982 brnf_sysctl_header = register_sysctl_paths(brnf_path, brnf_table);
1da177e4 983 if (brnf_sysctl_header == NULL) {
789bc3e5
SH
984 printk(KERN_WARNING
985 "br_netfilter: can't register to sysctl.\n");
5eb87f45
PM
986 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
987 return -ENOMEM;
1da177e4
LT
988 }
989#endif
1da177e4 990 printk(KERN_NOTICE "Bridge firewalling registered\n");
1da177e4
LT
991 return 0;
992}
993
994void br_netfilter_fini(void)
995{
5eb87f45 996 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1da177e4
LT
997#ifdef CONFIG_SYSCTL
998 unregister_sysctl_table(brnf_sysctl_header);
999#endif
1000}
This page took 0.558509 seconds and 4 git commands to generate.