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