br_netfilter.c 26.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
/*
 *	Handle firewalling
 *	Linux ethernet bridge
 *
 *	Authors:
6
7
 *	Lennert Buytenhek		<buytenh@gnu.org>
 *	Bart De Schuymer		<bdschuym@pandora.be>
Linus Torvalds's avatar
Linus Torvalds committed
8
9
10
11
12
13
14
15
16
17
18
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 *
 *	Lennert dedicates this file to Kerstin Wurdinger.
 */

#include <linux/module.h>
#include <linux/kernel.h>
19
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
20
21
22
#include <linux/ip.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
23
#include <linux/if_arp.h>
Linus Torvalds's avatar
Linus Torvalds committed
24
25
#include <linux/if_ether.h>
#include <linux/if_vlan.h>
26
27
#include <linux/if_pppox.h>
#include <linux/ppp_defs.h>
Linus Torvalds's avatar
Linus Torvalds committed
28
29
30
31
32
#include <linux/netfilter_bridge.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter_ipv6.h>
#include <linux/netfilter_arp.h>
#include <linux/in_route.h>
33
#include <linux/inetdevice.h>
34

Linus Torvalds's avatar
Linus Torvalds committed
35
36
#include <net/ip.h>
#include <net/ipv6.h>
37
38
#include <net/route.h>

Linus Torvalds's avatar
Linus Torvalds committed
39
40
41
42
43
44
45
46
#include <asm/uaccess.h>
#include "br_private.h"
#ifdef CONFIG_SYSCTL
#include <linux/sysctl.h>
#endif

#define skb_origaddr(skb)	 (((struct bridge_skb_cb *) \
				 (skb->nf_bridge->data))->daddr.ipv4)
47
48
#define store_orig_dstaddr(skb)	 (skb_origaddr(skb) = ip_hdr(skb)->daddr)
#define dnat_took_place(skb)	 (skb_origaddr(skb) != ip_hdr(skb)->daddr)
Linus Torvalds's avatar
Linus Torvalds committed
49
50
51

#ifdef CONFIG_SYSCTL
static struct ctl_table_header *brnf_sysctl_header;
52
53
54
static int brnf_call_iptables __read_mostly = 1;
static int brnf_call_ip6tables __read_mostly = 1;
static int brnf_call_arptables __read_mostly = 1;
55
56
static int brnf_filter_vlan_tagged __read_mostly = 0;
static int brnf_filter_pppoe_tagged __read_mostly = 0;
Linus Torvalds's avatar
Linus Torvalds committed
57
#else
58
59
60
#define brnf_call_iptables 1
#define brnf_call_ip6tables 1
#define brnf_call_arptables 1
61
62
#define brnf_filter_vlan_tagged 0
#define brnf_filter_pppoe_tagged 0
Linus Torvalds's avatar
Linus Torvalds committed
63
64
#endif

Dave Jones's avatar
Dave Jones committed
65
static inline __be16 vlan_proto(const struct sk_buff *skb)
66
{
67
68
69
70
71
72
	if (vlan_tx_tag_present(skb))
		return skb->protocol;
	else if (skb->protocol == htons(ETH_P_8021Q))
		return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
	else
		return 0;
73
74
75
}

#define IS_VLAN_IP(skb) \
76
	(vlan_proto(skb) == htons(ETH_P_IP) && \
77
78
79
	 brnf_filter_vlan_tagged)

#define IS_VLAN_IPV6(skb) \
80
	(vlan_proto(skb) == htons(ETH_P_IPV6) && \
81
82
83
	 brnf_filter_vlan_tagged)

#define IS_VLAN_ARP(skb) \
84
	(vlan_proto(skb) == htons(ETH_P_ARP) &&	\
85
	 brnf_filter_vlan_tagged)
Linus Torvalds's avatar
Linus Torvalds committed
86

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
static inline __be16 pppoe_proto(const struct sk_buff *skb)
{
	return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
			    sizeof(struct pppoe_hdr)));
}

#define IS_PPPOE_IP(skb) \
	(skb->protocol == htons(ETH_P_PPP_SES) && \
	 pppoe_proto(skb) == htons(PPP_IP) && \
	 brnf_filter_pppoe_tagged)

#define IS_PPPOE_IPV6(skb) \
	(skb->protocol == htons(ETH_P_PPP_SES) && \
	 pppoe_proto(skb) == htons(PPP_IPV6) && \
	 brnf_filter_pppoe_tagged)

103
104
105
106
107
108
static void fake_update_pmtu(struct dst_entry *dst, u32 mtu)
{
}

static struct dst_ops fake_dst_ops = {
	.family =		AF_INET,
109
	.protocol =		cpu_to_be16(ETH_P_IP),
110
111
112
	.update_pmtu =		fake_update_pmtu,
};

113
114
/*
 * Initialize bogus route table used to keep netfilter happy.
Linus Torvalds's avatar
Linus Torvalds committed
115
116
117
 * Currently, we fill in the PMTU entry because netfilter
 * refragmentation needs it, and the rt_flags entry because
 * ipt_REJECT needs it.  Future netfilter modules might
118
119
120
121
122
123
 * require us to fill additional fields.
 */
