blob: 246bf23a8775c991578e9cc8519fc5d280ea310a [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>
Michael Milner516299d2007-04-12 22:14:23 -070032#include <linux/if_pppox.h>
33#include <linux/ppp_defs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/netfilter_bridge.h>
35#include <linux/netfilter_ipv4.h>
36#include <linux/netfilter_ipv6.h>
37#include <linux/netfilter_arp.h>
38#include <linux/in_route.h>
Bart De Schuymerf216f082006-12-05 13:45:21 -080039#include <linux/inetdevice.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <net/ip.h>
42#include <net/ipv6.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020043#include <net/route.h>
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "br_private.h"
47#ifdef CONFIG_SYSCTL
48#include <linux/sysctl.h>
49#endif
50
51#define skb_origaddr(skb) (((struct bridge_skb_cb *) \
52 (skb->nf_bridge->data))->daddr.ipv4)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070053#define store_orig_dstaddr(skb) (skb_origaddr(skb) = ip_hdr(skb)->daddr)
54#define dnat_took_place(skb) (skb_origaddr(skb) != ip_hdr(skb)->daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#ifdef CONFIG_SYSCTL
57static struct ctl_table_header *brnf_sysctl_header;
Brian Haley9c1ea142006-09-18 00:03:41 -070058static int brnf_call_iptables __read_mostly = 1;
59static int brnf_call_ip6tables __read_mostly = 1;
60static int brnf_call_arptables __read_mostly = 1;
61static int brnf_filter_vlan_tagged __read_mostly = 1;
Michael Milner516299d2007-04-12 22:14:23 -070062static int brnf_filter_pppoe_tagged __read_mostly = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#else
64#define brnf_filter_vlan_tagged 1
Michael Milner516299d2007-04-12 22:14:23 -070065#define brnf_filter_pppoe_tagged 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#endif
67
Dave Jonesb6f99a22007-03-22 12:27:49 -070068static inline __be16 vlan_proto(const struct sk_buff *skb)
Stephen Hemminger8b42ec32006-03-20 22:58:05 -080069{
70 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
71}
72
73#define IS_VLAN_IP(skb) \
74 (skb->protocol == htons(ETH_P_8021Q) && \
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090075 vlan_proto(skb) == htons(ETH_P_IP) && \
Stephen Hemminger8b42ec32006-03-20 22:58:05 -080076 brnf_filter_vlan_tagged)
77
78#define IS_VLAN_IPV6(skb) \
79 (skb->protocol == htons(ETH_P_8021Q) && \
80 vlan_proto(skb) == htons(ETH_P_IPV6) &&\
81 brnf_filter_vlan_tagged)
82
83#define IS_VLAN_ARP(skb) \
84 (skb->protocol == htons(ETH_P_8021Q) && \
85 vlan_proto(skb) == htons(ETH_P_ARP) && \
86 brnf_filter_vlan_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Michael Milner516299d2007-04-12 22:14:23 -070088static inline __be16 pppoe_proto(const struct sk_buff *skb)
89{
90 return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
91 sizeof(struct pppoe_hdr)));
92}
93
94#define IS_PPPOE_IP(skb) \
95 (skb->protocol == htons(ETH_P_PPP_SES) && \
96 pppoe_proto(skb) == htons(PPP_IP) && \
97 brnf_filter_pppoe_tagged)
98
99#define IS_PPPOE_IPV6(skb) \
100 (skb->protocol == htons(ETH_P_PPP_SES) && \
101 pppoe_proto(skb) == htons(PPP_IPV6) && \
102 brnf_filter_pppoe_tagged)
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/* We need these fake structures to make netfilter happy --
105 * lots of places assume that skb->dst != NULL, which isn't
106 * all that unreasonable.
107 *
108 * Currently, we fill in the PMTU entry because netfilter
109 * refragmentation needs it, and the rt_flags entry because
110 * ipt_REJECT needs it. Future netfilter modules might
111 * require us to fill additional fields. */
112static struct net_device __fake_net_device = {
113 .hard_header_len = ETH_HLEN
114};
115
116static struct rtable __fake_rtable = {
117 .u = {
118 .dst = {
119 .__refcnt = ATOMIC_INIT(1),
120 .dev = &__fake_net_device,
121 .path = &__fake_rtable.u.dst,
122 .metrics = {[RTAX_MTU - 1] = 1500},
Patrick McHardy42cf93c2006-02-21 13:37:35 -0800123 .flags = DST_NOXFRM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125 },
126 .rt_flags = 0,
127};
128
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800129static inline struct net_device *bridge_parent(const struct net_device *dev)
130{
131 struct net_bridge_port *port = rcu_dereference(dev->br_port);
132
133 return port ? port->br->dev : NULL;
134}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800136static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
137{
138 skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
139 if (likely(skb->nf_bridge))
140 atomic_set(&(skb->nf_bridge->use), 1);
141
142 return skb->nf_bridge;
143}
144
Patrick McHardyfc385822007-05-03 03:36:16 -0700145static inline void nf_bridge_push_encap_header(struct sk_buff *skb)
146{
147 unsigned int len = nf_bridge_encap_header_len(skb);
148
149 skb_push(skb, len);
150 skb->network_header -= len;
151}
152
153static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
154{
155 unsigned int len = nf_bridge_encap_header_len(skb);
156
157 skb_pull(skb, len);
158 skb->network_header += len;
159}
160
161static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
162{
163 unsigned int len = nf_bridge_encap_header_len(skb);
164
165 skb_pull_rcsum(skb, len);
166 skb->network_header += len;
167}
168
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800169static inline void nf_bridge_save_header(struct sk_buff *skb)
170{
Patrick McHardyfc385822007-05-03 03:36:16 -0700171 int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800172
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300173 skb_copy_from_linear_data_offset(skb, -header_size,
174 skb->nf_bridge->data, header_size);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800175}
176
Stephen Hemminger073176212006-08-29 17:48:17 -0700177/*
178 * When forwarding bridge frames, we save a copy of the original
179 * header before processing.
180 */
181int nf_bridge_copy_header(struct sk_buff *skb)
182{
183 int err;
Patrick McHardyfc385822007-05-03 03:36:16 -0700184 int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
Stephen Hemminger073176212006-08-29 17:48:17 -0700185
Herbert Xud9cc2042007-09-16 16:21:16 -0700186 err = skb_cow_head(skb, header_size);
Stephen Hemminger073176212006-08-29 17:48:17 -0700187 if (err)
188 return err;
189
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300190 skb_copy_to_linear_data_offset(skb, -header_size,
191 skb->nf_bridge->data, header_size);
Patrick McHardyfc385822007-05-03 03:36:16 -0700192 __skb_push(skb, nf_bridge_encap_header_len(skb));
Stephen Hemminger073176212006-08-29 17:48:17 -0700193 return 0;
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/* PF_BRIDGE/PRE_ROUTING *********************************************/
197/* Undo the changes made for ip6tables PREROUTING and continue the
198 * bridge PRE_ROUTING hook. */
199static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
200{
201 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (nf_bridge->mask & BRNF_PKT_TYPE) {
204 skb->pkt_type = PACKET_OTHERHOST;
205 nf_bridge->mask ^= BRNF_PKT_TYPE;
206 }
207 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
208
209 skb->dst = (struct dst_entry *)&__fake_rtable;
210 dst_hold(skb->dst);
211
212 skb->dev = nf_bridge->physindev;
Patrick McHardyfc385822007-05-03 03:36:16 -0700213 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
215 br_handle_frame_finish, 1);
216
217 return 0;
218}
219
220static void __br_dnat_complain(void)
221{
222 static unsigned long last_complaint;
223
224 if (jiffies - last_complaint >= 5 * HZ) {
225 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800226 "forwarding to be enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 last_complaint = jiffies;
228 }
229}
230
231/* This requires some explaining. If DNAT has taken place,
232 * we will need to fix up the destination Ethernet address,
233 * and this is a tricky process.
234 *
235 * There are two cases to consider:
236 * 1. The packet was DNAT'ed to a device in the same bridge
237 * port group as it was received on. We can still bridge
238 * the packet.
239 * 2. The packet was DNAT'ed to a different device, either
240 * a non-bridged device or another bridge port group.
241 * The packet will need to be routed.
242 *
243 * The correct way of distinguishing between these two cases is to
244 * call ip_route_input() and to look at skb->dst->dev, which is
245 * changed to the destination device if ip_route_input() succeeds.
246 *
247 * Let us first consider the case that ip_route_input() succeeds:
248 *
249 * If skb->dst->dev equals the logical bridge device the packet
250 * came in on, we can consider this bridging. We then call
251 * skb->dst->output() which will make the packet enter br_nf_local_out()
252 * not much later. In that function it is assured that the iptables
253 * FORWARD chain is traversed for the packet.
254 *
255 * Otherwise, the packet is considered to be routed and we just
256 * change the destination MAC address so that the packet will
Bart De Schuymerf216f082006-12-05 13:45:21 -0800257 * later be passed up to the IP stack to be routed. For a redirected
258 * packet, ip_route_input() will give back the localhost as output device,
259 * which differs from the bridge device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 *
261 * Let us now consider the case that ip_route_input() fails:
262 *
Bart De Schuymerf216f082006-12-05 13:45:21 -0800263 * This can be because the destination address is martian, in which case
264 * the packet will be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
266 * will fail, while __ip_route_output_key() will return success. The source
267 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
268 * thinks we're handling a locally generated packet and won't care
269 * if IP forwarding is allowed. We send a warning message to the users's
270 * log telling her to put IP forwarding on.
271 *
272 * ip_route_input() will also fail if there is no route available.
273 * In that case we just drop the packet.
274 *
275 * --Lennert, 20020411
276 * --Bart, 20020416 (updated)
Bart De Schuymerf216f082006-12-05 13:45:21 -0800277 * --Bart, 20021007 (updated)
278 * --Bart, 20062711 (updated) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
280{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (skb->pkt_type == PACKET_OTHERHOST) {
282 skb->pkt_type = PACKET_HOST;
283 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
284 }
285 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
286
287 skb->dev = bridge_parent(skb->dev);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800288 if (!skb->dev)
289 kfree_skb(skb);
290 else {
Patrick McHardyfc385822007-05-03 03:36:16 -0700291 nf_bridge_pull_encap_header(skb);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800292 skb->dst->output(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return 0;
295}
296
297static int br_nf_pre_routing_finish(struct sk_buff *skb)
298{
299 struct net_device *dev = skb->dev;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700300 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
Bart De Schuymerf216f082006-12-05 13:45:21 -0800302 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (nf_bridge->mask & BRNF_PKT_TYPE) {
305 skb->pkt_type = PACKET_OTHERHOST;
306 nf_bridge->mask ^= BRNF_PKT_TYPE;
307 }
308 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (dnat_took_place(skb)) {
Bart De Schuymerf216f082006-12-05 13:45:21 -0800310 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 struct rtable *rt;
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800312 struct flowi fl = {
313 .nl_u = {
314 .ip4_u = {
315 .daddr = iph->daddr,
316 .saddr = 0,
317 .tos = RT_TOS(iph->tos) },
318 },
319 .proto = 0,
320 };
Bart De Schuymerf216f082006-12-05 13:45:21 -0800321 struct in_device *in_dev = in_dev_get(dev);
322
323 /* If err equals -EHOSTUNREACH the error is due to a
324 * martian destination or due to the fact that
325 * forwarding is disabled. For most martian packets,
326 * ip_route_output_key() will fail. It won't fail for 2 types of
327 * martian destinations: loopback destinations and destination
328 * 0.0.0.0. In both cases the packet will be dropped because the
329 * destination is the loopback device and not the bridge. */
330 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
331 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 if (!ip_route_output_key(&rt, &fl)) {
Bart De Schuymer1c011be2005-09-14 20:55:16 -0700334 /* - Bridged-and-DNAT'ed traffic doesn't
Bart De Schuymerf216f082006-12-05 13:45:21 -0800335 * require ip_forwarding. */
336 if (((struct dst_entry *)rt)->dev == dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 skb->dst = (struct dst_entry *)rt;
338 goto bridged_dnat;
339 }
Bart De Schuymerf216f082006-12-05 13:45:21 -0800340 /* we are sure that forwarding is disabled, so printing
341 * this message is no problem. Note that the packet could
342 * still have a martian destination address, in which case
343 * the packet could be dropped even if forwarding were enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 __br_dnat_complain();
345 dst_release((struct dst_entry *)rt);
346 }
Bart De Schuymerf216f082006-12-05 13:45:21 -0800347free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 kfree_skb(skb);
349 return 0;
350 } else {
351 if (skb->dst->dev == dev) {
352bridged_dnat:
353 /* Tell br_nf_local_out this is a
354 * bridged frame */
355 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
356 skb->dev = nf_bridge->physindev;
Patrick McHardyfc385822007-05-03 03:36:16 -0700357 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
359 skb, skb->dev, NULL,
360 br_nf_pre_routing_finish_bridge,
361 1);
362 return 0;
363 }
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800364 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 skb->pkt_type = PACKET_HOST;
366 }
367 } else {
368 skb->dst = (struct dst_entry *)&__fake_rtable;
369 dst_hold(skb->dst);
370 }
371
372 skb->dev = nf_bridge->physindev;
Patrick McHardyfc385822007-05-03 03:36:16 -0700373 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
375 br_handle_frame_finish, 1);
376
377 return 0;
378}
379
380/* Some common code for IPv4/IPv6 */
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800381static struct net_device *setup_pre_routing(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
384
385 if (skb->pkt_type == PACKET_OTHERHOST) {
386 skb->pkt_type = PACKET_HOST;
387 nf_bridge->mask |= BRNF_PKT_TYPE;
388 }
389
390 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
391 nf_bridge->physindev = skb->dev;
392 skb->dev = bridge_parent(skb->dev);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800393
394 return skb->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397/* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
398static int check_hbh_len(struct sk_buff *skb)
399{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700400 unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 u32 pkt_len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700402 const unsigned char *nh = skb_network_header(skb);
403 int off = raw - nh;
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800404 int len = (raw[1] + 1) << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 if ((raw + len) - skb->data > skb_headlen(skb))
407 goto bad;
408
409 off += 2;
410 len -= 2;
411
412 while (len > 0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700413 int optlen = nh[off + 1] + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700415 switch (nh[off]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 case IPV6_TLV_PAD0:
417 optlen = 1;
418 break;
419
420 case IPV6_TLV_PADN:
421 break;
422
423 case IPV6_TLV_JUMBO:
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700424 if (nh[off + 1] != 4 || (off & 3) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 goto bad;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700426 pkt_len = ntohl(*(__be32 *) (nh + off + 2));
Bart De Schuymerb0366482005-12-19 14:00:08 -0800427 if (pkt_len <= IPV6_MAXPLEN ||
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700428 ipv6_hdr(skb)->payload_len)
Bart De Schuymerb0366482005-12-19 14:00:08 -0800429 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
431 goto bad;
Bart De Schuymerb0366482005-12-19 14:00:08 -0800432 if (pskb_trim_rcsum(skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800433 pkt_len + sizeof(struct ipv6hdr)))
Bart De Schuymerb0366482005-12-19 14:00:08 -0800434 goto bad;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700435 nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 break;
437 default:
438 if (optlen > len)
439 goto bad;
440 break;
441 }
442 off += optlen;
443 len -= optlen;
444 }
445 if (len == 0)
446 return 0;
447bad:
448 return -1;
449
450}
451
452/* Replicate the checks that IPv6 does on packet reception and pass the packet
453 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
454static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800455 struct sk_buff *skb,
456 const struct net_device *in,
457 const struct net_device *out,
458 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct ipv6hdr *hdr;
461 u32 pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (skb->len < sizeof(struct ipv6hdr))
464 goto inhdr_error;
465
466 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
467 goto inhdr_error;
468
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700469 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 if (hdr->version != 6)
472 goto inhdr_error;
473
474 pkt_len = ntohs(hdr->payload_len);
475
476 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
477 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
478 goto inhdr_error;
Herbert Xub38dfee2006-06-09 16:13:01 -0700479 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
480 goto inhdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800483 goto inhdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800485 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800486 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800488 if (!setup_pre_routing(skb))
489 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
492 br_nf_pre_routing_finish_ipv6);
493
494 return NF_STOLEN;
495
496inhdr_error:
497 return NF_DROP;
498}
499
500/* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
501 * Replicate the checks that IPv4 does on packet reception.
502 * Set skb->dev to the bridge device (i.e. parent of the
503 * receiving device) to make netfilter happy, the REDIRECT
504 * target in particular. Save the original destination IP
505 * address to be able to detect DNAT afterwards. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700506static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff *skb,
Stephen Hemmingeree02b3a2006-01-06 13:13:29 -0800507 const struct net_device *in,
508 const struct net_device *out,
509 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 struct iphdr *iph;
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700512 __u32 len = nf_bridge_encap_header_len(skb);
513
514 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
515 return NF_STOLEN;
516
517 if (unlikely(!pskb_may_pull(skb, len)))
518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Michael Milner516299d2007-04-12 22:14:23 -0700520 if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
521 IS_PPPOE_IPV6(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522#ifdef CONFIG_SYSCTL
523 if (!brnf_call_ip6tables)
524 return NF_ACCEPT;
525#endif
Patrick McHardyfc385822007-05-03 03:36:16 -0700526 nf_bridge_pull_encap_header_rcsum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
528 }
529#ifdef CONFIG_SYSCTL
530 if (!brnf_call_iptables)
531 return NF_ACCEPT;
532#endif
533
Michael Milner516299d2007-04-12 22:14:23 -0700534 if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb) &&
535 !IS_PPPOE_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return NF_ACCEPT;
537
Patrick McHardyfc385822007-05-03 03:36:16 -0700538 nf_bridge_pull_encap_header_rcsum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
541 goto inhdr_error;
542
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700543 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (iph->ihl < 5 || iph->version != 4)
545 goto inhdr_error;
546
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800547 if (!pskb_may_pull(skb, 4 * iph->ihl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 goto inhdr_error;
549
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700550 iph = ip_hdr(skb);
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800551 if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 goto inhdr_error;
553
554 len = ntohs(iph->tot_len);
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800555 if (skb->len < len || len < 4 * iph->ihl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 goto inhdr_error;
557
Herbert Xub38dfee2006-06-09 16:13:01 -0700558 pskb_trim_rcsum(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800560 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800561 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800563 if (!setup_pre_routing(skb))
564 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 store_orig_dstaddr(skb);
566
567 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
568 br_nf_pre_routing_finish);
569
570 return NF_STOLEN;
571
572inhdr_error:
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800573// IP_INC_STATS_BH(IpInHdrErrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574out:
575 return NF_DROP;
576}
577
578
579/* PF_BRIDGE/LOCAL_IN ************************************************/
580/* The packet is locally destined, which requires a real
581 * dst_entry, so detach the fake one. On the way up, the
582 * packet would pass through PRE_ROUTING again (which already
583 * took place when the packet entered the bridge), but we
584 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
585 * prevent this from happening. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700586static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800587 const struct net_device *in,
588 const struct net_device *out,
589 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (skb->dst == (struct dst_entry *)&__fake_rtable) {
592 dst_release(skb->dst);
593 skb->dst = NULL;
594 }
595
596 return NF_ACCEPT;
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/* PF_BRIDGE/FORWARD *************************************************/
600static int br_nf_forward_finish(struct sk_buff *skb)
601{
602 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
603 struct net_device *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800605 if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 in = nf_bridge->physindev;
607 if (nf_bridge->mask & BRNF_PKT_TYPE) {
608 skb->pkt_type = PACKET_OTHERHOST;
609 nf_bridge->mask ^= BRNF_PKT_TYPE;
610 }
611 } else {
612 in = *((struct net_device **)(skb->cb));
613 }
Patrick McHardyfc385822007-05-03 03:36:16 -0700614 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800616 skb->dev, br_forward_finish, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return 0;
618}
619
620/* This is the 'purely bridged' case. For IP, we pass the packet to
621 * netfilter with indev and outdev set to the bridge device,
622 * but we are still able to filter on the 'real' indev/outdev
623 * because of the physdev module. For ARP, indev and outdev are the
624 * bridge ports. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700625static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800626 const struct net_device *in,
627 const struct net_device *out,
628 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct nf_bridge_info *nf_bridge;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800631 struct net_device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int pf;
633
634 if (!skb->nf_bridge)
635 return NF_ACCEPT;
636
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800637 parent = bridge_parent(out);
638 if (!parent)
639 return NF_DROP;
640
Michael Milner516299d2007-04-12 22:14:23 -0700641 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
642 IS_PPPOE_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 pf = PF_INET;
644 else
645 pf = PF_INET6;
646
Herbert Xu3db05fe2007-10-15 00:53:15 -0700647 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 nf_bridge = skb->nf_bridge;
650 if (skb->pkt_type == PACKET_OTHERHOST) {
651 skb->pkt_type = PACKET_HOST;
652 nf_bridge->mask |= BRNF_PKT_TYPE;
653 }
654
655 /* The physdev module checks on this */
656 nf_bridge->mask |= BRNF_BRIDGED;
657 nf_bridge->physoutdev = skb->dev;
658
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800659 NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in), parent,
660 br_nf_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 return NF_STOLEN;
663}
664
Herbert Xu3db05fe2007-10-15 00:53:15 -0700665static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800666 const struct net_device *in,
667 const struct net_device *out,
668 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct net_device **d = (struct net_device **)(skb->cb);
671
672#ifdef CONFIG_SYSCTL
673 if (!brnf_call_arptables)
674 return NF_ACCEPT;
675#endif
676
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800677 if (skb->protocol != htons(ETH_P_ARP)) {
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800678 if (!IS_VLAN_ARP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return NF_ACCEPT;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700680 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -0300683 if (arp_hdr(skb)->ar_pln != 4) {
Patrick McHardyfc385822007-05-03 03:36:16 -0700684 if (IS_VLAN_ARP(skb))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700685 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return NF_ACCEPT;
687 }
688 *d = (struct net_device *)in;
689 NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
690 (struct net_device *)out, br_nf_forward_finish);
691
692 return NF_STOLEN;
693}
694
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800695/* PF_BRIDGE/LOCAL_OUT ***********************************************
696 *
697 * This function sees both locally originated IP packets and forwarded
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 * IP packets (in both cases the destination device is a bridge
699 * device). It also sees bridged-and-DNAT'ed packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 *
701 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
702 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
703 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
704 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
705 * will be executed.
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800706 */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700707static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800708 const struct net_device *in,
709 const struct net_device *out,
710 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800712 struct net_device *realindev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct nf_bridge_info *nf_bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 if (!skb->nf_bridge)
716 return NF_ACCEPT;
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 nf_bridge = skb->nf_bridge;
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800719 if (!(nf_bridge->mask & BRNF_BRIDGED_DNAT))
720 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 /* Bridged, take PF_BRIDGE/FORWARD.
723 * (see big note in front of br_nf_pre_routing_finish) */
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800724 nf_bridge->physoutdev = skb->dev;
725 realindev = nf_bridge->physindev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800727 if (nf_bridge->mask & BRNF_PKT_TYPE) {
728 skb->pkt_type = PACKET_OTHERHOST;
729 nf_bridge->mask ^= BRNF_PKT_TYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
Patrick McHardyfc385822007-05-03 03:36:16 -0700731 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800733 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev, skb->dev,
734 br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return NF_STOLEN;
736}
737
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700738static int br_nf_dev_queue_xmit(struct sk_buff *skb)
739{
740 if (skb->protocol == htons(ETH_P_IP) &&
741 skb->len > skb->dev->mtu &&
Herbert Xu89114af2006-07-08 13:34:32 -0700742 !skb_is_gso(skb))
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700743 return ip_fragment(skb, br_dev_queue_push_xmit);
744 else
745 return br_dev_queue_push_xmit(skb);
746}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748/* PF_BRIDGE/POST_ROUTING ********************************************/
Herbert Xu3db05fe2007-10-15 00:53:15 -0700749static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800750 const struct net_device *in,
751 const struct net_device *out,
752 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700754 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 struct net_device *realoutdev = bridge_parent(skb->dev);
756 int pf;
757
758#ifdef CONFIG_NETFILTER_DEBUG
759 /* Be very paranoid. This probably won't happen anymore, but let's
760 * keep the check just to be sure... */
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700761 if (skb_mac_header(skb) < skb->head ||
762 skb_mac_header(skb) + ETH_HLEN > skb->data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
Stephen Hemminger8394e9b2006-08-29 17:49:31 -0700764 "bad mac.raw pointer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto print_error;
766 }
767#endif
768
769 if (!nf_bridge)
770 return NF_ACCEPT;
771
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800772 if (!realoutdev)
773 return NF_DROP;
774
Michael Milner516299d2007-04-12 22:14:23 -0700775 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
776 IS_PPPOE_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 pf = PF_INET;
778 else
779 pf = PF_INET6;
780
781#ifdef CONFIG_NETFILTER_DEBUG
782 if (skb->dst == NULL) {
Stephen Hemminger8394e9b2006-08-29 17:49:31 -0700783 printk(KERN_INFO "br_netfilter post_routing: skb->dst == NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 goto print_error;
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786#endif
787
788 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
789 * about the value of skb->pkt_type. */
790 if (skb->pkt_type == PACKET_OTHERHOST) {
791 skb->pkt_type = PACKET_HOST;
792 nf_bridge->mask |= BRNF_PKT_TYPE;
793 }
794
Patrick McHardyfc385822007-05-03 03:36:16 -0700795 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 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. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700824static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff *skb,
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{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700829 if (skb->nf_bridge &&
830 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 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 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 .procname = "bridge-nf-call-arptables",
901 .data = &brnf_call_arptables,
902 .maxlen = sizeof(int),
903 .mode = 0644,
904 .proc_handler = &brnf_sysctl_call_tables,
905 },
906 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 .procname = "bridge-nf-call-iptables",
908 .data = &brnf_call_iptables,
909 .maxlen = sizeof(int),
910 .mode = 0644,
911 .proc_handler = &brnf_sysctl_call_tables,
912 },
913 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 .procname = "bridge-nf-call-ip6tables",
915 .data = &brnf_call_ip6tables,
916 .maxlen = sizeof(int),
917 .mode = 0644,
918 .proc_handler = &brnf_sysctl_call_tables,
919 },
920 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 .procname = "bridge-nf-filter-vlan-tagged",
922 .data = &brnf_filter_vlan_tagged,
923 .maxlen = sizeof(int),
924 .mode = 0644,
925 .proc_handler = &brnf_sysctl_call_tables,
926 },
Michael Milner516299d2007-04-12 22:14:23 -0700927 {
Michael Milner516299d2007-04-12 22:14:23 -0700928 .procname = "bridge-nf-filter-pppoe-tagged",
929 .data = &brnf_filter_pppoe_tagged,
930 .maxlen = sizeof(int),
931 .mode = 0644,
932 .proc_handler = &brnf_sysctl_call_tables,
933 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 { .ctl_name = 0 }
935};
936
937static ctl_table brnf_bridge_table[] = {
938 {
939 .ctl_name = NET_BRIDGE,
940 .procname = "bridge",
941 .mode = 0555,
942 .child = brnf_table,
943 },
944 { .ctl_name = 0 }
945};
946
947static ctl_table brnf_net_table[] = {
948 {
949 .ctl_name = CTL_NET,
950 .procname = "net",
951 .mode = 0555,
952 .child = brnf_bridge_table,
953 },
954 { .ctl_name = 0 }
955};
956#endif
957
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800958int __init br_netfilter_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800960 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800962 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
963 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965#ifdef CONFIG_SYSCTL
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800966 brnf_sysctl_header = register_sysctl_table(brnf_net_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (brnf_sysctl_header == NULL) {
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800968 printk(KERN_WARNING
969 "br_netfilter: can't register to sysctl.\n");
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800970 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
971 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 printk(KERN_NOTICE "Bridge firewalling registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return 0;
976}
977
978void br_netfilter_fini(void)
979{
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800980 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981#ifdef CONFIG_SYSCTL
982 unregister_sysctl_table(brnf_sysctl_header);
983#endif
984}