blob: e4a418fcb35bfe3d163210f0ceeb19a09535e64c [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;
Herbert Xu47e0e1c2009-01-12 00:06:03 +000061static int brnf_filter_vlan_tagged __read_mostly = 0;
62static int brnf_filter_pppoe_tagged __read_mostly = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#else
Herbert Xu47e0e1c2009-01-12 00:06:03 +000064#define brnf_filter_vlan_tagged 0
65#define brnf_filter_pppoe_tagged 0
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
Herbert Xu631339f2008-11-24 16:06:50 -0800104static void fake_update_pmtu(struct dst_entry *dst, u32 mtu)
105{
106}
107
108static struct dst_ops fake_dst_ops = {
109 .family = AF_INET,
Harvey Harrison09640e62009-02-01 00:45:17 -0800110 .protocol = cpu_to_be16(ETH_P_IP),
Herbert Xu631339f2008-11-24 16:06:50 -0800111 .update_pmtu = fake_update_pmtu,
Herbert Xu631339f2008-11-24 16:06:50 -0800112 .entries = ATOMIC_INIT(0),
113};
114
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700115/*
116 * Initialize bogus route table used to keep netfilter happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * Currently, we fill in the PMTU entry because netfilter
118 * refragmentation needs it, and the rt_flags entry because
119 * ipt_REJECT needs it. Future netfilter modules might
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700120 * require us to fill additional fields.
121 */
122void br_netfilter_rtable_init(struct net_bridge *br)
123{
124 struct rtable *rt = &br->fake_rtable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700126 atomic_set(&rt->u.dst.__refcnt, 1);
Rami Rosenad619802008-08-05 01:21:22 -0700127 rt->u.dst.dev = br->dev;
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700128 rt->u.dst.path = &rt->u.dst;
129 rt->u.dst.metrics[RTAX_MTU - 1] = 1500;
130 rt->u.dst.flags = DST_NOXFRM;
Herbert Xu631339f2008-11-24 16:06:50 -0800131 rt->u.dst.ops = &fake_dst_ops;
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700132}
133
134static inline struct rtable *bridge_parent_rtable(const struct net_device *dev)
135{
136 struct net_bridge_port *port = rcu_dereference(dev->br_port);
137
138 return port ? &port->br->fake_rtable : NULL;
139}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800141static inline struct net_device *bridge_parent(const struct net_device *dev)
142{
143 struct net_bridge_port *port = rcu_dereference(dev->br_port);
144
145 return port ? port->br->dev : NULL;
146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800148static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
149{
150 skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
151 if (likely(skb->nf_bridge))
152 atomic_set(&(skb->nf_bridge->use), 1);
153
154 return skb->nf_bridge;
155}
156
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800157static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
158{
159 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
160
161 if (atomic_read(&nf_bridge->use) > 1) {
162 struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
163
164 if (tmp) {
165 memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
166 atomic_set(&tmp->use, 1);
167 nf_bridge_put(nf_bridge);
168 }
169 nf_bridge = tmp;
170 }
171 return nf_bridge;
172}
173
Patrick McHardyfc385822007-05-03 03:36:16 -0700174static inline void nf_bridge_push_encap_header(struct sk_buff *skb)
175{
176 unsigned int len = nf_bridge_encap_header_len(skb);
177
178 skb_push(skb, len);
179 skb->network_header -= len;
180}
181
182static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
183{
184 unsigned int len = nf_bridge_encap_header_len(skb);
185
186 skb_pull(skb, len);
187 skb->network_header += len;
188}
189
190static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
191{
192 unsigned int len = nf_bridge_encap_header_len(skb);
193
194 skb_pull_rcsum(skb, len);
195 skb->network_header += len;
196}
197
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800198static inline void nf_bridge_save_header(struct sk_buff *skb)
199{
Patrick McHardyfc385822007-05-03 03:36:16 -0700200 int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800201
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300202 skb_copy_from_linear_data_offset(skb, -header_size,
203 skb->nf_bridge->data, header_size);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800204}
205
Stephen Hemminger073176212006-08-29 17:48:17 -0700206/*
207 * When forwarding bridge frames, we save a copy of the original
208 * header before processing.
209 */
210int nf_bridge_copy_header(struct sk_buff *skb)
211{
212 int err;
Patrick McHardyfc385822007-05-03 03:36:16 -0700213 int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
Stephen Hemminger073176212006-08-29 17:48:17 -0700214
Herbert Xud9cc2042007-09-16 16:21:16 -0700215 err = skb_cow_head(skb, header_size);
Stephen Hemminger073176212006-08-29 17:48:17 -0700216 if (err)
217 return err;
218
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300219 skb_copy_to_linear_data_offset(skb, -header_size,
220 skb->nf_bridge->data, header_size);
Patrick McHardyfc385822007-05-03 03:36:16 -0700221 __skb_push(skb, nf_bridge_encap_header_len(skb));
Stephen Hemminger073176212006-08-29 17:48:17 -0700222 return 0;
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/* PF_BRIDGE/PRE_ROUTING *********************************************/
226/* Undo the changes made for ip6tables PREROUTING and continue the
227 * bridge PRE_ROUTING hook. */
228static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
229{
230 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (nf_bridge->mask & BRNF_PKT_TYPE) {
233 skb->pkt_type = PACKET_OTHERHOST;
234 nf_bridge->mask ^= BRNF_PKT_TYPE;
235 }
236 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
237
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700238 skb->rtable = bridge_parent_rtable(nf_bridge->physindev);
239 if (!skb->rtable) {
240 kfree_skb(skb);
241 return 0;
242 }
243 dst_hold(&skb->rtable->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 skb->dev = nf_bridge->physindev;
Patrick McHardyfc385822007-05-03 03:36:16 -0700246 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
248 br_handle_frame_finish, 1);
249
250 return 0;
251}
252
253static void __br_dnat_complain(void)
254{
255 static unsigned long last_complaint;
256
257 if (jiffies - last_complaint >= 5 * HZ) {
258 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800259 "forwarding to be enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 last_complaint = jiffies;
261 }
262}
263
264/* This requires some explaining. If DNAT has taken place,
265 * we will need to fix up the destination Ethernet address,
266 * and this is a tricky process.
267 *
268 * There are two cases to consider:
269 * 1. The packet was DNAT'ed to a device in the same bridge
270 * port group as it was received on. We can still bridge
271 * the packet.
272 * 2. The packet was DNAT'ed to a different device, either
273 * a non-bridged device or another bridge port group.
274 * The packet will need to be routed.
275 *
276 * The correct way of distinguishing between these two cases is to
277 * call ip_route_input() and to look at skb->dst->dev, which is
278 * changed to the destination device if ip_route_input() succeeds.
279 *
280 * Let us first consider the case that ip_route_input() succeeds:
281 *
282 * If skb->dst->dev equals the logical bridge device the packet
Patrick McHardy2948d2e2008-01-11 18:02:18 -0800283 * came in on, we can consider this bridging. The packet is passed
284 * through the neighbour output function to build a new destination
285 * MAC address, which will make the packet enter br_nf_local_out()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * not much later. In that function it is assured that the iptables
287 * FORWARD chain is traversed for the packet.
288 *
289 * Otherwise, the packet is considered to be routed and we just
290 * change the destination MAC address so that the packet will
Bart De Schuymerf216f082006-12-05 13:45:21 -0800291 * later be passed up to the IP stack to be routed. For a redirected
292 * packet, ip_route_input() will give back the localhost as output device,
293 * which differs from the bridge device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 *
295 * Let us now consider the case that ip_route_input() fails:
296 *
Bart De Schuymerf216f082006-12-05 13:45:21 -0800297 * This can be because the destination address is martian, in which case
298 * the packet will be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
300 * will fail, while __ip_route_output_key() will return success. The source
301 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
302 * thinks we're handling a locally generated packet and won't care
303 * if IP forwarding is allowed. We send a warning message to the users's
304 * log telling her to put IP forwarding on.
305 *
306 * ip_route_input() will also fail if there is no route available.
307 * In that case we just drop the packet.
308 *
309 * --Lennert, 20020411
310 * --Bart, 20020416 (updated)
Bart De Schuymerf216f082006-12-05 13:45:21 -0800311 * --Bart, 20021007 (updated)
312 * --Bart, 20062711 (updated) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
314{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (skb->pkt_type == PACKET_OTHERHOST) {
316 skb->pkt_type = PACKET_HOST;
317 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
318 }
319 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
320
321 skb->dev = bridge_parent(skb->dev);
Patrick McHardy2948d2e2008-01-11 18:02:18 -0800322 if (skb->dev) {
323 struct dst_entry *dst = skb->dst;
324
Patrick McHardyfc385822007-05-03 03:36:16 -0700325 nf_bridge_pull_encap_header(skb);
Patrick McHardy2948d2e2008-01-11 18:02:18 -0800326
327 if (dst->hh)
328 return neigh_hh_output(dst->hh, skb);
329 else if (dst->neighbour)
330 return dst->neighbour->output(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Patrick McHardy2948d2e2008-01-11 18:02:18 -0800332 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return 0;
334}
335
336static int br_nf_pre_routing_finish(struct sk_buff *skb)
337{
338 struct net_device *dev = skb->dev;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700339 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
Bart De Schuymerf216f082006-12-05 13:45:21 -0800341 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (nf_bridge->mask & BRNF_PKT_TYPE) {
344 skb->pkt_type = PACKET_OTHERHOST;
345 nf_bridge->mask ^= BRNF_PKT_TYPE;
346 }
347 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (dnat_took_place(skb)) {
Bart De Schuymerf216f082006-12-05 13:45:21 -0800349 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct rtable *rt;
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800351 struct flowi fl = {
352 .nl_u = {
353 .ip4_u = {
354 .daddr = iph->daddr,
355 .saddr = 0,
356 .tos = RT_TOS(iph->tos) },
357 },
358 .proto = 0,
359 };
Bart De Schuymerf216f082006-12-05 13:45:21 -0800360 struct in_device *in_dev = in_dev_get(dev);
361
362 /* If err equals -EHOSTUNREACH the error is due to a
363 * martian destination or due to the fact that
364 * forwarding is disabled. For most martian packets,
365 * ip_route_output_key() will fail. It won't fail for 2 types of
366 * martian destinations: loopback destinations and destination
367 * 0.0.0.0. In both cases the packet will be dropped because the
368 * destination is the loopback device and not the bridge. */
369 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
370 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Alexey Dobriyan249b6202008-11-04 14:31:29 +0100372 if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
Bart De Schuymer1c011be2005-09-14 20:55:16 -0700373 /* - Bridged-and-DNAT'ed traffic doesn't
Bart De Schuymerf216f082006-12-05 13:45:21 -0800374 * require ip_forwarding. */
375 if (((struct dst_entry *)rt)->dev == dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 skb->dst = (struct dst_entry *)rt;
377 goto bridged_dnat;
378 }
Bart De Schuymerf216f082006-12-05 13:45:21 -0800379 /* we are sure that forwarding is disabled, so printing
380 * this message is no problem. Note that the packet could
381 * still have a martian destination address, in which case
382 * the packet could be dropped even if forwarding were enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 __br_dnat_complain();
384 dst_release((struct dst_entry *)rt);
385 }
Bart De Schuymerf216f082006-12-05 13:45:21 -0800386free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 kfree_skb(skb);
388 return 0;
389 } else {
390 if (skb->dst->dev == dev) {
391bridged_dnat:
392 /* Tell br_nf_local_out this is a
393 * bridged frame */
394 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
395 skb->dev = nf_bridge->physindev;
Patrick McHardyfc385822007-05-03 03:36:16 -0700396 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
398 skb, skb->dev, NULL,
399 br_nf_pre_routing_finish_bridge,
400 1);
401 return 0;
402 }
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800403 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 skb->pkt_type = PACKET_HOST;
405 }
406 } else {
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700407 skb->rtable = bridge_parent_rtable(nf_bridge->physindev);
408 if (!skb->rtable) {
409 kfree_skb(skb);
410 return 0;
411 }
412 dst_hold(&skb->rtable->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
415 skb->dev = nf_bridge->physindev;
Patrick McHardyfc385822007-05-03 03:36:16 -0700416 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
418 br_handle_frame_finish, 1);
419
420 return 0;
421}
422
423/* Some common code for IPv4/IPv6 */
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800424static struct net_device *setup_pre_routing(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
427
428 if (skb->pkt_type == PACKET_OTHERHOST) {
429 skb->pkt_type = PACKET_HOST;
430 nf_bridge->mask |= BRNF_PKT_TYPE;
431 }
432
433 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
434 nf_bridge->physindev = skb->dev;
435 skb->dev = bridge_parent(skb->dev);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800436
437 return skb->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440/* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
441static int check_hbh_len(struct sk_buff *skb)
442{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700443 unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 u32 pkt_len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700445 const unsigned char *nh = skb_network_header(skb);
446 int off = raw - nh;
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800447 int len = (raw[1] + 1) << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 if ((raw + len) - skb->data > skb_headlen(skb))
450 goto bad;
451
452 off += 2;
453 len -= 2;
454
455 while (len > 0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700456 int optlen = nh[off + 1] + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700458 switch (nh[off]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 case IPV6_TLV_PAD0:
460 optlen = 1;
461 break;
462
463 case IPV6_TLV_PADN:
464 break;
465
466 case IPV6_TLV_JUMBO:
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700467 if (nh[off + 1] != 4 || (off & 3) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 goto bad;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700469 pkt_len = ntohl(*(__be32 *) (nh + off + 2));
Bart De Schuymerb0366482005-12-19 14:00:08 -0800470 if (pkt_len <= IPV6_MAXPLEN ||
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700471 ipv6_hdr(skb)->payload_len)
Bart De Schuymerb0366482005-12-19 14:00:08 -0800472 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
474 goto bad;
Bart De Schuymerb0366482005-12-19 14:00:08 -0800475 if (pskb_trim_rcsum(skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800476 pkt_len + sizeof(struct ipv6hdr)))
Bart De Schuymerb0366482005-12-19 14:00:08 -0800477 goto bad;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700478 nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480 default:
481 if (optlen > len)
482 goto bad;
483 break;
484 }
485 off += optlen;
486 len -= optlen;
487 }
488 if (len == 0)
489 return 0;
490bad:
491 return -1;
492
493}
494
495/* Replicate the checks that IPv6 does on packet reception and pass the packet
496 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
497static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800498 struct sk_buff *skb,
499 const struct net_device *in,
500 const struct net_device *out,
501 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct ipv6hdr *hdr;
504 u32 pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 if (skb->len < sizeof(struct ipv6hdr))
507 goto inhdr_error;
508
509 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
510 goto inhdr_error;
511
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700512 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 if (hdr->version != 6)
515 goto inhdr_error;
516
517 pkt_len = ntohs(hdr->payload_len);
518
519 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
520 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
521 goto inhdr_error;
Herbert Xub38dfee2006-06-09 16:13:01 -0700522 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
523 goto inhdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800526 goto inhdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800528 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800529 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800531 if (!setup_pre_routing(skb))
532 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800534 NF_HOOK(PF_INET6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 br_nf_pre_routing_finish_ipv6);
536
537 return NF_STOLEN;
538
539inhdr_error:
540 return NF_DROP;
541}
542
543/* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
544 * Replicate the checks that IPv4 does on packet reception.
545 * Set skb->dev to the bridge device (i.e. parent of the
546 * receiving device) to make netfilter happy, the REDIRECT
547 * target in particular. Save the original destination IP
548 * address to be able to detect DNAT afterwards. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700549static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff *skb,
Stephen Hemmingeree02b3a2006-01-06 13:13:29 -0800550 const struct net_device *in,
551 const struct net_device *out,
552 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 struct iphdr *iph;
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700555 __u32 len = nf_bridge_encap_header_len(skb);
556
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700557 if (unlikely(!pskb_may_pull(skb, len)))
558 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Michael Milner516299d2007-04-12 22:14:23 -0700560 if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
561 IS_PPPOE_IPV6(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562#ifdef CONFIG_SYSCTL
563 if (!brnf_call_ip6tables)
564 return NF_ACCEPT;
565#endif
Patrick McHardyfc385822007-05-03 03:36:16 -0700566 nf_bridge_pull_encap_header_rcsum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
568 }
569#ifdef CONFIG_SYSCTL
570 if (!brnf_call_iptables)
571 return NF_ACCEPT;
572#endif
573
Michael Milner516299d2007-04-12 22:14:23 -0700574 if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb) &&
575 !IS_PPPOE_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return NF_ACCEPT;
577
Patrick McHardyfc385822007-05-03 03:36:16 -0700578 nf_bridge_pull_encap_header_rcsum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
581 goto inhdr_error;
582
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700583 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (iph->ihl < 5 || iph->version != 4)
585 goto inhdr_error;
586
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800587 if (!pskb_may_pull(skb, 4 * iph->ihl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 goto inhdr_error;
589
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700590 iph = ip_hdr(skb);
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800591 if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 goto inhdr_error;
593
594 len = ntohs(iph->tot_len);
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800595 if (skb->len < len || len < 4 * iph->ihl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 goto inhdr_error;
597
Herbert Xub38dfee2006-06-09 16:13:01 -0700598 pskb_trim_rcsum(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800600 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800601 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800603 if (!setup_pre_routing(skb))
604 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 store_orig_dstaddr(skb);
606
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800607 NF_HOOK(PF_INET, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 br_nf_pre_routing_finish);
609
610 return NF_STOLEN;
611
612inhdr_error:
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800613// IP_INC_STATS_BH(IpInHdrErrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614out:
615 return NF_DROP;
616}
617
618
619/* PF_BRIDGE/LOCAL_IN ************************************************/
620/* The packet is locally destined, which requires a real
621 * dst_entry, so detach the fake one. On the way up, the
622 * packet would pass through PRE_ROUTING again (which already
623 * took place when the packet entered the bridge), but we
624 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
625 * prevent this from happening. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700626static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800627 const struct net_device *in,
628 const struct net_device *out,
629 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700631 if (skb->rtable && skb->rtable == bridge_parent_rtable(in)) {
632 dst_release(&skb->rtable->u.dst);
Eric Dumazetee6b9672008-03-05 18:30:47 -0800633 skb->rtable = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
636 return NF_ACCEPT;
637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639/* PF_BRIDGE/FORWARD *************************************************/
640static int br_nf_forward_finish(struct sk_buff *skb)
641{
642 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
643 struct net_device *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800645 if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 in = nf_bridge->physindev;
647 if (nf_bridge->mask & BRNF_PKT_TYPE) {
648 skb->pkt_type = PACKET_OTHERHOST;
649 nf_bridge->mask ^= BRNF_PKT_TYPE;
650 }
651 } else {
652 in = *((struct net_device **)(skb->cb));
653 }
Patrick McHardyfc385822007-05-03 03:36:16 -0700654 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800656 skb->dev, br_forward_finish, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return 0;
658}
659
660/* This is the 'purely bridged' case. For IP, we pass the packet to
661 * netfilter with indev and outdev set to the bridge device,
662 * but we are still able to filter on the 'real' indev/outdev
663 * because of the physdev module. For ARP, indev and outdev are the
664 * bridge ports. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700665static unsigned int br_nf_forward_ip(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 nf_bridge_info *nf_bridge;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800671 struct net_device *parent;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200672 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 if (!skb->nf_bridge)
675 return NF_ACCEPT;
676
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800677 /* Need exclusive nf_bridge_info since we might have multiple
678 * different physoutdevs. */
679 if (!nf_bridge_unshare(skb))
680 return NF_DROP;
681
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800682 parent = bridge_parent(out);
683 if (!parent)
684 return NF_DROP;
685
Michael Milner516299d2007-04-12 22:14:23 -0700686 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
687 IS_PPPOE_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 pf = PF_INET;
Herbert Xua2bd40a2009-01-12 00:06:02 +0000689 else if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
690 IS_PPPOE_IPV6(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 pf = PF_INET6;
Herbert Xua2bd40a2009-01-12 00:06:02 +0000692 else
693 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Herbert Xu3db05fe2007-10-15 00:53:15 -0700695 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 nf_bridge = skb->nf_bridge;
698 if (skb->pkt_type == PACKET_OTHERHOST) {
699 skb->pkt_type = PACKET_HOST;
700 nf_bridge->mask |= BRNF_PKT_TYPE;
701 }
702
703 /* The physdev module checks on this */
704 nf_bridge->mask |= BRNF_BRIDGED;
705 nf_bridge->physoutdev = skb->dev;
706
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800707 NF_HOOK(pf, NF_INET_FORWARD, skb, bridge_parent(in), parent,
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800708 br_nf_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 return NF_STOLEN;
711}
712
Herbert Xu3db05fe2007-10-15 00:53:15 -0700713static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800714 const struct net_device *in,
715 const struct net_device *out,
716 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct net_device **d = (struct net_device **)(skb->cb);
719
720#ifdef CONFIG_SYSCTL
721 if (!brnf_call_arptables)
722 return NF_ACCEPT;
723#endif
724
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800725 if (skb->protocol != htons(ETH_P_ARP)) {
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800726 if (!IS_VLAN_ARP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return NF_ACCEPT;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700728 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -0300731 if (arp_hdr(skb)->ar_pln != 4) {
Patrick McHardyfc385822007-05-03 03:36:16 -0700732 if (IS_VLAN_ARP(skb))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700733 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return NF_ACCEPT;
735 }
736 *d = (struct net_device *)in;
Jan Engelhardtfdc93142008-10-20 03:34:51 -0700737 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 (struct net_device *)out, br_nf_forward_finish);
739
740 return NF_STOLEN;
741}
742
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800743/* PF_BRIDGE/LOCAL_OUT ***********************************************
744 *
745 * This function sees both locally originated IP packets and forwarded
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 * IP packets (in both cases the destination device is a bridge
747 * device). It also sees bridged-and-DNAT'ed packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 *
749 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
750 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
751 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
752 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
753 * will be executed.
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800754 */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700755static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800756 const struct net_device *in,
757 const struct net_device *out,
758 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800760 struct net_device *realindev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 struct nf_bridge_info *nf_bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 if (!skb->nf_bridge)
764 return NF_ACCEPT;
765
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800766 /* Need exclusive nf_bridge_info since we might have multiple
767 * different physoutdevs. */
768 if (!nf_bridge_unshare(skb))
769 return NF_DROP;
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 nf_bridge = skb->nf_bridge;
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800772 if (!(nf_bridge->mask & BRNF_BRIDGED_DNAT))
773 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 /* Bridged, take PF_BRIDGE/FORWARD.
776 * (see big note in front of br_nf_pre_routing_finish) */
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800777 nf_bridge->physoutdev = skb->dev;
778 realindev = nf_bridge->physindev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800780 if (nf_bridge->mask & BRNF_PKT_TYPE) {
781 skb->pkt_type = PACKET_OTHERHOST;
782 nf_bridge->mask ^= BRNF_PKT_TYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Patrick McHardyfc385822007-05-03 03:36:16 -0700784 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800786 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev, skb->dev,
787 br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return NF_STOLEN;
789}
790
hummerbliss@gmail.comc197fac2009-04-20 17:12:35 +0200791#if defined(CONFIG_NF_CONNTRACK_IPV4) || defined(CONFIG_NF_CONNTRACK_IPV4_MODULE)
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700792static int br_nf_dev_queue_xmit(struct sk_buff *skb)
793{
hummerbliss@gmail.comc197fac2009-04-20 17:12:35 +0200794 if (skb->nfct != NULL &&
795 (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb)) &&
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700796 skb->len > skb->dev->mtu &&
Herbert Xu89114af2006-07-08 13:34:32 -0700797 !skb_is_gso(skb))
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700798 return ip_fragment(skb, br_dev_queue_push_xmit);
799 else
800 return br_dev_queue_push_xmit(skb);
801}
hummerbliss@gmail.comc197fac2009-04-20 17:12:35 +0200802#else
803static int br_nf_dev_queue_xmit(struct sk_buff *skb)
804{
805 return br_dev_queue_push_xmit(skb);
806}
807#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809/* PF_BRIDGE/POST_ROUTING ********************************************/
Herbert Xu3db05fe2007-10-15 00:53:15 -0700810static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800811 const struct net_device *in,
812 const struct net_device *out,
813 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700815 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 struct net_device *realoutdev = bridge_parent(skb->dev);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200817 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819#ifdef CONFIG_NETFILTER_DEBUG
820 /* Be very paranoid. This probably won't happen anymore, but let's
821 * keep the check just to be sure... */
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700822 if (skb_mac_header(skb) < skb->head ||
823 skb_mac_header(skb) + ETH_HLEN > skb->data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
Stephen Hemminger8394e9b2006-08-29 17:49:31 -0700825 "bad mac.raw pointer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 goto print_error;
827 }
828#endif
829
830 if (!nf_bridge)
831 return NF_ACCEPT;
832
Patrick McHardy81d9dda2007-11-13 02:58:44 -0800833 if (!(nf_bridge->mask & (BRNF_BRIDGED | BRNF_BRIDGED_DNAT)))
834 return NF_ACCEPT;
835
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800836 if (!realoutdev)
837 return NF_DROP;
838
Michael Milner516299d2007-04-12 22:14:23 -0700839 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
840 IS_PPPOE_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 pf = PF_INET;
Herbert Xua2bd40a2009-01-12 00:06:02 +0000842 else if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
843 IS_PPPOE_IPV6(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 pf = PF_INET6;
Herbert Xua2bd40a2009-01-12 00:06:02 +0000845 else
846 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848#ifdef CONFIG_NETFILTER_DEBUG
849 if (skb->dst == NULL) {
Stephen Hemminger8394e9b2006-08-29 17:49:31 -0700850 printk(KERN_INFO "br_netfilter post_routing: skb->dst == NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 goto print_error;
852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853#endif
854
855 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
856 * about the value of skb->pkt_type. */
857 if (skb->pkt_type == PACKET_OTHERHOST) {
858 skb->pkt_type = PACKET_HOST;
859 nf_bridge->mask |= BRNF_PKT_TYPE;
860 }
861
Patrick McHardyfc385822007-05-03 03:36:16 -0700862 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 nf_bridge_save_header(skb);
864
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800865 NF_HOOK(pf, NF_INET_POST_ROUTING, skb, NULL, realoutdev,
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700866 br_nf_dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 return NF_STOLEN;
869
870#ifdef CONFIG_NETFILTER_DEBUG
871print_error:
872 if (skb->dev != NULL) {
873 printk("[%s]", skb->dev->name);
Stephen Hemminger178a3252006-02-13 15:43:58 -0800874 if (realoutdev)
875 printk("[%s]", realoutdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700877 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb_mac_header(skb),
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800878 skb->data);
Stephen Hemminger8394e9b2006-08-29 17:49:31 -0700879 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return NF_ACCEPT;
881#endif
882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884/* IP/SABOTAGE *****************************************************/
885/* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
886 * for the second time. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700887static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800888 const struct net_device *in,
889 const struct net_device *out,
890 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700892 if (skb->nf_bridge &&
893 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return NF_STOP;
895 }
896
897 return NF_ACCEPT;
898}
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900/* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
901 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
902 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
903 * ip_refrag() can return NF_STOLEN. */
Patrick McHardy19994142007-12-05 01:23:00 -0800904static struct nf_hook_ops br_nf_ops[] __read_mostly = {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900905 { .hook = br_nf_pre_routing,
906 .owner = THIS_MODULE,
907 .pf = PF_BRIDGE,
908 .hooknum = NF_BR_PRE_ROUTING,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 .priority = NF_BR_PRI_BRNF, },
910 { .hook = br_nf_local_in,
911 .owner = THIS_MODULE,
912 .pf = PF_BRIDGE,
913 .hooknum = NF_BR_LOCAL_IN,
914 .priority = NF_BR_PRI_BRNF, },
915 { .hook = br_nf_forward_ip,
916 .owner = THIS_MODULE,
917 .pf = PF_BRIDGE,
918 .hooknum = NF_BR_FORWARD,
919 .priority = NF_BR_PRI_BRNF - 1, },
920 { .hook = br_nf_forward_arp,
921 .owner = THIS_MODULE,
922 .pf = PF_BRIDGE,
923 .hooknum = NF_BR_FORWARD,
924 .priority = NF_BR_PRI_BRNF, },
925 { .hook = br_nf_local_out,
926 .owner = THIS_MODULE,
927 .pf = PF_BRIDGE,
928 .hooknum = NF_BR_LOCAL_OUT,
929 .priority = NF_BR_PRI_FIRST, },
930 { .hook = br_nf_post_routing,
931 .owner = THIS_MODULE,
932 .pf = PF_BRIDGE,
933 .hooknum = NF_BR_POST_ROUTING,
934 .priority = NF_BR_PRI_LAST, },
935 { .hook = ip_sabotage_in,
936 .owner = THIS_MODULE,
937 .pf = PF_INET,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800938 .hooknum = NF_INET_PRE_ROUTING,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 .priority = NF_IP_PRI_FIRST, },
940 { .hook = ip_sabotage_in,
941 .owner = THIS_MODULE,
942 .pf = PF_INET6,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800943 .hooknum = NF_INET_PRE_ROUTING,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 .priority = NF_IP6_PRI_FIRST, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945};
946
947#ifdef CONFIG_SYSCTL
948static
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800949int brnf_sysctl_call_tables(ctl_table * ctl, int write, struct file *filp,
950 void __user * buffer, size_t * lenp, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 int ret;
953
954 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
955
956 if (write && *(int *)(ctl->data))
957 *(int *)(ctl->data) = 1;
958 return ret;
959}
960
961static ctl_table brnf_table[] = {
962 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 .procname = "bridge-nf-call-arptables",
964 .data = &brnf_call_arptables,
965 .maxlen = sizeof(int),
966 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800967 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 },
969 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 .procname = "bridge-nf-call-iptables",
971 .data = &brnf_call_iptables,
972 .maxlen = sizeof(int),
973 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800974 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 },
976 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 .procname = "bridge-nf-call-ip6tables",
978 .data = &brnf_call_ip6tables,
979 .maxlen = sizeof(int),
980 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800981 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 },
983 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 .procname = "bridge-nf-filter-vlan-tagged",
985 .data = &brnf_filter_vlan_tagged,
986 .maxlen = sizeof(int),
987 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800988 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 },
Michael Milner516299d2007-04-12 22:14:23 -0700990 {
Michael Milner516299d2007-04-12 22:14:23 -0700991 .procname = "bridge-nf-filter-pppoe-tagged",
992 .data = &brnf_filter_pppoe_tagged,
993 .maxlen = sizeof(int),
994 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800995 .proc_handler = brnf_sysctl_call_tables,
Michael Milner516299d2007-04-12 22:14:23 -0700996 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 { .ctl_name = 0 }
998};
999
Pavel Emelyanovb5ccd792008-01-09 00:30:05 -08001000static struct ctl_path brnf_path[] = {
1001 { .procname = "net", .ctl_name = CTL_NET, },
1002 { .procname = "bridge", .ctl_name = NET_BRIDGE, },
1003 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004};
1005#endif
1006
Patrick McHardy5eb87f42007-02-07 15:07:22 -08001007int __init br_netfilter_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Patrick McHardy5eb87f42007-02-07 15:07:22 -08001009 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Patrick McHardy5eb87f42007-02-07 15:07:22 -08001011 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1012 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014#ifdef CONFIG_SYSCTL
Pavel Emelyanovb5ccd792008-01-09 00:30:05 -08001015 brnf_sysctl_header = register_sysctl_paths(brnf_path, brnf_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (brnf_sysctl_header == NULL) {
Stephen Hemminger789bc3e2006-03-20 22:57:32 -08001017 printk(KERN_WARNING
1018 "br_netfilter: can't register to sysctl.\n");
Patrick McHardy5eb87f42007-02-07 15:07:22 -08001019 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1020 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 printk(KERN_NOTICE "Bridge firewalling registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return 0;
1025}
1026
1027void br_netfilter_fini(void)
1028{
Patrick McHardy5eb87f42007-02-07 15:07:22 -08001029 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030#ifdef CONFIG_SYSCTL
1031 unregister_sysctl_table(brnf_sysctl_header);
1032#endif
1033}