void br_netfilter_rtable_init(struct net_bridge *br)
{
	struct rtable *rt = &br->fake_rtable;

124
125
126
	atomic_set(&rt->dst.__refcnt, 1);
	rt->dst.dev = br->dev;
	rt->dst.path = &rt->dst;
127
	dst_metric_set(&rt->dst, RTAX_MTU, 1500);
128
129
	rt->dst.flags	= DST_NOXFRM;
	rt->dst.ops = &fake_dst_ops;
130
131
132
133
}

static inline struct rtable *bridge_parent_rtable(const struct net_device *dev)
{
134
135
136
137
	struct net_bridge_port *port;

	port = br_port_get_rcu(dev);
	return port ? &port->br->fake_rtable : NULL;
138
}
Linus Torvalds's avatar
Linus Torvalds committed
139

140
141
static inline struct net_device *bridge_parent(const struct net_device *dev)
{
142
	struct net_bridge_port *port;
143

144
145
	port = br_port_get_rcu(dev);
	return port ? port->br->dev : NULL;
146
}
Linus Torvalds's avatar
Linus Torvalds committed
147

148
149
150
151
152
153
154
155
156
static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
{
	skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
	if (likely(skb->nf_bridge))
		atomic_set(&(skb->nf_bridge->use), 1);

	return skb->nf_bridge;
}

157
158
159
160
161
162
163
164
165
166
167
static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
{
	struct nf_bridge_info *nf_bridge = skb->nf_bridge;

	if (atomic_read(&nf_bridge->use) > 1) {
		struct nf_bridge_info *tmp = nf_bridge_alloc(skb);

		if (tmp) {
			memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
			atomic_set(&tmp->use, 1);
		}
168
		nf_bridge_put(nf_bridge);
169
170
171
172
173
		nf_bridge = tmp;
	}
	return nf_bridge;
}

174
175
176
177
178
179
180
181
182
static inline void nf_bridge_push_encap_header(struct sk_buff *skb)
{
	unsigned int len = nf_bridge_encap_header_len(skb);

	skb_push(skb, len);
	skb->network_header -= len;
}

static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
183
{
184
185
186
187
188
	unsigned int len = nf_bridge_encap_header_len(skb);

	skb_pull(skb, len);
	skb->network_header += len;
}
189

190
191
192
193
194
195
196
197
198
199
200
static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
{
	unsigned int len = nf_bridge_encap_header_len(skb);

	skb_pull_rcsum(skb, len);
	skb->network_header += len;
}

static inline void nf_bridge_save_header(struct sk_buff *skb)
{
	int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
201

202
203
	skb_copy_from_linear_data_offset(skb, -header_size,
					 skb->nf_bridge->data, header_size);
204
205
}

206
207
208
209
210
211
212
213
static inline void nf_bridge_update_protocol(struct sk_buff *skb)
{
	if (skb->nf_bridge->mask & BRNF_8021Q)
		skb->protocol = htons(ETH_P_8021Q);
	else if (skb->nf_bridge->mask & BRNF_PPPoE)
		skb->protocol = htons(ETH_P_PPP_SES);
}

214
215
216
217
218
/* When handing a packet over to the IP layer
 * check whether we have a skb that is in the
 * expected format
 */

219
static int br_parse_ip_options(struct sk_buff *skb)
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
{
	struct ip_options *opt;
	struct iphdr *iph;
	struct net_device *dev = skb->dev;
	u32 len;

	iph = ip_hdr(skb);
	opt = &(IPCB(skb)->opt);

	/* Basic sanity checks */
	if (iph->ihl < 5 || iph->version != 4)
		goto inhdr_error;

	if (!pskb_may_pull(skb, iph->ihl*4))
		goto inhdr_error;

	iph = ip_hdr(skb);
	if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
		goto inhdr_error;

	len = ntohs(iph->tot_len);
	if (skb->len < len) {
		IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INTRUNCATEDPKTS);
		goto drop;
	} else if (len < (iph->ihl*4))
		goto inhdr_error;

	if (pskb_trim_rcsum(skb, len)) {
		IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
		goto drop;
	}

	/* Zero out the CB buffer if no options present */
	if (iph->ihl == 5) {
		memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
		return 0;
	}

	opt->optlen = iph->ihl*4 - sizeof(struct iphdr);
	if (ip_options_compile(dev_net(dev), opt, skb))
		goto inhdr_error;

	/* Check correct handling of SRR option */
	if (unlikely(opt->srr)) {
		struct in_device *in_dev = __in_dev_get_rcu(dev);
		if (in_dev && !IN_DEV_SOURCE_ROUTE(in_dev))
			goto drop;

		if (ip_options_rcv_srr(skb))
			goto drop;
	}

	return 0;

inhdr_error:
	IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
drop:
	return -1;
}

280
281
/* Fill in the header for fragmented IP packets handled by
 * the IPv4 connection tracking code.
282
283
284
285
 */
