blob: 5ed00bd7009f55d1bdb5b8697889594442848e3f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Handle firewalling
3 * Linux ethernet bridge
4 *
5 * Authors:
Bart De Schuymer82379082010-04-13 11:40:41 +02006 * Lennert Buytenhek <buytenh@gnu.org>
7 * Bart De Schuymer <bdschuym@pandora.be>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 * Lennert dedicates this file to Kerstin Wurdinger.
15 */
16
17#include <linux/module.h>
18#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/ip.h>
21#include <linux/netdevice.h>
22#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020023#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/if_ether.h>
25#include <linux/if_vlan.h>
Michael Milner516299d2007-04-12 22:14:23 -070026#include <linux/if_pppox.h>
27#include <linux/ppp_defs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/netfilter_bridge.h>
29#include <linux/netfilter_ipv4.h>
30#include <linux/netfilter_ipv6.h>
31#include <linux/netfilter_arp.h>
32#include <linux/in_route.h>
Bart De Schuymerf216f082006-12-05 13:45:21 -080033#include <linux/inetdevice.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <net/ip.h>
36#include <net/ipv6.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020037#include <net/route.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "br_private.h"
41#ifdef CONFIG_SYSCTL
42#include <linux/sysctl.h>
43#endif
44
45#define skb_origaddr(skb) (((struct bridge_skb_cb *) \
46 (skb->nf_bridge->data))->daddr.ipv4)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070047#define store_orig_dstaddr(skb) (skb_origaddr(skb) = ip_hdr(skb)->daddr)
48#define dnat_took_place(skb) (skb_origaddr(skb) != ip_hdr(skb)->daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#ifdef CONFIG_SYSCTL
51static struct ctl_table_header *brnf_sysctl_header;
Brian Haley9c1ea142006-09-18 00:03:41 -070052static int brnf_call_iptables __read_mostly = 1;
53static int brnf_call_ip6tables __read_mostly = 1;
54static int brnf_call_arptables __read_mostly = 1;
Herbert Xu47e0e1c2009-01-12 00:06:03 +000055static int brnf_filter_vlan_tagged __read_mostly = 0;
56static int brnf_filter_pppoe_tagged __read_mostly = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#else
Patrick McHardy4df53d82010-07-02 09:32:57 +020058#define brnf_call_iptables 1
59#define brnf_call_ip6tables 1
60#define brnf_call_arptables 1
Herbert Xu47e0e1c2009-01-12 00:06:03 +000061#define brnf_filter_vlan_tagged 0
62#define brnf_filter_pppoe_tagged 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif
64
Dave Jonesb6f99a22007-03-22 12:27:49 -070065static inline __be16 vlan_proto(const struct sk_buff *skb)
Stephen Hemminger8b42ec32006-03-20 22:58:05 -080066{
67 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
68}
69
70#define IS_VLAN_IP(skb) \
71 (skb->protocol == htons(ETH_P_8021Q) && \
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090072 vlan_proto(skb) == htons(ETH_P_IP) && \
Stephen Hemminger8b42ec32006-03-20 22:58:05 -080073 brnf_filter_vlan_tagged)
74
75#define IS_VLAN_IPV6(skb) \
76 (skb->protocol == htons(ETH_P_8021Q) && \
77 vlan_proto(skb) == htons(ETH_P_IPV6) &&\
78 brnf_filter_vlan_tagged)
79
80#define IS_VLAN_ARP(skb) \
81 (skb->protocol == htons(ETH_P_8021Q) && \
82 vlan_proto(skb) == htons(ETH_P_ARP) && \
83 brnf_filter_vlan_tagged)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Michael Milner516299d2007-04-12 22:14:23 -070085static inline __be16 pppoe_proto(const struct sk_buff *skb)
86{
87 return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
88 sizeof(struct pppoe_hdr)));
89}
90
91#define IS_PPPOE_IP(skb) \
92 (skb->protocol == htons(ETH_P_PPP_SES) && \
93 pppoe_proto(skb) == htons(PPP_IP) && \
94 brnf_filter_pppoe_tagged)
95
96#define IS_PPPOE_IPV6(skb) \
97 (skb->protocol == htons(ETH_P_PPP_SES) && \
98 pppoe_proto(skb) == htons(PPP_IPV6) && \
99 brnf_filter_pppoe_tagged)
100
Herbert Xu631339f2008-11-24 16:06:50 -0800101static void fake_update_pmtu(struct dst_entry *dst, u32 mtu)
102{
103}
104
105static struct dst_ops fake_dst_ops = {
106 .family = AF_INET,
Harvey Harrison09640e62009-02-01 00:45:17 -0800107 .protocol = cpu_to_be16(ETH_P_IP),
Herbert Xu631339f2008-11-24 16:06:50 -0800108 .update_pmtu = fake_update_pmtu,
Herbert Xu631339f2008-11-24 16:06:50 -0800109 .entries = ATOMIC_INIT(0),
110};
111
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700112/*
113 * Initialize bogus route table used to keep netfilter happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * Currently, we fill in the PMTU entry because netfilter
115 * refragmentation needs it, and the rt_flags entry because
116 * ipt_REJECT needs it. Future netfilter modules might
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700117 * require us to fill additional fields.
118 */
119void br_netfilter_rtable_init(struct net_bridge *br)
120{
121 struct rtable *rt = &br->fake_rtable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Changli Gaod8d1f302010-06-10 23:31:35 -0700123 atomic_set(&rt->dst.__refcnt, 1);
124 rt->dst.dev = br->dev;
125 rt->dst.path = &rt->dst;
126 rt->dst.metrics[RTAX_MTU - 1] = 1500;
127 rt->dst.flags = DST_NOXFRM;
128 rt->dst.ops = &fake_dst_ops;
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700129}
130
131static inline struct rtable *bridge_parent_rtable(const struct net_device *dev)
132{
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000133 if (!br_port_exists(dev))
134 return NULL;
135 return &br_port_get_rcu(dev)->br->fake_rtable;
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700136}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800138static inline struct net_device *bridge_parent(const struct net_device *dev)
139{
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000140 if (!br_port_exists(dev))
141 return NULL;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800142
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000143 return br_port_get_rcu(dev)->br->dev;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800144}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800146static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
147{
148 skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
149 if (likely(skb->nf_bridge))
150 atomic_set(&(skb->nf_bridge->use), 1);
151
152 return skb->nf_bridge;
153}
154
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800155static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
156{
157 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
158
159 if (atomic_read(&nf_bridge->use) > 1) {
160 struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
161
162 if (tmp) {
163 memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
164 atomic_set(&tmp->use, 1);
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800165 }
Changli Gao4c3a76a2010-08-22 19:03:26 +0000166 nf_bridge_put(nf_bridge);
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800167 nf_bridge = tmp;
168 }
169 return nf_bridge;
170}
171
Patrick McHardyfc385822007-05-03 03:36:16 -0700172static inline void nf_bridge_push_encap_header(struct sk_buff *skb)
173{
174 unsigned int len = nf_bridge_encap_header_len(skb);
175
176 skb_push(skb, len);
177 skb->network_header -= len;
178}
179
180static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
181{
182 unsigned int len = nf_bridge_encap_header_len(skb);
183
184 skb_pull(skb, len);
185 skb->network_header += len;
186}
187
188static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
189{
190 unsigned int len = nf_bridge_encap_header_len(skb);
191
192 skb_pull_rcsum(skb, len);
193 skb->network_header += len;
194}
195
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800196static inline void nf_bridge_save_header(struct sk_buff *skb)
197{
Patrick McHardyfc385822007-05-03 03:36:16 -0700198 int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800199
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300200 skb_copy_from_linear_data_offset(skb, -header_size,
201 skb->nf_bridge->data, header_size);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800202}
203
Bart De Schuymere179e632010-04-15 12:26:39 +0200204static inline void nf_bridge_update_protocol(struct sk_buff *skb)
205{
206 if (skb->nf_bridge->mask & BRNF_8021Q)
207 skb->protocol = htons(ETH_P_8021Q);
208 else if (skb->nf_bridge->mask & BRNF_PPPoE)
209 skb->protocol = htons(ETH_P_PPP_SES);
210}
211
212/* Fill in the header for fragmented IP packets handled by
213 * the IPv4 connection tracking code.
Stephen Hemminger073176212006-08-29 17:48:17 -0700214 */
215int nf_bridge_copy_header(struct sk_buff *skb)
216{
217 int err;
Bart De Schuymere179e632010-04-15 12:26:39 +0200218 unsigned int header_size;
Stephen Hemminger073176212006-08-29 17:48:17 -0700219
Bart De Schuymere179e632010-04-15 12:26:39 +0200220 nf_bridge_update_protocol(skb);
221 header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
Herbert Xud9cc2042007-09-16 16:21:16 -0700222 err = skb_cow_head(skb, header_size);
Stephen Hemminger073176212006-08-29 17:48:17 -0700223 if (err)
224 return err;
225
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300226 skb_copy_to_linear_data_offset(skb, -header_size,
227 skb->nf_bridge->data, header_size);
Patrick McHardyfc385822007-05-03 03:36:16 -0700228 __skb_push(skb, nf_bridge_encap_header_len(skb));
Stephen Hemminger073176212006-08-29 17:48:17 -0700229 return 0;
230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/* PF_BRIDGE/PRE_ROUTING *********************************************/
233/* Undo the changes made for ip6tables PREROUTING and continue the
234 * bridge PRE_ROUTING hook. */
235static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
236{
237 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
Eric Dumazet511c3f92009-06-02 05:14:27 +0000238 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (nf_bridge->mask & BRNF_PKT_TYPE) {
241 skb->pkt_type = PACKET_OTHERHOST;
242 nf_bridge->mask ^= BRNF_PKT_TYPE;
243 }
244 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
245
Eric Dumazet511c3f92009-06-02 05:14:27 +0000246 rt = bridge_parent_rtable(nf_bridge->physindev);
247 if (!rt) {
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700248 kfree_skb(skb);
249 return 0;
250 }
Patrick McHardyf9181f42010-06-15 17:31:06 +0200251 skb_dst_set_noref(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 skb->dev = nf_bridge->physindev;
Bart De Schuymere179e632010-04-15 12:26:39 +0200254 nf_bridge_update_protocol(skb);
Patrick McHardyfc385822007-05-03 03:36:16 -0700255 nf_bridge_push_encap_header(skb);
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100256 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 br_handle_frame_finish, 1);
258
259 return 0;
260}
261
Bart De Schuymere179e632010-04-15 12:26:39 +0200262/* Obtain the correct destination MAC address, while preserving the original
263 * source MAC address. If we already know this address, we just copy it. If we
264 * don't, we use the neighbour framework to find out. In both cases, we make
265 * sure that br_handle_frame_finish() is called afterwards.
266 */
267static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
268{
269 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
270 struct dst_entry *dst;
271
272 skb->dev = bridge_parent(skb->dev);
273 if (!skb->dev)
274 goto free_skb;
275 dst = skb_dst(skb);
276 if (dst->hh) {
277 neigh_hh_bridge(dst->hh, skb);
278 skb->dev = nf_bridge->physindev;
279 return br_handle_frame_finish(skb);
280 } else if (dst->neighbour) {
281 /* the neighbour function below overwrites the complete
282 * MAC header, so we save the Ethernet source address and
283 * protocol number. */
284 skb_copy_from_linear_data_offset(skb, -(ETH_HLEN-ETH_ALEN), skb->nf_bridge->data, ETH_HLEN-ETH_ALEN);
285 /* tell br_dev_xmit to continue with forwarding */
286 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
287 return dst->neighbour->output(skb);
288 }
289free_skb:
290 kfree_skb(skb);
291 return 0;
292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/* This requires some explaining. If DNAT has taken place,
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200295 * we will need to fix up the destination Ethernet address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 *
297 * There are two cases to consider:
298 * 1. The packet was DNAT'ed to a device in the same bridge
299 * port group as it was received on. We can still bridge
300 * the packet.
301 * 2. The packet was DNAT'ed to a different device, either
302 * a non-bridged device or another bridge port group.
303 * The packet will need to be routed.
304 *
305 * The correct way of distinguishing between these two cases is to
306 * call ip_route_input() and to look at skb->dst->dev, which is
307 * changed to the destination device if ip_route_input() succeeds.
308 *
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200309 * Let's first consider the case that ip_route_input() succeeds:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 *
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200311 * If the output device equals the logical bridge device the packet
312 * came in on, we can consider this bridging. The corresponding MAC
313 * address will be obtained in br_nf_pre_routing_finish_bridge.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * Otherwise, the packet is considered to be routed and we just
315 * change the destination MAC address so that the packet will
Bart De Schuymerf216f082006-12-05 13:45:21 -0800316 * later be passed up to the IP stack to be routed. For a redirected
317 * packet, ip_route_input() will give back the localhost as output device,
318 * which differs from the bridge device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 *
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200320 * Let's now consider the case that ip_route_input() fails:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 *
Bart De Schuymerf216f082006-12-05 13:45:21 -0800322 * This can be because the destination address is martian, in which case
323 * the packet will be dropped.
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200324 * If IP forwarding is disabled, ip_route_input() will fail, while
325 * ip_route_output_key() can return success. The source
326 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * thinks we're handling a locally generated packet and won't care
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200328 * if IP forwarding is enabled. If the output device equals the logical bridge
329 * device, we proceed as if ip_route_input() succeeded. If it differs from the
330 * logical bridge port or if ip_route_output_key() fails we drop the packet.
331 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static int br_nf_pre_routing_finish(struct sk_buff *skb)
333{
334 struct net_device *dev = skb->dev;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700335 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
Eric Dumazet511c3f92009-06-02 05:14:27 +0000337 struct rtable *rt;
Bart De Schuymerf216f082006-12-05 13:45:21 -0800338 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (nf_bridge->mask & BRNF_PKT_TYPE) {
341 skb->pkt_type = PACKET_OTHERHOST;
342 nf_bridge->mask ^= BRNF_PKT_TYPE;
343 }
344 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (dnat_took_place(skb)) {
Bart De Schuymerf216f082006-12-05 13:45:21 -0800346 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800347 struct flowi fl = {
348 .nl_u = {
349 .ip4_u = {
350 .daddr = iph->daddr,
351 .saddr = 0,
352 .tos = RT_TOS(iph->tos) },
353 },
354 .proto = 0,
355 };
Eric Dumazetf3abc9b2009-08-24 19:35:38 +0200356 struct in_device *in_dev = __in_dev_get_rcu(dev);
Bart De Schuymerf216f082006-12-05 13:45:21 -0800357
358 /* If err equals -EHOSTUNREACH the error is due to a
359 * martian destination or due to the fact that
360 * forwarding is disabled. For most martian packets,
361 * ip_route_output_key() will fail. It won't fail for 2 types of
362 * martian destinations: loopback destinations and destination
363 * 0.0.0.0. In both cases the packet will be dropped because the
364 * destination is the loopback device and not the bridge. */
365 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
366 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Alexey Dobriyan249b6202008-11-04 14:31:29 +0100368 if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
Bart De Schuymer1c011be2005-09-14 20:55:16 -0700369 /* - Bridged-and-DNAT'ed traffic doesn't
Bart De Schuymerf216f082006-12-05 13:45:21 -0800370 * require ip_forwarding. */
371 if (((struct dst_entry *)rt)->dev == dev) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000372 skb_dst_set(skb, (struct dst_entry *)rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 goto bridged_dnat;
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 dst_release((struct dst_entry *)rt);
376 }
Bart De Schuymerf216f082006-12-05 13:45:21 -0800377free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 kfree_skb(skb);
379 return 0;
380 } else {
Eric Dumazetadf30902009-06-02 05:19:30 +0000381 if (skb_dst(skb)->dev == dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382bridged_dnat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 skb->dev = nf_bridge->physindev;
Bart De Schuymere179e632010-04-15 12:26:39 +0200384 nf_bridge_update_protocol(skb);
Patrick McHardyfc385822007-05-03 03:36:16 -0700385 nf_bridge_push_encap_header(skb);
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100386 NF_HOOK_THRESH(NFPROTO_BRIDGE,
387 NF_BR_PRE_ROUTING,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 skb, skb->dev, NULL,
389 br_nf_pre_routing_finish_bridge,
390 1);
391 return 0;
392 }
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800393 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 skb->pkt_type = PACKET_HOST;
395 }
396 } else {
Eric Dumazet511c3f92009-06-02 05:14:27 +0000397 rt = bridge_parent_rtable(nf_bridge->physindev);
398 if (!rt) {
Simon Wunderlich4adf0af2008-07-30 16:27:55 -0700399 kfree_skb(skb);
400 return 0;
401 }
Patrick McHardyf9181f42010-06-15 17:31:06 +0200402 skb_dst_set_noref(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404
405 skb->dev = nf_bridge->physindev;
Bart De Schuymere179e632010-04-15 12:26:39 +0200406 nf_bridge_update_protocol(skb);
Patrick McHardyfc385822007-05-03 03:36:16 -0700407 nf_bridge_push_encap_header(skb);
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100408 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 br_handle_frame_finish, 1);
410
411 return 0;
412}
413
414/* Some common code for IPv4/IPv6 */
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800415static struct net_device *setup_pre_routing(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
418
419 if (skb->pkt_type == PACKET_OTHERHOST) {
420 skb->pkt_type = PACKET_HOST;
421 nf_bridge->mask |= BRNF_PKT_TYPE;
422 }
423
424 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
425 nf_bridge->physindev = skb->dev;
426 skb->dev = bridge_parent(skb->dev);
Bart De Schuymere179e632010-04-15 12:26:39 +0200427 if (skb->protocol == htons(ETH_P_8021Q))
428 nf_bridge->mask |= BRNF_8021Q;
429 else if (skb->protocol == htons(ETH_P_PPP_SES))
430 nf_bridge->mask |= BRNF_PPPoE;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800431
432 return skb->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435/* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
436static int check_hbh_len(struct sk_buff *skb)
437{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700438 unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 u32 pkt_len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700440 const unsigned char *nh = skb_network_header(skb);
441 int off = raw - nh;
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800442 int len = (raw[1] + 1) << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if ((raw + len) - skb->data > skb_headlen(skb))
445 goto bad;
446
447 off += 2;
448 len -= 2;
449
450 while (len > 0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700451 int optlen = nh[off + 1] + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700453 switch (nh[off]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 case IPV6_TLV_PAD0:
455 optlen = 1;
456 break;
457
458 case IPV6_TLV_PADN:
459 break;
460
461 case IPV6_TLV_JUMBO:
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700462 if (nh[off + 1] != 4 || (off & 3) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 goto bad;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700464 pkt_len = ntohl(*(__be32 *) (nh + off + 2));
Bart De Schuymerb0366482005-12-19 14:00:08 -0800465 if (pkt_len <= IPV6_MAXPLEN ||
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700466 ipv6_hdr(skb)->payload_len)
Bart De Schuymerb0366482005-12-19 14:00:08 -0800467 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
469 goto bad;
Bart De Schuymerb0366482005-12-19 14:00:08 -0800470 if (pskb_trim_rcsum(skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800471 pkt_len + sizeof(struct ipv6hdr)))
Bart De Schuymerb0366482005-12-19 14:00:08 -0800472 goto bad;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700473 nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 default:
476 if (optlen > len)
477 goto bad;
478 break;
479 }
480 off += optlen;
481 len -= optlen;
482 }
483 if (len == 0)
484 return 0;
485bad:
486 return -1;
487
488}
489
490/* Replicate the checks that IPv6 does on packet reception and pass the packet
491 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
492static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800493 struct sk_buff *skb,
494 const struct net_device *in,
495 const struct net_device *out,
496 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct ipv6hdr *hdr;
499 u32 pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (skb->len < sizeof(struct ipv6hdr))
502 goto inhdr_error;
503
504 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
505 goto inhdr_error;
506
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700507 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (hdr->version != 6)
510 goto inhdr_error;
511
512 pkt_len = ntohs(hdr->payload_len);
513
514 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
515 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
516 goto inhdr_error;
Herbert Xub38dfee2006-06-09 16:13:01 -0700517 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
518 goto inhdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800521 goto inhdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800523 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800524 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800526 if (!setup_pre_routing(skb))
527 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Bart De Schuymere179e632010-04-15 12:26:39 +0200529 skb->protocol = htons(ETH_P_IPV6);
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100530 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 br_nf_pre_routing_finish_ipv6);
532
533 return NF_STOLEN;
534
535inhdr_error:
536 return NF_DROP;
537}
538
539/* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
540 * Replicate the checks that IPv4 does on packet reception.
541 * Set skb->dev to the bridge device (i.e. parent of the
542 * receiving device) to make netfilter happy, the REDIRECT
543 * target in particular. Save the original destination IP
544 * address to be able to detect DNAT afterwards. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700545static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff *skb,
Stephen Hemmingeree02b3a2006-01-06 13:13:29 -0800546 const struct net_device *in,
547 const struct net_device *out,
548 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Patrick McHardy4df53d82010-07-02 09:32:57 +0200550 struct net_bridge_port *p;
551 struct net_bridge *br;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 struct iphdr *iph;
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700553 __u32 len = nf_bridge_encap_header_len(skb);
554
Evgeniy Polyakove7c243c2007-08-24 23:36:29 -0700555 if (unlikely(!pskb_may_pull(skb, len)))
556 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
David S. Millere490c1de2010-07-02 22:42:06 -0700558 p = br_port_get_rcu(in);
Patrick McHardy4df53d82010-07-02 09:32:57 +0200559 if (p == NULL)
560 goto out;
561 br = p->br;
562
Michael Milner516299d2007-04-12 22:14:23 -0700563 if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
564 IS_PPPOE_IPV6(skb)) {
Patrick McHardy4df53d82010-07-02 09:32:57 +0200565 if (!brnf_call_ip6tables && !br->nf_call_ip6tables)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return NF_ACCEPT;
Patrick McHardy4df53d82010-07-02 09:32:57 +0200567
Patrick McHardyfc385822007-05-03 03:36:16 -0700568 nf_bridge_pull_encap_header_rcsum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
570 }
Patrick McHardy4df53d82010-07-02 09:32:57 +0200571
572 if (!brnf_call_iptables && !br->nf_call_iptables)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Michael Milner516299d2007-04-12 22:14:23 -0700575 if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb) &&
576 !IS_PPPOE_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return NF_ACCEPT;
578
Patrick McHardyfc385822007-05-03 03:36:16 -0700579 nf_bridge_pull_encap_header_rcsum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
582 goto inhdr_error;
583
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700584 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (iph->ihl < 5 || iph->version != 4)
586 goto inhdr_error;
587
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800588 if (!pskb_may_pull(skb, 4 * iph->ihl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 goto inhdr_error;
590
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700591 iph = ip_hdr(skb);
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800592 if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 goto inhdr_error;
594
595 len = ntohs(iph->tot_len);
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800596 if (skb->len < len || len < 4 * iph->ihl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 goto inhdr_error;
598
Herbert Xub38dfee2006-06-09 16:13:01 -0700599 pskb_trim_rcsum(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Herbert Xu17762062010-07-05 21:29:28 +0000601 /* BUG: Should really parse the IP options here. */
602 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
603
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800604 nf_bridge_put(skb->nf_bridge);
Stephen Hemmingerfdeabde2006-03-20 22:58:21 -0800605 if (!nf_bridge_alloc(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return NF_DROP;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800607 if (!setup_pre_routing(skb))
608 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 store_orig_dstaddr(skb);
Bart De Schuymere179e632010-04-15 12:26:39 +0200610 skb->protocol = htons(ETH_P_IP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100612 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 br_nf_pre_routing_finish);
614
615 return NF_STOLEN;
616
617inhdr_error:
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800618// IP_INC_STATS_BH(IpInHdrErrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619out:
620 return NF_DROP;
621}
622
623
624/* PF_BRIDGE/LOCAL_IN ************************************************/
625/* The packet is locally destined, which requires a real
626 * dst_entry, so detach the fake one. On the way up, the
627 * packet would pass through PRE_ROUTING again (which already
628 * took place when the packet entered the bridge), but we
629 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
630 * prevent this from happening. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700631static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800632 const struct net_device *in,
633 const struct net_device *out,
634 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Eric Dumazet511c3f92009-06-02 05:14:27 +0000636 struct rtable *rt = skb_rtable(skb);
637
Eric Dumazetadf30902009-06-02 05:19:30 +0000638 if (rt && rt == bridge_parent_rtable(in))
639 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 return NF_ACCEPT;
642}
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644/* PF_BRIDGE/FORWARD *************************************************/
645static int br_nf_forward_finish(struct sk_buff *skb)
646{
647 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
648 struct net_device *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800650 if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 in = nf_bridge->physindev;
652 if (nf_bridge->mask & BRNF_PKT_TYPE) {
653 skb->pkt_type = PACKET_OTHERHOST;
654 nf_bridge->mask ^= BRNF_PKT_TYPE;
655 }
Bart De Schuymere94c6742010-05-13 14:55:34 +0200656 nf_bridge_update_protocol(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 } else {
658 in = *((struct net_device **)(skb->cb));
659 }
Patrick McHardyfc385822007-05-03 03:36:16 -0700660 nf_bridge_push_encap_header(skb);
Bart De Schuymere179e632010-04-15 12:26:39 +0200661
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100662 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, skb, in,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800663 skb->dev, br_forward_finish, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return 0;
665}
666
667/* This is the 'purely bridged' case. For IP, we pass the packet to
668 * netfilter with indev and outdev set to the bridge device,
669 * but we are still able to filter on the 'real' indev/outdev
670 * because of the physdev module. For ARP, indev and outdev are the
671 * bridge ports. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700672static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800673 const struct net_device *in,
674 const struct net_device *out,
675 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 struct nf_bridge_info *nf_bridge;
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800678 struct net_device *parent;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200679 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 if (!skb->nf_bridge)
682 return NF_ACCEPT;
683
Patrick McHardy2dc2f202008-01-20 06:25:48 -0800684 /* Need exclusive nf_bridge_info since we might have multiple
685 * different physoutdevs. */
686 if (!nf_bridge_unshare(skb))
687 return NF_DROP;
688
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800689 parent = bridge_parent(out);
690 if (!parent)
691 return NF_DROP;
692
Michael Milner516299d2007-04-12 22:14:23 -0700693 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
694 IS_PPPOE_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 pf = PF_INET;
Herbert Xua2bd40a2009-01-12 00:06:02 +0000696 else if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
697 IS_PPPOE_IPV6(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 pf = PF_INET6;
Herbert Xua2bd40a2009-01-12 00:06:02 +0000699 else
700 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Herbert Xu3db05fe2007-10-15 00:53:15 -0700702 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 nf_bridge = skb->nf_bridge;
705 if (skb->pkt_type == PACKET_OTHERHOST) {
706 skb->pkt_type = PACKET_HOST;
707 nf_bridge->mask |= BRNF_PKT_TYPE;
708 }
709
710 /* The physdev module checks on this */
711 nf_bridge->mask |= BRNF_BRIDGED;
712 nf_bridge->physoutdev = skb->dev;
Bart De Schuymere179e632010-04-15 12:26:39 +0200713 if (pf == PF_INET)
714 skb->protocol = htons(ETH_P_IP);
715 else
716 skb->protocol = htons(ETH_P_IPV6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800718 NF_HOOK(pf, NF_INET_FORWARD, skb, bridge_parent(in), parent,
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800719 br_nf_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 return NF_STOLEN;
722}
723
Herbert Xu3db05fe2007-10-15 00:53:15 -0700724static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800725 const struct net_device *in,
726 const struct net_device *out,
727 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Patrick McHardy4df53d82010-07-02 09:32:57 +0200729 struct net_bridge_port *p;
730 struct net_bridge *br;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct net_device **d = (struct net_device **)(skb->cb);
732
David S. Millere490c1de2010-07-02 22:42:06 -0700733 p = br_port_get_rcu(out);
Patrick McHardy4df53d82010-07-02 09:32:57 +0200734 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return NF_ACCEPT;
Patrick McHardy4df53d82010-07-02 09:32:57 +0200736 br = p->br;
737
738 if (!brnf_call_arptables && !br->nf_call_arptables)
739 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Stephen Hemmingerf8a26022006-03-20 22:57:46 -0800741 if (skb->protocol != htons(ETH_P_ARP)) {
Stephen Hemminger8b42ec32006-03-20 22:58:05 -0800742 if (!IS_VLAN_ARP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return NF_ACCEPT;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700744 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -0300747 if (arp_hdr(skb)->ar_pln != 4) {
Patrick McHardyfc385822007-05-03 03:36:16 -0700748 if (IS_VLAN_ARP(skb))
Herbert Xu3db05fe2007-10-15 00:53:15 -0700749 nf_bridge_push_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return NF_ACCEPT;
751 }
752 *d = (struct net_device *)in;
Jan Engelhardtfdc93142008-10-20 03:34:51 -0700753 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 (struct net_device *)out, br_nf_forward_finish);
755
756 return NF_STOLEN;
757}
758
hummerbliss@gmail.comc197fac2009-04-20 17:12:35 +0200759#if defined(CONFIG_NF_CONNTRACK_IPV4) || defined(CONFIG_NF_CONNTRACK_IPV4_MODULE)
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700760static int br_nf_dev_queue_xmit(struct sk_buff *skb)
761{
Bart De Schuymere179e632010-04-15 12:26:39 +0200762 if (skb->nfct != NULL && skb->protocol == htons(ETH_P_IP) &&
Bart De Schuymer6c79bf02010-04-20 16:22:01 +0200763 skb->len + nf_bridge_mtu_reduction(skb) > skb->dev->mtu &&
Herbert Xu89114af2006-07-08 13:34:32 -0700764 !skb_is_gso(skb))
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700765 return ip_fragment(skb, br_dev_queue_push_xmit);
766 else
767 return br_dev_queue_push_xmit(skb);
768}
hummerbliss@gmail.comc197fac2009-04-20 17:12:35 +0200769#else
770static int br_nf_dev_queue_xmit(struct sk_buff *skb)
771{
772 return br_dev_queue_push_xmit(skb);
773}
774#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776/* PF_BRIDGE/POST_ROUTING ********************************************/
Herbert Xu3db05fe2007-10-15 00:53:15 -0700777static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800778 const struct net_device *in,
779 const struct net_device *out,
780 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700782 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 struct net_device *realoutdev = bridge_parent(skb->dev);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200784 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200786 if (!nf_bridge || !(nf_bridge->mask & BRNF_BRIDGED))
Patrick McHardy81d9dda2007-11-13 02:58:44 -0800787 return NF_ACCEPT;
788
Stephen Hemminger5dce9712006-02-09 17:09:38 -0800789 if (!realoutdev)
790 return NF_DROP;
791
Michael Milner516299d2007-04-12 22:14:23 -0700792 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
793 IS_PPPOE_IP(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 pf = PF_INET;
Herbert Xua2bd40a2009-01-12 00:06:02 +0000795 else if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
796 IS_PPPOE_IPV6(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 pf = PF_INET6;
Herbert Xua2bd40a2009-01-12 00:06:02 +0000798 else
799 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
802 * about the value of skb->pkt_type. */
803 if (skb->pkt_type == PACKET_OTHERHOST) {
804 skb->pkt_type = PACKET_HOST;
805 nf_bridge->mask |= BRNF_PKT_TYPE;
806 }
807
Patrick McHardyfc385822007-05-03 03:36:16 -0700808 nf_bridge_pull_encap_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 nf_bridge_save_header(skb);
Bart De Schuymere179e632010-04-15 12:26:39 +0200810 if (pf == PF_INET)
811 skb->protocol = htons(ETH_P_IP);
812 else
813 skb->protocol = htons(ETH_P_IPV6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800815 NF_HOOK(pf, NF_INET_POST_ROUTING, skb, NULL, realoutdev,
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700816 br_nf_dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821/* IP/SABOTAGE *****************************************************/
822/* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
823 * for the second time. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700824static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff *skb,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800825 const struct net_device *in,
826 const struct net_device *out,
827 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700829 if (skb->nf_bridge &&
830 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return NF_STOP;
832 }
833
834 return NF_ACCEPT;
835}
836
Bart De Schuymerea2d9b42010-04-15 12:14:51 +0200837/* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
838 * br_dev_queue_push_xmit is called afterwards */
Patrick McHardy19994142007-12-05 01:23:00 -0800839static struct nf_hook_ops br_nf_ops[] __read_mostly = {
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +0000840 {
841 .hook = br_nf_pre_routing,
842 .owner = THIS_MODULE,
843 .pf = PF_BRIDGE,
844 .hooknum = NF_BR_PRE_ROUTING,
845 .priority = NF_BR_PRI_BRNF,
846 },
847 {
848 .hook = br_nf_local_in,
849 .owner = THIS_MODULE,
850 .pf = PF_BRIDGE,
851 .hooknum = NF_BR_LOCAL_IN,
852 .priority = NF_BR_PRI_BRNF,
853 },
854 {
855 .hook = br_nf_forward_ip,
856 .owner = THIS_MODULE,
857 .pf = PF_BRIDGE,
858 .hooknum = NF_BR_FORWARD,
859 .priority = NF_BR_PRI_BRNF - 1,
860 },
861 {
862 .hook = br_nf_forward_arp,
863 .owner = THIS_MODULE,
864 .pf = PF_BRIDGE,
865 .hooknum = NF_BR_FORWARD,
866 .priority = NF_BR_PRI_BRNF,
867 },
868 {
Cyrill Gorcunov1490fd82009-07-03 20:11:57 +0000869 .hook = br_nf_post_routing,
870 .owner = THIS_MODULE,
871 .pf = PF_BRIDGE,
872 .hooknum = NF_BR_POST_ROUTING,
873 .priority = NF_BR_PRI_LAST,
874 },
875 {
876 .hook = ip_sabotage_in,
877 .owner = THIS_MODULE,
878 .pf = PF_INET,
879 .hooknum = NF_INET_PRE_ROUTING,
880 .priority = NF_IP_PRI_FIRST,
881 },
882 {
883 .hook = ip_sabotage_in,
884 .owner = THIS_MODULE,
885 .pf = PF_INET6,
886 .hooknum = NF_INET_PRE_ROUTING,
887 .priority = NF_IP6_PRI_FIRST,
888 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889};
890
891#ifdef CONFIG_SYSCTL
892static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700893int brnf_sysctl_call_tables(ctl_table * ctl, int write,
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800894 void __user * buffer, size_t * lenp, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 int ret;
897
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700898 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 if (write && *(int *)(ctl->data))
901 *(int *)(ctl->data) = 1;
902 return ret;
903}
904
905static ctl_table brnf_table[] = {
906 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 .procname = "bridge-nf-call-arptables",
908 .data = &brnf_call_arptables,
909 .maxlen = sizeof(int),
910 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800911 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 },
913 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 .procname = "bridge-nf-call-iptables",
915 .data = &brnf_call_iptables,
916 .maxlen = sizeof(int),
917 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800918 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 },
920 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 .procname = "bridge-nf-call-ip6tables",
922 .data = &brnf_call_ip6tables,
923 .maxlen = sizeof(int),
924 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800925 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 },
927 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 .procname = "bridge-nf-filter-vlan-tagged",
929 .data = &brnf_filter_vlan_tagged,
930 .maxlen = sizeof(int),
931 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800932 .proc_handler = brnf_sysctl_call_tables,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 },
Michael Milner516299d2007-04-12 22:14:23 -0700934 {
Michael Milner516299d2007-04-12 22:14:23 -0700935 .procname = "bridge-nf-filter-pppoe-tagged",
936 .data = &brnf_filter_pppoe_tagged,
937 .maxlen = sizeof(int),
938 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800939 .proc_handler = brnf_sysctl_call_tables,
Michael Milner516299d2007-04-12 22:14:23 -0700940 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800941 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942};
943
Pavel Emelyanovb5ccd792008-01-09 00:30:05 -0800944static struct ctl_path brnf_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800945 { .procname = "net", },
946 { .procname = "bridge", },
Pavel Emelyanovb5ccd792008-01-09 00:30:05 -0800947 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948};
949#endif
950
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800951int __init br_netfilter_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800953 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800955 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
956 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958#ifdef CONFIG_SYSCTL
Pavel Emelyanovb5ccd792008-01-09 00:30:05 -0800959 brnf_sysctl_header = register_sysctl_paths(brnf_path, brnf_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (brnf_sysctl_header == NULL) {
Stephen Hemminger789bc3e2006-03-20 22:57:32 -0800961 printk(KERN_WARNING
962 "br_netfilter: can't register to sysctl.\n");
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800963 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
964 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 printk(KERN_NOTICE "Bridge firewalling registered\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return 0;
969}
970
971void br_netfilter_fini(void)
972{
Patrick McHardy5eb87f42007-02-07 15:07:22 -0800973 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974#ifdef CONFIG_SYSCTL
975 unregister_sysctl_table(brnf_sysctl_header);
976#endif
977}