blob: b498efcfe451b0e74c38e94626777292c23688bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020029#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <net/ip.h>
39#include <net/ipv6.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020040#include <net/route.h>
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#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
Patrick McHardy10ea6ac2006-07-24 22:54:55 -070064int brnf_deferred_hooks;
65EXPORT_SYMBOL_GPL(brnf_deferred_hooks);
66
Stephen Hemminger8b42ec32006-03-20 22:58:05 -080067static __be16 inline vlan_proto(const struct sk_buff *skb)
68{
69 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
70}
71
72#define IS_VLAN_IP(skb) \
73 (skb->protocol == htons(ETH_P_8021Q) && \
74 vlan_proto(skb) == htons(ETH_P_IP) && \
75 brnf_filter_vlan_tagged)
76
77#define IS_VLAN_IPV6(skb) \
78 (skb->protocol == htons(ETH_P_8021Q) && \
79 vlan_proto(skb) == htons(ETH_P_IPV6) &&\
80 brnf_filter_vlan_tagged)
81
82#define IS_VLAN_ARP(skb) \
83 (skb->protocol == htons(ETH_P_8021Q) && \
84 vlan_proto(skb) == htons(ETH_P_ARP) && \
85 brnf_filter_vlan_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/* We need these fake structures to make netfilter happy --
88 * lots of places assume that skb->dst != NULL, which isn't
89 * all that unreasonable.
90 *
91 * Currently, we fill in the PMTU entry because netfilter
92 * refragmentation needs it, and the rt_flags entry because
93 * ipt_REJECT needs it. Future netfilter modules might
94 * require us to fill additional fields. */
95static struct net_device __fake_net_device = {
96 .hard_header_len = ETH_HLEN
97};
98
99static struct rtable __fake_rtable = {
100 .u = {
101 .dst = {
102 .__refcnt = ATOMIC_INIT(1),
103 .dev = &__fake_net_device,
104 .path = &__fake_rtable.u.dst,
105 .metrics = {[RTAX_MTU - 1] = 1500},
Patrick McHardy42cf93c2006-02-21 13:37:35 -0800106 .flags = DST_NOXFRM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108 },
109 .rt_flags = 0,
110};
111
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800112static inline struct net_device *bridge_parent(const struct net_device *dev)
113{
114 struct net_bridge_port *port = rcu_dereference(dev->br_port);
115
116 return port ? port->br->dev : NULL;
117}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800119static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
120{
121 skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
122 if (likely(skb->nf_bridge))
123 atomic_set(&(skb->nf_bridge->use), 1);
124
125 return skb->nf_bridge;
126}
127
128static inline void nf_bridge_save_header(struct sk_buff *skb)
129{
Stephen Hemminger073176212006-08-29 17:48:17 -0700130 int header_size = ETH_HLEN;
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800131
132 if (skb->protocol == htons(ETH_P_8021Q))
Stephen Hemminger073176212006-08-29 17:48:17 -0700133 header_size += VLAN_HLEN;
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800134
135 memcpy(skb->nf_bridge->data, skb->data - header_size, header_size);
136}
137
Stephen Hemminger073176212006-08-29 17:48:17 -0700138/*
139 * When forwarding bridge frames, we save a copy of the original
140 * header before processing.
141 */
142int nf_bridge_copy_header(struct sk_buff *skb)
143{
144 int err;
145 int header_size = ETH_HLEN;
146
147 if (skb->protocol == htons(ETH_P_8021Q))
148 header_size += VLAN_HLEN;
149
150 err = skb_cow(skb, header_size);
151 if (err)
152 return err;
153
154 memcpy(skb->data - header_size, skb->nf_bridge->data, header_size);
155
156 if (skb->protocol == htons(ETH_P_8021Q))
157 __skb_push(skb, VLAN_HLEN);
158 return 0;
159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/* PF_BRIDGE/PRE_ROUTING *********************************************/
162/* Undo the changes made for ip6tables PREROUTING and continue the
163 * bridge PRE_ROUTING hook. */
164static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
165{
166 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (nf_bridge->mask & BRNF_PKT_TYPE) {
169 skb->pkt_type = PACKET_OTHERHOST;
170 nf_bridge->mask ^= BRNF_PKT_TYPE;
171 }
172 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
173
174 skb->dst = (struct dst_entry *)&__fake_rtable;
175 dst_hold(skb->dst);
176
177 skb->dev = nf_bridge->physindev;
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800178 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 skb_push(skb, VLAN_HLEN);
180 skb->nh.raw -= VLAN_HLEN;
181 }
182 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
183 br_handle_frame_finish, 1);
184
185 return 0;
186}
187
188static void __br_dnat_complain(void)
189{
190 static unsigned long last_complaint;
191
192 if (jiffies - last_complaint >= 5 * HZ) {
193 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800194 "forwarding to be enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 last_complaint = jiffies;
196 }
197}
198
199/* This requires some explaining. If DNAT has taken place,
200 * we will need to fix up the destination Ethernet address,
201 * and this is a tricky process.
202 *
203 * There are two cases to consider:
204 * 1. The packet was DNAT'ed to a device in the same bridge
205 * port group as it was received on. We can still bridge
206 * the packet.
207 * 2. The packet was DNAT'ed to a different device, either
208 * a non-bridged device or another bridge port group.
209 * The packet will need to be routed.
210 *
211 * The correct way of distinguishing between these two cases is to
212 * call ip_route_input() and to look at skb->dst->dev, which is
213 * changed to the destination device if ip_route_input() succeeds.
214 *
215 * Let us first consider the case that ip_route_input() succeeds:
216 *
217 * If skb->dst->dev equals the logical bridge device the packet
218 * came in on, we can consider this bridging. We then call
219 * skb->dst->output() which will make the packet enter br_nf_local_out()
220 * not much later. In that function it is assured that the iptables
221 * FORWARD chain is traversed for the packet.
222 *
223 * Otherwise, the packet is considered to be routed and we just
224 * change the destination MAC address so that the packet will
225 * later be passed up to the IP stack to be routed.
226 *
227 * Let us now consider the case that ip_route_input() fails:
228 *
229 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
230 * will fail, while __ip_route_output_key() will return success. The source
231 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
232 * thinks we're handling a locally generated packet and won't care
233 * if IP forwarding is allowed. We send a warning message to the users's
234 * log telling her to put IP forwarding on.
235 *
236 * ip_route_input() will also fail if there is no route available.
237 * In that case we just drop the packet.
238 *
239 * --Lennert, 20020411
240 * --Bart, 20020416 (updated)
241 * --Bart, 20021007 (updated) */
242static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
243{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (skb->pkt_type == PACKET_OTHERHOST) {
245 skb->pkt_type = PACKET_HOST;
246 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
247 }
248 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
249
250 skb->dev = bridge_parent(skb->dev);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800251 if (!skb->dev)
252 kfree_skb(skb);
253 else {
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800254 if (skb->protocol == htons(ETH_P_8021Q)) {
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800255 skb_pull(skb, VLAN_HLEN);
256 skb->nh.raw += VLAN_HLEN;
257 }
258 skb->dst->output(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 0;
261}
262
263static int br_nf_pre_routing_finish(struct sk_buff *skb)
264{
265 struct net_device *dev = skb->dev;
266 struct iphdr *iph = skb->nh.iph;
267 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (nf_bridge->mask & BRNF_PKT_TYPE) {
270 skb->pkt_type = PACKET_OTHERHOST;
271 nf_bridge->mask ^= BRNF_PKT_TYPE;
272 }
273 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
274
275 if (dnat_took_place(skb)) {
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800276 if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 struct rtable *rt;
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800278 struct flowi fl = {
279 .nl_u = {
280 .ip4_u = {
281 .daddr = iph->daddr,
282 .saddr = 0,
283 .tos = RT_TOS(iph->tos) },
284 },
285 .proto = 0,
286 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (!ip_route_output_key(&rt, &fl)) {
Bart De Schuymer1c011be2005-09-14 20:55:16 -0700289 /* - Bridged-and-DNAT'ed traffic doesn't
290 * require ip_forwarding.
291 * - Deal with redirected traffic. */
292 if (((struct dst_entry *)rt)->dev == dev ||
293 rt->rt_type == RTN_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 skb->dst = (struct dst_entry *)rt;
295 goto bridged_dnat;
296 }
297 __br_dnat_complain();
298 dst_release((struct dst_entry *)rt);
299 }
300 kfree_skb(skb);
301 return 0;
302 } else {
303 if (skb->dst->dev == dev) {
304bridged_dnat:
305 /* Tell br_nf_local_out this is a
306 * bridged frame */
307 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
308 skb->dev = nf_bridge->physindev;
309 if (skb->protocol ==
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800310 htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 skb_push(skb, VLAN_HLEN);
312 skb->nh.raw -= VLAN_HLEN;
313 }
314 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
315 skb, skb->dev, NULL,
316 br_nf_pre_routing_finish_bridge,
317 1);
318 return 0;
319 }
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800320 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 skb->pkt_type = PACKET_HOST;
322 }
323 } else {
324 skb->dst = (struct dst_entry *)&__fake_rtable;
325 dst_hold(skb->dst);
326 }
327
328 skb->dev = nf_bridge->physindev;
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800329 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 skb_push(skb, VLAN_HLEN);
331 skb->nh.raw -= VLAN_HLEN;
332 }
333 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
334 br_handle_frame_finish, 1);
335
336 return 0;
337}
338
339/* Some common code for IPv4/IPv6 */
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800340static struct net_device *setup_pre_routing(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
343
344 if (skb->pkt_type == PACKET_OTHERHOST) {
345 skb->pkt_type = PACKET_HOST;
346 nf_bridge->mask |= BRNF_PKT_TYPE;
347 }
348
349 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
350 nf_bridge->physindev = skb->dev;
351 skb->dev = bridge_parent(skb->dev);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800352
353 return skb->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356/* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
357static int check_hbh_len(struct sk_buff *skb)
358{
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800359 unsigned char *raw = (u8 *) (skb->nh.ipv6h + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 u32 pkt_len;
361 int off = raw - skb->nh.raw;
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800362 int len = (raw[1] + 1) << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 if ((raw + len) - skb->data > skb_headlen(skb))
365 goto bad;
366
367 off += 2;
368 len -= 2;
369
370 while (len > 0) {
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800371 int optlen = skb->nh.raw[off + 1] + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 switch (skb->nh.raw[off]) {
374 case IPV6_TLV_PAD0:
375 optlen = 1;
376 break;
377
378 case IPV6_TLV_PADN:
379 break;
380
381 case IPV6_TLV_JUMBO:
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800382 if (skb->nh.raw[off + 1] != 4 || (off & 3) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 goto bad;
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800384 pkt_len = ntohl(*(u32 *) (skb->nh.raw + off + 2));
Bart De Schuymerb0366482005-12-19 14:00:08 -0800385 if (pkt_len <= IPV6_MAXPLEN ||
386 skb->nh.ipv6h->payload_len)
387 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
389 goto bad;
Bart De Schuymerb0366482005-12-19 14:00:08 -0800390 if (pskb_trim_rcsum(skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800391 pkt_len + sizeof(struct ipv6hdr)))
Bart De Schuymerb0366482005-12-19 14:00:08 -0800392 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 break;
394 default:
395 if (optlen > len)
396 goto bad;
397 break;
398 }
399 off += optlen;
400 len -= optlen;
401 }
402 if (len == 0)
403 return 0;
404bad:
405 return -1;
406
407}
408
409/* Replicate the checks that IPv6 does on packet reception and pass the packet
410 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
411static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800412 struct sk_buff *skb,
413 const struct net_device *in,
414 const struct net_device *out,
415 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct ipv6hdr *hdr;
418 u32 pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 if (skb->len < sizeof(struct ipv6hdr))
421 goto inhdr_error;
422
423 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
424 goto inhdr_error;
425
426 hdr = skb->nh.ipv6h;
427
428 if (hdr->version != 6)
429 goto inhdr_error;
430
431 pkt_len = ntohs(hdr->payload_len);
432
433 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
434 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
435 goto inhdr_error;
Herbert Xub38dfee2006-06-09 16:13:01 -0700436 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
437 goto inhdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800440 goto inhdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800442 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800443 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800445 if (!setup_pre_routing(skb))
446 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
449 br_nf_pre_routing_finish_ipv6);
450
451 return NF_STOLEN;
452
453inhdr_error:
454 return NF_DROP;
455}
456
457/* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
458 * Replicate the checks that IPv4 does on packet reception.
459 * Set skb->dev to the bridge device (i.e. parent of the
460 * receiving device) to make netfilter happy, the REDIRECT
461 * target in particular. Save the original destination IP
462 * address to be able to detect DNAT afterwards. */
463static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
Stephen Hemmingeree02b3a2006-01-06 13:13:29 -0800464 const struct net_device *in,
465 const struct net_device *out,
466 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct iphdr *iph;
469 __u32 len;
470 struct sk_buff *skb = *pskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800472 if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473#ifdef CONFIG_SYSCTL
474 if (!brnf_call_ip6tables)
475 return NF_ACCEPT;
476#endif
477 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
478 goto out;
479
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800480 if (skb->protocol == htons(ETH_P_8021Q)) {
Herbert Xucbb042f2006-03-20 22:43:56 -0800481 skb_pull_rcsum(skb, VLAN_HLEN);
Stephen Hemmingeree02b3a2006-01-06 13:13:29 -0800482 skb->nh.raw += VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
485 }
486#ifdef CONFIG_SYSCTL
487 if (!brnf_call_iptables)
488 return NF_ACCEPT;
489#endif
490
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800491 if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return NF_ACCEPT;
493
494 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
495 goto out;
496
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800497 if (skb->protocol == htons(ETH_P_8021Q)) {
Herbert Xucbb042f2006-03-20 22:43:56 -0800498 skb_pull_rcsum(skb, VLAN_HLEN);
Stephen Hemmingeree02b3a2006-01-06 13:13:29 -0800499 skb->nh.raw += VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501
502 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
503 goto inhdr_error;
504
505 iph = skb->nh.iph;
506 if (iph->ihl < 5 || iph->version != 4)
507 goto inhdr_error;
508
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800509 if (!pskb_may_pull(skb, 4 * iph->ihl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 goto inhdr_error;
511
512 iph = skb->nh.iph;
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800513 if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 goto inhdr_error;
515
516 len = ntohs(iph->tot_len);
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800517 if (skb->len < len || len < 4 * iph->ihl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 goto inhdr_error;
519
Herbert Xub38dfee2006-06-09 16:13:01 -0700520 pskb_trim_rcsum(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800522 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800523 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800525 if (!setup_pre_routing(skb))
526 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 store_orig_dstaddr(skb);
528
529 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
530 br_nf_pre_routing_finish);
531
532 return NF_STOLEN;
533
534inhdr_error:
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800535// IP_INC_STATS_BH(IpInHdrErrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536out:
537 return NF_DROP;
538}
539
540
541/* PF_BRIDGE/LOCAL_IN ************************************************/
542/* The packet is locally destined, which requires a real
543 * dst_entry, so detach the fake one. On the way up, the
544 * packet would pass through PRE_ROUTING again (which already
545 * took place when the packet entered the bridge), but we
546 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
547 * prevent this from happening. */
548static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800549 const struct net_device *in,
550 const struct net_device *out,
551 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct sk_buff *skb = *pskb;
554
555 if (skb->dst == (struct dst_entry *)&__fake_rtable) {
556 dst_release(skb->dst);
557 skb->dst = NULL;
558 }
559
560 return NF_ACCEPT;
561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/* PF_BRIDGE/FORWARD *************************************************/
564static int br_nf_forward_finish(struct sk_buff *skb)
565{
566 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
567 struct net_device *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800569 if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 in = nf_bridge->physindev;
571 if (nf_bridge->mask & BRNF_PKT_TYPE) {
572 skb->pkt_type = PACKET_OTHERHOST;
573 nf_bridge->mask ^= BRNF_PKT_TYPE;
574 }
575 } else {
576 in = *((struct net_device **)(skb->cb));
577 }
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800578 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 skb_push(skb, VLAN_HLEN);
580 skb->nh.raw -= VLAN_HLEN;
581 }
582 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800583 skb->dev, br_forward_finish, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return 0;
585}
586
587/* This is the 'purely bridged' case. For IP, we pass the packet to
588 * netfilter with indev and outdev set to the bridge device,
589 * but we are still able to filter on the 'real' indev/outdev
590 * because of the physdev module. For ARP, indev and outdev are the
591 * bridge ports. */
592static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800593 const struct net_device *in,
594 const struct net_device *out,
595 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 struct sk_buff *skb = *pskb;
598 struct nf_bridge_info *nf_bridge;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800599 struct net_device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 int pf;
601
602 if (!skb->nf_bridge)
603 return NF_ACCEPT;
604
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800605 parent = bridge_parent(out);
606 if (!parent)
607 return NF_DROP;
608
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800609 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 pf = PF_INET;
611 else
612 pf = PF_INET6;
613
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800614 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 skb_pull(*pskb, VLAN_HLEN);
616 (*pskb)->nh.raw += VLAN_HLEN;
617 }
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 nf_bridge = skb->nf_bridge;
620 if (skb->pkt_type == PACKET_OTHERHOST) {
621 skb->pkt_type = PACKET_HOST;
622 nf_bridge->mask |= BRNF_PKT_TYPE;
623 }
624
625 /* The physdev module checks on this */
626 nf_bridge->mask |= BRNF_BRIDGED;
627 nf_bridge->physoutdev = skb->dev;
628
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800629 NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in), parent,
630 br_nf_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 return NF_STOLEN;
633}
634
635static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800636 const struct net_device *in,
637 const struct net_device *out,
638 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct sk_buff *skb = *pskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct net_device **d = (struct net_device **)(skb->cb);
642
643#ifdef CONFIG_SYSCTL
644 if (!brnf_call_arptables)
645 return NF_ACCEPT;
646#endif
647
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800648 if (skb->protocol != htons(ETH_P_ARP)) {
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800649 if (!IS_VLAN_ARP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return NF_ACCEPT;
651 skb_pull(*pskb, VLAN_HLEN);
652 (*pskb)->nh.raw += VLAN_HLEN;
653 }
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (skb->nh.arph->ar_pln != 4) {
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800656 if (IS_VLAN_ARP(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 skb_push(*pskb, VLAN_HLEN);
658 (*pskb)->nh.raw -= VLAN_HLEN;
659 }
660 return NF_ACCEPT;
661 }
662 *d = (struct net_device *)in;
663 NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
664 (struct net_device *)out, br_nf_forward_finish);
665
666 return NF_STOLEN;
667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/* PF_BRIDGE/LOCAL_OUT ***********************************************/
670static int br_nf_local_out_finish(struct sk_buff *skb)
671{
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800672 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 skb_push(skb, VLAN_HLEN);
674 skb->nh.raw -= VLAN_HLEN;
675 }
676
677 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800678 br_forward_finish, NF_BR_PRI_FIRST + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 return 0;
681}
682
683/* This function sees both locally originated IP packets and forwarded
684 * IP packets (in both cases the destination device is a bridge
685 * device). It also sees bridged-and-DNAT'ed packets.
686 * To be able to filter on the physical bridge devices (with the physdev
687 * module), we steal packets destined to a bridge device away from the
688 * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
689 * when we have determined the real output device. This is done in here.
690 *
691 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
692 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
693 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
694 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
695 * will be executed.
696 * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
697 * this packet before, and so the packet was locally originated. We fake
698 * the PF_INET/LOCAL_OUT hook.
699 * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
700 * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
701 * even routed packets that didn't arrive on a bridge interface have their
702 * nf_bridge->physindev set. */
703static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800704 const struct net_device *in,
705 const struct net_device *out,
706 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 struct net_device *realindev, *realoutdev;
709 struct sk_buff *skb = *pskb;
710 struct nf_bridge_info *nf_bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 int pf;
712
713 if (!skb->nf_bridge)
714 return NF_ACCEPT;
715
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800716 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 pf = PF_INET;
718 else
719 pf = PF_INET6;
720
721#ifdef CONFIG_NETFILTER_DEBUG
722 /* Sometimes we get packets with NULL ->dst here (for example,
723 * running a dhcp client daemon triggers this). This should now
724 * be fixed, but let's keep the check around. */
725 if (skb->dst == NULL) {
726 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
727 return NF_ACCEPT;
728 }
729#endif
730
731 nf_bridge = skb->nf_bridge;
732 nf_bridge->physoutdev = skb->dev;
733 realindev = nf_bridge->physindev;
734
735 /* Bridged, take PF_BRIDGE/FORWARD.
736 * (see big note in front of br_nf_pre_routing_finish) */
737 if (nf_bridge->mask & BRNF_BRIDGED_DNAT) {
738 if (nf_bridge->mask & BRNF_PKT_TYPE) {
739 skb->pkt_type = PACKET_OTHERHOST;
740 nf_bridge->mask ^= BRNF_PKT_TYPE;
741 }
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800742 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 skb_push(skb, VLAN_HLEN);
744 skb->nh.raw -= VLAN_HLEN;
745 }
746
747 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev,
748 skb->dev, br_forward_finish);
749 goto out;
750 }
751 realoutdev = bridge_parent(skb->dev);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800752 if (!realoutdev)
753 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
756 /* iptables should match -o br0.x */
757 if (nf_bridge->netoutdev)
758 realoutdev = nf_bridge->netoutdev;
759#endif
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800760 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 skb_pull(skb, VLAN_HLEN);
762 (*pskb)->nh.raw += VLAN_HLEN;
763 }
764 /* IP forwarded traffic has a physindev, locally
765 * generated traffic hasn't. */
766 if (realindev != NULL) {
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800767 if (!(nf_bridge->mask & BRNF_DONT_TAKE_PARENT)) {
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800768 struct net_device *parent = bridge_parent(realindev);
769 if (parent)
770 realindev = parent;
771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 NF_HOOK_THRESH(pf, NF_IP_FORWARD, skb, realindev,
774 realoutdev, br_nf_local_out_finish,
775 NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD + 1);
776 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 NF_HOOK_THRESH(pf, NF_IP_LOCAL_OUT, skb, realindev,
778 realoutdev, br_nf_local_out_finish,
779 NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT + 1);
780 }
781
782out:
783 return NF_STOLEN;
784}
785
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700786static int br_nf_dev_queue_xmit(struct sk_buff *skb)
787{
788 if (skb->protocol == htons(ETH_P_IP) &&
789 skb->len > skb->dev->mtu &&
Herbert Xu89114af2006-07-08 13:34:32 -0700790 !skb_is_gso(skb))
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700791 return ip_fragment(skb, br_dev_queue_push_xmit);
792 else
793 return br_dev_queue_push_xmit(skb);
794}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796/* PF_BRIDGE/POST_ROUTING ********************************************/
797static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800798 const struct net_device *in,
799 const struct net_device *out,
800 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 struct sk_buff *skb = *pskb;
803 struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 struct net_device *realoutdev = bridge_parent(skb->dev);
805 int pf;
806
807#ifdef CONFIG_NETFILTER_DEBUG
808 /* Be very paranoid. This probably won't happen anymore, but let's
809 * keep the check just to be sure... */
810 if (skb->mac.raw < skb->head || skb->mac.raw + ETH_HLEN > skb->data) {
811 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800812 "bad mac.raw pointer.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 goto print_error;
814 }
815#endif
816
817 if (!nf_bridge)
818 return NF_ACCEPT;
819
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800820 if (!realoutdev)
821 return NF_DROP;
822
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800823 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 pf = PF_INET;
825 else
826 pf = PF_INET6;
827
828#ifdef CONFIG_NETFILTER_DEBUG
829 if (skb->dst == NULL) {
830 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
831 goto print_error;
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833#endif
834
835 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
836 * about the value of skb->pkt_type. */
837 if (skb->pkt_type == PACKET_OTHERHOST) {
838 skb->pkt_type = PACKET_HOST;
839 nf_bridge->mask |= BRNF_PKT_TYPE;
840 }
841
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800842 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 skb_pull(skb, VLAN_HLEN);
844 skb->nh.raw += VLAN_HLEN;
845 }
846
847 nf_bridge_save_header(skb);
848
849#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
850 if (nf_bridge->netoutdev)
851 realoutdev = nf_bridge->netoutdev;
852#endif
853 NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev,
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700854 br_nf_dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 return NF_STOLEN;
857
858#ifdef CONFIG_NETFILTER_DEBUG
859print_error:
860 if (skb->dev != NULL) {
861 printk("[%s]", skb->dev->name);
Stephen Hemminger178a3252006-02-13 15:43:58 -0800862 if (realoutdev)
863 printk("[%s]", realoutdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb->mac.raw,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800866 skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return NF_ACCEPT;
868#endif
869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/* IP/SABOTAGE *****************************************************/
872/* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
873 * for the second time. */
874static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800875 const struct net_device *in,
876 const struct net_device *out,
877 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 if ((*pskb)->nf_bridge &&
880 !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
881 return NF_STOP;
882 }
883
884 return NF_ACCEPT;
885}
886
887/* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
888 * and PF_INET(6)/POST_ROUTING until we have done the forwarding
889 * decision in the bridge code and have determined nf_bridge->physoutdev. */
890static unsigned int ip_sabotage_out(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800891 const struct net_device *in,
892 const struct net_device *out,
893 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 struct sk_buff *skb = *pskb;
896
897 if ((out->hard_start_xmit == br_dev_xmit &&
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800898 okfn != br_nf_forward_finish &&
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700899 okfn != br_nf_local_out_finish && okfn != br_nf_dev_queue_xmit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
901 || ((out->priv_flags & IFF_802_1Q_VLAN) &&
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800902 VLAN_DEV_INFO(out)->real_dev->hard_start_xmit == br_dev_xmit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903#endif
904 ) {
905 struct nf_bridge_info *nf_bridge;
906
907 if (!skb->nf_bridge) {
908#ifdef CONFIG_SYSCTL
909 /* This code is executed while in the IP(v6) stack,
910 the version should be 4 or 6. We can't use
911 skb->protocol because that isn't set on
912 PF_INET(6)/LOCAL_OUT. */
913 struct iphdr *ip = skb->nh.iph;
914
915 if (ip->version == 4 && !brnf_call_iptables)
916 return NF_ACCEPT;
917 else if (ip->version == 6 && !brnf_call_ip6tables)
918 return NF_ACCEPT;
Patrick McHardy10ea6ac2006-07-24 22:54:55 -0700919 else if (!brnf_deferred_hooks)
920 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921#endif
922 if (hook == NF_IP_POST_ROUTING)
923 return NF_ACCEPT;
924 if (!nf_bridge_alloc(skb))
925 return NF_DROP;
926 }
927
928 nf_bridge = skb->nf_bridge;
929
930 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
931 * will need the indev then. For a brouter, the real indev
932 * can be a bridge port, so we make sure br_nf_local_out()
933 * doesn't use the bridge parent of the indev by using
934 * the BRNF_DONT_TAKE_PARENT mask. */
935 if (hook == NF_IP_FORWARD && nf_bridge->physindev == NULL) {
Patrick McHardy9666dae2005-06-28 16:04:44 -0700936 nf_bridge->mask |= BRNF_DONT_TAKE_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 nf_bridge->physindev = (struct net_device *)in;
938 }
939#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
940 /* the iptables outdev is br0.x, not br0 */
941 if (out->priv_flags & IFF_802_1Q_VLAN)
942 nf_bridge->netoutdev = (struct net_device *)out;
943#endif
944 return NF_STOP;
945 }
946
947 return NF_ACCEPT;
948}
949
950/* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
951 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
952 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
953 * ip_refrag() can return NF_STOLEN. */
954static struct nf_hook_ops br_nf_ops[] = {
955 { .hook = br_nf_pre_routing,
956 .owner = THIS_MODULE,
957 .pf = PF_BRIDGE,
958 .hooknum = NF_BR_PRE_ROUTING,
959 .priority = NF_BR_PRI_BRNF, },
960 { .hook = br_nf_local_in,
961 .owner = THIS_MODULE,
962 .pf = PF_BRIDGE,
963 .hooknum = NF_BR_LOCAL_IN,
964 .priority = NF_BR_PRI_BRNF, },
965 { .hook = br_nf_forward_ip,
966 .owner = THIS_MODULE,
967 .pf = PF_BRIDGE,
968 .hooknum = NF_BR_FORWARD,
969 .priority = NF_BR_PRI_BRNF - 1, },
970 { .hook = br_nf_forward_arp,
971 .owner = THIS_MODULE,
972 .pf = PF_BRIDGE,
973 .hooknum = NF_BR_FORWARD,
974 .priority = NF_BR_PRI_BRNF, },
975 { .hook = br_nf_local_out,
976 .owner = THIS_MODULE,
977 .pf = PF_BRIDGE,
978 .hooknum = NF_BR_LOCAL_OUT,
979 .priority = NF_BR_PRI_FIRST, },
980 { .hook = br_nf_post_routing,
981 .owner = THIS_MODULE,
982 .pf = PF_BRIDGE,
983 .hooknum = NF_BR_POST_ROUTING,
984 .priority = NF_BR_PRI_LAST, },
985 { .hook = ip_sabotage_in,
986 .owner = THIS_MODULE,
987 .pf = PF_INET,
988 .hooknum = NF_IP_PRE_ROUTING,
989 .priority = NF_IP_PRI_FIRST, },
990 { .hook = ip_sabotage_in,
991 .owner = THIS_MODULE,
992 .pf = PF_INET6,
993 .hooknum = NF_IP6_PRE_ROUTING,
994 .priority = NF_IP6_PRI_FIRST, },
995 { .hook = ip_sabotage_out,
996 .owner = THIS_MODULE,
997 .pf = PF_INET,
998 .hooknum = NF_IP_FORWARD,
999 .priority = NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD, },
1000 { .hook = ip_sabotage_out,
1001 .owner = THIS_MODULE,
1002 .pf = PF_INET6,
1003 .hooknum = NF_IP6_FORWARD,
1004 .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD, },
1005 { .hook = ip_sabotage_out,
1006 .owner = THIS_MODULE,
1007 .pf = PF_INET,
1008 .hooknum = NF_IP_LOCAL_OUT,
1009 .priority = NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
1010 { .hook = ip_sabotage_out,
1011 .owner = THIS_MODULE,
1012 .pf = PF_INET6,
1013 .hooknum = NF_IP6_LOCAL_OUT,
1014 .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
1015 { .hook = ip_sabotage_out,
1016 .owner = THIS_MODULE,
1017 .pf = PF_INET,
1018 .hooknum = NF_IP_POST_ROUTING,
1019 .priority = NF_IP_PRI_FIRST, },
1020 { .hook = ip_sabotage_out,
1021 .owner = THIS_MODULE,
1022 .pf = PF_INET6,
1023 .hooknum = NF_IP6_POST_ROUTING,
1024 .priority = NF_IP6_PRI_FIRST, },
1025};
1026
1027#ifdef CONFIG_SYSCTL
1028static
Stephen Hemminger789bc3e2006-03-20 22:57:32 -08001029int brnf_sysctl_call_tables(ctl_table * ctl, int write, struct file *filp,
1030 void __user * buffer, size_t * lenp, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 int ret;
1033
1034 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
1035
1036 if (write && *(int *)(ctl->data))
1037 *(int *)(ctl->data) = 1;
1038 return ret;
1039}
1040
1041static ctl_table brnf_table[] = {
1042 {
1043 .ctl_name = NET_BRIDGE_NF_CALL_ARPTABLES,
1044 .procname = "bridge-nf-call-arptables",
1045 .data = &brnf_call_arptables,
1046 .maxlen = sizeof(int),
1047 .mode = 0644,
1048 .proc_handler = &brnf_sysctl_call_tables,
1049 },
1050 {
1051 .ctl_name = NET_BRIDGE_NF_CALL_IPTABLES,
1052 .procname = "bridge-nf-call-iptables",
1053 .data = &brnf_call_iptables,
1054 .maxlen = sizeof(int),
1055 .mode = 0644,
1056 .proc_handler = &brnf_sysctl_call_tables,
1057 },
1058 {
1059 .ctl_name = NET_BRIDGE_NF_CALL_IP6TABLES,
1060 .procname = "bridge-nf-call-ip6tables",
1061 .data = &brnf_call_ip6tables,
1062 .maxlen = sizeof(int),
1063 .mode = 0644,
1064 .proc_handler = &brnf_sysctl_call_tables,
1065 },
1066 {
1067 .ctl_name = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
1068 .procname = "bridge-nf-filter-vlan-tagged",
1069 .data = &brnf_filter_vlan_tagged,
1070 .maxlen = sizeof(int),
1071 .mode = 0644,
1072 .proc_handler = &brnf_sysctl_call_tables,
1073 },
1074 { .ctl_name = 0 }
1075};
1076
1077static ctl_table brnf_bridge_table[] = {
1078 {
1079 .ctl_name = NET_BRIDGE,
1080 .procname = "bridge",
1081 .mode = 0555,
1082 .child = brnf_table,
1083 },
1084 { .ctl_name = 0 }
1085};
1086
1087static ctl_table brnf_net_table[] = {
1088 {
1089 .ctl_name = CTL_NET,
1090 .procname = "net",
1091 .mode = 0555,
1092 .child = brnf_bridge_table,
1093 },
1094 { .ctl_name = 0 }
1095};
1096#endif
1097
1098int br_netfilter_init(void)
1099{
1100 int i;
1101
1102 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) {
1103 int ret;
1104
1105 if ((ret = nf_register_hook(&br_nf_ops[i])) >= 0)
1106 continue;
1107
1108 while (i--)
1109 nf_unregister_hook(&br_nf_ops[i]);
1110
1111 return ret;
1112 }
1113
1114#ifdef CONFIG_SYSCTL
1115 brnf_sysctl_header = register_sysctl_table(brnf_net_table, 0);
1116 if (brnf_sysctl_header == NULL) {
Stephen Hemminger789bc3e2006-03-20 22:57:32 -08001117 printk(KERN_WARNING
1118 "br_netfilter: can't register to sysctl.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++)
1120 nf_unregister_hook(&br_nf_ops[i]);
1121 return -EFAULT;
1122 }
1123#endif
1124
1125 printk(KERN_NOTICE "Bridge firewalling registered\n");
1126
1127 return 0;
1128}
1129
1130void br_netfilter_fini(void)
1131{
1132 int i;
1133
1134 for (i = ARRAY_SIZE(br_nf_ops) - 1; i >= 0; i--)
1135 nf_unregister_hook(&br_nf_ops[i]);
1136#ifdef CONFIG_SYSCTL
1137 unregister_sysctl_table(brnf_sysctl_header);
1138#endif
1139}