int nf_bridge_copy_header(struct sk_buff *skb)
{
	int err;
286
	unsigned int header_size;
287

288
289
	nf_bridge_update_protocol(skb);
	header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
Herbert Xu's avatar
Herbert Xu committed
290
	err = skb_cow_head(skb, header_size);
291
292
293
	if (err)
		return err;

294
295
	skb_copy_to_linear_data_offset(skb, -header_size,
				       skb->nf_bridge->data, header_size);
296
	__skb_push(skb, nf_bridge_encap_header_len(skb));
297
298
299
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
300
301
302
303
304
305
/* PF_BRIDGE/PRE_ROUTING *********************************************/
/* Undo the changes made for ip6tables PREROUTING and continue the
 * bridge PRE_ROUTING hook. */
static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
{
	struct nf_bridge_info *nf_bridge = skb->nf_bridge;
Eric Dumazet's avatar
Eric Dumazet committed
306
	struct rtable *rt;
Linus Torvalds's avatar
Linus Torvalds committed
307
308
309
310
311
312
313

	if (nf_bridge->mask & BRNF_PKT_TYPE) {
		skb->pkt_type = PACKET_OTHERHOST;
		nf_bridge->mask ^= BRNF_PKT_TYPE;
	}
	nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;

Eric Dumazet's avatar
Eric Dumazet committed
314
315
	rt = bridge_parent_rtable(nf_bridge->physindev);
	if (!rt) {
316
317
318
		kfree_skb(skb);
		return 0;
	}
319
	skb_dst_set_noref(skb, &rt->dst);
Linus Torvalds's avatar
Linus Torvalds committed
320
321

	skb->dev = nf_bridge->physindev;
322
	nf_bridge_update_protocol(skb);
323
	nf_bridge_push_encap_header(skb);
324
	NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
Linus Torvalds's avatar
Linus Torvalds committed
325
326
327
328
329
		       br_handle_frame_finish, 1);

	return 0;
}

330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* Obtain the correct destination MAC address, while preserving the original
 * source MAC address. If we already know this address, we just copy it. If we
 * don't, we use the neighbour framework to find out. In both cases, we make
 * sure that br_handle_frame_finish() is called afterwards.
 */
static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
{
	struct nf_bridge_info *nf_bridge = skb->nf_bridge;
	struct dst_entry *dst;

	skb->dev = bridge_parent(skb->dev);
	if (!skb->dev)
		goto free_skb;
	dst = skb_dst(skb);
	if (dst->hh) {
		neigh_hh_bridge(dst->hh, skb);
		skb->dev = nf_bridge->physindev;
		return br_handle_frame_finish(skb);
	} else if (dst->neighbour) {
		/* the neighbour function below overwrites the complete
		 * MAC header, so we save the Ethernet source address and
		 * protocol number. */
		skb_copy_from_linear_data_offset(skb, -(ETH_HLEN-ETH_ALEN), skb->nf_bridge->data, ETH_HLEN-ETH_ALEN);
		/* tell br_dev_xmit to continue with forwarding */
		nf_bridge->mask |= BRNF_BRIDGED_DNAT;
		return dst->neighbour->output(skb);
	}
free_skb:
	kfree_skb(skb);
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
362
/* This requires some explaining. If DNAT has taken place,
363
 * we will need to fix up the destination Ethernet address.
Linus Torvalds's avatar
Linus Torvalds committed
364
365
366
367
368
369
370
371
372
373
374
375
376
 *
 * There are two cases to consider:
 * 1. The packet was DNAT'ed to a device in the same bridge
 *    port group as it was received on. We can still bridge
 *    the packet.
 * 2. The packet was DNAT'ed to a different device, either
 *    a non-bridged device or another bridge port group.
 *    The packet will need to be routed.
 *
 * The correct way of distinguishing between these two cases is to
 * call ip_route_input() and to look at skb->dst->dev, which is
 * changed to the destination device if ip_route_input() succeeds.
 *
377
 * Let's first consider the case that ip_route_input() succeeds:
Linus Torvalds's avatar
Linus Torvalds committed
378
 *
379
380
381
 * If the output device equals the logical bridge device the packet
 * came in on, we can consider this bridging. The corresponding MAC
 * address will be obtained in br_nf_pre_routing_finish_bridge.
Linus Torvalds's avatar
Linus Torvalds committed
382
383
 * Otherwise, the packet is considered to be routed and we just
 * change the destination MAC address so that the packet will
384
385
386
 * later be passed up to the IP stack to be routed. For a redirected
 * packet, ip_route_input() will give back the localhost as output device,
 * which differs from the bridge device.
Linus Torvalds's avatar
Linus Torvalds committed
387
 *
388
 * Let's now consider the case that ip_route_input() fails:
Linus Torvalds's avatar
Linus Torvalds committed
389
 *
390
391
 * This can be because the destination address is martian, in which case
 * the packet will be dropped.
392
393
394
 * If IP forwarding is disabled, ip_route_input() will fail, while
 * ip_route_output_key() can return success. The source
 * address for ip_route_output_key() is set to zero, so ip_route_output_key()
Linus Torvalds's avatar
Linus Torvalds committed
395
 * thinks we're handling a locally generated packet and won't care
396
397
398
399
 * if IP forwarding is enabled. If the output device equals the logical bridge
 * device, we proceed as if ip_route_input() succeeded. If it differs from the
 * logical bridge port or if ip_route_output_key() fails we drop the packet.
 */
Linus Torvalds's avatar
Linus Torvalds committed
400
401
402
static int br_nf_pre_routing_finish(struct sk_buff *skb)
{
	struct net_device *dev = skb->dev;
403
	struct iphdr *iph = ip_hdr(skb);
Linus Torvalds's avatar
Linus Torvalds committed
404
	struct nf_bridge_info *nf_bridge = skb->nf_bridge;
Eric Dumazet's avatar
Eric Dumazet committed
405
	struct rtable *rt;
406
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
407
408
409
410
411
412
413

	if (nf_bridge->mask & BRNF_PKT_TYPE) {
		skb->pkt_type = PACKET_OTHERHOST;
		nf_bridge->mask ^= BRNF_PKT_TYPE;
	}
	nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
	if (dnat_took_place(skb)) {
414
		if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
415
			struct flowi fl = {
416
417
				.fl4_dst = iph->daddr,
				.fl4_tos = RT_TOS(iph->tos),
418
			};
Eric Dumazet's avatar
Eric Dumazet committed
419
			struct in_device *in_dev = __in_dev_get_rcu(dev);
420
421
422
423
424
425
426
427
428
429

			/* If err equals -EHOSTUNREACH the error is due to a
			 * martian destination or due to the fact that
			 * forwarding is disabled. For most martian packets,
			 * ip_route_output_key() will fail. It won't fail for 2 types of
			 * martian destinations: loopback destinations and destination
			 * 0.0.0.0. In both cases the packet will be dropped because the
			 * destination is the loopback device and not the bridge. */
			if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
				goto free_skb;
Linus Torvalds's avatar
Linus Torvalds committed
430

431
432
			rt = ip_route_output_key(dev_net(dev), &fl);
			if (!IS_ERR(rt)) {
433
				/* - Bridged-and-DNAT'ed traffic doesn't
434
				 *   require ip_forwarding. */
435
436
				if (rt->dst.dev == dev) {
					skb_dst_set(skb, &rt->dst);
Linus Torvalds's avatar
Linus Torvalds committed
437
438
					goto bridged_dnat;
				}
439
				ip_rt_put(rt);
Linus Torvalds's avatar
Linus Torvalds committed
440
			}
441
free_skb:
Linus Torvalds's avatar
Linus Torvalds committed
442
443
444
			kfree_skb(skb);
			return 0;
		} else {
Eric Dumazet's avatar
Eric Dumazet committed
445
			if (skb_dst(skb)->dev == dev) {
Linus Torvalds's avatar
Linus Torvalds committed
446
447
bridged_dnat:
				skb->dev = nf_bridge->physindev;
448
				nf_bridge_update_protocol(skb);
449
				nf_bridge_push_encap_header(skb);
450
451
				NF_HOOK_THRESH(NFPROTO_BRIDGE,
					       NF_BR_PRE_ROUTING,
Linus Torvalds's avatar
Linus Torvalds committed
452
453
454
455
456
					       skb, skb->dev, NULL,
					       br_nf_pre_routing_finish_bridge,
					       1);
				return 0;
			}
457
			memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
Linus Torvalds's avatar
Linus Torvalds committed
458
459
460
			skb->pkt_type = PACKET_HOST;
		}
	} else {
Eric Dumazet's avatar
Eric Dumazet committed
461
462
		rt = bridge_parent_rtable(nf_bridge->physindev);
		if (!rt) {
463
464
465
			kfree_skb(skb);
			return 0;
		}
466
		skb_dst_set_noref(skb, &rt->dst);
Linus Torvalds's avatar
Linus Torvalds committed
467
468
469
	}

	skb->dev = nf_bridge->physindev;
470
	nf_bridge_update_protocol(skb);
471
	nf_bridge_push_encap_header(skb);
472
	NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
Linus Torvalds's avatar
Linus Torvalds committed
473
474
475
476
477
478
		       br_handle_frame_finish, 1);

	return 0;
}

