blob: bf9d6af9628aee6eb895eb94b1aca3673ccc0754 [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
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700104/*
105 * Initialize bogus route table used to keep netfilter happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * Currently, we fill in the PMTU entry because netfilter
107 * refragmentation needs it, and the rt_flags entry because
108 * ipt_REJECT needs it. Future netfilter modules might
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700109 * require us to fill additional fields.
110 */
111void br_netfilter_rtable_init(struct net_bridge *br)
112{
113 struct rtable *rt = &br->fake_rtable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700115 atomic_set(&rt->u.dst.__refcnt, 1);
Rami Rosenad619802008-08-05 01:21:22 -0700116 rt->u.dst.dev = br->dev;
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700117 rt->u.dst.path = &rt->u.dst;
118 rt->u.dst.metrics[RTAX_MTU - 1] = 1500;
119 rt->u.dst.flags = DST_NOXFRM;
120}
121
122static inline struct rtable *bridge_parent_rtable(const struct net_device *dev)
123{
124 struct net_bridge_port *port = rcu_dereference(dev->br_port);
125
126 return port ? &port->br->fake_rtable : NULL;
127}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
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 McHardy2dc2f202008-01-20 06:25:48 -0800145static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
146{
147 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
148
149 if (atomic_read(&nf_bridge->use) > 1) {
150 struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
151
152 if (tmp) {
153 memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
154 atomic_set(&tmp->use, 1);
155 nf_bridge_put(nf_bridge);
156 }
157 nf_bridge = tmp;
158 }
159 return nf_bridge;
160}
161
Patrick McHardyfc385822007-05-03 03:36:16 -0700162static inline void nf_bridge_push_encap_header(struct sk_buff *skb)
163{
164 unsigned int len = nf_bridge_encap_header_len(skb);
165
166 skb_push(skb, len);
167 skb->network_header -= len;
168}
169
170static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
171{
172 unsigned int len = nf_bridge_encap_header_len(skb);
173
174 skb_pull(skb, len);
175 skb->network_header += len;
176}
177
178static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
179{
180 unsigned int len = nf_bridge_encap_header_len(skb);
181
182 skb_pull_rcsum(skb, len);
183 skb->network_header += len;
184}
185
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800186static inline void nf_bridge_save_header(struct sk_buff *skb)
187{
Patrick McHardyfc385822007-05-03 03:36:16 -0700188 int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800189
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300190 skb_copy_from_linear_data_offset(skb, -header_size,
191 skb->nf_bridge->data, header_size);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800192}
193
Stephen Hemminger073176212006-08-29 17:48:17 -0700194/*
195 * When forwarding bridge frames, we save a copy of the original
196 * header before processing.
197 */
198int nf_bridge_copy_header(struct sk_buff *skb)
199{
200 int err;
Patrick McHardyfc385822007-05-03 03:36:16 -0700201 int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
Stephen Hemminger073176212006-08-29 17:48:17 -0700202
Herbert Xud9cc2042007-09-16 16:21:16 -0700203 err = skb_cow_head(skb, header_size);
Stephen Hemminger073176212006-08-29 17:48:17 -0700204 if (err)
205 return err;
206
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300207 skb_copy_to_linear_data_offset(skb, -header_size,
208 skb->nf_bridge->data, header_size);
Patrick McHardyfc385822007-05-03 03:36:16 -0700209 __skb_push(skb, nf_bridge_encap_header_len(skb));
Stephen Hemminger073176212006-08-29 17:48:17 -0700210 return 0;
211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/* PF_BRIDGE/PRE_ROUTING *********************************************/
214/* Undo the changes made for ip6tables PREROUTING and continue the
215 * bridge PRE_ROUTING hook. */
216static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
217{
218 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (nf_bridge->mask & BRNF_PKT_TYPE) {
221 skb->pkt_type = PACKET_OTHERHOST;
222 nf_bridge->mask ^= BRNF_PKT_TYPE;
223 }
224 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
225
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700226 skb->rtable = bridge_parent_rtable(nf_bridge->physindev);
227 if (!skb->rtable) {
228 kfree_skb(skb);
229 return 0;
230 }
231 dst_hold(&skb->rtable->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 skb->dev = nf_bridge->physindev;
Patrick McHardyfc385822007-05-03 03:36:16 -0700234 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
236 br_handle_frame_finish, 1);
237
238 return 0;
239}
240
241static void __br_dnat_complain(void)
242{
243 static unsigned long last_complaint;
244
245 if (jiffies - last_complaint >= 5 * HZ) {
246 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800247 "forwarding to be enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 last_complaint = jiffies;
249 }
250}
251
252/* This requires some explaining. If DNAT has taken place,
253 * we will need to fix up the destination Ethernet address,
254 * and this is a tricky process.
255 *
256 * There are two cases to consider:
257 * 1. The packet was DNAT'ed to a device in the same bridge
258 * port group as it was received on. We can still bridge
259 * the packet.
260 * 2. The packet was DNAT'ed to a different device, either
261 * a non-bridged device or another bridge port group.
262 * The packet will need to be routed.
263 *
264 * The correct way of distinguishing between these two cases is to
265 * call ip_route_input() and to look at skb->dst->dev, which is
266 * changed to the destination device if ip_route_input() succeeds.
267 *
268 * Let us first consider the case that ip_route_input() succeeds:
269 *
270 * If skb->dst->dev equals the logical bridge device the packet
Patrick McHardy2948d2e2008-01-11 18:02:18 -0800271 * came in on, we can consider this bridging. The packet is passed
272 * through the neighbour output function to build a new destination
273 * MAC address, which will make the packet enter br_nf_local_out()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * not much later. In that function it is assured that the iptables
275 * FORWARD chain is traversed for the packet.
276 *
277 * Otherwise, the packet is considered to be routed and we just
278 * change the destination MAC address so that the packet will
Bart De Schuymerf216f082006-12-05 13:45:21 -0800279 * later be passed up to the IP stack to be routed. For a redirected
280 * packet, ip_route_input() will give back the localhost as output device,
281 * which differs from the bridge device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *
283 * Let us now consider the case that ip_route_input() fails:
284 *
Bart De Schuymerf216f082006-12-05 13:45:21 -0800285 * This can be because the destination address is martian, in which case
286 * the packet will be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
288 * will fail, while __ip_route_output_key() will return success. The source
289 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
290 * thinks we're handling a locally generated packet and won't care
291 * if IP forwarding is allowed. We send a warning message to the users's
292 * log telling her to put IP forwarding on.
293 *
294 * ip_route_input() will also fail if there is no route available.
295 * In that case we just drop the packet.
296 *
297 * --Lennert, 20020411
298 * --Bart, 20020416 (updated)
Bart De Schuymerf216f082006-12-05 13:45:21 -0800299 * --Bart, 20021007 (updated)
300 * --Bart, 20062711 (updated) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
302{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (skb->pkt_type == PACKET_OTHERHOST) {
304 skb->pkt_type = PACKET_HOST;
305 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
306 }
307 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
308
309 skb->dev = bridge_parent(skb->dev);
Patrick McHardy2948d2e2008-01-11 18:02:18 -0800310 if (skb->dev) {
311 struct dst_entry *dst = skb->dst;
312
Patrick McHardyfc385822007-05-03 03:36:16 -0700313 nf_bridge_pull_encap_header(skb);
Patrick McHardy2948d2e2008-01-11 18:02:18 -0800314
315 if (dst->hh)
316 return neigh_hh_output(dst->hh, skb);
317 else if (dst->neighbour)
318 return dst->neighbour->output(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
Patrick McHardy2948d2e2008-01-11 18:02:18 -0800320 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return 0;
322}
323
324static int br_nf_pre_routing_finish(struct sk_buff *skb)
325{
326 struct net_device *dev = skb->dev;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700327 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
Bart De Schuymerf216f082006-12-05 13:45:21 -0800329 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (nf_bridge->mask & BRNF_PKT_TYPE) {
332 skb->pkt_type = PACKET_OTHERHOST;
333 nf_bridge->mask ^= BRNF_PKT_TYPE;
334 }
335 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (dnat_took_place(skb)) {
Bart De Schuymerf216f082006-12-05 13:45:21 -0800337 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct rtable *rt;
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800339 struct flowi fl = {
340 .nl_u = {
341 .ip4_u = {
342 .daddr = iph->daddr,
343 .saddr = 0,
344 .tos = RT_TOS(iph->tos) },
345 },
346 .proto = 0,
347 };
Bart De Schuymerf216f082006-12-05 13:45:21 -0800348 struct in_device *in_dev = in_dev_get(dev);
349
350 /* If err equals -EHOSTUNREACH the error is due to a
351 * martian destination or due to the fact that
352 * forwarding is disabled. For most martian packets,
353 * ip_route_output_key() will fail. It won't fail for 2 types of
354 * martian destinations: loopback destinations and destination
355 * 0.0.0.0. In both cases the packet will be dropped because the
356 * destination is the loopback device and not the bridge. */
357 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
358 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Alexey Dobriyan249b6202008-11-04 14:31:29 +0100360 if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
Bart De Schuymer1c011be2005-09-14 20:55:16 -0700361 /* - Bridged-and-DNAT'ed traffic doesn't
Bart De Schuymerf216f082006-12-05 13:45:21 -0800362 * require ip_forwarding. */
363 if (((struct dst_entry *)rt)->dev == dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 skb->dst = (struct dst_entry *)rt;
365 goto bridged_dnat;
366 }
Bart De Schuymerf216f082006-12-05 13:45:21 -0800367 /* we are sure that forwarding is disabled, so printing
368 * this message is no problem. Note that the packet could
369 * still have a martian destination address, in which case
370 * the packet could be dropped even if forwarding were enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 __br_dnat_complain();
372 dst_release((struct dst_entry *)rt);
373 }
Bart De Schuymerf216f082006-12-05 13:45:21 -0800374free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 kfree_skb(skb);
376 return 0;
377 } else {
378 if (skb->dst->dev == dev) {
379bridged_dnat:
380 /* Tell br_nf_local_out this is a
381 * bridged frame */
382 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
383 skb->dev = nf_bridge->physindev;
Patrick McHardyfc385822007-05-03 03:36:16 -0700384 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
386 skb, skb->dev, NULL,
387 br_nf_pre_routing_finish_bridge,
388 1);
389 return 0;
390 }
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800391 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 skb->pkt_type = PACKET_HOST;
393 }
394 } else {
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700395 skb->rtable = bridge_parent_rtable(nf_bridge->physindev);
396 if (!skb->rtable) {
397 kfree_skb(skb);
398 return 0;
399 }
400 dst_hold(&skb->rtable->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
403 skb->dev = nf_bridge->physindev;
Patrick McHardyfc385822007-05-03 03:36:16 -0700404 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
406 br_handle_frame_finish, 1);
407
408 return 0;
409}
410
411/* Some common code for IPv4/IPv6 */
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800412static struct net_device *setup_pre_routing(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
415
416 if (skb->pkt_type == PACKET_OTHERHOST) {
417 skb->pkt_type = PACKET_HOST;
418 nf_bridge->mask |= BRNF_PKT_TYPE;
419 }
420
421 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
422 nf_bridge->physindev = skb->dev;
423 skb->dev = bridge_parent(skb->dev);
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800424
425 return skb->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428/* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
429static int check_hbh_len(struct sk_buff *skb)
430{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700431 unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 u32 pkt_len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700433 const unsigned char *nh = skb_network_header(skb);
434 int off = raw - nh;
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800435 int len = (raw[1] + 1) << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 if ((raw + len) - skb->data > skb_headlen(skb))
438 goto bad;
439
440 off += 2;
441 len -= 2;
442
443 while (len > 0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700444 int optlen = nh[off + 1] + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700446 switch (nh[off]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 case IPV6_TLV_PAD0:
448 optlen = 1;
449 break;
450
451 case IPV6_TLV_PADN:
452 break;
453
454 case IPV6_TLV_JUMBO:
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700455 if (nh[off + 1] != 4 || (off & 3) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 goto bad;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700457 pkt_len = ntohl(*(__be32 *) (nh + off + 2));
Bart De Schuymerb0366482005-12-19 14:00:08 -0800458 if (pkt_len <= IPV6_MAXPLEN ||
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700459 ipv6_hdr(skb)->payload_len)
Bart De Schuymerb0366482005-12-19 14:00:08 -0800460 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
462 goto bad;
Bart De Schuymerb0366482005-12-19 14:00:08 -0800463 if (pskb_trim_rcsum(skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800464 pkt_len + sizeof(struct ipv6hdr)))
Bart De Schuymerb0366482005-12-19 14:00:08 -0800465 goto bad;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700466 nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 break;
468 default:
469 if (optlen > len)
470 goto bad;
471 break;
472 }
473 off += optlen;
474 len -= optlen;
475 }
476 if (len == 0)
477 return 0;
478bad:
479 return -1;
480
481}
482
483/* Replicate the checks that IPv6 does on packet reception and pass the packet
484 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
485static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800486 struct sk_buff *skb,
487 const struct net_device *in,
488 const struct net_device *out,
489 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct ipv6hdr *hdr;
492 u32 pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (skb->len < sizeof(struct ipv6hdr))
495 goto inhdr_error;
496
497 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
498 goto inhdr_error;
499
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700500 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (hdr->version != 6)
503 goto inhdr_error;
504
505 pkt_len = ntohs(hdr->payload_len);
506
507 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
508 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
509 goto inhdr_error;
Herbert Xub38dfee2006-06-09 16:13:01 -0700510 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
511 goto inhdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800514 goto inhdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800516 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800517 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800519 if (!setup_pre_routing(skb))
520 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800522 NF_HOOK(PF_INET6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 br_nf_pre_routing_finish_ipv6);
524
525 return NF_STOLEN;
526
527inhdr_error:
528 return NF_DROP;
529}
530
531/* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
532 * Replicate the checks that IPv4 does on packet reception.
533 * Set skb->dev to the bridge device (i.e. parent of the
534 * receiving device) to make netfilter happy, the REDIRECT
535 * target in particular. Save the original destination IP
536 * address to be able to detect DNAT afterwards. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700537static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff *skb,
Stephen Hemmingeree02b3a2006-01-06 13:13:29 -0800538 const struct net_device *in,
539 const struct net_device *out,
540 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct iphdr *iph;
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700543 __u32 len = nf_bridge_encap_header_len(skb);
544
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700545 if (unlikely(!pskb_may_pull(skb, len)))
546 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Michael Milner516299d2007-04-12 22:14:23 -0700548 if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
549 IS_PPPOE_IPV6(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550#ifdef CONFIG_SYSCTL
551 if (!brnf_call_ip6tables)
552 return NF_ACCEPT;
553#endif
Patrick McHardyfc385822007-05-03 03:36:16 -0700554 nf_bridge_pull_encap_header_rcsum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
556 }
557#ifdef CONFIG_SYSCTL
558 if (!brnf_call_iptables)
559 return NF_ACCEPT;
560#endif
561
Michael Milner516299d2007-04-12 22:14:23 -0700562 if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb) &&
563 !IS_PPPOE_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return NF_ACCEPT;
565
Patrick McHardyfc385822007-05-03 03:36:16 -0700566 nf_bridge_pull_encap_header_rcsum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
569 goto inhdr_error;
570
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700571 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (iph->ihl < 5 || iph->version != 4)
573 goto inhdr_error;
574
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800575 if (!pskb_may_pull(skb, 4 * iph->ihl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 goto inhdr_error;
577
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700578 iph = ip_hdr(skb);
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800579 if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 goto inhdr_error;
581
582 len = ntohs(iph->tot_len);
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800583 if (skb->len < len || len < 4 * iph->ihl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 goto inhdr_error;
585
Herbert Xub38dfee2006-06-09 16:13:01 -0700586 pskb_trim_rcsum(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800588 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800589 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800591 if (!setup_pre_routing(skb))
592 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 store_orig_dstaddr(skb);
594
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800595 NF_HOOK(PF_INET, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 br_nf_pre_routing_finish);
597
598 return NF_STOLEN;
599
600inhdr_error:
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800601// IP_INC_STATS_BH(IpInHdrErrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602out:
603 return NF_DROP;
604}
605
606
607/* PF_BRIDGE/LOCAL_IN ************************************************/
608/* The packet is locally destined, which requires a real
609 * dst_entry, so detach the fake one. On the way up, the
610 * packet would pass through PRE_ROUTING again (which already
611 * took place when the packet entered the bridge), but we
612 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
613 * prevent this from happening. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700614static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800615 const struct net_device *in,
616 const struct net_device *out,
617 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700619 if (skb->rtable && skb->rtable == bridge_parent_rtable(in)) {
620 dst_release(&skb->rtable->u.dst);
Eric Dumazetee6b9672008-03-05 18:30:47 -0800621 skb->rtable = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
624 return NF_ACCEPT;
625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627/* PF_BRIDGE/FORWARD *************************************************/
628static int br_nf_forward_finish(struct sk_buff *skb)
629{
630 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
631 struct net_device *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800633 if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 in = nf_bridge->physindev;
635 if (nf_bridge->mask & BRNF_PKT_TYPE) {
636 skb->pkt_type = PACKET_OTHERHOST;
637 nf_bridge->mask ^= BRNF_PKT_TYPE;
638 }
639 } else {
640 in = *((struct net_device **)(skb->cb));
641 }
Patrick McHardyfc385822007-05-03 03:36:16 -0700642 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800644 skb->dev, br_forward_finish, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return 0;
646}
647
648/* This is the 'purely bridged' case. For IP, we pass the packet to
649 * netfilter with indev and outdev set to the bridge device,
650 * but we are still able to filter on the 'real' indev/outdev
651 * because of the physdev module. For ARP, indev and outdev are the
652 * bridge ports. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700653static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800654 const struct net_device *in,
655 const struct net_device *out,
656 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 struct nf_bridge_info *nf_bridge;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800659 struct net_device *parent;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200660 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (!skb->nf_bridge)
663 return NF_ACCEPT;
664
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800665 /* Need exclusive nf_bridge_info since we might have multiple
666 * different physoutdevs. */
667 if (!nf_bridge_unshare(skb))
668 return NF_DROP;
669
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800670 parent = bridge_parent(out);
671 if (!parent)
672 return NF_DROP;
673
Michael Milner516299d2007-04-12 22:14:23 -0700674 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
675 IS_PPPOE_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 pf = PF_INET;
677 else
678 pf = PF_INET6;
679
Herbert Xu3db05fe2007-10-15 00:53:15 -0700680 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 nf_bridge = skb->nf_bridge;
683 if (skb->pkt_type == PACKET_OTHERHOST) {
684 skb->pkt_type = PACKET_HOST;
685 nf_bridge->mask |= BRNF_PKT_TYPE;
686 }
687
688 /* The physdev module checks on this */
689 nf_bridge->mask |= BRNF_BRIDGED;
690 nf_bridge->physoutdev = skb->dev;
691
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800692 NF_HOOK(pf, NF_INET_FORWARD, skb, bridge_parent(in), parent,
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800693 br_nf_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 return NF_STOLEN;
696}
697
Herbert Xu3db05fe2007-10-15 00:53:15 -0700698static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800699 const struct net_device *in,
700 const struct net_device *out,
701 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct net_device **d = (struct net_device **)(skb->cb);
704
705#ifdef CONFIG_SYSCTL
706 if (!brnf_call_arptables)
707 return NF_ACCEPT;
708#endif
709
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800710 if (skb->protocol != htons(ETH_P_ARP)) {
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800711 if (!IS_VLAN_ARP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return NF_ACCEPT;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700713 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -0300716 if (arp_hdr(skb)->ar_pln != 4) {
Patrick McHardyfc385822007-05-03 03:36:16 -0700717 if (IS_VLAN_ARP(skb))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700718 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return NF_ACCEPT;
720 }
721 *d = (struct net_device *)in;
Jan Engelhardtfdc93142008-10-20 03:34:51 -0700722 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 (struct net_device *)out, br_nf_forward_finish);
724
725 return NF_STOLEN;
726}
727
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800728/* PF_BRIDGE/LOCAL_OUT ***********************************************
729 *
730 * This function sees both locally originated IP packets and forwarded
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * IP packets (in both cases the destination device is a bridge
732 * device). It also sees bridged-and-DNAT'ed packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 *
734 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
735 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
736 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
737 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
738 * will be executed.
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800739 */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700740static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800741 const struct net_device *in,
742 const struct net_device *out,
743 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800745 struct net_device *realindev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 struct nf_bridge_info *nf_bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 if (!skb->nf_bridge)
749 return NF_ACCEPT;
750
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800751 /* Need exclusive nf_bridge_info since we might have multiple
752 * different physoutdevs. */
753 if (!nf_bridge_unshare(skb))
754 return NF_DROP;
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 nf_bridge = skb->nf_bridge;
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800757 if (!(nf_bridge->mask & BRNF_BRIDGED_DNAT))
758 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 /* Bridged, take PF_BRIDGE/FORWARD.
761 * (see big note in front of br_nf_pre_routing_finish) */
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800762 nf_bridge->physoutdev = skb->dev;
763 realindev = nf_bridge->physindev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800765 if (nf_bridge->mask & BRNF_PKT_TYPE) {
766 skb->pkt_type = PACKET_OTHERHOST;
767 nf_bridge->mask ^= BRNF_PKT_TYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
Patrick McHardyfc385822007-05-03 03:36:16 -0700769 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Patrick McHardy2bf540b2006-12-13 16:54:25 -0800771 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev, skb->dev,
772 br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return NF_STOLEN;
774}
775
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700776static int br_nf_dev_queue_xmit(struct sk_buff *skb)
777{
778 if (skb->protocol == htons(ETH_P_IP) &&
779 skb->len > skb->dev->mtu &&
Herbert Xu89114af2006-07-08 13:34:32 -0700780 !skb_is_gso(skb))
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700781 return ip_fragment(skb, br_dev_queue_push_xmit);
782 else
783 return br_dev_queue_push_xmit(skb);
784}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786/* PF_BRIDGE/POST_ROUTING ********************************************/
Herbert Xu3db05fe2007-10-15 00:53:15 -0700787static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800788 const struct net_device *in,
789 const struct net_device *out,
790 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700792 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct net_device *realoutdev = bridge_parent(skb->dev);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200794 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796#ifdef CONFIG_NETFILTER_DEBUG
797 /* Be very paranoid. This probably won't happen anymore, but let's
798 * keep the check just to be sure... */
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700799 if (skb_mac_header(skb) < skb->head ||
800 skb_mac_header(skb) + ETH_HLEN > skb->data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
Stephen Hemminger8394e9b2006-08-29 17:49:31 -0700802 "bad mac.raw pointer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 goto print_error;
804 }
805#endif
806
807 if (!nf_bridge)
808 return NF_ACCEPT;
809
Patrick McHardy81d9dda2007-11-13 02:58:44 -0800810 if (!(nf_bridge->mask & (BRNF_BRIDGED | BRNF_BRIDGED_DNAT)))
811 return NF_ACCEPT;
812
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800813 if (!realoutdev)
814 return NF_DROP;
815
Michael Milner516299d2007-04-12 22:14:23 -0700816 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
817 IS_PPPOE_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 pf = PF_INET;
819 else
820 pf = PF_INET6;
821
822#ifdef CONFIG_NETFILTER_DEBUG
823 if (skb->dst == NULL) {
Stephen Hemminger8394e9b2006-08-29 17:49:31 -0700824 printk(KERN_INFO "br_netfilter post_routing: skb->dst == NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 goto print_error;
826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827#endif
828
829 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
830 * about the value of skb->pkt_type. */
831 if (skb->pkt_type == PACKET_OTHERHOST) {
832 skb->pkt_type = PACKET_HOST;
833 nf_bridge->mask |= BRNF_PKT_TYPE;
834 }
835
Patrick McHardyfc385822007-05-03 03:36:16 -0700836 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 nf_bridge_save_header(skb);
838
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800839 NF_HOOK(pf, NF_INET_POST_ROUTING, skb, NULL, realoutdev,
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700840 br_nf_dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 return NF_STOLEN;
843
844#ifdef CONFIG_NETFILTER_DEBUG
845print_error:
846 if (skb->dev != NULL) {
847 printk("[%s]", skb->dev->name);
Stephen Hemminger178a3252006-02-13 15:43:58 -0800848 if (realoutdev)
849 printk("[%s]", realoutdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700851 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb_mac_header(skb),
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800852 skb->data);
Stephen Hemminger8394e9b2006-08-29 17:49:31 -0700853 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return NF_ACCEPT;
855#endif
856}
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858/* IP/SABOTAGE *****************************************************/
859/* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
860 * for the second time. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700861static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800862 const struct net_device *in,
863 const struct net_device *out,
864 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700866 if (skb->nf_bridge &&
867 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return NF_STOP;
869 }
870
871 return NF_ACCEPT;
872}
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
875 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
876 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
877 * ip_refrag() can return NF_STOLEN. */
Patrick McHardy19994142007-12-05 01:23:00 -0800878static struct nf_hook_ops br_nf_ops[] __read_mostly = {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900879 { .hook = br_nf_pre_routing,
880 .owner = THIS_MODULE,
881 .pf = PF_BRIDGE,
882 .hooknum = NF_BR_PRE_ROUTING,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 .priority = NF_BR_PRI_BRNF, },
884 { .hook = br_nf_local_in,
885 .owner = THIS_MODULE,
886 .pf = PF_BRIDGE,
887 .hooknum = NF_BR_LOCAL_IN,
888 .priority = NF_BR_PRI_BRNF, },
889 { .hook = br_nf_forward_ip,
890 .owner = THIS_MODULE,
891 .pf = PF_BRIDGE,
892 .hooknum = NF_BR_FORWARD,
893 .priority = NF_BR_PRI_BRNF - 1, },
894 { .hook = br_nf_forward_arp,
895 .owner = THIS_MODULE,
896 .pf = PF_BRIDGE,
897 .hooknum = NF_BR_FORWARD,
898 .priority = NF_BR_PRI_BRNF, },
899 { .hook = br_nf_local_out,
900 .owner = THIS_MODULE,
901 .pf = PF_BRIDGE,
902 .hooknum = NF_BR_LOCAL_OUT,
903 .priority = NF_BR_PRI_FIRST, },
904 { .hook = br_nf_post_routing,
905 .owner = THIS_MODULE,
906 .pf = PF_BRIDGE,
907 .hooknum = NF_BR_POST_ROUTING,
908 .priority = NF_BR_PRI_LAST, },
909 { .hook = ip_sabotage_in,
910 .owner = THIS_MODULE,
911 .pf = PF_INET,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800912 .hooknum = NF_INET_PRE_ROUTING,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 .priority = NF_IP_PRI_FIRST, },
914 { .hook = ip_sabotage_in,
915 .owner = THIS_MODULE,
916 .pf = PF_INET6,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800917 .hooknum = NF_INET_PRE_ROUTING,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 .priority = NF_IP6_PRI_FIRST, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919};
920
921#ifdef CONFIG_SYSCTL
922static
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800923int brnf_sysctl_call_tables(ctl_table * ctl, int write, struct file *filp,
924 void __user * buffer, size_t * lenp, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 int ret;
927
928 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
929
930 if (write && *(int *)(ctl->data))
931 *(int *)(ctl->data) = 1;
932 return ret;
933}
934
935static ctl_table brnf_table[] = {
936 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 .procname = "bridge-nf-call-arptables",
938 .data = &brnf_call_arptables,
939 .maxlen = sizeof(int),
940 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800941 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 },
943 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 .procname = "bridge-nf-call-iptables",
945 .data = &brnf_call_iptables,
946 .maxlen = sizeof(int),
947 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800948 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 },
950 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 .procname = "bridge-nf-call-ip6tables",
952 .data = &brnf_call_ip6tables,
953 .maxlen = sizeof(int),
954 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800955 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 },
957 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 .procname = "bridge-nf-filter-vlan-tagged",
959 .data = &brnf_filter_vlan_tagged,
960 .maxlen = sizeof(int),
961 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800962 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 },
Michael Milner516299d2007-04-12 22:14:23 -0700964 {
Michael Milner516299d2007-04-12 22:14:23 -0700965 .procname = "bridge-nf-filter-pppoe-tagged",
966 .data = &brnf_filter_pppoe_tagged,
967 .maxlen = sizeof(int),
968 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800969 .proc_handler = brnf_sysctl_call_tables,
Michael Milner516299d2007-04-12 22:14:23 -0700970 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 { .ctl_name = 0 }
972};
973
Pavel Emelyanovb5ccd792008-01-09 00:30:05 -0800974static struct ctl_path brnf_path[] = {
975 { .procname = "net", .ctl_name = CTL_NET, },
976 { .procname = "bridge", .ctl_name = NET_BRIDGE, },
977 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978};
979#endif
980
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800981int __init br_netfilter_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800983 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800985 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
986 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988#ifdef CONFIG_SYSCTL
Pavel Emelyanovb5ccd792008-01-09 00:30:05 -0800989 brnf_sysctl_header = register_sysctl_paths(brnf_path, brnf_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (brnf_sysctl_header == NULL) {
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800991 printk(KERN_WARNING
992 "br_netfilter: can't register to sysctl.\n");
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800993 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
994 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 printk(KERN_NOTICE "Bridge firewalling registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return 0;
999}
1000
1001void br_netfilter_fini(void)
1002{
Patrick McHardy5eb87f42007-02-07 15:07:22 -08001003 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004#ifdef CONFIG_SYSCTL
1005 unregister_sysctl_table(brnf_sysctl_header);
1006#endif
1007}