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