/* Some common code for IPv4/IPv6 */
479
static struct net_device *setup_pre_routing(struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
480
481
482
483
484
485
486
487
488
489
490
{
	struct nf_bridge_info *nf_bridge = skb->nf_bridge;

	if (skb->pkt_type == PACKET_OTHERHOST) {
		skb->pkt_type = PACKET_HOST;
		nf_bridge->mask |= BRNF_PKT_TYPE;
	}

	nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
	nf_bridge->physindev = skb->dev;
	skb->dev = bridge_parent(skb->dev);
491
492
493
494
	if (skb->protocol == htons(ETH_P_8021Q))
		nf_bridge->mask |= BRNF_8021Q;
	else if (skb->protocol == htons(ETH_P_PPP_SES))
		nf_bridge->mask |= BRNF_PPPoE;
495
496

	return skb->dev;
Linus Torvalds's avatar
Linus Torvalds committed
497
498
499
500
501
}

/* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
static int check_hbh_len(struct sk_buff *skb)
{
502
	unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
Linus Torvalds's avatar
Linus Torvalds committed
503
	u32 pkt_len;
504
505
	const unsigned char *nh = skb_network_header(skb);
	int off = raw - nh;
506
	int len = (raw[1] + 1) << 3;
Linus Torvalds's avatar
Linus Torvalds committed
507
508
509
510
511
512
513
514

	if ((raw + len) - skb->data > skb_headlen(skb))
		goto bad;

	off += 2;
	len -= 2;

	while (len > 0) {
515
		int optlen = nh[off + 1] + 2;
Linus Torvalds's avatar
Linus Torvalds committed
516

517
		switch (nh[off]) {
Linus Torvalds's avatar
Linus Torvalds committed
518
519
520
521
522
523
524
525
		case IPV6_TLV_PAD0:
			optlen = 1;
			break;

		case IPV6_TLV_PADN:
			break;

		case IPV6_TLV_JUMBO:
526
			if (nh[off + 1] != 4 || (off & 3) != 2)
Linus Torvalds's avatar
Linus Torvalds committed
527
				goto bad;
528
			pkt_len = ntohl(*(__be32 *) (nh + off + 2));
529
			if (pkt_len <= IPV6_MAXPLEN ||
530
			    ipv6_hdr(skb)->payload_len)
531
				goto bad;
Linus Torvalds's avatar
Linus Torvalds committed
532
533
			if (pkt_len > skb->len - sizeof(struct ipv6hdr))
				goto bad;
534
			if (pskb_trim_rcsum(skb,
535
					    pkt_len + sizeof(struct ipv6hdr)))
536
				goto bad;
537
			nh = skb_network_header(skb);
Linus Torvalds's avatar
Linus Torvalds committed
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
			break;
		default:
			if (optlen > len)
				goto bad;
			break;
		}
		off += optlen;
		len -= optlen;
	}
	if (len == 0)
		return 0;
bad:
	return -1;

}

/* Replicate the checks that IPv6 does on packet reception and pass the packet
 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
557
558
559
560
					   struct sk_buff *skb,
					   const struct net_device *in,
					   const struct net_device *out,
					   int (*okfn)(struct sk_buff *))
Linus Torvalds's avatar
Linus Torvalds committed
561
562
563
564
565
{
	struct ipv6hdr *hdr;
	u32 pkt_len;

	if (skb->len < sizeof(struct ipv6hdr))
566
		return NF_DROP;
Linus Torvalds's avatar
Linus Torvalds committed
567
568

	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
569
		return NF_DROP;
Linus Torvalds's avatar
Linus Torvalds committed
570

571
	hdr = ipv6_hdr(skb);
Linus Torvalds's avatar
Linus Torvalds committed
572
573

	if (hdr->version != 6)
574
		return NF_DROP;
Linus Torvalds's avatar
Linus Torvalds committed
575
576
577
578
579

	pkt_len = ntohs(hdr->payload_len);

	if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
		if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
580
			return NF_DROP;
Herbert Xu's avatar
Herbert Xu committed
581
		if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
582
			return NF_DROP;
Linus Torvalds's avatar
Linus Torvalds committed
583
584
	}
	if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
585
		return NF_DROP;
Linus Torvalds's avatar
Linus Torvalds committed
586

587
	nf_bridge_put(skb->nf_bridge);
588
	if (!nf_bridge_alloc(skb))
Linus Torvalds's avatar
Linus Torvalds committed
589
		return NF_DROP;
590
591
	if (!setup_pre_routing(skb))
		return NF_DROP;
Linus Torvalds's avatar
Linus Torvalds committed
592

593
	skb->protocol = htons(ETH_P_IPV6);
594
	NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
Linus Torvalds's avatar
Linus Torvalds committed
595
596
597
598
599
600
601
602
603
604
605
		br_nf_pre_routing_finish_ipv6);

	return NF_STOLEN;
}

/* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
 * Replicate the checks that IPv4 does on packet reception.
 * Set skb->dev to the bridge device (i.e. parent of the
 * receiving device) to make netfilter happy, the REDIRECT
 * target in particular.  Save the original destination IP
 * address to be able to detect DNAT afterwards. */
606
static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff *skb,
607
608
609
				      const struct net_device *in,
				      const struct net_device *out,
				      int (*okfn)(struct sk_buff *))
