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