blob: 8a56d89630252a255db6e6919486c73788196eca [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>
Bart De Schuymerf216f082006-12-05 13:45:21 -080037#include <linux/inetdevice.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <net/ip.h>
40#include <net/ipv6.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020041#include <net/route.h>
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#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;
Brian Haley9c1ea142006-09-18 00:03:41 -070056static 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#else
61#define brnf_filter_vlan_tagged 1
62#endif
63
Dave Jonesb6f99a22007-03-22 12:27:49 -070064static inline __be16 vlan_proto(const struct sk_buff *skb)
Stephen Hemminger8b42ec32006-03-20 22:58:05 -080065{
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) && \
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090071 vlan_proto(skb) == htons(ETH_P_IP) && \
Stephen Hemminger8b42ec32006-03-20 22:58:05 -080072 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
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},
Patrick McHardy42cf93c2006-02-21 13:37:35 -0800103 .flags = DST_NOXFRM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105 },
106 .rt_flags = 0,
107};
108
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800109static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800116static 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{
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900127 int header_size = ETH_HLEN;
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800128
129 if (skb->protocol == htons(ETH_P_8021Q))
Stephen Hemminger073176212006-08-29 17:48:17 -0700130 header_size += VLAN_HLEN;
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800131
132 memcpy(skb->nf_bridge->data, skb->data - header_size, header_size);
133}
134
Stephen Hemminger073176212006-08-29 17:48:17 -0700135/*
136 * When forwarding bridge frames, we save a copy of the original
137 * header before processing.
138 */
139int nf_bridge_copy_header(struct sk_buff *skb)
140{
141 int err;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900142 int header_size = ETH_HLEN;
Stephen Hemminger073176212006-08-29 17:48:17 -0700143
144 if (skb->protocol == htons(ETH_P_8021Q))
145 header_size += VLAN_HLEN;
146
147 err = skb_cow(skb, header_size);
148 if (err)
149 return err;
150
151 memcpy(skb->data - header_size, skb->nf_bridge->data, header_size);
152
153 if (skb->protocol == htons(ETH_P_8021Q))
154 __skb_push(skb, VLAN_HLEN);
155 return 0;
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* PF_BRIDGE/PRE_ROUTING *********************************************/
159/* Undo the changes made for ip6tables PREROUTING and continue the
160 * bridge PRE_ROUTING hook. */
161static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
162{
163 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (nf_bridge->mask & BRNF_PKT_TYPE) {
166 skb->pkt_type = PACKET_OTHERHOST;
167 nf_bridge->mask ^= BRNF_PKT_TYPE;
168 }
169 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
170
171 skb->dst = (struct dst_entry *)&__fake_rtable;
172 dst_hold(skb->dst);
173
174 skb->dev = nf_bridge->physindev;
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800175 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 skb_push(skb, VLAN_HLEN);
177 skb->nh.raw -= VLAN_HLEN;
178 }
179 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
180 br_handle_frame_finish, 1);
181
182 return 0;
183}
184
185static void __br_dnat_complain(void)
186{
187 static unsigned long last_complaint;
188
189 if (jiffies - last_complaint >= 5 * HZ) {
190 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800191 "forwarding to be enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 last_complaint = jiffies;
193 }
194}
195
196/* This requires some explaining. If DNAT has taken place,
197 * we will need to fix up the destination Ethernet address,
198 * and this is a tricky process.
199 *
200 * There are two cases to consider:
201 * 1. The packet was DNAT'ed to a device in the same bridge
202 * port group as it was received on. We can still bridge
203 * the packet.
204 * 2. The packet was DNAT'ed to a different device, either
205 * a non-bridged device or another bridge port group.
206 * The packet will need to be routed.
207 *
208 * The correct way of distinguishing between these two cases is to
209 * call ip_route_input() and to look at skb->dst->dev, which is
210 * changed to the destination device if ip_route_input() succeeds.
211 *
212 * Let us first consider the case that ip_route_input() succeeds:
213 *
214 * If skb->dst->dev equals the logical bridge device the packet
215 * came in on, we can consider this bridging. We then call
216 * skb->dst->output() which will make the packet enter br_nf_local_out()
217 * not much later. In that function it is assured that the iptables
218 * FORWARD chain is traversed for the packet.
219 *
220 * Otherwise, the packet is considered to be routed and we just
221 * change the destination MAC address so that the packet will
Bart De Schuymerf216f082006-12-05 13:45:21 -0800222 * later be passed up to the IP stack to be routed. For a redirected
223 * packet, ip_route_input() will give back the localhost as output device,
224 * which differs from the bridge device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 *
226 * Let us now consider the case that ip_route_input() fails:
227 *
Bart De Schuymerf216f082006-12-05 13:45:21 -0800228 * This can be because the destination address is martian, in which case
229 * the packet will be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
231 * will fail, while __ip_route_output_key() will return success. The source
232 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
233 * thinks we're handling a locally generated packet and won't care
234 * if IP forwarding is allowed. We send a warning message to the users's
235 * log telling her to put IP forwarding on.
236 *
237 * ip_route_input() will also fail if there is no route available.
238 * In that case we just drop the packet.
239 *
240 * --Lennert, 20020411
241 * --Bart, 20020416 (updated)
Bart De Schuymerf216f082006-12-05 13:45:21 -0800242 * --Bart, 20021007 (updated)
243 * --Bart, 20062711 (updated) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
245{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (skb->pkt_type == PACKET_OTHERHOST) {
247 skb->pkt_type = PACKET_HOST;
248 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
249 }
250 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
251
252 skb->dev = bridge_parent(skb->dev);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800253 if (!skb->dev)
254 kfree_skb(skb);
255 else {
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800256 if (skb->protocol == htons(ETH_P_8021Q)) {
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800257 skb_pull(skb, VLAN_HLEN);
258 skb->nh.raw += VLAN_HLEN;
259 }
260 skb->dst->output(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return 0;
263}
264
265static int br_nf_pre_routing_finish(struct sk_buff *skb)
266{
267 struct net_device *dev = skb->dev;
268 struct iphdr *iph = skb->nh.iph;
269 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
Bart De Schuymerf216f082006-12-05 13:45:21 -0800270 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (nf_bridge->mask & BRNF_PKT_TYPE) {
273 skb->pkt_type = PACKET_OTHERHOST;
274 nf_bridge->mask ^= BRNF_PKT_TYPE;
275 }
276 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (dnat_took_place(skb)) {
Bart De Schuymerf216f082006-12-05 13:45:21 -0800278 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 struct rtable *rt;
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800280 struct flowi fl = {
281 .nl_u = {
282 .ip4_u = {
283 .daddr = iph->daddr,
284 .saddr = 0,
285 .tos = RT_TOS(iph->tos) },
286 },
287 .proto = 0,
288 };
Bart De Schuymerf216f082006-12-05 13:45:21 -0800289 struct in_device *in_dev = in_dev_get(dev);
290
291 /* If err equals -EHOSTUNREACH the error is due to a
292 * martian destination or due to the fact that
293 * forwarding is disabled. For most martian packets,
294 * ip_route_output_key() will fail. It won't fail for 2 types of
295 * martian destinations: loopback destinations and destination
296 * 0.0.0.0. In both cases the packet will be dropped because the
297 * destination is the loopback device and not the bridge. */
298 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
299 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (!ip_route_output_key(&rt, &fl)) {
Bart De Schuymer1c011be2005-09-14 20:55:16 -0700302 /* - Bridged-and-DNAT'ed traffic doesn't
Bart De Schuymerf216f082006-12-05 13:45:21 -0800303 * require ip_forwarding. */
304 if (((struct dst_entry *)rt)->dev == dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 skb->dst = (struct dst_entry *)rt;
306 goto bridged_dnat;
307 }
Bart De Schuymerf216f082006-12-05 13:45:21 -0800308 /* we are sure that forwarding is disabled, so printing
309 * this message is no problem. Note that the packet could
310 * still have a martian destination address, in which case
311 * the packet could be dropped even if forwarding were enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 __br_dnat_complain();
313 dst_release((struct dst_entry *)rt);
314 }
Bart De Schuymerf216f082006-12-05 13:45:21 -0800315free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 kfree_skb(skb);
317 return 0;
318 } else {
319 if (skb->dst->dev == dev) {
320bridged_dnat:
321 /* Tell br_nf_local_out this is a
322 * bridged frame */
323 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
324 skb->dev = nf_bridge->physindev;
325 if (skb->protocol ==
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800326 htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 skb_push(skb, VLAN_HLEN);
328 skb->nh.raw -= VLAN_HLEN;
329 }
330 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
331 skb, skb->dev, NULL,
332 br_nf_pre_routing_finish_bridge,
333 1);
334 return 0;
335 }
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800336 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 skb->pkt_type = PACKET_HOST;
338 }
339 } else {
340 skb->dst = (struct dst_entry *)&__fake_rtable;
341 dst_hold(skb->dst);
342 }
343
344 skb->dev = nf_bridge->physindev;
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800345 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 skb_push(skb, VLAN_HLEN);
347 skb->nh.raw -= VLAN_HLEN;
348 }
349 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
350 br_handle_frame_finish, 1);
351
352 return 0;
353}
354
355/* Some common code for IPv4/IPv6 */
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800356static struct net_device *setup_pre_routing(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
359
360 if (skb->pkt_type == PACKET_OTHERHOST) {
361 skb->pkt_type = PACKET_HOST;
362 nf_bridge->mask |= BRNF_PKT_TYPE;
363 }
364
365 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
366 nf_bridge->physindev = skb->dev;
367 skb->dev = bridge_parent(skb->dev);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800368
369 return skb->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372/* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
373static int check_hbh_len(struct sk_buff *skb)
374{
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800375 unsigned char *raw = (u8 *) (skb->nh.ipv6h + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 u32 pkt_len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700377 const unsigned char *nh = skb_network_header(skb);
378 int off = raw - nh;
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800379 int len = (raw[1] + 1) << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 if ((raw + len) - skb->data > skb_headlen(skb))
382 goto bad;
383
384 off += 2;
385 len -= 2;
386
387 while (len > 0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700388 int optlen = nh[off + 1] + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700390 switch (nh[off]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 case IPV6_TLV_PAD0:
392 optlen = 1;
393 break;
394
395 case IPV6_TLV_PADN:
396 break;
397
398 case IPV6_TLV_JUMBO:
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700399 if (nh[off + 1] != 4 || (off & 3) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 goto bad;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700401 pkt_len = ntohl(*(__be32 *) (nh + off + 2));
Bart De Schuymerb0366482005-12-19 14:00:08 -0800402 if (pkt_len <= IPV6_MAXPLEN ||
403 skb->nh.ipv6h->payload_len)
404 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
406 goto bad;
Bart De Schuymerb0366482005-12-19 14:00:08 -0800407 if (pskb_trim_rcsum(skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800408 pkt_len + sizeof(struct ipv6hdr)))
Bart De Schuymerb0366482005-12-19 14:00:08 -0800409 goto bad;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700410 nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 break;
412 default:
413 if (optlen > len)
414 goto bad;
415 break;
416 }
417 off += optlen;
418 len -= optlen;
419 }
420 if (len == 0)
421 return 0;
422bad:
423 return -1;
424
425}
426
427/* Replicate the checks that IPv6 does on packet reception and pass the packet
428 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
429static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800430 struct sk_buff *skb,
431 const struct net_device *in,
432 const struct net_device *out,
433 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 struct ipv6hdr *hdr;
436 u32 pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 if (skb->len < sizeof(struct ipv6hdr))
439 goto inhdr_error;
440
441 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
442 goto inhdr_error;
443
444 hdr = skb->nh.ipv6h;
445
446 if (hdr->version != 6)
447 goto inhdr_error;
448
449 pkt_len = ntohs(hdr->payload_len);
450
451 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
452 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
453 goto inhdr_error;
Herbert Xub38dfee2006-06-09 16:13:01 -0700454 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
455 goto inhdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800458 goto inhdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800460 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800461 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800463 if (!setup_pre_routing(skb))
464 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
467 br_nf_pre_routing_finish_ipv6);
468
469 return NF_STOLEN;
470
471inhdr_error:
472 return NF_DROP;
473}
474
475/* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
476 * Replicate the checks that IPv4 does on packet reception.
477 * Set skb->dev to the bridge device (i.e. parent of the
478 * receiving device) to make netfilter happy, the REDIRECT
479 * target in particular. Save the original destination IP
480 * address to be able to detect DNAT afterwards. */
481static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
Stephen Hemmingeree02b3a2006-01-06 13:13:29 -0800482 const struct net_device *in,
483 const struct net_device *out,
484 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct iphdr *iph;
487 __u32 len;
488 struct sk_buff *skb = *pskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800490 if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491#ifdef CONFIG_SYSCTL
492 if (!brnf_call_ip6tables)
493 return NF_ACCEPT;
494#endif
495 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
496 goto out;
497
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800498 if (skb->protocol == htons(ETH_P_8021Q)) {
Herbert Xucbb042f2006-03-20 22:43:56 -0800499 skb_pull_rcsum(skb, VLAN_HLEN);
Stephen Hemmingeree02b3a2006-01-06 13:13:29 -0800500 skb->nh.raw += VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
503 }
504#ifdef CONFIG_SYSCTL
505 if (!brnf_call_iptables)
506 return NF_ACCEPT;
507#endif
508
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800509 if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return NF_ACCEPT;
511
512 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
513 goto out;
514
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800515 if (skb->protocol == htons(ETH_P_8021Q)) {
Herbert Xucbb042f2006-03-20 22:43:56 -0800516 skb_pull_rcsum(skb, VLAN_HLEN);
Stephen Hemmingeree02b3a2006-01-06 13:13:29 -0800517 skb->nh.raw += VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
520 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
521 goto inhdr_error;
522
523 iph = skb->nh.iph;
524 if (iph->ihl < 5 || iph->version != 4)
525 goto inhdr_error;
526
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800527 if (!pskb_may_pull(skb, 4 * iph->ihl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 goto inhdr_error;
529
530 iph = skb->nh.iph;
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800531 if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto inhdr_error;
533
534 len = ntohs(iph->tot_len);
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800535 if (skb->len < len || len < 4 * iph->ihl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 goto inhdr_error;
537
Herbert Xub38dfee2006-06-09 16:13:01 -0700538 pskb_trim_rcsum(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800540 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800541 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800543 if (!setup_pre_routing(skb))
544 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 store_orig_dstaddr(skb);
546
547 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
548 br_nf_pre_routing_finish);
549
550 return NF_STOLEN;
551
552inhdr_error:
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800553// IP_INC_STATS_BH(IpInHdrErrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554out:
555 return NF_DROP;
556}
557
558
559/* PF_BRIDGE/LOCAL_IN ************************************************/
560/* The packet is locally destined, which requires a real
561 * dst_entry, so detach the fake one. On the way up, the
562 * packet would pass through PRE_ROUTING again (which already
563 * took place when the packet entered the bridge), but we
564 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
565 * prevent this from happening. */
566static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800567 const struct net_device *in,
568 const struct net_device *out,
569 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 struct sk_buff *skb = *pskb;
572
573 if (skb->dst == (struct dst_entry *)&__fake_rtable) {
574 dst_release(skb->dst);
575 skb->dst = NULL;
576 }
577
578 return NF_ACCEPT;
579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581/* PF_BRIDGE/FORWARD *************************************************/
582static int br_nf_forward_finish(struct sk_buff *skb)
583{
584 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
585 struct net_device *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800587 if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 in = nf_bridge->physindev;
589 if (nf_bridge->mask & BRNF_PKT_TYPE) {
590 skb->pkt_type = PACKET_OTHERHOST;
591 nf_bridge->mask ^= BRNF_PKT_TYPE;
592 }
593 } else {
594 in = *((struct net_device **)(skb->cb));
595 }
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800596 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 skb_push(skb, VLAN_HLEN);
598 skb->nh.raw -= VLAN_HLEN;
599 }
600 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800601 skb->dev, br_forward_finish, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return 0;
603}
604
605/* This is the 'purely bridged' case. For IP, we pass the packet to
606 * netfilter with indev and outdev set to the bridge device,
607 * but we are still able to filter on the 'real' indev/outdev
608 * because of the physdev module. For ARP, indev and outdev are the
609 * bridge ports. */
610static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800611 const struct net_device *in,
612 const struct net_device *out,
613 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 struct sk_buff *skb = *pskb;
616 struct nf_bridge_info *nf_bridge;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800617 struct net_device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int pf;
619
620 if (!skb->nf_bridge)
621 return NF_ACCEPT;
622
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800623 parent = bridge_parent(out);
624 if (!parent)
625 return NF_DROP;
626
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800627 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 pf = PF_INET;
629 else
630 pf = PF_INET6;
631
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800632 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 skb_pull(*pskb, VLAN_HLEN);
634 (*pskb)->nh.raw += VLAN_HLEN;
635 }
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 nf_bridge = skb->nf_bridge;
638 if (skb->pkt_type == PACKET_OTHERHOST) {
639 skb->pkt_type = PACKET_HOST;
640 nf_bridge->mask |= BRNF_PKT_TYPE;
641 }
642
643 /* The physdev module checks on this */
644 nf_bridge->mask |= BRNF_BRIDGED;
645 nf_bridge->physoutdev = skb->dev;
646
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800647 NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in), parent,
648 br_nf_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 return NF_STOLEN;
651}
652
653static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800654 const struct net_device *in,
655 const struct net_device *out,
656 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 struct sk_buff *skb = *pskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 struct net_device **d = (struct net_device **)(skb->cb);
660
661#ifdef CONFIG_SYSCTL
662 if (!brnf_call_arptables)
663 return NF_ACCEPT;
664#endif
665
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800666 if (skb->protocol != htons(ETH_P_ARP)) {
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800667 if (!IS_VLAN_ARP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return NF_ACCEPT;
669 skb_pull(*pskb, VLAN_HLEN);
670 (*pskb)->nh.raw += VLAN_HLEN;
671 }
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (skb->nh.arph->ar_pln != 4) {
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800674 if (IS_VLAN_ARP(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 skb_push(*pskb, VLAN_HLEN);
676 (*pskb)->nh.raw -= VLAN_HLEN;
677 }
678 return NF_ACCEPT;
679 }
680 *d = (struct net_device *)in;
681 NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
682 (struct net_device *)out, br_nf_forward_finish);
683
684 return NF_STOLEN;
685}
686
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800687/* PF_BRIDGE/LOCAL_OUT ***********************************************
688 *
689 * This function sees both locally originated IP packets and forwarded
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 * IP packets (in both cases the destination device is a bridge
691 * device). It also sees bridged-and-DNAT'ed packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 *
693 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
694 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
695 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
696 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
697 * will be executed.
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800698 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800700 const struct net_device *in,
701 const struct net_device *out,
702 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800704 struct net_device *realindev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct sk_buff *skb = *pskb;
706 struct nf_bridge_info *nf_bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (!skb->nf_bridge)
709 return NF_ACCEPT;
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 nf_bridge = skb->nf_bridge;
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800712 if (!(nf_bridge->mask & BRNF_BRIDGED_DNAT))
713 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 /* Bridged, take PF_BRIDGE/FORWARD.
716 * (see big note in front of br_nf_pre_routing_finish) */
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800717 nf_bridge->physoutdev = skb->dev;
718 realindev = nf_bridge->physindev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800720 if (nf_bridge->mask & BRNF_PKT_TYPE) {
721 skb->pkt_type = PACKET_OTHERHOST;
722 nf_bridge->mask ^= BRNF_PKT_TYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800724 if (skb->protocol == htons(ETH_P_8021Q)) {
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800725 skb_push(skb, VLAN_HLEN);
726 skb->nh.raw -= VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800729 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev, skb->dev,
730 br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return NF_STOLEN;
732}
733
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700734static int br_nf_dev_queue_xmit(struct sk_buff *skb)
735{
736 if (skb->protocol == htons(ETH_P_IP) &&
737 skb->len > skb->dev->mtu &&
Herbert Xu89114af2006-07-08 13:34:32 -0700738 !skb_is_gso(skb))
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700739 return ip_fragment(skb, br_dev_queue_push_xmit);
740 else
741 return br_dev_queue_push_xmit(skb);
742}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744/* PF_BRIDGE/POST_ROUTING ********************************************/
745static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800746 const struct net_device *in,
747 const struct net_device *out,
748 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 struct sk_buff *skb = *pskb;
751 struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 struct net_device *realoutdev = bridge_parent(skb->dev);
753 int pf;
754
755#ifdef CONFIG_NETFILTER_DEBUG
756 /* Be very paranoid. This probably won't happen anymore, but let's
757 * keep the check just to be sure... */
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700758 if (skb_mac_header(skb) < skb->head ||
759 skb_mac_header(skb) + ETH_HLEN > skb->data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
Stephen Hemminger8394e9b2006-08-29 17:49:31 -0700761 "bad mac.raw pointer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 goto print_error;
763 }
764#endif
765
766 if (!nf_bridge)
767 return NF_ACCEPT;
768
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800769 if (!realoutdev)
770 return NF_DROP;
771
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800772 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 pf = PF_INET;
774 else
775 pf = PF_INET6;
776
777#ifdef CONFIG_NETFILTER_DEBUG
778 if (skb->dst == NULL) {
Stephen Hemminger8394e9b2006-08-29 17:49:31 -0700779 printk(KERN_INFO "br_netfilter post_routing: skb->dst == NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 goto print_error;
781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782#endif
783
784 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
785 * about the value of skb->pkt_type. */
786 if (skb->pkt_type == PACKET_OTHERHOST) {
787 skb->pkt_type = PACKET_HOST;
788 nf_bridge->mask |= BRNF_PKT_TYPE;
789 }
790
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800791 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 skb_pull(skb, VLAN_HLEN);
793 skb->nh.raw += VLAN_HLEN;
794 }
795
796 nf_bridge_save_header(skb);
797
798#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
799 if (nf_bridge->netoutdev)
800 realoutdev = nf_bridge->netoutdev;
801#endif
802 NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev,
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700803 br_nf_dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 return NF_STOLEN;
806
807#ifdef CONFIG_NETFILTER_DEBUG
808print_error:
809 if (skb->dev != NULL) {
810 printk("[%s]", skb->dev->name);
Stephen Hemminger178a3252006-02-13 15:43:58 -0800811 if (realoutdev)
812 printk("[%s]", realoutdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700814 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb_mac_header(skb),
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800815 skb->data);
Stephen Hemminger8394e9b2006-08-29 17:49:31 -0700816 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return NF_ACCEPT;
818#endif
819}
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821/* IP/SABOTAGE *****************************************************/
822/* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
823 * for the second time. */
824static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800825 const struct net_device *in,
826 const struct net_device *out,
827 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 if ((*pskb)->nf_bridge &&
830 !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
831 return NF_STOP;
832 }
833
834 return NF_ACCEPT;
835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
838 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
839 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
840 * ip_refrag() can return NF_STOLEN. */
841static struct nf_hook_ops br_nf_ops[] = {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900842 { .hook = br_nf_pre_routing,
843 .owner = THIS_MODULE,
844 .pf = PF_BRIDGE,
845 .hooknum = NF_BR_PRE_ROUTING,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 .priority = NF_BR_PRI_BRNF, },
847 { .hook = br_nf_local_in,
848 .owner = THIS_MODULE,
849 .pf = PF_BRIDGE,
850 .hooknum = NF_BR_LOCAL_IN,
851 .priority = NF_BR_PRI_BRNF, },
852 { .hook = br_nf_forward_ip,
853 .owner = THIS_MODULE,
854 .pf = PF_BRIDGE,
855 .hooknum = NF_BR_FORWARD,
856 .priority = NF_BR_PRI_BRNF - 1, },
857 { .hook = br_nf_forward_arp,
858 .owner = THIS_MODULE,
859 .pf = PF_BRIDGE,
860 .hooknum = NF_BR_FORWARD,
861 .priority = NF_BR_PRI_BRNF, },
862 { .hook = br_nf_local_out,
863 .owner = THIS_MODULE,
864 .pf = PF_BRIDGE,
865 .hooknum = NF_BR_LOCAL_OUT,
866 .priority = NF_BR_PRI_FIRST, },
867 { .hook = br_nf_post_routing,
868 .owner = THIS_MODULE,
869 .pf = PF_BRIDGE,
870 .hooknum = NF_BR_POST_ROUTING,
871 .priority = NF_BR_PRI_LAST, },
872 { .hook = ip_sabotage_in,
873 .owner = THIS_MODULE,
874 .pf = PF_INET,
875 .hooknum = NF_IP_PRE_ROUTING,
876 .priority = NF_IP_PRI_FIRST, },
877 { .hook = ip_sabotage_in,
878 .owner = THIS_MODULE,
879 .pf = PF_INET6,
880 .hooknum = NF_IP6_PRE_ROUTING,
881 .priority = NF_IP6_PRI_FIRST, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882};
883
884#ifdef CONFIG_SYSCTL
885static
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800886int brnf_sysctl_call_tables(ctl_table * ctl, int write, struct file *filp,
887 void __user * buffer, size_t * lenp, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 int ret;
890
891 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
892
893 if (write && *(int *)(ctl->data))
894 *(int *)(ctl->data) = 1;
895 return ret;
896}
897
898static ctl_table brnf_table[] = {
899 {
900 .ctl_name = NET_BRIDGE_NF_CALL_ARPTABLES,
901 .procname = "bridge-nf-call-arptables",
902 .data = &brnf_call_arptables,
903 .maxlen = sizeof(int),
904 .mode = 0644,
905 .proc_handler = &brnf_sysctl_call_tables,
906 },
907 {
908 .ctl_name = NET_BRIDGE_NF_CALL_IPTABLES,
909 .procname = "bridge-nf-call-iptables",
910 .data = &brnf_call_iptables,
911 .maxlen = sizeof(int),
912 .mode = 0644,
913 .proc_handler = &brnf_sysctl_call_tables,
914 },
915 {
916 .ctl_name = NET_BRIDGE_NF_CALL_IP6TABLES,
917 .procname = "bridge-nf-call-ip6tables",
918 .data = &brnf_call_ip6tables,
919 .maxlen = sizeof(int),
920 .mode = 0644,
921 .proc_handler = &brnf_sysctl_call_tables,
922 },
923 {
924 .ctl_name = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
925 .procname = "bridge-nf-filter-vlan-tagged",
926 .data = &brnf_filter_vlan_tagged,
927 .maxlen = sizeof(int),
928 .mode = 0644,
929 .proc_handler = &brnf_sysctl_call_tables,
930 },
931 { .ctl_name = 0 }
932};
933
934static ctl_table brnf_bridge_table[] = {
935 {
936 .ctl_name = NET_BRIDGE,
937 .procname = "bridge",
938 .mode = 0555,
939 .child = brnf_table,
940 },
941 { .ctl_name = 0 }
942};
943
944static ctl_table brnf_net_table[] = {
945 {
946 .ctl_name = CTL_NET,
947 .procname = "net",
948 .mode = 0555,
949 .child = brnf_bridge_table,
950 },
951 { .ctl_name = 0 }
952};
953#endif
954
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800955int __init br_netfilter_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800957 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800959 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
960 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962#ifdef CONFIG_SYSCTL
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800963 brnf_sysctl_header = register_sysctl_table(brnf_net_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (brnf_sysctl_header == NULL) {
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800965 printk(KERN_WARNING
966 "br_netfilter: can't register to sysctl.\n");
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800967 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
968 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 printk(KERN_NOTICE "Bridge firewalling registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return 0;
973}
974
975void br_netfilter_fini(void)
976{
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800977 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978#ifdef CONFIG_SYSCTL
979 unregister_sysctl_table(brnf_sysctl_header);
980#endif
981}