Linus Torvalds's avatar
Linus Torvalds committed
610
{
611
612
	struct net_bridge_port *p;
	struct net_bridge *br;
613
614
615
	__u32 len = nf_bridge_encap_header_len(skb);

	if (unlikely(!pskb_may_pull(skb, len)))
616
		return NF_DROP;
Linus Torvalds's avatar
Linus Torvalds committed
617

618
	p = br_port_get_rcu(in);
619
	if (p == NULL)
620
		return NF_DROP;
621
622
	br = p->br;

623
624
	if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
	    IS_PPPOE_IPV6(skb)) {
625
		if (!brnf_call_ip6tables && !br->nf_call_ip6tables)
Linus Torvalds's avatar
Linus Torvalds committed
626
			return NF_ACCEPT;
627

628
		nf_bridge_pull_encap_header_rcsum(skb);
Linus Torvalds's avatar
Linus Torvalds committed
629
630
		return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
	}
631
632

	if (!brnf_call_iptables && !br->nf_call_iptables)
Linus Torvalds's avatar
Linus Torvalds committed
633
634
		return NF_ACCEPT;

635
636
	if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb) &&
	    !IS_PPPOE_IP(skb))
Linus Torvalds's avatar
Linus Torvalds committed
637
638
		return NF_ACCEPT;

639
	nf_bridge_pull_encap_header_rcsum(skb);
Linus Torvalds's avatar
Linus Torvalds committed
640

641
	if (br_parse_ip_options(skb))
642
		return NF_DROP;
643

644
	nf_bridge_put(skb->nf_bridge);
645
	if (!nf_bridge_alloc(skb))
Linus Torvalds's avatar
Linus Torvalds committed
646
		return NF_DROP;
647
648
	if (!setup_pre_routing(skb))
		return NF_DROP;
Linus Torvalds's avatar
Linus Torvalds committed
649
	store_orig_dstaddr(skb);
650
	skb->protocol = htons(ETH_P_IP);
Linus Torvalds's avatar
Linus Torvalds committed
651

652
	NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
Linus Torvalds's avatar
Linus Torvalds committed
653
654
655
656
657
658
659
660
661
662
663
664
665
		br_nf_pre_routing_finish);

	return NF_STOLEN;
}


