blob: 8b45224699f475df4347d135a5847a27dded759a [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)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070051#define store_orig_dstaddr(skb) (skb_origaddr(skb) = ip_hdr(skb)->daddr)
52#define dnat_took_place(skb) (skb_origaddr(skb) != ip_hdr(skb)->daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
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 Hemminger07317622006-08-29 17:48:17 -0700130 header_size += VLAN_HLEN;
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800131
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300132 skb_copy_from_linear_data_offset(skb, -header_size,
133 skb->nf_bridge->data, header_size);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800134}
135
Stephen Hemminger07317622006-08-29 17:48:17 -0700136/*
137 * When forwarding bridge frames, we save a copy of the original
138 * header before processing.
139 */
140int nf_bridge_copy_header(struct sk_buff *skb)
141{
142 int err;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900143 int header_size = ETH_HLEN;
Stephen Hemminger07317622006-08-29 17:48:17 -0700144
145 if (skb->protocol == htons(ETH_P_8021Q))
146 header_size += VLAN_HLEN;
147
148 err = skb_cow(skb, header_size);
149 if (err)
150 return err;
151
152 memcpy(skb->data - header_size, skb->nf_bridge->data, header_size);
153
154 if (skb->protocol == htons(ETH_P_8021Q))
155 __skb_push(skb, VLAN_HLEN);
156 return 0;
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/* PF_BRIDGE/PRE_ROUTING *********************************************/
160/* Undo the changes made for ip6tables PREROUTING and continue the
161 * bridge PRE_ROUTING hook. */
162static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
163{
164 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (nf_bridge->mask & BRNF_PKT_TYPE) {
167 skb->pkt_type = PACKET_OTHERHOST;
168 nf_bridge->mask ^= BRNF_PKT_TYPE;
169 }
170 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
171
172 skb->dst = (struct dst_entry *)&__fake_rtable;
173 dst_hold(skb->dst);
174
175 skb->dev = nf_bridge->physindev;
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800176 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 skb_push(skb, VLAN_HLEN);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700178 skb->network_header -= VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
181 br_handle_frame_finish, 1);
182
183 return 0;
184}
185
186static void __br_dnat_complain(void)
187{
188 static unsigned long last_complaint;
189
190 if (jiffies - last_complaint >= 5 * HZ) {
191 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800192 "forwarding to be enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 last_complaint = jiffies;
194 }
195}
196
197/* This requires some explaining. If DNAT has taken place,
198 * we will need to fix up the destination Ethernet address,
199 * and this is a tricky process.
200 *
201 * There are two cases to consider:
202 * 1. The packet was DNAT'ed to a device in the same bridge
203 * port group as it was received on. We can still bridge
204 * the packet.
205 * 2. The packet was DNAT'ed to a different device, either
206 * a non-bridged device or another bridge port group.
207 * The packet will need to be routed.
208 *
209 * The correct way of distinguishing between these two cases is to
210 * call ip_route_input() and to look at skb->dst->dev, which is
211 * changed to the destination device if ip_route_input() succeeds.
212 *
213 * Let us first consider the case that ip_route_input() succeeds:
214 *
215 * If skb->dst->dev equals the logical bridge device the packet
216 * came in on, we can consider this bridging. We then call
217 * skb->dst->output() which will make the packet enter br_nf_local_out()
218 * not much later. In that function it is assured that the iptables
219 * FORWARD chain is traversed for the packet.
220 *
221 * Otherwise, the packet is considered to be routed and we just
222 * change the destination MAC address so that the packet will
Bart De Schuymerf216f082006-12-05 13:45:21 -0800223 * later be passed up to the IP stack to be routed. For a redirected
224 * packet, ip_route_input() will give back the localhost as output device,
225 * which differs from the bridge device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 *
227 * Let us now consider the case that ip_route_input() fails:
228 *
Bart De Schuymerf216f082006-12-05 13:45:21 -0800229 * This can be because the destination address is martian, in which case
230 * the packet will be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
232 * will fail, while __ip_route_output_key() will return success. The source
233 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
234 * thinks we're handling a locally generated packet and won't care
235 * if IP forwarding is allowed. We send a warning message to the users's
236 * log telling her to put IP forwarding on.
237 *
238 * ip_route_input() will also fail if there is no route available.
239 * In that case we just drop the packet.
240 *
241 * --Lennert, 20020411
242 * --Bart, 20020416 (updated)
Bart De Schuymerf216f082006-12-05 13:45:21 -0800243 * --Bart, 20021007 (updated)
244 * --Bart, 20062711 (updated) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
246{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (skb->pkt_type == PACKET_OTHERHOST) {
248 skb->pkt_type = PACKET_HOST;
249 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
250 }
251 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
252
253 skb->dev = bridge_parent(skb->dev);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800254 if (!skb->dev)
255 kfree_skb(skb);
256 else {
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800257 if (skb->protocol == htons(ETH_P_8021Q)) {
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800258 skb_pull(skb, VLAN_HLEN);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700259 skb->network_header += VLAN_HLEN;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800260 }
261 skb->dst->output(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return 0;
264}
265
266static int br_nf_pre_routing_finish(struct sk_buff *skb)
267{
268 struct net_device *dev = skb->dev;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700269 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
Bart De Schuymerf216f082006-12-05 13:45:21 -0800271 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (nf_bridge->mask & BRNF_PKT_TYPE) {
274 skb->pkt_type = PACKET_OTHERHOST;
275 nf_bridge->mask ^= BRNF_PKT_TYPE;
276 }
277 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (dnat_took_place(skb)) {
Bart De Schuymerf216f082006-12-05 13:45:21 -0800279 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct rtable *rt;
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800281 struct flowi fl = {
282 .nl_u = {
283 .ip4_u = {
284 .daddr = iph->daddr,
285 .saddr = 0,
286 .tos = RT_TOS(iph->tos) },
287 },
288 .proto = 0,
289 };
Bart De Schuymerf216f082006-12-05 13:45:21 -0800290 struct in_device *in_dev = in_dev_get(dev);
291
292 /* If err equals -EHOSTUNREACH the error is due to a
293 * martian destination or due to the fact that
294 * forwarding is disabled. For most martian packets,
295 * ip_route_output_key() will fail. It won't fail for 2 types of
296 * martian destinations: loopback destinations and destination
297 * 0.0.0.0. In both cases the packet will be dropped because the
298 * destination is the loopback device and not the bridge. */
299 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
300 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (!ip_route_output_key(&rt, &fl)) {
Bart De Schuymer1c011be2005-09-14 20:55:16 -0700303 /* - Bridged-and-DNAT'ed traffic doesn't
Bart De Schuymerf216f082006-12-05 13:45:21 -0800304 * require ip_forwarding. */
305 if (((struct dst_entry *)rt)->dev == dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 skb->dst = (struct dst_entry *)rt;
307 goto bridged_dnat;
308 }
Bart De Schuymerf216f082006-12-05 13:45:21 -0800309 /* we are sure that forwarding is disabled, so printing
310 * this message is no problem. Note that the packet could
311 * still have a martian destination address, in which case
312 * the packet could be dropped even if forwarding were enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 __br_dnat_complain();
314 dst_release((struct dst_entry *)rt);
315 }
Bart De Schuymerf216f082006-12-05 13:45:21 -0800316free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 kfree_skb(skb);
318 return 0;
319 } else {
320 if (skb->dst->dev == dev) {
321bridged_dnat:
322 /* Tell br_nf_local_out this is a
323 * bridged frame */
324 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
325 skb->dev = nf_bridge->physindev;
326 if (skb->protocol ==
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800327 htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 skb_push(skb, VLAN_HLEN);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700329 skb->network_header -= VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
332 skb, skb->dev, NULL,
333 br_nf_pre_routing_finish_bridge,
334 1);
335 return 0;
336 }
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800337 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 skb->pkt_type = PACKET_HOST;
339 }
340 } else {
341 skb->dst = (struct dst_entry *)&__fake_rtable;
342 dst_hold(skb->dst);
343 }
344
345 skb->dev = nf_bridge->physindev;
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800346 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 skb_push(skb, VLAN_HLEN);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700348 skb->network_header -= VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
351 br_handle_frame_finish, 1);
352
353 return 0;
354}
355
356/* Some common code for IPv4/IPv6 */
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800357static struct net_device *setup_pre_routing(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
360
361 if (skb->pkt_type == PACKET_OTHERHOST) {
362 skb->pkt_type = PACKET_HOST;
363 nf_bridge->mask |= BRNF_PKT_TYPE;
364 }
365
366 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
367 nf_bridge->physindev = skb->dev;
368 skb->dev = bridge_parent(skb->dev);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800369
370 return skb->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373/* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
374static int check_hbh_len(struct sk_buff *skb)
375{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700376 unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 u32 pkt_len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700378 const unsigned char *nh = skb_network_header(skb);
379 int off = raw - nh;
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800380 int len = (raw[1] + 1) << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 if ((raw + len) - skb->data > skb_headlen(skb))
383 goto bad;
384
385 off += 2;
386 len -= 2;
387
388 while (len > 0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700389 int optlen = nh[off + 1] + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700391 switch (nh[off]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 case IPV6_TLV_PAD0:
393 optlen = 1;
394 break;
395
396 case IPV6_TLV_PADN:
397 break;
398
399 case IPV6_TLV_JUMBO:
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700400 if (nh[off + 1] != 4 || (off & 3) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 goto bad;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700402 pkt_len = ntohl(*(__be32 *) (nh + off + 2));
Bart De Schuymerb0366482005-12-19 14:00:08 -0800403 if (pkt_len <= IPV6_MAXPLEN ||
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700404 ipv6_hdr(skb)->payload_len)
Bart De Schuymerb0366482005-12-19 14:00:08 -0800405 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
407 goto bad;
Bart De Schuymerb0366482005-12-19 14:00:08 -0800408 if (pskb_trim_rcsum(skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800409 pkt_len + sizeof(struct ipv6hdr)))
Bart De Schuymerb0366482005-12-19 14:00:08 -0800410 goto bad;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700411 nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 break;
413 default:
414 if (optlen > len)
415 goto bad;
416 break;
417 }
418 off += optlen;
419 len -= optlen;
420 }
421 if (len == 0)
422 return 0;
423bad:
424 return -1;
425
426}
427
428/* Replicate the checks that IPv6 does on packet reception and pass the packet
429 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
430static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800431 struct sk_buff *skb,
432 const struct net_device *in,
433 const struct net_device *out,
434 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 struct ipv6hdr *hdr;
437 u32 pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (skb->len < sizeof(struct ipv6hdr))
440 goto inhdr_error;
441
442 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
443 goto inhdr_error;
444
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700445 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 if (hdr->version != 6)
448 goto inhdr_error;
449
450 pkt_len = ntohs(hdr->payload_len);
451
452 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
453 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
454 goto inhdr_error;
Herbert Xub38dfee2006-06-09 16:13:01 -0700455 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
456 goto inhdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800459 goto inhdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800461 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800462 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800464 if (!setup_pre_routing(skb))
465 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
468 br_nf_pre_routing_finish_ipv6);
469
470 return NF_STOLEN;
471
472inhdr_error:
473 return NF_DROP;
474}
475
476/* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
477 * Replicate the checks that IPv4 does on packet reception.
478 * Set skb->dev to the bridge device (i.e. parent of the
479 * receiving device) to make netfilter happy, the REDIRECT
480 * target in particular. Save the original destination IP
481 * address to be able to detect DNAT afterwards. */
482static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
Stephen Hemmingeree02b3a2006-01-06 13:13:29 -0800483 const struct net_device *in,
484 const struct net_device *out,
485 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct iphdr *iph;
488 __u32 len;
489 struct sk_buff *skb = *pskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800491 if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492#ifdef CONFIG_SYSCTL
493 if (!brnf_call_ip6tables)
494 return NF_ACCEPT;
495#endif
496 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
497 goto out;
498
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800499 if (skb->protocol == htons(ETH_P_8021Q)) {
Herbert Xucbb042f2006-03-20 22:43:56 -0800500 skb_pull_rcsum(skb, VLAN_HLEN);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700501 skb->network_header += VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
504 }
505#ifdef CONFIG_SYSCTL
506 if (!brnf_call_iptables)
507 return NF_ACCEPT;
508#endif
509
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800510 if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return NF_ACCEPT;
512
513 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
514 goto out;
515
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800516 if (skb->protocol == htons(ETH_P_8021Q)) {
Herbert Xucbb042f2006-03-20 22:43:56 -0800517 skb_pull_rcsum(skb, VLAN_HLEN);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700518 skb->network_header += VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
521 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
522 goto inhdr_error;
523
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700524 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (iph->ihl < 5 || iph->version != 4)
526 goto inhdr_error;
527
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800528 if (!pskb_may_pull(skb, 4 * iph->ihl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 goto inhdr_error;
530
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700531 iph = ip_hdr(skb);
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800532 if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 goto inhdr_error;
534
535 len = ntohs(iph->tot_len);
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800536 if (skb->len < len || len < 4 * iph->ihl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 goto inhdr_error;
538
Herbert Xub38dfee2006-06-09 16:13:01 -0700539 pskb_trim_rcsum(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800541 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800542 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800544 if (!setup_pre_routing(skb))
545 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 store_orig_dstaddr(skb);
547
548 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
549 br_nf_pre_routing_finish);
550
551 return NF_STOLEN;
552
553inhdr_error:
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800554// IP_INC_STATS_BH(IpInHdrErrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555out:
556 return NF_DROP;
557}
558
559
560/* PF_BRIDGE/LOCAL_IN ************************************************/
561/* The packet is locally destined, which requires a real
562 * dst_entry, so detach the fake one. On the way up, the
563 * packet would pass through PRE_ROUTING again (which already
564 * took place when the packet entered the bridge), but we
565 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
566 * prevent this from happening. */
567static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800568 const struct net_device *in,
569 const struct net_device *out,
570 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 struct sk_buff *skb = *pskb;
573
574 if (skb->dst == (struct dst_entry *)&__fake_rtable) {
575 dst_release(skb->dst);
576 skb->dst = NULL;
577 }
578
579 return NF_ACCEPT;
580}
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582/* PF_BRIDGE/FORWARD *************************************************/
583static int br_nf_forward_finish(struct sk_buff *skb)
584{
585 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
586 struct net_device *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800588 if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 in = nf_bridge->physindev;
590 if (nf_bridge->mask & BRNF_PKT_TYPE) {
591 skb->pkt_type = PACKET_OTHERHOST;
592 nf_bridge->mask ^= BRNF_PKT_TYPE;
593 }
594 } else {
595 in = *((struct net_device **)(skb->cb));
596 }
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800597 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 skb_push(skb, VLAN_HLEN);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700599 skb->network_header -= VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800602 skb->dev, br_forward_finish, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 0;
604}
605
606/* This is the 'purely bridged' case. For IP, we pass the packet to
607 * netfilter with indev and outdev set to the bridge device,
608 * but we are still able to filter on the 'real' indev/outdev
609 * because of the physdev module. For ARP, indev and outdev are the
610 * bridge ports. */
611static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800612 const struct net_device *in,
613 const struct net_device *out,
614 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 struct sk_buff *skb = *pskb;
617 struct nf_bridge_info *nf_bridge;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800618 struct net_device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int pf;
620
621 if (!skb->nf_bridge)
622 return NF_ACCEPT;
623
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800624 parent = bridge_parent(out);
625 if (!parent)
626 return NF_DROP;
627
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800628 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 pf = PF_INET;
630 else
631 pf = PF_INET6;
632
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800633 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 skb_pull(*pskb, VLAN_HLEN);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700635 (*pskb)->network_header += VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 nf_bridge = skb->nf_bridge;
639 if (skb->pkt_type == PACKET_OTHERHOST) {
640 skb->pkt_type = PACKET_HOST;
641 nf_bridge->mask |= BRNF_PKT_TYPE;
642 }
643
644 /* The physdev module checks on this */
645 nf_bridge->mask |= BRNF_BRIDGED;
646 nf_bridge->physoutdev = skb->dev;
647
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800648 NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in), parent,
649 br_nf_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 return NF_STOLEN;
652}
653
654static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800655 const struct net_device *in,
656 const struct net_device *out,
657 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct sk_buff *skb = *pskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 struct net_device **d = (struct net_device **)(skb->cb);
661
662#ifdef CONFIG_SYSCTL
663 if (!brnf_call_arptables)
664 return NF_ACCEPT;
665#endif
666
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800667 if (skb->protocol != htons(ETH_P_ARP)) {
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800668 if (!IS_VLAN_ARP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return NF_ACCEPT;
670 skb_pull(*pskb, VLAN_HLEN);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700671 (*pskb)->network_header += VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -0300674 if (arp_hdr(skb)->ar_pln != 4) {
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800675 if (IS_VLAN_ARP(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 skb_push(*pskb, VLAN_HLEN);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700677 (*pskb)->network_header -= VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679 return NF_ACCEPT;
680 }
681 *d = (struct net_device *)in;
682 NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
683 (struct net_device *)out, br_nf_forward_finish);
684
685 return NF_STOLEN;
686}
687
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800688/* PF_BRIDGE/LOCAL_OUT ***********************************************
689 *
690 * This function sees both locally originated IP packets and forwarded
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 * IP packets (in both cases the destination device is a bridge
692 * device). It also sees bridged-and-DNAT'ed packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 *
694 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
695 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
696 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
697 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
698 * will be executed.
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800699 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800701 const struct net_device *in,
702 const struct net_device *out,
703 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800705 struct net_device *realindev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct sk_buff *skb = *pskb;
707 struct nf_bridge_info *nf_bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 if (!skb->nf_bridge)
710 return NF_ACCEPT;
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 nf_bridge = skb->nf_bridge;
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800713 if (!(nf_bridge->mask & BRNF_BRIDGED_DNAT))
714 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 /* Bridged, take PF_BRIDGE/FORWARD.
717 * (see big note in front of br_nf_pre_routing_finish) */
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800718 nf_bridge->physoutdev = skb->dev;
719 realindev = nf_bridge->physindev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800721 if (nf_bridge->mask & BRNF_PKT_TYPE) {
722 skb->pkt_type = PACKET_OTHERHOST;
723 nf_bridge->mask ^= BRNF_PKT_TYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800725 if (skb->protocol == htons(ETH_P_8021Q)) {
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800726 skb_push(skb, VLAN_HLEN);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700727 skb->network_header -= VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800730 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev, skb->dev,
731 br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return NF_STOLEN;
733}
734
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700735static int br_nf_dev_queue_xmit(struct sk_buff *skb)
736{
737 if (skb->protocol == htons(ETH_P_IP) &&
738 skb->len > skb->dev->mtu &&
Herbert Xu89114af2006-07-08 13:34:32 -0700739 !skb_is_gso(skb))
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700740 return ip_fragment(skb, br_dev_queue_push_xmit);
741 else
742 return br_dev_queue_push_xmit(skb);
743}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745/* PF_BRIDGE/POST_ROUTING ********************************************/
746static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800747 const struct net_device *in,
748 const struct net_device *out,
749 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 struct sk_buff *skb = *pskb;
752 struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct net_device *realoutdev = bridge_parent(skb->dev);
754 int pf;
755
756#ifdef CONFIG_NETFILTER_DEBUG
757 /* Be very paranoid. This probably won't happen anymore, but let's
758 * keep the check just to be sure... */
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700759 if (skb_mac_header(skb) < skb->head ||
760 skb_mac_header(skb) + ETH_HLEN > skb->data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
Stephen Hemminger8394e9b2006-08-29 17:49:31 -0700762 "bad mac.raw pointer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto print_error;
764 }
765#endif
766
767 if (!nf_bridge)
768 return NF_ACCEPT;
769
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800770 if (!realoutdev)
771 return NF_DROP;
772
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800773 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 pf = PF_INET;
775 else
776 pf = PF_INET6;
777
778#ifdef CONFIG_NETFILTER_DEBUG
779 if (skb->dst == NULL) {
Stephen Hemminger8394e9b2006-08-29 17:49:31 -0700780 printk(KERN_INFO "br_netfilter post_routing: skb->dst == NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 goto print_error;
782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783#endif
784
785 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
786 * about the value of skb->pkt_type. */
787 if (skb->pkt_type == PACKET_OTHERHOST) {
788 skb->pkt_type = PACKET_HOST;
789 nf_bridge->mask |= BRNF_PKT_TYPE;
790 }
791
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800792 if (skb->protocol == htons(ETH_P_8021Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 skb_pull(skb, VLAN_HLEN);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700794 skb->network_header += VLAN_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796
797 nf_bridge_save_header(skb);
798
799#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
800 if (nf_bridge->netoutdev)
801 realoutdev = nf_bridge->netoutdev;
802#endif
803 NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev,
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700804 br_nf_dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 return NF_STOLEN;
807
808#ifdef CONFIG_NETFILTER_DEBUG
809print_error:
810 if (skb->dev != NULL) {
811 printk("[%s]", skb->dev->name);
Stephen Hemminger178a3252006-02-13 15:43:58 -0800812 if (realoutdev)
813 printk("[%s]", realoutdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700815 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb_mac_header(skb),
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800816 skb->data);
Stephen Hemminger8394e9b2006-08-29 17:49:31 -0700817 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return NF_ACCEPT;
819#endif
820}
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822/* IP/SABOTAGE *****************************************************/
823/* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
824 * for the second time. */
825static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff **pskb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800826 const struct net_device *in,
827 const struct net_device *out,
828 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 if ((*pskb)->nf_bridge &&
831 !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
832 return NF_STOP;
833 }
834
835 return NF_ACCEPT;
836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838/* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
839 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
840 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
841 * ip_refrag() can return NF_STOLEN. */
842static struct nf_hook_ops br_nf_ops[] = {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900843 { .hook = br_nf_pre_routing,
844 .owner = THIS_MODULE,
845 .pf = PF_BRIDGE,
846 .hooknum = NF_BR_PRE_ROUTING,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 .priority = NF_BR_PRI_BRNF, },
848 { .hook = br_nf_local_in,
849 .owner = THIS_MODULE,
850 .pf = PF_BRIDGE,
851 .hooknum = NF_BR_LOCAL_IN,
852 .priority = NF_BR_PRI_BRNF, },
853 { .hook = br_nf_forward_ip,
854 .owner = THIS_MODULE,
855 .pf = PF_BRIDGE,
856 .hooknum = NF_BR_FORWARD,
857 .priority = NF_BR_PRI_BRNF - 1, },
858 { .hook = br_nf_forward_arp,
859 .owner = THIS_MODULE,
860 .pf = PF_BRIDGE,
861 .hooknum = NF_BR_FORWARD,
862 .priority = NF_BR_PRI_BRNF, },
863 { .hook = br_nf_local_out,
864 .owner = THIS_MODULE,
865 .pf = PF_BRIDGE,
866 .hooknum = NF_BR_LOCAL_OUT,
867 .priority = NF_BR_PRI_FIRST, },
868 { .hook = br_nf_post_routing,
869 .owner = THIS_MODULE,
870 .pf = PF_BRIDGE,
871 .hooknum = NF_BR_POST_ROUTING,
872 .priority = NF_BR_PRI_LAST, },
873 { .hook = ip_sabotage_in,
874 .owner = THIS_MODULE,
875 .pf = PF_INET,
876 .hooknum = NF_IP_PRE_ROUTING,
877 .priority = NF_IP_PRI_FIRST, },
878 { .hook = ip_sabotage_in,
879 .owner = THIS_MODULE,
880 .pf = PF_INET6,
881 .hooknum = NF_IP6_PRE_ROUTING,
882 .priority = NF_IP6_PRI_FIRST, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883};
884
885#ifdef CONFIG_SYSCTL
886static
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800887int brnf_sysctl_call_tables(ctl_table * ctl, int write, struct file *filp,
888 void __user * buffer, size_t * lenp, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 int ret;
891
892 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
893
894 if (write && *(int *)(ctl->data))
895 *(int *)(ctl->data) = 1;
896 return ret;
897}
898
899static ctl_table brnf_table[] = {
900 {
901 .ctl_name = NET_BRIDGE_NF_CALL_ARPTABLES,
902 .procname = "bridge-nf-call-arptables",
903 .data = &brnf_call_arptables,
904 .maxlen = sizeof(int),
905 .mode = 0644,
906 .proc_handler = &brnf_sysctl_call_tables,
907 },
908 {
909 .ctl_name = NET_BRIDGE_NF_CALL_IPTABLES,
910 .procname = "bridge-nf-call-iptables",
911 .data = &brnf_call_iptables,
912 .maxlen = sizeof(int),
913 .mode = 0644,
914 .proc_handler = &brnf_sysctl_call_tables,
915 },
916 {
917 .ctl_name = NET_BRIDGE_NF_CALL_IP6TABLES,
918 .procname = "bridge-nf-call-ip6tables",
919 .data = &brnf_call_ip6tables,
920 .maxlen = sizeof(int),
921 .mode = 0644,
922 .proc_handler = &brnf_sysctl_call_tables,
923 },
924 {
925 .ctl_name = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
926 .procname = "bridge-nf-filter-vlan-tagged",
927 .data = &brnf_filter_vlan_tagged,
928 .maxlen = sizeof(int),
929 .mode = 0644,
930 .proc_handler = &brnf_sysctl_call_tables,
931 },
932 { .ctl_name = 0 }
933};
934
935static ctl_table brnf_bridge_table[] = {
936 {
937 .ctl_name = NET_BRIDGE,
938 .procname = "bridge",
939 .mode = 0555,
940 .child = brnf_table,
941 },
942 { .ctl_name = 0 }
943};
944
945static ctl_table brnf_net_table[] = {
946 {
947 .ctl_name = CTL_NET,
948 .procname = "net",
949 .mode = 0555,
950 .child = brnf_bridge_table,
951 },
952 { .ctl_name = 0 }
953};
954#endif
955
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800956int __init br_netfilter_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800958 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800960 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
961 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963#ifdef CONFIG_SYSCTL
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800964 brnf_sysctl_header = register_sysctl_table(brnf_net_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (brnf_sysctl_header == NULL) {
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800966 printk(KERN_WARNING
967 "br_netfilter: can't register to sysctl.\n");
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800968 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
969 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 printk(KERN_NOTICE "Bridge firewalling registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return 0;
974}
975
976void br_netfilter_fini(void)
977{
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800978 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979#ifdef CONFIG_SYSCTL
980 unregister_sysctl_table(brnf_sysctl_header);
981#endif
982}