/* PF_BRIDGE/LOCAL_IN ************************************************/
/* The packet is locally destined, which requires a real
 * dst_entry, so detach the fake one.  On the way up, the
 * packet would pass through PRE_ROUTING again (which already
 * took place when the packet entered the bridge), but we
 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
 * prevent this from happening. */
666
static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff *skb,
667
668
669
				   const struct net_device *in,
				   const struct net_device *out,
				   int (*okfn)(struct sk_buff *))
Linus Torvalds's avatar
Linus Torvalds committed
670
{
Eric Dumazet's avatar
Eric Dumazet committed
671
672
	struct rtable *rt = skb_rtable(skb);

Eric Dumazet's avatar
Eric Dumazet committed
673
674
	if (rt && rt == bridge_parent_rtable(in))
		skb_dst_drop(skb);
Linus Torvalds's avatar
Linus Torvalds committed
675
676
677
678
679
680
681
682
683
684

	return NF_ACCEPT;
}

/* PF_BRIDGE/FORWARD *************************************************/
static int br_nf_forward_finish(struct sk_buff *skb)
{
	struct nf_bridge_info *nf_bridge = skb->nf_bridge;
	struct net_device *in;

685
	if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
Linus Torvalds's avatar
Linus Torvalds committed
686
687
688
689
690
		in = nf_bridge->physindev;
		if (nf_bridge->mask & BRNF_PKT_TYPE) {
			skb->pkt_type = PACKET_OTHERHOST;
			nf_bridge->mask ^= BRNF_PKT_TYPE;
		}
691
		nf_bridge_update_protocol(skb);
Linus Torvalds's avatar
Linus Torvalds committed
692
693
694
	} else {
		in = *((struct net_device **)(skb->cb));
	}
695
	nf_bridge_push_encap_header(skb);
696

697
	NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, skb, in,
698
		       skb->dev, br_forward_finish, 1);
Linus Torvalds's avatar
Linus Torvalds committed
699
700
701
702
703
704
705
706
	return 0;
}

/* This is the 'purely bridged' case.  For IP, we pass the packet to
 * netfilter with indev and outdev set to the bridge device,
 * but we are still able to filter on the 'real' indev/outdev
 * because of the physdev module. For ARP, indev and outdev are the
 * bridge ports. */
707
static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
708
709
710
				     const struct net_device *in,
				     const struct net_device *out,
				     int (*okfn)(struct sk_buff *))
Linus Torvalds's avatar
Linus Torvalds committed
711
712
{
	struct nf_bridge_info *nf_bridge;
713
	struct net_device *parent;
714
	u_int8_t pf;
Linus Torvalds's avatar
Linus Torvalds committed
715
716
717
718

	if (!skb->nf_bridge)
		return NF_ACCEPT;

719
720
721
722
723
	/* Need exclusive nf_bridge_info since we might have multiple
	 * different physoutdevs. */
	if (!nf_bridge_unshare(skb))
		return NF_DROP;

724
725
726
727
	parent = bridge_parent(out);
	if (!parent)
		return NF_DROP;

728
729
	if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
	    IS_PPPOE_IP(skb))
Linus Torvalds's avatar
Linus Torvalds committed
730
		pf = PF_INET;
731
732
	else if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
		 IS_PPPOE_IPV6(skb))
Linus Torvalds's avatar
Linus Torvalds committed
733
		pf = PF_INET6;
734
735
	else
		return NF_ACCEPT;
Linus Torvalds's avatar
Linus Torvalds committed
736

737
	nf_bridge_pull_encap_header(skb);
Linus Torvalds's avatar
Linus Torvalds committed
738
739
740
741
742
743
744
745
746
747

	nf_bridge = skb->nf_bridge;
	if (skb->pkt_type == PACKET_OTHERHOST) {
		skb->pkt_type = PACKET_HOST;
		nf_bridge->mask |= BRNF_PKT_TYPE;
	}

	/* The physdev module checks on this */
	nf_bridge->mask |= BRNF_BRIDGED;
	nf_bridge->physoutdev = skb->dev;
748
749
750
751
	if (pf == PF_INET)
		skb->protocol = htons(ETH_P_IP);
	else
		skb->protocol = htons(ETH_P_IPV6);
Linus Torvalds's avatar
Linus Torvalds committed
752

753
	NF_HOOK(pf, NF_INET_FORWARD, skb, bridge_parent(in), parent,
754
		br_nf_forward_finish);
Linus Torvalds's avatar
Linus Torvalds committed
755
756
757
758

	return NF_STOLEN;
}

759
static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff *skb,
760
761
762
				      const struct net_device *in,
				      const struct net_device *out,
				      int (*okfn)(struct sk_buff *))
Linus Torvalds's avatar
Linus Torvalds committed
763
{
764
765
	struct net_bridge_port *p;
	struct net_bridge *br;
Linus Torvalds's avatar
Linus Torvalds committed
766
767
	struct net_device **d = (struct net_device **)(skb->cb);

768
	p = br_port_get_rcu(out);
769
770
771
772
773
	if (p == NULL)
		return NF_ACCEPT;
	br = p->br;

	if (!brnf_call_arptables && !br->nf_call_arptables)
Linus Torvalds's avatar
Linus Torvalds committed
774
775
		return NF_ACCEPT;

776
	if (skb->protocol != htons(ETH_P_ARP)) {
777
		if (!IS_VLAN_ARP(skb))
Linus Torvalds's avatar
Linus Torvalds committed
778
			return NF_ACCEPT;
779
		nf_bridge_pull_encap_header(skb);
Linus Torvalds's avatar
Linus Torvalds committed
780
781
	}

782
	if (arp_hdr(skb)->ar_pln != 4) {
783
		if (IS_VLAN_ARP(skb))
784
			nf_bridge_push_encap_header(skb);
Linus Torvalds's avatar
Linus Torvalds committed
785
786
787
		return NF_ACCEPT;
	}
	*d = (struct net_device *)in;
788
	NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
Linus Torvalds's avatar
Linus Torvalds committed
789
790
791
792
793
		(struct net_device *)out, br_nf_forward_finish);

	return NF_STOLEN;
}

794
#if defined(CONFIG_NF_CONNTRACK_IPV4) || defined(CONFIG_NF_CONNTRACK_IPV4_MODULE)
795
796
static int br_nf_dev_queue_xmit(struct sk_buff *skb)
{
797
798
	int ret;

799
	if (skb->nfct != NULL && skb->protocol == htons(ETH_P_IP) &&
800
	    skb->len + nf_bridge_mtu_reduction(skb) > skb->dev->mtu &&
801
	    !skb_is_gso(skb)) {
802
803
804
805
		if (br_parse_ip_options(skb))
			/* Drop invalid packet */
			return NF_DROP;
		ret = ip_fragment(skb, br_dev_queue_push_xmit);
806
	} else
807
808
809
		ret = br_dev_queue_push_xmit(skb);

	return ret;
810
}
811
812
813
814
815
816
#else
static int br_nf_dev_queue_xmit(struct sk_buff *skb)
{
        return br_dev_queue_push_xmit(skb);
}
#endif
Linus Torvalds's avatar
Linus Torvalds committed
817
818

/* PF_BRIDGE/POST_ROUTING ********************************************/
819
static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
820
821
822
				       const struct net_device *in,
				       const struct net_device *out,
				       int (*okfn)(struct sk_buff *))
Linus Torvalds's avatar
Linus Torvalds committed
823
{
824
	struct nf_bridge_info *nf_bridge = skb->nf_bridge;
Linus Torvalds's avatar
Linus Torvalds committed
825
	struct net_device *realoutdev = bridge_parent(skb->dev);
826
	u_int8_t pf;
Linus Torvalds's avatar
Linus Torvalds committed
827

828
	if (!nf_bridge || !(nf_bridge->mask & BRNF_BRIDGED))
829
830
		return NF_ACCEPT;

831
832
833
	if (!realoutdev)
		return NF_DROP;

834
835
	if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
	    IS_PPPOE_IP(skb))
Linus Torvalds's avatar
Linus Torvalds committed
836
		pf = PF_INET;
837
838
	else if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
		 IS_PPPOE_IPV6(skb))
Linus Torvalds's avatar
Linus Torvalds committed
839
		pf = PF_INET6;
840
841
	else
		return NF_ACCEPT;
Linus Torvalds's avatar
Linus Torvalds committed
842
843
844
845
846
847
848
849

	/* We assume any code from br_dev_queue_push_xmit onwards doesn't care
	 * about the value of skb->pkt_type. */
	if (skb->pkt_type == PACKET_OTHERHOST) {
		skb->pkt_type = PACKET_HOST;
		nf_bridge->mask |= BRNF_PKT_TYPE;
	}

850
	nf_bridge_pull_encap_header(skb);
Linus Torvalds's avatar
Linus Torvalds committed
851
	nf_bridge_save_header(skb);
852
853
854
855
	if (pf == PF_INET)
		skb->protocol = htons(ETH_P_IP);
	else
		skb->protocol = htons(ETH_P_IPV6);
Linus Torvalds's avatar
Linus Torvalds committed
856

857
	NF_HOOK(pf, NF_INET_POST_ROUTING, skb, NULL, realoutdev,
858
		br_nf_dev_queue_xmit);
Linus Torvalds's avatar
Linus Torvalds committed
859
860
861
862
863
864
865

	return NF_STOLEN;
}

/* IP/SABOTAGE *****************************************************/
/* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
 * for the second time. */
866
static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff *skb,
867
868
869
				   const struct net_device *in,
				   const struct net_device *out,
				   int (*okfn)(struct sk_buff *))
Linus Torvalds's avatar
Linus Torvalds committed
870
{
871
872
	if (skb->nf_bridge &&
	    !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
Linus Torvalds's avatar
Linus Torvalds committed
873
874
875
876
877
878
		return NF_STOP;
	}

	return NF_ACCEPT;
}

879
880
/* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
 * br_dev_queue_push_xmit is called afterwards */
881
static struct nf_hook_ops br_nf_ops[] __read_mostly = {
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
	{
		.hook = br_nf_pre_routing,
		.owner = THIS_MODULE,
		.pf = PF_BRIDGE,
		.hooknum = NF_BR_PRE_ROUTING,
		.priority = NF_BR_PRI_BRNF,
	},
	{
		.hook = br_nf_local_in,
		.owner = THIS_MODULE,
		.pf = PF_BRIDGE,
		.hooknum = NF_BR_LOCAL_IN,
		.priority = NF_BR_PRI_BRNF,
	},
	{
		.hook = br_nf_forward_ip,
		.owner = THIS_MODULE,
		.pf = PF_BRIDGE,
		.hooknum = NF_BR_FORWARD,
		.priority = NF_BR_PRI_BRNF - 1,
	},
	{
		.hook = br_nf_forward_arp,
		.owner = THIS_MODULE,
		.pf = PF_BRIDGE,
		.hooknum = NF_BR_FORWARD,
		.priority = NF_BR_PRI_BRNF,
	},
	{
		.hook = br_nf_post_routing,
		.owner = THIS_MODULE,
		.pf = PF_BRIDGE,
		.hooknum = NF_BR_POST_ROUTING,
		.priority = NF_BR_PRI_LAST,
	},
	{
		.hook = ip_sabotage_in,
		.owner = THIS_MODULE,
		.pf = PF_INET,
		.hooknum = NF_INET_PRE_ROUTING,
		.priority = NF_IP_PRI_FIRST,
	},
	{
		.hook = ip_sabotage_in,
		.owner = THIS_MODULE,
		.pf = PF_INET6,
		.hooknum = NF_INET_PRE_ROUTING,
		.priority = NF_IP6_PRI_FIRST,
	},
Linus Torvalds's avatar
Linus Torvalds committed
931
932
933
934
};

#ifdef CONFIG_SYSCTL
static
935
int brnf_sysctl_call_tables(ctl_table * ctl, int write,
936
			    void __user * buffer, size_t * lenp, loff_t * ppos)
Linus Torvalds's avatar
Linus Torvalds committed
937
938
939
{
	int ret;

940
	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Linus Torvalds's avatar
Linus Torvalds committed
941
942
943
944
945
946
947
948
949
950
951
952

	if (write && *(int *)(ctl->data))
		*(int *)(ctl->data) = 1;
	return ret;
}

static ctl_table brnf_table[] = {
	{
		.procname	= "bridge-nf-call-arptables",
		.data		= &brnf_call_arptables,
		.maxlen		= sizeof(int),
		.mode		= 0644,
Alexey Dobriyan's avatar
Alexey Dobriyan committed
953
		.proc_handler	= brnf_sysctl_call_tables,
Linus Torvalds's avatar
Linus Torvalds committed
954
955
956
957
958
959
	},
	{
		.procname	= "bridge-nf-call-iptables",
		.data		= &brnf_call_iptables,
		.maxlen		= sizeof(int),
		.mode		= 0644,
Alexey Dobriyan's avatar
Alexey Dobriyan committed
960
		.proc_handler	= brnf_sysctl_call_tables,
Linus Torvalds's avatar
Linus Torvalds committed
961
962
963
964
965
966
	},
	{
		.procname	= "bridge-nf-call-ip6tables",
		.data		= &brnf_call_ip6tables,
		.maxlen		= sizeof(int),
		.mode		= 0644,
Alexey Dobriyan's avatar
Alexey Dobriyan committed
967
		.proc_handler	= brnf_sysctl_call_tables,
Linus Torvalds's avatar
Linus Torvalds committed
968
969
970
971
972
973
	},
	{
		.procname	= "bridge-nf-filter-vlan-tagged",
		.data		= &brnf_filter_vlan_tagged,
		.maxlen		= sizeof(int),
		.mode		= 0644,
Alexey Dobriyan's avatar
Alexey Dobriyan committed
974
		.proc_handler	= brnf_sysctl_call_tables,
975
976
977
978
979
980
	},
	{
		.procname	= "bridge-nf-filter-pppoe-tagged",
		.data		= &brnf_filter_pppoe_tagged,
		.maxlen		= sizeof(int),
		.mode		= 0644,
Alexey Dobriyan's avatar
Alexey Dobriyan committed
981
		.proc_handler	= brnf_sysctl_call_tables,
Linus Torvalds's avatar
Linus Torvalds committed
982
	},
983
	{ }
Linus Torvalds's avatar
Linus Torvalds committed
984
985
};

986
static struct ctl_path brnf_path[] = {
987
988
	{ .procname = "net", },
	{ .procname = "bridge", },
989
	{ }
Linus Torvalds's avatar
Linus Torvalds committed
990
991
992
};
#endif

993
int __init br_netfilter_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
994
{
995
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
996

997
	ret = dst_entries_init(&fake_dst_ops);
998
	if (ret < 0)
Linus Torvalds's avatar
Linus Torvalds committed
999
		return ret;
1000
1001
1002
1003
1004
1005

	ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
	if (ret < 0) {
		dst_entries_destroy(&fake_dst_ops);
		return ret;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1006
#ifdef CONFIG_SYSCTL
1007
	brnf_sysctl_header = register_sysctl_paths(brnf_path, brnf_table);
Linus Torvalds's avatar
Linus Torvalds committed
1008
	if (brnf_sysctl_header == NULL) {
1009
1010
		printk(KERN_WARNING
		       "br_netfilter: can't register to sysctl.\n");
1011
		nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1012
		dst_entries_destroy(&fake_dst_ops);
1013
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
1014
1015
1016
1017
1018
1019
1020
1021
	}
#endif
	printk(KERN_NOTICE "Bridge firewalling registered\n");
	return 0;
}

void br_netfilter_fini(void)
{
1022
	nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
Linus Torvalds's avatar
Linus Torvalds committed
1023
1024
1025
#ifdef CONFIG_SYSCTL
	unregister_sysctl_table(brnf_sysctl_header);
#endif
1026
	dst_entries_destroy(&fake_dst_ops);
Linus Torvalds's avatar
Linus Torvalds committed
1027
}